/* ============ SSS — "AI graffiti": electric violet on tinted canvas ============ */
:root {
  --bg: #f4f3fb;
  --surface: #ffffff;
  --surface-2: #f1effb;
  --surface-3: #e8e5f6;
  --ink-1: #1a1430;
  --ink-2: #423a5e;
  --ink-3: #726a8c;
  --ink-4: #a39bbd;
  --line: #e7e3f4;
  --line-soft: #f0edf9;
  --line-strong: #d6cfec;

  /* brand — electric violet */
  --brand: oklch(56% 0.24 300);
  --brand-strong: oklch(48% 0.26 300);
  --brand-soft: oklch(70% 0.18 300);
  --brand-ink: #ffffff;
  --brand-bg: oklch(95% 0.04 300);
  --brand-bg-2: oklch(90% 0.07 300);
  --ring: oklch(56% 0.24 300 / 0.35);

  --accent: var(--brand);
  --accent-ink: var(--brand-ink);

  /* graffiti chart / accent palette */
  --c-violet: oklch(58% 0.24 300);
  --c-pink: oklch(64% 0.25 350);
  --c-cyan: oklch(70% 0.15 210);
  --c-lime: oklch(78% 0.20 130);
  --c-amber: oklch(75% 0.17 70);
  --c-blue: oklch(60% 0.20 264);

  --success: oklch(64% 0.18 150);
  --success-bg: oklch(94% 0.07 150);
  --warning: oklch(72% 0.17 70);
  --warning-bg: oklch(95% 0.09 75);
  --danger: oklch(60% 0.23 18);
  --danger-bg: oklch(95% 0.06 18);
  --info: oklch(62% 0.18 250);
  --info-bg: oklch(95% 0.06 250);
  --shadow-sm: 0 1px 2px rgba(40,20,80,.06), 0 1px 1px rgba(40,20,80,.04);
  --shadow-md: 0 2px 4px rgba(40,20,80,.06), 0 8px 22px rgba(60,30,110,.10);
  --shadow-lg: 0 4px 10px rgba(40,20,80,.08), 0 18px 44px rgba(70,30,130,.18);
  --radius-sm: 9px;
  --radius: 13px;
  --radius-lg: 18px;
  --font: 'Bai Jamjuree', 'IBM Plex Sans Thai', -apple-system, system-ui, sans-serif;
  --font-display: 'Kanit', 'Bai Jamjuree', sans-serif;
  --font-mono: 'IBM Plex Mono', ui-monospace, SFMono-Regular, Menlo, monospace;
  --sidebar-w: 248px;
  --topbar-h: 56px;
}

[data-theme="dark"] {
  --bg: #0c0a1a;
  --surface: #15122a;
  --surface-2: #1d1838;
  --surface-3: #292247;
  --ink-1: #f4f1ff;
  --ink-2: #d9d2f0;
  --ink-3: #9990bd;
  --ink-4: #6f6691;
  --line: #2b2450;
  --line-soft: #1d1838;
  --line-strong: #3e3568;

  --brand: oklch(74% 0.20 300);
  --brand-strong: oklch(80% 0.18 300);
  --brand-soft: oklch(62% 0.20 300);
  --brand-ink: #0c0a1a;
  --brand-bg: oklch(34% 0.14 300);
  --brand-bg-2: oklch(42% 0.17 300);
  --ring: oklch(74% 0.20 300 / 0.45);

  --accent: var(--brand);
  --accent-ink: var(--brand-ink);

  --c-violet: oklch(74% 0.20 300);
  --c-pink: oklch(74% 0.22 350);
  --c-cyan: oklch(78% 0.14 210);
  --c-lime: oklch(84% 0.18 130);
  --c-amber: oklch(82% 0.15 70);
  --c-blue: oklch(72% 0.18 264);

  --success: oklch(78% 0.18 150);
  --success-bg: oklch(34% 0.10 150);
  --warning: oklch(82% 0.16 75);
  --warning-bg: oklch(34% 0.10 75);
  --danger: oklch(72% 0.20 18);
  --danger-bg: oklch(34% 0.11 18);
  --info: oklch(76% 0.16 250);
  --info-bg: oklch(34% 0.10 250);
  --shadow-sm: 0 1px 0 rgba(0,0,0,.4);
  --shadow-md: 0 1px 0 rgba(0,0,0,.4), 0 4px 16px rgba(0,0,0,.4);
  --shadow-lg: 0 1px 0 rgba(0,0,0,.4), 0 16px 42px rgba(0,0,0,.6);
}

* { box-sizing: border-box; }
html, body, #root { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink-1);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button { font-family: inherit; cursor: pointer; }
input, select, textarea, button { font-family: inherit; font-size: inherit; color: inherit; }
a { color: inherit; text-decoration: none; }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 8px; border: 2px solid var(--bg); }
::-webkit-scrollbar-track { background: transparent; }

/* ============ Layout ============ */
.shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;
}
.shell.collapsed { grid-template-columns: 64px 1fr; }

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  height: var(--topbar-h);
}
.brand-logo {
  width: 32px; height: 32px;
  border-radius: 9px;
  background: linear-gradient(140deg, var(--brand-soft), var(--brand-strong));
  color: #fff;
  display: grid; place-items: center;
  font-weight: 700; font-size: 12px; letter-spacing: 0.3px;
  flex-shrink: 0;
  box-shadow: 0 2px 6px var(--ring), inset 0 1px 0 rgba(255,255,255,.25);
}
.brand-name { font-family: var(--font-display); font-weight: 700; font-size: 15px; letter-spacing: 0.01em; }
.brand-sub { font-size: 11px; color: var(--ink-3); }

.nav {
  flex: 1;
  overflow-y: auto;
  padding: 10px 8px 20px;
}
.nav-section {
  padding: 14px 10px 6px;
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--ink-4);
}
.collapsed .nav-section { text-align: center; padding: 14px 0 6px; }
.collapsed .nav-section::before { content: "·"; }
.collapsed .nav-section > * { display: none; }

.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  color: var(--ink-2);
  font-size: 13px;
  cursor: pointer;
  user-select: none;
  position: relative;
}
.nav-item:hover { background: var(--surface-2); color: var(--ink-1); }
.nav-item.active {
  background: var(--brand-bg);
  color: var(--brand-strong);
  font-weight: 600;
}
.nav-item.active::before {
  content: "";
  position: absolute;
  left: -8px; top: 50%; transform: translateY(-50%);
  width: 3px; height: 18px;
  border-radius: 0 3px 3px 0;
  background: var(--brand);
}
.nav-item.active .icon { color: var(--brand); }
.collapsed .nav-item.active::before { left: 0; }
.nav-item .icon { width: 18px; height: 18px; flex-shrink: 0; }
.nav-item .label { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: inherit; font-size: 13px; }
.nav-item .count {
  font-size: 11px;
  padding: 1px 6px;
  border-radius: 999px;
  background: var(--surface-3);
  color: var(--ink-2);
}
.nav-item.active .count { background: var(--brand); color: var(--brand-ink); }
.collapsed .nav-item .label,
.collapsed .nav-item .count { display: none; }
.collapsed .nav-item { justify-content: center; padding: 8px; }

.sidebar-foot {
  border-top: 1px solid var(--line);
  padding: 10px;
  display: flex; align-items: center; gap: 10px;
}
.avatar {
  width: 30px; height: 30px;
  border-radius: 50%;
  background: var(--surface-3);
  display: grid; place-items: center;
  font-weight: 600; font-size: 12px;
  flex-shrink: 0;
}
.collapsed .sidebar-foot .who { display: none; }

.main {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
}
.topbar {
  height: var(--topbar-h);
  border-bottom: 1px solid var(--line);
  background: var(--surface);
  display: flex; align-items: center;
  padding: 0 18px;
  gap: 14px;
}
.topbar h1 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: -0.005em;
}
.topbar .crumb { color: var(--ink-3); font-size: 13px; }
.topbar .crumb b { color: var(--ink-1); font-weight: 600; }
.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }

.search {
  position: relative;
  width: 320px;
  max-width: 40vw;
}
.search input {
  width: 100%;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  padding: 0 10px 0 30px;
  outline: none;
}
.search input:focus { border-color: var(--brand); background: var(--surface); box-shadow: 0 0 0 3px var(--ring); }
.search svg { position: absolute; left: 9px; top: 7px; opacity: 0.5; }
.kbd {
  position: absolute; right: 6px; top: 5px;
  font-size: 11px; color: var(--ink-3);
  border: 1px solid var(--line);
  padding: 1px 5px; border-radius: 4px;
  background: var(--surface);
  font-family: var(--font-mono);
}

.icon-btn {
  width: 32px; height: 32px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line);
  background: var(--surface);
  display: grid; place-items: center;
  color: var(--ink-2);
  position: relative;
}
.icon-btn:hover { background: var(--brand-bg); color: var(--brand-strong); border-color: var(--brand-soft); }
.icon-btn .dot {
  position: absolute;
  top: 6px; right: 6px;
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--danger);
  border: 2px solid var(--surface);
  box-sizing: content-box;
}

.content {
  flex: 1;
  overflow-y: auto;
  padding: 22px 26px 60px;
}
.page-head {
  display: flex; align-items: end;
  gap: 18px; margin-bottom: 18px;
}
.page-head h2 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 23px;
  font-weight: 700;
  letter-spacing: 0;
}
.page-head .sub { color: var(--ink-3); font-size: 13px; margin-top: 2px; }
.page-head .right { margin-left: auto; display: flex; gap: 8px; }

/* ============ Buttons ============ */
.btn {
  height: 32px;
  padding: 0 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--line-strong);
  background: var(--surface);
  color: var(--ink-1);
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary {
  background: var(--brand);
  color: var(--brand-ink);
  border-color: var(--brand);
  box-shadow: 0 1px 2px var(--ring);
}
.btn.primary:hover { background: var(--brand-strong); border-color: var(--brand-strong); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { color: var(--danger); border-color: var(--line-strong); }
.btn.sm { height: 28px; padding: 0 10px; font-size: 12px; border-radius: 5px; }
.btn.lg { height: 38px; padding: 0 16px; font-size: 14px; }
.btn .icon { width: 14px; height: 14px; }

/* ============ Cards ============ */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}
.card-head {
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 10px;
}
.card-head h3 { margin: 0; font-family: var(--font-display); font-size: 14px; font-weight: 600; letter-spacing: 0; }
.card-head .sub { color: var(--ink-3); font-size: 12px; }
.card-head .right { margin-left: auto; display: flex; gap: 6px; align-items: center; }
.card-body { padding: 16px; }
.card-body.flush { padding: 0; }

/* ============ Table ============ */
.table-wrap { overflow-x: auto; }
table.t {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
table.t th, table.t td {
  padding: 10px 14px;
  text-align: left;
  border-bottom: 1px solid var(--line);
  vertical-align: middle;
}
table.t th {
  font-weight: 500;
  color: var(--ink-3);
  font-size: 11.5px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  background: var(--surface-2);
  position: sticky; top: 0; z-index: 1;
}
table.t tbody tr:hover { background: var(--surface-2); }
table.t tbody tr:last-child td { border-bottom: 0; }
table.t td.right, table.t th.right { text-align: right; font-variant-numeric: tabular-nums; }
table.t td.num { font-variant-numeric: tabular-nums; }
table.t .doc-no { font-family: var(--font-mono); font-size: 12px; }
.row-clickable { cursor: pointer; }

/* Segmented control (มุมมอง / ตัวกรองรอบ) */
.seg { display: inline-flex; background: var(--surface-3); border: 1px solid var(--line); border-radius: 8px; padding: 2px; gap: 2px; }
.seg-btn { display: inline-flex; align-items: center; gap: 5px; border: none; background: transparent; color: var(--ink-3); font-family: var(--font); font-size: 12px; font-weight: 500; padding: 5px 11px; border-radius: 6px; cursor: pointer; white-space: nowrap; }
.seg-btn .icon { opacity: 0.85; }
.seg-btn:hover { color: var(--ink-1); }
.seg-btn.on { background: var(--surface); color: var(--ink-1); box-shadow: var(--shadow-sm); }

/* Cash-advance grouped view */
.ca-table tr.ca-month:hover, .ca-table tr.ca-batch:hover { background: transparent; }
.ca-table tr.ca-month td { background: var(--surface-2); border-top: 1.5px solid var(--line-strong); border-bottom: 1px solid var(--line); padding: 9px 14px; }
.ca-table tbody:first-child tr.ca-month td { border-top: none; }
.ca-table tr.ca-batch td { padding: 6px 14px 6px 22px; border-bottom: 1px dashed var(--line); background: color-mix(in srgb, var(--surface-2) 45%, transparent); }
.ca-table tr.ca-batch + tr td { border-top: none; }

/* ============ Badge ============ */
.badge {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 11.5px;
  font-weight: 500;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-3);
  color: var(--ink-2);
  border: 1px solid transparent;
  white-space: nowrap;
}
.badge.success { background: var(--success-bg); color: var(--success); }
.badge.warning { background: var(--warning-bg); color: var(--warning); }
.badge.danger { background: var(--danger-bg); color: var(--danger); }
.badge.info { background: var(--info-bg); color: var(--info); }
.badge.outline { background: transparent; border-color: var(--line-strong); color: var(--ink-2); }
.badge .dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: 0.7; }

/* ============ Stat tile ============ */
.stat {
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  display: flex; flex-direction: column; gap: 6px;
  min-height: 96px;
}
.stat .label { font-size: 12px; color: var(--ink-3); display: flex; align-items: center; gap: 6px;}
.stat .value { font-family: var(--font-display); font-size: 25px; font-weight: 700; letter-spacing: -0.01em; font-variant-numeric: tabular-nums; }
.stat .delta { font-size: 12px; color: var(--ink-3); display: flex; align-items: center; gap: 4px; }
.stat .delta.up { color: var(--success); }
.stat .delta.down { color: var(--danger); }

/* ============ Form ============ */
.field { display: flex; flex-direction: column; gap: 5px; }
.field > label { font-size: 12px; color: var(--ink-3); font-weight: 500; }
.input, .select, .textarea {
  height: 34px;
  border: 1px solid var(--line-strong);
  background: var(--surface);
  border-radius: 6px;
  padding: 0 10px;
  outline: none;
  width: 100%;
}
.textarea { height: auto; padding: 8px 10px; min-height: 70px; resize: vertical; }
.input:focus, .select:focus, .textarea:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--ring); }
.field-row { display: grid; gap: 12px; }
.field-row.cols-2 { grid-template-columns: 1fr 1fr; }
.field-row.cols-3 { grid-template-columns: 1fr 1fr 1fr; }
.field-row.cols-4 { grid-template-columns: 1fr 1fr 1fr 1fr; }
@media (max-width: 720px) {
  .field-row.cols-2, .field-row.cols-3, .field-row.cols-4 { grid-template-columns: 1fr; }
}

/* ============ Grids ============ */
.grid { display: grid; gap: 14px; }
.grid.cols-2 { grid-template-columns: 1fr 1fr; }
.grid.cols-3 { grid-template-columns: 1fr 1fr 1fr; }
.grid.cols-4 { grid-template-columns: repeat(4, 1fr); }
.grid.cols-5 { grid-template-columns: repeat(5, 1fr); }
.grid.cols-6 { grid-template-columns: repeat(6, 1fr); }
.grid.dash-main { grid-template-columns: 2fr 1fr; }
@media (max-width: 1100px) {
  .grid.cols-4, .grid.cols-5, .grid.cols-6 { grid-template-columns: repeat(2, 1fr); }
  .grid.dash-main { grid-template-columns: 1fr; }
}

/* ============ Tabs ============ */
.tabs {
  display: flex;
  gap: 2px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 14px;
}
.tab {
  padding: 8px 12px;
  border: 0;
  border-bottom: 2px solid transparent;
  background: transparent;
  font-size: 13px;
  color: var(--ink-3);
  margin-bottom: -1px;
  cursor: pointer;
}
.tab:hover { color: var(--ink-1); }
.tab.active { color: var(--brand-strong); border-color: var(--brand); font-weight: 600; }

/* ============ Drawer / Modal ============ */
.scrim {
  position: fixed; inset: 0;
  background: rgba(12,10,9,0.42);
  z-index: 50;
  backdrop-filter: blur(3px);
}
[data-theme="dark"] .scrim { background: rgba(0,0,0,0.62); }
.drawer {
  position: fixed;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 540px; max-width: 94vw;
  max-height: 90vh;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  z-index: 51;
  box-shadow: var(--shadow-lg);
  display: flex; flex-direction: column;
  overflow: hidden;
  animation: modal-in 0.18s cubic-bezier(0.16, 1, 0.3, 1);
}
@keyframes modal-in {
  from { opacity: 0; transform: translate(-50%, -46%) scale(0.97); }
  to { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}
.drawer.wide { width: 720px; }
.drawer-head {
  height: 54px;
  border-bottom: 1px solid var(--line);
  display: flex; align-items: center;
  padding: 0 16px;
  gap: 10px;
  flex-shrink: 0;
}
.drawer-head h3 { margin: 0; font-family: var(--font-display); font-size: 15px; font-weight: 600; }
.drawer-body { flex: 1; overflow-y: auto; padding: 18px; }
.drawer-foot {
  border-top: 1px solid var(--line);
  padding: 12px 16px;
  display: flex; gap: 8px; justify-content: flex-end;
  flex-shrink: 0;
  background: var(--surface-2);
}

/* ============ Misc ============ */
.divider { height: 1px; background: var(--line); margin: 14px 0; }
.muted { color: var(--ink-3); }
.mono { font-family: var(--font-mono); }
.right { text-align: right; }
.center { text-align: center; }
.nowrap { white-space: nowrap; }
.flex { display: flex; align-items: center; gap: 8px; }
.flex.col { flex-direction: column; align-items: stretch; }
.between { justify-content: space-between; }
.gap-2 { gap: 4px; }
.gap-4 { gap: 8px; }
.gap-6 { gap: 14px; }
.mt { margin-top: 8px; }
.mt-lg { margin-top: 16px; }
.mb { margin-bottom: 8px; }
.mb-lg { margin-bottom: 16px; }
.full { width: 100%; }
.spacer { flex: 1; }
.h1 { font-family: var(--font-display); font-size: 19px; font-weight: 700; letter-spacing: 0; }
.h2 { font-family: var(--font-display); font-size: 14.5px; font-weight: 600; letter-spacing: 0; }
.label { font-size: 12px; color: var(--ink-3); }
.amount { font-variant-numeric: tabular-nums; font-weight: 500; }
.amount.big { font-size: 22px; letter-spacing: -0.02em; }
.amount.neg { color: var(--danger); }
.amount.pos { color: var(--success); }

/* ============ Charts ============ */
.bar-chart { display: flex; align-items: flex-end; gap: 6px; height: 160px; padding-top: 10px; }
.bar-chart .bar-col { display: flex; flex-direction: column; align-items: center; gap: 6px; flex: 1; height: 100%; }
.bar-chart .bar-stack { width: 100%; max-width: 28px; display: flex; flex-direction: column-reverse; gap: 2px; flex: 1; }
.bar-chart .bar-stack .seg { background: var(--c-violet); border-radius: 4px; }
.bar-chart .bar-stack .seg.alt { background: var(--c-cyan); }
.bar-chart .bar-label { font-size: 10.5px; color: var(--ink-3); }

/* Segmented control — pay round selector */
.payseg {
  display: inline-flex;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 2px;
  gap: 2px;
}
.payseg-btn {
  border: none;
  background: transparent;
  color: var(--ink-3);
  font-family: var(--font);
  font-size: 12.5px;
  font-weight: 500;
  padding: 5px 12px;
  border-radius: 7px;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.12s, color 0.12s;
}
.payseg-btn:hover { color: var(--ink-1); }
.payseg-btn.active {
  background: var(--surface);
  color: var(--brand-strong);
  font-weight: 600;
  box-shadow: var(--shadow-sm);
}
.auto-tag {
  font-size: 9.5px;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--brand-ink);
  background: linear-gradient(135deg, var(--c-violet), var(--c-pink));
  padding: 2px 7px;
  border-radius: 999px;
  text-transform: none;
}
.line-chart-wrap { position: relative; height: 180px; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--ink-3); }
.legend .item { display: flex; align-items: center; gap: 6px; }
.legend .swatch { width: 11px; height: 11px; border-radius: 3px; background: var(--c-violet); }
.legend .swatch.alt { background: var(--c-cyan); }

/* ============ Print preview / Document ============ */
.doc-paper {
  background: white;
  color: #0c0a09;
  border: 1px solid var(--line);
  padding: 36px 40px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  font-size: 12.5px;
  line-height: 1.55;
  min-height: 600px;
}
[data-theme="dark"] .doc-paper { box-shadow: var(--shadow-lg); }
.doc-paper h1 { margin: 0; font-size: 22px; font-weight: 600; letter-spacing: 0.02em; }
.doc-paper .doc-no { color: #57534e; font-size: 12px; font-family: var(--font-mono); }
.doc-paper table { width: 100%; border-collapse: collapse; }
.doc-paper table th, .doc-paper table td {
  padding: 8px 10px;
  text-align: left;
  border-bottom: 1px solid #e7e5e4;
  font-size: 12px;
}
.doc-paper table th { background: #f7f6f4; font-weight: 600; color: #44403c; font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; }
.doc-paper table td.right, .doc-paper table th.right { text-align: right; }
.doc-paper .totals {
  margin-left: auto;
  width: 280px;
  margin-top: 14px;
}
.doc-paper .totals .row { display: flex; justify-content: space-between; padding: 5px 0; font-size: 13px;}
.doc-paper .totals .row.grand { border-top: 2px solid #0c0a09; margin-top: 8px; padding-top: 10px; font-size: 16px; font-weight: 600; }
.doc-stamp {
  display: inline-block;
  padding: 4px 14px;
  border: 2px solid var(--success);
  color: var(--success);
  font-weight: 600;
  border-radius: 4px;
  letter-spacing: 0.06em;
  font-size: 11px;
  text-transform: uppercase;
  transform: rotate(-5deg);
}

/* ============ AI panel ============ */
.ai-btn {
  position: fixed;
  right: 22px; bottom: 22px;
  height: 44px;
  padding: 0 16px 0 14px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  border: 0;
  box-shadow: var(--shadow-lg);
  display: flex; align-items: center; gap: 8px;
  font-weight: 500;
  z-index: 40;
}
.ai-panel {
  position: fixed;
  right: 22px; bottom: 22px;
  width: 380px; max-width: 92vw;
  height: 540px; max-height: 80vh;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: var(--shadow-lg);
  z-index: 41;
  display: flex; flex-direction: column;
}
.ai-head {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 8px;
}
.ai-head h4 { margin: 0; font-size: 13px; font-weight: 600; }
.ai-body { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 10px; }
.ai-msg { font-size: 13px; line-height: 1.5; }
.ai-msg.user { align-self: flex-end; background: var(--accent); color: var(--accent-ink); padding: 8px 12px; border-radius: 14px 14px 4px 14px; max-width: 85%; }
.ai-msg.bot { align-self: flex-start; background: var(--surface-2); padding: 10px 12px; border-radius: 14px 14px 14px 4px; max-width: 90%; border: 1px solid var(--line); }
.ai-suggest { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 14px 8px; }
.ai-suggest .chip { font-size: 12px; padding: 5px 10px; border: 1px solid var(--line); border-radius: 999px; color: var(--ink-2); background: var(--surface); cursor: pointer; }
.ai-suggest .chip:hover { background: var(--surface-2); }
.ai-foot {
  padding: 10px 12px;
  border-top: 1px solid var(--line);
  display: flex; gap: 8px;
}
.ai-foot input { flex: 1; height: 34px; border: 1px solid var(--line-strong); border-radius: 6px; padding: 0 10px; background: var(--surface); outline: none; }
.ai-foot input:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--ring); }

/* ============ Toast ============ */
.toast-wrap {
  position: fixed;
  bottom: 22px; left: 50%; transform: translateX(-50%);
  z-index: 100;
  display: flex; flex-direction: column; gap: 8px;
}
.toast {
  background: var(--ink-1);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: var(--shadow-lg);
  display: flex; align-items: center; gap: 8px;
  min-width: 240px;
}

/* ============ Workflow / approval ============ */
.workflow {
  display: flex; align-items: center; gap: 8px;
  flex-wrap: wrap;
}
.workflow .step {
  display: flex; align-items: center; gap: 6px;
  padding: 4px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 12px;
  background: var(--surface-2);
  color: var(--ink-3);
}
.workflow .step.done { color: var(--success); border-color: var(--success); background: var(--success-bg); }
.workflow .step.current { color: var(--brand-strong); border-color: var(--brand); background: var(--brand-bg); font-weight: 600; }
.workflow .arrow { color: var(--ink-4); font-size: 12px; }

/* ============ Attachments ============ */
.attach-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.attach {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  background: var(--surface);
  display: flex; flex-direction: column; gap: 4px;
  cursor: pointer;
}
.attach:hover { background: var(--surface-2); }
.attach .ext { font-size: 10px; font-weight: 600; color: var(--ink-3); letter-spacing: 0.05em; }
.attach .name { font-size: 12px; font-weight: 500; }
.attach .meta { font-size: 11px; color: var(--ink-3); }
.attach-dropzone {
  border: 1.5px dashed var(--line-strong);
  border-radius: 8px;
  padding: 20px;
  text-align: center;
  color: var(--ink-3);
  background: var(--surface-2);
  font-size: 12.5px;
}

/* ============ Calendar / list ============ */
.calendar {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 1px;
  background: var(--line);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
}
.calendar .cell {
  background: var(--surface);
  min-height: 86px;
  padding: 8px;
  display: flex; flex-direction: column;
  font-size: 12px;
  gap: 4px;
}
.calendar .cell.head { background: var(--surface-2); min-height: 0; padding: 6px 8px; font-size: 11px; color: var(--ink-3); text-transform: uppercase; letter-spacing: 0.04em; }
.calendar .day { font-weight: 500; }
.calendar .cell.out { background: var(--surface-2); color: var(--ink-4); }
.calendar .cell.today .day { background: var(--accent); color: var(--accent-ink); width: 20px; height: 20px; border-radius: 50%; display: grid; place-items: center; font-size: 11px; }
.cal-event {
  font-size: 11px;
  padding: 2px 5px;
  border-radius: 3px;
  background: var(--surface-3);
  color: var(--ink-2);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.cal-event.danger { background: var(--danger-bg); color: var(--danger); }
.cal-event.warning { background: var(--warning-bg); color: var(--warning); }
.cal-event.success { background: var(--success-bg); color: var(--success); }
.cal-event.info { background: var(--info-bg); color: var(--info); }

/* ============ Barcode ============ */
.barcode {
  display: inline-flex; align-items: stretch;
  height: 50px;
  gap: 1px;
  padding: 6px;
  background: white;
  border: 1px solid var(--line);
  border-radius: 4px;
}
[data-theme="dark"] .barcode { background: #fafaf9; }
.barcode .b { background: #0c0a09; }

/* ============ Activity ============ */
.activity-item {
  display: flex; gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
}
.activity-item:last-child { border-bottom: 0; }
.activity-item .who { font-weight: 500; }
.activity-item .what { color: var(--ink-2); font-size: 13px; }
.activity-item .when { font-size: 11px; color: var(--ink-3); margin-top: 2px; }

/* ============ Switch ============ */
.switch {
  width: 36px; height: 20px;
  border-radius: 999px;
  background: var(--line-strong);
  position: relative; cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s;
}
.switch::after {
  content: '';
  position: absolute;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: white;
  top: 2px; left: 2px;
  transition: transform 0.15s;
  box-shadow: 0 1px 2px rgba(0,0,0,.2);
}
.switch.on { background: var(--accent); }
.switch.on::after { transform: translateX(16px); }

/* ============ Pill toggle ============ */
.pill-group { display: inline-flex; padding: 2px; background: var(--surface-2); border-radius: 7px; border: 1px solid var(--line); }
.pill-group .pill { padding: 4px 10px; font-size: 12px; color: var(--ink-3); border-radius: 5px; cursor: pointer; }
.pill-group .pill.on { background: var(--surface); color: var(--ink-1); box-shadow: var(--shadow-sm); }

/* ============ Mobile ============ */
@media (max-width: 880px) {
  /* sidebar กลายเป็น overlay (position:fixed) จึงไม่ใช่ grid item อีกต่อไป —
     บังคับคอลัมน์เดียวทั้งสองสถานะ (รวม .collapsed ที่ specificity สูงกว่า) แล้วให้ .main
     กินเต็มความกว้างเสมอ ไม่งั้นเนื้อหาจะถูกบีบลงคอลัมน์แคบ/ศูนย์ (จอว่างหรือคอลัมน์ผอม) */
  .shell, .shell.collapsed { grid-template-columns: 1fr !important; }
  .main { grid-column: 1 / -1 !important; min-width: 0; width: 100%; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 248px; z-index: 60; transform: translateX(-100%); transition: transform .2s; }
  .sidebar.open { transform: translateX(0); box-shadow: 0 0 40px rgba(0,0,0,0.25); }
  .search { display: none; }
  .content { padding: 16px; }
  .topbar { padding: 0 12px; }
  .ai-panel { width: 96vw; height: 70vh; right: 2vw; }
}

.hamburger { display: none; }
@media (max-width: 880px) {
  .hamburger { display: grid; }
  /* ปุ่มย่อ/ขยาย sidebar ไม่มีความหมายบนมือถือ (sidebar เป็น overlay) — ซ่อนไว้ กันปุ่มเมนูซ้ำ */
  .collapse-toggle { display: none; }
}

/* ============ Progress range slider ============ */
.progress-range {
  -webkit-appearance: none;
  appearance: none;
}
.progress-range::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--surface);
  border: 3px solid var(--ink-1);
  cursor: grab;
  box-shadow: 0 2px 6px rgba(0,0,0,0.2);
  margin-top: 0;
}
.progress-range::-webkit-slider-thumb:active { cursor: grabbing; transform: scale(1.1); }
.progress-range::-moz-range-thumb {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--surface);
  border: 3px solid var(--ink-1);
  cursor: grab;
  box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}
.progress-range::-webkit-slider-runnable-track {
  height: 18px;
  border-radius: 999px;
}
.progress-range::-moz-range-track {
  height: 18px;
  border-radius: 999px;
}

/* ============ Print preview lightbox ============ */
.print-modal {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(12, 10, 9, 0.65);
  backdrop-filter: blur(3px);
  overflow: auto;
  padding: 24px 20px 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}
.print-modal-bar {
  position: sticky; top: 0; z-index: 2;
  background: white;
  padding: 10px 16px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  gap: 12px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.25);
  width: 100%;
  max-width: 210mm;
  color: #0c0a09;
}
.print-modal-paper {
  background: white;
  width: 100%;
  max-width: 210mm;
  box-shadow: 0 20px 60px rgba(0,0,0,0.4);
  border-radius: 0;
  overflow: visible;
}
.print-modal-paper .doc-paper {
  margin: 0 !important;
  border-radius: 0 !important;
  border-left: 0 !important;
  border-right: 0 !important;
  border-bottom: 0 !important;
  box-shadow: none !important;
}
.pm-btn {
  height: 32px;
  padding: 0 14px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  border-radius: 6px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.pm-btn-primary { background: #0c0a09; color: white; border: 0; }
.pm-btn-primary:hover { background: #44403c; }
.pm-btn-ghost { background: white; color: #44403c; border: 1px solid #d6d3d1; }
.pm-btn-ghost:hover { background: #f7f6f4; }

/* ============ Print ============ */
@media print {
  @page { margin: 8mm; size: A4; }
  *, *::before, *::after {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
    color-adjust: exact !important;
  }
  html, body {
    background: white !important;
    color: #0c0a09 !important;
    margin: 0 !important;
    padding: 0 !important;
  }

  /* DEFAULT mode (no modal): hide everything, show .doc-paper at top */
  body:not(.printing-modal) * {
    visibility: hidden !important;
    box-shadow: none !important;
  }
  body:not(.printing-modal) .doc-paper,
  body:not(.printing-modal) .doc-paper * { visibility: visible !important; }
  body:not(.printing-modal) .doc-paper {
    position: absolute !important;
    left: 0 !important;
    top: 0 !important;
    right: 0 !important;
    margin: 0 !important;
    width: 100% !important;
    background: white !important;
    color: #0c0a09 !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    min-height: 0 !important;
    page-break-inside: avoid;
  }
  body:not(.printing-modal) .newdoc-drawer ~ .doc-paper { display: none !important; }
  body:not(.printing-modal):has(.newdoc-drawer) .main .doc-paper,
  body:not(.printing-modal):has(.newdoc-drawer) .content .doc-paper {
    display: none !important;
    visibility: hidden !important;
  }

  /* LIGHTBOX mode: hide page chrome, show only modal's doc-paper */
  body.printing-modal > *:not(.print-modal) { display: none !important; }
  body.printing-modal .print-modal {
    position: static !important;
    inset: auto !important;
    background: white !important;
    backdrop-filter: none !important;
    padding: 0 !important;
    overflow: visible !important;
    display: block !important;
  }
  body.printing-modal .no-print { display: none !important; }
  body.printing-modal .print-modal-paper {
    box-shadow: none !important;
    max-width: 100% !important;
    width: 100% !important;
    border-radius: 0 !important;
    overflow: visible !important;
  }
  body.printing-modal .print-modal-paper .doc-paper {
    box-shadow: none !important;
    border-radius: 0 !important;
    margin: 0 !important;
    width: 100% !important;
    background: white !important;
    color: #0c0a09 !important;
    page-break-inside: avoid;
  }
  body.printing-modal .print-modal-paper .doc-paper * {
    visibility: visible !important;
  }
}

/* ===== Floating team chat (left of AI button) ===== */
.chat-fab.ai-btn { right: 150px; background: var(--surface); color: var(--ink-1); border: 1px solid var(--line-strong); }
.chat-fab.ai-panel { right: 150px; width: 400px; }
@media (max-width: 760px) {
  .chat-fab.ai-btn { right: auto; left: 18px; }
  .chat-fab.ai-panel { right: 2vw; width: 96vw; height: 70vh; }
}

/* ===== Feed + chat layout: chat dominates ===== */
.feed-chat-grid {
  display: grid;
  grid-template-columns: minmax(220px, 1fr) minmax(340px, 2.2fr);
  gap: 14px;
  align-items: start;
}
@media (max-width: 900px) {
  .feed-chat-grid { grid-template-columns: 1fr; }
  .feed-chat-grid > :first-child { order: 2; }
}

/* ===== Journal interactive rows ===== */
.jrow:hover td, .spec-row:hover td {
  background: var(--surface-2) !important;
}
.spec-journal th { vertical-align: bottom; }
.spec-journal td { vertical-align: top; }
