/* โทเคนสี: ชุด default ของ dataviz skill — ผ่าน validator ทั้ง light/dark
   (ordinal ramp น้ำเงิน 2 ขั้นสำหรับกราฟ + status palette คงที่สำหรับสถานะบอท) */
.viz-root {
  color-scheme: light;
  --plane:          #f9f9f7;
  --surface-1:      #fcfcfb;
  --surface-2:      #f2f1ed;
  --text-primary:   #0b0b0b;
  --text-secondary: #52514e;
  --text-muted:     #898781;
  --grid:           #e1e0d9;
  --baseline:       #c3c2b7;
  --hairline:       rgba(11, 11, 11, .10);
  --series-ctx:     #86b6ef;  /* สแกนทั่วไป (ขั้นอ่อน)  2.06:1 vs surface */
  --series-hit:     #2a78d6;  /* เจอของ (ขั้นเข้ม) */
  --accent:         #2a78d6;
  --accent-ink:     #ffffff;
  --shadow:         0 1px 2px rgba(11, 11, 11, .05), 0 8px 24px rgba(11, 11, 11, .05);
}
.viz-root {
  --good:     #0ca30c;
  --warning:  #fab219;
  --serious:  #ec835a;
  --critical: #d03b3b;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) .viz-root {
    color-scheme: dark;
    --plane:          #0d0d0d;
    --surface-1:      #1a1a19;
    --surface-2:      #232322;
    --text-primary:   #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted:     #898781;
    --grid:           #2c2c2a;
    --baseline:       #383835;
    --hairline:       rgba(255, 255, 255, .10);
    --series-ctx:     #184f95;
    --series-hit:     #3987e5;
    --accent:         #3987e5;
    --shadow:         0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
  }
}
:root[data-theme="dark"] .viz-root {
  color-scheme: dark;
  --plane:          #0d0d0d;
  --surface-1:      #1a1a19;
  --surface-2:      #232322;
  --text-primary:   #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted:     #898781;
  --grid:           #2c2c2a;
  --baseline:       #383835;
  --hairline:       rgba(255, 255, 255, .10);
  --series-ctx:     #184f95;
  --series-hit:     #3987e5;
  --accent:         #3987e5;
  --shadow:         0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", "Noto Sans Thai", "Sarabun", sans-serif;
  background: var(--plane);
  color: var(--text-primary);
  -webkit-font-smoothing: antialiased;
}
.viz-root { min-height: 100%; background: var(--plane); padding-bottom: calc(72px + env(safe-area-inset-bottom)); }

/* ---- topbar ---- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(12px + env(safe-area-inset-top)) 16px 12px;
  background: color-mix(in srgb, var(--plane) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--hairline);
}
.topbar-title { display: flex; flex-direction: column; line-height: 1.2; }
.brand { font-size: 15px; font-weight: 650; letter-spacing: -.01em; }
.brand-sub { font-size: 12px; color: var(--text-muted); }
.icon-btn {
  border: 1px solid var(--hairline); background: var(--surface-1); color: var(--text-secondary);
  width: 36px; height: 36px; border-radius: 10px; font-size: 15px; cursor: pointer;
}
.icon-btn:active { background: var(--surface-2); }

.view { padding: 16px; display: flex; flex-direction: column; gap: 14px; max-width: 720px; margin: 0 auto; }
.boot, .empty { color: var(--text-muted); font-size: 14px; text-align: center; padding: 40px 0; }

/* ---- card ---- */
.card {
  background: var(--surface-1);
  border: 1px solid var(--hairline);
  border-radius: 16px;
  padding: 16px;
  box-shadow: var(--shadow);
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.card-title { font-size: 13px; font-weight: 650; letter-spacing: .01em; }
.card-note { font-size: 12px; color: var(--text-muted); }

/* ---- hero: สถานะบอท ---- */
.hero { display: flex; align-items: center; gap: 14px; }
.hero-dot { width: 12px; height: 12px; border-radius: 99px; flex: none; box-shadow: 0 0 0 4px color-mix(in srgb, currentColor 18%, transparent); }
.hero-main { flex: 1; min-width: 0; }
.hero-value { font-size: 26px; font-weight: 680; letter-spacing: -.02em; line-height: 1.15; }
.hero-meta { font-size: 12.5px; color: var(--text-secondary); margin-top: 3px; }
.state-good     { color: var(--good); }
.state-warning  { color: var(--warning); }
.state-critical { color: var(--critical); }

/* ---- stat tiles ---- */
.kpi { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
@media (min-width: 520px) { .kpi { grid-template-columns: repeat(4, 1fr); } }
.tile {
  background: var(--surface-1); border: 1px solid var(--hairline); border-radius: 14px;
  padding: 12px 13px; box-shadow: var(--shadow);
}
.tile-label { font-size: 11.5px; color: var(--text-muted); }
.tile-value { font-size: 24px; font-weight: 660; letter-spacing: -.02em; margin-top: 2px; line-height: 1.1; }
.tile-delta { font-size: 11.5px; color: var(--text-secondary); margin-top: 2px; }

/* ---- chart ---- */
.chart-wrap { position: relative; }
.chart { display: block; width: 100%; height: auto; touch-action: pan-y; }
.legend { display: flex; flex-wrap: wrap; gap: 14px; margin-top: 10px; }
.legend-item { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-secondary); }
.legend-key { width: 11px; height: 11px; border-radius: 3px; flex: none; }
.tip {
  position: absolute; pointer-events: none; z-index: 5;
  background: var(--surface-1); border: 1px solid var(--hairline); border-radius: 10px;
  box-shadow: var(--shadow); padding: 8px 10px; font-size: 12px; min-width: 132px;
  transform: translate(-50%, -100%); transition: opacity .1s;
}
.tip-when { color: var(--text-muted); font-size: 11px; margin-bottom: 4px; }
.tip-row { display: flex; align-items: center; gap: 7px; }
.tip-key { width: 10px; height: 2px; border-radius: 2px; flex: none; }
.tip-val { font-weight: 650; font-variant-numeric: tabular-nums; }
.tip-name { color: var(--text-secondary); }
.link-btn { background: none; border: 0; color: var(--text-muted); font: inherit; font-size: 12px; cursor: pointer; padding: 0; text-decoration: underline; }
table.data { width: 100%; border-collapse: collapse; font-size: 12px; margin-top: 10px; }
table.data th, table.data td { text-align: right; padding: 5px 6px; border-bottom: 1px solid var(--grid); font-variant-numeric: tabular-nums; }
table.data th:first-child, table.data td:first-child { text-align: left; font-variant-numeric: normal; }
table.data th { color: var(--text-muted); font-weight: 550; }

/* ---- buttons ---- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  border: 1px solid var(--hairline); border-radius: 12px; background: var(--surface-2);
  color: var(--text-primary); font: inherit; font-size: 14px; font-weight: 600;
  padding: 12px 16px; cursor: pointer; min-height: 46px;
}
.btn:active { transform: scale(.985); }
.btn[disabled] { opacity: .5; cursor: default; }
.btn-primary { background: var(--accent); border-color: transparent; color: var(--accent-ink); }
.btn-good { background: var(--good); border-color: transparent; color: #fff; }
.btn-danger { color: var(--critical); }
.btn-block { width: 100%; }
.btn-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* ---- group toggles ---- */
.grouplist { display: flex; flex-direction: column; }
.group-row {
  display: flex; align-items: center; gap: 12px; padding: 11px 0;
  border-top: 1px solid var(--grid);
}
.group-row:first-child { border-top: 0; }
.group-name { flex: 1; min-width: 0; font-size: 14px; }
.group-name small { display: block; color: var(--text-muted); font-size: 11.5px; margin-top: 1px; }
.switch { position: relative; width: 46px; height: 28px; flex: none; border: 0; padding: 0; border-radius: 99px; cursor: pointer; background: var(--baseline); transition: background .15s; }
.switch[aria-checked="true"] { background: var(--good); }
.switch::after { content: ""; position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 99px; background: #fff; transition: transform .15s; box-shadow: 0 1px 3px rgba(0,0,0,.3); }
.switch[aria-checked="true"]::after { transform: translateX(18px); }
.switch[disabled] { opacity: .45; cursor: default; }

/* ---- pills / status ---- */
.pill {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 9px; border-radius: 99px; font-size: 11.5px; font-weight: 620;
  background: var(--surface-2); color: var(--text-secondary);
}
.pill.awaiting_approval { background: color-mix(in srgb, var(--warning) 20%, var(--surface-1)); color: var(--text-primary); }
.pill.commented        { background: color-mix(in srgb, var(--good) 20%, var(--surface-1)); color: var(--text-primary); }
.pill.approved         { background: color-mix(in srgb, var(--accent) 20%, var(--surface-1)); color: var(--text-primary); }
.pill.failed           { background: color-mix(in srgb, var(--critical) 22%, var(--surface-1)); color: var(--text-primary); }
.pill.researching      { background: color-mix(in srgb, var(--serious) 20%, var(--surface-1)); color: var(--text-primary); }

/* ---- queue ---- */
.qitem { display: flex; flex-direction: column; gap: 7px; width: 100%; text-align: left; cursor: pointer; }
.qitem .qhead { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.qitem .qtext { font-size: 14px; line-height: 1.45; }
.qitem .qmeta { font-size: 11.5px; color: var(--text-muted); }
button.card { font: inherit; color: inherit; }

/* ---- review ---- */
.section-label { font-size: 11.5px; color: var(--text-muted); margin-bottom: 5px; }
.post-text { font-size: 14px; line-height: 1.55; white-space: pre-wrap; word-break: break-word; }
.cand { border-top: 1px solid var(--grid); padding: 11px 0; }
.cand:first-of-type { border-top: 0; }
.cand-name { font-size: 14px; font-weight: 620; }
.cand-why { font-size: 12.5px; color: var(--text-secondary); margin-top: 3px; line-height: 1.5; }
.shoplink { display: block; font-size: 12.5px; margin-top: 5px; color: var(--accent); word-break: break-all; }
textarea {
  width: 100%; min-height: 132px; font: inherit; font-size: 14px; line-height: 1.55;
  padding: 12px; border-radius: 12px; border: 1px solid var(--hairline);
  background: var(--surface-2); color: var(--text-primary); resize: vertical;
}
a { color: var(--accent); }
.back { align-self: flex-start; }

/* ---- logs ---- */
pre.log {
  margin: 0; font-size: 11.5px; line-height: 1.55; white-space: pre-wrap; word-break: break-word;
  color: var(--text-secondary); max-height: 60vh; overflow: auto;
}
.event { display: flex; gap: 10px; padding: 7px 0; border-top: 1px solid var(--grid); font-size: 12.5px; }
.event:first-child { border-top: 0; }
.event time { color: var(--text-muted); flex: none; font-variant-numeric: tabular-nums; }
.event .lv { flex: none; width: 74px; color: var(--text-muted); }
.event .msg { flex: 1; min-width: 0; word-break: break-word; }
.lv-error, .lv-checkpoint { color: var(--critical); }

/* ---- tabbar ---- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: grid; grid-template-columns: repeat(3, 1fr);
  background: color-mix(in srgb, var(--surface-1) 92%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--hairline);
  padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  position: relative; border: 0; background: none; color: var(--text-muted);
  font: inherit; font-size: 11px; font-weight: 600; padding: 9px 0 10px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
}
.tab-ico { font-size: 17px; line-height: 1; }
.tab[aria-selected="true"] { color: var(--accent); }
.tab-badge {
  position: absolute; top: 3px; left: 50%; margin-left: 9px;
  background: var(--critical); color: #fff; font-size: 10px; font-weight: 700;
  min-width: 17px; height: 17px; border-radius: 99px; padding: 0 4px;
  display: grid; place-items: center;
}

/* ---- toast ---- */
.toast {
  position: fixed; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  z-index: 40; max-width: calc(100% - 32px);
  background: var(--text-primary); color: var(--plane);
  padding: 11px 16px; border-radius: 12px; font-size: 13px; font-weight: 550;
  box-shadow: var(--shadow);
}
.toast.err { background: var(--critical); color: #fff; }
