/* ============================================================
   Dashboard ข้อมูลสารสนเทศด้านการศึกษา จังหวัดนครสวรรค์
   ============================================================ */

:root {
  /* พื้นผิว */
  --bg: #eef1f6;
  --surface: #ffffff;
  --surface-2: #f7f9fc;
  --surface-3: #eef2f8;
  --border: #ccd5e3;
  --border-strong: #a9b8cc;

  /* ตัวอักษร — คุมให้ทุกระดับผ่านเกณฑ์ความชัด WCAG AA (อัตราส่วนอย่างน้อย 4.5:1)
     ระดับจางที่สุด --text-3 อยู่ที่ราว 5.4:1 บนพื้นขาว จึงยังอ่านออกชัดแม้ตัวเล็ก */
  --text: #0b1222;
  --text-2: #2b3950;
  --text-3: #45536b;

  /* อัตลักษณ์จังหวัด — น้ำเงินคราม (ต้นแม่น้ำเจ้าพระยา) + ทอง (มังกร) */
  --brand: #14486e;
  --brand-2: #1b6a99;
  --brand-3: #2e8bc0;
  --accent: #d99522;
  --accent-2: #f0b849;

  /* จานสีชุดข้อมูล */
  --c1: #1b6a99;
  --c2: #d99522;
  --c3: #2f9e8f;
  --c4: #a8557d;
  --c5: #5b6bb5;
  --c6: #c96a4b;
  --c7: #4f9d5d;
  --c8: #8a7bbf;
  --c9: #2e8bc0;
  --c10: #b8863b;
  --c11: #57808f;
  --c12: #9a6b8f;

  --ok: #2f8f5b;
  --warn: #b8791b;
  --danger: #c04a3d;

  --radius: 10px;
  --radius-sm: 6px;

  /* แบบอักษร: Bai Jamjuree สำหรับหัวเรื่องและตัวเลข · Sarabun สำหรับเนื้อความ */
  --font-body: 'Sarabun', 'Noto Sans Thai', 'Leelawadee UI', Tahoma, sans-serif;
  --font-display: 'Bai Jamjuree', 'Sarabun', 'Leelawadee UI', Tahoma, sans-serif;
  --shadow: 0 1px 2px rgba(20, 33, 61, .06), 0 4px 16px rgba(20, 33, 61, .06);
  --shadow-lg: 0 2px 4px rgba(20, 33, 61, .08), 0 12px 32px rgba(20, 33, 61, .10);
  --sidebar-w: 264px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1521;
    --surface: #161f2e;
    --surface-2: #1c2637;
    --surface-3: #223047;
    --border: #33425c;
    --border-strong: #47597b;
    --text: #f3f7fc;
    --text-2: #ccd8ea;
    --text-3: #adbbd0;
    --brand: #7cb8dd;
    --brand-2: #5aa3d0;
    --brand-3: #3d8fc0;
    --accent: #f0b849;
    --accent-2: #ffd27a;
    --c1: #5aa3d0; --c2: #f0b849; --c3: #4fc0ae; --c4: #d17ba6;
    --c5: #8896d8; --c6: #e89172; --c7: #6fc47f; --c8: #ad9de0;
    --c9: #63b3e0; --c10: #d8a95e; --c11: #7fa8b8; --c12: #c091b3;
    --ok: #5cbd85; --warn: #e0a744; --danger: #e0736a;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    --shadow-lg: 0 2px 4px rgba(0, 0, 0, .35), 0 12px 32px rgba(0, 0, 0, .35);
  }
}

:root[data-theme="dark"] {
  --bg: #0e1521;
  --surface: #161f2e;
  --surface-2: #1c2637;
  --surface-3: #223047;
  --border: #33425c;
  --border-strong: #47597b;
  --text: #f3f7fc;
  --text-2: #ccd8ea;
  --text-3: #adbbd0;
  --brand: #7cb8dd;
  --brand-2: #5aa3d0;
  --brand-3: #3d8fc0;
  --accent: #f0b849;
  --accent-2: #ffd27a;
  --c1: #5aa3d0; --c2: #f0b849; --c3: #4fc0ae; --c4: #d17ba6;
  --c5: #8896d8; --c6: #e89172; --c7: #6fc47f; --c8: #ad9de0;
  --c9: #63b3e0; --c10: #d8a95e; --c11: #7fa8b8; --c12: #c091b3;
  --ok: #5cbd85; --warn: #e0a744; --danger: #e0736a;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
  --shadow-lg: 0 2px 4px rgba(0, 0, 0, .35), 0 12px 32px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 15.5px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, .brand-text b, .k-value, .ch-center-num, .toc .t-no, .nav a .num {
  font-family: var(--font-display);
}
h2, h3 { text-wrap: balance; }

/* ============ โครงหน้า ============ */
.layout { display: flex; min-height: 100vh; }

/* ---- แถบเมนูข้าง ---- */
.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.brand {
  padding: 20px 14px 16px;
  border-bottom: 1px solid var(--border);
  background: linear-gradient(180deg, color-mix(in srgb, var(--brand-2) 8%, transparent), transparent);
}
/* วางตราไว้เหนือข้อความ เพื่อให้ชื่อเต็มได้ความกว้างทั้งแถบ
   ภาษาไทยไม่มีช่องว่างระหว่างคำ ถ้าบีบให้แคบเบราว์เซอร์จะตัดคำกลางคำ */
.brand-mark {
  display: flex; flex-direction: column; align-items: center;
  text-align: center; gap: 10px;
}
/* ตราสัญลักษณ์เป็นวงกลมบนพื้นขาว จึงครอบเป็นวงกลมและวางบนพื้นขาวเสมอ
   เพื่อให้อ่านออกทั้งธีมสว่างและธีมมืด */
.brand-logo {
  width: 78px; height: 78px; flex: none;
  border-radius: 50%;
  background: #fff;
  object-fit: contain;
  box-shadow: 0 2px 10px rgba(20, 33, 61, .22);
  outline: 3px solid color-mix(in srgb, var(--brand-2) 22%, transparent);
  outline-offset: 2px;
}
.brand-text b {
  display: block; font-size: 17px; font-weight: 700; line-height: 1.25;
  letter-spacing: -.01em; color: var(--brand);
}
.brand-text span {
  display: block; font-size: 14.5px; font-weight: 600; color: var(--text-2); margin-top: 2px;
}
.brand-text em {
  display: block; font-style: normal; font-size: 12.5px; line-height: 1.45;
  color: var(--text-3); margin-top: 7px;
  padding-top: 7px; border-top: 1px solid var(--border);
}

.nav { padding: 10px 10px 16px; flex: 1; }
.nav-section {
  font-size: 11.5px; font-weight: 600; letter-spacing: .06em;
  color: var(--text-3); text-transform: uppercase;
  padding: 12px 10px 5px;
}
.nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 10px; border-radius: var(--radius-sm);
  color: var(--text-2); text-decoration: none;
  font-size: 14.5px; line-height: 1.35;
  transition: background .12s, color .12s;
}
.nav a:hover { background: var(--surface-3); color: var(--text); }
.nav a.active {
  background: color-mix(in srgb, var(--brand-2) 14%, transparent);
  color: var(--brand); font-weight: 600;
}
.nav a .num {
  flex: 0 0 24px; height: 20px; border-radius: 5px;
  background: var(--surface-3); color: var(--text-3);
  font-size: 11.5px; font-weight: 600;
  display: grid; place-items: center;
}
.nav a.active .num { background: var(--brand-2); color: #fff; }
.nav a .dot { width: 6px; height: 6px; border-radius: 50%; background: var(--warn); margin-left: auto; flex: none; }

.sidebar-foot {
  padding: 12px 16px 16px; border-top: 1px solid var(--border);
  font-size: 12px; color: var(--text-3); line-height: 1.5;
}

/* ---- เนื้อหาหลัก ---- */
.main { flex: 1; min-width: 0; }

.topbar {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  padding: 9px 22px;
  display: flex; align-items: center; gap: 14px;
}
.topbar h1 { font-size: 16.5px; margin: 0; font-weight: 600; letter-spacing: -.01em; }
.topbar .crumb { font-size: 12.5px; color: var(--text-3); }
.topbar .spacer { flex: 1; }
.chip {
  font-size: 12.5px; padding: 3px 10px; border-radius: 999px;
  background: var(--surface-3); color: var(--text-2); white-space: nowrap;
}
.chip.brand { background: color-mix(in srgb, var(--brand-2) 15%, transparent); color: var(--brand); font-weight: 600; }

.icon-btn {
  width: 31px; height: 31px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text-2); cursor: pointer;
  display: grid; place-items: center; font-size: 14px;
  transition: background .12s, color .12s;
}
.icon-btn:hover { background: var(--surface-3); color: var(--text); }

.menu-btn { display: none; }

.content { padding: 20px 22px 56px; max-width: 1280px; }

/* ============ ส่วนหัวหน้า ============ */
.page-head { margin-bottom: 18px; }
.page-head .eyebrow {
  font-size: 12px; font-weight: 600; letter-spacing: .05em;
  color: var(--brand-2); text-transform: uppercase; margin-bottom: 5px;
}
.page-head h2 { font-size: 24px; margin: 0 0 6px; font-weight: 700; letter-spacing: -.02em; line-height: 1.3; }
.page-head p { margin: 0; color: var(--text-2); font-size: 14.5px; max-width: 76ch; }

/* ============ การ์ด ============ */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 18px;
  margin-bottom: 14px;
}
.card > h3 {
  margin: 0 0 4px; font-size: 16.5px; font-weight: 650; letter-spacing: -.01em;
}
.card > .sub { margin: 0 0 13px; font-size: 13.8px; color: var(--text-3); }

.grid { display: grid; gap: 13px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.g-2-1 { grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); }
.g-1-2 { grid-template-columns: minmax(0, 1fr) minmax(0, 2fr); }

/* ============ การ์ดตัวเลขสำคัญ ============ */
.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px 16px;
  position: relative; overflow: hidden;
}
/* คีย์สีแบบเดียวกับคำอธิบายสัญลักษณ์ในกราฟ — เชื่อมการ์ดกับชุดข้อมูล */
.kpi .k-label {
  font-size: 13px; color: var(--text-2); font-weight: 500; margin-bottom: 3px;
  display: flex; align-items: center; gap: 7px;
}
.kpi .k-label::before {
  content: ''; width: 9px; height: 9px; flex: none; border-radius: 2px;
  background: var(--accent-color, var(--c1));
}
.kpi .k-value {
  font-size: 27px; font-weight: 700; letter-spacing: -.02em; line-height: 1.15;
  font-variant-numeric: tabular-nums;
}
.kpi .k-unit { font-size: 13.5px; font-weight: 500; color: var(--text-3); margin-left: 4px; }
.kpi .k-note { font-size: 12.8px; color: var(--text-3); margin-top: 5px; }
/* การ์ดที่แสดงแยกเป็นรายประเภท แทนตัวเลขรวมเพียงค่าเดียว */
.kpi .k-parts { display: flex; gap: 18px; flex-wrap: wrap; margin-top: 2px; }
.kpi .k-part { min-width: 0; }
.kpi .k-part .kp-name { display: block; font-size: 12px; color: var(--text-3); margin-bottom: 1px; }
.kpi .k-part b {
  display: block; font-family: var(--font-display);
  font-size: 21px; font-weight: 700; letter-spacing: -.02em; line-height: 1.2;
  font-variant-numeric: tabular-nums;
}
/* บรรทัดอ้างอิงตารางต้นทาง + ช่วงเวลาของข้อมูลใบนั้น */
.kpi .k-src {
  font-size: 12px; color: var(--text-3); margin-top: 7px;
  padding-top: 7px; border-top: 1px solid var(--border);
}

/* ============ ตาราง ============ */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
}
table { border-collapse: collapse; width: 100%; font-size: 14.5px; }
thead th {
  background: var(--surface-3);
  color: var(--text-2);
  font-weight: 600; font-size: 13.5px;
  text-align: right; padding: 8px 10px;
  border-bottom: 1px solid var(--border-strong);
  white-space: nowrap; position: sticky; top: 0;
}
thead th.lbl { text-align: left; min-width: 240px; white-space: normal; }
tbody td {
  padding: 6px 10px; border-bottom: 1px solid var(--border);
  text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap;
}
tbody td.lbl { text-align: left; white-space: normal; min-width: 240px; }
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: var(--surface-2); }

tr.section td { background: var(--surface-3); font-weight: 650; color: var(--brand); }
tr.section:hover td { background: var(--surface-3); }
tr.group td.lbl { font-weight: 600; }
tr.sub td.lbl { padding-left: 30px; color: var(--text-2); }
tr.total td {
  background: color-mix(in srgb, var(--brand-2) 10%, transparent);
  font-weight: 700; border-top: 2px solid var(--border-strong); border-bottom: none;
}
tr.total:hover td { background: color-mix(in srgb, var(--brand-2) 13%, transparent); }
td.muted, .muted { color: var(--text-3); }
td.num-strong { font-weight: 600; }

/* ============ กราฟ ============ */
/* จำกัดความกว้างที่แสดงจริง ไม่งั้นการ์ดเต็มความกว้างจะขยายกราฟ (และตัวอักษรในกราฟ) ใหญ่เกินไป */
.chart { width: 100%; max-width: 920px; height: auto; display: block; margin: 0 auto; overflow: visible; }
.chart.donut, .chart.ring { max-width: 260px; margin: 0 auto; }
.ch-label { font-size: 13.5px; fill: var(--text-2); }
.ch-value { font-size: 13.5px; fill: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
.ch-value.sm { font-size: 12px; }
.ch-axis { font-size: 12px; fill: var(--text-3); }
.ch-track { fill: var(--surface-3); }
.ch-track-stroke { stroke: var(--surface-3); }
.ch-grid { stroke: var(--border); stroke-width: 1; stroke-dasharray: 3 3; }
.ch-bar, .ch-slice { transition: opacity .12s; }
.ch-bar:hover, .ch-slice:hover { opacity: .78; }
.ch-center-num { font-size: 22px; font-weight: 700; fill: var(--text); }
.ch-center-sub { font-size: 12.5px; fill: var(--text-3); }

.legend { list-style: none; margin: 14px 0 0; padding: 0; }
.legend li {
  display: flex; align-items: center; gap: 9px;
  padding: 4px 0; font-size: 13.8px; border-bottom: 1px solid var(--border);
}
.legend li:last-child { border-bottom: none; }
.legend .sw { width: 11px; height: 11px; border-radius: 3px; flex: none; }
.legend .lg-name { flex: 1; min-width: 0; color: var(--text-2); }
.legend .lg-val { font-weight: 600; font-variant-numeric: tabular-nums; }
.legend .lg-pct { color: var(--text-3); font-size: 12.5px; width: 48px; text-align: left; font-variant-numeric: tabular-nums; }
.legend.compact li { padding: 3px 0; font-size: 13.8px; border: none; }

.propbar {
  display: flex; height: 30px; border-radius: 7px; overflow: hidden;
  background: var(--surface-3); margin: 6px 0 2px;
}
.pb-seg {
  display: grid; place-items: center; color: #fff;
  font-size: 11.5px; font-weight: 600; min-width: 0;
  transition: opacity .12s;
  /* เงาตัวอักษรช่วยให้อ่านตัวเลขได้ทั้งบนแถบสีเข้มและสีอ่อน */
  text-shadow: 0 1px 2px rgba(0, 0, 0, .55);
}
.pb-seg:hover { opacity: .85; }

/* ============ กล่องข้อความแจ้งเตือน / หมายเหตุ ============ */
.notice {
  display: flex; gap: 11px; align-items: flex-start;
  padding: 12px 15px; border-radius: var(--radius-sm);
  font-size: 13.8px; line-height: 1.55; margin-bottom: 13px;
}
.notice .ni { flex: none; font-size: 15px; line-height: 1.4; }
.notice.warn {
  background: color-mix(in srgb, var(--warn) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--warn) 32%, transparent);
  color: color-mix(in srgb, var(--warn) 78%, var(--text));
}
.notice.info {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text-2);
}
.notice.empty {
  background: var(--surface-2); border: 1px dashed var(--border-strong);
  color: var(--text-2); justify-content: center; padding: 26px; text-align: center;
  flex-direction: column; align-items: center; gap: 6px;
}
.notice.empty b { color: var(--text); font-size: 15px; }

.source {
  font-size: 13px; color: var(--text-3); margin-top: 12px;
  padding-top: 12px; border-top: 1px solid var(--border);
}
.source b { color: var(--text-2); font-weight: 600; }

/* ============ ป้ายสถานะ ============ */
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 999px;
}
.badge.ok { background: color-mix(in srgb, var(--ok) 15%, transparent); color: var(--ok); }
.badge.warn { background: color-mix(in srgb, var(--warn) 16%, transparent); color: var(--warn); }
.badge.empty { background: var(--surface-3); color: var(--text-3); }

/* ============ ตารางสารบัญ ============ */
.toc { display: grid; gap: 10px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.toc a {
  display: flex; gap: 12px; align-items: flex-start;
  padding: 11px 13px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface-2);
  text-decoration: none; color: var(--text);
  transition: border-color .12s, transform .12s, box-shadow .12s;
}
.toc a:hover { border-color: var(--brand-3); transform: translateY(-1px); box-shadow: var(--shadow); }
.toc .t-no {
  flex: 0 0 31px; height: 31px; border-radius: 7px;
  background: var(--surface-3); color: var(--brand);
  font-weight: 700; font-size: 13px; display: grid; place-items: center;
}
.toc .t-body { min-width: 0; }
.toc .t-title { font-size: 14.5px; font-weight: 500; line-height: 1.4; }
.toc .t-meta { font-size: 12px; color: var(--text-3); margin-top: 3px; }

/* ============ หน้าจอเล็ก ============ */
@media (max-width: 1100px) {
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .g3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .g-2-1, .g-1-2 { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 860px) {
  .menu-btn { display: grid; }
  .sidebar {
    position: fixed; z-index: 60; left: 0; top: 0;
    transform: translateX(-100%); transition: transform .22s ease;
    box-shadow: var(--shadow-lg);
  }
  .sidebar.open { transform: none; }
  .scrim {
    display: none; position: fixed; inset: 0; z-index: 50;
    background: rgba(10, 18, 30, .45);
  }
  .scrim.show { display: block; }
  .content { padding: 20px 16px 56px; }
  .topbar { padding: 11px 16px; gap: 10px; }
  /* ป้ายปีการศึกษาซ้ำกับหัวข้อในหน้า จึงซ่อนเพื่อไม่ให้แถบบนดันหน้าจอกว้างเกิน */
  .topbar .chip { display: none; }
  .topbar > div { min-width: 0; }
  .topbar h1 { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .topbar .crumb { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .g2, .g3, .g4 { grid-template-columns: minmax(0, 1fr); }
  .toc { grid-template-columns: minmax(0, 1fr); }
  .page-head h2 { font-size: 21px; }
}

/* ============ ตัวเลือกปีการศึกษา ============ */
.year-pick {
  display: flex; align-items: center; gap: 7px;
  padding: 4px 6px 4px 11px; border-radius: 999px;
  background: var(--surface-3); white-space: nowrap;
}
.year-pick > span { font-size: 12px; color: var(--text-2); }
.year-pick select {
  font-family: var(--font-display); font-size: 14px; font-weight: 600;
  color: var(--brand); background: var(--surface);
  border: 1px solid var(--border-strong); border-radius: 999px;
  padding: 3px 9px; cursor: pointer;
}
.year-pick select:focus-visible { outline: 2px solid var(--brand-3); outline-offset: 1px; }

/* ทิศทางการเปลี่ยนแปลงในหน้าเปรียบเทียบ
   ใช้สีกลางทั้งขึ้นและลง เพราะ "เพิ่มขึ้น" ไม่ได้แปลว่าดีเสมอไป
   เช่น นักเรียนออกกลางคันเพิ่มขึ้นเป็นเรื่องที่ต้องกังวล
   ลูกศรบอกทิศทางก็พอ ปล่อยให้ผู้อ่านตีความเอง */
.k-note.up, .k-note.down, .k-note.flat { color: var(--text-2); font-weight: 600; }
td.up, td.down { color: var(--text); }

/* ============ ปุ่มเล็ก ============ */
.btn-sm {
  font-family: inherit; font-size: 13.5px; font-weight: 500;
  padding: 6px 12px; border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong); background: var(--surface);
  color: var(--text); cursor: pointer; white-space: nowrap;
  transition: background .12s, border-color .12s;
}
.btn-sm:hover { background: var(--surface-3); }
.btn-sm.primary { background: var(--brand-2); border-color: var(--brand-2); color: #fff; }
.btn-sm.primary:hover { background: var(--brand); border-color: var(--brand); }
.btn-sm.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); }
.btn-sm.danger:hover { background: color-mix(in srgb, var(--danger) 10%, transparent); }

/* ============ เมนูพิมพ์ / ส่งออก ============ */
.menu-wrap { position: relative; }
.menu {
  display: none; position: absolute; right: 0; top: calc(100% + 8px); z-index: 40;
  min-width: 214px; padding: 6px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow-lg);
}
.menu.open { display: block; }
.menu-head {
  font-size: 11px; font-weight: 600; letter-spacing: .05em; text-transform: uppercase;
  color: var(--text-3); padding: 8px 10px 4px;
}
.menu button {
  display: block; width: 100%; text-align: right;
  font-family: inherit; font-size: 14px; color: var(--text);
  padding: 8px 10px; border: none; border-radius: var(--radius-sm);
  background: none; cursor: pointer;
}
.menu button:hover { background: var(--surface-3); }
/* รายการที่ทำได้เฉพาะตอนอยู่ในหน้าตาราง */
body:not(.on-table) .menu .only-table { display: none; }

/* ============ แถบเครื่องมือโหมดผู้ดูแล ============ */
.editbar {
  display: none; align-items: center; gap: 8px; flex-wrap: wrap;
  position: sticky; top: 52px; z-index: 19;
  padding: 8px 22px;
  background: color-mix(in srgb, var(--accent) 12%, var(--surface));
  border-bottom: 1px solid color-mix(in srgb, var(--accent) 35%, transparent);
}
body.is-editing .editbar { display: flex; }
.editbar .eb-title { font-size: 13.5px; font-weight: 600; color: var(--warn); }
.editbar .eb-dirty {
  display: none; font-size: 12.5px; color: var(--danger);
  padding: 2px 9px; border-radius: 999px;
  background: color-mix(in srgb, var(--danger) 13%, transparent);
}
body.is-editing.has-changes .editbar .eb-dirty { display: inline; }
/* บันทึกในเครื่องแล้ว แต่ผู้อื่นยังไม่เห็น (ยังไม่ได้ส่งออก/อัปโหลดไฟล์เผยแพร่) */
.editbar .eb-unpub {
  display: none; font-size: 12.5px; font-weight: 600; color: var(--warn);
  padding: 2px 9px; border-radius: 999px;
  background: color-mix(in srgb, var(--warn) 15%, transparent);
}
body.has-unpublished .editbar .eb-unpub { display: inline; }
.notice ol.steps { margin: 6px 0 8px; padding-left: 20px; }
.notice ol.steps li { margin-bottom: 3px; }
/* ปุ่มเผยแพร่ขึ้นเฉพาะเมื่อหน้านี้บันทึกเวอร์ชันใหม่ของตัวเองได้ */
#publishBtn { display: none; }
body.can-publish #publishBtn { display: inline-block; }

/* ป้ายบอกว่ากำลังดูข้อมูลที่แก้ไขไว้ในเครื่อง */
.local-badge {
  display: none; margin-bottom: 8px; padding: 4px 9px; border-radius: 6px;
  background: color-mix(in srgb, var(--accent) 18%, transparent);
  color: var(--warn); font-weight: 600; font-size: 11.5px;
}
body.has-local-data .local-badge { display: block; }

/* ============ ช่องกรอกในตาราง ============ */
.grid-table td.editing { padding: 3px 5px; }
.cell-in {
  width: 100%; min-width: 62px; box-sizing: border-box;
  font-family: inherit; font-size: 13.5px;
  font-variant-numeric: tabular-nums; text-align: right;
  padding: 5px 7px; border-radius: 5px;
  border: 1px solid var(--border-strong); background: var(--surface);
  color: var(--text);
}
.cell-in:focus {
  outline: none; border-color: var(--brand-3);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand-3) 22%, transparent);
}
.cell-in.lbl-in { text-align: right; min-width: 230px; font-variant-numeric: normal; }
/* ช่องที่ระบบคำนวณให้ แก้ไม่ได้ */
.grid-table td.computed { color: var(--text-3); background: var(--surface-2); }
/* ช่องผลรวมที่ระบบคำนวณให้ แต่พิมพ์ทับได้ — กรอบเส้นประบอกว่ายังเป็นค่าที่คำนวณอยู่ */
.grid-table td.computed-edit .cell-in {
  border-style: dashed; color: var(--text-2);
  background: color-mix(in srgb, var(--surface-2) 70%, var(--surface));
}
.grid-table td.computed-edit .cell-in:focus { border-style: solid; color: var(--text); }
.edit-hint { margin-bottom: 14px; }

/* ============ หน้าวิเคราะห์เปรียบเทียบ ============ */
.sec-head {
  margin: 22px 0 12px; font-size: 16px; font-weight: 650;
  letter-spacing: -.01em; color: var(--text);
}
.cmp-link { color: var(--brand); text-decoration: none; font-weight: 500; }
.cmp-link:hover { text-decoration: underline; }

/* ============ ข้อมูล ณ วันที่ (ท้ายตาราง) ============ */
.asof {
  margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--border);
  font-size: 12.5px; color: var(--text-2);
}
.asof b { color: var(--text-2); font-weight: 600; }
/* ช่องวัน/เดือน/ปี ของ "ข้อมูล ณ วันที่" */
.asof-edit .asof-fields {
  display: inline-flex; gap: 7px; align-items: center; flex-wrap: wrap; margin-left: 8px;
}
.asof-edit .asof-part {
  width: auto; min-width: 0; text-align: left; font-variant-numeric: normal;
}
.asof-edit .asof-d { width: 84px; }
.asof-edit .asof-y { width: 108px; }
.asof-edit .asof-m { min-width: 148px; }
.asof-edit em, .meta-edit em {
  display: block; font-style: normal; font-size: 11.5px;
  color: var(--text-3); margin-top: 6px;
}

/* ============ การ์ดสรุปเชิงวิเคราะห์ ============ */
.insight-card { border-left: 3px solid var(--brand-3); }
.insight-list { margin: 4px 0 0; padding-left: 20px; }
.insight-list li { margin-bottom: 6px; line-height: 1.6; }
.insight-list li::marker { color: var(--brand-3); }
.insight-list b { font-weight: 650; color: var(--text); }

/* ชื่อตารางที่แก้ไขได้ในโหมดผู้ดูแล — ให้หน้าตาใกล้เคียงหัวเรื่องเดิม */
.title-edit { margin: 0 0 6px; }
.title-edit .title-in {
  width: 100%; max-width: 860px;
  font-family: var(--font-display); font-size: 20px; font-weight: 700;
  letter-spacing: -.02em; text-align: left; font-variant-numeric: normal;
  padding: 7px 11px;
}
.title-edit em {
  display: block; font-style: normal; font-size: 12px;
  color: var(--text-3); margin-top: 6px;
}
.title-edit .head-fields {
  display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr);
  gap: 10px; margin-top: 9px; max-width: 860px;
}
.title-edit .head-fields label { display: block; }
.title-edit .head-fields span {
  display: block; font-size: 12px; color: var(--text-2); margin-bottom: 3px;
}
.title-edit .head-in { width: 100%; text-align: left; font-variant-numeric: normal; }
@media (max-width: 860px) { .title-edit .head-fields { grid-template-columns: minmax(0, 1fr); } }

/* หมายเหตุ / แหล่งที่มา ที่แก้ไขได้ในโหมดผู้ดูแล */
.meta-edit label { display: block; margin-bottom: 10px; }
.meta-edit label > b { display: block; margin-bottom: 4px; }
.meta-edit .meta-in {
  width: 100%; text-align: left; font-variant-numeric: normal; line-height: 1.5;
}
.meta-edit textarea.meta-in { resize: vertical; min-height: 54px; }

.issue-list { margin: 8px 0 0; padding-right: 18px; }
.issue-list li { margin-bottom: 3px; }

/* ============ ข้อความแจ้งเตือนลอย ============ */
.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translate(-50%, 16px);
  z-index: 100; max-width: min(560px, 92vw);
  padding: 11px 18px; border-radius: 10px;
  background: var(--text); color: var(--surface);
  font-size: 13.5px; box-shadow: var(--shadow-lg);
  opacity: 0; pointer-events: none;
  transition: opacity .18s, transform .18s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.ok { background: var(--ok); color: #fff; }
.toast.warn { background: var(--danger); color: #fff; }

/* ============ หน้าต่างปลดล็อก ============ */
dialog#unlockDlg {
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
  padding: 22px 24px; max-width: 380px; box-shadow: var(--shadow-lg);
}
dialog#unlockDlg::backdrop { background: rgba(10, 18, 30, .45); }
dialog#unlockDlg h3 { margin: 0 0 6px; font-size: 17px; }
dialog#unlockDlg p { margin: 0 0 12px; font-size: 13.5px; color: var(--text-2); }
dialog#unlockDlg input {
  width: 100%; box-sizing: border-box; font-family: inherit; font-size: 15px;
  padding: 9px 12px; border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong); background: var(--surface-2); color: var(--text);
}
dialog#unlockDlg .dlg-note { font-size: 12px; color: var(--text-3); margin: 10px 0 0; line-height: 1.5; }
.dlg-actions { display: flex; gap: 8px; justify-content: flex-start; margin-top: 16px; }

/* ============================================================
   บอร์ดประชาสัมพันธ์
   ============================================================ */
.nav-count {
  margin-left: auto; flex: none;
  font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 999px;
  background: var(--surface-3); color: var(--text-3);
}
.nav a.active .nav-count { background: var(--brand-2); color: #fff; }

.board-bar {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  margin-bottom: 14px;
}
.board-bar .spacer { flex: 1; }
.cat-chips { display: flex; gap: 6px; flex-wrap: wrap; }
.cat-chip {
  font-family: inherit; font-size: 12.5px; cursor: pointer;
  padding: 5px 12px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text-2); white-space: nowrap;
  transition: background .12s, border-color .12s, color .12s;
}
.cat-chip span {
  font-size: 11px; margin-left: 5px; color: var(--text-3);
  font-variant-numeric: tabular-nums;
}
.cat-chip:hover { background: var(--surface-3); }
.cat-chip.on {
  background: var(--brand-2); border-color: var(--brand-2); color: #fff; font-weight: 600;
}
.cat-chip.on span { color: rgba(255, 255, 255, .8); }

.board-search {
  font-family: inherit; font-size: 13px; color: var(--text);
  padding: 6px 12px; min-width: 230px;
  border: 1px solid var(--border-strong); border-radius: 999px;
  background: var(--surface);
}
.board-search:focus {
  outline: none; border-color: var(--brand-3);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand-3) 20%, transparent);
}

.board-grid {
  display: grid; gap: 13px;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
}

.post {
  display: flex; flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 15px 17px;
}
/* รายการปักหมุดใช้แถบสีด้านบนแทนพื้นหลังเข้ม จะได้ไม่แย่งสายตาจากเนื้อหา */
.post.pinned { border-top: 3px solid var(--accent); }

.post-top {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  margin-bottom: 7px;
}
.post-cat {
  font-size: 11px; font-weight: 600; padding: 2px 9px; border-radius: 999px;
  background: color-mix(in srgb, var(--brand-2) 13%, transparent); color: var(--brand);
}
.post-pin {
  font-size: 11px; font-weight: 600; padding: 2px 9px; border-radius: 999px;
  background: color-mix(in srgb, var(--accent) 20%, transparent); color: var(--warn);
}
.post-date { margin-left: auto; font-size: 12px; color: var(--text-3); white-space: nowrap; }

.post-title {
  margin: 0 0 6px; font-size: 16.5px; line-height: 1.35;
  font-family: var(--font-display); font-weight: 600;
}
.post-sum { margin: 0 0 12px; font-size: 13.8px; color: var(--text-2); line-height: 1.55; }

.post-foot {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  margin-top: auto; padding-top: 11px; border-top: 1px solid var(--border);
}
.post-file {
  display: flex; align-items: center; gap: 9px; min-width: 0;
  text-decoration: none; color: var(--text);
  padding: 6px 11px; border-radius: var(--radius-sm);
  background: var(--surface-2); border: 1px solid var(--border);
  transition: border-color .12s, background .12s;
}
.post-file:hover { border-color: var(--brand-3); background: var(--surface-3); }
.post-file .pf-icon { font-size: 17px; flex: none; }
.post-file b { display: block; font-size: 12.5px; font-weight: 600; }
.post-file em {
  display: block; font-style: normal; font-size: 11px; color: var(--text-3);
  max-width: 210px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.post-nofile { font-size: 12px; color: var(--text-3); }
.post-actions { margin-left: auto; display: flex; gap: 6px; }

/* ---- ฟอร์มในหน้าต่างเพิ่ม/แก้ไขรายการ ---- */
dialog#postDlg {
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text);
  padding: 22px 24px; width: min(520px, 92vw); box-shadow: var(--shadow-lg);
}
dialog#postDlg::backdrop { background: rgba(10, 18, 30, .45); }
dialog#postDlg h3 { margin: 0 0 16px; font-size: 17px; }

.fld { display: block; margin-bottom: 13px; }
.fld > span { display: block; font-size: 12.5px; color: var(--text-2); margin-bottom: 4px; }
.fld > span b { color: var(--danger); }
.fld input, .fld select, .fld textarea {
  width: 100%; box-sizing: border-box;
  font-family: inherit; font-size: 14px; color: var(--text);
  padding: 8px 11px; border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong); background: var(--surface-2);
}
.fld textarea { resize: vertical; line-height: 1.5; }
.fld input:focus, .fld select:focus, .fld textarea:focus {
  outline: none; border-color: var(--brand-3);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand-3) 20%, transparent);
}
.fld > em {
  display: block; font-style: normal; font-size: 11.5px;
  color: var(--text-3); margin-top: 4px; line-height: 1.45;
}
.fld-row { display: grid; grid-template-columns: 1fr 1fr; gap: 11px; }
.fld-check {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; color: var(--text-2); margin: 4px 0 0; cursor: pointer;
}
.fld-check input { width: 16px; height: 16px; }

code {
  font-family: ui-monospace, 'Cascadia Code', Consolas, monospace;
  font-size: .92em; padding: 1px 5px; border-radius: 4px;
  background: var(--surface-3); color: var(--brand);
}

@media (max-width: 860px) {
  .board-grid { grid-template-columns: minmax(0, 1fr); }
  .board-search { min-width: 0; flex: 1; }
  .fld-row { grid-template-columns: minmax(0, 1fr); }
}

/* บอร์ดไม่ใช่ตารางข้อมูล จึงไม่ต้องพิมพ์ไปกับรายงาน */
@media print { .board-bar, .board-grid { display: none !important; } }

/* ============ การเข้าถึง ============ */
a:focus-visible, button:focus-visible {
  outline: 2px solid var(--brand-3);
  outline-offset: 2px;
  border-radius: 4px;
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
  html { scroll-behavior: auto; }
}

/* ============================================================
   พื้นที่พิมพ์แบบตาราง Excel
   ซ่อนไว้บนจอ แสดงเฉพาะตอนสั่งพิมพ์จากเมนู
   ============================================================ */
#printArea { display: none; }

/* ============ สำหรับพิมพ์ ============ */
@page { size: A4 landscape; margin: 12mm 10mm; }

@media print {
  .sidebar, .topbar, .editbar, .scrim, .toast, dialog { display: none !important; }
  html, body { background: #fff !important; color: #000 !important; }
  body { font-size: 12px; }
  .content { padding: 0; max-width: none; }
  .card { box-shadow: none; border-color: #999; break-inside: avoid; page-break-inside: avoid; }
  .grid { gap: 10px; }

  /* โหมด "พิมพ์เป็นตาราง" — ซ่อนหน้าเว็บทั้งหมด เหลือเฉพาะตารางล้วน */
  body.printing .layout { display: none !important; }
  body.printing #printArea { display: block; }

  .xl-head { text-align: center; margin-bottom: 10mm; }
  .xl-head .xl-org { font-size: 13pt; }
  .xl-head h1 { font-size: 17pt; margin: 2mm 0 0; font-weight: 700; }
  .xl-head .xl-year { font-size: 13pt; margin-top: 1mm; }

  .xl-sheet { break-after: page; page-break-after: always; }
  .xl-sheet:last-child { break-after: auto; page-break-after: auto; }

  table.xl {
    width: 100%; border-collapse: collapse; font-size: 10pt;
    font-variant-numeric: tabular-nums;
  }
  table.xl caption {
    caption-side: top; text-align: left; padding: 0 0 3mm;
    font-size: 12pt; line-height: 1.4;
  }
  table.xl caption span { font-size: 10pt; font-weight: 400; }
  /* หัวตารางซ้ำทุกหน้าเมื่อตารางยาวข้ามหน้า */
  table.xl thead { display: table-header-group; }
  table.xl tr { break-inside: avoid; page-break-inside: avoid; }
  table.xl th, table.xl td {
    border: 0.6pt solid #000; padding: 1.2mm 2mm; vertical-align: middle;
  }
  table.xl th { background: #dce6f1 !important; font-weight: 700; text-align: center; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  /* ชื่อรายการชิดซ้าย ตัวเลขชิดขวา ตามแบบตารางใน Excel */
  table.xl td.lbl { text-align: left; }
  table.xl td.num { text-align: right; }
  table.xl tr.section td { background: #ededed !important; font-weight: 700; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  table.xl tr.group td.lbl { font-weight: 600; }
  table.xl tr.sub td.lbl { padding-left: 6mm; }
  table.xl tr.total td { background: #f2f2f2 !important; font-weight: 700; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .xl-src { font-size: 9pt; margin: 2mm 0 0; }
}
