:root {
  --bg: #f6f7f9; --surface: #fff; --text: #1d2430; --muted: #5f6b7a; --border: #dde2e8;
  --primary: #1f5fae; --primary-text: #fff; --danger: #b42318;
  --ok-bg: #e8f5ec; --ok: #1e6b3a; --warn-bg: #fff6e0; --warn: #8a5a00; --err-bg: #fdecea; --err: #a3271b;
  --control-border: #848e9c; --radius: 8px;
  font-family: "Sarabun", "Noto Sans Thai", "Thonburi", system-ui, -apple-system, "Segoe UI", sans-serif;
  /* Native UI (scrollbars, form controls, page canvas) follows the OS light/dark setting */
  color-scheme: light dark;
  accent-color: var(--primary);
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #14181f; --surface: #1c222b; --text: #e6e9ee; --muted: #9aa5b4; --border: #2e3642;
          --primary: #5b9bea; --primary-text: #0b1220; --danger: #f07167; --control-border: #677385;
          --ok-bg: #16301f; --ok: #7fd49a; --warn-bg: #352a10; --warn: #f0c060; --err-bg: #3a1714; --err: #f4a199; }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font-size: 16px; line-height: 1.6; }
a { color: var(--primary); }
h1 { font-size: 1.5rem; margin: 0 0 .75rem; }
h2 { font-size: 1.15rem; margin: 0 0 .5rem; }
.muted, .hint { color: var(--muted); }
.hint { display: block; font-size: .85rem; }
.nowrap { white-space: nowrap; }

.topbar { display: flex; flex-wrap: wrap; align-items: center; gap: .5rem 1.5rem; padding: .6rem 1rem;
          background: var(--surface); border-bottom: 1px solid var(--border); }
.brand { font-weight: 700; text-decoration: none; color: var(--text); }
.nav { display: flex; gap: 1rem; flex-wrap: wrap; }
.nav a { text-decoration: none; }
/* Current page: bold + underline, not colour alone */
.nav a[aria-current="page"] { font-weight: 700; text-decoration: underline; text-decoration-thickness: 2px; text-underline-offset: .35em; }
.me { margin-left: auto; display: flex; gap: 1rem; align-items: center; flex-wrap: wrap; font-size: .95rem; }

.container { max-width: 1100px; margin: 0 auto; padding: 1.25rem 1rem 3rem; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
        padding: 1.25rem; margin-bottom: 1rem; }
.card.narrow, .narrow-form { max-width: 520px; }
.stack details[open] { display: grid; gap: .4rem; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.page-head .actions { display: flex; gap: .5rem; flex-wrap: wrap; }

.stack { display: grid; gap: .9rem; }
label { display: grid; gap: .25rem; font-weight: 500; }
input, select { font: inherit; padding: .5rem .6rem; border: 1px solid var(--control-border); border-radius: 6px;
                background: var(--surface); color: var(--text); width: 100%; }
fieldset { border: 1px solid var(--border); border-radius: 6px; padding: .5rem .9rem; display: grid; gap: .4rem; }
legend { font-weight: 500; padding: 0 .3rem; }
label.radio, label.check { display: flex; gap: .5rem; align-items: baseline; font-weight: 400; }
label.radio input, label.check input { width: auto; }

button, .button { font: inherit; display: inline-block; padding: .5rem 1rem; border-radius: 6px; cursor: pointer;
                  border: 1px solid var(--control-border); background: var(--surface); color: var(--text); text-decoration: none;
                  min-height: 2.5rem; transition: background-color .15s ease-out, border-color .15s ease-out, color .15s ease-out; }
button:hover:not([disabled]), .button:hover { border-color: var(--primary); color: var(--primary); }
button.primary:hover:not([disabled]), .button.primary:hover { background: color-mix(in srgb, var(--primary) 85%, var(--text)); color: var(--primary-text); }
button.danger:hover:not([disabled]) { background: var(--err-bg); color: var(--danger); }
button.primary, .button.primary { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }
button.danger { background: transparent; color: var(--danger); border-color: var(--danger); }
button.link { border: 0; background: none; padding: 0 .15rem; min-height: 1.5rem; color: var(--primary); text-decoration: underline; }
button.link:hover:not([disabled]) { color: var(--primary); text-decoration-thickness: 2px; }
form.inline { display: inline; }

.flash { padding: .6rem .9rem; border-radius: 6px; margin-bottom: 1rem; }
.flash-ok { background: var(--ok-bg); color: var(--ok); }
.flash-warn { background: var(--warn-bg); color: var(--warn); }
.flash-error { background: var(--err-bg); color: var(--err); }

.role { font-size: .8rem; padding: .05rem .5rem; border-radius: 999px; border: 1px solid var(--border); white-space: nowrap; }
.role-admin { color: var(--err); }
.role-analyst { color: var(--primary); }
.role-executive { color: var(--ok); }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
table { border-collapse: collapse; width: 100%; font-size: .95rem; }
th, td { text-align: left; padding: .55rem .75rem; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 500; }
tr.inactive td { color: var(--muted); }
code.detail { font-size: .8rem; word-break: break-all; }
.action-auth { white-space: nowrap; }

.filters { display: flex; flex-wrap: wrap; gap: .75rem; align-items: end; margin-bottom: 1rem; }
.filters label { font-weight: 400; }
.pager { display: flex; gap: 1rem; justify-content: center; margin-top: 1rem; }

.temp-password code { font-size: 1.5rem; padding: .4rem .8rem; background: var(--bg); border-radius: 6px; user-select: all; }
.milestones li { color: var(--muted); }
.milestones li.done { color: var(--ok); font-weight: 500; }
.milestones li.done::after { content: " ✓"; }

.badge { display: inline-block; font-size: .8rem; padding: .05rem .55rem; border-radius: 999px;
         border: 1px solid var(--border); color: var(--muted); text-decoration: none; white-space: nowrap; }
.status-ok { color: var(--ok); background: var(--ok-bg); border-color: transparent; }
.status-empty, .status-partial, .status-running { color: var(--warn); background: var(--warn-bg); border-color: transparent; }
.status-error { color: var(--err); background: var(--err-bg); border-color: transparent; }
.err-text { color: var(--err); }
.row-form { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; margin: .2rem 0; }
.row-form button { padding: .3rem .7rem; min-height: 2rem; }
.inline-field { display: inline-flex; align-items: center; gap: .3rem; font-weight: 400; }
.inline-field input { width: 5rem; padding: .3rem .4rem; }
button[disabled] { opacity: .5; cursor: not-allowed; }
h2 { margin-top: 1.5rem; }

.card.wide { max-width: 760px; }
textarea { font: inherit; padding: .5rem .6rem; border: 1px solid var(--control-border); border-radius: 6px;
           background: var(--surface); color: var(--text); width: 100%; resize: vertical; }

.chip { display: inline-block; padding: .1rem .6rem; margin: .1rem .2rem .1rem 0; border-radius: 999px;
        background: var(--bg); border: 1px solid var(--border); font-size: .9rem; }
.chip-ex { text-decoration: line-through; color: var(--muted); }
.term-list { list-style: none; padding: 0; display: grid; gap: .3rem; }
.term-list li { display: flex; align-items: center; gap: .6rem; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 1rem; }
.search-row { display: flex; gap: .5rem; margin-bottom: .75rem; }
.search-row input { font-size: 1.1rem; }
.source-filter { display: flex; flex-wrap: wrap; gap: .3rem 1rem; margin-bottom: 1rem; }
.source-filter legend { width: 100%; }
.results { list-style: none; padding: 0; display: grid; gap: .75rem; }
.result { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .9rem 1rem; }
.result h3 { margin: .2rem 0 .3rem; font-size: 1.1rem; line-height: 1.45; }
.result h3 a { text-decoration: none; color: var(--text); }
.result h3 a:hover { text-decoration: underline; }
.result-meta { color: var(--muted); font-size: .9rem; display: flex; flex-wrap: wrap; gap: .35rem; align-items: center; }
.snippet { margin: .2rem 0 .4rem; color: var(--text); }
.small { font-size: .9rem; }
.badge.wl { color: var(--primary); }
mark { background: #ffe58a; color: #1d2430; padding: 0 .1em; border-radius: 3px; }
.facts { display: grid; grid-template-columns: max-content 1fr; gap: .3rem 1rem; margin: 1rem 0; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; }
.article-text p { margin: 0 0 .8rem; }
.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: .75rem; margin-bottom: 1rem; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .8rem 1rem; }
.stat b { display: block; font-size: 1.6rem; }
.stat span { color: var(--muted); font-size: .9rem; }
a.stat { text-decoration: none; color: inherit; } a.stat:hover { border-color: var(--primary); }

.ai-summary { margin: .2rem 0 .4rem; padding: .45rem .7rem; border-left: 3px solid var(--primary); background: var(--bg); border-radius: 4px; }
.ai-label { font-size: .8rem; color: var(--primary); font-weight: 600; margin: 0 .3rem 0 0; }
.ai-box { border: 1px solid var(--border); border-left: 4px solid var(--primary); border-radius: 6px; padding: .6rem .9rem; margin: 1rem 0; }
.ai-box p { margin: .2rem 0; }
details summary { cursor: pointer; color: var(--primary); margin-top: .4rem; }

.result.excluded { opacity: .55; }
.rstatus-pending_approval, .rstatus-approved { color: var(--warn); background: var(--warn-bg); border-color: transparent; }
.rstatus-sent { color: var(--ok); background: var(--ok-bg); border-color: transparent; }
.rstatus-failed { color: var(--err); background: var(--err-bg); border-color: transparent; }
.badge.big { font-size: 1rem; padding: .2rem .8rem; }
.approve-box { border-left: 4px solid var(--warn); }
.card.prose { max-width: 760px; line-height: 1.7; }
.card.prose h2 { margin-top: 1.4rem; font-size: 1.1rem; }

/* ---- Accessibility & interaction pass (UI UX Pro Max checklist) ---- */
/* Visible keyboard focus on every control (mouse clicks don't show it) */
:focus-visible { outline: 3px solid var(--primary); outline-offset: 2px; border-radius: 4px; }
input:focus-visible, select:focus-visible, textarea:focus-visible { outline-offset: 0; border-color: var(--primary); }
/* Skip link: hidden until focused with the keyboard */
.skip-link { position: absolute; left: .5rem; top: -3rem; z-index: 100; padding: .5rem 1rem; border-radius: 6px;
             background: var(--primary); color: var(--primary-text); font-weight: 600; text-decoration: none; }
.skip-link:focus { top: .5rem; }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden;
                   clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
/* Checkboxes/radios big enough to hit (WCAG 2.5.8: ≥ 24px target incl. label) */
input[type="checkbox"], input[type="radio"] { width: 1.15rem; height: 1.15rem; flex: none; }
label.radio, label.check { min-height: 1.5rem; align-items: center; }
/* Top-menu links: comfortable hit area without changing the look */
.nav a { padding: .2rem 0; }
/* Numbers line up in tables and stat tiles */
table, .stat b, .pager { font-variant-numeric: tabular-nums; }
/* Long Thai headlines and URLs wrap instead of overflowing */
h1, .result h3 { text-wrap: pretty; }
.result h3, td, .snippet, code.detail { overflow-wrap: anywhere; }
.stat, .result { transition: border-color .15s ease-out; }
.result:hover { border-color: var(--control-border); }
/* Touch screens: 44px targets for primary controls */
@media (pointer: coarse) {
  button, .button, select, input:not([type="checkbox"]):not([type="radio"]) { min-height: 44px; }
  /* Menu links: 36px (WCAG 2.5.8 asks ≥ 24px) keeps the wrapped top bar compact on phones */
  .topbar { row-gap: 0; }
  .nav { row-gap: 0; }
  .nav a, .me a, button.link { display: inline-flex; align-items: center; min-height: 36px; padding: 0; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; scroll-behavior: auto !important; }
}

/* Skip-link target: focused by script/anchor, no ring needed on the whole page */
main:focus { outline: none; }

/* ---- Home: trend line in stat tiles + daily bar chart ---- */
.stat .trend { display: block; margin-top: .2rem; font-size: .85rem; color: var(--muted); }
.stat .trend-up span, .stat .trend-down span { font-weight: 700; color: var(--primary); }
.chart { margin: 0 0 1rem; padding: .9rem 1rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.chart figcaption { display: flex; flex-wrap: wrap; gap: .2rem .75rem; align-items: baseline; margin-bottom: .6rem; }
.chart figcaption .muted { font-size: .9rem; }
.chart-bars { display: block; width: 100%; height: 96px; }
.chart-bars .bar { fill: color-mix(in srgb, var(--primary) 70%, var(--surface)); }
.chart-bars .bar-today { fill: var(--primary); }
.chart-bars rect:hover { fill: var(--text); }
.chart-base { stroke: var(--control-border); stroke-width: 1; }
.chart-axis { display: flex; justify-content: space-between; font-size: .8rem; color: var(--muted); margin-top: .25rem; }
.chart-table summary { font-size: .9rem; margin-top: .5rem; }
.chart-table .table-wrap { margin-top: .5rem; max-height: 16rem; overflow-y: auto; }
.chart-table td:last-child, .chart-table th:last-child { text-align: right; }
@media (forced-colors: active) {
  .chart-bars .bar, .chart-bars .bar-today { fill: CanvasText; }
}
