:root {
  --navy: #14314F;
  --navy-2: #1F4E79;
  --teal: #17707E;
  --teal-m: #2E9CB0;
  --amber: #C07A12;
  --red: #A93226;
  --green: #1E6F4C;
  --gray: #5D6B7A;
  --text: #1F2A35;
  --muted: #6B7785;
  --line: #E3E8EE;
  --bg: #F4F6F9;
  --card: #FFFFFF;
  --tint-navy: #E9EFF6;
  --tint-teal: #E4F1F3;
  --tint-amber: #FAF0DF;
  --tint-red: #F8E8E6;
  --shadow: 0 1px 2px rgba(20, 49, 79, .05), 0 8px 24px rgba(20, 49, 79, .06);
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Sarabun", "Noto Sans Thai", "Thonburi", "Tahoma", system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, h4 { margin: 0; font-weight: 700; color: var(--navy); }
a { color: var(--teal); text-decoration: none; }
button, select, input { font-family: inherit; font-size: inherit; color: inherit; }

/* ------------------------------------------------------------------ topbar */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 24px;
  padding: 12px 28px;
  background: var(--navy);
  color: #fff;
  position: sticky; top: 0; z-index: 40;
}
.brand { display: flex; align-items: center; gap: 14px; min-width: 0; }
.logo {
  width: 42px; height: 42px; border-radius: 12px;
  background: linear-gradient(135deg, var(--teal-m), var(--teal));
  display: grid; place-items: center;
  font-weight: 700; font-size: 18px; color: #fff; letter-spacing: .5px;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.15);
}
.brand h1 { font-size: 19px; color: #fff; letter-spacing: .2px; }
.brand p { margin: 1px 0 0; font-size: 12.5px; color: #A9BDD1; }

.toolbar { display: flex; align-items: center; gap: 18px; }
.field { display: flex; align-items: center; gap: 8px; font-size: 13px; color: #B9CBDD; }
.field select {
  background: rgba(255,255,255,.1); color: #fff; border: 1px solid rgba(255,255,255,.18);
  border-radius: 9px; padding: 7px 12px; outline: none; min-width: 190px;
}
.field select option { color: var(--text); background: #fff; }
.meta-note { font-size: 12px; color: #8FA6BC; text-align: right; line-height: 1.35; }
.meta-note b { color: #D8E4EF; font-weight: 600; }

/* -------------------------------------------------------------------- tabs */
.tabs {
  display: flex; gap: 4px; padding: 0 20px;
  background: #fff; border-bottom: 1px solid var(--line);
  position: sticky; top: 66px; z-index: 35; overflow-x: auto;
}
.tabs a {
  padding: 13px 16px 11px; font-size: 14px; font-weight: 600; color: var(--muted);
  border-bottom: 2.5px solid transparent; white-space: nowrap;
}
.tabs a:hover { color: var(--navy); }
.tabs a.active { color: var(--navy); border-bottom-color: var(--teal-m); }
.tabs .credit {
  margin-left: auto; display: inline-flex; align-items: center; gap: 4px;
  padding-left: 18px; font-size: 11.5px; color: var(--muted); white-space: nowrap;
  border-bottom: 2.5px solid transparent;
}
.tabs .credit b { color: var(--navy); font-weight: 700; }

/* ------------------------------------------------------------------- shell */
main { padding: 22px 28px 60px; max-width: 1560px; margin: 0 auto; }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 20px; margin-bottom: 16px; }
.page-head h2 { font-size: 21px; }
.page-head p { margin: 3px 0 0; color: var(--muted); font-size: 13.5px; }

.grid { display: grid; gap: 16px; }
.g-kpi { grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); }
.g-2 { grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); align-items: start; }
.g-3 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }

.card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px 18px; border: 1px solid rgba(20,49,79,.04); min-width: 0;
}
.card > h3 {
  font-size: 14px; letter-spacing: .2px; margin-bottom: 2px;
  display: flex; align-items: baseline; justify-content: space-between; gap: 10px;
}
.card > h3 .sub { font-size: 11.5px; font-weight: 500; color: var(--muted); }
.card .chart-wrap { margin-top: 10px; }

/* --------------------------------------------------------------------- kpi */
.kpi { position: relative; padding: 14px 16px 13px; overflow: hidden; }
.kpi::before {
  content: ""; position: absolute; inset: 0 0 auto 0; height: 3px; background: var(--navy);
}
.kpi.teal::before { background: var(--teal-m); }
.kpi.amber::before { background: var(--amber); }
.kpi.red::before { background: var(--red); }
.kpi .label { font-size: 12px; font-weight: 600; color: var(--muted); letter-spacing: .2px; }
.kpi .value { font-size: 30px; font-weight: 700; line-height: 1.15; margin: 4px 0 2px; color: var(--navy); letter-spacing: -.3px; }
.kpi.teal .value { color: var(--teal); }
.kpi.amber .value { color: var(--amber); }
.kpi.red .value { color: var(--red); }
.kpi .sub { font-size: 12px; color: var(--muted); }
.kpi .unit { font-size: 14px; font-weight: 600; color: var(--muted); margin-left: 3px; }

.delta { font-size: 12px; font-weight: 700; padding: 1px 6px; border-radius: 6px; margin-left: 6px; }
.delta.up { color: var(--green); background: rgba(30,111,76,.1); }
.delta.down { color: var(--red); background: rgba(169,50,38,.1); }
.delta.flat { color: var(--muted); background: rgba(93,107,122,.1); }

/* ------------------------------------------------------------------ tables */
table.data { width: 100%; border-collapse: collapse; font-size: 13.5px; }
table.data th {
  text-align: left; font-weight: 600; color: var(--muted); font-size: 12px;
  padding: 9px 10px; border-bottom: 1px solid var(--line); white-space: nowrap;
  position: sticky; top: 0; background: #fff; z-index: 2;
}
table.data th.num, table.data td.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data td { padding: 8px 10px; border-bottom: 1px solid #F1F4F8; }
table.data tbody tr:hover { background: #F7FAFC; }
table.data tbody tr.clickable { cursor: pointer; }
table.data tfoot td { font-weight: 700; color: var(--navy); border-top: 1.5px solid var(--line); }

.table-scroll { max-height: 560px; overflow: auto; border-radius: 10px; }

.pill {
  display: inline-block; padding: 2px 9px; border-radius: 999px;
  font-size: 11.5px; font-weight: 700; white-space: nowrap;
}
.pill.high { background: var(--tint-red); color: var(--red); }
.pill.mid { background: var(--tint-amber); color: var(--amber); }
.pill.ok { background: #E7F2EC; color: var(--green); }
.pill.info { background: var(--tint-teal); color: var(--teal); }

.bar-cell { display: block; height: 7px; border-radius: 4px; background: #EDF1F5; overflow: hidden; min-width: 60px; }
.bar-cell > i { display: block; height: 100%; background: var(--teal-m); border-radius: 4px; }

/* ------------------------------------------------------------------ charts */
svg.chart { display: block; width: 100%; overflow: visible; }
svg.chart text { font-family: "Sarabun", sans-serif; fill: var(--muted); font-size: 11px; }
svg.chart .axis-line { stroke: var(--line); stroke-width: 1; }
svg.chart .grid-line { stroke: #EEF2F6; stroke-width: 1; }
svg.chart .bar:hover, svg.chart .pt:hover { opacity: .82; }
svg.chart text.bar-total { font-size: 11px; font-weight: 700; fill: var(--navy); }
svg.chart text.seg-txt { font-size: 9.5px; font-weight: 700; fill: #fff; }
svg.chart text.seg-txt.out { fill: #8A5A12; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 14px; margin-top: 8px; font-size: 12px; color: var(--muted); }
.chart-legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; }
.empty {
  display: grid; place-items: center; height: 180px; color: var(--muted);
  font-size: 13px; text-align: center; padding: 0 20px;
}

/* ----------------------------------------------------------------- toolbar */
.controls { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 14px; }
.controls label { font-size: 12.5px; color: var(--muted); font-weight: 600; }
.controls select, .controls input[type="search"] {
  border: 1px solid var(--line); background: #fff; border-radius: 9px;
  padding: 8px 12px; outline: none;
}
.controls input[type="search"] { min-width: 240px; }
.controls select:focus, .controls input:focus { border-color: var(--teal-m); box-shadow: 0 0 0 3px rgba(46,156,176,.12); }

.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  padding: 7px 14px; border-radius: 999px; border: 1px solid var(--line);
  background: #fff; font-size: 13px; font-weight: 600; color: var(--muted); cursor: pointer;
}
.chip.active { background: var(--navy); border-color: var(--navy); color: #fff; }

.btn {
  border: 1px solid var(--line); background: #fff; border-radius: 9px;
  padding: 8px 16px; font-weight: 600; font-size: 13.5px; cursor: pointer;
}
.btn:hover { border-color: var(--teal-m); color: var(--teal); }
.btn.primary { background: var(--teal); border-color: var(--teal); color: #fff; }
.btn.primary:hover { background: #145f6b; color: #fff; }
.btn.danger { color: var(--red); border-color: #F0CFCB; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ------------------------------------------------------------------ upload */
.dropzone {
  border: 2px dashed #C9D6E2; border-radius: var(--radius); background: #FBFDFE;
  padding: 34px 20px; text-align: center; cursor: pointer; transition: .15s;
}
.dropzone:hover, .dropzone.over { border-color: var(--teal-m); background: #F2F9FA; }
.dropzone h3 { font-size: 15px; margin-bottom: 4px; }
.dropzone p { margin: 0; color: var(--muted); font-size: 13px; }
.dropzone .hint { margin-top: 10px; font-size: 12px; color: #8B99A8; }

.upload-result { margin-top: 16px; }
.kv { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; }
.kv > div { background: #F7FAFC; border-radius: 10px; padding: 10px 12px; }
.kv .k { font-size: 11.5px; color: var(--muted); font-weight: 600; }
.kv .v { font-size: 18px; font-weight: 700; color: var(--navy); }

.periods-list { display: flex; flex-direction: column; gap: 8px; }
.period-row {
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  padding: 10px 14px; border: 1px solid var(--line); border-radius: 10px; background: #fff;
}
.period-row .lbl { font-weight: 600; }
.period-row .sm { font-size: 12px; color: var(--muted); }

/* ------------------------------------------------------------------ drawer */
.drawer {
  position: fixed; top: 0; right: 0; height: 100vh; width: min(560px, 94vw);
  background: #fff; z-index: 60; box-shadow: -18px 0 48px rgba(20,49,79,.18);
  overflow-y: auto; padding: 22px 24px 40px;
  animation: slide .18s ease-out;
}
@keyframes slide { from { transform: translateX(24px); opacity: .4; } to { transform: none; opacity: 1; } }
.scrim { position: fixed; inset: 0; background: rgba(20,49,79,.28); z-index: 55; }
.drawer h3 { font-size: 18px; }
.drawer .close {
  position: absolute; top: 16px; right: 18px; border: none; background: #F1F4F8;
  width: 32px; height: 32px; border-radius: 9px; cursor: pointer; font-size: 16px; color: var(--gray);
}
.section-title { font-size: 13px; font-weight: 700; color: var(--navy); margin: 22px 0 8px; }
.kpi-mini { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.kpi-mini > div { background: #F7FAFC; border-radius: 10px; padding: 10px 12px; }
.kpi-mini .k { font-size: 11px; color: var(--muted); font-weight: 600; }
.kpi-mini .v { font-size: 19px; font-weight: 700; color: var(--navy); }

/* ------------------------------------------------------------------- misc */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--navy); color: #fff; padding: 11px 20px; border-radius: 10px;
  font-size: 13.5px; z-index: 80; box-shadow: var(--shadow);
}
.toast.err { background: var(--red); }
.loading { display: grid; place-items: center; height: 50vh; color: var(--muted); }
.spinner {
  width: 26px; height: 26px; border: 3px solid #E3E8EE; border-top-color: var(--teal-m);
  border-radius: 50%; animation: spin .8s linear infinite; margin-bottom: 10px;
}
@keyframes spin { to { transform: rotate(360deg); } }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.nowrap { white-space: nowrap; }
.tag-src { font-size: 11.5px; color: var(--muted); }

@media (max-width: 860px) {
  .tabs .credit { display: none; }
  .topbar { flex-direction: column; align-items: flex-start; gap: 12px; }
  .toolbar { width: 100%; justify-content: space-between; }
  .tabs { top: 118px; }
  main { padding: 16px 14px 40px; }
  .kpi .value { font-size: 25px; }
}

.chart-tip {
  position: fixed; z-index: 100; pointer-events: none;
  background: #14314F; color: #fff; padding: 6px 10px; border-radius: 8px;
  font-size: 12px; line-height: 1.45; box-shadow: 0 6px 18px rgba(20,49,79,.22);
  max-width: 260px;
}
.chart-tip b { color: #FFD08A; font-weight: 700; }
table.data tr.current { background: #F1F8FA; }
table.data tr.current td { font-weight: 600; }

/* ------------------------------------------------------------------- auth */
.auth-box { display: flex; align-items: center; gap: 8px; }
.user-badge {
  font-size: 12.5px; color: #D8E4EF; background: rgba(255,255,255,.10);
  padding: 6px 11px; border-radius: 8px; white-space: nowrap;
}
.user-badge b { color: #fff; }
.btn-ghost {
  background: rgba(255,255,255,.10); border: 1px solid rgba(255,255,255,.22);
  color: #fff; border-radius: 9px; padding: 7px 14px; font-size: 13px;
  font-weight: 600; cursor: pointer; white-space: nowrap;
}
.btn-ghost:hover { background: rgba(255,255,255,.2); }
.btn-ghost.accent { background: var(--teal); border-color: var(--teal); }
.btn-ghost.accent:hover { background: #145f6b; }

.modal-scrim {
  position: fixed; inset: 0; background: rgba(20,49,79,.5); z-index: 90;
  display: grid; place-items: center; padding: 20px;
}
.modal {
  background: #fff; border-radius: 16px; padding: 26px 28px 22px;
  width: min(390px, 94vw); box-shadow: 0 24px 60px rgba(20,49,79,.28);
  position: relative;
}
.modal h3 { font-size: 18px; }
.modal p.sub { margin: 4px 0 0; font-size: 13px; color: var(--muted); }
.modal label { display: block; font-size: 12.5px; font-weight: 700; color: var(--muted); margin: 16px 0 6px; }
.modal input {
  width: 100%; border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px; font-size: 14px; outline: none;
}
.modal input:focus { border-color: var(--teal-m); box-shadow: 0 0 0 3px rgba(46,156,176,.14); }
.modal .actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }
.modal .err { color: var(--red); font-size: 12.5px; margin-top: 10px; min-height: 18px; }
.modal .close-x {
  position: absolute; top: 14px; right: 16px; border: none; background: #F1F4F8;
  width: 30px; height: 30px; border-radius: 8px; cursor: pointer; color: var(--gray);
}
.lock-card { text-align: center; padding: 46px 20px; }
.lock-card .icon { font-size: 40px; line-height: 1; margin-bottom: 12px; }
.lock-card h3 { font-size: 18px; margin-bottom: 6px; }
.lock-card p { color: var(--muted); font-size: 13.5px; margin: 0 0 18px; }

/* ------------------------------------------------------------ service groups */
.group-card { cursor: pointer; transition: transform .12s ease, box-shadow .12s ease; }
.group-card:hover { transform: translateY(-2px); box-shadow: 0 10px 26px rgba(20,49,79,.13); }
.gc-head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
.gc-head b { font-size: 14.5px; color: var(--navy); }
.gc-head span { font-size: 11.5px; color: var(--muted); }
.gc-main { font-size: 26px; font-weight: 700; color: var(--navy); margin: 8px 0 4px; line-height: 1.1; }
.gc-main small { font-size: 12px; color: var(--muted); font-weight: 600; margin-left: 5px; }
.gc-row { display: flex; justify-content: space-between; font-size: 12.5px; color: var(--muted); padding: 2px 0; }
.gc-row .hi { color: var(--red); font-weight: 700; }
.gc-row .ok { color: var(--green); font-weight: 700; }
.stack-bar { display: flex; height: 6px; border-radius: 4px; overflow: hidden; background: #EDF1F5; margin-top: 10px; }
.stack-bar i { display: block; height: 100%; }

table.data tr.grp-row td { background: #F2F7FA; font-weight: 700; color: var(--navy); border-top: 1px solid var(--line); }
table.data tr.grp-row td:first-child { border-left: 3px solid var(--teal-m); }
table.data td.lvl { padding-left: 26px; color: #3A4756; }
table.data td.lvl::before { content: "└ "; color: #B6C3CF; }

/* ------------------------------------------------------------- segmented */
.seg { display: inline-flex; background: #F1F4F8; border-radius: 9px; padding: 2px; gap: 2px; }
.seg button {
  border: none; background: transparent; padding: 4px 10px; border-radius: 7px;
  font-size: 11.5px; font-weight: 700; color: var(--muted); cursor: pointer; white-space: nowrap;
}
.seg button:hover { color: var(--navy); }
.seg button.active { background: #fff; color: var(--navy); box-shadow: 0 1px 3px rgba(20,49,79,.14); }
.card > h3 .h3-right { display: flex; align-items: center; gap: 10px; }
