/* ======================================================================
   app.css — ดีไซน์ซิสเต็มของระบบ (เขียนเองทั้งหมด ไม่ใช้ Tailwind)

   เหตุผลที่เลิกใช้ Tailwind CDN
     1. Tailwind Play CDN คอมไพล์ CSS ใหม่ทุกครั้งที่เปิดหน้าเว็บ จึงช้า
     2. ตัดการพึ่งพาเซิร์ฟเวอร์ภายนอก ถ้า CDN ล่มหรือถูกแทรกแซง เว็บไม่กระทบ
     3. ทำให้รัดกุม Content-Security-Policy ได้ (ไม่ต้องเปิด unsafe-eval)
     4. บังคับให้ดีไซน์มีระบบจริง เพราะสี ระยะ และมุมโค้ง อยู่ที่เดียว

   หลักการ: 5 สี แต่ละสีมีหน้าที่เดียว
   ====================================================================== */

/* ---------------------------------------------------------- 1. โทเคน */
:root {
  /* สี */
  --ink:        #1c1c1a;   /* ตัวอักษรหลักและข้อมูล */
  --muted:      #5c5c58;   /* ตัวอักษรรอง  contrast ~6.4:1 บนพื้นอ่อน */
  --ground:     #f7f7f5;   /* พื้นหลังหน้า */
  --surface:    #ffffff;   /* พื้นการ์ด */
  --hairline:   #e2e2dd;   /* เส้นคั่น */
  --hairline-2: #c9c9c2;   /* เส้นคั่นเข้มขึ้น */

  --brand:      #5e1d7b;   /* ม่วงราชภัฏ ใช้เฉพาะแถบหัวและท้ายเว็บ */
  --brand-ink:  #4a1762;
  --brand-soft: #dcc9e8;
  --brand-2:    #c40046;   /* แดงราชภัฏ ใช้เน้นเฉพาะจุด */

  --down:       #0f766e;   /* ใช้ไฟลดลง = ดี        วัดได้ 5.47:1 บนพื้นขาว */
  --down-soft:  #e6f2f0;
  --up:         #b45309;   /* ใช้ไฟเพิ่มขึ้น = ต้องดู  วัดได้ 5.02:1 บนพื้นขาว */
  --up-soft:    #fdf3e7;

  /* ข้อมูลปีก่อนในกราฟ — ต้องเข้มพอให้เห็นได้จริง วัดได้ 3.25:1 บนพื้นขาว
     ตามเกณฑ์ WCAG 1.4.11 ที่กำหนดว่าวัตถุกราฟิกที่สื่อความหมายต้องไม่ต่ำกว่า 3:1
     ถ้าแก้ค่านี้ ต้องแก้ fill ของแท่งปีก่อนใน render_month_chart() ให้ตรงกันด้วย */
  --ghost:      #8f8f88;

  /* ระยะ */
  --s1: 4px;  --s2: 8px;  --s3: 12px; --s4: 16px;
  --s5: 24px; --s6: 32px; --s7: 40px; --s8: 56px;

  /* มุมโค้ง — มีแค่สองค่า */
  --r-sm: 6px;
  --r-lg: 12px;

  /* ตัวอักษร — มีแค่ห้าขั้น */
  --t-xs: 12px; --t-sm: 13px; --t-md: 15px; --t-lg: 20px; --t-xl: 30px;

  --maxw: 1280px;
}

/* ------------------------------------------------------ 2. รีเซ็ตพื้นฐาน */
*,
*::before,
*::after { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--ground);
  color: var(--ink);
  font-family: 'Prompt', 'Sarabun', system-ui, -apple-system, 'Segoe UI', sans-serif;
  font-size: var(--t-md);
  line-height: 1.65;
}

h1, h2, h3 { margin: 0; font-weight: 500; line-height: 1.4; }
h1 { font-size: var(--t-lg); }
h2 { font-size: 16px; }
h3 { font-size: var(--t-md); }

p { margin: 0; }

a { color: var(--brand); }
a:hover { color: var(--brand-ink); }

img, svg { max-width: 100%; }

:focus-visible {
  outline: 3px solid var(--brand);
  outline-offset: 2px;
  border-radius: 2px;
}

.visually-hidden {
  position: absolute; width: 1px; height: 1px;
  margin: -1px; padding: 0; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.skip-link {
  position: absolute; left: -9999px; top: 0; z-index: 100;
  background: var(--surface); color: var(--ink);
  padding: var(--s3) var(--s4); border-radius: 0 0 var(--r-sm) 0;
}
.skip-link:focus { left: 0; }

/* ------------------------------------------------------- 3. แถบหัวเว็บ */
.app-bar {
  background: var(--brand);
  color: #ffffff;
  border-bottom: 3px solid var(--brand-2);
}

.app-bar__inner {
  max-width: var(--maxw);
  margin: 0 auto;
  min-height: 64px;
  padding: var(--s2) var(--s7);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s4);
  flex-wrap: wrap;
}

.app-bar__title {
  font-size: 16px;
  font-weight: 500;
  color: #ffffff;
  text-decoration: none;
}
.app-bar__title:hover { color: #ffffff; text-decoration: underline; }

.app-bar__org {
  font-size: var(--t-sm);
  color: var(--brand-soft);
  margin-left: var(--s3);
}

.app-bar__tools {
  display: flex;
  align-items: center;
  gap: var(--s3);
  flex-wrap: wrap;
}

.app-bar label { font-size: var(--t-sm); color: var(--brand-soft); }

/* ขอบต้องสว่างพอให้เห็นขอบเขตของปุ่มบนพื้นม่วง (4.69:1) */
.app-bar select,
.app-bar .btn {
  background: var(--brand-ink);
  color: #ffffff;
  border: 1px solid #c49cd8;
}
.app-bar select:hover,
.app-bar .btn:hover { background: #3d1252; color: #ffffff; }

/* เส้นโฟกัสสีม่วงจะมองไม่เห็นเลยบนแถบสีม่วง จึงต้องเปลี่ยนเป็นสีขาว */
.app-bar :focus-visible { outline-color: #ffffff; }

.app-bar__link {
  font-size: var(--t-sm);
  font-weight: 500;
  color: #ffffff;
  text-decoration: none;
  border-bottom: 1px solid #c49cd8;
  padding-bottom: 2px;
}
.app-bar__link:hover { color: #ffffff; border-bottom-color: #ffffff; }

/* --------------------------------------------------------- 4. เลย์เอาต์ */
.page {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: var(--s7) var(--s7) var(--s6);
  display: flex;
  flex-direction: column;
  gap: var(--s5);
}

.page--narrow {
  max-width: 560px;
  min-height: calc(100vh - 140px);
  justify-content: center;
}

.row {
  display: flex;
  align-items: center;
  gap: var(--s3);
  flex-wrap: wrap;
}
.row--between { justify-content: space-between; }
.grow { flex-grow: 1; }

/* ------------------------------------------------------------ 5. การ์ด */
.card {
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: var(--r-lg);
  padding: var(--s5) 28px;
}

.card__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s4);
  flex-wrap: wrap;
  margin-bottom: var(--s3);
}

.card__title { font-size: 16px; font-weight: 500; }

/* --------------------------------------------- 6. การ์ดสรุป (headline) */
.headline {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s7);
  flex-wrap: wrap;
}

.headline__eyebrow {
  font-size: var(--t-sm);
  color: var(--muted);
  margin-bottom: var(--s2);
}

.headline__main {
  font-size: var(--t-xl);
  font-weight: 500;
  line-height: 1.35;
}

.headline__note {
  font-size: 14px;
  color: var(--muted);
  margin-top: var(--s2);
}

.headline__aside {
  display: flex;
  gap: var(--s6);
  flex-shrink: 0;
  border-left: 1px solid var(--hairline);
  padding-left: var(--s6);
}

.headline__aside > div { text-align: right; }

/* ---------------------------------------------------------- 7. ตัวเลข */
.kpis {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: var(--r-lg);
  overflow: hidden;
}

.kpi {
  padding: var(--s5) 28px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: var(--s2);
  border-left: 1px solid var(--hairline);
}
.kpi:first-child { border-left: 0; }

.kpi__label { font-size: var(--t-sm); color: var(--muted); }

.kpi__value {
  font-size: 26px;
  font-weight: 500;
  color: var(--ink);
  line-height: 1.3;
}

.kpi__unit {
  font-size: var(--t-sm);
  font-weight: 400;
  color: var(--muted);
}

.stat-label { font-size: var(--t-sm); color: var(--muted); }
.stat-value { font-size: 22px; font-weight: 500; }

.is-down { color: var(--down); }
.is-up   { color: var(--up); }
.is-flat { color: var(--muted); }
.is-ghost { color: #76766f; }

/* ------------------------------------------------------------ 8. กราฟ */
.chart-wrap { position: relative; }

.chart { width: 100%; height: auto; display: block; }

/* พื้นที่รับเมาส์ของแต่ละเดือน — ใสจนมองไม่เห็น แต่รับการชี้ได้เต็มคอลัมน์ */
.chart .hit { cursor: default; }

.chart .hit:hover,
.chart .hit:focus {
  fill: rgba(94, 29, 123, 0.07);
}

/* กล่องข้อมูลที่ขึ้นตามเมาส์ */
.chart-tip {
  position: absolute;
  z-index: 5;
  left: 0;
  top: 0;
  pointer-events: none;
  background: var(--ink);
  color: #ffffff;
  border-radius: var(--r-sm);
  padding: 10px 14px;
  font-size: var(--t-sm);
  line-height: 1.7;

  /*
   * ห้ามใช้ white-space: nowrap ตรงนี้
   * ถ้ากล่องกว้างกว่าตัวกราฟ (เช่นบนมือถือจอแคบ) การจำกัดตำแหน่งใน app.js
   * จะดันกล่องหลุดออกนอกขอบซ้ายจนเกิดแถบเลื่อนแนวนอน
   */
  max-width: 240px;
  transform: translate(-50%, calc(-100% - 14px));
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.12s ease-out;
}

.chart-tip.is-on {
  opacity: 1;
  visibility: visible;
}

.chart-tip__month {
  font-weight: 500;
  font-size: var(--t-md);
  display: block;
  margin-bottom: 2px;
}

.chart-tip__row { display: block; color: #d8d8d2; }

/* สีในกล่องทึบต้องสว่างกว่าปกติ ของเดิมสว่างไม่พอเมื่ออยู่บนพื้นดำ */
.chart-tip__row .is-down { color: #5eead4; }
.chart-tip__row .is-up   { color: #fbbf5a; }

.legend {
  display: flex;
  gap: var(--s5);
  font-size: var(--t-sm);
  color: var(--muted);
  flex-wrap: wrap;
}

.legend span { display: flex; align-items: center; gap: 7px; }

.legend i {
  width: 11px; height: 11px;
  display: inline-block;
  font-style: normal;
}
.legend i.now   { background: var(--down); }
.legend i.prev  { background: var(--ghost); }
.legend i.avg   { width: 14px; height: 0; border-top: 2px dashed var(--ink); }

/* ------------------------------------------------------- 9. แถบไตรมาส */
.quarters {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  background: var(--surface);
  border: 1px solid var(--hairline);
  border-radius: var(--r-lg);
  overflow: hidden;
}

.quarter {
  padding: 18px var(--s5);
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 6px;
  border-left: 1px solid var(--hairline);
}
.quarter:first-child { border-left: 0; }
.quarter__value { font-size: var(--t-lg); font-weight: 500; }

/* ----------------------------------------------------------- 10. ตาราง */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border: 1px solid var(--hairline);
  border-radius: var(--r-lg);
  background: var(--surface);
}

.table-wrap--tall { max-height: 560px; overflow-y: auto; }

table.data {
  width: 100%;
  min-width: 900px;
  border-collapse: collapse;
  font-size: var(--t-sm);
}

table.data caption { text-align: left; padding: var(--s3); color: var(--muted); }

/*
 * ตารางที่ซ่อนไว้สำหรับโปรแกรมอ่านหน้าจอ
 * ต้องเขียนกฎนี้ไว้ เพราะ table.data มี specificity (0,1,1) สูงกว่า .visually-hidden (0,1,0)
 * ถ้าไม่มี ความกว้าง 900px จะชนะ แล้วกล่องที่ควรเล็ก 1px จะกว้างล้นออกไปนอกจอ
 */
table.data.visually-hidden { width: 1px; min-width: 0; }

table.data thead th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: #f0f0ec;
  color: var(--ink);
  font-weight: 500;
  text-align: left;
  padding: 10px var(--s3);
  border-bottom: 1px solid var(--hairline-2);
  white-space: nowrap;
}

table.data tbody th,
table.data tbody td {
  padding: 10px var(--s3);
  border-bottom: 1px solid var(--hairline);
  font-weight: 400;
  text-align: left;
  vertical-align: middle;
}

table.data tbody tr:last-child th,
table.data tbody tr:last-child td { border-bottom: 0; }

table.data tbody tr:hover { background: #fafaf8; }
table.data tr.is-invalid { background: var(--up-soft); }

.num { text-align: right; font-variant-numeric: tabular-nums; }
.mid { text-align: center; }
.nowrap { white-space: nowrap; }

/* ------------------------------------------------------------ 11. ฟอร์ม */
.field { display: flex; flex-direction: column; gap: 6px; }

label { font-size: var(--t-sm); font-weight: 500; color: var(--ink); }

.hint { font-size: var(--t-xs); color: var(--muted); }

input[type="text"],
input[type="number"],
input[type="password"],
select {
  font-family: inherit;
  font-size: 14px;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--hairline-2);
  border-radius: var(--r-sm);
  padding: 9px var(--s3);
  min-height: 40px;
  width: 100%;
}

input:hover, select:hover { border-color: #a8a8a0; }

/*
 * ห้ามใส่ outline: none ตรงนี้เด็ดขาด
 * เพราะกฎนี้อยู่หลัง :focus-visible และมี specificity สูงกว่า
 * จะไปลบเส้นโฟกัสของช่องกรอกทั้งระบบ ทำให้คนที่ใช้แป้นพิมพ์หาไม่เจอว่าอยู่ช่องไหน
 * เงาสีม่วงจาง ๆ ด้านล่างเป็นแค่ตัวเสริม ไม่ใช่ตัวบอกตำแหน่งโฟกัส
 */
input:focus, select:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(94, 29, 123, 0.15);
}

select { cursor: pointer; }

.form-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: var(--s4);
}
.form-grid--2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.form-grid .span-all { grid-column: 1 / -1; }

.stack { display: flex; flex-direction: column; gap: var(--s4); }

/* ------------------------------------------------------------ 12. ปุ่ม */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--s2);
  font-family: inherit;
  font-size: 14px;
  font-weight: 500;
  line-height: 1.2;
  min-height: 44px;
  padding: 10px 18px;
  border-radius: var(--r-sm);
  border: 1px solid var(--hairline-2);
  background: var(--surface);
  color: var(--ink);
  text-decoration: none;
  cursor: pointer;
  white-space: nowrap;
}

.btn:hover { background: #f0f0ec; color: var(--ink); }
.btn:active { transform: translateY(1px); }

.btn--primary {
  background: var(--brand);
  border-color: var(--brand);
  color: #ffffff;
}
.btn--primary:hover { background: var(--brand-ink); color: #ffffff; }

.btn--go {
  background: var(--down);
  border-color: var(--down);
  color: #ffffff;
}
.btn--go:hover { background: #0b5d56; color: #ffffff; }

.btn--danger {
  background: var(--surface);
  border-color: #e3b7b7;
  color: #a1121b;
}
.btn--danger:hover { background: #fbeff0; color: #a1121b; }

.btn--sm { min-height: 36px; padding: 6px var(--s3); font-size: var(--t-sm); }
.btn--block { width: 100%; }

/* --------------------------------------------------------- 13. แจ้งเตือน */
.alert {
  border: 1px solid var(--hairline);
  border-radius: var(--r-lg);
  padding: var(--s4) var(--s5);
  font-size: 14px;
}

.alert ul { margin: 0; padding-left: 20px; }
.alert p + p, .alert p + ul { margin-top: var(--s2); }

.alert--ok    { background: var(--down-soft); border-color: #b9dcd7; color: #0b5149; }
.alert--warn  { background: var(--up-soft);   border-color: #eccfa6; color: #7c3a06; }
.alert--error { background: #fdf0f1;          border-color: #e8bcc0; color: #8f1020; }
.alert--info  { background: #f1eef5;          border-color: #d5c8de; color: #4a1762; }

.badge {
  display: inline-block;
  font-size: var(--t-xs);
  font-weight: 500;
  padding: 2px 8px;
  border-radius: 999px;
  background: #f0f0ec;
  color: var(--muted);
}
.badge--admin { background: #f1eef5; color: var(--brand-ink); }

/* ---------------------------------------------------------- 14. ท้ายเว็บ */
.site-footer {
  border-top: 1px solid var(--hairline);
  background: var(--surface);
  margin-top: auto;
}

.site-footer__inner {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: var(--s4) var(--s7);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--s4);
  flex-wrap: wrap;
  font-size: var(--t-sm);
  color: var(--muted);
}

body { display: flex; flex-direction: column; min-height: 100vh; }
main { flex-grow: 1; }

/* ------------------------------------------------ 14.5 คลาสช่วยจัดระยะ
   Content-Security-Policy ของระบบตั้งไว้เป็น style-src 'self'
   โดยไม่เปิด unsafe-inline ซึ่งแปลว่า attribute style="..." ที่เขียนติดใน HTML
   จะถูกเบราว์เซอร์ทิ้งทั้งหมด จึงต้องมีคลาสสำเร็จรูปไว้ใช้แทน
   ====================================================================== */
.mb-1 { margin-bottom: var(--s1); }
.mb-2 { margin-bottom: var(--s2); }
.mb-3 { margin-bottom: var(--s3); }
.mb-4 { margin-bottom: var(--s4); }
.mb-5 { margin-bottom: 20px; }
.mt-1 { margin-top: var(--s1); }
.mt-2 { margin-top: var(--s2); }
.mt-3 { margin-top: var(--s3); }
.mt-4 { margin-top: var(--s4); }

.center      { text-align: center; }
.d-inline    { display: inline; }
.d-block     { display: block; }
.justify-mid { justify-content: center; }
.w-auto      { width: auto; }
.w-pw        { width: 170px; }

.card--tint { background: var(--ground); }

.lead { font-size: 18px; font-weight: 500; line-height: 1.45; }

.kpi__value--sm { font-size: var(--t-lg); }
.kpi__value--xs { font-size: 18px; }

.empty-cell { padding: var(--s7); }

.report-title    { font-size: 19px; }
.report-subtitle { font-size: 16px; color: var(--brand); margin-top: 2px; }

.report-head {
  border-bottom: 2px solid var(--brand-2);
  padding-bottom: var(--s3);
}

.report-foot {
  text-align: center;
  border-top: 1px solid var(--hairline);
  padding-top: 10px;
  margin-top: var(--s1);
}

/* ---------------------------------------------------------- 15. จอเล็ก */
@media (max-width: 900px) {
  .kpis      { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .quarters  { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpi:nth-child(3), .quarter:nth-child(3) { border-left: 0; }
  .kpi:nth-child(n+3), .quarter:nth-child(n+3) { border-top: 1px solid var(--hairline); }
  .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .headline__aside { border-left: 0; padding-left: 0; }
}

@media (max-width: 600px) {
  :root { --t-xl: 24px; --s7: 20px; }

  .page { padding: var(--s5) var(--s4) var(--s5); }
  .app-bar__inner { padding: var(--s3) var(--s4); }
  .site-footer__inner { padding: var(--s4); }
  .card { padding: var(--s4); }
  .kpi  { padding: var(--s4); }
  .form-grid { grid-template-columns: 1fr; }
  .headline__aside { gap: var(--s5); }
  .app-bar__org { margin-left: 0; display: block; }
}

/* ------------------------------------------------- 16. รายงาน A4 */
body.report {
  background: #525659;
  display: block;
}

.a4 {
  width: 210mm;
  min-height: 297mm;
  margin: 10mm auto;
  padding: 12mm 15mm;
  background: #ffffff;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.a4 .card { padding: var(--s4); border-radius: var(--r-sm); }

.report-tools {
  position: absolute;
  top: 16px;
  right: 16px;
  display: flex;
  gap: var(--s2);
}

@media screen and (max-width: 230mm) {
  body.report { background: #ffffff; }
  .a4 { width: 100%; min-height: 0; margin: 0; padding: var(--s4); }
  .report-tools { position: static; padding: var(--s4); }
}

@media print {
  @page { size: A4 portrait; margin: 0; }

  html, body.report {
    width: 210mm;
    background: #ffffff;
    margin: 0;
    padding: 0;
    display: block;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  .a4 {
    width: 210mm;
    margin: 0;
    padding: 12mm 15mm;
    box-shadow: none;
  }

  .no-print { display: none !important; }

  /* กล่องข้อมูลตามเมาส์ และไฮไลต์คอลัมน์ ไม่ต้องมีบนกระดาษ */
  .chart-tip { display: none !important; }
  .chart .hit:hover { fill: transparent !important; }

  .card { break-inside: avoid; }
  table.data { min-width: 0; }
  .table-wrap { overflow: visible; }
}
