@layer tokens, reset, base, components, layout, pages, utilities;

@layer tokens {
  :root {
    color-scheme: light;
    --font: Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
    --bg: #d8e3ea;
    --bg-deep: #c9d7e0;
    --surface: #e5edf2;
    --surface-raised: #edf3f6;
    --surface-sunken: #cfdae2;
    --surface-hover: #d9e8ef;
    --text: #172a38;
    --text-soft: #526b7c;
    --text-faint: #718796;
    --line: #b9cad4;
    --line-soft: #c9d7df;
    --primary: #229ed9;
    --primary-hover: #168fc8;
    --primary-soft: #c5e8f7;
    --success: #1f9d78;
    --success-soft: #ccefe3;
    --warning: #c27a1d;
    --warning-soft: #f5e2c3;
    --danger: #c64d62;
    --danger-soft: #f3d6dc;
    --shadow-lg: 12px 14px 30px rgba(75, 101, 119, .22), -8px -8px 20px rgba(255, 255, 255, .5);
    --shadow-md: 7px 8px 18px rgba(75, 101, 119, .18), -5px -5px 13px rgba(255, 255, 255, .46);
    --shadow-sm: 4px 5px 11px rgba(75, 101, 119, .16), -3px -3px 8px rgba(255, 255, 255, .42);
    --inset: inset 3px 3px 7px rgba(75, 101, 119, .2), inset -3px -3px 7px rgba(255, 255, 255, .56);
    --focus: 0 0 0 3px rgba(34, 158, 217, .28);
    --radius-xl: 26px;
    --radius-lg: 20px;
    --radius-md: 14px;
    --radius-sm: 10px;
  }
  html[data-theme="dark"] {
    color-scheme: dark;
    --bg: #172632;
    --bg-deep: #111f2a;
    --surface: #223542;
    --surface-raised: #293f4f;
    --surface-sunken: #182936;
    --surface-hover: #29495b;
    --text: #f1f7fa;
    --text-soft: #bfd0da;
    --text-faint: #91a8b7;
    --line: #365164;
    --line-soft: #2e4657;
    --primary-soft: #1d4c63;
    --success-soft: #1f4b40;
    --warning-soft: #554326;
    --danger-soft: #57313b;
    --shadow-lg: 12px 14px 30px rgba(3, 9, 14, .5), -6px -6px 16px rgba(77, 105, 125, .12);
    --shadow-md: 7px 8px 18px rgba(3, 9, 14, .42), -4px -4px 11px rgba(77, 105, 125, .1);
    --shadow-sm: 4px 5px 11px rgba(3, 9, 14, .38), -2px -2px 7px rgba(77, 105, 125, .09);
    --inset: inset 3px 3px 7px rgba(3, 9, 14, .46), inset -2px -2px 6px rgba(77, 105, 125, .12);
  }
}

@layer reset {
  *, *::before, *::after { box-sizing: border-box; }
  html { min-height: 100%; background: var(--bg-deep); }
  body { min-height: 100vh; margin: 0; }
  button, input, select { font: inherit; }
  button, a { -webkit-tap-highlight-color: transparent; }
  button { cursor: pointer; }
  img { max-width: 100%; }
  h1, h2, h3, p { margin: 0; }
  [hidden] { display: none !important; }
}

@layer base {
  body {
    color: var(--text);
    background:
      radial-gradient(circle at 88% -8%, rgba(34, 158, 217, .14), transparent 30%),
      radial-gradient(circle at -10% 90%, rgba(31, 157, 120, .07), transparent 26%),
      linear-gradient(145deg, var(--bg-deep), var(--bg));
    font: 14px/1.45 var(--font);
  }
  body, button, input, select, a { color: var(--text); }
  ::selection { color: #fff; background: var(--primary); }
  :focus-visible { outline: none; box-shadow: var(--focus) !important; }
  * { scrollbar-width: thin; scrollbar-color: var(--line) transparent; }
  svg { width: 1em; height: 1em; fill: currentColor; }
  a { text-decoration: none; }
  .subtle { color: var(--text-soft); }
  .kicker { margin-bottom: 5px; color: var(--primary); font-size: 10px; font-weight: 850; letter-spacing: 1.55px; }
  h1 { font-size: clamp(25px, 2.4vw, 32px); line-height: 1.05; letter-spacing: -.9px; }
  h2 { font-size: 20px; line-height: 1.2; letter-spacing: -.45px; }
  h3 { font-size: 15px; line-height: 1.25; }
}

@layer components {
  .panel, .metric-card, .app-sidebar, .app-header {
    border: 1px solid var(--line-soft);
    background: linear-gradient(145deg, var(--surface-raised), var(--surface));
    box-shadow: var(--shadow-lg);
  }
  .panel { padding: 20px; border-radius: var(--radius-lg); }
  .button, .icon-control {
    display: inline-flex; align-items: center; justify-content: center; gap: 7px;
    min-height: 40px; padding: 9px 14px; border: 1px solid var(--line); border-radius: var(--radius-sm);
    background: linear-gradient(145deg, var(--surface-raised), var(--surface)); box-shadow: var(--shadow-sm);
    color: var(--text); font-weight: 740; transition: transform .15s ease, background .15s ease, box-shadow .15s ease;
  }
  .button:hover, .icon-control:hover { background: var(--surface-hover); transform: translateY(-1px); }
  .button:active, .icon-control:active { transform: translateY(1px); box-shadow: var(--inset); }
  .button:disabled, .icon-control:disabled { cursor: not-allowed; opacity: .48; transform: none; }
  .button-primary { border-color: transparent; color: #fff; background: linear-gradient(145deg, #45b8f4, var(--primary)); box-shadow: 5px 7px 15px rgba(19, 122, 171, .28); }
  .button-primary:hover { background: linear-gradient(145deg, #38ace7, var(--primary-hover)); }
  .button-danger { border-color: transparent; color: #fff; background: linear-gradient(145deg, #da7183, #c9566b); box-shadow: 4px 6px 13px rgba(172, 59, 81, .2); }
  .button.button-danger:hover { border-color: transparent; color: #fff; background: linear-gradient(145deg, #cf5b72, #bd455d); box-shadow: 5px 7px 15px rgba(172, 59, 81, .27); }
  .button-icon, .icon-control { width: 40px; padding: 0; }
  .button-icon svg, .icon-control svg { width: 18px; height: 18px; }
  .link-button, .text-action { padding: 3px; border: 0; background: transparent; color: var(--primary); font-weight: 760; }
  .text-action:hover, .link-button:hover { color: var(--primary-hover); }
  input:not([type="range"]), select {
    width: 100%; min-height: 42px; padding: 10px 12px; border: 1px solid var(--line); border-radius: var(--radius-sm);
    outline: 0; color: var(--text); background: var(--surface-sunken); box-shadow: var(--inset);
  }
  select { color-scheme: inherit; }
  select option { color: var(--text); background: var(--surface); }
  input::placeholder { color: var(--text-faint); }
  input[readonly] { color: var(--text-soft); }
  label { display: grid; gap: 6px; color: var(--text-soft); font-size: 12px; font-weight: 750; }
  label small { color: var(--text-faint); font-size: 10px; font-weight: 520; }
  .check-control { display: inline-flex; align-items: center; width: max-content; max-width: 100%; gap: 10px; padding: 8px 10px; border-radius: var(--radius-sm); background: var(--surface-sunken); }
  .check-control input { appearance: none; display: grid; place-content: center; flex: 0 0 18px; width: 18px !important; height: 18px; min-height: 18px !important; margin: 0; padding: 0 !important; border: 1px solid var(--line); border-radius: 5px; background: var(--surface); box-shadow: var(--inset); }
  .check-control input::before { content: ""; width: 8px; height: 4px; border: solid #fff; border-width: 0 0 2px 2px; transform: translateY(-1px) rotate(-45deg) scale(0); transition: transform .12s ease; }
  .check-control input:checked { border-color: var(--primary); background: var(--primary); box-shadow: 0 3px 8px rgba(34, 158, 217, .28); }
  .check-control input:checked::before { transform: translateY(-1px) rotate(-45deg) scale(1); }
  .check-control span { display: grid; gap: 1px; }
  .check-control b { color: var(--text); font-size: 12px; }
  .badge, .nav-badge, .status-badge { display: inline-flex; align-items: center; justify-content: center; border-radius: 99px; font-size: 10px; font-weight: 850; }
  .badge { min-height: 26px; padding: 4px 9px; color: var(--text-soft); background: var(--surface-sunken); }
  .status-badge { min-height: 23px; padding: 4px 8px; letter-spacing: .55px; text-transform: uppercase; }
  .status-online { color: #11755a; background: var(--success-soft); }
  .status-idle { color: #936018; background: var(--warning-soft); }
  .status-offline, .status-uninstalled { color: var(--text-soft); background: var(--surface-sunken); }
  .status-pending { color: #936018; background: var(--warning-soft); }
  .department-badge { max-width: 110px; min-height: 23px; padding: 4px 8px; overflow: hidden; color: var(--primary-strong); background: var(--primary-soft); letter-spacing: 0; text-overflow: ellipsis; text-transform: none; white-space: nowrap; }
  html[data-theme="dark"] .status-online { color: #78e3c1; }
  html[data-theme="dark"] .status-idle, html[data-theme="dark"] .status-pending { color: #f2c275; }
  .empty-state { display: grid; place-items: center; min-height: 160px; padding: 28px; border: 1px dashed var(--line); border-radius: var(--radius-lg); color: var(--text-soft); text-align: center; }
  .panel-header { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin-bottom: 15px; }
  .form-stack { display: grid; gap: 13px; margin-top: 18px; }
  .form-grid { display: grid; grid-template-columns: repeat(3, minmax(150px, 1fr)); gap: 13px; margin-top: 18px; }
  .policy-grid > label:not(.check-control), #storage-form > label:not(.check-control) { grid-template-rows: auto 42px minmax(28px, auto); align-content: start; }
  .policy-grid .field-label, #storage-form .field-label { align-self: end; min-height: 18px; }
  .policy-grid > label > small, #storage-form > label > small { align-self: start; margin: 0; line-height: 1.35; }
  .policy-grid .check-control { align-self: start; }
  .form-actions { grid-column: 1 / -1; display: flex; align-items: center; gap: 12px; }
  .form-result { min-height: 18px; color: var(--primary); font-size: 12px; }
  .storage-actions { flex-wrap: wrap; }
  .storage-guidance { margin-top: 18px; padding: 16px 18px; border-radius: var(--radius-md); background: var(--surface-sunken); box-shadow: var(--inset); }
  .storage-guidance p { margin-top: 6px; color: var(--text-soft); }
  .segmented { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; padding: 4px; border-radius: 12px; background: var(--surface-sunken); box-shadow: var(--inset); }
  .segmented button { min-height: 32px; padding: 6px 10px; border: 0; border-radius: 8px; color: var(--text-soft); background: transparent; font-size: 11px; font-weight: 750; }
  .segmented button[aria-pressed="true"] { color: #fff; background: var(--primary); box-shadow: 3px 4px 9px rgba(19, 122, 171, .28); }
  .toast-region { position: fixed; z-index: 500; right: 18px; bottom: 18px; display: grid; gap: 9px; width: min(380px, calc(100vw - 36px)); }
  .toast { padding: 12px 14px; border: 1px solid var(--line); border-radius: var(--radius-md); background: var(--surface-raised); box-shadow: var(--shadow-lg); }
  .toast.success { border-left: 4px solid var(--success); }
  .toast.error { border-left: 4px solid var(--danger); }
  .dialog-backdrop { position: fixed; inset: 0; z-index: 450; display: grid; place-items: center; padding: 18px; background: rgba(7, 16, 23, .64); backdrop-filter: blur(8px); }
  .dialog { width: min(460px, 100%); padding: 24px; border: 1px solid var(--line); border-radius: var(--radius-lg); background: linear-gradient(145deg, var(--surface-raised), var(--surface)); box-shadow: var(--shadow-lg); }
  .dialog p { margin-top: 8px; color: var(--text-soft); }
  .dialog label { margin-top: 17px; }
  .dialog-error { min-height: 18px; margin-top: 6px; color: var(--danger) !important; }
  .dialog-actions { display: flex; justify-content: flex-end; gap: 9px; margin-top: 18px; }
}

@layer layout {
  .app-shell { display: grid; grid-template-columns: 220px minmax(0, 1fr); gap: 20px; width: min(2200px, 100%); min-height: 100vh; margin: auto; padding: 18px; }
  .app-sidebar { position: sticky; top: 18px; display: flex; flex-direction: column; justify-content: space-between; height: calc(100vh - 36px); padding: 19px 13px 15px; border-radius: var(--radius-xl); }
  .brand { display: flex; align-items: center; gap: 11px; padding: 3px 8px; }
  .brand-symbol, .brand-mark { display: grid; place-items: center; width: 42px; height: 42px; border-radius: 14px; color: #fff; background: linear-gradient(145deg, #45b8f4, var(--primary)); box-shadow: 6px 8px 16px rgba(19, 122, 171, .27); font-size: 16px; font-weight: 900; }
  .brand span:last-child { display: grid; line-height: 1.05; }
  .brand small { color: var(--text-soft); font-size: 10px; font-weight: 800; letter-spacing: 1.2px; }
  .brand strong { margin-top: 3px; font-size: 17px; }
  .app-nav { display: grid; gap: 6px; margin-top: 29px; }
  .nav-link { position: relative; display: grid; grid-template-columns: 24px 1fr auto; align-items: center; gap: 10px; min-height: 44px; padding: 8px 12px; border: 0; border-radius: 13px; color: var(--text-soft); background: transparent; font-weight: 730; text-align: left; transition: color .15s ease, background .15s ease, box-shadow .15s ease; }
  .nav-link svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; justify-self: center; }
  .nav-link:hover { color: var(--text); background: var(--surface-hover); }
  .nav-link.is-active { color: var(--primary); background: linear-gradient(145deg, var(--primary-soft), transparent); box-shadow: inset 3px 0 0 var(--primary), var(--inset); }
  .nav-badge { min-width: 20px; min-height: 20px; padding: 2px 5px; color: #fff; background: var(--danger); }
  .nav-separator { margin: 13px 9px 3px; color: var(--text-faint); font-size: 9px; font-weight: 850; letter-spacing: 1.3px; text-transform: uppercase; }
  .sidebar-footer { display: grid; gap: 8px; padding: 13px 8px 0; border-top: 1px solid var(--line-soft); }
  .server-state { display: flex; align-items: center; gap: 8px; color: var(--text-soft); font-size: 11px; }
  .server-state i { width: 8px; height: 8px; border-radius: 50%; background: #44c79d; box-shadow: 0 0 0 5px rgba(68, 199, 157, .12); }
  .sidebar-footer .link-button { justify-self: start; }
  .app-main { min-width: 0; padding-bottom: 42px; }
  .app-header { position: sticky; z-index: 100; top: 10px; display: flex; align-items: center; justify-content: space-between; min-height: 70px; margin-bottom: 21px; padding: 11px 13px 11px 17px; border-radius: var(--radius-lg); backdrop-filter: blur(18px); }
  body[data-view="history"] .app-header { position: relative; top: 0; }
  .page-heading p { margin-bottom: 2px; color: var(--primary); font-size: 9px; font-weight: 850; letter-spacing: 1.3px; }
  .header-actions { display: flex; align-items: center; gap: 11px; }
  .header-actions .subtle { font-size: 11px; }
  .view { display: none; }
  .view.is-active { display: block; }
  .page-toolbar { display: flex; align-items: flex-end; justify-content: space-between; gap: 18px; margin: 7px 0 16px; }
  .page-toolbar h2 { font-size: 25px; }
  .page-toolbar .subtle { margin-top: 4px; }
  .toolbar-controls { display: flex; align-items: center; justify-content: flex-end; gap: 9px; }
  .toolbar-controls select { width: auto; min-width: 150px; }
  .search-box { position: relative; display: block; width: min(360px, 34vw); }
  .search-box svg { position: absolute; z-index: 1; top: 50%; left: 12px; width: 16px; height: 16px; color: var(--text-faint); transform: translateY(-50%); }
  .search-box input { padding-left: 36px; }
}

@layer pages {
  .metric-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 13px; }
  .metric-card { display: flex; align-items: center; gap: 14px; min-height: 115px; padding: 17px; border-radius: var(--radius-lg); }
  .metric-icon { display: grid; flex: 0 0 auto; place-items: center; width: 44px; height: 44px; border-radius: 14px; }
  .metric-icon svg { width: 21px; height: 21px; }
  .metric-icon.blue, .metric-icon.cyan { color: #087caf; background: var(--primary-soft); }
  .metric-icon.amber { color: #9a631a; background: var(--warning-soft); }
  .metric-icon.green { color: #127257; background: var(--success-soft); }
  html[data-theme="dark"] .metric-icon.blue, html[data-theme="dark"] .metric-icon.cyan { color: #70d1fb; }
  html[data-theme="dark"] .metric-icon.amber { color: #f2c275; }
  html[data-theme="dark"] .metric-icon.green { color: #78e3c1; }
  .metric-card small, .metric-card p { display: block; color: var(--text-soft); }
  .metric-card strong { display: block; margin: 3px 0 0; font-size: 28px; line-height: 1.1; }
  .metric-card p { margin-top: 2px; font-size: 11px; }
  .overview-grid { display: grid; grid-template-columns: minmax(320px, 2fr) minmax(0, 3fr); align-items: stretch; gap: 16px; margin-top: 16px; }
  .overview-grid > .panel { display: flex; flex-direction: column; min-width: 0; height: auto; }
  .overview-grid > .panel > .panel-header { flex-shrink: 0; }
  .overview-grid .attention-list { min-height: 0; align-content: start; }
  .overview-grid .attention-item { padding: 7px 10px; }
  .overview-grid .recent-grid { min-height: 0; grid-auto-rows: auto; }
  .overview-grid .recent-capture { display: grid; grid-template-rows: minmax(0, 1fr) auto; min-height: 0; }
  .overview-grid .recent-capture img { height: clamp(120px, 10vw, 180px); min-height: 0; aspect-ratio: auto; }
  .attention-list { display: grid; gap: 8px; }
  .attention-item { display: grid; grid-template-columns: 34px minmax(0, 1fr) auto; align-items: center; gap: 10px; width: 100%; padding: 10px; border: 1px solid var(--line-soft); border-radius: var(--radius-md); color: var(--text); background: linear-gradient(145deg, var(--surface-raised), var(--surface)); box-shadow: var(--shadow-sm); text-align: left; transition: transform .15s ease, background .15s ease, box-shadow .15s ease; }
  .attention-item:hover { background: var(--surface-hover); box-shadow: var(--shadow-md); transform: translateY(-1px); }
  .attention-item:active { box-shadow: var(--inset); transform: translateY(1px); }
  .attention-symbol { display: grid; place-items: center; width: 34px; height: 34px; border-radius: 11px; color: var(--primary); background: var(--primary-soft); font-size: 11px; font-weight: 850; }
  .attention-item p { margin-top: 2px; color: var(--text-soft); font-size: 10px; }
  .recent-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
  .recent-capture { min-width: 0; margin: 0; }
  .recent-capture img { display: block; width: 100%; aspect-ratio: 16/9; border-radius: 11px; background: #081018; object-fit: contain; }
  .recent-capture figcaption { display: flex; justify-content: space-between; gap: 7px; margin-top: 5px; color: var(--text-soft); font-size: 9px; }
  .recent-capture figcaption span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .device-grid { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 12px; }
  .device-card { position: relative; min-width: 0; padding: 11px; border: 1px solid var(--line-soft); border-radius: 17px; background: linear-gradient(145deg, var(--surface-raised), var(--surface)); box-shadow: var(--shadow-md); transition: transform .15s ease, box-shadow .15s ease; }
  .device-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }
  .device-card-header { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 6px; }
  .device-card-badges { display: flex; align-items: center; justify-content: flex-end; min-width: 0; gap: 5px; }
  .device-name-wrap { display: flex; align-items: center; min-width: 0; gap: 5px; }
  .device-name-wrap h3 { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .device-rename { display: grid; flex: 0 0 auto; place-items: center; width: 25px; height: 25px; padding: 0; border: 0; border-radius: 8px; color: var(--primary); background: transparent; }
  .device-id { display: block; margin: 4px 0 7px; overflow: hidden; color: var(--text-faint); font: 9px/1.3 ui-monospace, SFMono-Regular, Consolas, monospace; text-overflow: ellipsis; white-space: nowrap; }
  .device-thumbnail { display: block; width: 100%; aspect-ratio: 16/9; border: 0; border-radius: 10px; background: #081018; object-fit: cover; }
  .device-thumbnail-empty { display: grid; place-items: center; width: 100%; aspect-ratio: 16/9; border-radius: 10px; color: #7f94a4; background: #101d27; font-size: 10px; }
  .device-details { display: flex; justify-content: space-between; gap: 8px; margin-top: 7px; color: var(--text-soft); font-size: 9px; }
  .device-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; margin-top: 8px; }
  .device-actions .button { min-height: 35px; padding: 6px 8px; font-size: 10px; }
  .device-actions .button:only-child { grid-column: 1 / -1; }
  .pagination { display: flex; align-items: center; justify-content: center; gap: 12px; margin-top: 17px; }
  .pagination span { color: var(--text-soft); font-size: 11px; }
  .incident-list { display: grid; gap: 10px; }
  .incident-card { display: grid; grid-template-columns: minmax(190px, .7fr) minmax(260px, 1.3fr) auto; align-items: center; gap: 18px; padding: 16px 18px; border: 1px solid var(--line-soft); border-left: 4px solid var(--warning); border-radius: var(--radius-md); background: linear-gradient(145deg, var(--surface-raised), var(--surface)); box-shadow: var(--shadow-md); }
  .incident-card.critical { border-left-color: var(--danger); }
  .incident-card code, .incident-card p, .incident-card small { color: var(--text-soft); }
  .incident-card code { font-size: 9px; }
  .incident-card p { margin-top: 3px; }
  .history-player { padding: 10px; }
  .history-stage { position: relative; display: grid; place-items: center; overflow: hidden; aspect-ratio: 16/9; border-radius: 14px; background: #05090d; }
  .history-stage img { width: 100%; height: 100%; object-fit: contain; }
  .stage-empty { color: #91a1af; }
  .stage-time { position: absolute; right: 11px; bottom: 10px; padding: 5px 8px; border-radius: 8px; color: #fff; background: rgba(8, 15, 21, .72); font-size: 10px; backdrop-filter: blur(8px); }
  .history-controls { display: grid; grid-template-columns: auto auto auto minmax(150px, 1fr) auto auto auto; align-items: center; gap: 8px; padding: 11px 2px 1px; }
  .play-control { min-width: 82px; }
  .timeline { width: 100%; accent-color: var(--primary); }
  .history-controls span { color: var(--text-soft); font-size: 11px; white-space: nowrap; }
  .settings-stack { display: grid; gap: 14px; }
  .settings-section > header p, .split-section > div:first-child > p:last-child, .form-panel > p { margin-top: 7px; color: var(--text-soft); }
  .split-section { display: grid; grid-template-columns: minmax(240px, .7fr) minmax(400px, 1.3fr); align-items: center; gap: 25px; }
  .section-actions { display: flex; align-items: center; justify-content: flex-start; flex-wrap: wrap; gap: 10px; }
  .section-actions p { flex: 1 1 260px; }
  .operations-grid { display: grid; grid-template-columns: repeat(4, minmax(105px, 1fr)); gap: 8px; margin-bottom: 10px; }
  .operation-stat { padding: 10px; border-radius: 11px; background: var(--surface-sunken); }
  .operation-stat span { color: var(--text-soft); font-size: 10px; }
  .operation-stat strong { display: block; margin-top: 3px; }
  .deployment-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 15px; }
  .form-panel > h2 { margin-top: 2px; }
  .deployment-panel { display: flex; flex-direction: column; }
  .deployment-panel .form-stack { flex: 1; }
  .deployment-panel .form-stack .button-primary { margin-top: auto; }
  .cleanup-tool { grid-column: 1 / -1; display: grid; grid-template-columns: minmax(0, 1fr) minmax(300px, .75fr); align-items: center; gap: 22px; padding: 16px 20px; }
  .cleanup-tool p { margin-top: 3px; color: var(--text-soft); font-size: 11px; }
  .cleanup-tool .button { width: 100%; min-height: 42px; white-space: nowrap; }
  .build-library { grid-column: 1 / -1; }
  .build-table-header, .build-row { display: grid; grid-template-columns: minmax(220px, 2fr) 100px 150px 100px minmax(260px, auto); align-items: center; gap: 12px; }
  .build-table-header { padding: 11px 14px; color: var(--text-faint); font-size: 10px; font-weight: 800; letter-spacing: .55px; text-transform: uppercase; }
  .build-list { display: grid; gap: 8px; }
  .build-row { min-height: 58px; padding: 10px 14px; border: 1px solid var(--line-soft); border-radius: var(--radius-md); background: var(--surface-sunken); }
  .build-row code { min-width: 0; overflow: hidden; color: var(--text); font-family: var(--font); font-weight: 750; text-overflow: ellipsis; white-space: nowrap; }
  .build-row time, .build-row .build-size { color: var(--text-soft); font-size: 11px; }
  .build-actions { display: flex; justify-content: flex-end; gap: 6px; }
  .build-actions .button { min-height: 33px; padding: 6px 10px; font-size: 10px; }
  .password-value { display: flex; align-items: center; gap: 8px; margin-top: 12px; padding: 11px; border-radius: var(--radius-sm); background: var(--surface-sunken); box-shadow: var(--inset); }
  .password-value code { flex: 1; overflow-wrap: anywhere; color: var(--text); }
  .account-profile { display: grid; grid-template-columns: minmax(220px, 1fr) auto minmax(350px, auto); align-items: center; gap: 18px; }
  .profile-identity { display: flex; align-items: center; min-width: 0; gap: 12px; }
  .profile-avatar, .user-avatar { display: grid; flex: 0 0 auto; place-items: center; border-radius: 13px; color: #087caf; background: var(--primary-soft); box-shadow: var(--shadow-sm); font-weight: 880; }
  .profile-avatar { width: 45px; height: 45px; }
  .user-avatar { width: 36px; height: 36px; font-size: 11px; }
  html[data-theme="dark"] .profile-avatar, html[data-theme="dark"] .user-avatar { color: #77d4fb; }
  .theme-setting { display: flex; align-items: center; justify-content: flex-end; gap: 10px; color: var(--text-soft); font-size: 11px; font-weight: 750; }
  .account-admin-grid { display: grid; grid-template-columns: minmax(0, 1.35fr) minmax(300px, .65fr); gap: 14px; margin-top: 14px; }
  .system-panel { grid-column: 1 / -1; }
  .user-list { display: grid; gap: 9px; }
  .user-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 12px; padding: 11px; border-radius: var(--radius-md); background: var(--surface-sunken); }
  .user-identity { display: flex; align-items: center; gap: 10px; }
  .user-meta { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 4px; }
  .user-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 7px; }
  .user-actions .button { min-height: 34px; padding: 6px 9px; font-size: 10px; }
  .path-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
  .path-item { min-width: 0; padding: 10px; border-radius: 11px; background: var(--surface-sunken); }
  .path-item span { display: block; margin-bottom: 3px; color: var(--text-faint); font-size: 9px; font-weight: 800; text-transform: uppercase; }
  .path-item code { display: block; overflow: hidden; color: var(--text-soft); font-size: 10px; text-overflow: ellipsis; white-space: nowrap; }
  .audit-panel { padding-top: 12px; }
  .audit-table-header, .audit-row { display: grid; grid-template-columns: 145px minmax(190px, .75fr) minmax(210px, .9fr) minmax(260px, 1.15fr) minmax(180px, .8fr); gap: 14px; align-items: center; }
  .audit-table-header { padding: 0 9px 8px; color: var(--text-faint); font-size: 9px; font-weight: 850; text-transform: uppercase; }
  .audit-list { display: grid; max-height: calc(100vh - 230px); overflow: auto; }
  .audit-row { padding: 10px 9px; border-top: 1px solid var(--line-soft); font-size: 11px; }
  .audit-row time, .audit-row strong, .audit-row code, .audit-row span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .audit-row time, .audit-row code, .audit-row span { color: var(--text-soft); }
}

@layer utilities {
  @media (max-width: 1650px) { .device-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
  @media (max-width: 1280px) {
    .app-shell { grid-template-columns: 190px minmax(0, 1fr); gap: 14px; padding: 12px; }
    .app-sidebar { top: 12px; height: calc(100vh - 24px); }
    .device-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
    .metric-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .overview-grid { grid-template-columns: 1fr; }
    .overview-grid > .panel { height: auto; }
    .overview-grid .recent-grid { flex: none; grid-template-rows: auto; }
    .overview-grid .recent-capture img { height: auto; aspect-ratio: 16/9; max-height: 240px; }
    .account-profile { grid-template-columns: 1fr auto; }
    .theme-setting { grid-column: 1 / -1; justify-content: flex-start; }
  }
  @media (max-width: 980px) {
    .app-shell { display: block; }
    .app-sidebar { position: static; width: auto; height: auto; margin-bottom: 12px; }
    .app-sidebar > div:first-child { display: flex; align-items: center; gap: 15px; }
    .app-nav { display: flex; min-width: 0; margin: 0; overflow-x: auto; }
    .nav-link { flex: 0 0 auto; grid-template-columns: 24px auto auto; }
    .nav-separator, .sidebar-footer { display: none; }
    .app-header { top: 6px; }
    .device-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .page-toolbar { align-items: flex-start; flex-direction: column; }
    .toolbar-controls { width: 100%; justify-content: flex-start; flex-wrap: wrap; }
    .search-box { width: 100%; }
    .form-grid { grid-template-columns: repeat(2, minmax(140px, 1fr)); }
    .split-section, .deployment-grid, .account-admin-grid { grid-template-columns: 1fr; }
    .build-table-header { display: none; }
    .build-row { grid-template-columns: 1fr auto; }
    .build-row code, .build-actions { grid-column: 1 / -1; }
    .cleanup-tool { grid-template-columns: 1fr; align-items: stretch; }
    .build-actions { justify-content: flex-start; }
    .system-panel { grid-column: auto; }
    .audit-table-header { display: none; }
    .audit-row { grid-template-columns: 1fr 1fr; }
    .audit-row span, .audit-row code { grid-column: 1 / -1; }
  }
  @media (max-width: 620px) {
    .app-shell { padding: 8px; }
    .brand span:last-child { display: none; }
    .metric-grid, .device-grid, .recent-grid, .form-grid, .path-grid { grid-template-columns: 1fr; }
    .metric-card { min-height: 95px; }
    .app-header { min-height: 61px; }
    .header-actions .subtle { display: none; }
    .toolbar-controls select { flex: 1 1 140px; min-width: 0; }
    .history-controls { grid-template-columns: auto auto auto minmax(70px, 1fr); }
    .history-controls .timeline { grid-column: 1 / -1; grid-row: 2; }
    .history-controls #player-counter, .history-controls #player-speed, .history-controls #player-window { grid-row: 3; }
    .history-controls #player-window { grid-column: 3 / -1; }
    .account-profile { grid-template-columns: 1fr; }
    .account-profile > .button { justify-self: start; }
    .theme-setting { display: grid; }
    .user-row, .incident-card { grid-template-columns: 1fr; }
    .user-actions { justify-content: flex-start; }
  }
  @media (prefers-reduced-motion: reduce) { *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; } }
}

/* Standalone authentication */
.auth-page { display: grid; place-items: center; min-height: 100vh; padding: 20px; }
.auth-card { width: min(440px, 100%); padding: 30px; border: 1px solid var(--line-soft); border-radius: var(--radius-xl); background: linear-gradient(145deg, var(--surface-raised), var(--surface)); box-shadow: var(--shadow-lg); }
.auth-brand { display: flex; align-items: center; gap: 11px; }
.auth-card h1 { margin: 25px 0 8px; }
.auth-card > p { color: var(--text-soft); }
.auth-card .form-stack { margin-top: 21px; }
.auth-card .button-primary { width: 100%; }
.auth-submit { min-height: 46px; border-radius: 13px; font-weight: 800; letter-spacing: .1px; }
.auth-error { min-height: 20px; color: var(--danger); font-size: 12px; }

/* Standalone history and live viewers intentionally stay cinema-dark. */
.viewer-body { width: 100vw; height: 100vh; margin: 0; overflow: hidden; color: #f4f8fb; background: #05090d; }
.viewer-layout { display: grid; grid-template-rows: minmax(0, 1fr) auto; width: 100%; height: 100%; }
.viewer-stage { display: grid; place-items: center; min-width: 0; min-height: 0; overflow: hidden; background: #020609; }
.viewer-stage img { display: block; width: 100%; height: 100%; object-fit: contain; }
.viewer-empty { color: #a7b7c3; }
.viewer-footer { display: grid; grid-template-columns: auto minmax(120px, 1fr) auto auto auto minmax(130px, auto) minmax(150px, auto); align-items: center; gap: 8px; min-height: 49px; padding: 7px 10px; border-top: 1px solid #293c4a; background: #13212c; }
.viewer-buttons { display: flex; gap: 5px; }
.viewer-button, .viewer-select { height: 34px; border: 0; border-radius: 9px; color: #f4f8fb; background: #203441; box-shadow: inset 2px 2px 5px rgba(3, 12, 18, .55), inset -1px -1px 2px rgba(88, 132, 157, .16); }
.viewer-button { min-width: 34px; padding: 0 10px; }
.viewer-button.primary { border-color: transparent; background: #229ed9; font-weight: 800; }
.viewer-select { padding: 5px 30px 5px 9px; outline: 0; color-scheme: dark; }
.viewer-select:hover { background: #263d4b; }
.viewer-select:focus-visible { box-shadow: 0 0 0 2px rgba(34, 158, 217, .65), inset 2px 2px 5px rgba(3, 12, 18, .55) !important; }
.viewer-select option { color: #f4f8fb; background: #203441; }
.viewer-timeline { width: 100%; accent-color: #229ed9; }
.viewer-meta { color: #b9c8d3; font-size: 11px; white-space: nowrap; }
.viewer-frame-time { min-width: 68px; color: #f4f8fb; font-size: 12px; font-variant-numeric: tabular-nums; font-weight: 800; text-align: right; white-space: nowrap; }
.live-viewer-footer { grid-template-columns: minmax(180px, 1fr) auto auto auto; }
@media (max-width: 760px) { .viewer-footer { grid-template-columns: auto minmax(80px, 1fr) auto auto auto; } .viewer-footer .viewer-device { grid-column: 1 / -1; } }
:root .language-control { display: inline-flex; align-items: center; gap: .5rem; font-size: .85rem; flex-shrink: 0; }
:root .language-control select { width: auto; min-width: 7.5rem; min-height: 2.5rem; }
.auth-card > .language-control { display: flex; justify-content: flex-end; margin-bottom: 1rem; }
.viewer-footer .language-control { color: inherit; }
@media (max-width: 640px) { .header-actions { flex-wrap: wrap; } .header-actions .language-control > span { display: none; } }
