:root {
  color-scheme: light;
  --bg: #f4f4f1;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --line: #e2e1dc;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7a7974;
  --brand: #14213d;
  --accent: #b86e00;
  --accent-bg: #fcf1dd;
  --good: #0f7a2f; --good-bg: #e3f4e7;
  --warn: #9a6400; --warn-bg: #fdf1d6;
  --bad: #b3261e; --bad-bg: #fbe5e3;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100; --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948;
  --grid: #e7e6e1;
  --neutral: #c9c8c2;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 2px 8px rgba(0,0,0,.04);
  --r: 12px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111110; --surface: #1a1a19; --surface-2: #232321; --line: #33332f;
    --text: #ffffff; --text-2: #c3c2b7; --muted: #8f8e86; --brand: #e8eefc;
    --accent: #f0a83a; --accent-bg: #3a2a10;
    --good: #5fd27e; --good-bg: #13301b; --warn: #f0b84a; --warn-bg: #352708; --bad: #ff8a80; --bad-bg: #3a1513;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --grid: #2b2b28; --neutral: #55554f; --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111110; --surface: #1a1a19; --surface-2: #232321; --line: #33332f;
  --text: #ffffff; --text-2: #c3c2b7; --muted: #8f8e86; --brand: #e8eefc;
  --accent: #f0a83a; --accent-bg: #3a2a10;
  --good: #5fd27e; --good-bg: #13301b; --warn: #f0b84a; --warn-bg: #352708; --bad: #ff8a80; --bad-bg: #3a1513;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500; --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --grid: #2b2b28; --neutral: #55554f; --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.5 'IBM Plex Sans Thai', system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h2 { font-size: 18px; font-weight: 600; }
h3 { font-size: 15px; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.err { color: var(--bad); min-height: 1.4em; margin: 0; }
.num { font-variant-numeric: tabular-nums; }

.btn {
  font: inherit; font-size: 14px; border: 1px solid var(--line); background: var(--surface);
  color: var(--text); border-radius: 8px; padding: 6px 12px; cursor: pointer; min-height: 36px;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--brand); color: var(--surface); border-color: var(--brand); font-weight: 600; }
.btn.ghost { background: transparent; }
.btn:focus-visible, select:focus-visible, input:focus-visible, .chip:focus-visible, .tab:focus-visible, .click:focus-visible {
  outline: 2px solid var(--s1); outline-offset: 2px;
}
select, input {
  font: inherit; font-size: 14px; color: var(--text); background: var(--surface);
  border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; min-height: 36px;
}

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(380px, 100%); background: var(--surface); border: 1px solid var(--line);
  border-radius: 16px; padding: 28px; display: grid; gap: 14px; box-shadow: var(--shadow);
}
.login-card h1 { font-size: 22px; }
.login-card p { margin: 0; }
.login-card label { display: grid; gap: 4px; font-size: 13px; color: var(--text-2); }
.brand-mark {
  width: 44px; height: 44px; border-radius: 12px; background: #14213d; color: #fca311;
  display: grid; place-items: center; font-weight: 700; font-size: 22px;
}
.brand-mark.sm { width: 28px; height: 28px; font-size: 15px; border-radius: 8px; }

/* header */
.top { position: sticky; top: 0; z-index: 20; background: var(--bg); border-bottom: 1px solid var(--line); }
.top-row, .filters, .tabs { max-width: 1280px; margin: 0 auto; padding: 0 16px; }
.top-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding-top: 10px; }
.brand { display: flex; align-items: center; gap: 8px; font-size: 16px; white-space: nowrap; }
.top-actions { display: flex; align-items: center; gap: 4px; }
.filters { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; padding-top: 8px; }
.field { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text-2); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  font: inherit; font-size: 13px; border: 1px solid var(--line); background: var(--surface); color: var(--text-2);
  border-radius: 999px; padding: 4px 12px; cursor: pointer; min-height: 32px; display: inline-flex; align-items: center; gap: 6px;
}
.chip[aria-pressed="true"] { background: var(--brand); color: var(--surface); border-color: var(--brand); }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; flex: none; }
.tabs { display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; padding-top: 6px; }
.tabs::-webkit-scrollbar { display: none; }
.tab {
  font: inherit; font-size: 14px; background: none; border: 0; border-bottom: 2px solid transparent;
  color: var(--text-2); padding: 8px 12px; cursor: pointer; white-space: nowrap;
}
.tab[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }

/* layout */
.view { max-width: 1280px; margin: 0 auto; padding: 16px 16px 64px; display: grid; gap: 20px; }
.section { display: grid; gap: 10px; min-width: 0; }
.view > *, .section > *, .grid > * { min-width: 0; }
.section-head { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 8px; }
.section-head p { margin: 0; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 14px; box-shadow: var(--shadow); min-width: 0; }
.grid { display: grid; gap: 10px; }
.card.grid { align-content: start; }
.tiles { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
.two { grid-template-columns: repeat(auto-fit, minmax(min(100%, 460px), 1fr)); }
.three { grid-template-columns: repeat(auto-fit, minmax(min(100%, 320px), 1fr)); }

/* tiles */
.tile {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--r); padding: 12px 14px;
  display: grid; gap: 2px; text-align: left; font: inherit; color: inherit; box-shadow: var(--shadow); min-width: 0;
}
button.tile { cursor: pointer; }
button.tile:hover { border-color: var(--text-2); }
.tile .lbl { font-size: 13px; color: var(--text-2); }
.tile .val { font-size: 26px; font-weight: 600; font-variant-numeric: tabular-nums; line-height: 1.2; }
.tile .sub { font-size: 12.5px; color: var(--muted); }
.tile.hero .val { font-size: 32px; }
.delta { font-size: 12px; font-weight: 500; }
.delta.up { color: var(--good); } .delta.down { color: var(--bad); }
.bar { height: 6px; background: var(--surface-2); border-radius: 4px; overflow: hidden; margin-top: 4px; }
.bar > i { display: block; height: 100%; border-radius: 4px; background: var(--s1); }
.bar.ok > i { background: var(--good); } .bar.low > i { background: var(--warn); }

/* tables */
.tbl-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; border-radius: 8px; }
table { border-collapse: collapse; width: 100%; font-size: 13.5px; font-variant-numeric: tabular-nums; }
th, td { padding: 7px 10px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--text-2); font-weight: 500; font-size: 12.5px; background: var(--surface-2); position: sticky; top: 0; }
th:first-child, td:first-child { text-align: left; position: sticky; left: 0; background: var(--surface); }
th:first-child { background: var(--surface-2); z-index: 1; }
td.l, th.l { text-align: left; white-space: normal; min-width: 180px; }
tr.total td { font-weight: 600; background: var(--surface-2); }
tr.total td:first-child { background: var(--surface-2); }
tr.hl td { background: var(--accent-bg); }
tr.hl td:first-child { background: var(--accent-bg); }
th.sortable { cursor: pointer; user-select: none; }
.click { cursor: pointer; color: inherit; text-decoration: underline dotted var(--muted); text-underline-offset: 3px; background: none; border: 0; font: inherit; padding: 0; }
.click:hover { color: var(--s1); }
.zero { color: var(--muted); }
.tgt { color: var(--muted); font-size: 11.5px; }
.ok-c { color: var(--good); } .low-c { color: var(--bad); }

.badge { display: inline-block; font-size: 11.5px; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); color: var(--text-2); white-space: nowrap; }
.badge.good { background: var(--good-bg); color: var(--good); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.bad { background: var(--bad-bg); color: var(--bad); }
.badge.acc { background: var(--accent-bg); color: var(--accent); }

/* charts */
.chart-box { position: relative; height: 280px; }
.chart-box.sm { height: 220px; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 12.5px; color: var(--text-2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.seg { display: inline-flex; flex-wrap: wrap; gap: 2px; border: 1px solid var(--line); border-radius: 8px; padding: 2px; background: var(--surface); }
.seg button { font: inherit; font-size: 13px; border: 0; background: var(--surface); color: var(--text-2); padding: 5px 10px; cursor: pointer; min-height: 32px; white-space: nowrap; border-radius: 6px; }
.seg button[aria-pressed="true"] { background: var(--brand); color: var(--surface); }
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

/* donut */
.donut { position: relative; height: 190px; }
.donut-c { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; pointer-events: none; }
.donut-c b { font-size: 28px; font-weight: 600; font-variant-numeric: tabular-nums; line-height: 1.1; }
.donut-c span { font-size: 12px; color: var(--muted); }
.donut-legend { display: grid; gap: 4px; font-size: 13px; }
.donut-legend .row { display: flex; align-items: center; gap: 8px; }
.donut-legend .row .v { margin-left: auto; font-variant-numeric: tabular-nums; }

/* rank list */
.rank { display: grid; gap: 6px; }
.rank-row { display: grid; grid-template-columns: 22px 1fr auto; gap: 8px; align-items: center; font-size: 13.5px; }
.rank-row .meter { grid-column: 2 / 4; height: 6px; background: var(--surface-2); border-radius: 4px; overflow: hidden; margin-top: -2px; }
.rank-row .meter i { display: block; height: 100%; background: var(--s1); border-radius: 4px; }
.rank-row .n { color: var(--muted); font-size: 12px; }

.focus-agent { display: grid; gap: 8px; }
.note { font-size: 12.5px; color: var(--text-2); background: var(--surface-2); border-radius: 8px; padding: 8px 10px; margin: 0; }

/* drill */
.drill { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.45); display: flex; justify-content: flex-end; }
.drill-panel { width: min(1000px, 100%); height: 100%; background: var(--bg); display: flex; flex-direction: column; }
.drill-head { display: flex; justify-content: space-between; gap: 8px; padding: 14px 16px 6px; }
.drill-head p { margin: 2px 0 0; }
.drill-tools { padding: 0 16px 8px; }
.drill-tools input { width: 100%; }
.drill-body { flex: 1; overflow: auto; padding: 0 16px 24px; }
.drill-body th { top: 0; }

.tip {
  position: fixed; z-index: 60; pointer-events: none; background: var(--surface); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; font-size: 12.5px; box-shadow: var(--shadow); max-width: 280px;
}
.loading { padding: 48px 0; text-align: center; color: var(--muted); }

@media (max-width: 640px) {
  .top { position: static; }
  .top-row { padding-top: 8px; }
  .brand { font-size: 15px; }
  .top-actions .btn { padding: 6px 8px; }
  .chip { padding: 3px 10px; min-height: 30px; }
  .tile .val { font-size: 22px; }
  .tile.hero .val { font-size: 26px; }
  #stamp { display: none; }
  .chart-box { height: 240px; }
}
