:root {
    color-scheme: dark;
    --bg: #0b1017;
    --surface: #121a24;
    --surface-2: #182230;
    --surface-3: #202d3d;
    --line: #29384a;
    --text: #eef4fa;
    --muted: #91a2b6;
    --primary: #42d6a4;
    --primary-dark: #1a9d78;
    --blue: #69a9ff;
    --orange: #ffad4d;
    --red: #ff5f6d;
    --shadow: 0 18px 60px rgba(0, 0, 0, .24);
    --radius: 14px;
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html { min-height: 100%; background: var(--bg); }
body { min-height: 100vh; margin: 0; color: var(--text); background: radial-gradient(circle at 95% -5%, rgba(66, 214, 164, .09), transparent 28rem), var(--bg); }
button, input, select, textarea { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
a { color: inherit; text-decoration: none; }
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: .4rem; font-size: clamp(1.65rem, 3vw, 2.25rem); letter-spacing: -.035em; }
h2 { margin-bottom: 1rem; font-size: 1.1rem; }
h3 { margin-bottom: .25rem; font-size: 1rem; }
.muted, .form-help { color: var(--muted); }
.form-help { margin: .6rem 0 0; font-size: .82rem; line-height: 1.5; }
.eyebrow { margin-bottom: .35rem; color: var(--primary); font-size: .7rem; font-weight: 800; letter-spacing: .14em; text-transform: uppercase; }
.hidden { display: none !important; }

.app-header { position: sticky; z-index: 20; top: 0; display: flex; align-items: center; justify-content: space-between; min-height: 72px; padding: 0 clamp(1rem, 4vw, 3rem); border-bottom: 1px solid var(--line); background: rgba(11, 16, 23, .88); backdrop-filter: blur(16px); }
.brand { display: inline-flex; gap: .75rem; align-items: center; }
.brand strong, .brand small { display: block; }
.brand strong { letter-spacing: -.02em; }
.brand small { color: var(--muted); font-size: .72rem; }
.brand-mark { display: grid; width: 38px; height: 38px; place-items: center; border-radius: 10px; color: #062219; background: linear-gradient(135deg, #69ebbc, #27b989); font-size: 1.15rem; font-weight: 900; box-shadow: 0 6px 24px rgba(66, 214, 164, .22); }
.header-meta { display: flex; gap: .85rem; align-items: center; color: var(--muted); font-size: .8rem; }
.user-chip { padding: .35rem .7rem; border: 1px solid var(--line); border-radius: 999px; color: var(--text); background: var(--surface); }

.tab-nav { position: sticky; z-index: 15; top: 72px; display: flex; gap: .3rem; padding: .7rem clamp(1rem, 4vw, 3rem) 0; overflow-x: auto; border-bottom: 1px solid var(--line); background: rgba(11, 16, 23, .94); }
.tab { padding: .75rem 1rem .9rem; border: 0; border-bottom: 2px solid transparent; color: var(--muted); background: transparent; cursor: pointer; font-weight: 700; white-space: nowrap; }
.tab:hover { color: var(--text); }
.tab.active { border-bottom-color: var(--primary); color: var(--primary); }

.app-main { width: min(1500px, 100%); margin: 0 auto; padding: clamp(1rem, 3vw, 2.25rem) clamp(1rem, 4vw, 3rem) 4rem; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }
.panel { border: 1px solid var(--line); border-radius: var(--radius); background: linear-gradient(145deg, rgba(24, 34, 48, .95), rgba(18, 26, 36, .95)); box-shadow: var(--shadow); }
.panel-heading, .section-heading { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.panel-heading { margin-bottom: 1rem; }
.panel-heading h2 { margin: 0; }
.section-heading { margin-bottom: 1.5rem; }

.summary-grid { display: grid; grid-template-columns: repeat(4, minmax(150px, 1fr)); gap: .8rem; margin-bottom: 1rem; }
.summary-card { position: relative; overflow: hidden; padding: 1.15rem; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.summary-card::after { position: absolute; right: -1.5rem; bottom: -2.5rem; width: 6rem; height: 6rem; border-radius: 50%; background: currentColor; content: ""; opacity: .055; }
.summary-card span { display: block; color: var(--muted); font-size: .75rem; font-weight: 700; text-transform: uppercase; }
.summary-card strong { display: block; margin-top: .25rem; font-size: 2rem; letter-spacing: -.05em; }
.summary-card.problem { color: var(--red); }
.summary-card.warning { color: var(--orange); }
.summary-card.online { color: var(--primary); }

.toolbar { display: grid; grid-template-columns: minmax(220px, 1fr) repeat(3, auto) auto; gap: .8rem; align-items: end; padding: 1rem; margin-bottom: 1.5rem; box-shadow: none; }
label { display: grid; gap: .4rem; color: var(--muted); font-size: .78rem; font-weight: 700; }
input, select, textarea { width: 100%; border: 1px solid var(--line); border-radius: 9px; outline: 0; color: var(--text); background: #0e151e; transition: border-color .15s, box-shadow .15s; }
input, select { min-height: 42px; padding: .55rem .7rem; }
textarea { padding: .7rem; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--primary-dark); box-shadow: 0 0 0 3px rgba(66, 214, 164, .1); }

.button { display: inline-flex; min-height: 40px; padding: .55rem .9rem; align-items: center; justify-content: center; border: 1px solid transparent; border-radius: 9px; cursor: pointer; font-weight: 800; transition: transform .12s, border-color .12s, background .12s; }
.button:hover { transform: translateY(-1px); }
.button:disabled { cursor: wait; opacity: .55; transform: none; }
.button-primary { color: #062219; background: var(--primary); }
.button-primary:hover { background: #62e4b8; }
.button-secondary { border-color: var(--line); color: var(--text); background: var(--surface-3); }
.button-ghost { border-color: var(--line); color: var(--muted); background: transparent; }
.button-danger { border-color: rgba(255, 95, 109, .35); color: #ff9ba4; background: rgba(255, 95, 109, .09); }
.button-small { min-height: 32px; padding: .35rem .65rem; font-size: .75rem; }
.button-wide { width: 100%; }
.button-row { display: flex; flex-wrap: wrap; gap: .6rem; margin-top: 1rem; }
.icon-button { display: grid; width: 34px; height: 34px; place-items: center; border: 1px solid var(--line); border-radius: 8px; color: var(--muted); background: transparent; cursor: pointer; font-size: 1.4rem; }

.device-groups { display: grid; gap: 1.75rem; }
.group-heading { display: flex; justify-content: space-between; align-items: center; margin-bottom: .75rem; padding-bottom: .6rem; border-bottom: 1px solid var(--line); }
.group-heading h2 { margin: 0; font-size: 1rem; }
.group-heading span { color: var(--muted); font-size: .75rem; }
.device-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(335px, 1fr)); gap: 1rem; }
.device-card { position: relative; overflow: hidden; padding: 1rem; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); box-shadow: 0 12px 32px rgba(0, 0, 0, .16); }
.device-card::before { position: absolute; top: 0; left: 0; width: 4px; height: 100%; background: var(--primary); content: ""; }
.device-card.warning::before { background: var(--orange); }
.device-card.problem { border-color: rgba(255, 95, 109, .6); background: linear-gradient(145deg, rgba(86, 24, 31, .38), var(--surface)); animation: problem-pulse 1.65s ease-in-out infinite; }
.device-card.problem::before { width: 6px; background: var(--red); }
.device-card.problem h3 { font-weight: 900; }
@keyframes problem-pulse { 50% { border-color: rgba(255, 95, 109, .2); box-shadow: 0 0 26px rgba(255, 95, 109, .08); } }
@media (prefers-reduced-motion: reduce) { .device-card.problem { animation: none; } * { scroll-behavior: auto !important; } }
.device-head { display: flex; gap: .7rem; align-items: flex-start; justify-content: space-between; padding-left: .3rem; }
.device-head p { margin: 0; color: var(--muted); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .68rem; overflow-wrap: anywhere; }
.status-badge { padding: .3rem .55rem; border-radius: 999px; color: #07241b; background: var(--primary); font-size: .67rem; font-weight: 900; white-space: nowrap; }
.status-badge.problem { color: white; background: var(--red); }
.status-badge.warning { color: #281704; background: var(--orange); }
.status-badge.new { color: var(--text); background: var(--surface-3); }
.status-badge.maintenance { color: #0b2342; background: var(--blue); }
.problem-reasons { margin: .75rem .3rem 0; padding: .55rem .65rem; border-radius: 8px; color: #ffc3c8; background: rgba(255, 95, 109, .11); font-size: .75rem; font-weight: 800; }
.device-metrics { display: grid; grid-template-columns: repeat(3, 1fr); gap: .5rem; margin: .9rem .3rem; }
.metric { padding: .65rem; border-radius: 9px; background: rgba(7, 11, 17, .5); }
.metric span { display: block; color: var(--muted); font-size: .64rem; font-weight: 700; text-transform: uppercase; }
.metric strong { display: block; margin-top: .2rem; font-size: .95rem; }
.metric.low strong { color: var(--orange); }
.device-details { margin: 0 .3rem; }
.detail-row { display: flex; gap: .8rem; justify-content: space-between; padding: .38rem 0; border-bottom: 1px solid rgba(41, 56, 74, .55); font-size: .74rem; }
.detail-row span:first-child { color: var(--muted); }
.detail-row span:last-child { max-width: 68%; text-align: right; overflow-wrap: anywhere; }
.consumables { display: flex; flex-wrap: wrap; gap: .4rem; margin: .75rem .3rem; }
.consumable { padding: .38rem .55rem; border: 1px solid var(--line); border-radius: 8px; color: var(--muted); background: transparent; cursor: pointer; font-size: .68rem; }
.consumable.low { border-color: rgba(255, 173, 77, .55); color: var(--orange); background: rgba(255, 173, 77, .08); }
.consumable.ignored { border-style: dashed; color: #718094; opacity: .68; }
.device-actions { display: flex; flex-wrap: wrap; gap: .4rem; padding: .85rem .3rem 0; }
.device-actions .button { min-height: 32px; padding: .35rem .55rem; font-size: .68rem; }
.empty-state { padding: 3rem 1rem; border: 1px dashed var(--line); border-radius: var(--radius); color: var(--muted); text-align: center; }

.config-layout { display: grid; grid-template-columns: minmax(230px, 310px) minmax(0, 1fr); gap: 1rem; }
.config-sidebar, .config-editor-panel { padding: 1rem; }
.config-list { display: grid; gap: .45rem; }
.config-item { width: 100%; padding: .75rem; border: 1px solid var(--line); border-radius: 9px; color: var(--text); background: transparent; cursor: pointer; text-align: left; }
.config-item:hover, .config-item.active { border-color: var(--primary-dark); background: rgba(66, 214, 164, .06); }
.config-item strong, .config-item small { display: block; }
.config-item small { margin-top: .2rem; color: var(--muted); }
.form-row { display: flex; gap: 1rem; align-items: end; }
.form-row-main label { flex: 1; }
.version-chip { padding: .65rem .8rem; border: 1px solid var(--line); border-radius: 9px; color: var(--muted); background: var(--surface); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .75rem; }
.code-editor { min-height: 540px; margin-top: .4rem; tab-size: 2; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .78rem; line-height: 1.55; }
.config-segments { display: grid; gap: .75rem; margin-top: 1.25rem; }
.config-segment { overflow: hidden; border: 1px solid var(--line); border-radius: 11px; background: rgba(7, 11, 17, .28); }
.config-segment summary { display: flex; gap: 1rem; align-items: center; justify-content: space-between; padding: 1rem; cursor: pointer; list-style: none; user-select: none; }
.config-segment summary::-webkit-details-marker { display: none; }
.config-segment summary::after { margin-left: auto; color: var(--muted); content: "+"; font-size: 1.35rem; font-weight: 400; }
.config-segment[open] summary::after { content: "−"; }
.config-segment summary > span:first-child { display: grid; gap: .18rem; }
.config-segment summary strong { color: var(--text); font-size: .92rem; }
.config-segment summary small { color: var(--muted); font-size: .7rem; font-weight: 500; }
.segment-badge { padding: .25rem .5rem; border-radius: 999px; color: #062219; background: var(--primary); font-size: .63rem; font-weight: 900; text-transform: uppercase; }
.config-segment-body { padding: 1rem; border-top: 1px solid var(--line); }
.config-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.config-grid .field-wide { grid-column: 1 / -1; }
.check-field { grid-template-columns: auto 1fr; align-content: center; align-items: center; min-height: 42px; padding: .55rem .7rem; border: 1px solid var(--line); border-radius: 9px; color: var(--text); background: #0e151e; }
.check-field input { width: 18px; min-height: 18px; margin: 0; }
.input-help { color: var(--muted); font-size: .68rem; font-weight: 500; }
#cfg-custom-css, #cfg-custom-js { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .76rem; line-height: 1.5; }

.history-section { margin-top: 2rem; padding-top: 1.25rem; border-top: 1px solid var(--line); }
.history-list { display: grid; gap: .4rem; }
.history-item { display: grid; grid-template-columns: auto 1fr auto; gap: .7rem; align-items: center; padding: .6rem; border-radius: 8px; background: rgba(7, 11, 17, .38); font-size: .75rem; }
.history-item code { color: var(--primary); }

.settings-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.settings-grid > .panel { padding: 1rem; }
.data-list { display: grid; gap: .45rem; }
.data-row { display: flex; gap: .8rem; align-items: center; justify-content: space-between; padding: .7rem; border-radius: 9px; background: rgba(7, 11, 17, .38); }
.data-row-actions { display: flex; gap: .4rem; }
.data-row-metrics { display: flex; gap: 1rem; color: var(--muted); font-size: .78rem; }
.inline-form { display: flex; gap: .5rem; margin-bottom: 1rem; }
.stack-form { display: grid; gap: 1rem; }
.stack-form.compact { max-width: 500px; }

.alert { margin-bottom: 1rem; padding: .75rem .9rem; border: 1px solid; border-radius: 9px; font-size: .8rem; line-height: 1.45; }
.alert-error { border-color: rgba(255, 95, 109, .4); color: #ffc4c9; background: rgba(255, 95, 109, .1); }
.alert-warning { border-color: rgba(255, 173, 77, .35); color: #ffd5a3; background: rgba(255, 173, 77, .08); }

dialog { width: min(520px, calc(100% - 2rem)); padding: 0; border: 1px solid var(--line); border-radius: var(--radius); color: var(--text); background: var(--surface); box-shadow: 0 24px 90px rgba(0, 0, 0, .6); }
dialog::backdrop { background: rgba(3, 6, 10, .75); backdrop-filter: blur(5px); }
.dialog-form { display: grid; gap: 1rem; padding: 1.25rem; }
.dialog-heading { display: flex; align-items: flex-start; justify-content: space-between; }
.dialog-heading h2 { margin: 0; font-size: 1.35rem; }
.check-row { grid-template-columns: auto 1fr; justify-content: start; align-items: center; color: var(--text); }
.check-row input { width: 20px; min-height: 20px; }
.toast { position: fixed; z-index: 100; right: 1.25rem; bottom: 1.25rem; max-width: min(420px, calc(100% - 2rem)); padding: .8rem 1rem; border: 1px solid var(--line); border-radius: 10px; opacity: 0; color: var(--text); background: var(--surface-3); box-shadow: var(--shadow); pointer-events: none; transform: translateY(10px); transition: opacity .2s, transform .2s; }
.toast.visible { opacity: 1; transform: translateY(0); }
.toast.error { border-color: var(--red); }

.login-page { display: grid; place-items: center; }
.login-shell { width: min(420px, calc(100% - 2rem)); padding: 2rem 0; }
.login-card { padding: 2rem; border: 1px solid var(--line); border-radius: 18px; background: linear-gradient(145deg, var(--surface-2), var(--surface)); box-shadow: var(--shadow); }
.login-card .brand-mark { margin-bottom: 1.5rem; }
.login-card h1 { margin-bottom: .25rem; }
.login-card > .muted { margin-bottom: 1.5rem; }

@media (max-width: 950px) {
    .summary-grid { grid-template-columns: repeat(2, 1fr); }
    .toolbar { grid-template-columns: 1fr 1fr; }
    .search-field { grid-column: 1 / -1; }
    .config-layout { grid-template-columns: 1fr; }
    .settings-grid { grid-template-columns: 1fr; }
}

@media (max-width: 620px) {
    .app-header { min-height: 62px; }
    .tab-nav { top: 62px; }
    .header-meta > span:not(.user-chip) { display: none; }
    .user-chip { display: none; }
    .toolbar { grid-template-columns: 1fr; }
    .search-field { grid-column: auto; }
    .summary-grid { grid-template-columns: 1fr 1fr; }
    .device-grid { grid-template-columns: 1fr; }
    .device-metrics { grid-template-columns: 1fr 1fr; }
    .form-row { display: grid; }
    .config-grid { grid-template-columns: 1fr; }
    .config-grid .field-wide { grid-column: auto; }
    .inline-form { flex-wrap: wrap; }
}
