/* JT AI Accounting — รองรับคอมและมือถือ, โหมดสว่าง/มืดตามเครื่อง */
:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f8fafb;
  --border: #e3e8ec;
  --text: #17222b;
  --muted: #64727e;
  --primary: #0f5f63;
  --primary-ink: #ffffff;
  --primary-soft: #e3f1f1;
  --accent: #c78a1c;
  --pos: #18794e;
  --neg: #b4382c;
  --in: #1f8a70;
  --ex: #d9822b;
  --warn-bg: #fff6e0;
  --warn-ink: #7a5300;
  --err-bg: #fdecea;
  --err-ink: #8c2318;
  --ok-bg: #e6f4ec;
  --ok-ink: #155e3b;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 32, .04), 0 2px 8px rgba(16, 24, 32, .05);
  --font: "Noto Sans Thai", "Sarabun", "Leelawadee UI", "Thonburi", "Tahoma", system-ui, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1418; --surface: #161d22; --surface-2: #1b242a; --border: #2a353d; --text: #e6edf2; --muted: #93a2ad;
    --primary: #4fb3b4; --primary-ink: #06222a; --primary-soft: #183236; --pos: #5ccf96; --neg: #f08070;
    --in: #4fc3a1; --ex: #f0a35c; --warn-bg: #3a2e12; --warn-ink: #f3d38a; --err-bg: #3d1b17; --err-ink: #f7b1a8;
    --ok-bg: #143224; --ok-ink: #9be2bd; --shadow: none; color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font-family: var(--font); font-size: 15px; line-height: 1.55; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.45rem; margin: 0; line-height: 1.3; }
h2 { font-size: 1rem; margin: 0; }
.icon { width: 20px; height: 20px; flex: none; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.mono { font-family: ui-monospace, "SF Mono", Consolas, monospace; letter-spacing: .02em; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.pos { color: var(--pos); } .neg { color: var(--neg); }

.mockbar { background: var(--accent); color: #fff; text-align: center; font-size: .8rem; padding: 4px 12px; }

/* โครงหน้า */
.shell { display: flex; min-height: calc(100vh - 28px); }
.sidebar { width: 240px; flex: none; background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 20px 14px; position: sticky; top: 0; height: 100vh; }
.main { flex: 1; min-width: 0; }
.main-solo { display: flex; justify-content: center; }
.page { max-width: 1180px; margin: 0 auto; padding: 24px 28px 48px; }
.main-solo .page { width: 100%; max-width: 440px; }
.pagehead { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.pagehead-actions { display: flex; gap: 8px; align-items: center; }

.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { width: 36px; height: 36px; border-radius: 10px; background: var(--primary); color: var(--primary-ink);
  display: grid; place-items: center; font-weight: 700; letter-spacing: .03em; flex: none; }
.brand-name { font-weight: 700; }
.brand-sub { font-size: .75rem; color: var(--muted); }
.brand-lg .brand-mark { width: 44px; height: 44px; font-size: 1.1rem; }

.side-nav { display: flex; flex-direction: column; gap: 4px; margin-top: 28px; }
.side-nav a { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 9px; color: var(--text); }
.side-nav a:hover { background: var(--surface-2); text-decoration: none; }
.side-nav a.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.side-foot { margin-top: auto; border-top: 1px solid var(--border); padding-top: 14px; display: flex; flex-direction: column; gap: 10px; }
.who-name { font-weight: 600; font-size: .9rem; }
.who-role { display: inline-block; font-size: .72rem; padding: 1px 8px; border-radius: 99px; background: var(--surface-2); color: var(--muted); }
.who-role.role-executive { background: var(--primary-soft); color: var(--primary); }
.btn-ghost { display: flex; align-items: center; gap: 8px; background: none; border: 0; color: var(--muted); cursor: pointer; padding: 6px 0; font: inherit; }
.btn-ghost:hover { color: var(--text); }
.topbar, .bottomnav { display: none; }

/* การ์ด */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); margin-bottom: 16px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.grid-2 { display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr); gap: 16px; align-items: start; }
.grid-2.detail { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
.stack { display: flex; flex-direction: column; gap: 12px; }
.grid-2 > .card, .stack > .card { margin-bottom: 0; }
.note { margin: -4px 0 16px; }

.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin-bottom: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.kpi-label { font-size: .82rem; color: var(--muted); }
.kpi-value { font-size: 1.45rem; font-weight: 700; font-variant-numeric: tabular-nums; margin: 2px 0; overflow-wrap: anywhere; }
.kpi-sub { font-size: .76rem; color: var(--muted); }

.pills { display: flex; flex-wrap: wrap; gap: 8px; }
.pill { display: flex; align-items: baseline; gap: 6px; padding: 8px 14px; border-radius: 10px; background: var(--surface-2); border: 1px solid var(--border); color: var(--text); }
.pill:hover { text-decoration: none; border-color: var(--primary); }
.pill-n { font-weight: 700; font-size: 1.1rem; font-variant-numeric: tabular-nums; }

/* กราฟ */
.chart-wrap { width: 100%; overflow: hidden; }
.chart { width: 100%; height: auto; display: block; }
.chart .grid { stroke: var(--border); stroke-width: 1; }
.chart .axis { fill: var(--muted); font-size: 11px; font-family: var(--font); }
.chart .bar-in { fill: var(--in); } .chart .bar-ex { fill: var(--ex); }
.legend { display: flex; align-items: center; gap: 6px; font-size: .8rem; color: var(--muted); }
.lg { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-left: 6px; }
.lg-in { background: var(--in); } .lg-ex { background: var(--ex); }
.cats { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 12px; }
.cat-row { display: flex; justify-content: space-between; gap: 8px; font-size: .88rem; }
.cat-bar { width: 100%; height: 6px; display: block; margin: 4px 0 2px; }
.cat-bar .track { fill: var(--surface-2); } .cat-bar .fill { fill: var(--ex); }

/* ปุ่มและฟอร์ม */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 8px 16px; border-radius: 9px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; font-weight: 500; cursor: pointer; min-height: 40px; }
.btn:hover { border-color: var(--primary); text-decoration: none; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn-primary:hover { filter: brightness(1.08); }
.btn-danger { color: var(--neg); border-color: var(--neg); }
.btn-block { width: 100%; }
.btn-sm { padding: 5px 12px; min-height: 34px; font-size: .85rem; }
.btn.disabled { opacity: .45; pointer-events: none; }
.btn-row { display: flex; gap: 8px; justify-content: flex-end; }
label { display: flex; flex-direction: column; gap: 4px; font-size: .84rem; color: var(--muted); }
input, select { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 9px; padding: 8px 10px; min-height: 40px; width: 100%; }
input:focus, select:focus, .btn:focus-visible { outline: 2px solid var(--primary); outline-offset: 1px; }
.inline-form select { min-width: 150px; }
form.stack { gap: 14px; }

.filters { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; }
.filters label { min-width: 150px; }
.filters .grow { flex: 1; min-width: 220px; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.span-2 { grid-column: span 2; }
.confirm-form, .reject-form { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-top: 14px; padding-top: 14px; border-top: 1px dashed var(--border); }
.confirm-form p { width: 100%; margin: 0; }
.confirm-form input, .reject-form input { flex: 1; min-width: 180px; }
.approve-row { display: flex; flex-direction: column; gap: 4px; }
.approve-row .reject-form { margin-top: 10px; }

/* ตาราง */
.table-card { padding: 0; overflow: hidden; }
.table-card .card-head { padding: 16px 18px 0; }
.table { width: 100%; border-collapse: collapse; font-size: .9rem; }
.table th { text-align: left; font-weight: 600; color: var(--muted); font-size: .78rem; padding: 10px 14px; border-bottom: 1px solid var(--border); background: var(--surface-2); }
.table td { padding: 10px 14px; border-bottom: 1px solid var(--border); vertical-align: top; }
.table th.num { text-align: right; }
.table tbody tr[data-href] { cursor: pointer; }
.table tbody tr:hover { background: var(--surface-2); }
.table .wrap { max-width: 320px; overflow-wrap: anywhere; }
.empty-row { text-align: center; color: var(--muted); padding: 28px !important; }
.pager { display: flex; gap: 8px; justify-content: flex-end; padding: 12px 14px; }

.badge { display: inline-block; padding: 2px 10px; border-radius: 99px; font-size: .76rem; font-weight: 600; white-space: nowrap; }
.st-queued { background: #e8eef8; color: #2e4f8a; }
.st-needs_review { background: var(--warn-bg); color: var(--warn-ink); }
.st-ready { background: var(--primary-soft); color: var(--primary); }
.st-approved { background: var(--ok-bg); color: var(--ok-ink); }
.st-rejected, .st-failed { background: var(--err-bg); color: var(--err-ink); }
@media (prefers-color-scheme: dark) { .st-queued { background: #1c2a40; color: #9fbdf0; } }
.pill.st-needs_review, .pill.st-ready, .pill.st-approved, .pill.st-rejected, .pill.st-queued, .pill.st-failed { border-color: transparent; }
.dir { font-size: .82rem; font-weight: 600; }
.dir-income { color: var(--in); } .dir-expense { color: var(--ex); }

/* รายละเอียด */
.kvs { margin: 0; }
.kv { display: grid; grid-template-columns: 140px 1fr; gap: 10px; padding: 7px 0; border-bottom: 1px dashed var(--border); }
.kv:last-child { border-bottom: 0; }
.kv dt { color: var(--muted); font-size: .85rem; }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.file-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-top: 14px; }
.history { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px; font-size: .86rem; }

.alert { border-radius: 10px; padding: 10px 14px; margin-bottom: 14px; font-size: .9rem; }
.alert ul { margin: 6px 0 0; padding-left: 20px; }
.alert-warn { background: var(--warn-bg); color: var(--warn-ink); }
.alert-error { background: var(--err-bg); color: var(--err-ink); }
.alert-ok { background: var(--ok-bg); color: var(--ok-ink); }

/* อัปโหลด */
.upload-form { display: flex; flex-direction: column; gap: 14px; }
.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.seg label { flex-direction: row; align-items: center; gap: 8px; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; color: var(--text); cursor: pointer; }
.seg input { width: auto; min-height: 0; accent-color: var(--primary); }
.seg label:has(input:checked) { border-color: var(--primary); background: var(--primary-soft); }
.dropzone { align-items: center; text-align: center; gap: 6px; padding: 28px 16px; border: 2px dashed var(--border);
  border-radius: var(--radius); color: var(--text); cursor: pointer; position: relative; }
.dropzone:hover, .dropzone.dragging { border-color: var(--primary); background: var(--primary-soft); }
.dropzone .icon { width: 34px; height: 34px; color: var(--primary); }
.dropzone input { position: absolute; inset: 0; opacity: 0; cursor: pointer; min-height: 0; }
.results { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; font-size: .9rem; }
.results .ok { color: var(--pos); } .results .bad { color: var(--neg); }

/* ล็อกอิน / ผิดพลาด */
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 28px; box-shadow: var(--shadow); margin-top: 6vh; }
.login-card .brand { margin-bottom: 22px; }
.main-solo .pagehead { display: none; }
.empty { text-align: center; padding: 40px 18px; }
.err-code { font-size: 2.6rem; font-weight: 700; color: var(--muted); }

/* ---------- แท็บเล็ต ---------- */
@media (max-width: 1080px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-2, .grid-2.detail { grid-template-columns: minmax(0, 1fr); }
}

/* ---------- มือถือ ---------- */
@media (max-width: 760px) {
  body { font-size: 15px; }
  .sidebar { display: none; }
  .topbar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5; }
  .topbar .brand { flex: 1; min-width: 0; }
  .topbar .brand-mark { width: 30px; height: 30px; font-size: .85rem; }
  .topbar .who { text-align: right; }
  .topbar .who-name { font-size: .8rem; }
  .topbar .logout-form span { display: none; }
  .page { padding: 16px 16px 96px; }
  h1 { font-size: 1.2rem; }
  .pagehead-actions { width: 100%; }
  .pagehead-actions > * { flex: 1; }
  .kpi-value { font-size: 1.15rem; }
  .card { padding: 14px; }
  .filters label, .filters .grow { min-width: 0; flex: 1 1 calc(50% - 10px); }
  .filters .grow { flex-basis: 100%; }
  .filters .btn { flex-basis: 100%; }
  .form-grid { grid-template-columns: 1fr; }
  .span-2 { grid-column: auto; }
  .kv { grid-template-columns: 110px 1fr; }
  .seg { grid-template-columns: 1fr; }

  /* ตารางเปลี่ยนเป็นการ์ดบนมือถือ */
  .table.responsive thead { display: none; }
  .table.responsive, .table.responsive tbody, .table.responsive tr, .table.responsive td { display: block; width: 100%; }
  .table.responsive tr { padding: 10px 14px; border-bottom: 1px solid var(--border); }
  .table.responsive td { border: 0; padding: 3px 0; display: flex; justify-content: space-between; gap: 12px; text-align: right; }
  .table.responsive td::before { content: attr(data-label); color: var(--muted); font-size: .8rem; text-align: left; flex: none; }
  .table.responsive td > div { text-align: right; }
  .table.responsive td.wrap { max-width: none; }
  .table.responsive td.empty-row { display: block; text-align: center; }
  .table.responsive td.empty-row::before { content: none; }

  .bottomnav { display: flex; position: fixed; bottom: 0; left: 0; right: 0; z-index: 10; background: var(--surface);
    border-top: 1px solid var(--border); padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); }
  .bottomnav a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: .7rem; color: var(--muted); padding: 4px 0; }
  .bottomnav a.active { color: var(--primary); font-weight: 600; }
  .bottomnav a:hover { text-decoration: none; }
}

/* บัญชีผู้ใช้ */
a.who { color: var(--text); display: block; border-radius: 8px; }
a.who:hover { text-decoration: none; }
a.who:hover .who-name { color: var(--primary); }
.side-foot a.btn-ghost { text-decoration: none; }
.side-foot a.btn-ghost.active { color: var(--primary); font-weight: 600; }
.grid-2.narrow { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); max-width: 900px; }
h3.sub { font-size: .9rem; margin: 18px 0 6px; }
.tips { margin: 0; padding-left: 20px; font-size: .86rem; color: var(--muted); display: flex; flex-direction: column; gap: 4px; }
.temp-pw .temp-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin: 8px 0; }
.temp-pw code.big { font-size: 1.3rem; background: var(--surface); color: var(--text); padding: 4px 12px; border-radius: 8px; letter-spacing: .08em; user-select: all; }
.temp-pw p { margin: 0; }
.add-user { grid-template-columns: 1fr 1fr 180px auto; align-items: end; }
.btn-row.end { align-items: flex-end; }
.user-form { display: grid; grid-template-columns: minmax(140px, 1fr) 130px 120px auto; gap: 8px; align-items: center; }
.user-form input, .user-form select { min-height: 36px; padding: 6px 8px; }
.user-form select:disabled { opacity: .6; }
form.inline { display: inline; }
.table.users td { vertical-align: middle; }
@media (max-width: 1080px) {
  .add-user { grid-template-columns: 1fr 1fr; }
  .grid-2.narrow { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 760px) {
  .add-user { grid-template-columns: 1fr; }
  .topbar a.who .who-role { display: none; }
  .table.responsive.users td.user-edit { display: block; }
  .table.responsive.users td.user-edit::before { display: block; margin-bottom: 6px; }
  .user-form { grid-template-columns: 1fr 1fr; }
  .user-form input { grid-column: span 2; }
  .user-form button { grid-column: span 2; }
}

/* ร้านค้า / ขายตรง */
.inline-form { display: flex; gap: 8px; flex-wrap: wrap; }
.pagehead .inline-form select { width: auto; min-width: 170px; }
.pad { padding: 0 18px 14px; margin: 8px 0 0; }
.total-row td { background: var(--surface-2); }
.store-form { grid-template-columns: minmax(160px, 1fr) 190px 120px auto; }
.sale-form label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); align-self: end; min-height: 40px; }
.sale-form label.check input { width: auto; min-height: 0; accent-color: var(--primary); }
.sale-form input[type=file] { padding: 6px; }
@media (max-width: 760px) {
  .inline-form { width: 100%; }
  .pagehead .inline-form select { flex: 1 1 100%; width: 100%; }
  .store-form { grid-template-columns: 1fr 1fr; }
}

/* รายงาน */
.kpis.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.report-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.report-item { display: flex; align-items: center; gap: 12px; padding: 12px 14px; border: 1px solid var(--border); border-radius: 10px; color: var(--text); }
.report-item:hover { border-color: var(--primary); text-decoration: none; background: var(--surface-2); }
.report-item > span:first-of-type { flex: 1; display: flex; flex-direction: column; gap: 2px; }
.report-item .icon { color: var(--primary); width: 26px; height: 26px; }
@media (max-width: 760px) {
  .kpis.three { grid-template-columns: minmax(0, 1fr); }
  .report-item { flex-wrap: wrap; }
  .report-item .btn { width: 100%; }
}
.btn-row.start { justify-content: flex-start; flex-wrap: wrap; }
.backup-list { margin: 12px 0 0; padding-left: 18px; color: var(--muted); }
.grid-2.even { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
.cat-form { grid-template-columns: minmax(120px, 1fr) 120px auto; }
@media (max-width: 1080px) { .grid-2.even { grid-template-columns: minmax(0, 1fr); } }
