  :root {
    --bg: #0f172a;
    --panel: #1e293b;
    --panel-2: #273549;
    --line: #334155;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --accent: #38bdf8;
    --good: #22c55e;
    --warn: #f59e0b;
    --bad: #ef4444;
    --radius: 10px;
    color-scheme: dark;
  }
  * { box-sizing: border-box; }
  html, body { margin: 0; padding: 0; }
  body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg); color: var(--text); font-size: 14px;
  }
  .hidden { display: none !important; }
  button { cursor: pointer; font-family: inherit; }
  a { color: var(--accent); }

  /* --- Passcode gate --- */
  #gate {
    position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
    background: radial-gradient(circle at 50% 30%, #16233f, var(--bg));
  }
  .gate-card {
    background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
    padding: 32px; width: 340px; text-align: center; box-shadow: 0 20px 60px rgba(0,0,0,.5);
  }
  .gate-card h1 { margin: 0 0 4px; font-size: 20px; }
  .gate-sub { color: var(--muted); margin: 0 0 20px; }
  #gate-form { display: flex; flex-direction: column; gap: 10px; }
  #passcode-input {
    padding: 12px; border-radius: 8px; border: 1px solid var(--line);
    background: var(--panel-2); color: var(--text); font-size: 15px;
  }
  #gate-btn {
    padding: 12px; border-radius: 8px; border: none; background: var(--accent);
    color: #062031; font-weight: 600; font-size: 15px;
  }
  #gate-btn:disabled { opacity: .6; cursor: default; }
  .gate-error { color: var(--bad); min-height: 18px; margin: 12px 0 0; }
  .gate-hint { color: var(--muted); font-size: 12px; margin: 8px 0 0; }

  /* --- Topbar --- */
  .topbar {
    display: flex; align-items: center; gap: 20px; padding: 10px 18px;
    background: var(--panel); border-bottom: 1px solid var(--line);
    position: sticky; top: 0; z-index: 20;
  }
  .brand { font-weight: 700; letter-spacing: .3px; }
  .nav { display: flex; gap: 6px; }
  .nav-btn {
    padding: 8px 14px; border-radius: 8px; border: 1px solid transparent;
    background: transparent; color: var(--muted); font-weight: 500;
  }
  .nav-btn:hover { color: var(--text); background: var(--panel-2); }
  .nav-btn.active { color: var(--text); background: var(--panel-2); border-color: var(--line); }
  .topbar-right { margin-left: auto; display: flex; align-items: center; gap: 12px; }
  .admin-actions { display: flex; align-items: center; gap: 12px; }
  .admin-menu-btn { display: none; }
  .meta-info { color: var(--muted); font-size: 12px; }
  .save-status {
    font-size: 12px; padding: 4px 10px; border-radius: 999px; display: flex;
    align-items: center; gap: 6px; background: var(--panel-2); color: var(--muted);
  }
  .save-status.saving { color: var(--accent); }
  .save-status.saving .save-dot {
    width: 7px; height: 7px; border-radius: 50%; background: var(--accent);
    animation: pulse 1s ease-in-out infinite;
  }
  .save-status.saved { color: #4ade80; }
  .save-status.saved .save-dot { width: 7px; height: 7px; border-radius: 50%; background: #4ade80; }
  .save-status.error { color: #f87171; }
  .save-status.error .save-dot { width: 7px; height: 7px; border-radius: 50%; background: #f87171; }
  @keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .3; } }
  .ghost-btn {
    padding: 7px 12px; border-radius: 8px; border: 1px solid var(--line);
    background: transparent; color: var(--text);
  }
  .ghost-btn:hover { background: var(--panel-2); }

  .warnings-bar {
    background: #422006; color: #fbbf24; border-bottom: 1px solid #7c520a;
    padding: 8px 18px; font-size: 13px;
  }
  .content { padding: 18px; max-width: 1400px; margin: 0 auto; }
  .view { animation: fade .15s ease; }
  @keyframes fade { from { opacity: 0; } to { opacity: 1; } }

  /* --- Cards / KPI --- */
  .grid { display: grid; gap: 14px; }
  .kpi-row { grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); }
  .breakdown-row { grid-template-columns: 1fr 1fr; }
  .card {
    background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
    padding: 16px;
  }
  .card h3 { margin: 0 0 10px; font-size: 13px; color: var(--muted); font-weight: 600;
             text-transform: uppercase; letter-spacing: .4px; }
  .kpi-num { font-size: 30px; font-weight: 700; }
  .kpi-sub { color: var(--muted); font-size: 12px; margin-top: 4px; }

  .bar { height: 10px; border-radius: 6px; background: var(--panel-2); overflow: hidden; display: flex; }
  .bar > span { display: block; height: 100%; }

  /* --- Tables --- */
  table { width: 100%; border-collapse: collapse; }
  th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); font-size: 13px; }
  th { color: var(--muted); font-weight: 600; cursor: pointer; user-select: none; position: sticky; top: 0;
       background: var(--panel); }
  th.no-sort { cursor: default; }
  tr:hover td { background: rgba(255,255,255,.02); }
  .table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); }

  .badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; }
  .badge.good { background: rgba(34,197,94,.15); color: #4ade80; }
  .badge.warn { background: rgba(245,158,11,.15); color: #fbbf24; }
  .badge.bad  { background: rgba(239,68,68,.15); color: #f87171; }
  .badge.neutral { background: var(--panel-2); color: var(--muted); }
  .dot { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: middle; }

  /* --- Inventory mobile card list (hidden on desktop; shown ≤768px) --------- */
  .device-cards { display: none; flex-direction: column; gap: 10px; }
  .device-card {
    background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
    padding: 12px;
  }
  .device-card-head { display: flex; justify-content: space-between; align-items: center; font-weight: 600; }
  .device-card-meta { font-size: 12px; color: var(--muted); line-height: 1.6; }

  /* --- Controls --- */
  .toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 14px; }
  input[type="text"], input[type="search"], input[type="date"], select {
    padding: 8px 10px; border-radius: 8px; border: 1px solid var(--line);
    background: var(--panel-2); color: var(--text); font-size: 13px;
  }
  .primary-btn {
    padding: 8px 14px; border-radius: 8px; border: none; background: var(--accent);
    color: #062031; font-weight: 600;
  }
  .danger-btn {
    padding: 6px 10px; border-radius: 8px; border: 1px solid #7f1d1d; background: transparent; color: #f87171;
  }
  .danger-btn:hover { background: rgba(239,68,68,.12); }
  .inline-input { width: 100%; min-width: 90px; }

  /* --- Map --- */
  .map-layout { display: grid; grid-template-columns: 1fr 320px; gap: 16px; align-items: start; }
  .switch-block { margin-bottom: 20px; }
  .switch-block h3 { margin: 0 0 8px; }
  .svg-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .port-svg { width: 100%; height: auto; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
  .port-cell { cursor: pointer; }
  .port-cell:hover { opacity: .85; }
  .detail-panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
                  padding: 16px; position: sticky; top: 70px; }
  .detail-panel h3 { margin-top: 0; }
  .detail-row { display: flex; justify-content: space-between; gap: 10px; padding: 5px 0;
                border-bottom: 1px solid var(--line); font-size: 13px; }
  .detail-row .k { color: var(--muted); }
  .legend { display: flex; flex-wrap: wrap; gap: 10px 16px; margin: 10px 0 4px; font-size: 12px; color: var(--muted); }
  .legend span.item { display: inline-flex; align-items: center; }
  .tabs { display: flex; gap: 6px; margin-bottom: 12px; }
  .subtab-btn { padding: 6px 12px; border-radius: 8px; border: 1px solid var(--line); background: transparent; color: var(--muted); }
  .subtab-btn.active { background: var(--panel-2); color: var(--text); }
  .topo-svg { width: 100%; height: auto; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }

  /* --- Toast + modal --- */
  .toast {
    position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
    padding: 12px 18px; border-radius: 8px; font-weight: 500; z-index: 100;
    box-shadow: 0 10px 30px rgba(0,0,0,.4);
  }
  .toast.good { background: #14532d; color: #bbf7d0; }
  .toast.bad  { background: #7f1d1d; color: #fecaca; }
  .toast.info { background: var(--panel-2); color: var(--text); }
  .modal-host { position: fixed; inset: 0; z-index: 90; display: flex; align-items: center; justify-content: center; }
  .modal-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.55); }
  .modal-card {
    position: relative; background: var(--panel); border: 1px solid var(--line);
    border-radius: var(--radius); padding: 22px; width: 420px; max-width: 92vw; max-height: 88vh; overflow: auto;
  }
  .modal-card h3 { margin-top: 0; }
  .form-row { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
  .form-row label { font-size: 12px; color: var(--muted); }
  .modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 8px; }

  .muted { color: var(--muted); }
  .right { text-align: right; }
  .nowrap { white-space: nowrap; }

  /* --- Device edit drawer --- */
  .drawer-host { position: fixed; inset: 0; z-index: 95; }
  .drawer-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.45); }
  .drawer-panel {
    position: absolute; top: 0; right: 0; bottom: 0; width: 420px; max-width: 92vw;
    background: var(--panel); border-left: 1px solid var(--line); padding: 20px;
    overflow-y: auto; box-shadow: -12px 0 40px rgba(0,0,0,.4);
    animation: slide-in .16s ease;
  }
  @keyframes slide-in { from { transform: translateX(24px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
  .drawer-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; margin-bottom: 4px; }
  .drawer-header h3 { margin: 0; }
  .drawer-close { background: transparent; border: none; color: var(--muted); font-size: 20px; line-height: 1; padding: 2px 6px; }
  .drawer-close:hover { color: var(--text); }
  .drawer-sub { color: var(--muted); font-size: 12px; margin: 0 0 16px; }
  .drawer-section-title {
    font-size: 11px; text-transform: uppercase; letter-spacing: .4px; color: var(--muted);
    margin: 18px 0 8px; padding-top: 12px; border-top: 1px solid var(--line);
  }
  .drawer-section-title:first-of-type { border-top: none; padding-top: 0; margin-top: 6px; }
  .drawer-field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
  .drawer-field label { font-size: 12px; color: var(--muted); }
  .drawer-field input[type="text"], .drawer-field input[type="date"], .drawer-field select { width: 100%; }
  .drawer-field-check { flex-direction: row; align-items: center; gap: 8px; }
  .drawer-field-check label { order: 2; }
  .drawer-actions { display: flex; gap: 8px; margin-top: 20px; }

  /* =========================================================================
     Mobile / responsive overrides. Desktop rules above are untouched — every
     appearance change for small screens lives in these two breakpoints.
     ========================================================================= */
  @media (max-width: 768px) {
    .content { padding: 12px; }

    /* Page-level guard against residual horizontal overflow. Deliberately NOT
       placed on .topbar itself: .topbar contains the absolutely-positioned
       .admin-actions dropdown, which extends below the topbar's own box by
       design (top:100%) — clipping overflow there would clip the dropdown
       too. html/body is the standard place for this: browsers propagate a
       non-visible overflow-x on <body> up to the viewport's scrolling box,
       so it stops page-level horizontal scroll without creating a local
       clipping box around any in-page content. */
    html, body { overflow-x: hidden; }

    /* --- Topbar --- */
    .topbar {
      flex-wrap: wrap; gap: 10px; padding: 10px 12px;
      max-width: 100vw;
    }
    .brand { font-size: 13px; }
    .nav {
      overflow-x: auto; -webkit-overflow-scrolling: touch;
      scrollbar-width: none;
      min-width: 0; flex: 1 1 auto;
    }
    .nav::-webkit-scrollbar { display: none; }
    .nav-btn { flex: 0 0 auto; white-space: nowrap; padding: 8px 10px; }
    .topbar-right { gap: 8px; flex-shrink: 0; }

    /* --- Admin overflow menu --- */
    .admin-menu-btn { display: block; }
    .admin-actions {
      display: none;
      position: absolute; top: 100%; right: 12px;
      background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
      box-shadow: 0 12px 30px rgba(0,0,0,.4);
      padding: 8px; z-index: 30;
      min-width: 200px; max-width: calc(100vw - 24px);
    }
    .admin-actions.open {
      display: flex; flex-direction: column;
    }

    /* --- Map layout --- */
    .map-layout { grid-template-columns: minmax(0, 1fr); }
    .map-layout > * { min-width: 0; }
    .detail-panel { order: -1; position: static; }

    /* --- Port / topology svg scroll --- */
    .svg-scroll { max-width: 100%; }
    .svg-scroll .port-svg { min-width: 720px; }
    .svg-scroll .topo-svg { min-width: 640px; }

    /* --- Inputs / tap targets --- */
    input[type="text"], input[type="search"], input[type="date"], select {
      font-size: 16px;
    }
    button, select {
      min-height: 40px;
    }

    /* --- Drawer --- */
    .drawer-panel {
      width: 380px; max-width: 100vw;
      padding-bottom: max(20px, env(safe-area-inset-bottom));
    }

    /* --- Dashboard --- */
    .breakdown-row { grid-template-columns: 1fr; }

    /* --- Inventory: cards replace the table --- */
    .device-cards { display: flex; }
    .inventory-table-wrap { display: none; }
  }

  @media (max-width: 480px) {
    #meta-info { display: none; }

    .drawer-panel { width: 100%; max-width: 100%; }
  }
