:root {
  --bg: #f3f5f8;
  --surface: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --brand: #0f766e;
  --brand-2: #14b8a6;
  --brand-soft: #ccfbf1;
  --danger: #b91c1c;
  --danger-bg: #fef2f2;
  --warn: #b45309;
  --warn-bg: #fffbeb;
  --ok: #047857;
  --ok-bg: #ecfdf5;
  --sidebar: #0b1220;
  --sidebar-ink: #e2e8f0;
  --sidebar-muted: #94a3b8;
  --shadow: 0 10px 30px rgba(15, 23, 42, 0.06);
  --radius: 16px;
  --font: "Noto Sans Thai", "Sarabun", "Sukhumvit Set", "Leelawadee UI", "Segoe UI", Tahoma, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  min-height: 100vh;
}
a { color: inherit; }

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

.sidebar {
  width: 268px;
  background: var(--sidebar);
  color: var(--sidebar-ink);
  display: flex;
  flex-direction: column;
  padding: 22px 16px;
  flex-shrink: 0;
}
.brand { display: flex; gap: 12px; align-items: center; }
.brand-mark {
  width: 40px; height: 40px; border-radius: 12px;
  background: linear-gradient(180deg, var(--brand-2), var(--brand));
  color: #fff; display: grid; place-items: center;
  font-weight: 700; letter-spacing: 0.02em;
}
.brand-name { font-weight: 700; font-size: 1.05rem; }
.brand-sub { color: var(--sidebar-muted); font-size: 0.75rem; margin-top: 1px; }

.menu { margin-top: 28px; display: flex; flex-direction: column; gap: 6px; flex: 1; }
.menu-item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px; border-radius: 12px;
  text-decoration: none; color: var(--sidebar-ink);
}
.menu-item:hover { background: rgba(255,255,255,0.06); }
.menu-item.is-active {
  background: rgba(20, 184, 166, 0.16);
  color: #99f6e4;
}
.menu-icon { display: grid; place-items: center; width: 22px; opacity: 0.9; }
.badge-soon {
  margin-left: auto;
  font-size: 0.68rem;
  background: rgba(255,255,255,0.08);
  color: var(--sidebar-muted);
  border-radius: 999px;
  padding: 2px 8px;
  white-space: nowrap;
}

.sidebar-foot { border-top: 1px solid rgba(255,255,255,0.08); padding-top: 16px; }
.user-chip { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.user-avatar {
  width: 32px; height: 32px; border-radius: 50%;
  background: #134e4a; color: #99f6e4;
  display: grid; place-items: center; font-weight: 700;
}
.user-name { font-size: 0.92rem; }
.logout-link {
  display: inline-block;
  color: var(--sidebar-muted);
  text-decoration: none;
  font-size: 0.88rem;
}
.logout-link:hover { color: #fda4af; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 22px 28px 8px;
}
.topbar h1 { margin: 0; font-size: 1.45rem; letter-spacing: -0.02em; }
.content { padding: 16px 28px 32px; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.card h2 { margin: 0 0 8px; font-size: 1.05rem; }

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 16px;
}
.kpi-card { min-height: 118px; }
.kpi-label { color: var(--muted); font-size: 0.9rem; }
.kpi-value { font-size: 2rem; font-weight: 700; margin-top: 14px; letter-spacing: -0.03em; }

.chart-card { min-height: 380px; }
.chart-wrap { height: 300px; position: relative; }
.placeholder-note {
  margin: 0 0 12px;
  color: var(--muted);
  background: #f8fafc;
  border: 1px dashed var(--line);
  border-radius: 10px;
  padding: 10px 12px;
}

.status-badge {
  display: inline-flex; align-items: center; gap: 8px;
  border-radius: 999px; padding: 6px 12px;
  font-size: 0.88rem; font-weight: 600;
  border: 1px solid transparent;
}
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.status-ok { color: var(--ok); background: var(--ok-bg); border-color: #a7f3d0; }
.status-warn { color: var(--warn); background: var(--warn-bg); border-color: #fde68a; }
.status-err { color: var(--danger); background: var(--danger-bg); border-color: #fecaca; }

.coming-card { max-width: 640px; }
.coming-card p { color: var(--muted); }
.badge-soon-lg { background: #e2e8f0; color: #334155; padding: 4px 10px; }

.login-body {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background:
    radial-gradient(circle at top left, rgba(20,184,166,0.16), transparent 28%),
    linear-gradient(180deg, #0b1220 0%, #111827 100%);
}
.login-card {
  width: min(420px, calc(100vw - 32px));
  background: var(--surface);
  border-radius: 20px;
  padding: 28px;
  box-shadow: 0 24px 60px rgba(0,0,0,0.25);
}
.login-brand { margin-bottom: 22px; }
.login-brand .brand-sub { color: var(--muted); }
.login-card h1 { margin: 0 0 6px; font-size: 1.4rem; }
.login-lead { margin: 0 0 20px; color: var(--muted); }
.login-card label { display: block; font-size: 0.88rem; margin: 0 0 6px; color: #334155; }
.login-card input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 11px 12px;
  margin-bottom: 14px;
  font: inherit;
  background: #fff;
}
.login-card input:focus {
  outline: 2px solid rgba(20,184,166,0.35);
  border-color: var(--brand-2);
}
.btn-primary {
  width: 100%;
  border: 0;
  border-radius: 12px;
  padding: 12px 14px;
  background: linear-gradient(180deg, #14b8a6, #0f766e);
  color: #fff;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  margin-top: 6px;
}
.btn-primary:hover { filter: brightness(1.05); }
.alert { border-radius: 12px; padding: 10px 12px; margin-bottom: 16px; }
.alert-error { background: var(--danger-bg); color: var(--danger); }

/* Prevent flex children from forcing horizontal overflow */
html, body { max-width: 100%; overflow-x: hidden; }
.app-body, .main, .content, .section, .card, .table-wrap { min-width: 0; }
.main { max-width: 100%; }

.nav-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  min-width: 40px;
  min-height: 40px;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
  flex-shrink: 0;
}
.nav-overlay { display: none; }

.menu-item { min-height: 40px; }
.logout-link {
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  padding: 8px 4px;
}
.btn-primary, .btn-secondary {
  min-height: 40px;
  min-width: 40px;
}
.login-card input, .search-label input {
  min-height: 40px;
}

.table-wrap {
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  max-width: 100%;
  max-height: min(70vh, 640px);
}
.data-table thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: #f8fafc;
  box-shadow: 0 1px 0 var(--line);
}
.data-table th, .data-table td { min-height: 40px; }

@media (max-width: 1100px) {
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 900px) {
  .nav-toggle { display: inline-flex; }
  .nav-overlay {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.5);
    z-index: 25;
  }
  .nav-overlay[hidden] { display: none !important; }
  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(280px, 86vw);
    height: 100%;
    transform: translateX(-110%);
    transition: transform 0.2s ease;
    z-index: 30;
    box-shadow: 12px 0 30px rgba(0,0,0,0.18);
  }
  .nav-open .sidebar { transform: translateX(0); }
  .topbar, .content { padding-left: 16px; padding-right: 16px; }
  .topbar { gap: 10px; }
  .topbar h1 { font-size: 1.15rem; }
  .kpi-grid { grid-template-columns: 1fr; }
  .kpi-card-lg { max-width: 100%; width: 100%; }
  .section-head { align-items: flex-start; }
  .section-tools { width: 100%; justify-content: space-between; }
  .modal-dialog {
    width: 100vw;
    height: 100vh;
    max-height: 100vh;
    margin: 0;
    border-radius: 0;
  }
  .modal-body { flex: 1; min-height: 0; }
  .json-view { overflow: auto; max-width: 100%; }
  .search-label { width: 100%; }
  .search-label input { min-width: 0; width: 100%; }
  .login-body { padding: 16px; }
  .login-card { width: min(420px, 100%); }
}

@media (max-width: 600px) {
  /* S1: stack table rows into cards on narrow phones */
  #section-s1 .data-table thead { display: none; }
  #section-s1 .data-table,
  #section-s1 .data-table tbody,
  #section-s1 .data-table tr,
  #section-s1 .data-table td {
    display: block;
    width: 100%;
  }
  #section-s1 .table-wrap { max-height: none; overflow: visible; }
  #section-s1 .data-table tr {
    border-bottom: 1px solid var(--line);
    padding: 10px 8px 12px;
  }
  #section-s1 .data-table td {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    border-bottom: 0;
    padding: 6px 4px;
    text-align: right;
  }
  #section-s1 .data-table td::before {
    content: attr(data-label);
    font-weight: 600;
    color: var(--muted);
    text-align: left;
    flex: 0 0 36%;
  }
  #section-s1 .data-table td.num { text-align: right; }
  #section-s1 .data-table .table-empty { text-align: center; display: block; }
  .status-badge { max-width: 100%; white-space: normal; }
}

.section { margin-bottom: 28px; }
.section-head {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.section-head h2 { margin: 0; font-size: 1.12rem; flex: 1; }
.section-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 36px;
  padding: 3px 8px;
  border-radius: 999px;
  background: #0f766e;
  color: #fff;
  font-weight: 700;
  font-size: 0.78rem;
  letter-spacing: 0.04em;
}
.section-tools { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.section-meta { color: var(--muted); font-size: 0.82rem; }
.btn-secondary {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  border-radius: 10px;
  padding: 7px 12px;
  font: inherit;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
}
.btn-secondary:hover { background: #f8fafc; }
.btn-secondary.is-current, .btn-secondary:disabled { opacity: 0.6; cursor: default; }

.kpi-card-lg { max-width: 420px; min-height: 150px; }
.kpi-updated { margin-top: 10px; color: var(--muted); font-size: 0.86rem; }

.table-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}
.search-label { color: var(--muted); font-size: 0.86rem; display: flex; align-items: center; gap: 8px; }
.search-label input {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 7px 10px;
  font: inherit;
  min-width: 220px;
}
.row-count { color: var(--muted); font-size: 0.86rem; }
.table-wrap { overflow: auto; border: 1px solid var(--line); border-radius: 12px; }
.data-table { width: 100%; border-collapse: collapse; font-size: 0.92rem; }
.data-table th, .data-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}
.data-table th { background: #f8fafc; font-weight: 600; white-space: nowrap; }
.data-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.data-table tr.is-clickable { cursor: pointer; }
.data-table tr.is-clickable:hover { background: #f0fdfa; }
.table-empty { text-align: center; color: var(--muted); padding: 20px 10px; }
.group-band-0 { background: #fff; }
.group-band-1 { background: #f8fafc; }
.process-table td .status-badge { font-weight: 600; }
.status-neutral { color: #334155; background: #f1f5f9; border-color: #e2e8f0; }

.pager { display: flex; align-items: center; gap: 8px; }
.pager-info { color: var(--muted); font-size: 0.86rem; }

.modal[hidden] { display: none; }
.modal { position: fixed; inset: 0; z-index: 40; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(15, 23, 42, 0.5); }
.modal-dialog {
  position: relative;
  z-index: 41;
  width: min(960px, calc(100vw - 32px));
  max-height: calc(100vh - 40px);
  margin: 20px auto;
  background: #fff;
  border-radius: 16px;
  display: flex;
  flex-direction: column;
  box-shadow: 0 24px 60px rgba(0,0,0,0.25);
}
.modal-head, .modal-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.modal-head { justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.modal-head h3 { margin: 0; font-size: 1.05rem; }
.modal-body { overflow: auto; padding: 16px; }

.json-view {
  margin: 0;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 13px;
  line-height: 1.55;
  color: var(--ink);
  white-space: pre-wrap;
  word-break: break-word;
}
.json-key { color: #0f766e; }
.json-string { color: #9a3412; }
.json-number { color: #1d4ed8; }
.json-bool { color: #7c3aed; }
.json-null { color: #64748b; }
.json-punct { color: #475569; }
.json-toggle {
  border: 0;
  background: transparent;
  color: #0f766e;
  cursor: pointer;
  padding: 0 4px 0 0;
  font: inherit;
}
.json-children { margin-left: 1.25em; }
.json-collapsed { color: var(--muted); }

/* S5 SQL runner */
.sql-label { display: block; font-weight: 600; margin-bottom: 8px; }
.sql-input {
  width: 100%;
  min-height: 160px;
  max-width: 100%;
  resize: vertical;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 13px;
  line-height: 1.5;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  background: #0b1220;
  color: #e2e8f0;
  box-sizing: border-box;
}
.sql-input:focus {
  outline: 2px solid rgba(20,184,166,0.35);
  border-color: var(--brand-2);
}
.sql-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin: 12px 0 8px;
}
.sql-run.btn-primary,
.sql-actions .btn-primary {
  width: auto;
  min-width: 96px;
  margin-top: 0;
}
.sql-history-label {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.86rem;
  margin-left: auto;
}
.sql-history-label select {
  min-height: 40px;
  min-width: 180px;
  max-width: min(420px, 100%);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 6px 10px;
  font: inherit;
  background: #fff;
}
.sql-hint { margin: 0 0 12px; color: var(--muted); font-size: 0.86rem; }
.sql-result-toolbar { margin-top: 8px; }
.sql-null {
  color: #94a3b8;
  font-style: italic;
  background: #f1f5f9;
  border-radius: 6px;
  padding: 1px 6px;
  font-size: 0.85em;
}
.sql-json-btn {
  border: 1px solid var(--line);
  background: #f0fdfa;
  color: #0f766e;
  border-radius: 8px;
  min-height: 40px;
  padding: 6px 10px;
  font: inherit;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  cursor: pointer;
  text-align: left;
  max-width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sql-json-btn:hover { background: #ccfbf1; }
.truncated-note { color: var(--warn); font-size: 0.86rem; }

@media (max-width: 900px) {
  .sql-history-label { margin-left: 0; width: 100%; }
  .sql-history-label select { width: 100%; max-width: none; }
  .sql-input { min-height: 140px; font-size: 16px; } /* avoid iOS zoom */
  .sql-actions .btn-primary,
  .sql-actions .btn-secondary { min-height: 40px; }
}

.kpi-history {
  margin-top: 14px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
}
.kpi-history-title {
  color: var(--muted);
  font-size: 0.8rem;
  margin-bottom: 6px;
}
.kpi-history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 4px;
  font-size: 0.82rem;
}
.kpi-history-item {
  display: flex;
  align-items: baseline;
  gap: 8px;
  flex-wrap: wrap;
}
.kpi-history-time {
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}
.kpi-history-tag {
  font-size: 0.7rem;
  padding: 1px 6px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
  white-space: nowrap;
}
.kpi-history-tag.is-live { color: var(--ok); border-color: var(--ok); background: var(--ok-bg); }
.kpi-history-tag.is-cached { color: var(--muted); }
.kpi-history-detail { margin-left: auto; color: var(--muted); }
.kpi-history-item.is-error .kpi-history-time { color: var(--danger); }
.kpi-history-empty { color: var(--muted); font-size: 0.82rem; }
@media (max-width: 640px) {
  .kpi-history-item { row-gap: 2px; }
  .kpi-history-detail { margin-left: 0; }
}

/* ---- S2: KPI card with history + chart ---- */
.kpi-card-wide { max-width: 100%; width: 100%; }
.kpi-layout { display: block; }
.kpi-layout-split {
  display: grid;
  grid-template-columns: minmax(260px, 400px) minmax(0, 1fr);
  gap: 24px;
  align-items: start;
}
.kpi-col { min-width: 0; }
.kpi-chart-col { border-left: 1px solid var(--line); padding-left: 22px; }
.kpi-chart-title {
  display: flex;
  align-items: baseline;
  gap: 8px;
  flex-wrap: wrap;
  color: var(--muted);
  font-size: 0.8rem;
  margin-bottom: 8px;
}
.kpi-chart-badge {
  font-size: 0.7rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 1px 7px;
  white-space: nowrap;
}
.kpi-chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.kpi-chart-empty {
  margin: 0;
  padding: 30px 0;
  text-align: center;
  color: var(--muted);
  font-size: 0.82rem;
}
.kpi-chart-note {
  margin-top: 6px;
  color: var(--muted);
  font-size: 0.76rem;
  font-variant-numeric: tabular-nums;
}
.kpi-chart .spark-grid { stroke: var(--line); stroke-width: 1; }
.kpi-chart .spark-area { fill: var(--brand-soft); stroke: none; }
.kpi-chart .spark-line { fill: none; stroke: var(--brand); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.kpi-chart .spark-dot { fill: var(--surface); stroke: var(--brand); stroke-width: 1.6; }
.kpi-chart .spark-dot.is-last { fill: var(--brand); stroke: var(--surface); stroke-width: 2; }
.kpi-chart .spark-ytick,
.kpi-chart .spark-xtick { fill: var(--muted); font-size: 10px; font-family: var(--font); }
@media (max-width: 900px) {
  .kpi-card-wide { max-width: 100%; width: 100%; }
  .kpi-layout-split { grid-template-columns: 1fr; gap: 14px; }
  .kpi-chart-col {
    border-left: 0;
    padding-left: 0;
    border-top: 1px solid var(--line);
    padding-top: 12px;
  }
  .kpi-chart .spark-ytick,
  .kpi-chart .spark-xtick { font-size: 11px; }
}
