/* MixKey Lab - experimental environment UI. No inline styles anywhere (CSP style-src 'self'). */
:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ee;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #6f6d68;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.12);
  --accent: #1c5cab;
  --accent-ink: #ffffff;
  --accent-wash: rgba(42, 120, 214, 0.10);
  --link: #1c5cab;
  --series-1: #2a78d6;
  --good: #0ca30c;
  --good-text: #006300;
  --warning: #fab219;
  --warning-text: #7a5200;
  --critical: #d03b3b;
  --critical-text: #b02a2a;
  --lab: #7a5200;
  --lab-bg: #fff4d6;
  --radius: 8px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
  --font: system-ui, -apple-system, "Segoe UI", "Noto Sans TC", "PingFang TC", "Microsoft JhengHei", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Noto Sans Mono CJK TC", monospace;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321; --ink: #ffffff; --ink-2: #c3c2b7; --muted: #9a988f;
    --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.12);
    --accent: #256abf; --accent-ink: #ffffff; --accent-wash: rgba(57, 135, 229, 0.16); --link: #86b6ef; --series-1: #3987e5;
    --good-text: #0ca30c; --warning-text: #fab219; --critical-text: #e66767; --lab: #fab219; --lab-bg: #2e2510;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321; --ink: #ffffff; --ink-2: #c3c2b7; --muted: #9a988f;
  --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.12);
  --accent: #256abf; --accent-ink: #ffffff; --accent-wash: rgba(57, 135, 229, 0.16); --link: #86b6ef; --series-1: #3987e5;
  --good-text: #0ca30c; --warning-text: #fab219; --critical-text: #e66767; --lab: #fab219; --lab-bg: #2e2510;
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body { background: var(--page); color: var(--ink); font: 15px/1.55 var(--font); -webkit-text-size-adjust: 100%; }
a { color: var(--link); }
h1, h2, h3, h4 { line-height: 1.3; margin: 0 0 8px; }
h1 { font-size: 22px; }
h2 { font-size: 18px; margin-top: 4px; }
h3 { font-size: 15px; }
p { margin: 0 0 10px; }
code, pre, .mono { font-family: var(--mono); font-size: 13px; }
pre { background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; padding: 10px; overflow: auto; max-height: 520px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.nowrap { white-space: nowrap; }
.num { font-variant-numeric: tabular-nums; text-align: right; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- layout ---------- */
.top { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 12px; padding: 8px 16px; background: var(--surface); border-bottom: 1px solid var(--border); }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; text-decoration: none; color: var(--ink) !important; }
.brand img { width: 28px; height: 28px; }
.lab-badge { font-size: 12px; font-weight: 600; color: var(--lab); background: var(--lab-bg); border: 1px solid var(--border); padding: 1px 8px; border-radius: 999px; white-space: nowrap; }
.top .spacer { flex: 1; }
.top .who { font-size: 13px; color: var(--ink-2); max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.shell { display: grid; grid-template-columns: 210px minmax(0, 1fr); min-height: calc(100vh - 49px); }
nav.side { border-right: 1px solid var(--border); background: var(--surface); padding: 12px 8px; }
nav.side .group { font-size: 12px; color: var(--muted); padding: 10px 10px 4px; }
nav.side a { display: block; padding: 7px 10px; border-radius: 6px; color: var(--ink-2) !important; text-decoration: none; }
nav.side a:hover { background: var(--accent-wash); }
nav.side a.on { background: var(--accent-wash); color: var(--ink) !important; font-weight: 600; }
main { padding: 20px 24px 48px; min-width: 0; }
.page-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 8px 16px; margin-bottom: 14px; }
.page-head p { margin: 0; color: var(--ink-2); }
footer.foot { padding: 16px 24px; color: var(--muted); font-size: 12px; border-top: 1px solid var(--border); }

@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  nav.side { border-right: 0; border-bottom: 1px solid var(--border); display: flex; overflow-x: auto; gap: 4px; padding: 6px 8px; position: sticky; top: 49px; z-index: 15; }
  nav.side .group { display: none; }
  nav.side a { white-space: nowrap; flex: 0 0 auto; }
  main { padding: 16px 16px 40px; }
  .top { padding: 8px 16px; gap: 8px; }
  .top .who { display: none; }
  .brand span.t { display: none; }
}

/* ---------- controls ---------- */
button, .btn { font: inherit; font-size: 14px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--ink); padding: 6px 12px; cursor: pointer; text-decoration: none; display: inline-flex; align-items: center; gap: 6px; min-height: 34px; }
button:hover, .btn:hover { background: var(--surface-2); }
button.primary, .btn.primary { background: var(--accent); color: var(--accent-ink) !important; border-color: transparent; }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--critical-text); }
button.ghost { background: transparent; }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, [tabindex]:focus-visible { outline: 2px solid var(--series-1); outline-offset: 2px; }
input, select, textarea { font: inherit; font-size: 14px; color: var(--ink); background: var(--surface); border: 1px solid var(--axis); border-radius: 6px; padding: 6px 8px; min-height: 34px; max-width: 100%; }
textarea { width: 100%; min-height: 90px; resize: vertical; }
textarea.code { font-family: var(--mono); font-size: 13px; min-height: 420px; line-height: 1.45; tab-size: 2; }
label { font-size: 13px; color: var(--ink-2); }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; min-height: 30px; padding: 4px 10px; }
.seg button.on { background: var(--accent-wash); font-weight: 600; }
.filters { display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center; margin: 0 0 16px; }
.filters .field { display: flex; align-items: center; gap: 6px; }

/* ---------- cards, grids, tables ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); min-width: 0; }
.card + .card { margin-top: 14px; }
.card h2 { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.grid { display: grid; gap: 14px; }
.grid > .card, .grid > section { margin-top: 0; }
figure { margin: 0; }
main > div > h2 { margin-top: 24px; }
.grid.c2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.grid.c3 { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.grid.c4 { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
.grid + .card, .card + .grid, .grid + .grid { margin-top: 14px; }
.tablewrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--grid); vertical-align: top; }
th { color: var(--ink-2); font-weight: 600; font-size: 13px; white-space: nowrap; }
tbody tr:hover { background: var(--accent-wash); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
dl.kv { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 4px 14px; margin: 0; }
dl.kv dt { color: var(--muted); font-size: 13px; }
dl.kv dd { margin: 0; overflow-wrap: anywhere; }

/* stat tiles */
.stat { display: flex; flex-direction: column; gap: 2px; }
.stat .label { font-size: 13px; color: var(--ink-2); }
.stat .value { font-size: 26px; font-weight: 600; line-height: 1.2; overflow-wrap: anywhere; }
.stat .sub { font-size: 12px; color: var(--muted); }

/* status pills (icon + label, never color alone) */
.pill { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; font-weight: 600; padding: 1px 8px; border-radius: 999px; border: 1px solid var(--border); white-space: nowrap; color: var(--ink-2); background: var(--surface-2); }
.pill::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--axis); }
.pill.good { color: var(--good-text); } .pill.good::before { background: var(--good); }
.pill.warn { color: var(--warning-text); } .pill.warn::before { background: var(--warning); }
.pill.bad { color: var(--critical-text); } .pill.bad::before { background: var(--critical); }
.pill.info::before { background: var(--series-1); }

/* notices */
.notice { border: 1px solid var(--border); border-left: 4px solid var(--series-1); background: var(--surface); border-radius: 6px; padding: 10px 12px; margin: 0 0 14px; }
.notice.warn { border-left-color: var(--warning); }
.notice.bad { border-left-color: var(--critical); }
.notice.good { border-left-color: var(--good); }
.notice ul { margin: 6px 0 0; padding-left: 20px; }
.toast { position: fixed; right: 16px; bottom: 16px; z-index: 50; max-width: min(420px, calc(100vw - 32px)); }
.toast .notice { box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15); }

/* invention title + elements */
.hero { padding: 18px 20px; }
.hero .title { font-size: 20px; font-weight: 600; line-height: 1.45; margin: 4px 0 6px; }
.hero .title-en { color: var(--ink-2); font-size: 14px; }
.elem { display: flex; flex-direction: column; gap: 6px; text-decoration: none; color: var(--ink) !important; }
.elem:hover { border-color: var(--series-1); }
.elem .k { font-size: 12px; color: var(--muted); }
.elem .n { font-size: 16px; font-weight: 600; }
.elem .d { font-size: 13px; color: var(--ink-2); }
.elem .pill { white-space: normal; }
ol.flow { list-style: none; counter-reset: s; padding: 0; margin: 0; display: grid; gap: 8px; }
ol.flow li { counter-increment: s; display: grid; grid-template-columns: 30px minmax(0, 1fr) auto; gap: 10px; align-items: start; padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); }
ol.flow li::before { content: counter(s); width: 26px; height: 26px; border-radius: 50%; background: var(--accent-wash); color: var(--ink); display: grid; place-items: center; font-weight: 600; font-size: 13px; }
ol.flow .t { font-weight: 600; }
ol.flow .d { font-size: 13px; color: var(--ink-2); }

/* forms */
.form { display: grid; gap: 12px; }
.form .row { display: grid; grid-template-columns: 200px minmax(0, 1fr); gap: 8px 12px; align-items: start; }
.form .row > label { padding-top: 7px; }
.form .hint { font-size: 12px; color: var(--muted); margin-top: 2px; }
.form .actions { display: flex; flex-wrap: wrap; gap: 8px; }
.form .req::after { content: " *"; color: var(--critical-text); }
@media (max-width: 640px) { .form .row { grid-template-columns: 1fr; } .form .row > label { padding-top: 0; } }
.settings td input[type="number"] { width: 110px; }
@media (max-width: 640px) {
  table.settings thead { display: none; }
  table.settings tr { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 12px; padding: 10px 0; border-bottom: 1px solid var(--grid); }
  table.settings td { border: 0; padding: 0; }
  table.settings td:first-child { grid-column: 1 / -1; font-weight: 600; }
  table.settings td:last-child { grid-column: 1 / -1; }
  table.settings td[data-label]::before { content: attr(data-label); display: block; font-size: 12px; font-weight: 400; color: var(--muted); }
}
.diffrow.add td { background: rgba(12, 163, 12, 0.08); }
.diffrow.del td { background: rgba(208, 59, 59, 0.08); }
.errlist li { margin-bottom: 4px; }
.errlist code { color: var(--ink-2); }
.editor-grid { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr); gap: 14px; align-items: start; }
@media (max-width: 1000px) { .editor-grid { grid-template-columns: 1fr; } }

/* ---------- charts ---------- */
.charts { display: grid; gap: 14px; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); }
@media (max-width: 520px) { .charts { grid-template-columns: 1fr; } }
.chart { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px 10px; min-width: 0; }
.chart-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.chart-head .ttl { font-weight: 600; font-size: 14px; }
.chart-head .sub { font-size: 12px; color: var(--muted); }
.chart-head .last { text-align: right; }
.chart-head .last .v { font-size: 20px; font-weight: 600; line-height: 1.2; }
.chart-head .last .u { font-size: 12px; color: var(--muted); margin-left: 3px; }
.plot { position: relative; margin-top: 6px; }
.plot svg { display: block; width: 100%; overflow: visible; }
.plot.loading { opacity: 0.45; transition: opacity 0.15s; }
.plot .hit { fill: transparent; cursor: crosshair; }
.plot .hit:focus { outline: none; }
.plot .hit:focus-visible { stroke: var(--series-1); stroke-width: 1; }
svg .gridline { stroke: var(--grid); stroke-width: 1; shape-rendering: crispEdges; }
svg .baseline { stroke: var(--axis); stroke-width: 1; shape-rendering: crispEdges; }
svg .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; font-family: var(--font); }
svg .series { fill: none; stroke: var(--series-1); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
svg .wash { fill: var(--series-1); opacity: 0.10; stroke: none; }
svg .band-on { fill: var(--series-1); }
svg .band-track { fill: var(--surface-2); stroke: var(--grid); stroke-width: 1; }
svg .range { fill: var(--good); opacity: 0.07; }
svg .marker-line { stroke: var(--axis); stroke-width: 1; shape-rendering: crispEdges; }
svg .marker-text { fill: var(--muted); font-size: 11px; font-family: var(--font); }
svg .fault { fill: var(--critical); stroke: var(--surface); stroke-width: 2; }
svg .crosshair { stroke: var(--ink-2); stroke-width: 1; shape-rendering: crispEdges; pointer-events: none; }
svg .dot { fill: var(--series-1); stroke: var(--surface); stroke-width: 2; pointer-events: none; }
svg .ref { stroke: var(--critical); stroke-width: 1; shape-rendering: crispEdges; }
svg .ref-text { fill: var(--critical-text); font-size: 11px; font-family: var(--font); }
.tip { position: absolute; pointer-events: none; z-index: 5; background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 6px 9px; font-size: 12px; box-shadow: 0 4px 14px rgba(0, 0, 0, 0.14); min-width: 120px; white-space: nowrap; }
.tip .tv { font-size: 15px; font-weight: 600; display: flex; align-items: center; gap: 6px; }
.tip .tv::before { content: ""; width: 12px; height: 2px; background: var(--series-1); border-radius: 1px; }
.tip .tl { color: var(--ink-2); }
.tip .tw { color: var(--critical-text); font-weight: 600; }
details.tbl { margin-top: 6px; }
details.tbl summary { cursor: pointer; font-size: 12px; color: var(--ink-2); }
details.tbl .tablewrap { max-height: 260px; overflow: auto; margin-top: 6px; }
details.tbl table { font-size: 12px; }
.empty { padding: 28px 12px; text-align: center; color: var(--muted); border: 1px dashed var(--axis); border-radius: var(--radius); }

/* login */
.login { max-width: 520px; margin: 10vh auto; text-align: center; }
.login .card { padding: 28px 24px; }
.login img { width: 56px; height: 56px; }
.spinner { width: 18px; height: 18px; border: 2px solid var(--grid); border-top-color: var(--series-1); border-radius: 50%; animation: spin 0.8s linear infinite; display: inline-block; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation: none; } .plot.loading { transition: none; } }
@media (forced-colors: active) { svg .series { stroke: CanvasText; } svg .band-on { fill: CanvasText; } .pill::before { forced-color-adjust: none; } }
@media print { nav.side, .top, .filters, button { display: none !important; } .shell { display: block; } }
