:root {
  color-scheme: light;
  --bg: #f3f3f1;
  --surface-1: #fcfcfb;
  --surface-2: #f0f0ed;
  --border: #e2e1dc;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #7a7974;
  --grid-line: #e9e8e4;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --danger: #c93a39;
  --good: #0a7d32;
  --bad: #c93a39;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #6250d6; --s8: #e34948;
  --live: #0a7d32;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 2px 8px rgba(0,0,0,.04);
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --bg: #111110; --surface-1: #1a1a19; --surface-2: #232321; --border: #2f2f2c;
    --text-primary: #f0efec; --text-secondary: #c3c2b7; --text-muted: #8f8e86; --grid-line: #2a2a27;
    --accent: #3987e5; --danger: #e66767; --good: #4cc27a; --bad: #e66767;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767; --live: #4cc27a;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111110; --surface-1: #1a1a19; --surface-2: #232321; --border: #2f2f2c;
  --text-primary: #f0efec; --text-secondary: #c3c2b7; --text-muted: #8f8e86; --grid-line: #2a2a27;
  --accent: #3987e5; --danger: #e66767; --good: #4cc27a; --bad: #e66767;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767; --live: #4cc27a;
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text-primary);
  font: 14px/1.45 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif; }
.hidden { display: none !important; }
.muted { color: var(--text-muted); }
.small { font-size: 12px; }
.err { color: var(--danger); min-height: 1.2em; }
h1, h2, h3 { margin: 0; font-weight: 600; }
h2 { font-size: 16px; }
input, select, textarea, button { font: inherit; color: inherit; }
input, select, textarea { background: var(--surface-1); border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; }
textarea { width: 100%; font-family: Consolas, monospace; font-size: 13px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-secondary); }

.btn { border: 1px solid var(--border); background: var(--surface-1); border-radius: 6px; padding: 6px 12px; cursor: pointer; white-space: nowrap; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.sm { padding: 4px 9px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { border: 0; background: transparent; cursor: pointer; font-size: 16px; padding: 4px 6px; border-radius: 6px; color: var(--text-secondary); }
.icon-btn:hover { background: var(--surface-2); }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(360px, 100%); background: var(--surface-1); border: 1px solid var(--border); border-radius: 12px; padding: 28px; display: flex; flex-direction: column; gap: 12px; box-shadow: var(--shadow); }
.brand-mark { width: 40px; height: 40px; border-radius: 9px; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 700; letter-spacing: .5px; }
.brand-mark.sm { width: 26px; height: 26px; font-size: 11px; border-radius: 6px; }

/* top bar */
.topbar { display: flex; align-items: center; gap: 18px; padding: 8px 16px; background: var(--surface-1); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 20; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 15px; }
nav { display: flex; gap: 2px; }
.nav { border: 0; background: transparent; padding: 7px 12px; border-radius: 6px; cursor: pointer; color: var(--text-secondary); }
.nav.active { background: var(--surface-2); color: var(--text-primary); font-weight: 600; }
.topright { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.badge { font-size: 12px; padding: 2px 8px; border-radius: 99px; border: 1px solid var(--border); color: var(--text-secondary); display: inline-flex; align-items: center; gap: 5px; }
.badge .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--text-muted); }
.badge.live .dot { background: var(--live); }

.page { padding: 14px 16px 40px; max-width: 1600px; margin: 0 auto; }
.toolbar, .editbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.editbar { background: var(--surface-1); border: 1px dashed var(--accent); border-radius: 8px; padding: 8px 10px; }
.editbar label { flex-direction: row; align-items: center; }
.spacer { flex: 1; }
.dash-select { font-size: 16px; font-weight: 600; padding: 6px 10px; min-width: 260px; max-width: 100%; }

/* filters row */
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-end; margin-bottom: 12px; }
.filters:empty { display: none; }
.fctl { display: flex; flex-direction: column; gap: 3px; position: relative; }
.fctl > span { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: .3px; }
.fbtn { min-width: 150px; max-width: 240px; text-align: left; border: 1px solid var(--border); background: var(--surface-1); border-radius: 6px; padding: 6px 26px 6px 9px; cursor: pointer; position: relative; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fbtn::after { content: "▾"; position: absolute; right: 9px; color: var(--text-muted); }
.fbtn.on { border-color: var(--accent); }
.fctl .x { position: absolute; right: -6px; top: 10px; border: 0; background: var(--danger); color: #fff; border-radius: 50%; width: 16px; height: 16px; font-size: 10px; cursor: pointer; line-height: 16px; padding: 0; }
.pop { position: absolute; top: 100%; left: 0; z-index: 30; margin-top: 4px; width: 280px; background: var(--surface-1); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.15); padding: 8px; }
.pop input[type=search] { width: 100%; margin-bottom: 6px; }
.pop .opts { max-height: 260px; overflow: auto; }
.pop .opt { display: flex; flex-direction: row; align-items: center; gap: 6px; padding: 3px 4px; border-radius: 4px; cursor: pointer; color: var(--text-primary); font-size: 13px; }
.pop .opt:hover { background: var(--surface-2); }
.pop .acts { display: flex; justify-content: space-between; margin-top: 6px; }
.fctl input[type=date], .fctl select { padding: 5px 8px; }

/* grid */
.canvas { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); grid-template-rows: repeat(6, var(--rowH, 110px)); grid-auto-flow: dense; gap: var(--gap, 10px); }
.card { background: var(--surface-1); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow); display: flex; flex-direction: column; min-width: 0; overflow: hidden; }
.card-head { display: flex; align-items: center; gap: 6px; padding: 10px 12px 0; min-height: 30px; }
.card-head h3 { font-size: 13px; color: var(--text-secondary); font-weight: 600; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-tools { display: flex; gap: 2px; opacity: 0; transition: opacity .15s; }
.card:hover .card-tools, .editing .card-tools { opacity: 1; }
.card-tools .icon-btn { font-size: 13px; padding: 2px 5px; }
.src { font-size: 10px; text-transform: uppercase; letter-spacing: .4px; padding: 1px 6px; border-radius: 99px; border: 1px solid var(--border); color: var(--text-muted); }
.src.live { color: var(--live); border-color: currentColor; }
.card-body { flex: 1; min-height: 0; position: relative; padding: 4px 8px 8px; }
.chart { position: absolute; inset: 4px 8px 8px; }
.editing .card { outline: 1px dashed var(--accent); }
.canvas.editing { background-image: linear-gradient(var(--grid-line) 1px, transparent 1px); background-size: 100% calc(var(--rowH) + var(--gap)); border-radius: 8px; }
.card-msg { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; padding: 12px; color: var(--text-muted); font-size: 13px; }
.card-msg.err { color: var(--danger); }
.loading::before { content: ""; position: absolute; top: 0; left: 0; height: 2px; width: 30%; background: var(--accent); animation: load 1s infinite ease-in-out; z-index: 2; }
@keyframes load { 0% { left: -30%; } 100% { left: 100%; } }

.kpi { padding: 2px 6px; display: flex; flex-direction: column; justify-content: center; height: 100%; }
.kpi .val { font-size: 30px; font-weight: 600; letter-spacing: -.5px; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: 12px; color: var(--text-muted); }

/* tables */
.tbl-wrap { position: absolute; inset: 4px 8px 8px; overflow: auto; }
table.tbl { border-collapse: collapse; width: 100%; font-size: 13px; font-variant-numeric: tabular-nums; }
.tbl th { position: sticky; top: 0; background: var(--surface-1); text-align: left; font-weight: 600; color: var(--text-secondary); border-bottom: 1px solid var(--border); padding: 6px 8px; white-space: nowrap; cursor: pointer; z-index: 1; }
.tbl td { padding: 5px 8px; border-bottom: 1px solid var(--grid-line); white-space: nowrap; }
.tbl td.n, .tbl th.n { text-align: right; }
.tbl tr:hover td { background: var(--surface-2); }
.tbl tr.total td { font-weight: 600; border-top: 1px solid var(--border); background: var(--surface-2); position: sticky; bottom: 0; }
.tbl .grp th { text-align: center; border-bottom: 1px solid var(--border); font-size: 11px; text-transform: uppercase; letter-spacing: .3px; color: var(--text-muted); cursor: default; }
.tbl th.sep, .tbl td.sep { border-left: 1px solid var(--border); }
.pct-good { color: var(--good); } .pct-bad { color: var(--bad); }
.pct-good::before { content: "▲ "; font-size: 9px; } .pct-bad::before { content: "▼ "; font-size: 9px; }

/* panels */
.panel { background: var(--surface-1); border: 1px solid var(--border); border-radius: 10px; padding: 14px; margin-bottom: 14px; min-width: 0; }
.panel-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.two-col { display: grid; grid-template-columns: minmax(260px, 1fr) 2fr; gap: 14px; align-items: start; }
.list .item { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 6px; cursor: pointer; border: 1px solid transparent; }
.list .item:hover { background: var(--surface-2); }
.list .item.active { border-color: var(--accent); }
.list .item small { color: var(--text-muted); display: block; }
.drop { display: block; border: 2px dashed var(--border); border-radius: 10px; padding: 28px; text-align: center; cursor: pointer; color: var(--text-secondary); font-size: 14px; }
.drop.over { border-color: var(--accent); background: var(--surface-2); }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px; margin: 10px 0; }
.banner { padding: 10px 12px; border-radius: 8px; background: var(--surface-2); border-left: 3px solid var(--accent); margin: 10px 0; }
.scroll-x { overflow: auto; max-height: 340px; border: 1px solid var(--border); border-radius: 8px; }
.measure-row { display: grid; grid-template-columns: 1.3fr 1fr 1.5fr 1fr auto; gap: 6px; margin-bottom: 6px; align-items: center; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 3px 8px; border-radius: 99px; background: var(--surface-2); border: 1px solid var(--border); font-size: 12px; }
.chip button { border: 0; background: none; cursor: pointer; color: var(--text-muted); padding: 0; }
.checks { display: flex; flex-wrap: wrap; gap: 4px 14px; max-height: 160px; overflow: auto; padding: 6px; border: 1px solid var(--border); border-radius: 6px; }
.checks label { flex-direction: row; align-items: center; color: var(--text-primary); font-size: 13px; }

/* modal & toast */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal-card { background: var(--surface-1); border-radius: 12px; width: min(720px, 100%); max-height: 90vh; overflow: auto; padding: 18px; border: 1px solid var(--border); }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--text-primary); color: var(--surface-1); padding: 9px 16px; border-radius: 8px; z-index: 60; max-width: 90vw; }
.empty { text-align: center; padding: 60px 16px; }
.empty p { max-width: 520px; margin: 8px auto; }

@media (max-width: 900px) {
  .canvas { grid-template-columns: 1fr; grid-template-rows: none; grid-auto-rows: 105px; }
  .canvas .card { grid-column: 1 / -1 !important; }
  .side { position: fixed; z-index: 40; top: 49px; bottom: 0; left: 0; box-shadow: 4px 0 18px rgba(0,0,0,.2); }
  .dash-main { padding: 10px 12px 30px; }
  .two-col { grid-template-columns: 1fr; }
  .dash-select { min-width: 0; width: 100%; }
  .measure-row { grid-template-columns: 1fr 1fr; }
}

/* ---------- v2: sidebar + pages ---------- */
.page.dash-layout { max-width: none; padding: 0; display: flex; min-height: calc(100vh - 49px); }
.side { width: 250px; flex: none; background: var(--surface-1); border-right: 1px solid var(--border); display: flex; flex-direction: column; height: calc(100vh - 49px); position: sticky; top: 49px; overflow: hidden; }
.side.closed { display: none; }
#sideTree { flex: 1; overflow: auto; padding: 8px 6px; }
.side-foot { padding: 8px; border-top: 1px solid var(--border); }
.ws-group { margin-bottom: 6px; }
.ws-name { font-size: 11px; text-transform: uppercase; letter-spacing: .5px; color: var(--text-muted); padding: 8px 8px 4px; display: flex; justify-content: space-between; }
.dash-item { display: flex; align-items: center; gap: 6px; padding: 6px 8px; border-radius: 6px; cursor: pointer; font-weight: 600; font-size: 13px; color: var(--text-primary); }
.dash-item:hover { background: var(--surface-2); }
.dash-item.open { background: var(--surface-2); }
.dash-item .chev { color: var(--text-muted); font-size: 10px; width: 10px; }
.page-item { display: flex; align-items: center; gap: 4px; padding: 5px 8px 5px 26px; border-radius: 6px; cursor: pointer; font-size: 13px; color: var(--text-secondary); border-left: 3px solid transparent; }
.page-item:hover { background: var(--surface-2); }
.page-item.active { color: var(--text-primary); font-weight: 600; border-left-color: var(--accent); background: var(--surface-2); }
.page-item .pname { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.page-item .icon-btn { font-size: 11px; padding: 1px 4px; }
.add-page { margin: 4px 0 4px 26px; }
.dash-main { flex: 1; min-width: 0; padding: 10px 16px 12px; display: flex; flex-direction: column; }
.dash-title { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.dash-title h1 { font-size: 18px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ws-badge { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 4px; background: var(--accent); color: #fff; align-self: center; }
.ws-badge:empty { display: none; }
.toolbar { margin-bottom: 8px; }
.filters { margin-bottom: 8px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.two-col.wide-right { grid-template-columns: minmax(260px, 1fr) 2.2fr; }
.tbl tr.lvl0 td { font-weight: 600; background: var(--surface-2); }
.tbl tr.lvl1 td:first-child { padding-left: 22px; }
.tbl td.empty { color: var(--text-muted); }
.tbl .heat { position: relative; }
.ws-chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; }
.map-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.map-table td, .map-table th { padding: 4px 6px; text-align: left; }
.map-table select { width: 100%; }
.tag { font-size: 11px; padding: 1px 7px; border-radius: 99px; background: var(--surface-2); border: 1px solid var(--border); margin-right: 3px; white-space: nowrap; }
.tag.admin { border-color: var(--accent); color: var(--accent); }
.off { opacity: .5; }
.page-dots { display: none; }

.fctl-row { display: flex; gap: 8px; align-items: flex-end; }
.pct-good.plain::before, .pct-bad.plain::before { content: none; }
.filters .fbtn { min-width: 110px; max-width: 200px; padding-top: 5px; padding-bottom: 5px; }
.filters .fctl > span { font-size: 10px; }
.filters { gap: 6px; }

.tbl.compact { font-size: 12px; }
.tbl.compact th { white-space: normal; vertical-align: bottom; padding: 5px 6px; line-height: 1.2; font-size: 11.5px; }
.tbl.compact td { padding: 4px 6px; }
.tbl.compact td:first-child { white-space: normal; min-width: 90px; }

.card-body { container-type: inline-size; }
@container (max-width: 520px) {
  .tbl.compact { font-size: 11px; }
  .tbl.compact th { font-size: 10.5px; padding: 4px 4px; }
  .tbl.compact td { padding: 3px 4px; }
  .tbl.compact td:first-child { min-width: 70px; }
}
.tbl.pv td:first-child, .tbl.pv th:first-child { white-space: nowrap; max-width: 260px; overflow: hidden; text-overflow: ellipsis; position: sticky; left: 0; background: var(--surface-1); z-index: 1; }
.tbl.pv tr.total td:first-child, .tbl.pv tr.lvl0 td:first-child { background: var(--surface-2); }

.tbl td.fname { max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.note { padding: 6px 4px; font-size: 14px; line-height: 1.7; color: var(--text-secondary); overflow: auto; position: absolute; inset: 4px 12px 8px; }

#limitNote:empty { display: none; }

.fixed-val { height: 28px; display: flex; align-items: center; padding: 0 2px; font-weight: 600; font-size: 12px; white-space: nowrap; max-width: 180px; overflow: hidden; text-overflow: ellipsis; }
