:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f8fafb;
  --border: #e3e8ec;
  --text: #17262c;
  --muted: #66767d;
  --brand: #123c46;
  --brand-2: #1c5562;
  --accent: #f2711c;
  --accent-soft: #fff1e7;
  --good: #1f8a5b;
  --good-soft: #e5f5ed;
  --bad: #c9372c;
  --bad-soft: #fdecea;
  --warn: #b7791f;
  --warn-soft: #fdf4e3;
  --info: #2b6cb0;
  --info-soft: #e8f1fb;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 40, 48, .06), 0 4px 16px rgba(16, 40, 48, .05);
  --font: "IBM Plex Sans Thai", Tahoma, "Segoe UI", sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font-family: var(--font); font-size: 15px; color: var(--text); background: var(--bg); line-height: 1.5; }
[hidden] { display: none !important; }
a { color: inherit; }
h1, h2, h3 { margin: 0; font-weight: 600; }
.muted { color: var(--muted); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.pos { color: var(--good); }
.neg { color: var(--bad); }

/* ---------- buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  padding: 8px 14px; border-radius: 9px; font: inherit; font-weight: 500; cursor: pointer; text-decoration: none;
  transition: background .15s, border-color .15s;
}
.btn:hover { background: var(--surface-2); border-color: #cfd8de; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #dc6213; }
.btn.danger { color: var(--bad); border-color: #f1c4c0; }
.btn.danger:hover { background: var(--bad-soft); }
.btn.block { width: 100%; }
.btn.sm { padding: 5px 10px; font-size: 13.5px; }
.btn.ghost-light { background: transparent; border-color: rgba(255,255,255,.18); color: #d6e4e8; }
.btn.ghost-light:hover { background: rgba(255,255,255,.08); }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 9px; padding: 8px 11px; width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(242,113,28,.35); border-color: var(--accent); }
textarea { resize: vertical; min-height: 64px; }
label { display: flex; flex-direction: column; gap: 5px; font-size: 13.5px; font-weight: 500; color: #3c4c52; }
label.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 400; }
label.check input { width: auto; }
.error { color: var(--bad); min-height: 1.2em; margin: 0; font-size: 14px; }

/* ---------- auth ---------- */
.auth { min-height: 100%; display: grid; place-items: center; padding: 16px;
  background: radial-gradient(circle at 20% 10%, #1c5562 0, var(--brand) 55%, #0b262d 100%); }
.auth-card { width: min(380px, 100%); background: var(--surface); border-radius: 16px; padding: 28px; box-shadow: 0 20px 60px rgba(0,0,0,.3); display: flex; flex-direction: column; gap: 14px; }
.auth-card h1 { font-size: 22px; }
.auth-card p { margin: -8px 0 4px; }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: 236px 1fr; min-height: 100%; }
.sidebar { background: var(--brand); color: #d6e4e8; display: flex; flex-direction: column; padding: 18px 12px; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; align-items: center; gap: 10px; padding: 0 8px 18px; }
.brand-mark { width: 38px; height: 38px; border-radius: 10px; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 700; letter-spacing: .5px; flex: none; font-family: Arial, sans-serif; }
.brand-logo { width: 46px; height: 46px; border-radius: 10px; background: #fff; object-fit: contain; padding: 2px; flex: none; }
.auth-logo { width: 120px; height: 120px; object-fit: contain; align-self: center; margin-bottom: -4px; }
.logo-preview { display: grid; place-items: center; background: var(--surface-2); border-radius: 10px; padding: 12px; }
.logo-preview img { width: 140px; height: 140px; object-fit: contain; }
.brand-name { color: #fff; font-weight: 600; line-height: 1.2; }
.brand-sub { font-size: 12.5px; opacity: .7; }
nav { display: flex; flex-direction: column; gap: 2px; flex: 1; overflow-y: auto; }
nav a { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 9px; text-decoration: none; color: #c4d6db; }
nav a:hover { background: rgba(255,255,255,.07); color: #fff; }
nav a.active { background: rgba(242,113,28,.18); color: #fff; box-shadow: inset 3px 0 0 var(--accent); }
.ico { width: 18px; text-align: center; opacity: .85; }
.sidebar-foot { display: flex; flex-direction: column; gap: 8px; padding-top: 12px; }
main { min-width: 0; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 18px 28px 6px; flex-wrap: wrap; }
.topbar h1 { font-size: 23px; flex: 1; }
.topbar-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.topbar-actions select { width: auto; }
.menu-btn { display: none; border: 0; background: transparent; font-size: 22px; cursor: pointer; padding: 4px 8px; color: var(--text); }
.view { padding: 12px 28px 40px; display: flex; flex-direction: column; gap: 18px; }
.scrim { display: none; }

/* ---------- cards ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; min-width: 0; }
.card > h2 { font-size: 16px; margin-bottom: 12px; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.card > h2 .muted { font-size: 13px; font-weight: 400; }
.grid { display: grid; gap: 18px; }
.grid.kpi { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 14px; }
.grid.two { grid-template-columns: 1.4fr 1fr; }
.grid.half { grid-template-columns: 1fr 1fr; }
.kpi-card { padding: 16px 18px; }
.kpi-label { font-size: 13px; color: var(--muted); }
.kpi-value { font-size: 25px; font-weight: 600; font-variant-numeric: tabular-nums; margin-top: 2px; }
.kpi-sub { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.kpi-card.accent { background: var(--brand); color: #fff; border-color: var(--brand); }
.kpi-card.accent .kpi-label, .kpi-card.accent .kpi-sub { color: #b9d0d6; }

.progress { height: 14px; border-radius: 99px; background: #edf1f3; overflow: hidden; }
.progress > div { height: 100%; background: linear-gradient(90deg, var(--accent), #f6a04d); border-radius: 99px; min-width: 2px; }
.payback-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 8px; gap: 8px; flex-wrap: wrap; }
.payback-pct { font-size: 30px; font-weight: 700; color: var(--accent); }
.stat-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 14px; }
.stat-row div { background: var(--surface-2); border-radius: 9px; padding: 9px 12px; }
.stat-row b { display: block; font-size: 17px; font-variant-numeric: tabular-nums; }
.stat-row span { font-size: 12.5px; color: var(--muted); }

/* ---------- tables ---------- */
.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.toolbar input[type=search] { max-width: 280px; }
.toolbar select { width: auto; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--border); background: var(--surface); border-radius: 99px; padding: 4px 12px; cursor: pointer; font: inherit; font-size: 13.5px; }
.chip.on { background: var(--brand); border-color: var(--brand); color: #fff; }
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); }
.card .table-wrap { box-shadow: none; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 12px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; }
th { font-size: 12.5px; font-weight: 600; color: var(--muted); background: var(--surface-2); white-space: nowrap; position: sticky; top: 0; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover { background: #fbf7f4; }
tfoot td { font-weight: 600; background: var(--surface-2); border-top: 1px solid var(--border); border-bottom: 0; }
td.wrap { min-width: 200px; }
td.nowrap { white-space: nowrap; }
tr.opening td { background: #fafbfc; color: var(--muted); font-style: italic; }
tr.low td { background: var(--bad-soft); }
.empty { padding: 28px; text-align: center; color: var(--muted); }
.cell-sub { font-size: 12.5px; color: var(--muted); }

.badge { display: inline-block; font-size: 12.5px; padding: 2px 10px; border-radius: 99px; font-weight: 500; white-space: nowrap; }
.st-รอผลิต { background: var(--warn-soft); color: var(--warn); }
.st-กำลังผลิต { background: var(--info-soft); color: var(--info); }
.st-ส่งแล้ว { background: #efe9fb; color: #6b46c1; }
.st-ชำระแล้ว { background: var(--good-soft); color: var(--good); }
.tag { background: var(--accent-soft); color: #b4500e; }
.st-bad { background: var(--bad-soft); color: var(--bad); }
select.status { width: auto; border-radius: 99px; padding: 3px 10px; font-size: 13px; font-weight: 500; border: 0; cursor: pointer; }
tr.inactive td { color: var(--muted); }

/* bank ledger: colour stripe by where the entry came from */
:root { --src-order: #1f8a5b; --src-purchase: #f2711c; --src-expense: #c9372c; --src-manual: #7a8b93; }
tr.src-order td:first-child { box-shadow: inset 4px 0 0 var(--src-order); }
tr.src-purchase td:first-child { box-shadow: inset 4px 0 0 var(--src-purchase); }
tr.src-expense td:first-child { box-shadow: inset 4px 0 0 var(--src-expense); }
tr.src-manual td:first-child { box-shadow: inset 4px 0 0 var(--src-manual); }
.src-badge.src-order { background: var(--good-soft); color: var(--good); }
.src-badge.src-purchase { background: var(--accent-soft); color: #b4500e; }
.src-badge.src-expense { background: var(--bad-soft); color: var(--bad); }
.src-badge.src-manual { background: #eef1f3; color: #566; }
.chip .dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 6px; vertical-align: 0; }
.dot.src-order { background: var(--src-order); } .dot.src-purchase { background: var(--src-purchase); }
.dot.src-expense { background: var(--src-expense); } .dot.src-manual { background: var(--src-manual); }
.chip.on .dot { outline: 2px solid rgba(255,255,255,.7); }

/* month summary */
.month-summary { padding: 0; }
.month-summary > summary { cursor: pointer; padding: 12px 18px; font-weight: 600; list-style: none; display: flex; justify-content: space-between; gap: 10px; }
.month-summary > summary::-webkit-details-marker { display: none; }
.month-summary > summary::after { content: '▾'; color: var(--muted); }
.month-summary[open] > summary::after { content: '▴'; }
.month-summary > summary .muted { font-weight: 400; font-size: 13px; }
.month-summary .table-wrap { border: 0; border-top: 1px solid var(--border); border-radius: 0 0 var(--radius) var(--radius); box-shadow: none; }
.month-summary tr.on td { background: var(--accent-soft); font-weight: 600; }

/* stock cards */
.stock-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 14px; }
.stock-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px 16px; display: flex; flex-direction: column; gap: 8px; }
.stock-card.low { border-color: #f1c892; background: #fffaf3; }
.stock-card.out { border-color: #f1c4c0; background: #fff6f5; }
.stock-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.stock-name { font-weight: 600; line-height: 1.3; }
.stock-qty { display: flex; align-items: baseline; gap: 6px; }
.stock-qty b { font-size: 26px; font-variant-numeric: tabular-nums; }
.stock-qty .muted { font-size: 12.5px; }
.progress.slim { height: 8px; }
.stock-card.low .progress > div { background: linear-gradient(90deg, #d9862a, #f0b35c); }
.stock-card.out .progress > div { background: var(--bad); }
.stock-meta { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 12.5px; color: var(--muted); }
.stock-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: auto; padding-top: 4px; }
.mv-ใช้ไป { background: var(--bad-soft); color: var(--bad); }
.mv-รับเข้า { background: var(--good-soft); color: var(--good); }
.mv-ปรับยอด { background: var(--info-soft); color: var(--info); }

/* quotation editor */
.grid.quote-grid { grid-template-columns: 1.3fr 1fr; align-items: start; }
.quote-summary .sum-rows .row { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 6px 0; border-bottom: 1px dashed var(--border); }
.quote-summary .sum-rows .row.total { font-size: 18px; border-bottom: 2px solid var(--brand); }
.quote-summary .sum-rows input { width: 130px; text-align: right; }
.quote-summary h3 { font-size: 14px; margin: 14px 0 6px; color: var(--muted); font-weight: 600; }
.quote-summary .status-list .row.neg b { color: var(--bad); }
.q-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 16px; }
.btn-row { display: flex; gap: 6px; flex-wrap: wrap; }
.btn-row .btn { flex: 1; }
.btn.accent-outline { border-color: var(--accent); color: #b4500e; background: var(--accent-soft); }
.btn.accent-outline:hover { background: #ffe5d0; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.items-table td { padding: 8px 6px; vertical-align: middle; }
.items-table input, .items-table select { padding: 6px 8px; border-radius: 7px; }
.items-table .item-product { margin-bottom: 4px; }
.items-table .w-qty { width: 84px; } .items-table .w-unit { width: 80px; } .items-table .w-money { width: 104px; }
.items-table input.num { text-align: right; }
.stock-pick { display: flex; flex-direction: column; gap: 4px; }
.stock-pick .per { flex-direction: row; align-items: center; gap: 4px; font-weight: 400; color: var(--muted); }
.stock-pick .per input { width: 70px; }
.calc summary { cursor: pointer; font-weight: 500; color: var(--brand-2); }
.calc-body { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; padding: 10px 0 0; }
.calc-body label { width: 160px; }
.calc-out { flex: 1 1 100%; font-size: 14px; background: var(--surface-2); border-radius: 9px; padding: 8px 12px; }

/* ---------- chart ---------- */
.chart svg { width: 100%; height: auto; display: block; }
.chart .legend { display: flex; gap: 16px; font-size: 13px; color: var(--muted); margin-bottom: 6px; flex-wrap: wrap; }
.chart .legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.status-list { display: flex; flex-direction: column; gap: 10px; }
.status-list .row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }

/* ---------- forms / modal ---------- */
.modal { position: fixed; inset: 0; background: rgba(10, 30, 36, .45); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal-card { background: var(--surface); border-radius: 16px; width: min(620px, 100%); max-height: calc(100vh - 32px); display: flex; flex-direction: column; box-shadow: 0 24px 70px rgba(0,0,0,.3); }
.modal-head { padding: 18px 22px 0; display: flex; justify-content: space-between; align-items: center; }
.modal-head h2 { font-size: 18px; }
.modal-close { border: 0; background: transparent; font-size: 22px; cursor: pointer; color: var(--muted); }
.modal-body { padding: 16px 22px; overflow-y: auto; }
.modal-foot { padding: 14px 22px 18px; display: flex; gap: 8px; border-top: 1px solid var(--border); }
.modal-foot .spacer { flex: 1; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid .full { grid-column: 1 / -1; }
.hint { font-size: 12.5px; color: var(--muted); font-weight: 400; }
.hint.warn { color: var(--warn); margin-top: 4px; }
.settings-form { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.settings-form .full { grid-column: 1 / -1; }
.calc-table td:first-child { color: var(--muted); }
.calc-table td.num { font-weight: 600; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--brand); color: #fff; padding: 10px 18px; border-radius: 10px; z-index: 60; box-shadow: var(--shadow); max-width: calc(100% - 32px); }
.toast.err { background: var(--bad); }

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  .grid.two, .grid.half, .grid.quote-grid { grid-template-columns: 1fr; }
}
@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; left: 0; top: 0; bottom: 0; width: 250px; z-index: 40; transform: translateX(-100%); transition: transform .2s; }
  .app.nav-open .sidebar { transform: none; }
  .app.nav-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 30; }
  .menu-btn { display: block; }
  .topbar { padding: 12px 16px 4px; }
  .topbar h1 { font-size: 20px; }
  .view { padding: 10px 16px 32px; }
  .form-grid, .settings-form { grid-template-columns: 1fr; }
  .stat-row { grid-template-columns: 1fr; }
  .toolbar input[type=search] { max-width: none; flex: 1 1 100%; }
  .kpi-value { font-size: 21px; }
  td.wrap { min-width: 150px; }
  th, td { padding: 9px 10px; }
}
