:root {
  color-scheme: light;
  --bg: #f4f3ef;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e3e1db;
  --grid: #e8e6e0;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7a7974;
  --accent: #2a78d6;
  --gold: #c98500;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948;
  --shadow: 0 1px 2px rgba(0, 0, 0, .04), 0 4px 16px rgba(0, 0, 0, .04);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111110; --surface: #1a1a19; --surface-2: #232321; --border: #2e2e2b; --grid: #2a2a28;
    --text: #ffffff; --text-2: #c3c2b7; --muted: #8f8e86; --accent: #3987e5; --gold: #eda100;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111110; --surface: #1a1a19; --surface-2: #232321; --border: #2e2e2b; --grid: #2a2a28;
  --text: #ffffff; --text-2: #c3c2b7; --muted: #8f8e86; --accent: #3987e5; --gold: #eda100;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --shadow: none;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 "Noto Sans Thai", system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
}
.wrap { max-width: 1200px; margin: 0 auto; padding: 0 16px; }
h1, h2 { margin: 0; line-height: 1.3; }
h1 { font-size: 22px; font-weight: 700; }
h2 { font-size: 16px; font-weight: 600; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.r { text-align: right; }
.num { font-variant-numeric: tabular-nums; }

/* --- header --- */
.top { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5; }
.top-row { display: flex; flex-wrap: wrap; gap: 12px 24px; align-items: center; justify-content: space-between; padding-top: 14px; padding-bottom: 10px; }
.sub { margin: 2px 0 0; color: var(--text-2); font-size: 13px; }
.controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.ctl { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text-2); }
select, .btn {
  font: inherit; font-size: 13px; color: var(--text);
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 10px; cursor: pointer;
}
.btn:hover, select:hover { border-color: var(--muted); }
.btn-icon { width: 34px; padding: 6px 0; }
.progress { height: 3px; background: var(--surface-2); border-radius: 2px; overflow: hidden; margin-bottom: 0; }
.progress > div { height: 100%; background: var(--accent); width: 0; transition: width .4s; }

main { padding-top: 20px; padding-bottom: 24px; display: grid; gap: 16px; transition: opacity .2s; }
main.loading { opacity: .5; pointer-events: none; }
.alert { background: #fdecec; color: #8a1c1c; border: 1px solid #f3c2c2; padding: 10px 14px; border-radius: 10px; }

/* --- cards --- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 16px 18px; box-shadow: var(--shadow); min-width: 0; }
.card-head { margin-bottom: 14px; display: grid; gap: 2px; }
.card-head.row { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 8px; }
.card-head small { font-size: 12px; }
.grid-2 { display: grid; grid-template-columns: minmax(0, 5fr) minmax(0, 7fr); gap: 16px; }
@media (max-width: 900px) { .grid-2 { grid-template-columns: minmax(0, 1fr); } }

/* --- KPI tiles --- */
.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
@media (max-width: 800px) { .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 14px 16px; box-shadow: var(--shadow); min-width: 0; }
.kpi .label { font-size: 12px; color: var(--text-2); }
.kpi .value { font-size: 26px; font-weight: 700; line-height: 1.25; margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.kpi .note { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* --- team bars --- */
.team-bars { display: grid; gap: 10px; }
.tb { display: grid; grid-template-columns: 26px minmax(0, 1fr); gap: 10px; align-items: center; }
.tb .rank { font-weight: 700; font-size: 15px; text-align: center; color: var(--text-2); }
.tb.first .rank { color: var(--gold); }
.tb .line1 { display: flex; justify-content: space-between; gap: 8px; font-size: 14px; }
.tb .name { font-weight: 600; display: inline-flex; gap: 6px; align-items: center; }
.tb .pts { font-weight: 700; font-variant-numeric: tabular-nums; }
.tb .track { height: 10px; background: var(--surface-2); border-radius: 2px 4px 4px 2px; margin: 4px 0 2px; overflow: hidden; }
.tb .fill { height: 100%; border-radius: 0 4px 4px 0; transition: width .5s; }
.tb .line2 { font-size: 12px; color: var(--muted); display: flex; justify-content: space-between; gap: 8px; }
.dot { width: 10px; height: 10px; border-radius: 3px; display: inline-block; flex: none; }

/* --- line chart --- */
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 12px; color: var(--text-2); margin-bottom: 6px; }
.legend button { all: unset; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; padding: 2px 0; }
.legend button[aria-pressed="false"] { opacity: .35; }
.legend .key { width: 14px; height: 2px; border-radius: 1px; }
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart text { fill: var(--muted); font-size: 11px; font-family: inherit; }

.tip {
  position: fixed; z-index: 20; pointer-events: none; min-width: 160px;
  background: var(--surface); color: var(--text); border: 1px solid var(--border);
  border-radius: 10px; padding: 8px 10px; font-size: 12px; box-shadow: 0 6px 24px rgba(0, 0, 0, .14);
}
.tip .t-head { color: var(--text-2); margin-bottom: 4px; }
.tip .t-row { display: grid; grid-template-columns: 12px 1fr auto; gap: 6px; align-items: center; }
.tip .t-row .key { height: 2px; border-radius: 1px; }
.tip .t-row b { font-variant-numeric: tabular-nums; }

/* --- categories --- */
.cats { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; }
.cat { border: 1px solid var(--border); border-radius: 12px; padding: 12px 14px; background: var(--surface); }
.cat-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.cat-head .ic { font-size: 22px; }
.cat-head .t { font-weight: 600; }
.cat-head .u { font-size: 12px; color: var(--muted); margin-left: auto; }
.cat ol { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.cat li { display: grid; grid-template-columns: 22px minmax(0, 1fr) auto; gap: 8px; align-items: baseline; font-size: 14px; }
.cat li .m { text-align: center; }
.cat li .who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat li .who small { color: var(--muted); margin-left: 4px; }
.cat li .amt { font-weight: 600; font-variant-numeric: tabular-nums; text-align: right; }
.cat li .amt small { display: block; font-weight: 400; color: var(--muted); font-size: 11px; line-height: 1.2; }
.cat li.lead { padding: 6px 8px; margin: 0 -8px 2px; background: var(--surface-2); border-radius: 8px; }
.cat li.lead .amt { font-size: 16px; }
.cat details { margin-top: 6px; font-size: 13px; }
.cat summary { cursor: pointer; color: var(--accent); font-size: 12px; }
.cat details ol { margin-top: 6px; }

/* --- table --- */
.table-wrap { overflow-x: auto; }
.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
.tbl th { text-align: left; font-weight: 500; font-size: 12px; color: var(--muted); padding: 6px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.tbl td { padding: 7px 10px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
.tbl td.r, .tbl th.r { text-align: right; font-variant-numeric: tabular-nums; }
.tbl tbody tr:hover { background: var(--surface-2); }
.tbl .team-cell { display: inline-flex; align-items: center; gap: 6px; }

.warn summary { cursor: pointer; color: var(--text-2); font-size: 13px; }
.warn ul { margin: 10px 0 0; padding-left: 18px; font-size: 13px; color: var(--text-2); max-height: 260px; overflow: auto; }
.foot { color: var(--muted); font-size: 12px; padding-bottom: 28px; }
@media (max-width: 600px) { .top { position: static; } h1 { font-size: 19px; } }

/* --- การ์ดพิเศษ --- */
:root { --x2-bg: #e3eefb; --x2-fg: #184f95; --carry-bg: #dff3ea; --carry-fg: #0f6e4a; --block-bg: #fbe5e4; --block-fg: #a32d2d; --pos: #0f7a3d; --neg: #c22f2f; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) { --x2-bg: #1c3555; --x2-fg: #b7d3f6; --carry-bg: #12382b; --carry-fg: #9fe1cb; --block-bg: #4a1d1d; --block-fg: #f7c1c1; --pos: #5fd394; --neg: #f08a8a; }
}
:root[data-theme="dark"] { --x2-bg: #1c3555; --x2-fg: #b7d3f6; --carry-bg: #12382b; --carry-fg: #9fe1cb; --block-bg: #4a1d1d; --block-fg: #f7c1c1; --pos: #5fd394; --neg: #f08a8a; }
.card-tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 10px; margin-bottom: 18px; }
.ctile { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; min-width: 0; }
.ctile .label { font-size: 12px; color: var(--text-2); }
.ctile .value { font-size: 20px; font-weight: 700; line-height: 1.3; }
.ctile .note { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sub-h { margin: 18px 0 8px; }
h3.sub-h, .sub-h h3 { font-size: 14px; font-weight: 600; margin-top: 18px; }
.sub-h.row { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 8px; }
.sub-h.row h3 { margin: 0; }
.pill { display: inline-block; font-size: 11px; font-weight: 500; padding: 1px 7px; border-radius: 6px; margin-right: 4px; white-space: nowrap; }
.pill.x2 { background: var(--x2-bg); color: var(--x2-fg); }
.pill.carry { background: var(--carry-bg); color: var(--carry-fg); }
.pill.block { background: var(--block-bg); color: var(--block-fg); }
.pill.off { opacity: .35; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.tb .cardnet { font-size: 11px; font-weight: 500; margin-left: 4px; }

/* --- ไทม์ไลน์การ์ดแนวนอน --- */
.lanes-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 10px; }
.lanes { display: grid; min-width: max-content; font-size: 12px; }
.lanes > div { border-bottom: 1px solid var(--grid); }
.lane-corner, .lane-team { position: sticky; left: 0; z-index: 1; background: var(--surface); border-right: 1px solid var(--border); }
.lane-team { display: flex; align-items: center; gap: 6px; padding: 0 8px; font-weight: 600; font-size: 13px; white-space: nowrap; }
.lane-day { text-align: center; color: var(--muted); padding: 6px 2px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.lane-day.today { color: var(--accent); font-weight: 700; }
.lane-cell { min-height: 52px; padding: 4px 3px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; border-left: 1px solid var(--grid); }
.lane-cell.today { background: color-mix(in srgb, var(--accent) 7%, transparent); }
.lane-cell.has { cursor: pointer; }
.lane-cell.has:hover { background: var(--surface-2); }
.lane-cell .pill { margin: 0; font-size: 11px; padding: 1px 5px; }
.hit { font-size: 11px; font-weight: 600; color: var(--neg); white-space: nowrap; }
.pending-chip { outline: 1.5px dashed var(--block-fg); outline-offset: -1.5px; }
.pending-hit { opacity: .7; font-style: italic; }
.dim { opacity: .2; }
.lanes-legend { font-size: 12px; margin: 8px 0 0; line-height: 2; }
.lanes-legend .pill { margin: 0 2px 0 0; }
.tip .t-card { padding: 4px 0; border-top: 1px solid var(--grid); }
.tip .t-card:first-of-type { border-top: 0; }
.tbl td .pill { margin: 1px 4px 1px 0; }

/* --- ประวัติคะแนนรายวัน --- */
.hist { display: grid; min-width: max-content; font-size: 12px; }
.hist > div { border-bottom: 1px solid var(--grid); }
.hist .hist-team { background: var(--surface-2); font-size: 13px; }
.hist .hist-name { font-weight: 400; padding-left: 24px; font-size: 13px; }
.hc {
  min-height: 38px; display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-variant-numeric: tabular-nums; font-weight: 600; line-height: 1.15; cursor: default;
  background: color-mix(in srgb, var(--accent) var(--mix, 0%), var(--surface)); border-left: 1px solid var(--surface);
}
.hc small { font-size: 10px; font-weight: 500; opacity: .85; white-space: nowrap; }
.hc.strong { color: #fff; }
.hc.empty { color: var(--muted); font-weight: 400; background: var(--surface); }
.hc.blocked { background: var(--block-bg); color: var(--block-fg); }
.hc.pending { outline: 1.5px dashed var(--block-fg); outline-offset: -2px; }
.hc.team-day { background: var(--surface-2); color: var(--text-2); font-weight: 600; min-height: 32px; }
.hc.total { background: var(--surface); font-size: 13px; border-left: 1px solid var(--border); }
.hc.team-day.total { background: var(--surface-2); }
.hc.today:not(.blocked):not(.strong) { box-shadow: inset 0 0 0 999px color-mix(in srgb, var(--accent) 6%, transparent); }
.hc[style] { cursor: pointer; }
.lane-day.total { border-left: 1px solid var(--border); }
.scale { display: inline-flex; align-items: center; gap: 4px; margin-right: 8px; }
.scale i { width: 80px; height: 10px; border-radius: 2px; background: linear-gradient(90deg, color-mix(in srgb, var(--accent) 10%, var(--surface)), color-mix(in srgb, var(--accent) 80%, var(--surface))); }
.hmark { font-size: 10px; font-weight: 600; background: var(--surface-2); border-radius: 4px; padding: 0 4px; }
.lanes-legend .hc { display: inline-flex; min-height: 0; width: 22px; height: 18px; border-radius: 4px; vertical-align: middle; }
.pending-sample { outline: 1.5px dashed var(--block-fg); outline-offset: -2px; background: var(--surface); }
.blocked-sample { background: var(--block-bg); }
.empty-sample { border: 1px solid var(--border); }
