/* ==========================================================================
   CPS App — design system
   สี/ระยะ/มุมโค้ง ทั้งหมดเป็น token ด้านล่าง  (โหมดมืด: html[data-bs-theme="dark"])
   ========================================================================== */
:root {
  --sw: 252px;                    /* sidebar width */
  --font: 'Inter', 'IBM Plex Sans Thai', system-ui, -apple-system, 'Segoe UI', sans-serif;

  --bg:        #f5f6f8;
  --surface:   #ffffff;
  --surface-2: #f8f9fb;           /* หัวตาราง, ช่อง readonly */
  --surface-3: #eef0f4;           /* hover */
  --border:    #e5e7ec;
  --border-2:  #d5d9e1;           /* เส้นของ input */
  --text:      #111827;
  --text-2:    #4b5563;
  --muted:     #8a93a3;


  /* สถานะ (ใช้คู่กับข้อความเสมอ) */
  --ok-bg:    #e9f8f0; --ok-fg:    #087443;
  --warn-bg:  #fff5e1; --warn-fg:  #9a5b00;
  --bad-bg:   #fdecec; --bad-fg:   #b42318;
  --info-bg:  #e9f1ff; --info-fg:  #1d4ed8;
  --violet-bg:#f2edff; --violet-fg:#6d28d9;
  --mute-bg:  #f0f1f4; --mute-fg:  #5b6474;

  --r-sm: 8px; --r: 12px; --r-lg: 16px;
  --sh-1: 0 1px 2px rgba(16, 24, 40, .05);
  --sh-2: 0 6px 20px -6px rgba(16, 24, 40, .12);
  --sh-3: 0 20px 48px -12px rgba(16, 24, 40, .25);
}
html[data-bs-theme="dark"] {
  --bg:        #0f1115;
  --surface:   #171a21;
  --surface-2: #1d2129;
  --surface-3: #252a34;
  --border:    #2a2f3a;
  --border-2:  #363c49;
  --text:      #eceef2;
  --text-2:    #b4bac6;
  --muted:     #7f8796;


  --ok-bg:    #0f2a1e; --ok-fg:    #5fd39a;
  --warn-bg:  #2e2310; --warn-fg:  #f2b54b;
  --bad-bg:   #331717; --bad-fg:   #f48a83;
  --info-bg:  #14233f; --info-fg:  #86b0ff;
  --violet-bg:#251b3d; --violet-fg:#bda4ff;
  --mute-bg:  #232833; --mute-fg:  #a2aab8;

  --sh-1: 0 1px 2px rgba(0, 0, 0, .3);
  --sh-2: 0 8px 24px -8px rgba(0, 0, 0, .5);
}

/* ==========================================================================
   โทนสี 10 แบบ — แต่ละแบบมีเฉด 50 (อ่อนสุด) → 900 (เข้มสุด)
   ใส่ data-accent="..." ที่ <html> เพื่อเลือกโทน (ใส่ที่ element อื่นได้ด้วย เช่น ตัวอย่างสีในเมนู)
   ========================================================================== */
:root, [data-accent="indigo"] {
  --p-50:#eef2ff; --p-100:#e0e7ff; --p-200:#c7d2fe; --p-300:#a5b4fc; --p-400:#818cf8;
  --p-500:#6366f1; --p-600:#4f46e5; --p-700:#4338ca; --p-800:#3730a3; --p-900:#312e81;
}
[data-accent="blue"] {
  --p-50:#eff6ff; --p-100:#dbeafe; --p-200:#bfdbfe; --p-300:#93c5fd; --p-400:#60a5fa;
  --p-500:#3b82f6; --p-600:#2563eb; --p-700:#1d4ed8; --p-800:#1e40af; --p-900:#1e3a8a;
}
[data-accent="sky"] {
  --p-50:#f0f9ff; --p-100:#e0f2fe; --p-200:#bae6fd; --p-300:#7dd3fc; --p-400:#38bdf8;
  --p-500:#0ea5e9; --p-600:#0284c7; --p-700:#0369a1; --p-800:#075985; --p-900:#0c4a6e;
}
[data-accent="teal"] {
  --p-50:#f0fdfa; --p-100:#ccfbf1; --p-200:#99f6e4; --p-300:#5eead4; --p-400:#2dd4bf;
  --p-500:#14b8a6; --p-600:#0d9488; --p-700:#0f766e; --p-800:#115e59; --p-900:#134e4a;
}
[data-accent="emerald"] {
  --p-50:#ecfdf5; --p-100:#d1fae5; --p-200:#a7f3d0; --p-300:#6ee7b7; --p-400:#34d399;
  --p-500:#10b981; --p-600:#059669; --p-700:#047857; --p-800:#065f46; --p-900:#064e3b;
}
[data-accent="orange"] {
  --p-50:#fff7ed; --p-100:#ffedd5; --p-200:#fed7aa; --p-300:#fdba74; --p-400:#fb923c;
  --p-500:#f97316; --p-600:#ea580c; --p-700:#c2410c; --p-800:#9a3412; --p-900:#7c2d12;
}
[data-accent="rose"] {
  --p-50:#fff1f2; --p-100:#ffe4e6; --p-200:#fecdd3; --p-300:#fda4af; --p-400:#fb7185;
  --p-500:#f43f5e; --p-600:#e11d48; --p-700:#be123c; --p-800:#9f1239; --p-900:#881337;
}
[data-accent="violet"] {
  --p-50:#f5f3ff; --p-100:#ede9fe; --p-200:#ddd6fe; --p-300:#c4b5fd; --p-400:#a78bfa;
  --p-500:#8b5cf6; --p-600:#7c3aed; --p-700:#6d28d9; --p-800:#5b21b6; --p-900:#4c1d95;
}
[data-accent="coffee"] {
  --p-50:#fbf6f1; --p-100:#f3e8dc; --p-200:#e6d0b8; --p-300:#d4b08c; --p-400:#c08d62;
  --p-500:#a87145; --p-600:#8b5a33; --p-700:#714829; --p-800:#573822; --p-900:#3d2817;
}
[data-accent="slate"] {
  --p-50:#f8fafc; --p-100:#f1f5f9; --p-200:#e2e8f0; --p-300:#cbd5e1; --p-400:#94a3b8;
  --p-500:#64748b; --p-600:#475569; --p-700:#334155; --p-800:#1e293b; --p-900:#0f172a;
}

/* เฉดไหนใช้ทำอะไร — โหมดสว่าง: ปุ่ม 600, hover 700, พื้นอ่อน 50, ตัวหนังสือบนพื้นอ่อน 800 */
:root {
  --a-main: var(--p-600); --a-hover: var(--p-700);
  --accent:      var(--a-main);
  --accent-600:  var(--a-hover);
  --accent-soft: var(--p-50);
  --accent-ink:  var(--p-800);
  --ring:        color-mix(in srgb, var(--p-500) 22%, transparent);
  --brand-1: var(--p-400); --brand-2: var(--p-600); --brand-3: var(--p-900);
}
/* โทนที่สี 600 อ่อนเกินไปสำหรับตัวหนังสือขาว → ใช้ 700 เป็นสีปุ่ม */
html[data-accent="sky"], html[data-accent="teal"], html[data-accent="emerald"], html[data-accent="orange"] {
  --a-main: var(--p-700); --a-hover: var(--p-800);
}
/* โหมดมืด: ปุ่ม 400 (ตัวหนังสือเข้ม), hover 300, ตัวหนังสือ 200 */
html[data-bs-theme="dark"] {
  --accent:      var(--p-400);
  --accent-600:  var(--p-300);
  --accent-soft: color-mix(in srgb, var(--p-500) 18%, #171a21);
  --accent-ink:  var(--p-200);
  --ring:        color-mix(in srgb, var(--p-400) 30%, transparent);
  --brand-1: var(--p-500); --brand-2: var(--p-700); --brand-3: var(--p-900);
}

/* ตัวเลือกโทนสีในแถบบน */
.theme-menu { width: 340px; padding: 10px; }
.theme-menu .tm-title { font-size: .72rem; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); padding: 4px 6px 8px; }
.tm-item {
  display: grid; grid-template-columns: 26px 1fr 16px; align-items: center; gap: 10px;
  width: 100%; border: 0; background: none; padding: 7px 8px; border-radius: 9px; text-align: left; color: var(--text);
}
.tm-item:hover { background: var(--surface-3); }
.tm-dot { width: 26px; height: 26px; border-radius: 8px; background: linear-gradient(135deg, var(--p-400), var(--p-700)); }
.tm-name { font-size: .84rem; font-weight: 600; line-height: 1.2; }
.tm-shades { display: grid; grid-template-columns: repeat(10, 1fr); height: 8px; border-radius: 4px; overflow: hidden; margin-top: 5px; }
.tm-shades i { display: block; }
.tm-check { color: var(--accent); visibility: hidden; }
.tm-item.on .tm-check { visibility: visible; }
.tm-item.on { background: var(--accent-soft); }
.tm-mode { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-top: 8px; padding-top: 10px; border-top: 1px solid var(--border); }
.tm-mode button { border: 1px solid var(--border-2); background: var(--surface); color: var(--text-2); border-radius: 9px; padding: 7px; font-size: .8rem; font-weight: 600; }
.tm-mode button.on { border-color: var(--accent); color: var(--accent-ink); background: var(--accent-soft); }

/* Bootstrap ใช้ token ของเรา */
:root, html[data-bs-theme="dark"] {
  --bs-body-font-family: var(--font);
  --bs-body-bg: var(--bg);
  --bs-body-color: var(--text);
  --bs-border-color: var(--border);
  --bs-secondary-color: var(--muted);
  --bs-tertiary-bg: var(--surface-2);
  --bs-primary: var(--accent);
  --bs-link-color: var(--accent);
  --bs-link-hover-color: var(--accent-600);
  --bs-focus-ring-color: var(--ring);
}

*, *::before, *::after { box-sizing: border-box; }
body {
  font-family: var(--font); font-size: 14px; line-height: 1.55;
  background: var(--bg); color: var(--text); margin: 0;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
a:hover { color: var(--accent-600); }
::selection { background: var(--accent-soft); }
.text-muted { color: var(--muted) !important; }
.num { font-variant-numeric: tabular-nums; }

/* ── Sidebar ─────────────────────────────────────────────────────────── */
#sidebar {
  position: fixed; inset: 0 auto 0 0; width: var(--sw); z-index: 100;
  display: flex; flex-direction: column;
  background: var(--surface); border-right: 1px solid var(--border);
  transition: transform .25s cubic-bezier(.4, 0, .2, 1);
}
.brand { display: flex; align-items: center; gap: 11px; padding: 18px 18px 14px; }
.brand-icon {
  width: 36px; height: 36px; border-radius: 10px; flex-shrink: 0;
  display: grid; place-items: center; color: #fff; font-size: 17px;
  background: linear-gradient(135deg, var(--brand-1), var(--brand-2));
  box-shadow: 0 6px 14px -4px color-mix(in srgb, var(--brand-2) 60%, transparent);
}
.brand-text { font-weight: 700; font-size: .98rem; letter-spacing: -.2px; }
.brand-sub  { font-size: .72rem; color: var(--muted); margin-top: -1px; }

.nav-section { flex: 1; overflow-y: auto; padding: 6px 10px 12px; }
.nav-label {
  font-size: .68rem; font-weight: 600; letter-spacing: .08em; text-transform: uppercase;
  color: var(--muted); padding: 14px 10px 6px;
}
.nav-item {
  display: flex; align-items: center; gap: 11px;
  padding: 8px 10px; margin-bottom: 2px; border-radius: 9px;
  color: var(--text-2); text-decoration: none; font-weight: 500; font-size: .875rem;
  transition: background .12s, color .12s;
}
.nav-item i { font-size: 1.02rem; width: 20px; text-align: center; color: var(--muted); transition: color .12s; }
.nav-item:hover { background: var(--surface-3); color: var(--text); }
.nav-item:hover i { color: var(--text-2); }
.nav-item.active { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
.nav-item.active i { color: var(--accent); }

.sidebar-footer { padding: 12px; border-top: 1px solid var(--border); }
.sync-card {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 10px 12px; border-radius: 10px; font-size: .76rem;
  background: var(--warn-bg); color: var(--warn-fg);
}
.sync-card i { font-size: .95rem; margin-top: 1px; }
.sync-card strong { display: block; font-size: .78rem; }
.sync-card.bad { background: var(--bad-bg); color: var(--bad-fg); }

/* ── Main / Topbar ───────────────────────────────────────────────────── */
#main { margin-left: var(--sw); min-height: 100vh; display: flex; flex-direction: column; }
#topbar {
  position: sticky; top: 0; z-index: 50; height: 60px;
  display: flex; align-items: center; gap: 12px; padding: 0 28px;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: saturate(160%) blur(10px);
  border-bottom: 1px solid var(--border);
}
.crumb { font-size: .8rem; color: var(--muted); }
.crumb b { color: var(--text); font-weight: 600; font-size: .92rem; }
.icon-btn {
  width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center;
  border: 1px solid var(--border); background: var(--surface); color: var(--text-2);
  cursor: pointer; transition: background .12s, color .12s;
}
.icon-btn:hover { background: var(--surface-3); color: var(--text); }
.sidebar-toggle { display: none; }
.user-btn {
  display: flex; align-items: center; gap: 9px; padding: 4px 10px 4px 4px;
  border: 1px solid var(--border); border-radius: 40px; background: var(--surface);
  color: var(--text); cursor: pointer;
}
.user-btn:hover { background: var(--surface-3); }
.avatar {
  width: 28px; height: 28px; border-radius: 50%; display: grid; place-items: center;
  font-size: .75rem; font-weight: 700; color: #fff;
  background: linear-gradient(135deg, var(--brand-1), var(--brand-2));
}
.user-btn .name { font-size: .82rem; font-weight: 600; line-height: 1.1; }
.user-btn .role { font-size: .7rem; color: var(--muted); line-height: 1.1; }
.dropdown-menu {
  --bs-dropdown-bg: var(--surface); --bs-dropdown-border-color: var(--border);
  --bs-dropdown-link-color: var(--text); --bs-dropdown-link-hover-bg: var(--surface-3);
  --bs-dropdown-link-active-bg: var(--accent); border-radius: var(--r); box-shadow: var(--sh-2);
  padding: 6px; font-size: .86rem;
}
.dropdown-item { border-radius: 7px; padding: 7px 10px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px;
  border-radius: 999px; font-size: .74rem; font-weight: 600;
  background: var(--warn-bg); color: var(--warn-fg);
}

#content { flex: 1; padding: 26px 28px 28px; max-width: 1600px; width: 100%; }

/* ── Page header & toolbar ──────────────────────────────────────────── */
.page-header {
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: 16px; flex-wrap: wrap; margin-bottom: 20px;
}
.page-header h5, .page-title-lg {
  font-size: 1.32rem; font-weight: 700; letter-spacing: -.02em; margin: 0;
  display: flex; align-items: center; gap: 10px;
}
.page-header h5 > i:first-child, .page-icon {
  width: 36px; height: 36px; border-radius: 10px; display: inline-grid; place-items: center;
  font-size: 1.05rem; background: var(--accent-soft); color: var(--accent) !important; margin: 0 !important;
}
.page-sub { font-size: .82rem; color: var(--muted); margin-top: 4px; }
.toolbar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.search-box {
  display: flex; align-items: center; gap: 8px; height: 38px; padding: 0 12px;
  background: var(--surface); border: 1px solid var(--border-2); border-radius: 10px;
  transition: border-color .12s, box-shadow .12s; min-width: 240px;
}
.search-box:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--ring); }
.search-box i { color: var(--muted); }
.search-box input { border: 0; outline: 0; background: transparent; color: var(--text); font: inherit; font-size: .86rem; width: 100%; }
.top-search { height: 36px; min-width: 0; width: 240px; transition: width .2s var(--ease), border-color .12s, box-shadow .12s; }
.top-search:focus-within { width: 320px; }

/* ── Cards ─────────────────────────────────────────────────────────── */
.card {
  --bs-card-bg: var(--surface); --bs-card-border-color: var(--border);
  border-radius: var(--r-lg); box-shadow: var(--sh-1); overflow: hidden; margin-bottom: 18px;
}
.card.border-0 { border: 1px solid var(--border) !important; }
.card.shadow-sm { box-shadow: var(--sh-1) !important; }
.card-header {
  background: var(--surface) !important; color: var(--text) !important;
  border-bottom: 1px solid var(--border); padding: 14px 20px;
  font-weight: 600; font-size: .92rem; display: flex; align-items: center; gap: 8px;
}
.card-header .btn-light { background: var(--surface-2); border: 1px solid var(--border-2); color: var(--text); }
.card-header .btn-light:hover { background: var(--surface-3); }
.card-body { padding: 20px; }

/* ── Buttons ───────────────────────────────────────────────────────── */
.btn { font-weight: 600; font-size: .86rem; border-radius: 10px; padding: 8px 16px; transition: all .12s; }
.btn-sm { font-size: .8rem; padding: 6px 12px; border-radius: 9px; }
.btn-primary, .btn-success, .btn-warning, .btn-info, .btn-danger, .btn-dark {
  --bs-btn-color: #fff; --bs-btn-hover-color: #fff; --bs-btn-active-color: #fff;
  --bs-btn-bg: var(--accent); --bs-btn-border-color: var(--accent);
  --bs-btn-hover-bg: var(--accent-600); --bs-btn-hover-border-color: var(--accent-600);
  --bs-btn-active-bg: var(--accent-600); --bs-btn-active-border-color: var(--accent-600);
  --bs-btn-disabled-bg: var(--accent); --bs-btn-disabled-border-color: var(--accent);
  box-shadow: 0 1px 2px rgba(16, 24, 40, .08), inset 0 1px 0 rgba(255, 255, 255, .12);
}
html[data-bs-theme="dark"] .btn-primary { --bs-btn-color: #0f1115; --bs-btn-hover-color: #0f1115; }
.btn-secondary, .btn-outline-secondary {
  --bs-btn-color: var(--text); --bs-btn-bg: var(--surface); --bs-btn-border-color: var(--border-2);
  --bs-btn-hover-color: var(--text); --bs-btn-hover-bg: var(--surface-3); --bs-btn-hover-border-color: var(--border-2);
  --bs-btn-active-color: var(--text); --bs-btn-active-bg: var(--surface-3);
}
.btn-action {
  width: 30px; height: 30px; padding: 0; border-radius: 8px; display: inline-grid; place-items: center;
  font-size: .85rem; border: 1px solid var(--border-2); background: var(--surface); color: var(--text-2);
}
.btn-action:hover { background: var(--surface-3); color: var(--text); }
.btn-action.btn-outline-danger:hover { background: var(--bad-bg); color: var(--bad-fg); border-color: transparent; }
.btn-action.btn-outline-warning, .btn-action.btn-outline-secondary { color: var(--text-2); border-color: var(--border-2); }
.btn-chip {
  display: inline-flex; align-items: center; gap: 5px; height: 30px; padding: 0 10px;
  border-radius: 8px; font-size: .76rem; font-weight: 600; text-decoration: none;
  background: var(--accent-soft); color: var(--accent-ink); border: 0;
}
.btn-chip:hover { filter: brightness(.97); color: var(--accent-ink); }

/* ── Forms ─────────────────────────────────────────────────────────── */
.form-label { font-size: .8rem; font-weight: 600; color: var(--text-2); margin-bottom: 6px; }
.form-control, .form-select {
  font-size: .875rem; border-radius: 10px; border: 1px solid var(--border-2);
  background-color: var(--surface); color: var(--text); padding: 8px 12px; min-height: 38px;
}
.form-control-sm, .form-select-sm { font-size: .82rem; border-radius: 8px; padding: 5px 9px; min-height: 32px; }
.form-control:focus, .form-select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--ring); }
.form-control::placeholder { color: var(--muted); }
.form-control[readonly], .form-control:disabled, .form-select:disabled { background: var(--surface-2); color: var(--text-2); }
fieldset:disabled .form-control, fieldset:disabled .form-select { opacity: 1; }
fieldset:disabled .btn-outline-danger { display: none; }
.form-text { font-size: .74rem; color: var(--muted); }
.form-check-input:checked { background-color: var(--accent); border-color: var(--accent); }

/* แถบปุ่มบันทึก ติดล่างจอ */
.form-actions {
  position: sticky; bottom: 0; z-index: 40; margin: 20px -28px -28px; padding: 14px 28px;
  display: flex; gap: 10px; align-items: center;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(10px); border-top: 1px solid var(--border);
}
.form-actions .ms-auto { margin-left: auto; }

/* ตารางรายการในฟอร์ม */
.table { --bs-table-bg: transparent; --bs-table-color: var(--text); --bs-table-border-color: var(--border); font-size: .85rem; margin: 0; }
.table > thead th, .table-secondary th {
  background: var(--surface-2) !important; color: var(--muted) !important;
  font-size: .72rem; font-weight: 600; letter-spacing: .04em; text-transform: uppercase;
  border-color: var(--border) !important; padding: 10px 12px; white-space: nowrap;
}
.table > tbody td { padding: 10px 12px; vertical-align: middle; border-color: var(--border); }
.table-hover > tbody tr:hover > * { background: var(--surface-2); }
.table-bordered > :not(caption) > * > * { border-color: var(--border); }
.table tfoot th { background: var(--surface-2); font-weight: 700; }

/* ── Alerts ─────────────────────────────────────────────────────────── */
.alert { border: 0; border-radius: var(--r); font-size: .86rem; padding: 12px 16px; display: flex; gap: 10px; align-items: center; }
.alert-danger  { background: var(--bad-bg);  color: var(--bad-fg); }
.alert-success { background: var(--ok-bg);   color: var(--ok-fg); }
.alert-info    { background: var(--info-bg); color: var(--info-fg); }
.alert-secondary { background: var(--mute-bg); color: var(--mute-fg); }
.alert-danger::before  { content: "\F33A"; font-family: bootstrap-icons; }
.alert-success::before { content: "\F26A"; font-family: bootstrap-icons; }

/* ── Status pills ───────────────────────────────────────────────────── */
.pill {
  display: inline-flex; align-items: center; gap: 5px; padding: 3px 9px;
  border-radius: 999px; font-size: .72rem; font-weight: 600; line-height: 1.4; white-space: nowrap;
}
.pill::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: .85; }
.pill-ok     { background: var(--ok-bg);     color: var(--ok-fg); }
.pill-warn   { background: var(--warn-bg);   color: var(--warn-fg); }
.pill-bad    { background: var(--bad-bg);    color: var(--bad-fg); }
.pill-info   { background: var(--info-bg);   color: var(--info-fg); }
.pill-violet { background: var(--violet-bg); color: var(--violet-fg); }
.pill-mute   { background: var(--mute-bg);   color: var(--mute-fg); }
.badge-open   { background: var(--ok-bg);  color: var(--ok-fg); }
.badge-closed { background: var(--bad-bg); color: var(--bad-fg); }
.badge-active { background: var(--info-bg); color: var(--info-fg); }

/* ── AG Grid ───────────────────────────────────────────────────────── */
.ag-wrapper { border: 1px solid var(--border); border-radius: var(--r-lg); overflow: hidden; box-shadow: var(--sh-1); background: var(--surface); }
.ag-theme-quartz {
  --ag-font-family: var(--font);
  --ag-font-size: 13px;
  --ag-background-color: var(--surface);
  --ag-foreground-color: var(--text);
  --ag-header-background-color: var(--surface-2);
  --ag-header-foreground-color: var(--muted);
  --ag-border-color: var(--border);
  --ag-row-border-color: var(--border);
  --ag-row-hover-color: var(--surface-2);
  --ag-selected-row-background-color: var(--accent-soft);
  --ag-odd-row-background-color: var(--surface);
  --ag-control-panel-background-color: var(--surface);
  --ag-input-border-color: var(--border-2);
  --ag-input-focus-border-color: var(--accent);
  --ag-range-selection-border-color: var(--accent);
  --ag-active-color: var(--accent);
  --ag-wrapper-border-radius: 0;
  --ag-borders: none;
  --ag-header-column-resize-handle-color: var(--border-2);
  --ag-cell-horizontal-padding: 14px;
  --ag-secondary-foreground-color: var(--muted);
  --ag-modal-overlay-background-color: color-mix(in srgb, var(--surface) 70%, transparent);
}
.ag-theme-quartz .ag-header-cell-text { font-size: 11.5px; font-weight: 600; letter-spacing: .03em; text-transform: uppercase; }
.ag-theme-quartz .ag-cell { display: flex; align-items: center; }
.ag-theme-quartz .ag-right-aligned-cell { justify-content: flex-end; font-variant-numeric: tabular-nums; }
.ag-theme-quartz .ag-paging-panel { border-top: 1px solid var(--border); color: var(--text-2); }
.doc-link { font-weight: 600; text-decoration: none; color: var(--accent); }
.doc-link:hover { text-decoration: underline; }

/* ── Stat tiles (Dashboard) ─────────────────────────────────────────── */
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--r-lg);
  padding: 18px 18px 16px; box-shadow: var(--sh-1); height: 100%;
  display: flex; flex-direction: column; gap: 6px; text-decoration: none; color: var(--text);
  transition: border-color .15s, box-shadow .15s, transform .15s;
}
a.stat:hover { border-color: var(--border-2); box-shadow: var(--sh-2); transform: translateY(-1px); color: var(--text); }
.stat-top { display: flex; align-items: center; justify-content: space-between; }
.stat-label { font-size: .8rem; color: var(--text-2); font-weight: 500; }
.stat-icon { width: 32px; height: 32px; border-radius: 9px; display: grid; place-items: center; font-size: .95rem; background: var(--accent-soft); color: var(--accent); }
.stat-value { font-size: 1.6rem; font-weight: 700; letter-spacing: -.02em; line-height: 1.15; }
.stat-foot { font-size: .76rem; color: var(--muted); }
.hero {
  position: relative; overflow: hidden; color: #fff; border-radius: var(--r-lg); padding: 24px;
  background: radial-gradient(120% 140% at 100% 0%, var(--brand-1) 0%, var(--brand-2) 45%, var(--brand-3) 100%);
  box-shadow: 0 18px 40px -18px color-mix(in srgb, var(--brand-2) 80%, transparent); height: 100%;
}
.hero::after { content: ""; position: absolute; right: -60px; bottom: -80px; width: 220px; height: 220px; border-radius: 50%; background: rgba(255, 255, 255, .07); }
.hero-label { font-size: .78rem; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; opacity: .8; }
.hero-value { font-size: 2.9rem; font-weight: 700; letter-spacing: -.03em; line-height: 1.1; margin-top: 6px; }
.hero-value small { font-size: 1.1rem; font-weight: 500; opacity: .75; margin-left: 4px; }
.hero-foot { display: flex; justify-content: space-between; gap: 10px; margin-top: 18px; padding-top: 14px; border-top: 1px solid rgba(255, 255, 255, .18); font-size: .8rem; opacity: .9; }
.hero a { color: #fff; font-weight: 600; text-decoration: none; }
.progress-row + .progress-row { margin-top: 16px; }
.bar-track { height: 8px; border-radius: 99px; background: var(--surface-3); overflow: hidden; margin-top: 6px; }
.bar-fill { height: 100%; border-radius: 99px; background: var(--accent); }
.list-row { display: flex; align-items: center; gap: 12px; padding: 12px 20px; border-top: 1px solid var(--border); text-decoration: none; color: var(--text); }
.list-row:first-child { border-top: 0; }
.list-row:hover { background: var(--surface-2); color: var(--text); }

/* กราฟรายเดือน */
.viz-root { --series-1: var(--accent); --grid: var(--border); }   /* 1 ชุดข้อมูล ใช้สีหลักของโทน */
.viz { position: relative; }
.viz svg { display: block; width: 100%; height: 260px; overflow: visible; }
.viz .tick { font-size: 11px; fill: var(--muted); font-variant-numeric: tabular-nums; }
.viz .bar { fill: var(--series-1); transition: opacity .12s; }
.viz .bar-hit { fill: transparent; cursor: pointer; }
.viz .bar-hit:hover + .bar, .viz .bar.on { opacity: .82; }
.viz-tip {
  position: absolute; pointer-events: none; z-index: 5; min-width: 150px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--sh-2);
  padding: 8px 12px; font-size: .78rem; transform: translate(-50%, calc(-100% - 10px)); display: none;
}
.viz-tip b { display: block; font-size: .92rem; font-variant-numeric: tabular-nums; }
.viz-tip small { display: block; font-size: .7rem; opacity: .7; margin-top: 2px; }
.seg { display: inline-flex; padding: 3px; gap: 2px; border-radius: 10px; background: var(--surface-3); }
.seg button { border: 0; background: transparent; color: var(--text-2); font-size: .76rem; font-weight: 600; padding: 4px 10px; border-radius: 7px; }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--sh-1); }

/* ── Responsive ────────────────────────────────────────────────────── */
.sidebar-overlay { display: none; }
@media (max-width: 992px) {
  #sidebar { transform: translateX(-100%); box-shadow: none; }
  #sidebar.open { transform: none; box-shadow: var(--sh-3); }
  #main { margin-left: 0; }
  .sidebar-toggle { display: grid; }
  #topbar { padding: 0 16px; }
  #content { padding: 18px 16px 16px; }
  .form-actions { margin: 20px -16px -16px; padding: 12px 16px; }
  .sidebar-overlay { position: fixed; inset: 0; background: rgba(15, 17, 21, .5); z-index: 99; }
  .sidebar-overlay.show { display: block; }
  .search-box { min-width: 0; flex: 1; }
  .user-btn .who { display: none; }
}
@media print { #sidebar, #topbar, .form-actions { display: none !important; } #main { margin: 0; } }

/* เพิ่มเติม */
.ag-cell .pill + .pill { margin-left: 4px; }
.form-actions-inline { display: flex; gap: 10px; align-items: center; margin-top: 22px; padding-top: 18px; border-top: 1px solid var(--border); }

/* ==========================================================================
   มิติ & การตอบสนอง (ปิดเองเมื่อผู้ใช้ตั้ง "ลดการเคลื่อนไหว")
   ========================================================================== */
:root {
  --ease: cubic-bezier(.2, .8, .2, 1);
  --sh-1: 0 1px 2px rgba(16, 24, 40, .04), 0 1px 3px rgba(16, 24, 40, .06);
  --sh-2: 0 2px 4px rgba(16, 24, 40, .04), 0 12px 24px -8px rgba(16, 24, 40, .14);
  --sh-lift: 0 4px 8px rgba(16, 24, 40, .05), 0 22px 40px -14px color-mix(in srgb, var(--p-700) 30%, transparent);
}
html[data-bs-theme="dark"] {
  --sh-lift: 0 4px 10px rgba(0, 0, 0, .35), 0 24px 44px -16px color-mix(in srgb, var(--p-500) 35%, transparent);
}

/* พื้นหลังมีแสงจาง ๆ ตามโทนสี */
body {
  background:
    radial-gradient(900px 500px at 100% -10%, color-mix(in srgb, var(--p-200) 28%, transparent), transparent 60%),
    radial-gradient(700px 420px at -10% 110%, color-mix(in srgb, var(--p-100) 40%, transparent), transparent 60%),
    var(--bg);
  background-attachment: fixed;
}
html[data-bs-theme="dark"] body {
  background:
    radial-gradient(900px 500px at 100% -10%, color-mix(in srgb, var(--p-700) 18%, transparent), transparent 60%),
    radial-gradient(700px 420px at -10% 110%, color-mix(in srgb, var(--p-900) 30%, transparent), transparent 60%),
    var(--bg);
  background-attachment: fixed;
}

/* เข้าหน้า: ค่อย ๆ ลอยขึ้นทีละส่วน */
@keyframes rise { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }
#content > *, #content > .row > [class*="col"] { animation: rise .45s var(--ease) both; }
#content > *:nth-child(2) { animation-delay: .04s; } #content > *:nth-child(3) { animation-delay: .08s; }
#content > *:nth-child(4) { animation-delay: .12s; } #content > *:nth-child(5) { animation-delay: .16s; }
#content .row > [class*="col"]:nth-child(2) { animation-delay: .06s; }
#content .row > [class*="col"]:nth-child(3) { animation-delay: .10s; }
#content .row > [class*="col"]:nth-child(4) { animation-delay: .14s; }

/* การ์ดลอยเมื่อชี้ */
.card, .stat, .ag-wrapper { transition: transform .25s var(--ease), box-shadow .25s var(--ease), border-color .25s; }
.card:hover { box-shadow: var(--sh-2); }
a.stat:hover, .stat:hover { transform: translateY(-3px); box-shadow: var(--sh-lift); border-color: color-mix(in srgb, var(--accent) 35%, var(--border)); }
.stat-icon { transition: transform .3s var(--ease), background .3s; }
.stat:hover .stat-icon { transform: scale(1.12) rotate(-6deg); background: var(--accent); color: #fff; }
html[data-bs-theme="dark"] .stat:hover .stat-icon { color: #0f1115; }

/* แสงส่องตามเมาส์ (ใส่ class="spot") */
.spot { position: relative; isolation: isolate; }
.spot::before {
  content: ""; position: absolute; inset: 0; border-radius: inherit; pointer-events: none; z-index: -1;
  opacity: 0; transition: opacity .3s;
  background: radial-gradient(360px circle at var(--mx, 50%) var(--my, 50%),
              color-mix(in srgb, var(--accent) 12%, transparent), transparent 60%);
}
.spot:hover::before { opacity: 1; }
.hero.spot::before { z-index: 0; background: radial-gradient(420px circle at var(--mx, 50%) var(--my, 50%), rgba(255, 255, 255, .18), transparent 60%); }
.hero > * { position: relative; z-index: 1; }

/* ปุ่ม: ลอยเมื่อชี้, ยุบเมื่อกด, primary มีแสงวาบ */
.btn { position: relative; overflow: hidden; transition: transform .15s var(--ease), box-shadow .2s, background-color .2s, border-color .2s, color .2s; }
.btn:hover { transform: translateY(-1px); }
.btn:active, .btn-action:active, .btn-chip:active, .icon-btn:active, .nav-item:active { transform: translateY(0) scale(.97); }
.btn-primary { background-image: linear-gradient(180deg, rgba(255, 255, 255, .14), rgba(255, 255, 255, 0)); }
.btn-primary:hover { box-shadow: 0 8px 18px -6px color-mix(in srgb, var(--accent) 60%, transparent); }
.btn-primary::after {
  content: ""; position: absolute; top: 0; left: -60%; width: 40%; height: 100%;
  background: linear-gradient(100deg, transparent, rgba(255, 255, 255, .35), transparent);
  transform: skewX(-20deg); transition: left .6s var(--ease);
}
.btn-primary:hover::after { left: 130%; }
.btn-action, .btn-chip, .icon-btn { transition: transform .15s var(--ease), background .15s, color .15s, box-shadow .15s; }
.btn-action:hover, .icon-btn:hover { transform: translateY(-1px); box-shadow: var(--sh-1); }
.icon-btn:hover i { animation: wiggle .5s var(--ease); }
@keyframes wiggle { 30% { transform: rotate(-14deg); } 60% { transform: rotate(10deg); } }

/* ปุ่มกำลังบันทึก */
.btn.is-loading { pointer-events: none; color: transparent !important; }
.btn.is-loading::before {
  content: ""; position: absolute; inset: 0; margin: auto; width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, .45); border-top-color: #fff; animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* เมนู: แถบแสดงหน้าปัจจุบัน + เลื่อนเมื่อชี้ */
.nav-item { position: relative; }
.nav-item i { transition: transform .2s var(--ease), color .12s; }
.nav-item:hover i { transform: translateX(2px); }
.nav-item.active::before {
  content: ""; position: absolute; left: -10px; top: 8px; bottom: 8px; width: 3px; border-radius: 0 3px 3px 0;
  background: var(--accent); box-shadow: 0 0 12px color-mix(in srgb, var(--accent) 70%, transparent);
  animation: grow .35s var(--ease);
}
@keyframes grow { from { transform: scaleY(0); } }
.brand-icon { transition: transform .4s var(--ease); }
.brand:hover .brand-icon { transform: rotate(-8deg) scale(1.06); }

/* ช่องกรอก: focus นุ่มขึ้น */
.form-control, .form-select, .search-box { transition: border-color .15s, box-shadow .2s, background-color .15s; }
.form-control:hover:not(:focus):not([readonly]):not(:disabled), .form-select:hover:not(:focus):not(:disabled) { border-color: color-mix(in srgb, var(--accent) 35%, var(--border-2)); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.form-control:focus-visible, .form-select:focus-visible, .search-box input:focus-visible { outline: 0; }

/* แถวตาราง/รายการ */
.ag-theme-quartz .ag-row { transition: background-color .15s; }
.ag-theme-quartz .ag-row-hover .doc-link { text-decoration: underline; }
.list-row { transition: background .15s, padding-left .2s var(--ease); }
.list-row:hover { padding-left: 24px; }
.bar-fill { transition: width 1s var(--ease); background: linear-gradient(90deg, var(--p-400), var(--accent)); }
.pill { transition: transform .15s; }
.pill:hover { transform: translateY(-1px); }

/* กราฟ */
.viz .bar { transform-origin: bottom; transform-box: fill-box; animation: barUp .7s var(--ease) both; }
@keyframes barUp { from { transform: scaleY(0); } }
.viz .bar.on { opacity: 1; filter: brightness(1.12) drop-shadow(0 6px 10px color-mix(in srgb, var(--accent) 45%, transparent)); }

/* Toast แจ้งผล */
.toast-stack { position: fixed; right: 20px; bottom: 20px; z-index: 2000; display: grid; gap: 10px; }
.cps-toast {
  display: flex; align-items: center; gap: 10px; min-width: 260px; max-width: 380px;
  padding: 12px 14px; border-radius: 12px; font-size: .86rem; font-weight: 500;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); box-shadow: var(--sh-lift);
  animation: toastIn .4s var(--ease) both;
}
.cps-toast i { width: 28px; height: 28px; border-radius: 8px; display: grid; place-items: center; background: var(--ok-bg); color: var(--ok-fg); }
.cps-toast.err i { background: var(--bad-bg); color: var(--bad-fg); }
.cps-toast.out { animation: toastOut .3s var(--ease) both; }
@keyframes toastIn { from { opacity: 0; transform: translateY(16px) scale(.96); } }
@keyframes toastOut { to { opacity: 0; transform: translateX(30px); } }

/* แถบบน: เงาเมื่อเลื่อนหน้า */
#topbar { transition: box-shadow .2s, background-color .2s; }
#topbar.scrolled { box-shadow: 0 6px 20px -12px rgba(16, 24, 40, .25); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
  .btn:hover, .stat:hover, a.stat:hover { transform: none; }
}

/* ==========================================================================
   โหมดเพิ่มเติม (html[data-mode]) — สว่าง/มืด ใช้ token เดิม
   sepia, contrast = Bootstrap light   ·   dim = Bootstrap dark
   ========================================================================== */
/* ถนอมสายตา: พื้นครีมโทนกระดาษ ตัวหนังสือน้ำตาลเข้ม */
html[data-mode="sepia"] {
  --bg:        #f3ede2;
  --surface:   #fbf7ef;
  --surface-2: #f4ede0;
  --surface-3: #ebe2d1;
  --border:    #e1d6c2;
  --border-2:  #d2c4aa;
  --text:      #3a2e22;
  --text-2:    #5d4c3a;
  --muted:     #8a7760;
  --accent-soft: color-mix(in srgb, var(--p-100) 70%, #fbf7ef);
  --sh-1: 0 1px 2px rgba(80, 60, 30, .06), 0 1px 3px rgba(80, 60, 30, .06);
  --sh-2: 0 2px 4px rgba(80, 60, 30, .05), 0 12px 24px -8px rgba(80, 60, 30, .16);
}
html[data-mode="sepia"] body {
  background:
    radial-gradient(900px 500px at 100% -10%, color-mix(in srgb, var(--p-200) 18%, transparent), transparent 60%),
    var(--bg);
  background-attachment: fixed;
}

/* หม่น: มืดแบบนุ่ม เทาอมฟ้า คอนทราสต์ต่ำกว่าโหมดมืด */
html[data-mode="dim"] {
  --bg:        #1f2530;
  --surface:   #283040;
  --surface-2: #2e3747;
  --surface-3: #364052;
  --border:    #3b4558;
  --border-2:  #485368;
  --text:      #dde2ea;
  --text-2:    #b2b9c6;
  --muted:     #8a93a3;
  --accent-soft: color-mix(in srgb, var(--p-500) 20%, #283040);
  --ok-bg: #1f3a33; --warn-bg: #3a3222; --bad-bg: #3f2629; --info-bg: #22324d; --violet-bg: #312a4a; --mute-bg: #313a49;
}

/* คอนทราสต์สูง: ขาว-ดำ เส้นขอบเข้ม สีหลักเข้มขึ้น 2 ระดับ */
html[data-mode="contrast"] {
  --bg:        #ffffff;
  --surface:   #ffffff;
  --surface-2: #f1f1f1;
  --surface-3: #e4e4e4;
  --border:    #6b6b6b;
  --border-2:  #1a1a1a;
  --text:      #000000;
  --text-2:    #111111;
  --muted:     #333333;
  --accent:      var(--p-800);
  --accent-600:  var(--p-900);
  --accent-soft: var(--p-100);
  --accent-ink:  var(--p-900);
  --ring:        color-mix(in srgb, var(--p-700) 45%, transparent);
  --ok-fg: #05532f; --warn-fg: #6b3d00; --bad-fg: #8a1010; --info-fg: #0b3a9c; --violet-fg: #4a1499; --mute-fg: #222;
  --sh-1: none; --sh-2: none; --sh-lift: 0 0 0 2px var(--text);
}
html[data-mode="contrast"] body { background: #fff; }
html[data-mode="contrast"] a:not(.btn):not(.nav-item):not(.stat):not(.list-row):not(.dropdown-item) { text-decoration: underline; }
html[data-mode="contrast"] .card, html[data-mode="contrast"] .stat, html[data-mode="contrast"] .ag-wrapper { border-width: 1.5px; }
html[data-mode="contrast"] .nav-item.active { outline: 2px solid var(--accent); }
html[data-mode="contrast"] :focus-visible { outline-width: 3px; }
html[data-mode="contrast"] .pill { border: 1px solid currentColor; }

/* ปุ่มเลือกโหมด 5 แบบในเมนู */
.tm-mode { grid-template-columns: repeat(5, 1fr); }
.tm-mode button { display: grid; justify-items: center; gap: 4px; padding: 8px 2px 6px; font-size: .7rem; white-space: nowrap; }
.tm-mode button i { font-size: 1rem; margin: 0 !important; }
.tm-mode .sw { width: 30px; height: 18px; border-radius: 5px; border: 1px solid var(--border-2); }

/* ── กระดิ่งแจ้งเตือน ──────────────────────────────────────────────── */
.bell { position: relative; }
.bell .badge-dot {
  position: absolute; top: -4px; right: -4px; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: #ef4444; color: #fff; font-size: .66rem; font-weight: 700; line-height: 18px; text-align: center;
  box-shadow: 0 0 0 2px var(--bg); display: none;
}
.bell.has .badge-dot { display: block; animation: pop .35s var(--ease); }
.bell.ring i { animation: ring 1s var(--ease); transform-origin: 50% 0; }
@keyframes pop { from { transform: scale(0); } }
@keyframes ring { 10%, 30% { transform: rotate(16deg); } 20%, 40% { transform: rotate(-14deg); } 50% { transform: rotate(6deg); } 60% { transform: none; } }
.bell-menu { width: 380px; padding: 0; overflow: hidden; }
.bell-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.bell-head b { font-size: .9rem; }
.bell-head button { border: 0; background: none; color: var(--accent); font-size: .78rem; font-weight: 600; }
.bell-list { max-height: 420px; overflow-y: auto; }
.bell-foot { display: block; text-align: center; padding: 10px; border-top: 1px solid var(--border); font-size: .8rem; font-weight: 600; text-decoration: none; }
.notif-row { align-items: flex-start; cursor: pointer; }
.notif-row.unread { background: color-mix(in srgb, var(--accent-soft) 60%, transparent); }
.notif-row.unread .notif-title::after {
  content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--accent); margin-left: 6px; vertical-align: middle;
}
.bell-list .list-row { padding: 11px 14px; }
.bell-list .list-row:hover { padding-left: 16px; }
.notif-ic { width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center; flex-shrink: 0; font-size: .9rem; }
.lv-info { background: var(--info-bg); color: var(--info-fg); }
.lv-ok   { background: var(--ok-bg);   color: var(--ok-fg); }
.lv-warn { background: var(--warn-bg); color: var(--warn-fg); }
.lv-bad  { background: var(--bad-bg);  color: var(--bad-fg); }
.seg .btn { color: var(--text-2); }
.seg .btn.on { background: var(--surface); color: var(--text); box-shadow: var(--sh-1); }
@media (max-width: 576px) { .bell-menu { width: calc(100vw - 24px); } }

/* แผงเซ็นชื่อ (assets/sigpad.js) */
.sig-wrap { position: relative; border: 2px dashed var(--border); border-radius: 10px; background: #fff; touch-action: none; }
#sigPad { display: block; width: 100%; height: 220px; cursor: crosshair; }
.sig-hint { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: #9aa3b2; pointer-events: none; font-size: .95rem; }
.sig-hint[hidden] { display: none; }
.sig-wrap::after { content: ''; position: absolute; left: 8%; right: 8%; bottom: 46px; border-bottom: 1px solid #cfd6e4; pointer-events: none; }
@media (max-width: 576px) { #sigPad { height: 200px; } }
