:root {
  --bg: #f6f7f5;
  --surface: #ffffff;
  --text: #1c2321;
  --muted: #64716d;
  --line: #e2e6e3;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --band: #16a34a;
  --f-N: #16a34a;
  --f-H: #dc2626;
  --f-L: #d97706;
  --f-A: #dc2626;
  --warn-bg: #fff7e6;
  --warn: #9a5b00;
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #111615;
    --surface: #1a2120;
    --text: #e7ecea;
    --muted: #93a19c;
    --line: #2b3533;
    --accent: #2dd4bf;
    --accent-ink: #062b27;
    --band: #4ade80;
    --f-N: #4ade80;
    --f-H: #f87171;
    --f-L: #fbbf24;
    --f-A: #f87171;
    --warn-bg: #2d2412;
    --warn: #fbbf24;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 'IBM Plex Sans Thai', system-ui, -apple-system, 'Segoe UI', sans-serif;
}
h1, h2, h3 { line-height: 1.3; margin: 0 0 .6em; }
h1 { font-size: clamp(1.45rem, 3vw, 2rem); font-weight: 600; letter-spacing: -.025em; }
a { color: var(--accent); }
code { font-size: .85em; overflow-wrap: anywhere; }
.eyebrow { display: block; color: var(--accent); font-size: .78rem; font-weight: 600; margin-bottom: .6rem; }
.page-heading { display: flex; justify-content: space-between; align-items: center; gap: 1rem; margin: 1rem 0 1.5rem; flex-wrap: wrap; }
.page-heading h1 { margin: 0; }
.setup { max-width: 780px; margin: 2rem auto; padding: clamp(1rem, 4vw, 2.5rem); }
.setup-notice { padding: 1rem; border-left: 3px solid var(--accent); background: var(--bg); margin: 1.5rem 0; }
.setup-steps { padding-left: 1.5rem; text-align: left; }
.setup-steps li { padding: .65rem 0 .65rem .5rem; border-bottom: 1px solid var(--line); }
.setup-steps li::marker { color: var(--accent); font-weight: 600; }
.setup-steps p { margin: .25rem 0; font-size: .9rem; color: var(--muted); }
.setup-help { margin-top: 1.25rem; text-align: left; }
summary { cursor: pointer; color: var(--accent); }
.date-filter { display: flex; align-items: end; flex-wrap: wrap; gap: .65rem; margin-bottom: 1rem; }
.date-filter label { color: var(--muted); font-size: .8rem; }
.date-filter input { display: block; }
.connection-list { list-style: none; padding: 0; }
.connection-list li { display: flex; justify-content: space-between; gap: 1rem; flex-wrap: wrap; padding: .6rem 0; border-bottom: 1px solid var(--line); }
.connection-list b { font-size: .88rem; font-weight: 500; overflow-wrap: anywhere; }
.review-check { white-space: normal !important; padding: 1rem 0; }
.review-check input { flex: 0 0 auto; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1rem; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.spacer { flex: 1; }
.warn { background: var(--warn-bg); color: var(--warn); padding: .5em .75em; border-radius: 8px; margin: .5em 0; }
.warn.small { padding: 0; background: none; }
.error { color: var(--f-H); }

/* header / nav */
.top {
  display: flex; align-items: center; gap: .75rem;
  padding: .7rem 16px; background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.brand { font-weight: 600; display: flex; align-items: center; gap: .5rem; white-space: nowrap; }
.logo { display: inline-grid; place-items: center; width: 28px; height: 28px; border-radius: 8px; background: var(--accent); color: var(--accent-ink); font-size: .75rem; }
#who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 30vw; }
nav#tabs { display: flex; gap: .25rem; padding: .5rem 16px; overflow-x: auto; border-bottom: 1px solid var(--line); background: var(--surface); }
nav#tabs button { background: none; border: 0; padding: .45rem .8rem; border-radius: 999px; color: var(--muted); white-space: nowrap; }
nav#tabs button.active { background: var(--accent); color: var(--accent-ink); }
main { max-width: 1100px; margin: 0 auto; padding: 16px; }

/* controls */
button, .btn-like, input, select, textarea {
  font: inherit; color: inherit;
}
button, .btn-like {
  cursor: pointer; border-radius: 8px; padding: .5rem .95rem; border: 1px solid var(--line); background: var(--surface);
}
button:disabled { opacity: .5; cursor: not-allowed; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 500; }
button.ghost, .btn-like.ghost { background: transparent; }
button.small { padding: .25rem .6rem; font-size: .82rem; }
button.danger { color: var(--f-H); }
input, select, textarea {
  width: 100%; padding: .5rem .6rem; border: 1px solid var(--line); border-radius: 8px; background: var(--surface);
}
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
textarea { resize: vertical; }
#memberSel { width: auto; max-width: 40vw; }
.row { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; margin-top: .75rem; }
.chk { display: flex; align-items: center; gap: .4rem; white-space: nowrap; }
.chk input { width: auto; }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.1rem; margin-bottom: 1rem; }
.card.empty, .login { text-align: center; padding: 2.5rem 1rem; }
.login { max-width: 520px; margin: 3rem auto; }

.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: .75rem; margin: .75rem 0; }
.form-grid label { display: flex; flex-direction: column; gap: .25rem; font-size: .85rem; color: var(--muted); }
.form-grid .wide { grid-column: 1 / -1; }

.upload { border: 1px dashed var(--line); border-radius: 10px; padding: .9rem; margin: .75rem 0; }
.upload input[type=file] { border: 0; padding: 0; }
.previews .pv { display: flex; gap: .5rem; overflow-x: auto; padding: .5rem 0; }
.previews img { height: 220px; border-radius: 6px; border: 1px solid var(--line); }
.pill { border: 1px solid var(--line); border-radius: 999px; padding: .2rem .6rem; font-size: .8rem; }

/* summary + cards */
.summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: .75rem; margin-bottom: 1rem; }
.summary > div { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: .8rem 1rem; }
.toolbar { display: flex; gap: .75rem; align-items: center; flex-wrap: wrap; margin-bottom: .5rem; }
.toolbar input[type=search] { flex: 1; min-width: 200px; }
h3.cat { margin: 1.2rem 0 .5rem; color: var(--muted); font-weight: 500; font-size: .9rem; }
.grid-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: .6rem; }
.tcard {
  text-align: left; display: flex; flex-direction: column; gap: .15rem; padding: .75rem .8rem .5rem;
  border-radius: 10px; border: 1px solid var(--line); border-left: 4px solid var(--f-N); color: var(--accent);
}
.tcard:hover { border-color: var(--accent); }
.tcard > * { color: var(--text); }
.tcard .spark { color: var(--accent); }
.f-border-H, .f-border-A { border-left-color: var(--f-H); }
.f-border-L { border-left-color: var(--f-L); }
.f-border-unknown { border-left-color: var(--muted); }
.tc-head { display: flex; justify-content: space-between; gap: .4rem; align-items: start; }
.tc-name { font-weight: 500; font-size: .9rem; }
.tc-val b { font-size: 1.35rem; font-variant-numeric: tabular-nums; }
.tc-foot { display: flex; justify-content: space-between; gap: .3rem; flex-wrap: wrap; }
.delta { font-size: .78rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.spark { width: 100%; height: 32px; margin-top: .2rem; }
.dot-N { fill: var(--f-N); } .dot-H, .dot-A { fill: var(--f-H); } .dot-L { fill: var(--f-L); }
.dot-unknown { fill: var(--muted); }

.flag { font-size: .72rem; padding: .05rem .45rem; border-radius: 999px; white-space: nowrap; font-weight: 500; }
.f-H, .f-A { background: color-mix(in srgb, var(--f-H) 15%, transparent); color: var(--f-H); }
.f-L { background: color-mix(in srgb, var(--f-L) 18%, transparent); color: var(--f-L); }
.txt-H, .txt-A { color: var(--f-H); } .txt-L { color: var(--f-L); } .txt-N { color: var(--f-N); }

/* tables */
.table-wrap { overflow-x: auto; margin: .5rem 0; }
.tbl { width: 100%; border-collapse: collapse; font-size: .88rem; }
.tbl th, .tbl td { padding: .4rem .45rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
.tbl th { color: var(--muted); font-weight: 500; font-size: .8rem; }
.tbl td.num { font-variant-numeric: tabular-nums; }
.tbl.edit td { padding: .2rem; }
.tbl.edit input { padding: .3rem .4rem; }
.w-code { width: 90px; text-transform: uppercase; }
.w-code.unk { border-style: dashed; }
.w-name { min-width: 170px; }
.w-num { width: 80px; }
.w-unit { width: 90px; }

/* reports / members */
.rep, .mem { display: flex; justify-content: space-between; gap: .75rem; padding: .75rem 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.rep-act, .mem > div:last-child { display: flex; gap: .3rem; align-items: start; flex-wrap: wrap; }
.chips { display: flex; flex-wrap: wrap; gap: .25rem; margin-top: .3rem; }

/* modal */
dialog#modal {
  width: min(860px, calc(100vw - 32px)); max-height: calc(100vh - 32px); border: 1px solid var(--line);
  border-radius: var(--radius); background: var(--surface); color: var(--text); padding: 1rem;
}
dialog::backdrop { background: rgb(0 0 0 / .45); }
.modal-head { display: flex; justify-content: space-between; align-items: center; gap: 1rem; }
.modal-head h3 { margin: 0; }
.chart-box { position: relative; height: 300px; margin: .5rem 0 1rem; }

/* toast + spinner */
#toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: .6rem 1rem; border-radius: 10px;
  opacity: 0; pointer-events: none; transition: .2s; max-width: calc(100vw - 32px); z-index: 20;
}
#toast.show { opacity: 1; transform: translateX(-50%); }
.spinner { display: inline-block; width: 14px; height: 14px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; vertical-align: -2px; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 600px) {
  .top { flex-wrap: wrap; }
  #who { display: none; }
  .grid-cards { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .tc-val b { font-size: 1.15rem; }
  .previews img { height: 160px; }
  .chk { white-space: normal; }
  .grid-cards > *, .form-grid > *, .summary > * { min-width: 0; }
  .tc-head { flex-wrap: wrap; }
  .setup { margin-top: .5rem; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition: none !important; }
}
