/* ProcureFlow — design system
   Cher Group red on a warm-neutral ground. Every colour is a token so the
   dark theme is a token swap, never a second set of component rules. */

:root {
  --accent: #d61f1f;
  --accent-ink: #ffffff;
  --accent-soft: #fdecec;
  --accent-line: #f6cccc;

  --ground: #f4f4f5;
  --surface: #ffffff;
  --surface-2: #fafafa;
  --surface-3: #f2f2f3;

  --ink: #18181a;
  --ink-2: #45454a;
  --muted: #75757d;
  --faint: #a1a1aa;

  --rule: #e3e3e6;
  --rule-soft: #eeeef0;

  --ok: #16794b;      --ok-soft: #e6f4ee;
  --warn: #9a5b06;    --warn-soft: #fdf1de;
  --danger: #b3261e;  --danger-soft: #fdeceb;
  --info: #1d4ed8;    --info-soft: #e8eefc;
  --neutral: #57575e; --neutral-soft: #eeeef0;

  --shadow-sm: 0 1px 2px rgba(24, 24, 26, .05);
  --shadow: 0 1px 3px rgba(24,24,26,.06), 0 8px 24px rgba(24,24,26,.05);
  --shadow-lg: 0 24px 64px rgba(24,24,26,.18);

  --r: 10px;
  --r-lg: 14px;
  --sans: "IBM Plex Sans Thai", "Noto Sans Thai", system-ui, -apple-system, sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --accent: #ff6b6b; --accent-ink: #1a0d0d; --accent-soft: #331a1a; --accent-line: #4d2626;
    --ground: #121214; --surface: #1a1a1d; --surface-2: #1f1f23; --surface-3: #26262b;
    --ink: #eaeaec; --ink-2: #c4c4c9; --muted: #8e8e96; --faint: #6b6b73;
    --rule: #2e2e34; --rule-soft: #26262b;
    --ok: #56c08d; --ok-soft: #16301f; --warn: #e0a44a; --warn-soft: #33270f;
    --danger: #f08a80; --danger-soft: #331b19; --info: #7ea6f5; --info-soft: #16223d;
    --neutral: #9a9aa2; --neutral-soft: #26262b;
    --shadow-sm: 0 1px 2px rgba(0,0,0,.4);
    --shadow: 0 1px 3px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.3);
    --shadow-lg: 0 24px 64px rgba(0,0,0,.55);
  }
}
:root[data-theme="dark"] {
  --accent: #ff6b6b; --accent-ink: #1a0d0d; --accent-soft: #331a1a; --accent-line: #4d2626;
  --ground: #121214; --surface: #1a1a1d; --surface-2: #1f1f23; --surface-3: #26262b;
  --ink: #eaeaec; --ink-2: #c4c4c9; --muted: #8e8e96; --faint: #6b6b73;
  --rule: #2e2e34; --rule-soft: #26262b;
  --ok: #56c08d; --ok-soft: #16301f; --warn: #e0a44a; --warn-soft: #33270f;
  --danger: #f08a80; --danger-soft: #331b19; --info: #7ea6f5; --info-soft: #16223d;
  --neutral: #9a9aa2; --neutral-soft: #26262b;
  --shadow-sm: 0 1px 2px rgba(0,0,0,.4);
  --shadow: 0 1px 3px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.3);
  --shadow-lg: 0 24px 64px rgba(0,0,0,.55);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--ground); color: var(--ink);
  font-family: var(--sans); font-size: 14.5px; line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }
.mono, .num { font-family: var(--mono); font-variant-numeric: tabular-nums; }

/* ───────────────────────────────────────────── login */
.login-screen {
  position: fixed; inset: 0; z-index: 100; display: flex;
  align-items: center; justify-content: center; padding: 20px;
  background: linear-gradient(160deg, #1c1c20, #0e0e10);
}
.login-card {
  width: min(390px, 100%); background: var(--surface); color: var(--ink);
  border-radius: 18px; padding: 32px 30px; box-shadow: var(--shadow-lg);
}
.login-brand { display: flex; align-items: center; gap: 10px; font-size: 19px; font-weight: 700; }
.login-sub { color: var(--muted); font-size: 13px; margin: 6px 0 22px; }
.login-foot {
  margin: 18px 0 0; padding-top: 14px; border-top: 1px solid var(--rule-soft);
  font-size: 11.5px; color: var(--faint); text-align: center;
}
.logo {
  width: 30px; height: 30px; border-radius: 9px; background: var(--accent);
  color: var(--accent-ink); display: grid; place-items: center; font-weight: 800; flex: none;
}

/* ───────────────────────────────────────────── layout */
.app { display: grid; grid-template-columns: 240px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--surface); border-right: 1px solid var(--rule);
  display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh;
}
.brand {
  display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px;
  padding: 16px 16px 12px;
}
.nav { flex: 1; overflow-y: auto; padding: 4px 10px 14px; }
.nav-group {
  font-size: 10.5px; font-family: var(--mono); text-transform: uppercase;
  letter-spacing: .1em; color: var(--faint); padding: 14px 10px 5px;
}
.nav a {
  display: flex; align-items: center; gap: 9px; padding: 8px 10px; border-radius: 8px;
  color: var(--ink-2); font-size: 14px; margin: 1px 0; text-decoration: none;
}
.nav a:hover { background: var(--surface-3); text-decoration: none; }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav a .ico { width: 18px; text-align: center; flex: none; opacity: .85; }
.nav a .count {
  margin-left: auto; font-family: var(--mono); font-size: 11px;
  background: var(--accent); color: var(--accent-ink); border-radius: 99px; padding: 1px 7px;
}
.sidebar-foot {
  border-top: 1px solid var(--rule); padding: 12px 14px;
  display: flex; align-items: center; gap: 10px; justify-content: space-between;
}
.who b { display: block; font-size: 13px; }
.who small { color: var(--muted); font-size: 11.5px; }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: flex-start; gap: 14px; padding: 20px 26px 14px;
  border-bottom: 1px solid var(--rule); background: var(--ground);
  position: sticky; top: 0; z-index: 20;
}
.page-title { flex: 1; min-width: 0; }
.page-title h1 { margin: 0; font-size: 22px; font-weight: 700; letter-spacing: -.01em; }
.page-title p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.topbar-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.view { padding: 20px 26px 60px; flex: 1; }
.mobile-only { display: none; }

/* ───────────────────────────────────────────── buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  border: 1px solid var(--rule); background: var(--surface); color: var(--ink);
  padding: 8px 13px; border-radius: var(--r); font-weight: 600; font-size: 13.5px;
  cursor: pointer; white-space: nowrap; box-shadow: var(--shadow-sm);
}
.btn:hover:not(:disabled) { background: var(--surface-2); border-color: var(--faint); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.06); background: var(--accent); }
.btn.dark { background: var(--ink); border-color: var(--ink); color: var(--ground); }
.btn.danger { background: var(--danger-soft); border-color: var(--danger); color: var(--danger); }
.btn.ghost { background: transparent; box-shadow: none; }
.btn.sm { padding: 5px 9px; font-size: 12.5px; }
.btn.icon { padding: 7px 10px; }
.btn.block { width: 100%; padding: 11px; }

/* ───────────────────────────────────────────── cards & grids */
.card {
  background: var(--surface); border: 1px solid var(--rule);
  border-radius: var(--r-lg); box-shadow: var(--shadow-sm);
}
.card.pad { padding: 18px; }
.card h3 { margin: 0 0 12px; font-size: 15px; font-weight: 600; }
.grid { display: grid; gap: 14px; }
.grid.k2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.k3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.k4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid.split { grid-template-columns: 1.4fr 1fr; }
.stack { display: grid; gap: 14px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.spacer { flex: 1; }

.kpi { padding: 16px 18px; }
.kpi small { color: var(--muted); font-size: 12.5px; }
.kpi strong { display: block; font-size: 26px; font-weight: 700; margin-top: 4px; letter-spacing: -.02em; }
.kpi .delta { font-size: 12px; margin-top: 3px; color: var(--muted); }
.kpi .delta.up { color: var(--ok); }
.kpi .delta.down { color: var(--danger); }

/* ───────────────────────────────────────────── tables */
.table-wrap { overflow-x: auto; border-radius: var(--r-lg); }
table.data { width: 100%; border-collapse: collapse; font-size: 13.5px; }
table.data th {
  text-align: left; color: var(--muted); font-size: 11px; font-family: var(--mono);
  text-transform: uppercase; letter-spacing: .06em; font-weight: 500;
  padding: 10px 12px; border-bottom: 1px solid var(--rule); white-space: nowrap;
  background: var(--surface-2); position: sticky; top: 0;
}
table.data td { padding: 10px 12px; border-bottom: 1px solid var(--rule-soft); vertical-align: middle; }
table.data tr:last-child td { border-bottom: 0; }
table.data tbody tr:hover { background: var(--surface-2); }
table.data td.r, table.data th.r { text-align: right; }
table.data td.c, table.data th.c { text-align: center; }
table.data .doc-no { font-family: var(--mono); font-weight: 600; font-size: 12.5px; }
.empty { padding: 44px 20px; text-align: center; color: var(--muted); }
.empty .big { font-size: 15px; color: var(--ink-2); margin-bottom: 4px; }

/* ───────────────────────────────────────────── chips */
.chip {
  display: inline-flex; align-items: center; gap: 5px; padding: 2px 9px;
  border-radius: 99px; font-size: 11.5px; font-weight: 600; white-space: nowrap;
  background: var(--neutral-soft); color: var(--neutral);
}
.chip.ok { background: var(--ok-soft); color: var(--ok); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.danger { background: var(--danger-soft); color: var(--danger); }
.chip.info { background: var(--info-soft); color: var(--info); }
.chip.accent { background: var(--accent-soft); color: var(--accent); }

/* ───────────────────────────────────────────── forms */
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field > span, .field > label { font-size: 12px; font-weight: 600; color: var(--ink-2); }
.field > span .opt { font-weight: 400; color: var(--faint); }
.field input, .field select, .field textarea, .control {
  width: 100%; border: 1px solid var(--rule); border-radius: var(--r);
  background: var(--surface); padding: 8px 10px; min-height: 38px;
}
.field textarea { min-height: 74px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}
.field input:disabled, .field select:disabled, .field textarea:disabled {
  background: var(--surface-3); color: var(--muted); cursor: not-allowed;
}
.field .hint { font-size: 11.5px; color: var(--muted); }
.field .err { font-size: 11.5px; color: var(--danger); }
.form-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 13px; }
.form-grid.k2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.form-grid .full { grid-column: 1 / -1; }
.form-grid .span2 { grid-column: span 2; }
.check { display: flex; align-items: center; gap: 8px; min-height: 38px; font-size: 13.5px; }
.check input { width: 17px; height: 17px; min-height: 0; accent-color: var(--accent); }

/* line-item editor */
.lines { border: 1px solid var(--rule); border-radius: var(--r-lg); overflow: hidden; }
.lines-scroll { overflow-x: auto; }
.lines table { width: 100%; border-collapse: collapse; font-size: 13px; min-width: 900px; }
.lines th {
  background: var(--ink); color: var(--ground); font-size: 10.5px; font-family: var(--mono);
  text-transform: uppercase; letter-spacing: .05em; font-weight: 500;
  padding: 8px 8px; text-align: left; white-space: nowrap;
}
.lines td { padding: 5px 6px; border-bottom: 1px solid var(--rule-soft); }
.lines tr:last-child td { border-bottom: 0; }
.lines input, .lines select {
  width: 100%; border: 1px solid transparent; background: transparent;
  border-radius: 7px; padding: 6px 7px; min-height: 32px;
}
.lines input:hover, .lines select:hover { border-color: var(--rule); background: var(--surface); }
.lines input:focus, .lines select:focus {
  outline: none; border-color: var(--accent); background: var(--surface);
  box-shadow: 0 0 0 2px var(--accent-soft);
}
.lines input.num, .lines td.r { text-align: right; }
.lines .rowno { color: var(--faint); font-family: var(--mono); font-size: 11.5px; text-align: center; }
.lines .amt { font-weight: 700; font-family: var(--mono); text-align: right; white-space: nowrap; }
.lines-foot { display: flex; gap: 8px; padding: 10px; background: var(--surface-2); flex-wrap: wrap; }
.iconbtn {
  border: 0; background: transparent; color: var(--muted); cursor: pointer;
  width: 28px; height: 28px; border-radius: 7px; display: grid; place-items: center;
}
.iconbtn:hover { background: var(--surface-3); color: var(--ink); }
.iconbtn.del:hover { background: var(--danger-soft); color: var(--danger); }

/* totals panel */
.totals { display: grid; gap: 0; }
.totals .line {
  display: flex; justify-content: space-between; gap: 12px; padding: 7px 0;
  border-bottom: 1px solid var(--rule-soft); font-size: 13.5px;
}
.totals .line b { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.totals .line.grand {
  border: 0; margin-top: 8px; padding: 13px 15px; border-radius: var(--r);
  background: var(--accent-soft); color: var(--accent); align-items: center;
}
.totals .line.grand b { font-size: 19px; }
.totals .line.wht { color: var(--warn); }

/* ───────────────────────────────────────────── misc */
.alert { padding: 10px 13px; border-radius: var(--r); font-size: 13px; border: 1px solid transparent; }
.alert.error { background: var(--danger-soft); color: var(--danger); border-color: var(--danger); }
.alert.warn { background: var(--warn-soft); color: var(--warn); border-color: var(--warn-soft); }
.alert.info { background: var(--info-soft); color: var(--info); border-color: var(--info-soft); }
.alert.ok { background: var(--ok-soft); color: var(--ok); border-color: var(--ok-soft); }

.filters { display: flex; gap: 9px; flex-wrap: wrap; margin-bottom: 14px; }
.filters input, .filters select {
  border: 1px solid var(--rule); border-radius: var(--r); padding: 7px 11px;
  background: var(--surface); min-height: 36px;
}
.filters input { min-width: 220px; }

.bar-chart { display: flex; align-items: flex-end; gap: 10px; height: 160px; padding-top: 22px; }
.bar-chart .bar { flex: 1; background: var(--ink); border-radius: 6px 6px 2px 2px; position: relative; min-height: 3px; }
.bar-chart .bar.now { background: var(--accent); }
.bar-chart .bar em { position: absolute; bottom: -20px; left: 0; right: 0; text-align: center;
  font-size: 11px; color: var(--muted); font-style: normal; }
.bar-chart .bar b { position: absolute; top: -19px; left: 0; right: 0; text-align: center;
  font-size: 10.5px; font-family: var(--mono); color: var(--ink-2); }
.chart-pad { padding-bottom: 24px; }

.progress { height: 7px; background: var(--surface-3); border-radius: 99px; overflow: hidden; }
.progress span { display: block; height: 100%; background: var(--accent); border-radius: inherit; }
.progress.ok span { background: var(--ok); }
.progress.warn span { background: var(--warn); }
.progress.over span { background: var(--danger); }

.stat-row {
  display: flex; justify-content: space-between; gap: 10px; align-items: center;
  padding: 9px 0; border-bottom: 1px solid var(--rule-soft); font-size: 13.5px;
}
.stat-row:last-child { border-bottom: 0; }

.trail { display: grid; gap: 0; }
.trail-item { display: flex; gap: 10px; padding: 9px 0; border-bottom: 1px solid var(--rule-soft); }
.trail-item:last-child { border-bottom: 0; }
.trail-item .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent);
  margin-top: 7px; flex: none; }
.trail-item .body { flex: 1; min-width: 0; }
.trail-item b { font-size: 13px; }
.trail-item small { display: block; color: var(--muted); font-size: 11.5px; }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; font-size: 13.5px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }

.section-title { font-size: 12px; font-family: var(--mono); text-transform: uppercase;
  letter-spacing: .08em; color: var(--muted); margin: 0 0 8px; }

/* ───────────────────────────────────────────── toast & modal */
.toast-stack { position: fixed; right: 18px; bottom: 18px; z-index: 200;
  display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: var(--ink); color: var(--ground); padding: 11px 15px; border-radius: var(--r);
  box-shadow: var(--shadow-lg); font-size: 13.5px; max-width: 380px;
  animation: toast-in .18s ease-out;
}
.toast.error { background: var(--danger); color: #fff; }
.toast.ok { background: var(--ok); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

.modal-root {
  position: fixed; inset: 0; z-index: 150; background: rgba(10,10,12,.5);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.modal {
  width: min(720px, 100%); max-height: 92vh; overflow: auto; background: var(--surface);
  border-radius: 18px; box-shadow: var(--shadow-lg); padding: 22px 24px;
}
.modal.wide { width: min(1000px, 100%); }
.modal-head { display: flex; justify-content: space-between; gap: 12px;
  align-items: flex-start; margin-bottom: 16px; }
.modal-head h2 { margin: 0; font-size: 18px; }
.modal-head p { margin: 3px 0 0; color: var(--muted); font-size: 12.5px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 9px; margin-top: 20px;
  padding-top: 16px; border-top: 1px solid var(--rule-soft); }
.close-x { border: 0; background: var(--surface-3); border-radius: 9px;
  width: 32px; height: 32px; cursor: pointer; flex: none; }

.skeleton { padding: 40px; text-align: center; color: var(--muted); }

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

@media (max-width: 1100px) {
  .grid.k4 { grid-template-columns: repeat(2, 1fr); }
  .grid.split { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 780px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; z-index: 60; left: 0; top: 0; width: 250px;
    transform: translateX(-100%); transition: transform .2s;
  }
  .sidebar.open { transform: none; box-shadow: var(--shadow-lg); }
  .mobile-only { display: inline-flex; }
  .topbar { padding: 14px 16px 12px; }
  .view { padding: 14px 16px 50px; }
  .grid.k2, .grid.k3, .grid.k4 { grid-template-columns: 1fr; }
  .form-grid, .form-grid.k2 { grid-template-columns: 1fr; }
  .form-grid .span2 { grid-column: auto; }
}
