/* ระบบรายงานเชิงบริหาร 7-Eleven — โทนสี ส้ม แดง เขียว
   สีมีหน้าที่สื่อความหมายเท่านั้น: เขียว = ถึงเกณฑ์ · ส้ม = เฝ้าระวัง · แดง = ต้องแก้ไข
   ส้มเข้มใช้เป็นสีหลักของแบรนด์ (ปุ่ม/หัวเรื่อง) เพราะเป็นสีประจำ 7-Eleven */

:root {
  --orange:        #F07C1E;
  --orange-deep:   #C85A00;
  --orange-soft:   #FFF1E3;
  --red:           #E1173A;
  --red-deep:      #A70E2A;
  --red-soft:      #FFECEF;
  --green:         #0E8A4A;
  --green-deep:    #076437;
  --green-soft:    #E6F5EC;

  --bg:            #FAF7F3;
  --surface:       #FFFFFF;
  --surface-2:     #F5F1EB;
  --line:          rgba(60, 45, 30, 0.12);
  --line-soft:     rgba(60, 45, 30, 0.06);
  --ink:           #1F1A16;
  --ink-2:         rgba(31, 26, 22, 0.66);
  --ink-3:         rgba(31, 26, 22, 0.40);
  --shadow:        0 1px 2px rgba(60, 40, 20, 0.05), 0 1px 1px rgba(60, 40, 20, 0.03);
  --shadow-up:     0 6px 22px rgba(60, 40, 20, 0.10), 0 1px 3px rgba(60, 40, 20, 0.05);

  --r-card: 14px;
  --r-btn: 10px;
  --r-chip: 999px;
  --sp: 8px;
  --sidebar: 252px;
  --ease: cubic-bezier(0.32, 0.72, 0, 1);

  /* Kanit เป็นตัวเรขาคณิตไม่มีหัว อ่านง่ายและดูทันสมัย ใช้เป็นตัวหลักทั้งระบบ */
  --th: 'Kanit', 'Noto Sans Thai', 'Leelawadee UI', 'Sarabun', 'Tahoma', sans-serif;
  /* Kanit ไม่มีตัวเลขความกว้างเท่ากัน (เลข 1 แคบกว่าเลข 9 เกือบเท่าตัว)
     ตัวเลขในตารางการเงินจึงจะไม่ตรงหลักถ้าใช้ Kanit
     เฉพาะช่องตัวเลขในตารางจึงใช้ IBM Plex Sans ซึ่งมี tabular figures
     ส่วนตัวเลข KPI ที่ยืนเดี่ยวไม่ต้องเรียงหลัก ยังคงเป็น Kanit ตามเดิม */
  --num: 'IBM Plex Sans', 'Kanit', 'Segoe UI', sans-serif;
  --mono: 'IBM Plex Sans', 'Consolas', monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --orange-soft: rgba(240, 124, 30, 0.16);
    --red-soft:    rgba(225, 23, 58, 0.16);
    --green-soft:  rgba(14, 138, 74, 0.18);
    --orange:      #FF9440;
    --red:         #FF5A75;
    --green:       #34C97B;
    --bg:          #14110E;
    --surface:     #1D1917;
    --surface-2:   #262120;
    --line:        rgba(255, 240, 225, 0.14);
    --line-soft:   rgba(255, 240, 225, 0.07);
    --ink:         #F6F1EC;
    --ink-2:       rgba(246, 241, 236, 0.68);
    --ink-3:       rgba(246, 241, 236, 0.40);
    --shadow:      0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-up:   0 8px 26px rgba(0, 0, 0, 0.5);
  }
}

[data-theme="dark"] {
  --orange-soft: rgba(240, 124, 30, 0.16);
  --red-soft:    rgba(225, 23, 58, 0.16);
  --green-soft:  rgba(14, 138, 74, 0.18);
  --orange:      #FF9440;
  --red:         #FF5A75;
  --green:       #34C97B;
  --bg:          #14110E;
  --surface:     #1D1917;
  --surface-2:   #262120;
  --line:        rgba(255, 240, 225, 0.14);
  --line-soft:   rgba(255, 240, 225, 0.07);
  --ink:         #F6F1EC;
  --ink-2:       rgba(246, 241, 236, 0.68);
  --ink-3:       rgba(246, 241, 236, 0.40);
  --shadow:      0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-up:   0 8px 26px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--th);
  font-size: 16px;
  line-height: 1.62;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 { line-height: 1.45; font-weight: 600; margin: 0; }
h1 { font-size: 30px; }
h2 { font-size: 21px; }
h3 { font-size: 18px; }
h4 { font-size: 16px; }
p  { margin: 0 0 10px; }
a  { color: var(--orange-deep); }

.num { font-variant-numeric: tabular-nums; font-feature-settings: "tnum"; }

/* ── โครงหน้า ─────────────────────────────────────────── */

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

.sidebar {
  width: var(--sidebar);
  flex: 0 0 var(--sidebar);
  border-right: 1px solid var(--line);
  background: var(--surface);
  padding: 20px 12px 24px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}

.brand { display: flex; align-items: center; gap: 10px; padding: 0 10px 18px; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 9px; flex: 0 0 34px;
  background: linear-gradient(160deg, var(--green) 0 33%, var(--orange) 33% 66%, var(--red) 66% 100%);
}
.brand-name { font-weight: 600; font-size: 15px; line-height: 1.3; }
.brand-sub  { font-size: 12px; color: var(--ink-3); line-height: 1.3; }

.nav-group { margin-top: 16px; }
.nav-title {
  font-size: 11px; letter-spacing: 0.06em; text-transform: uppercase;
  color: var(--ink-3); padding: 0 10px 6px; font-weight: 600;
}
.nav-item {
  display: flex; align-items: center; gap: 9px;
  padding: 7px 10px; border-radius: 8px; cursor: pointer;
  font-size: 14.5px; color: var(--ink-2);
  transition: background 160ms var(--ease), color 160ms var(--ease);
  user-select: none;
}
.nav-item:hover { background: var(--surface-2); color: var(--ink); }
.nav-item.on { background: var(--orange-soft); color: var(--orange-deep); font-weight: 600; }
.nav-dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; opacity: .5; flex: 0 0 7px; }

.main { flex: 1; min-width: 0; }

.toolbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 12px 28px;
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  backdrop-filter: blur(18px) saturate(180%);
  border-bottom: 1px solid var(--line);
}
.toolbar .grow { flex: 1; }

.content { padding: 24px 28px 72px; max-width: 1440px; }

/* ── ส่วนประกอบ ───────────────────────────────────────── */

.card {
  background: var(--surface);
  border: 1px solid var(--line-soft);
  border-radius: var(--r-card);
  box-shadow: var(--shadow);
  padding: 18px 20px;
}
.card + .card { margin-top: 16px; }
.card-head { display: flex; align-items: baseline; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }
.card-head h2, .card-head h3 { margin: 0; }
.card-note { font-size: 13px; color: var(--ink-3); }

.grid { display: grid; gap: 16px; }
.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)); }
.g23 { grid-template-columns: minmax(0, 2fr) minmax(0, 3fr); }
.g32 { grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); }

.section-title {
  display: flex; align-items: baseline; gap: 10px;
  margin: 34px 0 14px; padding-bottom: 7px;
  border-bottom: 1px solid var(--line);
}
.section-title:first-child { margin-top: 4px; }
.section-title h2 { font-size: 19px; }
/* เลขลำดับกับป้ายชื่อทฤษฎีเป็นข้อมูลรอง ต้องถอยให้หัวข้อเด่น */
.section-title .idx { font-size: 13px; font-weight: 600; color: var(--orange); flex: 0 0 auto; }
.section-title .theory {
  margin-left: auto; font-size: 11.5px; color: var(--ink-3); white-space: nowrap;
}

/* ปุ่ม */
.btn {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 8px 14px; border-radius: var(--r-btn);
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  font-family: inherit; font-size: 14px; cursor: pointer;
  transition: all 180ms var(--ease);
}
.btn:hover { background: var(--surface-2); }
.btn-primary {
  background: var(--orange); border-color: transparent; color: #fff; font-weight: 600;
}
.btn-primary:hover { background: var(--orange-deep); }
.btn[disabled] { opacity: .55; cursor: progress; }

select, input[type="search"] {
  font-family: inherit; font-size: 14px; color: var(--ink);
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--r-btn); padding: 7px 11px;
}

/* ชิปสถานะ */
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12.5px; padding: 2px 10px; border-radius: var(--r-chip);
  background: var(--surface-2); color: var(--ink-2); white-space: nowrap;
}
.chip.good { background: var(--green-soft); color: var(--green-deep); }
.chip.warn { background: var(--orange-soft); color: var(--orange-deep); }
.chip.bad  { background: var(--red-soft);   color: var(--red-deep); }
[data-theme="dark"] .chip.good, [data-theme="dark"] .chip.warn, [data-theme="dark"] .chip.bad { color: var(--ink); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .chip.good,
  :root:not([data-theme="light"]) .chip.warn,
  :root:not([data-theme="light"]) .chip.bad { color: var(--ink); }
}

/* KPI */
.kpi {
  background: var(--surface); border: 1px solid var(--line-soft);
  border-radius: var(--r-card); padding: 14px 16px 12px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 2px; position: relative; overflow: hidden;
}
.kpi::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px; background: var(--ink-3);
}
.kpi.good::before { background: var(--green); }
.kpi.warn::before { background: var(--orange); }
.kpi.bad::before  { background: var(--red); }
.kpi-label { font-size: 13px; color: var(--ink-2); }
/* Kanit ตัวสูงกว่าฟอนต์ไทยทั่วไป ถ้าใช้ระยะบรรทัด 1.25 ตามเดิม หัวตัวเลขกับสระจะโดนตัด */
.kpi-value { font-size: 27px; font-weight: 600; letter-spacing: 0; line-height: 1.42; }
.kpi-unit  { font-size: 15px; font-weight: 500; color: var(--ink-2); margin-left: 3px; }
.kpi-foot  { font-size: 12.5px; color: var(--ink-3); display: flex; align-items: center; gap: 6px; }
.delta-up   { color: var(--green); font-weight: 600; }
.delta-down { color: var(--red);   font-weight: 600; }
.delta-flat { color: var(--ink-3); }

/* ตาราง */
table.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
table.tbl th {
  text-align: left; font-weight: 600; font-size: 12.5px; color: var(--ink-2);
  padding: 8px 10px; border-bottom: 1px solid var(--line); position: sticky; top: 0;
  background: var(--surface); white-space: nowrap;
}
table.tbl td { padding: 8px 10px; border-bottom: 1px solid var(--line-soft); vertical-align: middle; }
table.tbl tbody tr:hover { background: var(--surface-2); }
table.tbl .r { text-align: right; white-space: nowrap; }
/* ช่องตัวเลขในตารางต้องเรียงหลักตรงกันทุกแถว จึงใช้ฟอนต์ที่มี tabular figures */
table.tbl .r.num, table.tbl td.num, table.tbl .c.num {
  font-family: var(--num);
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
  letter-spacing: 0.01em;
}
table.tbl .c { text-align: center; }
.tbl-wrap { overflow-x: auto; }
.bar-cell { position: relative; }
.bar-cell .fill {
  position: absolute; left: 0; top: 3px; bottom: 3px; border-radius: 4px; opacity: .16;
}

/* รายการข้อสังเกต / ข้อเสนอแนะ */
.finding {
  display: flex; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line-soft);
}
.finding:last-child { border-bottom: 0; }
.finding .mark {
  width: 8px; height: 8px; border-radius: 50%; margin-top: 9px; flex: 0 0 8px; background: var(--ink-3);
}
.finding.good .mark { background: var(--green); }
.finding.warn .mark { background: var(--orange); }
.finding.bad  .mark { background: var(--red); }
.finding h4 { font-size: 15px; margin-bottom: 2px; }
.finding p  { font-size: 14px; color: var(--ink-2); margin: 0; }
.finding .why { font-size: 13px; color: var(--ink-3); margin-top: 4px; }

/* ── แถบคำตัดสิน — สิ่งแรกที่ตาต้องเห็น ─────────────────── */
.verdict {
  display: flex; gap: 18px; align-items: flex-start;
  border-radius: var(--r-card); padding: 20px 24px;
  border: 1px solid var(--line-soft); box-shadow: var(--shadow);
  background: var(--surface); position: relative; overflow: hidden;
}
.verdict::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 6px; background: var(--ink-3); }
.verdict.good::before { background: var(--green); }
.verdict.warn::before { background: var(--orange); }
.verdict.bad::before  { background: var(--red); }
.verdict-icon {
  width: 52px; height: 52px; flex: 0 0 52px; border-radius: 14px;
  display: grid; place-items: center; font-size: 26px; font-weight: 600; line-height: 1;
}
.verdict.good .verdict-icon { background: var(--green-soft); color: var(--green); }
.verdict.warn .verdict-icon { background: var(--orange-soft); color: var(--orange-deep); }
.verdict.bad  .verdict-icon { background: var(--red-soft);   color: var(--red-deep); }
.verdict-body { flex: 1; min-width: 0; }
.verdict-head { font-size: 22px; font-weight: 600; line-height: 1.45; margin-bottom: 4px; }
.verdict-sub  { font-size: 14.5px; color: var(--ink-2); }
.verdict-money {
  text-align: right; padding-left: 20px; border-left: 1px solid var(--line);
  flex: 0 0 auto; align-self: stretch; display: flex; flex-direction: column; justify-content: center;
}
.verdict-money .amt { font-size: 26px; font-weight: 600; color: var(--orange-deep); line-height: 1.35; }
.verdict-money .cap { font-size: 12.5px; color: var(--ink-3); }

/* ── แถบสัญญาณย่อ — ตัวชี้วัดทั้งหมดในแถวเดียว อ่านจบใน 3 วินาที ── */
.signals { display: flex; flex-wrap: wrap; gap: 8px; }
.sig {
  display: flex; align-items: center; gap: 9px;
  padding: 9px 14px 9px 11px; border-radius: 11px;
  background: var(--surface); border: 1px solid var(--line-soft); box-shadow: var(--shadow);
  min-width: 152px; flex: 1 1 152px;
}
.sig-dot { width: 11px; height: 11px; border-radius: 50%; flex: 0 0 11px; background: var(--ink-3); }
.sig.good .sig-dot { background: var(--green); }
.sig.warn .sig-dot { background: var(--orange); }
.sig.bad  .sig-dot { background: var(--red); }
.sig-txt { min-width: 0; line-height: 1.4; }
.sig-name { font-size: 12.5px; color: var(--ink-2); white-space: nowrap; }
.sig-val  { font-size: 17px; font-weight: 600; font-family: var(--num); font-variant-numeric: tabular-nums; }
.sig.good .sig-val { color: var(--green-deep); }
.sig.warn .sig-val { color: var(--orange-deep); }
.sig.bad  .sig-val { color: var(--red-deep); }
[data-theme="dark"] .sig-val { color: var(--ink) !important; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .sig-val { color: var(--ink) !important; }
}

/* ── สามเรื่องที่ต้องทำ ─────────────────────────────────── */
.todo { display: grid; gap: 10px; }
.todo-item {
  display: flex; gap: 14px; align-items: flex-start;
  padding: 14px 16px; border-radius: 12px; background: var(--surface-2);
}
.todo-no {
  width: 28px; height: 28px; flex: 0 0 28px; border-radius: 9px;
  display: grid; place-items: center; font-size: 14px; font-weight: 600;
  background: var(--surface); color: var(--ink-2); box-shadow: var(--shadow);
}
.todo-item.p1 .todo-no { background: var(--red);    color: #fff; }
.todo-item.p2 .todo-no { background: var(--orange); color: #fff; }
.todo-item.p3 .todo-no { background: var(--green);  color: #fff; }
.todo-body { flex: 1; min-width: 0; }
.todo-title { font-size: 15.5px; font-weight: 600; margin-bottom: 2px; }
.todo-text  { font-size: 14px; color: var(--ink-2); line-height: 1.55; }
.todo-gain  { font-size: 13px; color: var(--orange-deep); font-weight: 600; white-space: nowrap; align-self: center; }

/* ── คำอธิบายยาวที่พับเก็บได้ ───────────────────────────── */
details.more { margin-top: 12px; }
details.more > summary {
  cursor: pointer; list-style: none; display: inline-flex; align-items: center; gap: 6px;
  font-size: 13.5px; color: var(--orange-deep); font-weight: 500;
  padding: 5px 12px; border-radius: var(--r-chip); background: var(--orange-soft);
  user-select: none;
}
details.more > summary::-webkit-details-marker { display: none; }
details.more > summary::after { content: "▾"; font-size: 11px; transition: transform 180ms var(--ease); }
details.more[open] > summary::after { transform: rotate(180deg); }
details.more > .more-body {
  margin-top: 10px; font-size: 14px; color: var(--ink-2); line-height: 1.65;
  padding-left: 14px; border-left: 2px solid var(--line);
}

.callout {
  border-left: 3px solid var(--orange); background: var(--orange-soft);
  padding: 12px 16px; border-radius: 0 10px 10px 0; font-size: 14px;
}
.callout.green { border-color: var(--green); background: var(--green-soft); }
.callout.red   { border-color: var(--red);   background: var(--red-soft); }

.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 12.5px; color: var(--ink-2); }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; margin-right: 5px; }

.matrix { display: grid; gap: 8px; }
.matrix .cell {
  border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; min-height: 108px;
  background: var(--surface);
}
.matrix .cell h4 { font-size: 14px; margin-bottom: 6px; }
.matrix .cell ul { margin: 0; padding-left: 17px; font-size: 13.5px; color: var(--ink-2); }
.matrix .cell li { margin-bottom: 3px; }

.empty {
  display: grid; place-items: center; text-align: center; gap: 8px;
  padding: 56px 20px; color: var(--ink-3); font-size: 14.5px;
}

.toast {
  position: fixed; right: 22px; bottom: 22px; z-index: 60;
  background: var(--surface); border: 1px solid var(--line);
  box-shadow: var(--shadow-up); border-radius: 12px;
  padding: 12px 16px; font-size: 14px; max-width: 380px;
  display: flex; gap: 10px; align-items: flex-start;
  animation: rise 280ms var(--ease);
}
@keyframes rise { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }

.spin { animation: spin 900ms linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }

svg { display: block; max-width: 100%; }
svg text { font-family: var(--th); }
.fig { overflow-x: auto; }
.fig-cap { font-size: 12.5px; color: var(--ink-3); margin-top: 8px; }

.stack { display: flex; flex-direction: column; gap: 10px; }
.row   { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.mt    { margin-top: 16px; }
.muted { color: var(--ink-2); font-size: 14px; }
.small { font-size: 13px; }

@media (max-width: 1180px) {
  .g4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .g3 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .g23, .g32 { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 860px) {
  /* จอแคบ แถบคำตัดสินต้องเรียงลงมาแทนการเบียดกันในแนวนอน */
  .verdict { flex-wrap: wrap; padding: 16px 18px; }
  .verdict-head { font-size: 19px; }
  .verdict-money {
    border-left: 0; border-top: 1px solid var(--line);
    padding: 12px 0 0; margin-top: 12px; width: 100%; text-align: left;
  }
  .sig { min-width: 132px; flex: 1 1 132px; }
  .todo-item { flex-wrap: wrap; }
  .todo-gain { width: 100%; padding-left: 42px; }

  /* เมนูแนวตั้งกินพื้นที่เต็มจอแรกบนมือถือ ต้องยุบเป็นแถบเลื่อนแนวนอน
     ไม่งั้นเปิดหน้ามาต้องเลื่อนผ่านเมนูทั้งหมดกว่าจะเจอเนื้อหา */
  .shell { flex-direction: column; }
  .sidebar {
    width: 100%; height: auto; flex: none; border-right: 0;
    border-bottom: 1px solid var(--line);
    position: sticky; top: 0; z-index: 30; padding: 10px 12px 8px;
    background: color-mix(in srgb, var(--surface) 92%, transparent);
    backdrop-filter: blur(18px) saturate(180%);
    overflow: visible;
  }
  .brand { padding: 0 2px 10px; }
  #nav { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
  #nav::-webkit-scrollbar { display: none; }
  .nav-group { margin-top: 0; display: flex; gap: 6px; }
  .nav-title { display: none; }
  .nav-item { white-space: nowrap; padding: 7px 13px; border-radius: var(--r-chip); background: var(--surface-2); }
  .nav-dot { display: none; }
  /* ต้องอยู่หลัง .nav-group เพราะความจำเพาะเท่ากัน กฎที่มาทีหลังจะชนะ */
  .sidebar .nav-theory { display: none; }
  /* แถบเครื่องมือบนมือถือต้องเตี้ยที่สุด เหลือเฉพาะตัวเลือกสาขา งวด และปุ่มอัปเดต */
  .toolbar { top: 0; position: static; padding: 10px 16px; gap: 8px; }
  .toolbar #stamp, .toolbar label { display: none; }
  .toolbar .btn { padding: 7px 11px; font-size: 13.5px; }
  .toolbar select { flex: 1 1 auto; min-width: 0; }
  .content, .toolbar { padding-left: 16px; padding-right: 16px; }
  .g2, .g3, .g4 { grid-template-columns: minmax(0, 1fr); }
}

@media print {
  .sidebar, .toolbar { display: none; }
  .content { padding: 0; max-width: none; }
  .card { break-inside: avoid; box-shadow: none; border: 1px solid #ddd; }
  body { background: #fff; }
}
