/* ---------- tokens ---------- */
:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f2f2f2;          /* Excel zebra F2F2F2 */
  --line: #dcdfe4;
  --text: #16181c;
  --text-2: #4d525b;
  --muted: #767b85;
  --accent: #2E75B6;
  --sec-bg: #1F3864;             /* Excel title/section */
  --sec-fg: #ffffff;
  --hdr-bg: #2E75B6;             /* Excel header */
  --hdr-fg: #ffffff;
  --disc: #1640c9;               /* font 0000FF = disclosed */
  --calc: var(--text);           /* black = Excel formula */
  --est: #0b6b1f;                /* 006100 = calculated / estimated */
  --flag: #c42121;               /* FF0000 = flag / input label */
  --na: #7b7f87;                 /* grey italic = "-" / note */
  --input-bg: #ffff00;           /* FFFF00 = input assumption */
  --input-fg: #16181c;
  --input-bd: transparent;
  --partial-bg: #fff2cc;
  --series-1: #2a78d6;
  --ref: #8a8f98;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111317; --surface: #1a1d22; --surface-2: #22262c; --line: #33383f;
    --text: #eceef1; --text-2: #b9bec6; --muted: #8d939c; --accent: #5fa2e6;
    --sec-bg: #22335a; --sec-fg: #eef2fb; --hdr-bg: #1d4f7c; --hdr-fg: #eef5fc;
    --disc: #86adff; --est: #62d68b; --flag: #ff7d7d; --na: #8d939c;
    --input-bg: rgba(255, 221, 0, .16); --input-fg: #fff3a6; --input-bd: #c9ab00;
    --partial-bg: rgba(255, 214, 102, .13);
    --series-1: #3987e5; --ref: #7d838c;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111317; --surface: #1a1d22; --surface-2: #22262c; --line: #33383f;
  --text: #eceef1; --text-2: #b9bec6; --muted: #8d939c; --accent: #5fa2e6;
  --sec-bg: #22335a; --sec-fg: #eef2fb; --hdr-bg: #1d4f7c; --hdr-fg: #eef5fc;
  --disc: #86adff; --est: #62d68b; --flag: #ff7d7d; --na: #8d939c;
  --input-bg: rgba(255, 221, 0, .16); --input-fg: #fff3a6; --input-bd: #c9ab00;
  --partial-bg: rgba(255, 214, 102, .13);
  --series-1: #3987e5; --ref: #7d838c;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4);
}

/* ---------- base ---------- */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.55 "Sarabun", system-ui, -apple-system, sans-serif;
  overflow-x: hidden;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { line-height: 1.3; margin: 0; }
h1 { font-size: 1.35rem; font-weight: 700; }
h2 { font-size: 1.1rem; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.num { font-variant-numeric: tabular-nums; }
.wrap { max-width: 1280px; margin: 0 auto; padding: 12px 16px 48px; }

.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 8px 16px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); font-weight: 700; }
.brand:hover { text-decoration: none; }
.icon-btn {
  display: inline-flex; align-items: center; gap: 6px; min-height: 36px; padding: 0 12px;
  border: 1px solid var(--line); border-radius: 999px; background: var(--surface); color: var(--text-2);
  font: inherit; font-size: .85rem; cursor: pointer;
}

.crumbs { display: flex; flex-wrap: wrap; gap: 4px 6px; align-items: center; font-size: .85rem; color: var(--muted); margin: 4px 0 10px; }
.crumbs a { color: var(--text-2); }
.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 8px 16px; margin-bottom: 10px; }
.scope { color: var(--text-2); font-size: .85rem; margin: 4px 0 0; }

/* ---------- controls ---------- */
.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.chip, .seg button, select, input[type=search] {
  font: inherit; font-size: .9rem; color: var(--text); background: var(--surface);
  border: 1px solid var(--line); border-radius: 999px; min-height: 36px; padding: 0 14px;
}
.chip { cursor: pointer; display: inline-flex; align-items: center; gap: 6px; }
.chip[aria-pressed="true"], .chip.on { background: var(--hdr-bg); color: var(--hdr-fg); border-color: var(--hdr-bg); }
.chip.off { color: var(--muted); text-decoration: line-through; }
.chip.sm { min-height: 32px; min-width: 34px; padding: 0 9px; justify-content: center; }
.chip.sm[aria-pressed="false"] { background: var(--surface); color: var(--muted); }
select { padding-right: 28px; }
input[type=search] { width: 100%; border-radius: 10px; padding: 0 14px; min-height: 44px; font-size: 1rem; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; overflow: hidden; background: var(--surface); }
.seg button { border: 0; border-radius: 0; cursor: pointer; }
.seg button[aria-pressed="true"] { background: var(--hdr-bg); color: var(--hdr-fg); }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin: 6px 0 10px; overflow-x: auto; }
.tabs a { padding: 10px 14px; color: var(--text-2); border-bottom: 3px solid transparent; white-space: nowrap; font-weight: 500; }
.tabs a.on { color: var(--text); border-bottom-color: var(--hdr-bg); }
.tabs a:hover { text-decoration: none; color: var(--text); }

.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: .8rem; color: var(--text-2); margin: 0 0 10px; }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px 12px; align-items: center; margin-bottom: 10px; }
.toolbar .grow { flex: 1 1 auto; }

/* ---------- cards / grid ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); }
.sector-card { padding: 14px 16px; display: flex; flex-direction: column; gap: 8px; color: var(--text); }
.sector-card:hover { text-decoration: none; border-color: var(--accent); }
.sector-card .en { color: var(--muted); font-size: .85rem; }
.tick-list { display: flex; flex-wrap: wrap; gap: 6px; }
.tick { font-size: .8rem; font-weight: 600; padding: 2px 8px; border-radius: 6px; background: var(--surface-2); color: var(--text-2); }
a.tick:hover { text-decoration: none; background: var(--hdr-bg); color: var(--hdr-fg); }
.home-top { display: grid; gap: 12px; grid-template-columns: 1fr; margin: 6px 0 16px; }
@media (min-width: 768px) { .home-top { grid-template-columns: 1fr auto; align-items: center; } }
.results { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.result { display: flex; justify-content: space-between; gap: 8px; padding: 10px 14px; color: var(--text); }
.compare-link { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 14px 16px; margin: 12px 0; color: var(--text); font-weight: 600; }

/* ---------- semantic cell colours (Excel meaning) ---------- */
.c-disc { color: var(--disc); }
.c-calc { color: var(--calc); }
.c-est { color: var(--est); }
.c-flag { color: var(--flag); }
.c-na { color: var(--na); }
.c-i { font-style: italic; }
.c-b { font-weight: 700; }
.c-inv { color: var(--text); }
.c-input { background: var(--input-bg) !important; color: var(--input-fg); box-shadow: inset 0 0 0 1px var(--input-bd); }
.c-input.c-flag { color: var(--flag); }
.c-partial { background: var(--partial-bg) !important; }

/* ---------- Financial Summary table ---------- */
.tbl-wrap {
  overflow: auto; max-height: calc(100dvh - 130px);
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
}
table.fs { border-collapse: separate; border-spacing: 0; font-size: .86rem; width: 100%; }
table.fs th, table.fs td { padding: 5px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.fs thead th { position: sticky; background: var(--hdr-bg); color: var(--hdr-fg); font-weight: 600; text-align: center; z-index: 3; white-space: nowrap; }
table.fs thead tr:first-child th { top: 0; }
table.fs thead tr:nth-child(2) th { top: var(--h1, 31px); font-weight: 500; font-size: .8rem; }
table.fs thead th .sfx { display: block; font-weight: 400; font-size: .72rem; opacity: .85; }
table.fs .lbl { position: sticky; left: 0; z-index: 2; background: var(--surface); text-align: left; min-width: 180px; width: clamp(180px, 30vw, 320px); max-width: 320px; white-space: normal; }
table.fs thead .lbl { z-index: 4; background: var(--hdr-bg); vertical-align: middle; }
table.fs td.v { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
table.fs td.v.txt { text-align: left; white-space: normal; min-width: 88px; max-width: 150px; font-size: .78rem; overflow-wrap: anywhere; }
table.fs td.grp { border-left: 1px solid var(--line); }
table.fs tr.z td, table.fs tr.z .lbl { background: var(--surface-2); }
table.fs tr.sec td { background: var(--sec-bg); color: var(--sec-fg); font-weight: 700; position: sticky; left: 0; }
table.fs tr.sec td span { position: sticky; left: 10px; }
table.fs tr.note td { color: var(--na); font-style: italic; }
table.fs tbody tr:hover td { filter: brightness(.97); }

.notes { margin: 14px 0 0; padding: 12px 16px; }
.notes h3 { font-size: .95rem; margin-bottom: 6px; }
.notes ul { margin: 0; padding-left: 18px; color: var(--text-2); font-size: .85rem; }
.notes li { margin: 4px 0; }

/* ---------- Card view (per ticker) ---------- */
.card-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 8px; }
details.sec { margin-bottom: 10px; overflow: hidden; }
details.sec > summary {
  list-style: none; cursor: pointer; padding: 10px 14px; background: var(--sec-bg); color: var(--sec-fg);
  font-weight: 700; display: flex; justify-content: space-between; gap: 8px;
}
details.sec > summary::-webkit-details-marker { display: none; }
details.sec > summary::after { content: "▾"; transition: transform .15s; }
details.sec:not([open]) > summary::after { transform: rotate(-90deg); }
.kv-head, .kv { display: grid; grid-template-columns: minmax(0, 1fr) 84px 84px; gap: 4px 10px; padding: 7px 14px; align-items: baseline; }
.kv-head { font-size: .75rem; color: var(--muted); border-bottom: 1px solid var(--line); padding-top: 6px; padding-bottom: 6px; }
.kv-head span:not(:first-child), .kv .v { text-align: right; }
.kv { border-bottom: 1px solid var(--line); font-size: .88rem; }
.kv:last-child { border-bottom: 0; }
.kv.z { background: var(--surface-2); }
.kv .l { color: var(--text); min-width: 0; overflow-wrap: anywhere; }
.kv .v { font-variant-numeric: tabular-nums; white-space: nowrap; border-radius: 4px; padding: 0 2px; }
.kv .v.txt { white-space: normal; text-align: left; overflow-wrap: anywhere; font-size: .82rem; }
.kv .v.span2 { grid-column: 2 / 4; text-align: right; }
.kv .v.txt.span2 { text-align: left; }
.kv.textrow { grid-template-columns: 1fr; color: var(--na); font-style: italic; }
.kv.wide { grid-template-columns: 1fr; }
.kv.wide .vals { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 10px; }
.kv.wide .vals .v { text-align: left; white-space: normal; }
.kv.wide .vals .yr { font-size: .72rem; color: var(--muted); display: block; font-style: normal; }
@media (min-width: 560px) { .kv-head, .kv { grid-template-columns: minmax(0, 1fr) 120px 120px; } }

/* ---------- Insights ---------- */
.ins { padding: 0; overflow: hidden; margin-bottom: 12px; }
.ins h3 { padding: 12px 16px; background: var(--sec-bg); color: var(--sec-fg); font-size: 1rem; }
.ins .blk { padding: 10px 16px; border-top: 1px solid var(--line); }
.ins .blk b { display: block; font-size: .8rem; color: var(--text-2); margin-bottom: 2px; }
.ins .blk p { margin: 0; white-space: pre-line; overflow-wrap: anywhere; }
.ins .blk.ev { background: var(--surface-2); font-size: .88rem; }
@media (min-width: 900px) {
  .ins .pair { display: grid; grid-template-columns: 1fr 1fr; }
  .ins .pair .blk + .blk { border-left: 1px solid var(--line); }
}

/* ---------- chart ---------- */
.chart-card { padding: 12px 14px 8px; margin-bottom: 14px; }
.chart-box { position: relative; }
.chart-box svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart-box .grid-line { stroke: var(--line); stroke-width: 1; }
.chart-box .axis-t { fill: var(--muted); font-size: 11px; }
.chart-box .ref { stroke: var(--ref); stroke-width: 1; stroke-dasharray: 4 4; }
.chart-box .ref.avg { stroke-dasharray: 2 3; }
.chart-box .ref-t { fill: var(--muted); font-size: 10.5px; }
.chart-box .series { stroke: var(--series-1); stroke-width: 2; fill: none; stroke-linejoin: round; stroke-linecap: round; }
.chart-box .dot { fill: var(--series-1); stroke: var(--surface); stroke-width: 2; }
.chart-box .val-t { fill: var(--text); font-size: 11px; font-weight: 600; }
.chart-box .cross { stroke: var(--text-2); stroke-width: 1; }
.chart-box .hit { fill: transparent; cursor: crosshair; }
.tip {
  position: absolute; pointer-events: none; background: var(--surface); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px; box-shadow: var(--shadow); padding: 6px 10px; font-size: .8rem;
  white-space: nowrap; transform: translate(-50%, -110%);
}
.tip b { font-variant-numeric: tabular-nums; }
.chart-table { width: 100%; border-collapse: collapse; font-size: .8rem; margin-top: 6px; }
.chart-table th, .chart-table td { padding: 3px 4px; text-align: right; border-top: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.chart-table th:first-child, .chart-table td:first-child { text-align: left; color: var(--muted); font-weight: 400; }

/* ---------- compare ---------- */
table.cmp { border-collapse: separate; border-spacing: 0; font-size: .86rem; width: 100%; min-width: 760px; }
table.cmp th, table.cmp td { padding: 7px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; text-align: right; font-variant-numeric: tabular-nums; }
table.cmp thead th { position: sticky; top: 0; background: var(--hdr-bg); color: var(--hdr-fg); font-weight: 600; z-index: 3; cursor: pointer; user-select: none; }
table.cmp thead th[aria-sort="ascending"]::after { content: " ▲"; font-size: .7em; }
table.cmp thead th[aria-sort="descending"]::after { content: " ▼"; font-size: .7em; }
table.cmp .t { position: sticky; left: 0; background: var(--surface); text-align: left; z-index: 2; font-weight: 600; }
table.cmp thead .t { z-index: 4; background: var(--hdr-bg); }
table.cmp td.s { text-align: left; color: var(--text-2); }
table.cmp tbody tr:nth-child(even) td { background: var(--surface-2); }
.band { display: inline-flex; align-items: center; gap: 6px; }
.band svg { width: 64px; height: 12px; }
.band .rail { stroke: var(--line); stroke-width: 4; stroke-linecap: round; }
.band .mk { fill: var(--series-1); stroke: var(--surface); stroke-width: 1.5; }
.band .avg { stroke: var(--ref); stroke-width: 1.5; }

.err { padding: 16px; border-left: 4px solid var(--flag); }
