:root {
  color-scheme: light;
  --page: #f4f4f1;
  --surface: #fcfcfb;
  --surface-2: #f0efeb;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --accent-wash: rgba(42, 120, 214, 0.10);
  --track: #cde2fb;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-other: #a9a79f;
  --neutral: #d6d5cf;
  --negative: #e34948;
  --good: #0ca30c;
  --good-ink: #006300;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --critical-ink: #b42f2f;
  --lost-bg: #fff3d1;
  --lost-ink: #6b4a00;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.04), 0 1px 1px rgba(11, 11, 11, 0.03);
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic UI", "Segoe UI", system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232322;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --accent-ink: #86b6ef;
    --accent-wash: rgba(57, 135, 229, 0.12);
    --track: #184f95;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-other: #6d6c66;
    --neutral: #4a4945;
    --negative: #e66767;
    --good-ink: #0ca30c;
    --critical-ink: #e66767;
    --lost-bg: #3a2e10;
    --lost-ink: #f5d38a;
    --shadow: none;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232322;
  --ink: #ffffff;
  --ink-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --accent: #3987e5;
  --accent-ink: #86b6ef;
  --accent-wash: rgba(57, 135, 229, 0.12);
  --track: #184f95;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #008300;
  --series-other: #6d6c66;
  --neutral: #4a4945;
  --negative: #e66767;
  --good-ink: #0ca30c;
  --critical-ink: #e66767;
  --lost-bg: #3a2e10;
  --lost-ink: #f5d38a;
  --shadow: none;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--page);
  color: var(--ink);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent-ink); }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: 224px minmax(0, 1fr); min-height: 100vh; }
.sidebar {
  position: sticky; top: 0; height: 100vh;
  display: flex; flex-direction: column; gap: 20px;
  padding: 20px 14px;
  background: var(--surface);
  border-right: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; padding: 0 6px; }
.brand-mark {
  width: 30px; height: 30px; flex: none; border-radius: 7px;
  background: linear-gradient(var(--accent) 0 30%, var(--surface) 30%);
  border: 2px solid var(--accent);
}
.brand-name { font-weight: 700; font-size: 15px; letter-spacing: .02em; }
.brand-sub { font-size: 11px; color: var(--muted); }
nav { display: flex; flex-direction: column; gap: 2px; }
nav a {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 10px; border-radius: 7px;
  color: var(--ink-2); text-decoration: none; font-weight: 500;
}
nav a:hover { background: var(--surface-2); color: var(--ink); }
nav a.active { background: var(--accent-wash); color: var(--accent-ink); font-weight: 700; }
.nav-badge {
  min-width: 20px; padding: 0 6px; border-radius: 999px;
  background: var(--lost-bg); color: var(--lost-ink);
  font-size: 11px; font-weight: 700; text-align: center; line-height: 20px;
}
.edition-picker { margin-top: auto; display: grid; gap: 4px; padding: 0 6px; font-size: 12px; color: var(--muted); }
.user-line { padding: 0 6px; font-size: 11px; color: var(--muted); overflow-wrap: anywhere; }
.user-actions { display: flex; gap: 10px; margin-top: 4px; }
.linkbtn { background: none; border: 0; padding: 0; color: var(--accent-ink); font-size: 11px; cursor: pointer; text-decoration: underline; }
.auth { min-height: 100vh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card { width: min(400px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); padding: 24px; display: grid; gap: 12px; }
.auth-card h1 { margin: 8px 0 0; font-size: 20px; }
.auth-form { display: grid; gap: 12px; }
.auth-form .btn.primary { justify-content: center; padding: 10px; }
.secret { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 18px; letter-spacing: .06em; padding: 10px 12px; background: var(--surface-2); border-radius: 8px; user-select: all; overflow-wrap: anywhere; }
.edition-picker select { padding: 6px 8px; border-radius: 7px; border: 1px solid var(--border); background: var(--surface); }

main { min-width: 0; padding: 24px 28px 48px; }
.topbar { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.topbar h1 { margin: 0; font-size: 22px; line-height: 1.3; }
.page-sub { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
.page-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.view { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: 16px; }
.span-12 { grid-column: span 12; }
.span-8 { grid-column: span 8; }
.span-7 { grid-column: span 7; }
.span-6 { grid-column: span 6; }
.span-5 { grid-column: span 5; }
.span-4 { grid-column: span 4; }

/* ---------- cards & tiles ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 18px 20px; min-width: 0;
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.card-title { margin: 0; font-size: 15px; font-weight: 700; }
.card-sub { margin: 2px 0 0; font-size: 12px; color: var(--muted); }
.card-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.tiles { grid-column: span 12; display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 16px; }
.tile { display: flex; flex-direction: column; gap: 4px; }
.tile-label { font-size: 12px; color: var(--ink-2); font-weight: 600; }
.tile-value { font-size: 26px; font-weight: 700; line-height: 1.2; letter-spacing: -.01em; }
.tile-value .unit { font-size: 13px; font-weight: 600; color: var(--ink-2); margin-left: 3px; }
.tile-value .of { font-size: 15px; font-weight: 600; color: var(--muted); }
.tile-sub { font-size: 12px; color: var(--muted); }
.tile-value { overflow-wrap: anywhere; }
.tile.hero { grid-column: span 2; }
.tile.hero .tile-value { font-size: clamp(30px, 2.6vw, 48px); overflow-wrap: normal; white-space: nowrap; }
.tile.hero { background: linear-gradient(180deg, var(--accent-wash), transparent 70%), var(--surface); }
.meter { height: 8px; border-radius: 999px; background: var(--track); overflow: hidden; margin: 6px 0 2px; }
.meter > span { display: block; height: 100%; background: var(--accent); border-radius: 999px; }
.status-line { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; color: var(--ink-2); }

/* ---------- status ---------- */
.status-icon {
  width: 18px; height: 18px; flex: none; border-radius: 50%;
  display: inline-grid; place-items: center;
  font-size: 11px; font-weight: 800; color: #0b0b0b; line-height: 1;
}
.status-icon.warning { background: var(--warning); }
.status-icon.serious { background: var(--serious); }
.status-icon.critical { background: var(--critical); color: #fff; }
.status-icon.good { background: var(--good); color: #fff; }
.status-icon.info { background: var(--surface-2); color: var(--ink-2); border: 1px solid var(--border); }

.alerts { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.alert { display: grid; grid-template-columns: auto 1fr auto; gap: 10px; align-items: start; padding: 10px 4px; border-top: 1px solid var(--grid); }
.alert:first-child { border-top: 0; }
.alert-body { min-width: 0; }
.alert-title { font-weight: 600; }
.alert-status { font-size: 11px; font-weight: 700; color: var(--ink-2); margin-right: 6px; }
.alert-detail { font-size: 12px; color: var(--ink-2); margin-top: 2px; overflow-wrap: anywhere; }
.alert a { font-size: 12px; white-space: nowrap; }

/* ---------- buttons & chips ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 14px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); color: var(--ink); font-weight: 600; font-size: 13px;
  cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.sm { padding: 3px 9px; font-size: 12px; font-weight: 500; }
.btn.danger { color: var(--critical-ink); }
.btn:focus-visible, a:focus-visible, select:focus-visible, input:focus-visible, textarea:focus-visible, [tabindex]:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.chip {
  display: inline-block; padding: 1px 8px; border-radius: 999px;
  background: var(--surface-2); color: var(--ink-2); font-size: 12px; font-weight: 600; white-space: nowrap;
}
.chip.good { color: var(--good-ink); }
.chip.bad { color: var(--critical-ink); }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; flex: none; }
.muted { color: var(--muted); }
.nowrap { white-space: nowrap; display: inline-flex; align-items: center; }
.chip.muted { color: var(--muted); }
.table tbody tr.current { background: var(--accent-wash); }
.note-box { font-size: 12px; color: var(--ink-2); background: var(--surface-2); border-radius: 8px; padding: 10px 12px; overflow-wrap: anywhere; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; margin: 0 -4px; }
.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th, .table td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--grid); vertical-align: middle; }
.table th { font-size: 12px; font-weight: 600; color: var(--muted); white-space: nowrap; background: var(--surface); }
.table td.num, .table th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table tbody tr:hover { background: var(--surface-2); }
.table tfoot td { font-weight: 700; border-bottom: 0; border-top: 1px solid var(--axis); }
.table tfoot tr + tr td { border-top: 0; font-weight: 600; color: var(--ink-2); }
.table td.empty { text-align: center; color: var(--muted); padding: 24px; }
.table .wrap { min-width: 180px; max-width: 360px; overflow-wrap: anywhere; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }
.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 12px; }
.filters label { display: inline-flex; gap: 6px; align-items: center; font-size: 12px; color: var(--ink-2); }
.filters select { padding: 5px 8px; border-radius: 7px; border: 1px solid var(--border); background: var(--surface); }

/* ---------- charts ---------- */
.chart { display: block; overflow: visible; }
.chart text { fill: var(--muted); font-size: 11px; font-family: var(--font); }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; }
.chart .crosshair { stroke: var(--ink-2); stroke-width: 1; }

.stack { display: flex; gap: 2px; height: 28px; }
.stack-seg { min-width: 3px; flex-basis: 0; cursor: default; }
.stack-seg:first-child { border-radius: 4px 0 0 4px; }
.stack-seg:last-child { border-radius: 0 4px 4px 0; }
.stack-seg:hover, .stack-seg:focus-visible { filter: brightness(1.1) saturate(1.1); }

.legend { display: flex; flex-wrap: wrap; gap: 6px 18px; margin-top: 12px; font-size: 12px; color: var(--ink-2); }
.legend-item { display: inline-flex; align-items: center; }
.legend-item b { color: var(--ink); margin-left: 6px; font-weight: 700; font-variant-numeric: tabular-nums; }
.legend-item .pct { color: var(--muted); margin-left: 4px; }
.legend .key-tick { display: inline-block; width: 2px; height: 12px; background: var(--ink); margin: 0 8px 0 4px; }

.hbars { display: grid; gap: 6px; }
.hbar { display: grid; grid-template-columns: minmax(90px, 30%) 1fr; gap: 12px; align-items: center; font-size: 12px; }
.hbar-label { color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hbar-plot { position: relative; height: 24px; display: flex; align-items: center; gap: 8px; }
.hbar-plot .bar { height: 14px; border-radius: 0 4px 4px 0; background: var(--series-1); min-width: 2px; }
.hbar-plot .val { color: var(--ink); font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }

.bullet { display: grid; grid-template-columns: minmax(90px, 26%) 1fr auto; gap: 12px; align-items: center; font-size: 12px; padding: 3px 0; border-radius: 6px; }
.bullet:hover, .bullet:focus-visible { background: var(--surface-2); }
.bullet-plot { position: relative; height: 22px; }
.bullet-plot .track { position: absolute; left: 0; top: 2px; height: 18px; background: var(--track); border-radius: 0 4px 4px 0; }
.bullet-plot .bar { position: absolute; left: 0; top: 6px; height: 10px; background: var(--series-1); border-radius: 0 4px 4px 0; }
.bullet-plot .tick { position: absolute; top: 0; width: 2px; height: 22px; background: var(--ink); margin-left: -1px; }
.bullet-val { font-variant-numeric: tabular-nums; color: var(--ink-2); white-space: nowrap; text-align: right; min-width: 88px; }
.bullet-val b { color: var(--ink); }

.table tr.section-row td { background: var(--surface-2); padding-top: 6px; padding-bottom: 6px; }
.section-label { font-size: 12px; color: var(--ink-2); }
.kv-table th { text-align: left; font-weight: 500; color: var(--ink-2); font-size: 12px; white-space: normal; }
.kv-table tr.strong-row th, .kv-table tr.strong-row td { font-weight: 700; color: var(--ink); background: var(--accent-wash); }
.view > .migrated-banner { background: var(--lost-bg); color: var(--lost-ink); font-weight: 600; }
.view > .note-box { grid-column: span 12; display: flex; gap: 8px; align-items: flex-start; font-size: 13px; }
.sim-table tbody th { text-align: left; font-weight: 500; color: var(--ink-2); white-space: normal; min-width: 180px; font-size: 12px; }
.sim-table tr.strong-row th, .sim-table tr.strong-row td { font-weight: 700; color: var(--ink); background: var(--accent-wash); }
.div-plot { position: relative; height: 20px; }
.div-axis { position: absolute; left: 50%; top: -2px; bottom: -2px; width: 1px; background: var(--axis); }
.div-bar { position: absolute; top: 4px; height: 12px; }
.div-bar.pos { background: var(--series-1); border-radius: 0 4px 4px 0; }
.div-bar.neg { background: var(--negative); border-radius: 4px 0 0 4px; }
.div-plot .tick { position: absolute; top: 0; width: 2px; height: 20px; background: var(--ink); margin-left: -1px; }
.ed-rows { display: grid; gap: 8px; }
.ed-row { display: grid; grid-template-columns: 56px 1fr 84px; gap: 12px; align-items: center; font-size: 12px; }
.ed-label { color: var(--ink-2); font-weight: 600; }
.ed-row.current .ed-label { color: var(--accent-ink); }
.ed-val { text-align: right; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.ed-val b { color: var(--ink); }
.range-row { display: grid; grid-template-columns: minmax(90px, 30%) 1fr auto; gap: 12px; align-items: center; font-size: 12px; padding: 2px 0; }
.range-plot { position: relative; height: 20px; }
.range-plot .min { position: absolute; left: 0; top: 4px; height: 12px; background: var(--series-1); border-radius: 0 4px 4px 0; }
.range-plot .ext { position: absolute; top: 4px; height: 12px; background: var(--track); border-radius: 0 4px 4px 0; }

.tooltip {
  position: fixed; z-index: 50; pointer-events: none;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.14);
  padding: 8px 10px; font-size: 12px; min-width: 120px; max-width: 280px;
}
.tooltip .tt-title { color: var(--muted); font-size: 11px; margin-bottom: 4px; }
.tooltip .tt-row { display: flex; align-items: center; gap: 8px; }
.tooltip .tt-row b { font-size: 13px; font-variant-numeric: tabular-nums; }
.tooltip .tt-row span { color: var(--ink-2); }
.tooltip .tt-key { width: 12px; height: 2px; border-radius: 1px; flex: none; }

/* ---------- dialog ---------- */
dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 0;
  background: var(--surface); color: var(--ink);
  width: min(520px, calc(100vw - 32px));
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.35); }
dialog form { padding: 20px 22px; display: grid; gap: 12px; max-height: calc(100vh - 64px); overflow-y: auto; }
dialog h2 { margin: 0 0 4px; font-size: 17px; }
.field { display: grid; gap: 4px; font-size: 12px; font-weight: 600; color: var(--ink-2); }
.field em { color: var(--critical-ink); font-style: normal; }
.field input, .field select, .field textarea {
  width: 100%; padding: 8px 10px; border-radius: 8px;
  border: 1px solid var(--axis); background: var(--surface); font-size: 14px; font-weight: 400; color: var(--ink);
}
.field small { font-weight: 400; color: var(--muted); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-error { margin: 0; color: var(--critical-ink); font-size: 13px; font-weight: 600; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 60;
  background: var(--ink); color: var(--surface); padding: 9px 16px; border-radius: 999px;
  font-size: 13px; font-weight: 600; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.2);
}

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  .span-8, .span-7, .span-6, .span-5, .span-4 { grid-column: span 12; }
}
@media (max-width: 760px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar {
    position: sticky; top: 0; z-index: 10; height: auto;
    flex-direction: row; flex-wrap: wrap; align-items: center; gap: 10px;
    padding: 10px 16px; border-right: 0; border-bottom: 1px solid var(--border);
  }
  .brand-sub { display: none; }
  nav { order: 3; flex: 1 1 100%; min-width: 0; flex-direction: row; overflow-x: auto; gap: 4px; }
  nav a { white-space: nowrap; padding: 6px 10px; }
  .edition-picker { margin: 0 0 0 auto; grid-template-columns: auto auto; align-items: center; }
  .user-line { order: 4; flex: 1 1 100%; display: flex; justify-content: space-between; align-items: center; padding: 0; }
  .user-line[hidden] { display: none; }
  .user-actions { margin-top: 0; }
  main { padding: 16px 16px 40px; }
  .tile.hero { grid-column: 1 / -1; }
  .tile-value { font-size: 19px; white-space: nowrap; overflow-wrap: normal; }
  .tile-value .of { font-size: 13px; }
  .tile.hero .tile-value { font-size: 40px; overflow-wrap: normal; }
  .tiles { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
  .field-row { grid-template-columns: 1fr; }
  .hbar, .bullet, .range-row { grid-template-columns: minmax(70px, 32%) 1fr auto; }
  .hbar { grid-template-columns: minmax(70px, 32%) 1fr; }
}
