/* ============================================================
   SKCC ERP v4 — สไตล์หลัก
   ============================================================ */
:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --line: #e2e8f0;
  --line-strong: #cbd5e1;
  --text: #16202e;
  --muted: #64748b;
  --brand: #16558f;
  --brand-dark: #0f3f6d;
  --brand-soft: #e7f0f9;
  --accent: #c2410c;
  --ok: #15803d;
  --ok-soft: #dcfce7;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --bad: #b91c1c;
  --bad-soft: #fee2e2;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .06), 0 8px 24px rgba(15, 23, 42, .06);
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, .08);
  --header-h: 56px;
  --sidebar-w: 268px;
  --font: "IBM Plex Sans Thai", "Sarabun", "Noto Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1420;
    --surface: #151e2d;
    --surface-2: #1b2637;
    --line: #27354a;
    --line-strong: #35465f;
    --text: #e6edf6;
    --muted: #93a4bb;
    --brand: #4d9bdc;
    --brand-dark: #2f7cbb;
    --brand-soft: #16293d;
    --ok-soft: #10301d;
    --warn-soft: #34260c;
    --bad-soft: #3a1414;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.35);
  }
}
:root[data-theme="dark"] {
  --bg: #0d1420; --surface: #151e2d; --surface-2: #1b2637; --line: #27354a; --line-strong: #35465f;
  --text: #e6edf6; --muted: #93a4bb; --brand: #4d9bdc; --brand-dark: #2f7cbb; --brand-soft: #16293d;
  --ok-soft: #10301d; --warn-soft: #34260c; --bad-soft: #3a1414;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.35);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: var(--font); font-size: 15px; line-height: 1.55;
  -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent;
}
a { color: var(--brand); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3, h4 { margin: 0 0 .5rem; line-height: 1.3; font-weight: 700; }
h1 { font-size: 1.45rem; } h2 { font-size: 1.15rem; } h3 { font-size: 1rem; }
p { margin: 0 0 .75rem; }

/* ---------- ล็อกอิน ---------- */
.login-wrap {
  min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px;
  background: linear-gradient(160deg, var(--brand-dark), #08243d 55%, #061726);
}
.login-card {
  width: min(400px, 100%); background: var(--surface); border-radius: 18px;
  padding: 28px 24px 24px; box-shadow: 0 24px 60px rgba(0,0,0,.35);
}
.login-logo {
  width: 58px; height: 58px; border-radius: 16px; display: grid; place-items: center;
  background: var(--brand); color: #fff; font-weight: 800; font-size: 1.3rem; margin: 0 auto 14px;
  letter-spacing: .5px;
}
.login-card h1 { text-align: center; font-size: 1.25rem; margin-bottom: 2px; }
.login-card .sub { text-align: center; color: var(--muted); font-size: .88rem; margin-bottom: 20px; }

/* ---------- โครงหน้า ---------- */
.app { min-height: 100dvh; }
.topbar {
  position: sticky; top: 0; z-index: 40; height: var(--header-h);
  display: flex; align-items: center; gap: 10px; padding: 0 12px;
  background: var(--brand); color: #fff; box-shadow: var(--shadow-sm);
}
.topbar .brand { font-weight: 700; font-size: 1.02rem; display: flex; align-items: center; gap: 8px; }
.topbar .brand small { font-weight: 400; opacity: .78; font-size: .74rem; }
.topbar .spacer { flex: 1; }
.icon-btn {
  width: 38px; height: 38px; border: 0; border-radius: 10px; background: rgba(255,255,255,.14);
  color: #fff; display: grid; place-items: center; cursor: pointer; font-size: 1.05rem;
}
.icon-btn:hover { background: rgba(255,255,255,.24); }
.who { font-size: .82rem; text-align: right; line-height: 1.25; opacity: .95; }
.who b { display: block; font-size: .9rem; }

.shell { display: flex; align-items: flex-start; }
.sidebar {
  width: var(--sidebar-w); flex: 0 0 var(--sidebar-w); position: sticky; top: var(--header-h);
  height: calc(100dvh - var(--header-h)); overflow-y: auto; background: var(--surface);
  border-inline-end: 1px solid var(--line); padding: 12px 10px 32px;
}
.sidebar .group-title {
  font-size: .72rem; font-weight: 700; color: var(--muted); letter-spacing: .04em;
  padding: 14px 10px 6px; text-transform: none;
}
.nav-item {
  display: flex; align-items: center; gap: 10px; width: 100%; text-align: start;
  padding: 9px 10px; border-radius: 10px; border: 0; background: transparent; cursor: pointer;
  color: var(--text); font-size: .93rem;
}
.nav-item:hover { background: var(--surface-2); }
.nav-item.active { background: var(--brand-soft); color: var(--brand); font-weight: 600; }
.nav-item .ico { width: 22px; text-align: center; font-size: 1.02rem; }

.main { flex: 1; min-width: 0; padding: 18px 20px 64px; }
.page-head { display: flex; align-items: flex-start; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head .grow { flex: 1; min-width: 200px; }
.page-head .desc { color: var(--muted); font-size: .88rem; margin: 0; }

/* ---------- การ์ด ---------- */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow-sm); padding: 16px; margin-bottom: 16px;
}
.card > h2, .card > h3 { margin-top: 0; }
.card-head { display: flex; align-items: center; gap: 10px; margin: -4px 0 12px; flex-wrap: wrap; }
.card-head .grow { flex: 1; }
.grid { display: grid; gap: 14px; }
.grid.cols-2 { grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); }
.grid.cols-4 { grid-template-columns: repeat(auto-fit, minmax(165px, 1fr)); }

.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; }
.stat .label { color: var(--muted); font-size: .8rem; }
.stat { min-width: 0; }
.stat .value { font-size: 1.5rem; font-weight: 700; letter-spacing: -.01em; overflow-wrap: anywhere;
  display: flex; flex-wrap: wrap; align-items: baseline; column-gap: 4px; }
.stat .unit { font-size: .82rem; color: var(--muted); font-weight: 400; }

/* ---------- ฟอร์ม ---------- */
[hidden] { display: none !important; }
.field { margin-bottom: 12px; }
.field > label { display: block; font-size: .84rem; font-weight: 600; margin-bottom: 5px; }
.field .help { font-size: .78rem; color: var(--muted); margin-top: 4px; }
.input, select.input, textarea.input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--line-strong); border-radius: var(--radius-sm);
  background: var(--surface); outline: none; transition: border-color .12s, box-shadow .12s;
}
.input:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.input:disabled { background: var(--surface-2); color: var(--muted); }
textarea.input { min-height: 84px; resize: vertical; }
.input-lg { padding: 13px 14px; font-size: 1.05rem; }
.pin-input { letter-spacing: .5em; text-align: center; font-size: 1.3rem; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  padding: 10px 16px; border-radius: var(--radius-sm); border: 1px solid transparent;
  background: var(--brand); color: #fff; font-weight: 600; cursor: pointer;
  transition: filter .12s, background .12s;
}
.btn:hover { filter: brightness(1.07); }
.btn:disabled { opacity: .55; cursor: not-allowed; filter: none; }
.btn.block { width: 100%; }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--line-strong); }
.btn.ghost:hover { background: var(--surface-2); }
.btn.soft { background: var(--brand-soft); color: var(--brand); }
.btn.danger { background: var(--bad); }
.btn.sm { padding: 6px 11px; font-size: .86rem; }

/* ---------- ตาราง ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
table.tbl { width: 100%; border-collapse: collapse; font-size: .9rem; }
table.tbl th, table.tbl td { padding: 9px 12px; text-align: start; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.tbl th { background: var(--surface-2); font-weight: 600; font-size: .82rem; color: var(--muted); position: sticky; top: 0; }
table.tbl tbody tr:last-child td { border-bottom: 0; }
table.tbl tbody tr:hover { background: var(--surface-2); }
table.tbl td.num, table.tbl th.num { text-align: end; font-variant-numeric: tabular-nums; }
table.tbl td.wrap { white-space: normal; min-width: 200px; }

/* ---------- ป้าย / ข้อความ ---------- */
.badge {
  display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: .76rem; font-weight: 600;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--line);
}
.badge.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.bad { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
.badge.brand { background: var(--brand-soft); color: var(--brand); border-color: transparent; }

.alert { padding: 11px 14px; border-radius: var(--radius-sm); font-size: .9rem; margin-bottom: 12px; }
.alert.bad { background: var(--bad-soft); color: var(--bad); }
.alert.ok { background: var(--ok-soft); color: var(--ok); }
.alert.warn { background: var(--warn-soft); color: var(--warn); }
.alert.info { background: var(--brand-soft); color: var(--brand); }

.muted { color: var(--muted); }
.warn-text { color: var(--warn, #b26a00); }
.small { font-size: .84rem; }
.center { text-align: center; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.mt { margin-top: 14px; } .mb { margin-bottom: 14px; }
.empty { padding: 36px 16px; text-align: center; color: var(--muted); }
.empty .big { font-size: 2rem; display: block; margin-bottom: 6px; opacity: .6; }

/* ---------- สถานะออฟไลน์ ---------- */
.offline-bar {
  display: none; background: var(--warn-soft); color: var(--warn); font-size: .86rem;
  padding: 7px 14px; text-align: center; font-weight: 600;
}
body.is-offline .offline-bar { display: block; }
.sync-chip {
  display: none; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px;
  background: rgba(255,255,255,.18); font-size: .8rem;
}
.sync-chip.show { display: inline-flex; }

/* ---------- โหลด ---------- */
.spinner {
  width: 18px; height: 18px; border: 2px solid currentColor; border-top-color: transparent;
  border-radius: 50%; animation: spin .7s linear infinite; display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading { padding: 40px; text-align: center; color: var(--muted); }

/* ---------- มือถือ ---------- */
.backdrop { display: none; position: fixed; inset: 0; background: rgba(8,15,26,.45); z-index: 45; }
@media (max-width: 900px) {
  .main { padding: 14px 14px 80px; }
  .sidebar {
    position: fixed; inset-inline-start: 0; top: var(--header-h); z-index: 50;
    transform: translateX(-100%); transition: transform .2s ease; box-shadow: var(--shadow);
  }
  body.nav-open .sidebar { transform: none; }
  body.nav-open .backdrop { display: block; }
  .grid.cols-4 { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
}
@media (min-width: 901px) { .menu-toggle { display: none; } }

/* ---------- หน้าจัดการระบบ ---------- */
.split { display: grid; gap: 16px; grid-template-columns: minmax(240px, 320px) 1fr; align-items: start; }
.stats { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); }
@media (max-width: 640px) {
  .stats { grid-template-columns: repeat(auto-fit, minmax(112px, 1fr)); }
  .stat { padding: 11px 12px; }
  .stat .value { font-size: 1.18rem; }
}
.strong { font-weight: 600; }
.small.block { display: block; }

.list { display: flex; flex-direction: column; gap: 6px; max-height: 62vh; overflow-y: auto; }
.list-item {
  display: flex; align-items: center; gap: 8px; width: 100%; text-align: start;
  padding: 9px 11px; border: 1px solid var(--line); border-radius: var(--radius-sm);
  background: var(--surface); cursor: pointer; font: inherit; color: inherit;
}
.list-item:hover { background: var(--surface-2); }
.list-item.active { border-color: var(--brand); background: var(--brand-soft); }
.list-item .grow { flex: 1; min-width: 0; }

.perm-groups { display: flex; flex-direction: column; gap: 14px; max-height: 58vh; overflow-y: auto; padding-inline-end: 4px; }
.perm-group > .row { margin-bottom: 6px; }
.perm-group h3 { margin: 0; font-size: .95rem; }
.check { display: flex; align-items: flex-start; gap: 8px; padding: 7px 9px; border-radius: var(--radius-sm); cursor: pointer; }
.check:hover { background: var(--surface-2); }
.check input { margin-top: 3px; flex: none; }
.check .grow { flex: 1; min-width: 0; }

.input.locked { color: var(--muted); background: var(--surface-2); }

@media (max-width: 900px) {
  .split { grid-template-columns: 1fr; }
  .list { max-height: 40vh; }
  .perm-groups { max-height: none; overflow: visible; }
}

/* ---------- แถบกำลังผลิต / ยอดรวมเอกสาร ---------- */
.bar { height: 9px; border-radius: 99px; background: var(--surface-2); overflow: hidden; margin: 10px 0 6px; }
.bar-fill { display: block; height: 100%; background: var(--brand); transition: width .25s; }
.bar-fill.warn { background: var(--warn, #d98324); }
.bar-fill.over { background: var(--bad); }

.totals { margin-top: 12px; margin-inline-start: auto; max-width: 340px; }
.total-row { display: flex; gap: 12px; padding: 6px 2px; border-bottom: 1px dashed var(--line); }
.total-row:last-child { border-bottom: 0; }
.total-row .grow { flex: 1; color: var(--muted); }
.total-row.strong { font-weight: 700; font-size: 1.06rem; border-top: 2px solid var(--line-strong); margin-top: 4px; padding-top: 9px; }
.total-row.strong .grow { color: inherit; }

@media print {
  .sidebar, .topbar, .backdrop, .page-head .row, .btn { display: none !important; }
  .main { padding: 0 !important; }
  .card { border: 0 !important; box-shadow: none !important; }
}

/* ---------- หน้าเข้าสู่ระบบ (รหัสผ่าน / PIN / สมัคร) ---------- */
.grow { flex: 1; min-width: 0; }
.linkbtn {
  background: none; border: 0; padding: 0; margin: 0; font: inherit;
  color: var(--brand); cursor: pointer; text-decoration: underline;
}
.linkbtn:hover { filter: brightness(1.2); }
.login-card .row .btn.sm { flex: 1; }
.login-card .field { margin-bottom: 14px; }

/* ข้อความเน้นสี (ใช้ในตารางการเงิน) */
.ok-text { color: var(--ok); font-weight: 600; }
.bad-text { color: var(--bad); font-weight: 600; }
