/* Layout and components. All colors come from tokens in themes.css. */

:root {
  --font-body: 'IBM Plex Sans Thai', 'Noto Sans Thai', 'Leelawadee UI', Tahoma, sans-serif;
  --font-head: 'Bai Jamjuree', 'IBM Plex Sans Thai', 'Noto Sans Thai', 'Leelawadee UI', Tahoma, sans-serif;
  --font-num: 'IBM Plex Mono', ui-monospace, 'Cascadia Mono', Consolas, monospace;
  --radius: 16px;
  --radius-btn: 12px;
  --tap: 3rem;
  --border-w: 1px;
  --weight-body: 400;
  --weight-strong: 700;
  --accent-soft: color-mix(in srgb, var(--accent) 12%, var(--surface));
}

/* Text size control: ก / ก+ / ก++ */
html { font-size: 112.5%; }
html[data-size="2"] { font-size: 125%; }
html[data-size="3"] { font-size: 137.5%; }

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html.theme-anim body,
html.theme-anim body * {
  transition: background-color 200ms ease, border-color 200ms ease, color 200ms ease, fill 200ms ease;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  font-weight: var(--weight-body);
  line-height: 1.7;
  -webkit-text-size-adjust: 100%;
  overflow-wrap: break-word;
}

h1, h2, h3 {
  font-family: var(--font-head);
  line-height: 1.35;
  font-weight: 700;
  margin: 0 0 0.5em;
}
h1 { font-size: 1.75rem; }
h2 { font-size: 1.4rem; margin-top: 2em; }
h3 { font-size: 1.15rem; margin-top: 1.5em; }
p, ul, ol { margin: 0 0 1em; }
strong, b { font-weight: var(--weight-strong); }

a { color: var(--accent); text-underline-offset: 0.2em; }
a:hover { text-decoration-thickness: 2px; }

:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

.num { font-family: var(--font-num); font-variant-numeric: tabular-nums; }

.skip {
  position: absolute; left: -999px; top: 0;
  background: var(--accent); color: var(--on-accent); padding: 0.5rem 1rem; z-index: 50;
}
.skip:focus { left: 1rem; }

.wrap { max-width: 68rem; margin: 0 auto; padding: 0 1rem; }
.narrow { max-width: 42rem; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---------- Icons ---------- */
.icon { width: 1.5rem; height: 1.5rem; flex: none; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- Buttons ---------- */
.btn, .icon-btn {
  font: inherit;
  font-weight: 600;
  min-height: var(--tap);
  min-width: var(--tap);
  border-radius: var(--radius-btn);
  cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 0.5rem;
  transition: transform 150ms ease, background-color 150ms ease;
  -webkit-tap-highlight-color: transparent;
}
.btn {
  background: var(--accent); color: var(--on-accent);
  border: 2px solid var(--accent);
  padding: 0.5rem 1.25rem;
  text-decoration: none;
}
.btn:hover { background: color-mix(in srgb, var(--accent) 88%, var(--text)); }
.btn-ghost { background: var(--surface); color: var(--text); border: calc(var(--border-w) + 1px) solid var(--border); }
.btn-ghost:hover { background: var(--accent-soft); }
.icon-btn {
  background: transparent; color: var(--text);
  border: var(--border-w) solid var(--border);
  padding: 0 0.6rem;
}
.icon-btn:hover { background: var(--accent-soft); }
.btn:active, .icon-btn:active, .step:active { transform: scale(0.96); }

/* ---------- Header ---------- */
.site-header { position: relative; background: var(--surface); border-bottom: var(--border-w) solid var(--border); }
.header-row { display: flex; align-items: center; gap: 0.5rem; min-height: 4rem; flex-wrap: wrap; padding-top: 0.5rem; padding-bottom: 0.5rem; }
.brand {
  display: inline-flex; align-items: center; gap: 0.5rem;
  font-family: var(--font-head); font-weight: 700; font-size: 1.25rem;
  color: var(--text); text-decoration: none; margin-right: auto; min-height: var(--tap);
}
.brand-mark { width: 2rem; height: 2rem; border-radius: 8px; background: var(--accent); color: var(--on-accent); display: grid; place-items: center; }
.brand-mark .icon { width: 1.25rem; height: 1.25rem; }
.header-tools { display: flex; align-items: center; gap: 0.5rem; }
.btn-label { font-size: 0.9rem; }

.size-ctl { display: flex; align-items: center; gap: 0.5rem; }
.size-ctl .label { font-size: 0.85rem; color: var(--muted); }
.size-ctl .seg-btns { display: flex; border: var(--border-w) solid var(--border); border-radius: var(--radius-btn); overflow: hidden; }
.size-ctl button {
  font: inherit; font-family: var(--font-head); font-weight: 700;
  min-width: var(--tap); min-height: var(--tap);
  background: transparent; color: var(--text); border: 0; cursor: pointer;
}
.size-ctl button + button { border-left: var(--border-w) solid var(--border); }
.size-ctl button[data-size="1"] { font-size: 0.9rem; }
.size-ctl button[data-size="2"] { font-size: 1.05rem; }
.size-ctl button[data-size="3"] { font-size: 1.2rem; }
.size-ctl button[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }

.size-row { display: flex; justify-content: flex-end; padding-bottom: 0.5rem; }
@media (min-width: 48rem) {
  .size-row { display: none; }
}
.header-tools .size-ctl { display: none; }
@media (min-width: 48rem) {
  .header-tools .size-ctl { display: flex; }
}

/* Calculator menu (works without JS via <details>) */
.menu { position: relative; }
.menu > summary { list-style: none; }
.menu > summary::-webkit-details-marker { display: none; }
.menu-panel {
  position: absolute; right: 0; top: calc(100% + 0.5rem); z-index: 30;
  width: min(20rem, calc(100vw - 2rem));
  background: var(--surface); border: var(--border-w) solid var(--border); border-radius: var(--radius);
  box-shadow: 0 12px 32px rgb(0 0 0 / 0.18);
  padding: 0.5rem;
}
.menu-panel ul { list-style: none; margin: 0; padding: 0; }
.menu-panel a, .menu-panel span {
  display: flex; align-items: center; gap: 0.75rem; min-height: var(--tap);
  padding: 0.25rem 0.75rem; border-radius: 10px; color: var(--text); text-decoration: none;
}
.menu-panel a:hover { background: var(--accent-soft); }
.menu-panel a[aria-current="page"] { font-weight: 700; background: var(--accent-soft); }
.menu-panel span { color: var(--muted); }
.menu-panel small { margin-left: auto; font-size: 0.8rem; }

/* ---------- Theme picker ---------- */
.theme-panel[hidden] { display: none; }
.theme-panel {
  position: fixed; z-index: 40; inset: auto 0 0 0;
  background: var(--surface); color: var(--text);
  border-top: var(--border-w) solid var(--border);
  border-radius: var(--radius) var(--radius) 0 0;
  box-shadow: 0 -12px 40px rgb(0 0 0 / 0.25);
  padding: 1rem 1rem calc(1rem + env(safe-area-inset-bottom));
  max-height: 80vh; overflow-y: auto;
}
.theme-backdrop { position: fixed; inset: 0; z-index: 39; background: rgb(0 0 0 / 0.35); }
.theme-backdrop[hidden] { display: none; }
@media (min-width: 48rem) {
  .theme-panel {
    position: absolute; inset: auto; right: 1rem; top: 4.25rem; width: 26rem;
    border: var(--border-w) solid var(--border); border-radius: var(--radius);
    box-shadow: 0 12px 32px rgb(0 0 0 / 0.2);
  }
  .theme-backdrop { background: transparent; }
}
.theme-panel-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 0.75rem; }
.theme-panel-head h2 { margin: 0; font-size: 1.15rem; }
.theme-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.75rem; }
@media (min-width: 30rem) { .theme-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.theme-card {
  font: inherit; text-align: left; cursor: pointer;
  background: var(--surface); color: var(--text);
  border: 2px solid var(--border); border-radius: 12px;
  padding: 0.5rem; min-height: var(--tap);
  display: flex; flex-direction: column; gap: 0.4rem;
  position: relative;
}
.theme-card[aria-checked="true"] { border-color: var(--text); outline: 2px solid var(--accent); outline-offset: 1px; }
.theme-card .check { position: absolute; top: 0.35rem; right: 0.35rem; width: 1.5rem; height: 1.5rem; border-radius: 50%; background: var(--text); color: var(--surface); display: none; place-items: center; }
.theme-card .check .icon { width: 1rem; height: 1rem; stroke-width: 3; }
.theme-card[aria-checked="true"] .check { display: grid; }
.theme-card .name { font-weight: 600; font-size: 0.95rem; line-height: 1.3; }
.theme-card .note { font-size: 0.8rem; color: var(--muted); line-height: 1.4; }
.swatch {
  height: 3.25rem; border-radius: 8px; padding: 0.4rem; display: flex; align-items: flex-end; gap: 0.3rem;
  border: 1px solid rgb(128 128 128 / 0.35);
}
.swatch i { display: block; border-radius: 4px; }
.swatch .sw-card { flex: 1; height: 100%; }
.swatch .sw-btn { width: 1.6rem; height: 0.9rem; }
.swatch.split { background: linear-gradient(135deg, #F5F7F6 50%, #111716 50%); }

/* ---------- Main ---------- */
main { padding: 1.5rem 0 3rem; }
.crumbs { font-size: 0.9rem; color: var(--muted); margin-bottom: 0.75rem; }
.crumbs ol { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 0.25rem; }
.crumbs li + li::before { content: '›'; margin-right: 0.25rem; }
.crumbs a { color: var(--muted); }
.lead { font-size: 1.05rem; color: var(--muted); }

/* ---------- Homepage ---------- */
.home-head { padding: 0.5rem 0 1rem; }
.home-head h1 { margin-bottom: 0.25rem; }
.home-head p { color: var(--muted); margin: 0; }
.calc-grid { list-style: none; padding: 0; margin: 0; display: grid; gap: 1rem; grid-template-columns: 1fr; }
@media (min-width: 40rem) { .calc-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (min-width: 64rem) { .calc-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.calc-card {
  display: flex; gap: 1rem; align-items: flex-start; height: 100%;
  background: var(--surface); color: var(--text); text-decoration: none;
  border: var(--border-w) solid var(--border); border-radius: var(--radius);
  padding: 1.25rem;
  transition: border-color 150ms ease, transform 150ms ease;
}
a.calc-card:hover { border-color: var(--accent); }
a.calc-card:active { transform: scale(0.99); }
.calc-card h2 { font-size: 1.2rem; margin: 0 0 0.25rem; }
.calc-card p { margin: 0; color: var(--muted); }
.calc-card .go { margin-top: 0.5rem; color: var(--accent); font-weight: 600; display: inline-flex; align-items: center; gap: 0.25rem; }
.cat-icon { width: 3rem; height: 3rem; border-radius: 12px; display: grid; place-items: center; flex: none; border: 2px solid currentColor; }
.cat-elec { color: var(--cat-elec); }
.cat-water { color: var(--cat-water); }
.cat-tax { color: var(--cat-tax); }
.cat-split { color: var(--cat-split); }
.calc-card.soon { background: transparent; border-style: dashed; }
.calc-card.soon .cat-icon { color: var(--muted); }
.soon-tag { display: inline-block; font-size: 0.8rem; font-weight: 600; color: var(--muted); border: var(--border-w) solid var(--border); border-radius: 999px; padding: 0 0.6rem; margin-top: 0.5rem; }
.home-note { margin-top: 2rem; color: var(--muted); }

/* ---------- Tool card ---------- */
.tool {
  background: var(--surface);
  border: var(--border-w) solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin: 1.25rem 0 1rem;
}
@media (min-width: 40rem) { .tool { padding: 1.75rem; } }
.tool h2 { margin-top: 0; font-size: 1.2rem; }

.seg { display: grid; grid-template-columns: 1fr 1fr; border: var(--border-w) solid var(--border); border-radius: var(--radius-btn); overflow: hidden; margin-bottom: 1.25rem; }
.seg legend { padding: 0; }
.seg label {
  min-height: var(--tap); display: flex; align-items: center; justify-content: center; text-align: center;
  padding: 0.4rem 0.5rem; cursor: pointer; font-weight: 600; line-height: 1.3;
}
.seg input + label ~ label { border-left: var(--border-w) solid var(--border); }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg input:checked + label { background: var(--accent); color: var(--on-accent); }
.seg input:focus-visible + label { outline: 3px solid var(--accent); outline-offset: -5px; }
fieldset { border: 0; padding: 0; margin: 0; min-width: 0; }

.field { margin-bottom: 1.25rem; }
.field > label, .field-label { display: block; font-weight: 600; margin-bottom: 0.35rem; }
.field .hint { display: block; font-size: 0.9rem; color: var(--muted); margin-top: 0.35rem; }
.input-row { display: flex; gap: 0.5rem; align-items: stretch; }
.input-wrap { position: relative; flex: 1; min-width: 0; }
.input-wrap input {
  width: 100%; min-height: 3.25rem;
  font: inherit; font-family: var(--font-num); font-size: 1.25rem; font-weight: 500;
  color: var(--text); background: var(--bg);
  border: calc(var(--border-w) + 1px) solid var(--border); border-radius: var(--radius-btn);
  padding: 0.4rem 4.5rem 0.4rem 0.9rem;
}
.input-wrap input:focus { border-color: var(--accent); outline: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 35%, transparent); }
.input-wrap .unit { position: absolute; right: 0.9rem; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; font-size: 0.95rem; }
/* Larger text sizes: the label already names the unit, so free up room in the input */
html[data-size] .input-wrap .unit { display: none; }
html[data-size] .input-wrap input { padding-right: 0.6rem; padding-left: 0.6rem; }
html[data-size] .tool { padding-left: 0.9rem; padding-right: 0.9rem; }
html[data-size] .input-row { gap: 0.35rem; }
.input-wrap input { min-width: 0; }
.step {
  font: inherit; font-size: 1.5rem; font-weight: 600; line-height: 1;
  width: 3.25rem; min-height: 3.25rem; flex: none;
  background: var(--surface); color: var(--text);
  border: calc(var(--border-w) + 1px) solid var(--border); border-radius: var(--radius-btn);
  cursor: pointer; transition: transform 150ms ease, background-color 150ms ease;
}
.step:hover { background: var(--accent-soft); }
.field-error { color: var(--bad); font-weight: 600; display: flex; gap: 0.4rem; align-items: center; margin-top: 0.4rem; }
.field-error[hidden] { display: none; }
.field-error .icon { width: 1.2rem; height: 1.2rem; }
.meter-fields[hidden], .units-fields[hidden] { display: none; }
.sample-note { font-size: 0.9rem; color: var(--muted); margin: -0.5rem 0 1rem; }
.noscript-note { border: var(--border-w) solid var(--border); border-radius: var(--radius-btn); padding: 0.75rem 1rem; }

/* ---------- Results ---------- */
.result { border-top: var(--border-w) solid var(--border); padding-top: 1.25rem; margin-top: 0.5rem; }
.result h2 { margin-bottom: 0.75rem; }
.big-pair { display: grid; gap: 1rem; grid-template-columns: 1fr; }
@media (min-width: 30rem) { .big-pair { grid-template-columns: 1fr 1fr; } }
.big { display: flex; flex-direction: column; gap: 0.1rem; }
.big .k { color: var(--muted); font-size: 0.95rem; font-weight: 600; }
.big .v { font-family: var(--font-num); font-weight: 600; font-size: 2rem; line-height: 1.2; }
.big .v small { font-family: var(--font-body); font-size: 1rem; font-weight: 500; color: var(--muted); margin-left: 0.25rem; }
.big .sub { font-size: 0.9rem; color: var(--muted); }

.chip {
  display: inline-flex; align-items: center; gap: 0.5rem;
  border: 2px solid currentColor; border-radius: 999px;
  padding: 0.3rem 0.9rem 0.3rem 0.6rem; font-weight: 700; margin: 1.25rem 0 0.25rem;
  background: var(--surface);
}
.chip.good { color: var(--good); }
.chip.warn { color: var(--warn); }
.chip.bad { color: var(--bad); }
.chip.none { color: var(--muted); }
.chip-text { margin: 0.25rem 0 1rem; }

.diff-grid { display: grid; grid-template-columns: 1fr; gap: 0.75rem; margin: 1rem 0; }
@media (min-width: 30rem) { .diff-grid { grid-template-columns: 1fr 1fr; } }
.diff-grid .v { font-size: 1.6rem; }
.big .v { overflow-wrap: anywhere; }

/* Comparison bars */
.bars { margin: 1.25rem 0; display: grid; gap: 0.75rem; }
.bar-row .bar-label { display: flex; justify-content: space-between; gap: 0.5rem; font-weight: 600; font-size: 0.95rem; }
.bar-track { height: 1.4rem; background: var(--bg); border: var(--border-w) solid var(--border); border-radius: 6px; overflow: hidden; margin-top: 0.25rem; }
.bar-fill { height: 100%; width: 0; border-radius: 5px 0 0 5px; transition: width 450ms cubic-bezier(.2,.8,.2,1); }
.bar-fill.dorm { background: var(--accent); }
.bar-fill.official {
  background: repeating-linear-gradient(135deg, var(--muted) 0 6px, color-mix(in srgb, var(--muted) 70%, var(--surface)) 6px 12px);
}

.result-actions { display: flex; flex-wrap: wrap; gap: 0.75rem; margin-top: 1.25rem; }
.share-out { margin-top: 1rem; }
.share-out[hidden] { display: none; }
.share-out img { width: 100%; max-width: 20rem; height: auto; border: var(--border-w) solid var(--border); border-radius: 12px; display: block; margin-bottom: 0.75rem; }
.share-out .actions { display: flex; flex-wrap: wrap; gap: 0.75rem; }

details.breakdown { margin-top: 1.25rem; border: var(--border-w) solid var(--border); border-radius: var(--radius-btn); }
details.breakdown > summary { min-height: var(--tap); padding: 0.6rem 1rem; cursor: pointer; font-weight: 600; display: flex; align-items: center; }
details.breakdown[open] > summary { border-bottom: var(--border-w) solid var(--border); }
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.95rem; }
th, td { text-align: left; padding: 0.55rem 0.75rem; border-bottom: var(--border-w) solid var(--border); vertical-align: top; }
td.r, th.r { text-align: right; font-family: var(--font-num); white-space: nowrap; }
tfoot td { font-weight: 700; border-bottom: 0; }
.rate-note { font-size: 0.9rem; color: var(--muted); padding: 0.75rem 1rem 0; margin: 0; }
.stale { color: var(--warn); font-weight: 600; display: flex; gap: 0.4rem; align-items: flex-start; }
.stale[hidden] { display: none; }

/* Reserved ad slot: collapsed while empty */
.ad-slot { margin: 1.5rem 0; min-height: 0; }
.ad-slot:empty { display: none; }

/* ---------- Article ---------- */
.article { margin-top: 2.5rem; }
.article table { margin: 0 0 1.25rem; }
.callout {
  background: var(--accent-soft); border-left: 4px solid var(--accent);
  border-radius: 0 var(--radius-btn) var(--radius-btn) 0;
  padding: 1rem 1.25rem; margin: 1.25rem 0;
}
.callout p:last-child { margin-bottom: 0; }
.faq details { border-bottom: var(--border-w) solid var(--border); }
.faq summary { min-height: var(--tap); padding: 0.75rem 0; cursor: pointer; font-weight: 600; font-family: var(--font-head); font-size: 1.05rem; }
.faq details p { margin-bottom: 1rem; }
.related { list-style: none; padding: 0; display: grid; gap: 0.5rem; }
.related a, .related span { display: flex; min-height: var(--tap); align-items: center; gap: 0.6rem; }
.related span { color: var(--muted); }
.sources li { margin-bottom: 0.4rem; }

/* ---------- Footer ---------- */
.site-footer { border-top: var(--border-w) solid var(--border); background: var(--surface); padding: 1.5rem 0 2rem; font-size: 0.95rem; }
.site-footer nav ul { list-style: none; padding: 0; margin: 0 0 0.75rem; display: flex; flex-wrap: wrap; gap: 0.25rem 1.25rem; }
.site-footer nav a { display: inline-flex; min-height: var(--tap); align-items: center; }
.site-footer p { color: var(--muted); margin: 0 0 0.25rem; }

/* High contrast: links always underlined, thicker focus */
:root[data-theme="contrast"] a { text-decoration-thickness: 2px; }
:root[data-theme="contrast"] :focus-visible { outline-width: 4px; }

/* Onyx: accent is near-white, so add weight to active states */
:root[data-theme="onyx"] .seg input:checked + label,
:root[data-theme="onyx"] .size-ctl button[aria-pressed="true"] { box-shadow: inset 0 0 0 2px var(--bg); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}
