/* NIZE KPI — hand-written responsive CSS. No framework, no build step. */

:root {
  --bg: #f4f6fa;
  --panel: #ffffff;
  --ink: #16233a;
  --ink-soft: #5a6982;
  --line: #dde3ee;
  --line-strong: #c3ccdd;
  --brand: #1d4ed8;
  --brand-soft: #e8eeff;
  --good: #12805c;
  --good-soft: #e2f6ee;
  --warn: #a35a06;
  --warn-soft: #fdf0da;
  --bad: #b42318;
  --bad-soft: #fdeceb;
  --shadow: 0 1px 2px rgba(20, 32, 60, .06), 0 6px 20px rgba(20, 32, 60, .05);
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans Thai", "Sarabun", "Leelawadee UI", Tahoma, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
}

.boot { padding: 3rem; text-align: center; color: var(--ink-soft); }

a { color: var(--brand); }
h1, h2, h3, h4 { margin: 0 0 .4rem; line-height: 1.3; }
h1 { font-size: 1.35rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .6rem; }
small { font-size: .82rem; }
code { font-family: ui-monospace, Menlo, monospace; font-size: .85em; background: #eef1f7; padding: .1em .35em; border-radius: 4px; }

/* ------------------------------------------------------------- layout */

.app-shell { min-height: 100vh; display: flex; flex-direction: column; }

.topbar {
  display: flex; align-items: center; gap: .75rem; flex-wrap: wrap;
  padding: .55rem 1rem; background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 40;
}
.topbar .brand { font-weight: 700; color: var(--brand); display: flex; align-items: center; gap: .45rem; text-decoration: none; }
.topbar .brand .logo { width: 26px; height: 26px; border-radius: 7px; background: var(--brand); color: #fff; display: grid; place-items: center; font-size: .9rem; }
.topbar nav { display: flex; gap: .2rem; flex-wrap: wrap; }
.topbar nav a {
  padding: .35rem .7rem; border-radius: 999px; text-decoration: none; color: var(--ink-soft); font-weight: 500; white-space: nowrap;
}
.topbar nav a:hover { background: #eef1f7; }
.topbar nav a.active { background: var(--brand-soft); color: var(--brand); }
.topbar .spacer { flex: 1; }
.topbar .who { font-size: .85rem; color: var(--ink-soft); text-align: right; }
.topbar .who strong { color: var(--ink); display: block; }

main.page { flex: 1; width: 100%; max-width: 1280px; margin: 0 auto; padding: 1rem 1rem 4rem; }
.page-head { display: flex; align-items: flex-start; gap: .75rem; flex-wrap: wrap; margin-bottom: .85rem; }
.page-head .grow { flex: 1; min-width: 240px; }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem; margin-bottom: 1rem; }
.panel > h2:first-child, .panel > h3:first-child { margin-top: 0; }
.panel-tight { padding: .65rem .8rem; }
.muted { color: var(--ink-soft); }
.mono { font-variant-numeric: tabular-nums; }
.right { text-align: right; }
.center { text-align: center; }
.nowrap { white-space: nowrap; }
.row { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; }
.row-tight { gap: .3rem; }
.stack > * + * { margin-top: .6rem; }
.grid { display: grid; gap: .75rem; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.grid-4 { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.hidden { display: none !important; }

/* -------------------------------------------------------------- inputs */

label { display: block; font-size: .84rem; color: var(--ink-soft); margin-bottom: .15rem; font-weight: 500; }
input, select, textarea, button { font: inherit; color: inherit; }
input[type=text], input[type=number], input[type=password], input[type=date], select, textarea {
  width: 100%; padding: .42rem .55rem; border: 1px solid var(--line-strong); border-radius: 7px; background: #fff;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: -1px; border-color: var(--brand); }
input[disabled], select[disabled], textarea[disabled] { background: #f2f4f8; color: var(--ink-soft); }
textarea { min-height: 68px; resize: vertical; }
input[type=number] { text-align: right; font-variant-numeric: tabular-nums; }
.field { margin-bottom: .6rem; }
.field-inline { display: flex; align-items: center; gap: .4rem; }
.field-inline label { margin: 0; }
.check { display: flex; align-items: center; gap: .4rem; font-size: .9rem; color: var(--ink); }
.check input { width: auto; }
.w-sm { max-width: 90px; }
.w-md { max-width: 150px; }
.w-lg { max-width: 260px; }

button, .btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .35rem;
  padding: .42rem .8rem; border-radius: 8px; border: 1px solid var(--line-strong);
  background: #fff; cursor: pointer; font-weight: 500; text-decoration: none; color: var(--ink);
}
button:hover, .btn:hover { background: #f4f6fa; }
button:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn-primary:hover { background: #1740b8; }
.btn-good { background: var(--good); border-color: var(--good); color: #fff; }
.btn-good:hover { background: #0d6a4c; }
.btn-warn { background: var(--warn-soft); border-color: #e6c68d; color: var(--warn); }
.btn-bad { background: var(--bad-soft); border-color: #f0b7b2; color: var(--bad); }
.btn-sm { padding: .22rem .5rem; font-size: .84rem; border-radius: 6px; }
.btn-ghost { border-color: transparent; background: transparent; color: var(--brand); }
.btn-ghost:hover { background: var(--brand-soft); }
.btn-icon { padding: .2rem .4rem; line-height: 1; }

/* ------------------------------------------------------- rating buttons */

.rating-set { display: flex; gap: .2rem; }
.rating-set button { width: 30px; padding: .2rem 0; font-variant-numeric: tabular-nums; }
.rating-set button.on { background: var(--brand); border-color: var(--brand); color: #fff; }

/* -------------------------------------------------------------- tables */

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { padding: .45rem .5rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { font-size: .8rem; text-transform: none; color: var(--ink-soft); font-weight: 600; background: #f8fafd; position: sticky; top: 0; }
tbody tr:hover { background: #fbfcfe; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tr.cat-head td { background: var(--brand-soft); font-weight: 600; color: var(--brand); }
tr.is-na td { opacity: .6; }
tr.has-problem { background: var(--bad-soft); }

/* --------------------------------------------------------------- badges */

.badge {
  display: inline-flex; align-items: center; gap: .25rem; padding: .1rem .45rem;
  border-radius: 999px; font-size: .78rem; font-weight: 600; white-space: nowrap;
  background: #eef1f7; color: var(--ink-soft);
}
.badge-good { background: var(--good-soft); color: var(--good); }
.badge-warn { background: var(--warn-soft); color: var(--warn); }
.badge-bad { background: var(--bad-soft); color: var(--bad); }
.badge-info { background: var(--brand-soft); color: var(--brand); }

.banner { border-radius: 8px; padding: .6rem .8rem; margin-bottom: .7rem; border: 1px solid; font-size: .9rem; }
.banner ul { margin: .3rem 0 0; padding-inline-start: 1.2rem; }
.banner-bad { background: var(--bad-soft); border-color: #f0b7b2; color: #7a1a12; }
.banner-warn { background: var(--warn-soft); border-color: #e6c68d; color: #6d3c04; }
.banner-good { background: var(--good-soft); border-color: #a9ddc9; color: #0a5942; }
.banner-info { background: var(--brand-soft); border-color: #bcccf8; color: #17347f; }

/* ---------------------------------------------------------------- cards */

.stat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: .7rem .85rem; }
.stat .k { font-size: .8rem; color: var(--ink-soft); }
.stat .v { font-size: 1.5rem; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.2; }
.stat .sub { font-size: .78rem; color: var(--ink-soft); }
.stat-brand { background: var(--brand); border-color: var(--brand); color: #fff; }
.stat-brand .k, .stat-brand .sub { color: #d5e0ff; }

.bar { height: 8px; border-radius: 999px; background: #e7ebf3; overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--brand); }
.bar-good > span { background: var(--good); }
.bar-warn > span { background: var(--warn); }
.bar-bad > span { background: var(--bad); }

/* ----------------------------------------------------------------- tabs */

.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--line); margin-bottom: .85rem; overflow-x: auto; }
.tabs button {
  border: none; background: transparent; border-bottom: 2px solid transparent; border-radius: 0;
  padding: .45rem .75rem; color: var(--ink-soft); white-space: nowrap;
}
.tabs button.on { color: var(--brand); border-bottom-color: var(--brand); font-weight: 600; }

.seg-tabs { display: flex; gap: .3rem; flex-wrap: wrap; margin-bottom: .7rem; }
.seg-tabs button.on { background: var(--brand); border-color: var(--brand); color: #fff; }

/* --------------------------------------------------------------- modals */

.modal-back {
  position: fixed; inset: 0; background: rgba(16, 24, 40, .45); display: flex; align-items: flex-start;
  justify-content: center; padding: 1rem; z-index: 60; overflow-y: auto;
}
.modal {
  background: var(--panel); border-radius: 12px; box-shadow: 0 20px 60px rgba(16, 24, 40, .3);
  width: 100%; max-width: 720px; margin: 3vh 0; padding: 1rem 1.1rem;
}
.modal-wide { max-width: 980px; }
.modal-head { display: flex; align-items: flex-start; gap: .6rem; margin-bottom: .6rem; }
.modal-head h3 { flex: 1; margin: 0; }
.modal-foot { display: flex; gap: .5rem; justify-content: flex-end; margin-top: .9rem; flex-wrap: wrap; }

/* --------------------------------------------------------------- toasts */

.toasts { position: fixed; right: 1rem; bottom: 1rem; z-index: 80; display: flex; flex-direction: column; gap: .5rem; max-width: min(420px, calc(100vw - 2rem)); }
.toast { padding: .6rem .8rem; border-radius: 9px; box-shadow: var(--shadow); font-size: .9rem; border: 1px solid; background: #fff; }
.toast ul { margin: .3rem 0 0; padding-inline-start: 1.1rem; }
.toast-bad { background: var(--bad-soft); border-color: #f0b7b2; color: #7a1a12; }
.toast-good { background: var(--good-soft); border-color: #a9ddc9; color: #0a5942; }
.toast-info { background: var(--brand-soft); border-color: #bcccf8; color: #17347f; }
.toast-warn { background: var(--warn-soft); border-color: #e6c68d; color: #6d3c04; }

.saving { position: fixed; left: 1rem; bottom: 1rem; z-index: 80; font-size: .82rem; color: var(--ink-soft); background: #fff; border: 1px solid var(--line); border-radius: 999px; padding: .2rem .7rem; box-shadow: var(--shadow); }

/* ------------------------------------------------------------ login page */

.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.auth-card { width: 100%; max-width: 380px; }

/* --------------------------------------------------------- item editor */

.item-title { font-weight: 600; }
.item-desc { font-size: .82rem; color: var(--ink-soft); white-space: pre-wrap; }
.item-desc-clip { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.inline-inputs { display: flex; gap: .25rem; align-items: center; }
.inline-inputs input { max-width: 74px; }
.frac { color: var(--ink-soft); }
.flagline { font-size: .78rem; color: var(--bad); }
.help { font-size: .78rem; color: var(--ink-soft); }
.tooltip { border-bottom: 1px dotted var(--ink-soft); cursor: help; }

.detail-list { display: grid; grid-template-columns: max-content 1fr; gap: .15rem .6rem; font-size: .88rem; }
.detail-list dt { color: var(--ink-soft); }
.detail-list dd { margin: 0; }

/* ------------------------------------------------- responsive: cards */

@media (max-width: 720px) {
  body { font-size: 14.5px; }
  main.page { padding: .7rem .7rem 4rem; }
  .topbar { padding: .5rem .6rem; }
  .topbar .who { font-size: .78rem; }

  /* Tables marked .collapse become stacked cards */
  table.collapse thead { display: none; }
  table.collapse, table.collapse tbody, table.collapse tr, table.collapse td { display: block; width: 100%; }
  table.collapse tr {
    border: 1px solid var(--line); border-radius: 9px; margin-bottom: .6rem; padding: .35rem .5rem; background: #fff;
  }
  table.collapse tr.cat-head { background: var(--brand-soft); }
  table.collapse td { border: none; padding: .2rem 0; display: flex; gap: .6rem; align-items: flex-start; }
  table.collapse td::before {
    content: attr(data-th); flex: 0 0 40%; max-width: 40%; color: var(--ink-soft); font-size: .8rem; font-weight: 500;
  }
  table.collapse td.num { text-align: left; }
  table.collapse td.full { display: block; }
  table.collapse td.full::before { display: block; }
  .grid-4, .grid-3 { grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); }
}

/* ----------------------------------------------------------- print view */

.print-sheet { background: #fff; max-width: 210mm; margin: 0 auto; padding: 12mm; color: #000; }
.print-sheet h1 { font-size: 1.2rem; }
.print-sheet table { font-size: .8rem; }
.print-sheet th, .print-sheet td { border: 1px solid #999; padding: .25rem .35rem; }
.print-sheet th { background: #eee; color: #000; position: static; }
.sign-row { display: flex; gap: 1rem; margin-top: 2rem; }
.sign-box { flex: 1; text-align: center; }
.sign-line { border-top: 1px solid #000; margin-top: 3rem; padding-top: .3rem; font-size: .8rem; }

@media print {
  body { background: #fff; }
  .topbar, .toasts, .no-print, .saving { display: none !important; }
  main.page { padding: 0; max-width: none; }
  .panel { box-shadow: none; border: none; padding: 0; margin: 0 0 .5rem; }
  .print-sheet { padding: 0; max-width: none; }
  .page-break { page-break-before: always; }
  table { font-size: 9pt; }
  @page { size: A4; margin: 12mm; }
}

/* ================================================= v2.2: monthly + groups */

/* group tabs (ออฟฟิศ | โรงงานและโกดัง | ทั้งหมด) */
.group-tabs { display: flex; gap: .3rem; flex-wrap: wrap; }
.group-tabs button {
  padding: .3rem .75rem; border-radius: 999px; border: 1px solid var(--line-strong);
  background: #fff; color: var(--ink-soft); font-weight: 500;
}
.group-tabs button.on { background: var(--brand); border-color: var(--brand); color: #fff; }
.group-tabs .tab-count {
  margin-inline-start: .35rem; font-size: .75rem; opacity: .8; font-variant-numeric: tabular-nums;
}

/* the employee monthly form */
.month-head { border-inline-start: 4px solid var(--brand); }

/* 1–10 rating buttons: still tappable at 375px */
.rating-set-10 { flex-wrap: wrap; gap: .18rem; }
.rating-set-10 button { width: 30px; min-width: 30px; }

/* the supervisor rating table (rows = items, columns = people) */
.rate-table { font-size: .88rem; }
.rate-table th, .rate-table td { border-bottom: 1px solid var(--line); vertical-align: top; }
.rate-table tfoot th { background: var(--brand-soft); color: var(--brand); position: static; }
.rate-cell {
  width: 54px; max-width: 54px; text-align: center; padding: .3rem .2rem;
  font-variant-numeric: tabular-nums; font-weight: 600;
}
.rate-cell.cell-dirty { border-color: var(--warn); background: var(--warn-soft); }
.rate-cell.cell-copied { border-color: var(--brand); background: var(--brand-soft); }

/* the 12-month grid (rows = people, columns = months) */
.grid-table { font-size: .86rem; }
.grid-table th, .grid-table td { border: 1px solid var(--line); padding: .3rem .4rem; }
.grid-table th { background: #f8fafd; }
.grid-table td.cell-good { background: var(--good-soft); color: var(--good); font-weight: 600; }
.grid-table td.cell-bad { background: var(--bad-soft); color: var(--bad); font-weight: 600; }
.grid-table td.cell-provisional { background: var(--warn-soft); color: var(--warn); }
.grid-table td.cell-empty { background: #f4f6fa; }

@media (max-width: 720px) {
  /* Wide monthly tables keep their own horizontal scroll instead of stacking */
  .rate-table, .grid-table { font-size: .82rem; }
  .rate-cell { width: 46px; max-width: 46px; }
  .group-tabs button { padding: .28rem .6rem; font-size: .86rem; }
  .rating-set-10 button { width: 27px; min-width: 27px; }
}

@media print {
  .group-tabs, .rate-cell { display: revert; }
  .rate-table th, .rate-table td, .grid-table th, .grid-table td { border: 1px solid #999; }
  .grid-table td.cell-good, .grid-table td.cell-bad, .grid-table td.cell-provisional, .grid-table td.cell-empty {
    background: #fff; color: #000;
  }
  .month-head { border-inline-start: none; }
}

/* ------------------------------------------------- v2.3 §16: bucket_mix */
/* One number box per bucket, laid out in a single wrapping row with the live score. */
.bucket-row { display: flex; flex-wrap: wrap; gap: .35rem; align-items: flex-end; }
.bucket-cell { display: flex; flex-direction: column; gap: .1rem; min-width: 72px; max-width: 140px; margin: 0; }
.bucket-cell .bucket-lab {
  font-size: .72rem; line-height: 1.15; color: var(--ink-soft);
  display: block; overflow: hidden; text-overflow: ellipsis;
}
.bucket-cell .bucket-lab em { font-style: normal; font-weight: 700; color: var(--brand); }
.bucket-cell input { width: 100%; text-align: right; font-variant-numeric: tabular-nums; }
.bucket-total {
  align-self: flex-end; padding: .2rem .45rem; border-radius: 6px;
  background: var(--brand-soft); color: var(--brand); font-size: .82rem; white-space: nowrap;
}

@media (max-width: 720px) {
  .bucket-cell { min-width: 64px; max-width: none; flex: 1 1 64px; }
  .bucket-total { flex: 1 1 100%; text-align: right; }
}

@media print {
  .bucket-total { background: #fff; color: #000; border: 1px solid #999; }
  .bucket-cell input { border: none; padding: 0; }
}

/* --------------------------------------------- v2.3 §8.4: password reveal */
.pw-reveal {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 1.6rem; font-weight: 700; letter-spacing: .06em; text-align: center;
  padding: .7rem .5rem; margin: .5rem 0; border-radius: 10px;
  background: var(--brand-soft); color: var(--brand); border: 1px dashed var(--brand);
  user-select: all; overflow-wrap: anywhere;
}
.pw-inline { font-size: 1rem; font-weight: 700; letter-spacing: .04em; user-select: all; }
.pick-list {
  max-height: 260px; overflow-y: auto; border: 1px solid var(--line); border-radius: 8px; padding: .4rem .5rem;
}
.pick-list .check { display: flex; gap: .4rem; align-items: center; padding: .12rem 0; }

@media (max-width: 720px) {
  .pw-reveal { font-size: 1.25rem; }
  .pick-list { max-height: 200px; }
}

/* --------------------------------------- v2.3 §8.3: per-person statistics */
/* CSS-only bars: one column per period, the company median drawn as a line. */
.hist-bars {
  display: flex; gap: .3rem; align-items: flex-end; overflow-x: auto; padding: .3rem 0 .1rem;
}
.hist-bar { flex: 0 0 46px; text-align: center; }
.hist-col {
  position: relative; height: 130px; display: flex; align-items: flex-end;
  background: #f4f6fa; border-radius: 5px 5px 0 0; overflow: hidden;
}
.hist-fill { width: 100%; background: var(--brand); border-radius: 5px 5px 0 0; }
.hist-fill.is-prov {
  background: repeating-linear-gradient(45deg, var(--warn), var(--warn) 4px, #fff5e6 4px, #fff5e6 8px);
}
.hist-median { position: absolute; left: 0; right: 0; border-top: 2px dashed var(--ink-soft); }
.hist-val { font-size: .78rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.hist-lab { font-size: .7rem; color: var(--ink-soft); }
.hist-prov { font-size: .62rem; color: var(--warn); }

/* The drill-down grid: 12 monthly columns scroll, the item name stays put. */
.hist-table { font-size: .84rem; }
.hist-table td, .hist-table th { vertical-align: top; }
.hist-sticky {
  position: sticky; inset-inline-start: 0; background: #fff; z-index: 1;
  box-shadow: 2px 0 0 var(--line);
}
.hist-table thead .hist-sticky { background: #f8fafd; z-index: 2; }
.hist-absent { font-size: .72rem; white-space: normal; }
.seg-row td { background: #fbfcfe; font-size: .86rem; }
.summary-lines { margin: 0; padding-inline-start: 1.2rem; }
.summary-lines li { margin-bottom: .3rem; }

@media (max-width: 720px) {
  .hist-bar { flex: 0 0 38px; }
  .hist-col { height: 100px; }
  .hist-table { font-size: .78rem; }
  .hist-sticky { position: static; box-shadow: none; }
}

@media print {
  .hist-fill { background: #555 !important; }
  .hist-fill.is-prov { background: #bbb !important; }
  .hist-median { border-top-color: #000; }
  .hist-sticky { position: static; box-shadow: none; }
  .hist-table td, .hist-table th { border: 1px solid #999; }
  .seg-row td { background: #fff; }
}

/* ------------------------------------ v2.3 §7.1: the segment summary table */
.seg-strip { border-inline-start: 4px solid var(--brand); }
.seg-table tfoot th { background: var(--brand-soft); color: var(--brand); position: static; }
.seg-table tr.seg-current > td { background: var(--brand-soft); }
.seg-table .help { white-space: normal; }

@media print {
  .seg-strip { border-inline-start: none; }
  .seg-table tfoot th { background: #fff; color: #000; border-top: 2px solid #000; }
  .seg-table tr.seg-current > td { background: #fff; }
  .seg-table th, .seg-table td { border: 1px solid #999; }
}

/* ============================================================================
   v2.4 §18.2 — the Notion-style KPI table
   ========================================================================== */

.ktbl-wrap { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: .6rem .7rem .8rem; margin-bottom: .9rem; }
.ktbl-head { display: flex; align-items: center; gap: .5rem; margin-bottom: .5rem; flex-wrap: wrap; }
.ktbl-hint { border-color: var(--brand); }

/* "บันทึกแล้ว" — quiet, fades in and out, never a button */
.ktbl-saved { font-size: .8rem; color: var(--good); opacity: 0; transition: opacity .25s ease; }
.ktbl-saved.on { opacity: 1; }

table.ktbl { font-size: .9rem; }
table.ktbl th { vertical-align: bottom; }
table.ktbl td { vertical-align: top; }
table.ktbl .ktbl-c-title { min-width: 220px; }
table.ktbl .ktbl-c-measure { min-width: 190px; }
table.ktbl .ktbl-c-value { min-width: 170px; }
table.ktbl .ktbl-c-score { width: 92px; }
table.ktbl .ktbl-c-weight { width: 96px; }
table.ktbl .ktbl-c-menu { width: 34px; }

/* §17 ข้อ 4 — the columns you can type in are tinted so they are impossible to miss */
table.ktbl th.ktbl-fillable { background: var(--brand-soft); color: var(--brand); }
table.ktbl td.ktbl-fillable { background: #fbfcff; }
table.ktbl input, table.ktbl select { font-size: .9rem; }
table.ktbl input.ktbl-title { width: 100%; border-color: transparent; background: transparent; font-weight: 600; }
table.ktbl input.ktbl-title:hover { border-color: var(--line); }
table.ktbl input.ktbl-title:focus { border-color: var(--brand); background: #fff; }
table.ktbl .ktbl-title-ro { font-weight: 600; }
table.ktbl select.ktbl-mode { width: 100%; }
table.ktbl .ktbl-inline { display: flex; align-items: center; gap: .25rem; margin-top: .2rem; flex-wrap: wrap; }
table.ktbl tr.ktbl-new td { background: #f8fafd; }
table.ktbl tfoot td { background: #f8fafd; border-top: 2px solid var(--line-strong); position: static; }
table.ktbl tfoot tr.ktbl-total-bad td { background: var(--bad-soft); color: #7a1a12; }
table.ktbl tfoot tr.ktbl-total-ok td { background: var(--good-soft); color: var(--good); }
.ktbl-foot { display: flex; align-items: center; gap: .6rem; margin-top: .6rem; flex-wrap: wrap; }

/* the row menu (ลบ / ย้ายขึ้น-ลง / ย้ายหมวด / ทำสำเนา / ตั้งค่าละเอียด) */
.ktbl-c-menu { position: relative; }
.ktbl-menu {
  position: absolute; inset-inline-end: 0; top: 100%; z-index: 40; min-width: 170px;
  background: #fff; border: 1px solid var(--line-strong); border-radius: 8px; box-shadow: var(--shadow); padding: .25rem;
}
.ktbl-menu button, .ktbl-submenu button {
  display: block; width: 100%; text-align: start; border: none; background: transparent;
  padding: .35rem .5rem; border-radius: 6px; font-size: .87rem; cursor: pointer; color: var(--ink);
}
.ktbl-menu button:hover, .ktbl-submenu button:hover { background: var(--brand-soft); }
.ktbl-menu button.btn-bad { color: var(--bad); }
.ktbl-submenu { border-top: 1px solid var(--line); margin: .2rem 0; padding-inline-start: .5rem; }
.ktbl-submenu button:disabled { opacity: .45; cursor: default; }

/* the side panel that holds everything advanced */
.ktbl-panel-back { position: fixed; inset: 0; background: rgba(16, 24, 40, .35); z-index: 50; }
.ktbl-panel {
  position: fixed; inset-block: 0; inset-inline-end: 0; z-index: 51; width: min(420px, 100%);
  background: var(--panel); box-shadow: -8px 0 24px rgba(20, 32, 60, .18);
  padding: .9rem 1rem 3rem; overflow-y: auto;
}
.ktbl-panel .field { margin-bottom: .5rem; }

@media (max-width: 720px) {
  .ktbl-wrap { padding: .5rem .45rem .7rem; }
  table.ktbl { font-size: .85rem; }
  table.ktbl .ktbl-c-title { min-width: 150px; }
  table.ktbl .ktbl-c-measure { min-width: 150px; }
  table.ktbl .ktbl-c-value { min-width: 140px; }
  .ktbl-panel { width: 100%; }
}

/* ---------------- v2.4 §18.3: the item x month matrix in "โหมดกรอก" ---------- */
.mx-input {
  width: 78px; text-align: right; font-variant-numeric: tabular-nums; padding: .15rem .3rem;
}
.hist-table td.mx-fillable { background: #fbfcff; }
.hist-table td.mx-locked { background: #f3f4f7; }
.hist-table tfoot tr.mx-total td {
  background: var(--brand-soft); color: var(--brand); border-top: 2px solid var(--line-strong); position: static;
}
.hist-table tfoot tr.mx-total td.hist-sticky { position: sticky; inset-inline-start: 0; }
.mx-cell { display: flex; justify-content: flex-end; }

@media (max-width: 720px) {
  .mx-input { width: 62px; }
  .hist-table tfoot tr.mx-total td.hist-sticky { position: static; }
}

@media print {
  .hist-table tfoot tr.mx-total td { background: #fff; color: #000; border-top: 2px solid #000; }
}

/* ------------------ v2.4 §17: "เริ่มที่นี่" and the one-page guide ------------- */
.start-here { border-inline-start: 4px solid var(--brand); }
.start-grid { display: grid; gap: .5rem; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); margin-top: .4rem; }
.start-btn {
  display: flex; gap: .6rem; align-items: center; text-decoration: none; color: var(--ink);
  border: 1px solid var(--line-strong); border-radius: 10px; padding: .7rem .8rem; background: #fff;
}
.start-btn:hover { border-color: var(--brand); background: var(--brand-soft); }
.start-btn small { display: block; color: var(--ink-soft); font-size: .8rem; font-weight: 400; }
.start-ico { font-size: 1.5rem; line-height: 1; color: var(--brand); }

.help-page ol.help-steps { padding-inline-start: 1.3rem; }
.help-page ol.help-steps > li { margin-bottom: .7rem; }
.help-page ul { padding-inline-start: 1.2rem; margin: .3rem 0; }
.help-page code { background: #eef1f7; padding: 0 .25rem; border-radius: 4px; }

/* ====================================================================
   v2.5 §20.2 / §20.5 — the simple sheet: four columns, two buttons,
   thumb-sized 1–5 keys, readable at 375px, clean print.
   ==================================================================== */

.score-page, .results-page, .my-results { max-width: 1180px; }

.score-top {
  display: flex; flex-wrap: wrap; gap: .75rem 1.25rem;
  align-items: center; justify-content: space-between; margin-bottom: .75rem;
}
.month-nav { display: flex; align-items: center; gap: .5rem; }
.month-nav .month-name { font-size: 1.25rem; }
.month-nav .btn-icon { font-size: 1.25rem; min-width: 2.75rem; min-height: 2.75rem; }

/* the two big view toggles (§20.5) */
.view-toggle { display: flex; gap: .5rem; margin: .25rem 0 1rem; }
.view-toggle button {
  flex: 1 1 0; min-height: 3rem; font-size: 1.05rem; font-weight: 600;
  border: 2px solid var(--line-strong); background: #fff; border-radius: 10px; cursor: pointer;
}
.view-toggle button.on { background: var(--brand); border-color: var(--brand); color: #fff; }

/* name chips */
.chips { display: flex; flex-wrap: wrap; gap: .4rem; margin-bottom: 1rem; }
.chip {
  display: inline-flex; align-items: center; gap: .35rem;
  min-height: 2.75rem; padding: .35rem .85rem; border-radius: 999px;
  border: 1px solid var(--line-strong); background: #fff; cursor: pointer; font-size: .95rem;
}
.chip.on { background: var(--brand); border-color: var(--brand); color: #fff; }
.chip .dot { width: .6rem; height: .6rem; border-radius: 50%; background: #d97706; flex: none; }
.chip.todo { border-color: #d97706; }

.cat-head { margin: 1.25rem 0 .35rem; font-size: 1.1rem; }
.sheet-name { margin-bottom: .5rem; display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }

/* the four-column table */
.sheet-table { width: 100%; border-collapse: collapse; font-size: .95rem; }
.sheet-table th, .sheet-table td {
  border: 1px solid var(--line); padding: .3rem .5rem; text-align: start; vertical-align: middle;
}
.sheet-table th { background: #f1f5f9; font-weight: 600; white-space: nowrap; }
.sheet-table input[type="text"], .sheet-table input[type="number"] {
  width: 100%; min-height: 2.5rem; border: 1px solid transparent; background: transparent;
  padding: .25rem .35rem; font: inherit; border-radius: 6px; color: inherit;
}
.sheet-table input:focus { border-color: var(--brand); background: #fff; outline: 2px solid var(--brand-soft); }
.sheet-table .c-title { width: 22%; min-width: 9rem; }
.sheet-table .c-criteria { width: 34%; min-width: 11rem; }
.sheet-table .c-weight { width: 9%; min-width: 5.5rem; white-space: nowrap; }
/* wide enough that all five 1–5 keys sit on ONE line at 1440px (§20.6) */
.sheet-table .c-value { width: 25%; min-width: 15.5rem; }
.sheet-table .c-tools { width: 10%; white-space: nowrap; }
.sheet-table .w-unit { max-width: 3.4rem; text-align: center; }
.sheet-table .v-input { max-width: 4.5rem; text-align: center; }
.sheet-table .v-read { font-weight: 600; }
.new-row { background: #f8fafc; }
.new-row input::placeholder { color: var(--ink-soft); }

/* §20.7 — the monthly form ("ผลของฉัน") uses the very same 1–5 cell, and its table is
   `table.collapse`, so the two value styles must not be locked to .sheet-table alone.
   The cell claims its natural width so all five keys sit on ONE line (§20.6 item 3). */
.month-value { white-space: nowrap; width: 1%; }
.month-value .score-btns { flex-wrap: nowrap; }
.v-input { max-width: 4.5rem; text-align: center; }
.v-read { font-weight: 600; }
.sheet-total { margin: .6rem 0 0; font-size: 1.05rem; display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }

/* the 1–5 keys: thumb-sized (§20.2 "ปุ่มตัวเลข 1–5 กดบนมือถือได้") */
.score-btns { display: flex; gap: .3rem; flex-wrap: wrap; }
.score-btns .sbtn {
  min-width: 2.75rem; min-height: 2.75rem; font-size: 1.05rem; font-weight: 600;
  border: 1px solid var(--line-strong); background: #fff; border-radius: 8px; cursor: pointer;
  padding: 0;
}
.score-btns .sbtn.on { background: var(--brand); border-color: var(--brand); color: #fff; }

.tag-btn {
  font-size: .8rem; padding: .2rem .5rem; border-radius: 999px; white-space: nowrap;
  border: 1px dashed var(--line-strong); background: #fff; cursor: pointer; margin-inline-start: .35rem;
}
/* §20.6 item 5 — the optional way back to a formula row: quiet, never in the way */
.tag-btn.back-target { border-style: dotted; color: var(--ink-soft); display: inline-block; margin-top: .3rem; }
/* §20.6 item 3 — the one line under the table that explains the น้ำหนัก column */
.weight-hint { margin: .3rem 0 0; }
.all-check { display: inline-flex; align-items: center; gap: .3rem; font-size: .8rem; white-space: nowrap; }
.all-check input { min-height: 1.15rem; min-width: 1.15rem; }
.scope-sel { font-size: .8rem; margin-top: .25rem; max-width: 11rem; }
.ord { display: inline-flex; gap: .15rem; margin-inline-start: .35rem; }
.ord .btn-icon { min-width: 2.25rem; min-height: 2.25rem; }
.ord .del { color: #b91c1c; }

/* the footer line */
.sheet-foot { margin-top: 1.25rem; padding-top: .75rem; border-top: 2px solid var(--line-strong); }
.foot-score { display: flex; gap: .75rem; align-items: center; flex-wrap: wrap; }
.foot-score .big { font-size: 1.3rem; }
.foot-weights { margin: .35rem 0 .5rem; font-size: .9rem; }
.foot-actions { display: flex; gap: .6rem; margin-top: .75rem; flex-wrap: wrap; }
.foot-actions button { min-height: 3rem; padding: 0 1.25rem; font-size: 1.05rem; }
.foot-actions button:disabled { opacity: .45; cursor: not-allowed; }
.unlock-link { margin-inline-start: .5rem; font-size: .9rem; }
.small { font-size: .85rem; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
.hist-total td { background: #f1f5f9; }

/* 375px: every row becomes a labelled card, every control stays thumb-sized */
@media (max-width: 720px) {
  .sheet-table, .sheet-table tbody, .sheet-table tr, .sheet-table td { display: block; }
  .sheet-table thead { display: none; }
  .sheet-table tr {
    border: 1px solid var(--line-strong); border-radius: 10px; margin-bottom: .6rem; padding: .35rem;
  }
  .sheet-table td {
    border: none; border-bottom: 1px dashed var(--line); padding: .4rem .3rem;
    width: auto; min-width: 0;
  }
  .sheet-table tr td:last-child { border-bottom: none; }
  .sheet-table .c-title::before { content: 'หัวข้อ'; }
  .sheet-table .c-criteria::before { content: 'หลักที่ใช้วัด'; }
  .sheet-table .c-weight::before { content: 'น้ำหนัก'; }
  .sheet-table .c-value::before { content: 'ได้เท่าไหร่'; }
  .sheet-table td::before { display: block; font-size: .75rem; color: var(--ink-soft); }
  .sheet-table .c-tools::before { content: none; }
  .sheet-table input[type="text"], .sheet-table input[type="number"] {
    border-color: var(--line-strong); background: #fff; max-width: none;
  }
  .sheet-table .w-unit, .sheet-table .v-input { max-width: 5rem; }
  .foot-actions button { flex: 1 1 100%; }
  .month-nav .month-name { font-size: 1.1rem; }
}

@media print {
  .sheet-table { font-size: 11pt; }
  .sheet-table th, .sheet-table td { border: 1px solid #999; }
  .score-page, .results-page, .my-results { max-width: none; }
  .sheet-foot { break-inside: avoid; }
  .score-btns .sbtn { border: none; padding: 0 .15rem; }
  .score-btns .sbtn:not(.on) { display: none; }
}
