:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --line: #e6e6e1;
  --line-strong: #d3d3cc;
  --text: #1c1c1a;
  --muted: #74746e;
  --accent: #1f3a5f;
  --accent-soft: #eef2f7;
  --calc: #f4f4f1;
  --good: #2f7d4f;
  --warn: #b4531a;
  --danger: #a32d2d;
  --radius: 10px;
  --head: #fbfbfa;
  --hover: #fafaf8;
  --on-accent: #ffffff;
  --ring: rgba(31,58,95,.12);
  --font: "IBM Plex Sans Thai", "IBM Plex Sans", "Leelawadee UI", "Segoe UI", Tahoma, sans-serif;
}

@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) {
  --bg: #16181c; --surface: #1e2126; --line: #2c3037; --line-strong: #3b4048; --text: #e8e8e4; --muted: #9a9c98;
  --accent: #8fb1de; --accent-soft: #262d38; --calc: #262a30; --good: #6fc08d; --warn: #e39a63; --danger: #e07a7a;
  --head: #22262b; --hover: #252930; --on-accent: #12161c; --ring: rgba(143,177,222,.25);
  color-scheme: dark;
} }
:root[data-theme="dark"] {
  --bg: #16181c; --surface: #1e2126; --line: #2c3037; --line-strong: #3b4048; --text: #e8e8e4; --muted: #9a9c98;
  --accent: #8fb1de; --accent-soft: #262d38; --calc: #262a30; --good: #6fc08d; --warn: #e39a63; --danger: #e07a7a;
  --head: #22262b; --hover: #252930; --on-accent: #12161c; --ring: rgba(143,177,222,.25);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: var(--font); font-size: 14px; line-height: 1.5; font-weight: 400;
}
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- top bar ---------- */
.topbar { position: sticky; top: 0; z-index: 20; background: var(--surface); border-bottom: 1px solid var(--line); }
.topbar-inner {
  max-width: 1240px; margin: 0 auto; padding: 12px 24px;
  display: flex; align-items: center; gap: 24px; flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 12px; margin-right: auto; }
.brand-mark { width: 10px; height: 34px; background: var(--accent); border-radius: 3px; }
.brand-title { font-weight: 600; font-size: 16px; letter-spacing: .1px; }
.brand-sub { font-size: 12px; color: var(--muted); letter-spacing: .6px; text-transform: uppercase; }

.period { display: flex; align-items: center; gap: 6px; }
.period select {
  border: 1px solid var(--line-strong); background: var(--surface); border-radius: 8px;
  padding: 7px 10px; min-width: 96px; cursor: pointer;
}
.icon-btn {
  width: 34px; height: 34px; border: 1px solid var(--line-strong); background: var(--surface);
  border-radius: 8px; font-size: 18px; line-height: 1; cursor: pointer;
}
.icon-btn:hover { background: var(--accent-soft); }

.actions { display: flex; align-items: center; gap: 10px; }
.saved { font-size: 12px; color: var(--muted); }
.saved.flash { color: var(--good); }
.btn {
  border: 1px solid var(--accent); background: var(--accent); color: var(--on-accent);
  padding: 8px 14px; border-radius: 8px; cursor: pointer; font-weight: 500;
}
.btn:hover { filter: brightness(1.12); }
.btn.ghost { background: var(--surface); color: var(--text); border-color: var(--line-strong); padding: 8px 12px; }
.btn.ghost:hover { background: var(--accent-soft); filter: none; }
.menu { position: relative; }
.menu-list {
  position: absolute; top: calc(100% + 6px); left: 0; min-width: 220px; z-index: 30;
  background: var(--surface); border: 1px solid var(--line-strong); border-radius: 10px;
  padding: 6px; box-shadow: 0 8px 24px rgba(0,0,0,.08);
  display: flex; flex-direction: column;
}
.menu-list.right { left: auto; right: 0; }
.menu-list button {
  text-align: left; border: 0; background: none; padding: 9px 12px; border-radius: 6px; cursor: pointer;
}
.menu-list button:hover { background: var(--accent-soft); }
.menu-list .strong { font-weight: 600; color: var(--accent); }
.menu-list .danger { color: var(--danger); }

.tabs { max-width: 1240px; margin: 0 auto; padding: 0 24px; display: flex; gap: 4px; }
.tab {
  border: 0; background: none; padding: 10px 16px; cursor: pointer; color: var(--muted);
  border-bottom: 2px solid transparent; font-weight: 500;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

/* ---------- page ---------- */
.page { max-width: 1240px; margin: 0 auto; padding: 24px; }
.page-head { display: flex; align-items: baseline; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head h1 { margin: 0; font-size: 22px; font-weight: 600; }
.page-head p { margin: 0; color: var(--muted); }
.field-inline { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 20px 22px; margin-bottom: 18px;
}
.card-head { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.card-head h2 { margin: 0; font-size: 15px; font-weight: 600; }
.card-head .no {
  width: 24px; height: 24px; border-radius: 50%; background: var(--accent-soft); color: var(--accent);
  display: inline-flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 600;
}
.card-head .spacer { margin-left: auto; }
.hint { color: var(--muted); font-size: 12.5px; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }
@media (max-width: 900px) { .grid-2 { grid-template-columns: 1fr; } }

/* ---------- tables ---------- */
.tbl-wrap { overflow-x: auto; }
table.tbl { width: 100%; border-collapse: collapse; }
.tbl th, .tbl td { border-bottom: 1px solid var(--line); padding: 0; }
.tbl thead th {
  font-weight: 500; font-size: 12.5px; color: var(--muted); padding: 8px 10px; text-align: center;
  white-space: nowrap; background: var(--head); border-bottom: 1px solid var(--line-strong);
}
.tbl thead tr.grp th { border-bottom: 1px solid var(--line); color: var(--text); font-weight: 600; font-size: 12px; letter-spacing: .3px; }
.tbl thead th.left, .tbl td.left { text-align: left; }
.tbl th.sep, .tbl td.sep { border-left: 1px solid var(--line-strong); }
.tbl tbody th { text-align: left; font-weight: 500; padding: 0 10px; white-space: nowrap; background: var(--head); }
.tbl td.calc, .tbl th.calc {
  background: var(--calc); text-align: right; padding: 8px 10px; font-variant-numeric: tabular-nums; font-weight: 500;
}
.tbl tr.total td, .tbl tr.total th { background: var(--accent-soft); font-weight: 600; border-top: 1px solid var(--line-strong); }
.tbl tr.total td.calc { background: var(--accent-soft); }
.tbl tbody tr:last-child td, .tbl tbody tr:last-child th { border-bottom: 0; }

.tbl input {
  width: 100%; min-width: 74px; border: 0; background: transparent; padding: 9px 10px; text-align: right;
  font-variant-numeric: tabular-nums; outline: none; border-radius: 0;
}
.tbl input[type=text].txt { text-align: left; min-width: 110px; }
.tbl input:hover { background: var(--hover); }
.tbl input:focus { background: var(--accent-soft); box-shadow: inset 0 -2px 0 var(--accent); }
input[type=number]::-webkit-outer-spin-button, input[type=number]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
input[type=number] { -moz-appearance: textfield; }

.pct-good { color: var(--good); }
.pct-warn { color: var(--warn); }

.x-btn { border: 0; background: none; color: var(--muted); cursor: pointer; padding: 6px 10px; font-size: 16px; }
.x-btn:hover { color: var(--danger); }
.add-btn {
  margin-top: 10px; border: 1px dashed var(--line-strong); background: none; padding: 7px 14px;
  border-radius: 8px; cursor: pointer; color: var(--muted);
}
.add-btn:hover { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }

/* ---------- forms ---------- */
label.lbl { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 4px; }
.input, textarea.input, select.input {
  width: 100%; border: 1px solid var(--line-strong); border-radius: 8px; padding: 8px 10px;
  background: var(--surface); outline: none;
}
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(31,58,95,.12); }
textarea.input { resize: vertical; min-height: 64px; line-height: 1.55; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 14px; }
.form-grid .calc-box {
  border: 1px solid var(--line); background: var(--calc); border-radius: 8px; padding: 8px 10px;
  font-weight: 600; font-variant-numeric: tabular-nums; min-height: 38px;
}
.note-block { margin-top: 14px; }

/* market cards */
.zone { border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; background: var(--surface); }
.zone-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.zone-head .input { max-width: 260px; font-weight: 600; }
.zone-cols { display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: 14px; }
@media (max-width: 900px) { .zone-cols { grid-template-columns: 1fr; } }
.zone textarea.input { min-height: 120px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; transition: .25s; z-index: 50;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 720px) {
  .topbar-inner, .page, .tabs { padding-left: 14px; padding-right: 14px; }
  .card { padding: 16px; }
}

/* ---------- fixed table widths ---------- */
.tbl { table-layout: fixed; min-width: 560px; }
.tbl thead tr:first-child th:first-child { width: 110px; }
.tbl thead th.del { width: 44px; }
.tbl input { min-width: 0; }
.tbl td.calc { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tbl.wide { min-width: 1020px; }

/* ---------- confirm dialog ---------- */
.modal-ov { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: flex; align-items: center; justify-content: center; z-index: 60; padding: 16px; }
.modal { background: var(--surface); color: var(--text); border: 1px solid var(--line-strong); border-radius: var(--radius); padding: 20px; max-width: 420px; width: 100%; }
.modal p { margin: 0 0 16px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }
.topbar { top: env(safe-area-inset-top, 0px); }

/* ---------- sync UI ---------- */
.saved.ok { color: var(--good); }
.saved.bad { color: var(--danger); }
.peers { font-size: 12.5px; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.peers .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--good); flex: none; }
.login-ov { position: fixed; inset: 0; z-index: 80; background: var(--bg); display: flex; align-items: center; justify-content: center; padding: 16px; }
.login { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 28px; display: flex; flex-direction: column; gap: 6px; }
.login .brand { margin-bottom: 18px; }
.login .btn { margin-top: 14px; padding: 10px; }
.login-err { color: var(--danger); min-height: 20px; font-size: 13px; margin-top: 4px; }

/* ============================================================
   UI revision: channel colours, key products, uniform cells, no scrollbars
   ============================================================ */
:root {
  --c-mt: #2f6db5; --c-ka: #23856a; --c-on: #b96f12; --c-ex: #b83a5a;
  --row-h: 40px;
}
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) {
  --c-mt: #7fb0ea; --c-ka: #6fc3a0; --c-on: #e3a55a; --c-ex: #ec8fa9;
} }
:root[data-theme="dark"] { --c-mt: #7fb0ea; --c-ka: #6fc3a0; --c-on: #e3a55a; --c-ex: #ec8fa9; }

.ch-mt { --ch: var(--c-mt); }
.ch-ka { --ch: var(--c-ka); }
.ch-on { --ch: var(--c-on); }
.ch-ex { --ch: var(--c-ex); }
.tbl td.ch-mt, .tbl td.ch-ka, .tbl td.ch-on, .tbl td.ch-ex { background: color-mix(in srgb, var(--ch) 8%, var(--surface)); }
.tbl th.chh { background: color-mix(in srgb, var(--ch) 16%, var(--surface)); color: var(--ch); font-weight: 600; box-shadow: inset 0 -2px 0 var(--ch); }
.tbl th.chl { color: var(--ch); font-weight: 600; box-shadow: inset 3px 0 0 var(--ch); background: color-mix(in srgb, var(--ch) 10%, var(--surface)); }
.tbl td.ch-mt:focus-within, .tbl td.ch-ka:focus-within, .tbl td.ch-on:focus-within, .tbl td.ch-ex:focus-within { background: color-mix(in srgb, var(--ch) 18%, var(--surface)); }
.tbl td.ch-mt input:focus, .tbl td.ch-ka input:focus, .tbl td.ch-on input:focus, .tbl td.ch-ex input:focus { background: transparent; box-shadow: inset 0 -2px 0 var(--ch); }

/* สินค้าหลัก 205S / 200 */
.tbl tr.key th, .tbl tr.key input, .tbl tr.key td.calc, .tbl td.key input, .tbl td.key.calc, .tbl th.key { font-weight: 700; }
.tbl tr.key > th:first-child, .tbl th.key { color: var(--accent); }

/* ช่องกรอกและช่องแสดงผลขนาดเท่ากัน */
.tbl { table-layout: fixed; width: 100%; min-width: 0; }
.tbl.wide { min-width: 0; }
.tbl th, .tbl td { height: var(--row-h); }
.tbl input { height: var(--row-h); padding: 0 10px; font-size: 14px; line-height: var(--row-h); min-width: 0; }
.tbl input[type=text].txt { min-width: 0; }
.tbl td.calc { padding: 0 10px; height: var(--row-h); line-height: var(--row-h); font-size: 14px; }
.tbl thead th { height: auto; padding: 8px 6px; white-space: normal; line-height: 1.3; }
.tbl tbody th { padding: 0 10px; }
.tbl-wrap { overflow: visible; }
.input, .calc-box { height: var(--row-h); }
.calc-box { display: flex; align-items: center; justify-content: flex-end; padding: 0 10px; font-size: 14px; }
textarea.input { height: auto; }
.grid-tbl { max-width: 820px; }

/* การ์ดที่ซ่อนได้ */
.card.collapsed .card-head { margin-bottom: 0; }
.toggle-btn { padding: 6px 14px; min-width: 84px; }

/* การ์ดโรงเรียน: ช่องเรียงแถวเดียว */
.form-row { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 14px; align-items: end; }
.form-row .lbl { min-height: 2.8em; display: flex; align-items: flex-end; line-height: 1.35; }
.form-row .input { text-align: right; }
@media (max-width: 1000px) { .form-row { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
@media (max-width: 560px)  { .form-row { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
@media (max-width: 820px) {
  .tbl input, .tbl td.calc { padding: 0 4px; font-size: 12.5px; }
  .tbl thead th { font-size: 11.5px; padding: 6px 2px; }
  .tbl tbody th { padding: 0 6px; }
}
.calc-box { border: 1px solid var(--line); background: var(--calc); border-radius: 8px; font-weight: 600; font-variant-numeric: tabular-nums; }

/* ---------- สภาพตลาด: ตารางตามช่องทางการขาย ---------- */
.mkt-tbl thead th { padding: 10px 12px; }
.mkt-tbl thead th:first-child { width: 110px; }
.mkt-tbl tbody th { vertical-align: top; padding-top: 14px; }
.mkt-tbl td { vertical-align: top; border-left: 1px solid var(--line); }
.mkt-tbl textarea {
  display: block; width: 100%; min-height: 150px; padding: 12px; border: 0; outline: none;
  background: transparent; resize: vertical; line-height: 1.55; border-radius: 0;
}
.mkt-tbl textarea:focus { box-shadow: inset 0 -2px 0 var(--ch); }

/* แต่ละช่องกรอกแบ่ง 2 ส่วน คั่นด้วยเส้นบางสีของช่องทาง */
.mkt-tbl textarea { min-height: 76px; }
.mkt-tbl textarea + textarea { border-top: 1px solid color-mix(in srgb, var(--ch) 55%, var(--surface)); }

/* ---------- ติดตามโควตา / ประเมินปิดเดือน ---------- */
.chip { margin-left: auto; font-size: 12px; color: var(--muted); border: 1px solid var(--line-strong); border-radius: 999px; padding: 2px 10px; }
.qt-stack { display: flex; flex-direction: column; gap: 22px; }
.qt-h { margin: 0 0 8px; font-size: 14px; font-weight: 600; }
.qt-tbl { table-layout: fixed; }
.qt-tbl thead th { font-size: 12px; padding: 6px 2px; }
.qt-tbl thead tr.grp th { font-size: 12.5px; }
.qt-tbl input, .qt-tbl td.calc { font-size: 13px; padding: 0 6px; }
.qt-tbl thead th:first-child, .qt-tbl tbody th { width: 96px; }
.qt-tbl tbody th { font-size: 13px; }

/* ============================================================
   UI revision: compact tables, stronger card headings
   ============================================================ */
:root { --row-h: 32px; }
.tbl input, .tbl td.calc { font-size: 13px; }
.tbl tbody th { font-size: 13px; padding: 0 8px; }
.tbl thead th { font-size: 12px; padding: 6px 4px; }
.tbl thead tr.grp th { font-size: 12px; }
.input, .calc-box { height: 34px; font-size: 13px; }
textarea.input { font-size: 13px; }
.qt-tbl input, .qt-tbl td.calc { font-size: 12.5px; }
.qt-tbl thead th, .qt-tbl tbody th { font-size: 12px; }
.mkt-tbl textarea { min-height: 60px; font-size: 13px; padding: 8px 10px; }
.mkt-tbl thead th { padding: 8px 10px; }
.mkt-tbl tbody th { padding-top: 10px; }
.tbl td.calc { line-height: var(--row-h); }
.tbl input { line-height: var(--row-h); }
.form-row .lbl { min-height: 2.6em; font-size: 12px; }

/* หัวข้อการ์ดเด่นชัดขึ้น */
.card-head { padding-bottom: 12px; margin-bottom: 16px; border-bottom: 1px solid var(--line); gap: 12px; }
.card-head h2 { font-size: 18px; font-weight: 700; letter-spacing: .1px; color: var(--text); line-height: 1.3; }
.card-head .no { width: 30px; height: 30px; font-size: 14px; font-weight: 700; background: var(--accent); color: var(--on-accent); }
.card-head h2:first-child::before {
  content: ""; display: inline-block; width: 5px; height: 20px; margin-right: 10px;
  background: var(--accent); border-radius: 2px; vertical-align: -4px;
}
.card.collapsed .card-head { border-bottom: 0; padding-bottom: 0; margin-bottom: 0; }
.qt-h { font-size: 15px; font-weight: 700; color: var(--accent); }
.page-head h1 { font-size: 24px; }

/* ช่องทาง A2 (หน้าสภาพตลาด) */
:root { --c-a2: #0891b2; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) { --c-a2: #67d0e8; } }
:root[data-theme="dark"] { --c-a2: #67d0e8; }
.ch-a2 { --ch: var(--c-a2); }
.tbl td.ch-a2 { background: color-mix(in srgb, var(--ch) 8%, var(--surface)); }
.tbl th.chl.ch-a2 { color: var(--ch); font-weight: 600; box-shadow: inset 3px 0 0 var(--ch); background: color-mix(in srgb, var(--ch) 10%, var(--surface)); }
.tbl td.ch-a2:focus-within { background: color-mix(in srgb, var(--ch) 18%, var(--surface)); }
.menu-list .logout { border-top: 1px solid var(--line); margin-top: 4px; font-weight: 500; }
