/* หน้าเว็บจัดการ pss-gateway — ธีม Emerald (rebrand) · sidebar layout · light/dark ตาม prefers-color-scheme
   โทเคนจาก design handoff (tokens.css) + alias ชื่อตัวแปรเดิมให้ชี้มาที่โทเคนใหม่ เพื่อไม่ต้องแก้ selector ที่ app.js อ้างถึง */
@import url("https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;500;600&family=IBM+Plex+Sans+Thai:wght@400;500;600;700&display=swap");

:root {
  color-scheme: light;
  --font: "IBM Plex Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, Consolas, monospace;

  --bg: #f5f7f6;
  --surface: #ffffff;
  --surface-2: #eef2f0;
  --line: #dfe5e2;
  --ink: #0e1915;
  --ink-2: #39443f;
  --muted: #56635e;

  --accent: #0a7a5a;
  --accent-ink: #ffffff;
  --accent-soft: #e1f2ea;
  --accent-strong: #075c44;

  --sidebar: #0d2620;
  --sidebar-ink: #cfe3da;
  --sidebar-active: rgba(52, 194, 143, 0.16);
  --sidebar-icon-active: #34c28f;

  --good: #075c44;  --good-bg: #e1f2ea;
  --warn: #7a4a00;  --warn-bg: #fcf1d9;
  --attn: #9e3709;  --attn-bg: #fde9de;
  --crit: #b42323;  --crit-bg: #fbe5e5;

  --series-1: #0a7a5a; /* Ksher */
  --series-2: #1f4fa8; /* BBL ThaiQR */
  --series-3: #6b4fd8; /* Stripe PromptPay */

  --radius-sm: 6px;
  --radius: 10px;
  --radius-lg: 16px;
  --radius-xl: 20px;
  --shadow: 0 1px 2px rgba(14, 25, 21, 0.04), 0 6px 20px rgba(14, 25, 21, 0.04);
  --shadow-modal: 0 24px 64px rgba(0, 0, 0, 0.25);
  --sidebar-w: 232px;

  /* ---- alias ชื่อเดิม → โทเคนใหม่ (selector เดิมใน style/app.js ยังทำงาน) ---- */
  --page: var(--bg);
  --border: var(--line);
  --grid: var(--line);
  --baseline: var(--line);
  --neutral-bg: var(--surface-2);
  --accent-tint: var(--accent-soft);
  --good-text: var(--good);
  --warning: var(--warn);
  --warning-bg: var(--warn-bg);
  --serious: var(--attn);
  --critical: var(--crit);
  --critical-bg: var(--crit-bg);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0a0f0d;
    --surface: #121a17;
    --surface-2: #18221e;
    --line: #24312c;
    --ink: #e6eeea;
    --ink-2: #b9ccc4;
    --muted: #93a49d;

    --accent: #34c28f;
    --accent-ink: #04140e;
    --accent-soft: #12352a;
    --accent-strong: #5fd6aa;

    --sidebar: #070b0a;
    --sidebar-ink: #b9ccc4;
    --sidebar-active: rgba(52, 194, 143, 0.18);
    --sidebar-icon-active: #34c28f;

    --good: #5fd6aa;  --good-bg: #12352a;
    --warn: #f2b84b;  --warn-bg: #33270e;
    --attn: #f49a6b;  --attn-bg: #3a2015;
    --crit: #f07a7a;  --crit-bg: #3a1717;

    --series-1: #34c28f;
    --series-2: #7fa6f0;
    --series-3: #a996f2;

    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 6px 20px rgba(0, 0, 0, 0.35);
    --shadow-modal: 0 24px 64px rgba(0, 0, 0, 0.55);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; cursor: pointer; }
input, select, textarea { font: inherit; color: var(--ink); }
.num, .mono, td.num, td.amount, code, .kv dd { font-variant-numeric: tabular-nums; }

/* ---- shell: sidebar ซ้าย + เนื้อหาขวา ---- */
.shell { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--sidebar);
  color: var(--sidebar-ink);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  padding: 18px 12px 14px;
}

.brand { display: flex; align-items: center; gap: 11px; padding: 4px 8px 18px; }
.brand-mark {
  width: 32px; height: 32px; flex: 0 0 32px;
  display: flex; align-items: center; justify-content: center;
}
.brand-mark svg { width: 32px; height: 32px; display: block; }
.brand-name { font-weight: 700; font-size: 15px; letter-spacing: 0.1px; color: #fff; }
.brand-sub { font-size: 11px; color: var(--sidebar-ink); opacity: 0.7; margin-top: 1px; }

.side-nav { display: flex; flex-direction: column; gap: 3px; flex: 1; }
.side-nav button {
  display: flex; align-items: center; gap: 12px;
  width: 100%; text-align: left;
  background: none; border: none; color: var(--sidebar-ink);
  padding: 0 12px; height: 44px; border-radius: var(--radius);
  font-size: 13.5px; font-weight: 500;
  transition: background 0.12s, color 0.12s;
}
.side-nav button svg { width: 18px; height: 18px; flex: 0 0 18px; opacity: 0.75; }
.side-nav button:hover { background: rgba(255, 255, 255, 0.06); color: #fff; }
.side-nav button.active { background: var(--sidebar-active); color: #fff; font-weight: 600; }
.side-nav button.active svg { opacity: 1; color: var(--sidebar-icon-active); }

.side-foot { border-top: 1px solid rgba(255, 255, 255, 0.1); padding-top: 12px; }
.user-chip { display: flex; align-items: center; gap: 10px; padding: 0 6px; }
.avatar {
  width: 32px; height: 32px; flex: 0 0 32px; border-radius: 50%;
  background: var(--sidebar-active); color: var(--sidebar-icon-active);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 14px;
}
.user-meta { display: flex; flex-direction: column; min-width: 0; }
.user-meta #who {
  font-size: 13px; font-weight: 600; color: #fff;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.user-meta .hint { color: var(--sidebar-ink); opacity: 0.65; }
.linkbtn {
  background: none; border: none; color: var(--sidebar-ink); opacity: 0.75; padding: 0;
  font-size: 12px; text-align: left; text-decoration: none;
}
.linkbtn:hover { color: #fff; opacity: 1; text-decoration: underline; }

main { flex: 1; min-width: 0; max-width: 1240px; padding: 32px 40px 48px; }
section.view { display: none; }
section.view.active { display: block; }

.page-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; flex-wrap: wrap; margin-bottom: 20px;
}
.page-head h2 { margin: 0; }

.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
h2 { font-size: 28px; font-weight: 700; letter-spacing: -0.01em; margin: 0 0 4px; }
h3 { font-size: 15px; margin: 0 0 12px; color: var(--ink); font-weight: 600; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow);
}

/* ---- stat tiles ---- */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(185px, 1fr)); gap: 14px; margin-bottom: 18px; }
.tile {
  position: relative;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-lg);
  padding: 18px 20px; box-shadow: var(--shadow);
}
.tile .label { color: var(--muted); font-size: 12px; font-weight: 600; letter-spacing: 0.02em; }
.tile .value { font-family: var(--mono); font-size: 28px; font-weight: 600; margin-top: 6px; letter-spacing: -0.02em; }
.tile .value .unit { font-size: 13px; font-weight: 400; color: var(--muted); letter-spacing: 0; font-family: var(--font); }
.tile .note { font-size: 12px; color: var(--muted); margin-top: 4px; }
.tile::after {
  content: ""; position: absolute; top: 18px; right: 18px;
  width: 8px; height: 8px; border-radius: 50%; background: var(--accent); opacity: 0.55;
}
.tile.warn::after { background: var(--attn); opacity: 1; }
.tile.bad::after { background: var(--crit); opacity: 1; }
.tile.warn .value { color: var(--attn); }
.tile.bad .value { color: var(--crit); }

/* ---- chart ---- */
.chart-wrap { position: relative; }
.chart-wrap svg { display: block; width: 100%; height: auto; }
#chart-tooltip {
  position: fixed; pointer-events: none; z-index: 50; display: none;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow-modal);
  padding: 9px 11px; font-size: 12px; min-width: 130px;
}
#chart-tooltip .t-day { color: var(--muted); }
#chart-tooltip .t-val { font-weight: 600; font-size: 14px; font-family: var(--mono); }

.breakdowns { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 14px; margin-top: 18px; }

/* ---- tables ---- */
.table-scroll { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; }
th {
  text-align: left; color: var(--muted); font-size: 12px; font-weight: 600;
  letter-spacing: 0.02em;
  padding: 10px 12px; background: var(--surface-2); border-bottom: 1px solid var(--line); white-space: nowrap;
}
thead th:first-child { border-top-left-radius: var(--radius-sm); }
thead th:last-child { border-top-right-radius: var(--radius-sm); }
td { padding: 12px; border-top: 1px solid var(--line); white-space: nowrap; }
tbody tr:first-child td { border-top: none; }
td.num, th.num, td.amount { text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums; }
tbody tr:hover { background: var(--surface-2); }
tbody tr.clickable:hover { background: var(--surface-2); cursor: pointer; }
.empty { color: var(--muted); text-align: center; padding: 32px 0; }

/* ---- badges (icon + label — ไม่พึ่งสีอย่างเดียว) ---- */
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  border-radius: 999px; padding: 0 10px; height: 24px; font-size: 12px; font-weight: 600;
}
.badge.paid { background: var(--good-bg); color: var(--good); }
.badge.pending { background: var(--warn-bg); color: var(--warn); }
.badge.failed { background: var(--crit-bg); color: var(--crit); }
.badge.err { background: var(--attn-bg); color: var(--attn); }      /* จ่ายแล้ว · รอปิดงาน */
.badge.warnb { background: var(--warn-bg); color: var(--warn); }
.badge.neutral { background: var(--surface-2); color: var(--muted); }

/* ---- provider badge (มีจุดสีต่อเจ้า) ---- */
.provider {
  display: inline-flex; align-items: center; gap: 6px; height: 22px; padding: 0 9px;
  border: 1px solid var(--line); border-radius: var(--radius-sm);
  font: 500 11px var(--mono); text-transform: uppercase; color: var(--ink-2); letter-spacing: 0.02em;
}
.provider::before { content: ""; width: 7px; height: 7px; border-radius: 2px; background: var(--series-1); }
.provider.bbl::before { background: var(--series-2); }
.provider.stripe::before { background: var(--series-3); }

/* ---- forms / filters ---- */
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 18px; align-items: center; }
input[type="text"], input[type="password"], input[type="date"], select, textarea {
  background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 9px 12px; height: 40px; outline: none; color: var(--ink);
  transition: border-color 0.12s, box-shadow 0.12s;
}
textarea { height: auto; }
input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
textarea { width: 100%; font-family: var(--mono); font-size: 12px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  background: var(--accent); color: var(--accent-ink); border: 1px solid transparent; border-radius: var(--radius);
  padding: 0 18px; height: 40px; font-weight: 600;
  transition: filter 0.12s, background 0.12s;
}
.btn:hover { filter: brightness(1.06); }
.btn:disabled { opacity: 0.45; cursor: default; filter: none; }
.btn.secondary { background: var(--surface); color: var(--ink); border-color: var(--line); font-weight: 500; }
.btn.secondary:hover { background: var(--surface-2); filter: none; }
.btn.danger { background: var(--crit-bg); color: var(--crit); border-color: var(--crit-bg); }
.btn.del { background: none; border-color: transparent; color: var(--crit); padding: 0 10px; }
.btn.del:hover { background: var(--crit-bg); filter: none; }

.pager { display: flex; gap: 8px; align-items: center; justify-content: flex-end; margin-top: 14px; color: var(--muted); font-size: 13px; }

/* ---- login (split: panel เขียว + การ์ด) ---- */
#login-view {
  position: fixed; inset: 0; z-index: 100; display: none;
  align-items: center; justify-content: center; background: var(--bg); padding: 24px;
}
#login-view.active { display: flex; }
.login-split {
  display: grid; grid-template-columns: 1.1fr 1fr; width: 100%; max-width: 860px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-xl);
  overflow: hidden; box-shadow: var(--shadow-modal);
}
.login-aside {
  background: var(--sidebar); color: #fff; padding: 40px 36px;
  display: flex; flex-direction: column; gap: 18px;
}
.login-aside .brand { padding: 0 0 6px; }
.login-aside .brand-name { font-size: 16px; }
.login-aside .brand-sub { color: #cfe3da; opacity: 0.8; }
.login-head { font-size: 30px; line-height: 1.2; font-weight: 700; letter-spacing: -0.01em; margin: 8px 0 0; }
.login-tag { color: #cfe3da; opacity: 0.85; font-size: 13.5px; margin: 0; }
.login-chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: auto; }
.login-chips span {
  display: inline-flex; align-items: center; gap: 6px; height: 28px; padding: 0 12px;
  border-radius: 999px; background: rgba(255, 255, 255, 0.08); color: #e6fff4;
  font: 500 11.5px var(--mono); text-transform: uppercase; letter-spacing: 0.04em;
}
.login-card { border: none; box-shadow: none; border-radius: 0; padding: 40px 36px; display: flex; flex-direction: column; }
.login-card-head { font-size: 18px; font-weight: 600; margin-bottom: 18px; }
.brand-login { display: none; }
#login-view label { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin: 12px 0 4px; }
#login-view input { width: 100%; }
#oidc-btn { display: flex; width: 100%; gap: 8px; text-align: center; text-decoration: none; height: 48px; }
#login-error { color: var(--crit); font-size: 13px; margin-top: 12px; min-height: 18px; }
.login-divider {
  display: flex; align-items: center; gap: 10px;
  color: var(--muted); font-size: 12px; margin: 18px 0 4px;
}
.login-divider::before, .login-divider::after {
  content: ""; flex: 1; border-top: 1px solid var(--line);
}

/* ---- modal ---- */
#modal-backdrop {
  position: fixed; inset: 0; z-index: 60; display: none;
  background: rgba(6, 14, 11, 0.48); align-items: flex-start; justify-content: center;
  overflow-y: auto; padding: 44px 16px;
}
#modal-backdrop.active { display: flex; }
#modal {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-xl);
  width: 100%; max-width: 580px; padding: 24px;
  box-shadow: var(--shadow-modal);
}
#modal h2 { margin-top: 0; font-size: 18px; }
#modal h3 { margin-top: 14px; }
.kv { display: grid; grid-template-columns: 140px 1fr; gap: 6px 14px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
pre.meta {
  background: var(--surface-2); border-radius: var(--radius); padding: 12px;
  font-size: 12px; font-family: var(--mono); overflow-x: auto; max-height: 220px; color: var(--ink-2);
}
fieldset {
  border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin: 14px 0;
}
fieldset legend { font-size: 13px; font-weight: 600; padding: 0 6px; }
fieldset label { display: block; font-size: 12px; color: var(--muted); margin: 10px 0 3px; }
fieldset input[type="text"], fieldset input[type="password"] { width: 100%; }
.hint { font-size: 12px; color: var(--muted); }
code { background: var(--surface-2); padding: 1px 6px; border-radius: 5px; font-size: 12px; font-family: var(--mono); color: var(--ink-2); }
pre.meta code { background: none; padding: 0; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; flex-wrap: wrap; }
#modal-msg { min-height: 18px; font-size: 13px; }
#modal-msg.ok { color: var(--good); }
#modal-msg.err { color: var(--crit); }
a { color: var(--accent); }

.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); z-index: 200;
  background: var(--ink); color: var(--bg); border-radius: var(--radius); padding: 11px 18px; font-size: 13px;
  opacity: 0; transition: opacity 0.25s; pointer-events: none;
}
.toast.show { opacity: 1; }

/* ---- จอแคบ: sidebar เป็นแถบบน ---- */
@media (max-width: 860px) {
  .login-split { grid-template-columns: 1fr; max-width: 420px; }
  .login-aside { padding: 28px 28px; }
  .login-head { font-size: 24px; }
  .login-chips { margin-top: 6px; }
}
@media (max-width: 800px) {
  .shell { flex-direction: column; }
  .sidebar {
    width: 100%; flex: none; height: auto; position: sticky; top: 0; z-index: 30;
    flex-direction: row; align-items: center; gap: 8px;
    padding: 10px 14px;
  }
  .brand { padding: 0 4px; }
  .brand-sub { display: none; }
  .side-nav { flex-direction: row; overflow-x: auto; gap: 2px; }
  .side-nav button { white-space: nowrap; height: 38px; padding: 0 12px; }
  .side-nav button svg { display: none; }
  .side-foot { border-top: none; padding-top: 0; margin-left: auto; }
  .user-meta #who, .user-meta .hint { display: none; }
  main { padding: 20px 16px 36px; }
  h2 { font-size: 23px; }
}
