/* Traffic Tracker — modern SaaS UI.
   Light and dark are both selected, not flipped: the dark steps are chosen for
   the dark surface and validated against it. */
:root {
  color-scheme: light;

  --surface-1: #ffffff;
  --surface-2: #fafaf9;
  --plane: #f6f6f4;
  --elevated: #ffffff;

  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #8a8883;

  --border: rgba(11, 11, 11, 0.09);
  --border-strong: rgba(11, 11, 11, 0.16);
  --grid: #ececea;
  --axis: #d7d6d1;

  --series-1: #2a78d6;
  --series-2: #1baf7a;
  --series-3: #eb6834;
  --series-4: #4a3aa7;
  --series-5: #eda100;
  --series-6: #e87ba4;

  --accent: #2a78d6;
  --accent-hover: #256abf;
  --accent-soft: rgba(42, 120, 214, 0.09);

  --good: #0ca30c;
  --good-soft: rgba(12, 163, 12, 0.10);
  --warning: #fab219;
  --critical: #d03b3b;
  --critical-soft: rgba(208, 59, 59, 0.10);
  --delta-up: #006300;

  --shadow-sm: 0 1px 2px rgba(11, 11, 11, 0.05);
  --shadow: 0 1px 3px rgba(11, 11, 11, 0.07), 0 8px 24px -12px rgba(11, 11, 11, 0.13);
  --shadow-lg: 0 2px 8px rgba(11, 11, 11, 0.08), 0 20px 48px -16px rgba(11, 11, 11, 0.18);

  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, monospace;
  --radius: 12px;
  --radius-sm: 8px;
  --radius-lg: 16px;
  --sidebar-w: 236px;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface-1: #171716;
    --surface-2: #1e1e1c;
    --plane: #0e0e0d;
    --elevated: #201f1e;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #8a8883;
    --border: rgba(255, 255, 255, 0.10);
    --border-strong: rgba(255, 255, 255, 0.18);
    --grid: #2a2a28;
    --axis: #383835;
    --series-1: #3987e5;
    --series-2: #199e70;
    --series-3: #d95926;
    --series-4: #9085e9;
    --series-5: #c98500;
    --series-6: #d55181;
    --accent: #3987e5;
    --accent-hover: #4d94e8;
    --accent-soft: rgba(57, 135, 229, 0.14);
    --delta-up: #0ca30c;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.5), 0 8px 24px -12px rgba(0, 0, 0, 0.7);
    --shadow-lg: 0 2px 8px rgba(0, 0, 0, 0.5), 0 20px 48px -16px rgba(0, 0, 0, 0.8);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-1: #171716;
  --surface-2: #1e1e1c;
  --plane: #0e0e0d;
  --elevated: #201f1e;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #8a8883;
  --border: rgba(255, 255, 255, 0.10);
  --border-strong: rgba(255, 255, 255, 0.18);
  --grid: #2a2a28;
  --axis: #383835;
  --series-1: #3987e5;
  --series-2: #199e70;
  --series-3: #d95926;
  --series-4: #9085e9;
  --series-5: #c98500;
  --series-6: #d55181;
  --accent: #3987e5;
  --accent-hover: #4d94e8;
  --accent-soft: rgba(57, 135, 229, 0.14);
  --delta-up: #0ca30c;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.5), 0 8px 24px -12px rgba(0, 0, 0, 0.7);
  --shadow-lg: 0 2px 8px rgba(0, 0, 0, 0.5), 0 20px 48px -16px rgba(0, 0, 0, 0.8);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--plane);
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

::selection { background: var(--accent-soft); }

/* ── layout ─────────────────────────────────────────────────────────── */
.app { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w); flex-shrink: 0;
  background: var(--surface-1);
  border-right: 1px solid var(--border);
  padding: 18px 0 24px;
  position: sticky; top: 0; height: 100vh;
  overflow-y: auto;
  display: flex; flex-direction: column;
}

.logo {
  padding: 0 18px 18px;
  font-weight: 650; font-size: 15px; letter-spacing: -0.015em;
  display: flex; align-items: center; gap: 9px;
}
.logo-mark {
  width: 26px; height: 26px; border-radius: 7px;
  background: linear-gradient(135deg, var(--accent), var(--series-4));
  display: grid; place-items: center;
  color: #fff; font-size: 13px; font-weight: 700;
  flex-shrink: 0;
}

.nav-section {
  padding: 12px 18px 5px;
  font-size: 10.5px; font-weight: 650; letter-spacing: 0.07em;
  text-transform: uppercase; color: var(--text-muted);
}

.nav-item {
  display: flex; align-items: center; gap: 10px;
  margin: 1px 8px; padding: 8px 10px;
  color: var(--text-secondary); text-decoration: none;
  cursor: pointer; border-radius: var(--radius-sm);
  font-size: 13.5px; font-weight: 480;
  transition: background .12s, color .12s;
}
.nav-item:hover { background: var(--surface-2); color: var(--text-primary); }
.nav-item.active {
  background: var(--accent-soft); color: var(--accent); font-weight: 570;
}
.nav-item svg {
  width: 16px; height: 16px;
  min-width: 16px; min-height: 16px;   /* flex must not stretch the icon */
  flex: 0 0 16px;
  opacity: .85;
}

.sidebar-footer {
  margin-top: auto; padding: 14px 18px 0;
  border-top: 1px solid var(--border);
  font-size: 12px; color: var(--text-muted);
}

.main { flex: 1; min-width: 0; padding: 22px 28px 60px; }

.header {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 16px; margin-bottom: 22px; flex-wrap: wrap;
}
h1 { font-size: 22px; font-weight: 660; margin: 0; letter-spacing: -0.022em; }
h2 { font-size: 14.5px; font-weight: 600; margin: 0 0 14px; letter-spacing: -0.01em; }
h3 { font-size: 13px; font-weight: 600; margin: 0 0 8px; }
.subtitle { color: var(--text-muted); font-size: 13px; margin-top: 3px; }

/* ── controls ───────────────────────────────────────────────────────── */
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }

select, input[type="text"], input[type="search"], input[type="email"],
input[type="password"], input[type="date"], textarea {
  font-family: var(--font); font-size: 13px;
  padding: 7px 11px;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--surface-1); color: var(--text-primary);
  transition: border-color .12s, box-shadow .12s;
}
select:focus, input:focus, textarea:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
input[type="search"] { min-width: 230px; }
textarea { font-family: var(--mono); font-size: 12.5px; line-height: 1.6; resize: vertical; }

button {
  font-family: var(--font); font-size: 13px; font-weight: 520;
  padding: 7px 13px;
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  background: var(--surface-1); color: var(--text-primary);
  cursor: pointer; white-space: nowrap;
  display: inline-flex; align-items: center; gap: 6px;
  transition: background .12s, border-color .12s, transform .06s;
}
button:hover:not(:disabled) { background: var(--surface-2); border-color: var(--text-muted); }
button:active:not(:disabled) { transform: translateY(0.5px); }
button:disabled { opacity: .45; cursor: not-allowed; }
button.primary {
  background: var(--accent); color: #fff; border-color: transparent;
  box-shadow: var(--shadow-sm);
}
button.primary:hover:not(:disabled) { background: var(--accent-hover); border-color: transparent; }
button.ghost { background: transparent; border-color: transparent; color: var(--text-secondary); }
button.ghost:hover:not(:disabled) { background: var(--surface-2); }
button.danger { color: var(--critical); border-color: var(--border-strong); }
button.danger:hover:not(:disabled) { background: var(--critical-soft); border-color: var(--critical); }
button.sm { padding: 4px 9px; font-size: 12px; }

/* Segmented date range control */
.segmented {
  display: inline-flex; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 2px;
}
.segmented button {
  border: none; background: transparent; padding: 5px 11px;
  font-size: 12.5px; border-radius: 6px; color: var(--text-secondary);
}
.segmented button:hover { background: transparent; color: var(--text-primary); }
.segmented button.on {
  background: var(--surface-1); color: var(--text-primary);
  font-weight: 570; box-shadow: var(--shadow-sm);
}

/* ── stat tiles ─────────────────────────────────────────────────────── */
.tiles {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(168px, 1fr));
  gap: 12px; margin-bottom: 18px;
}
.tile {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 15px 16px 14px;
  position: relative; overflow: hidden;
  transition: border-color .15s, box-shadow .15s;
}
.tile:hover { border-color: var(--border-strong); box-shadow: var(--shadow); }
.tile-label {
  font-size: 11px; font-weight: 600; letter-spacing: 0.04em;
  text-transform: uppercase; color: var(--text-muted);
  margin-bottom: 7px; display: flex; align-items: center; gap: 5px;
}
.tile-value {
  font-size: 27px; font-weight: 660; letter-spacing: -0.028em;
  line-height: 1.1;
}
.tile-delta {
  font-size: 12px; margin-top: 5px; color: var(--text-muted);
  display: flex; align-items: center; gap: 4px;
}
.tile-delta.up { color: var(--delta-up); }
.tile-delta.down { color: var(--critical); }
.tile-spark { position: absolute; right: 0; bottom: 0; opacity: .16; pointer-events: none; }

/* ── cards ──────────────────────────────────────────────────────────── */
.card {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 17px 18px; margin-bottom: 16px;
}
.card.pad-0 { padding: 0; overflow: hidden; }
.card-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; margin-bottom: 14px; flex-wrap: wrap;
}
.card-head h2 { margin: 0; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(330px, 1fr)); gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(250px, 1fr)); gap: 16px; }

/* ── charts ─────────────────────────────────────────────────────────── */
.chart-wrap { position: relative; overflow-x: auto; overflow-y: hidden; }
/* Chart SVGs fill their container; the sidebar's icons must not, so the
   block/width rule is scoped to the chart wrappers rather than every svg. */
.chart-wrap svg, .tile-spark svg { display: block; }
.grid-line { stroke: var(--grid); stroke-width: 1; }
.axis-line { stroke: var(--axis); stroke-width: 1; }
.axis-text { fill: var(--text-muted); font-size: 11px; font-family: var(--font); }
.tabular { font-variant-numeric: tabular-nums; }

.legend { display: flex; gap: 16px; flex-wrap: wrap; margin-bottom: 12px; }
.legend-item {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12.5px; color: var(--text-secondary); cursor: default;
}
.legend-swatch { width: 9px; height: 9px; border-radius: 2.5px; flex-shrink: 0; }

.tooltip {
  position: fixed; pointer-events: none; z-index: 200;
  background: var(--elevated); border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm); padding: 9px 11px; font-size: 12.5px;
  box-shadow: var(--shadow-lg); opacity: 0;
  transition: opacity .1s; max-width: 300px;
}
.tooltip.show { opacity: 1; }
.tooltip-title {
  font-weight: 600; margin-bottom: 6px; word-break: break-all;
  padding-bottom: 5px; border-bottom: 1px solid var(--border);
}
.tooltip-row {
  display: flex; justify-content: space-between; gap: 18px;
  color: var(--text-secondary); line-height: 1.7;
}
.tooltip-row b { color: var(--text-primary); font-variant-numeric: tabular-nums; }

/* ── tables ─────────────────────────────────────────────────────────── */
.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead th {
  text-align: left; padding: 9px 12px;
  font-size: 11px; font-weight: 620; letter-spacing: 0.04em;
  text-transform: uppercase; color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  white-space: nowrap; background: var(--surface-1);
  position: sticky; top: 0; z-index: 2;
}
thead th.sortable { cursor: pointer; user-select: none; }
thead th.sortable:hover { color: var(--text-primary); }
th.num, td.num { text-align: right; font-variant-numeric: tabular-nums; }
td { padding: 9px 12px; border-bottom: 1px solid var(--grid); }
tbody tr { transition: background .1s; }
tbody tr:hover { background: var(--surface-2); }
tbody tr.clickable { cursor: pointer; }
td.truncate { max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Ranked rows: the bar sits behind the label so the value stays readable. */
.rank-row { position: relative; }
.rank-bar {
  position: absolute; left: 0; top: 3px; bottom: 3px;
  background: var(--accent-soft); border-radius: 0 5px 5px 0;
  pointer-events: none; transition: width .35s cubic-bezier(.4,0,.2,1);
}
.rank-label { position: relative; z-index: 1; }

.pill {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 8px; border-radius: 20px;
  font-size: 11.5px; font-weight: 530;
  background: var(--surface-2); border: 1px solid var(--border);
  color: var(--text-secondary);
}
.pill.good { background: var(--good-soft); color: var(--good); border-color: transparent; }
.pill.bad { background: var(--critical-soft); color: var(--critical); border-color: transparent; }
.pill.accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }

.mono { font-family: var(--mono); font-size: 12px; }
.muted { color: var(--text-muted); }
.empty {
  padding: 44px 20px; text-align: center; color: var(--text-muted); font-size: 13.5px;
}
.empty-icon { font-size: 30px; opacity: .35; margin-bottom: 10px; display: block; }

/* ── skeleton loading ───────────────────────────────────────────────── */
.skeleton {
  background: linear-gradient(90deg, var(--surface-2) 25%, var(--grid) 50%, var(--surface-2) 75%);
  background-size: 200% 100%; animation: shimmer 1.4s infinite;
  border-radius: 6px;
}
@keyframes shimmer { 0% { background-position: 200% 0 } 100% { background-position: -200% 0 } }
.sk-tile { height: 92px; border-radius: var(--radius); }
.sk-card { height: 240px; border-radius: var(--radius); margin-bottom: 16px; }

.live-dot {
  display: inline-block; width: 7px; height: 7px; border-radius: 50%;
  background: var(--good); box-shadow: 0 0 0 0 var(--good-soft);
  animation: pulse 2s infinite;
}
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(12,163,12,.45) }
  70% { box-shadow: 0 0 0 7px rgba(12,163,12,0) }
  100% { box-shadow: 0 0 0 0 rgba(12,163,12,0) }
}

.pager {
  display: flex; gap: 10px; align-items: center; justify-content: flex-end;
  margin-top: 14px; font-size: 12.5px; color: var(--text-muted);
}

/* ── modal ──────────────────────────────────────────────────────────── */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: grid; place-items: center; z-index: 300; padding: 20px;
  animation: fade .14s;
}
@keyframes fade { from { opacity: 0 } }
.modal {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-lg);
  width: 100%; max-width: 620px; max-height: 88vh; overflow-y: auto;
  animation: rise .18s cubic-bezier(.4,0,.2,1);
}
@keyframes rise { from { opacity: 0; transform: translateY(10px) } }
.modal-head {
  padding: 18px 20px 14px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.modal-head h2 { margin: 0; font-size: 16px; }
.modal-body { padding: 20px; }
.modal-foot {
  padding: 14px 20px; border-top: 1px solid var(--border);
  display: flex; justify-content: flex-end; gap: 9px;
}

.field { margin-bottom: 15px; }
.field label {
  display: block; font-size: 12.5px; font-weight: 570;
  margin-bottom: 5px; color: var(--text-secondary);
}
.field input, .field select, .field textarea { width: 100%; }
.field .hint { font-size: 12px; color: var(--text-muted); margin-top: 5px; line-height: 1.5; }

/* Snippet block with copy affordance */
.snippet {
  position: relative; background: var(--surface-2);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 13px 46px 13px 14px;
  font-family: var(--mono); font-size: 12.5px; line-height: 1.6;
  word-break: break-all; color: var(--text-primary);
}
.snippet-copy { position: absolute; top: 8px; right: 8px; }

.banner {
  padding: 11px 14px; border-radius: var(--radius-sm);
  font-size: 13px; margin-bottom: 14px;
  display: flex; align-items: flex-start; gap: 9px;
}
.banner.info { background: var(--accent-soft); color: var(--accent); }
.banner.good { background: var(--good-soft); color: var(--good); }
.banner.bad { background: var(--critical-soft); color: var(--critical); }

.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: var(--elevated); border: 1px solid var(--border-strong);
  box-shadow: var(--shadow-lg); border-radius: var(--radius-sm);
  padding: 11px 17px; font-size: 13px; z-index: 400;
  animation: rise .18s; display: flex; align-items: center; gap: 8px;
}

/* ── login ──────────────────────────────────────────────────────────── */
.login-wrap { display: grid; place-items: center; min-height: 100vh; padding: 20px; }
.login-card {
  width: 100%; max-width: 366px;
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius-lg); padding: 30px; box-shadow: var(--shadow-lg);
}

@media (max-width: 860px) {
  .sidebar {
    position: fixed; left: -100%; z-index: 250;
    transition: left .2s; box-shadow: var(--shadow-lg);
  }
  .sidebar.open { left: 0; }
  .main { padding: 16px; }
  .menu-btn { display: inline-flex !important; }
}
.menu-btn { display: none; }
