:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface-1: #fcfcfb;
  --surface-2: #f3f2ef;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --gridline: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --point-muted: #c3c2b7;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --accent-wash: rgba(42, 120, 214, 0.10);
  --outline-fill: rgba(11, 11, 11, 0.04);
  --outline-stroke: #a9a8a1;
  --seq-1: #86b6ef; --seq-2: #5598e7; --seq-3: #2a78d6; --seq-4: #1c5cab; --seq-5: #0d366b;
  --seq-ink-1: #0b0b0b; --seq-ink-2: #0b0b0b; --seq-ink-3: #ffffff; --seq-ink-4: #ffffff; --seq-ink-5: #ffffff;
  --critical: #d03b3b;
  --success-text: #006300;
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface-1: #1a1a19;
    --surface-2: #232321;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --gridline: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --accent-ink: #86b6ef;
    --point-muted: #4a4a46;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --success-text: #0ca30c;
    --accent-wash: rgba(57, 135, 229, 0.16);
    --outline-fill: rgba(255, 255, 255, 0.05);
    --outline-stroke: #5f5e59;
    --seq-1: #184f95; --seq-2: #256abf; --seq-3: #5598e7; --seq-4: #9ec5f4; --seq-5: #cde2fb;
    --seq-ink-1: #ffffff; --seq-ink-2: #ffffff; --seq-ink-3: #0b0b0b; --seq-ink-4: #0b0b0b; --seq-ink-5: #0b0b0b;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface-1: #1a1a19;
  --surface-2: #232321;
  --text-primary: #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --gridline: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --accent: #3987e5;
  --accent-ink: #86b6ef;
  --point-muted: #4a4a46;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --success-text: #0ca30c;
  --accent-wash: rgba(57, 135, 229, 0.16);
  --outline-fill: rgba(255, 255, 255, 0.05);
  --outline-stroke: #5f5e59;
  --seq-1: #184f95; --seq-2: #256abf; --seq-3: #5598e7; --seq-4: #9ec5f4; --seq-5: #cde2fb;
  --seq-ink-1: #ffffff; --seq-ink-2: #ffffff; --seq-ink-3: #0b0b0b; --seq-ink-4: #0b0b0b; --seq-ink-5: #0b0b0b;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--page); color: var(--text-primary); font-size: 14px; line-height: 1.45; }
a { color: var(--accent-ink); }
button, input { font: inherit; color: inherit; }

.topbar {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 12px 16px; border-bottom: 1px solid var(--border); background: var(--surface-1);
}
.topbar h1 { font-size: 16px; margin: 0; font-weight: 600; }
.topbar nav { display: flex; gap: 4px; }
.topbar nav a { padding: 6px 10px; border-radius: 8px; text-decoration: none; color: var(--text-secondary); }
.topbar nav a[aria-current="page"] { background: var(--surface-2); color: var(--text-primary); }
.topbar .spacer { flex: 1; }
.topbar .who { color: var(--text-muted); font-size: 13px; }

.btn {
  border: 1px solid var(--border); background: var(--surface-1); border-radius: 8px;
  padding: 6px 12px; cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--critical); }

.field {
  width: 100%; padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface-1);
}
.field:focus, .btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.card { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); }
.muted { color: var(--text-muted); }
.secondary { color: var(--text-secondary); }
.error { color: var(--critical); }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; padding: 16px 16px 0; }
.kpi { padding: 12px 14px; }
.kpi .value { font-size: 24px; font-weight: 600; }
.kpi .label { color: var(--text-secondary); font-size: 13px; }

.layout {
  display: grid; gap: 12px; padding: 12px 16px 16px;
  grid-template-columns: 300px minmax(0, 1fr) 380px;
  height: calc(100vh - 170px); min-height: 560px;
}
.sidebar, .details { overflow: auto; padding: 12px; }
.map-card { display: flex; flex-direction: column; min-height: 0; }
.map-toolbar { display: flex; gap: 8px 12px; align-items: center; flex-wrap: wrap; padding: 10px 12px; border-bottom: 1px solid var(--border); }
#map, #structure { flex: 1; min-height: 380px; }
[hidden] { display: none !important; }
.toggle { display: inline-flex; gap: 6px; align-items: center; font-size: 13px; color: var(--text-secondary); cursor: pointer; }
.legend .swatch.square { border-radius: 2px; }

.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.segmented button { border: 0; background: transparent; padding: 5px 10px; cursor: pointer; color: var(--text-secondary); }
.segmented button[aria-pressed="true"] { background: var(--surface-2); color: var(--text-primary); font-weight: 600; }

.map-toolbar .legend { flex: 1 1 100%; }
.map-toolbar #reset { margin-left: auto; }
.legend { display: flex; gap: 12px; flex-wrap: wrap; font-size: 13px; color: var(--text-secondary); }
.legend .swatch { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 5px; vertical-align: -1px; }

.macro { margin-bottom: 6px; }
.macro-head {
  width: 100%; text-align: left; border: 0; background: transparent; cursor: pointer;
  padding: 6px 8px; border-radius: 8px; display: flex; justify-content: space-between; gap: 8px; font-weight: 600;
}
.macro-head:hover, .topic-row:hover { background: var(--surface-2); }
.macro-head[aria-expanded="true"] { background: var(--surface-2); }
.topic-row {
  width: 100%; text-align: left; border: 0; background: transparent; cursor: pointer;
  padding: 6px 8px 6px 20px; border-radius: 8px; display: grid; grid-template-columns: 1fr auto; gap: 2px 8px;
}
.topic-row[aria-current="true"] { background: var(--surface-2); box-shadow: inset 3px 0 0 var(--accent); }
.topic-row .share { grid-column: 1 / -1; height: 4px; border-radius: 2px; background: var(--gridline); overflow: hidden; }
.topic-row .share span { display: block; height: 100%; background: var(--series-1); border-radius: 2px; }
.count { color: var(--text-muted); font-variant-numeric: tabular-nums; font-weight: 400; }

.details h2 { font-size: 17px; margin: 0 0 4px; }
.details h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted); margin: 16px 0 6px; }
.stackbar { display: flex; gap: 2px; height: 14px; margin: 8px 0 6px; }
.stackbar span:first-child { border-radius: 4px 0 0 4px; }
.stackbar span:last-child { border-radius: 0 4px 4px 0; }
.stackbar span:only-child { border-radius: 4px; }
.idea-list { list-style: none; margin: 0; padding: 0; }
.idea-list li { border-top: 1px solid var(--border); }
.idea-list button { width: 100%; text-align: left; border: 0; background: transparent; padding: 8px 4px; cursor: pointer; }
.idea-list button:hover { background: var(--surface-2); }
.idea-meta { font-size: 12px; color: var(--text-muted); }
.badge { display: inline-block; font-size: 12px; padding: 1px 7px; border-radius: 999px; background: var(--surface-2); color: var(--text-secondary); }
.idea-text { white-space: pre-wrap; }
.sort { display: flex; gap: 6px; align-items: center; font-size: 13px; }

table.data { width: 100%; border-collapse: collapse; }
table.data th, table.data td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); }
table.data th { font-weight: 600; color: var(--text-secondary); font-size: 13px; }
table.data td.num { font-variant-numeric: tabular-nums; }

.page { max-width: 880px; margin: 0 auto; padding: 16px; display: grid; gap: 16px; }
.page section { padding: 16px; }
.page h2 { font-size: 16px; margin: 0 0 12px; }
.form-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.form-row .field { flex: 1 1 220px; width: auto; }
.notice { padding: 10px 12px; border-radius: 8px; background: var(--surface-2); margin-top: 12px; }
.notice code { font-size: 15px; user-select: all; }

.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login { width: 100%; max-width: 360px; padding: 24px; display: grid; gap: 12px; }
.login h1 { font-size: 18px; margin: 0; }

@media (max-width: 1100px) {
  .layout { grid-template-columns: 1fr; height: auto; }
  .sidebar { max-height: 340px; }
  #map, #structure { height: 460px; }
  .details { max-height: none; }
}

.view-page { display: flex; flex-direction: column; height: 100vh; }
.view-frame { flex: 1; width: 100%; border: 0; background: var(--surface-1); }
