:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --ink: #1b2330;
  --ink-2: #5b6575;
  --line: #e3e6eb;
  --line-2: #eef0f3;
  --navy: #17325c;
  --navy-2: #22457a;
  --buy: #1f5fbf;
  --buy-bg: #eaf1fc;
  --pay: #13804a;
  --pay-bg: #e8f5ee;
  --due: #c0392b;
  --due-bg: #fdeeec;
  --warn: #9a6b00;
  --warn-bg: #fff6dd;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .04);
  font-size: 15px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: 'IBM Plex Sans Thai', system-ui, -apple-system, 'Segoe UI', Tahoma, sans-serif;
  line-height: 1.55;
}
a { color: var(--buy); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2 { margin: 0; line-height: 1.3; }
h1 { font-size: 1.55rem; font-weight: 700; }
h2 { font-size: 1.05rem; font-weight: 600; margin-bottom: .75rem; }
.muted { color: var(--ink-2); font-weight: 400; }
.small { font-size: .85rem; }
.strong { font-weight: 600; }
.nowrap { white-space: nowrap; }
.neg { color: var(--due); }
.pos { color: var(--pay); }
.pre { white-space: pre-wrap; margin: 0; }
.clamp { display: -webkit-box; -webkit-line-clamp: 1; -webkit-box-orient: vertical; overflow: hidden; }

/* ---------- Top bar ---------- */
.topbar { background: var(--navy); color: #fff; position: sticky; top: 0; z-index: 20; }
.topbar-in {
  max-width: 1240px; margin: 0 auto; padding: 0 20px;
  display: flex; align-items: center; gap: 24px; min-height: 58px; flex-wrap: wrap;
}
.brand { color: #fff; font-weight: 700; font-size: 1.05rem; display: inline-flex; align-items: center; gap: 10px; }
.brand:hover { text-decoration: none; }
.brand-mark {
  width: 30px; height: 30px; border-radius: 8px; background: #f2c94c; color: var(--navy);
  display: inline-grid; place-items: center; font-weight: 700;
}
.brand.big { color: var(--navy); font-size: 1.3rem; margin-bottom: 1.25rem; }
.nav { display: flex; gap: 4px; }
.nav a { color: #c9d5ea; padding: 8px 12px; border-radius: 8px; font-weight: 500; }
.nav a:hover { color: #fff; background: rgba(255, 255, 255, .08); text-decoration: none; }
.nav a.on { color: #fff; background: rgba(255, 255, 255, .14); }
.actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.usermenu { position: relative; }
.usermenu summary {
  list-style: none; cursor: pointer; padding: 6px 10px; border-radius: 8px; color: #fff;
  border: 1px solid rgba(255, 255, 255, .25); font-size: .9rem;
}
.usermenu summary::-webkit-details-marker { display: none; }
.usermenu summary::after { content: ' ▾'; opacity: .7; }
.usermenu .menu {
  position: absolute; right: 0; top: calc(100% + 6px); background: #fff; border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .15); min-width: 180px; padding: 6px; z-index: 30;
}
.usermenu .menu a, .usermenu .menu button {
  display: block; width: 100%; text-align: left; padding: 8px 12px; border-radius: 6px; color: var(--ink);
  background: none; border: 0; font: inherit; cursor: pointer;
}
.usermenu .menu a:hover, .usermenu .menu button:hover { background: var(--bg); text-decoration: none; }

/* ---------- Layout ---------- */
.wrap { max-width: 1240px; margin: 0 auto; padding: 24px 20px 64px; }
.page-head {
  display: flex; align-items: flex-end; justify-content: space-between; gap: 16px;
  margin-bottom: 20px; flex-wrap: wrap;
}
.head-actions { display: flex; gap: 8px; }
.back { display: inline-block; font-size: .9rem; margin-bottom: 4px; }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 20px; margin-bottom: 20px; box-shadow: var(--shadow);
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.grid-2 { display: grid; grid-template-columns: 3fr 2fr; gap: 20px; align-items: start; }
.grid-2 > .card { margin-bottom: 0; min-width: 0; }
.empty { color: var(--ink-2); margin: 8px 0; }

/* ---------- Stats ---------- */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 20px; }
.stats-3 { grid-template-columns: repeat(3, 1fr); }
.stat {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px 18px; display: flex; flex-direction: column; gap: 2px; color: var(--ink);
  box-shadow: var(--shadow); border-top: 4px solid var(--line);
}
a.stat:hover { text-decoration: none; border-color: #c9ced6; }
.stat-label { font-size: .9rem; color: var(--ink-2); font-weight: 500; }
.stat-value { font-size: 1.65rem; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.stat-sub { font-size: .85rem; color: var(--ink-2); }
.stat-buy { border-top-color: var(--buy); }
.stat-buy .stat-value { color: var(--buy); }
.stat-pay { border-top-color: var(--pay); }
.stat-pay .stat-value { color: var(--pay); }
.stat-due { border-top-color: var(--due); background: linear-gradient(var(--due-bg), #fff 70%); }
.stat-due .stat-value { color: var(--due); }
.stat-change { border-top-color: #8a93a3; }
.stat-change .stat-value { font-size: 1.35rem; padding-top: .3rem; }

.month-nav { display: flex; align-items: center; gap: 6px; background: #fff; border: 1px solid var(--line); border-radius: 999px; padding: 4px; }
.month-nav strong { padding: 0 10px; font-weight: 600; }
.month-nav a { width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; font-size: 1.2rem; color: var(--ink); }
.month-nav a:hover { background: var(--bg); text-decoration: none; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; margin: 0 -20px; padding: 0 20px; }
.table { width: 100%; border-collapse: collapse; font-size: .95rem; }
.table th {
  text-align: left; font-weight: 600; color: var(--ink-2); font-size: .82rem;
  padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; background: #fafbfc;
}
.table td { padding: 10px; border-bottom: 1px solid var(--line-2); vertical-align: top; }
.table tbody tr:last-child td { border-bottom: 0; }
.table tfoot td { border-top: 2px solid var(--line); font-weight: 600; padding: 10px; }
.table .num, .items-table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table .c, .items-table .c { text-align: center; }
tr.rowlink { cursor: pointer; }
tr.rowlink:hover td { background: #f7f9fc; }
.summary-row {
  display: flex; flex-wrap: wrap; gap: 8px 24px; padding-bottom: 12px; margin-bottom: 4px;
  border-bottom: 1px solid var(--line); color: var(--ink-2);
}
.summary-row strong { color: var(--ink); font-variant-numeric: tabular-nums; }
.summary-row strong.neg { color: var(--due); }

.badge { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: .8rem; font-weight: 600; white-space: nowrap; }
.badge.ok { background: var(--pay-bg); color: var(--pay); }
.badge.part { background: var(--warn-bg); color: var(--warn); }
.badge.due { background: var(--due-bg); color: var(--due); }

.pager { display: flex; gap: 4px; justify-content: center; margin-top: 16px; flex-wrap: wrap; }
.pager a, .pager span { min-width: 34px; height: 34px; display: grid; place-items: center; border-radius: 8px; padding: 0 8px; }
.pager a:hover { background: var(--bg); text-decoration: none; }
.pager span.on { background: var(--navy); color: #fff; }

/* ---------- Filters & forms ---------- */
.filters { display: flex; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; align-items: center; }
.filters input[type=search] { flex: 1 1 280px; }
input, select, textarea {
  font: inherit; color: var(--ink); background: #fff; border: 1px solid #cfd4dc; border-radius: 8px;
  padding: 8px 11px; width: 100%; min-height: 40px;
}
.filters input, .filters select { width: auto; }
input:focus, select:focus, textarea:focus { outline: 2px solid rgba(31, 95, 191, .25); border-color: var(--buy); }
input[readonly] { background: #f5f7fa; }
input.invalid { border-color: var(--due); background: var(--due-bg); }
textarea { resize: vertical; }
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 14px; }
.label { font-weight: 500; font-size: .92rem; }
.hint { font-size: .85rem; color: var(--ink-2); }
.form-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0 16px; }
.span-2 { grid-column: span 2; }
.span-3 { grid-column: span 3; }
.form.narrow { max-width: 460px; }
.section-title { margin: 10px 0 10px; font-size: 1rem; }
.section-title .muted { font-size: .85rem; margin-left: 4px; }
.section-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.section-row input[type=search] { width: 240px; }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--line); }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px 2px; margin: 0 0 16px; }
legend { font-weight: 600; padding: 0 6px; }

.items-table { width: 100%; border-collapse: collapse; min-width: 640px; }
.items-table th { font-size: .82rem; color: var(--ink-2); font-weight: 600; text-align: left; padding: 6px; }
.items-table td { padding: 4px; vertical-align: middle; }
.items-table th:nth-child(3), .items-table td:nth-child(3) { width: 100px; }
.items-table th:nth-child(4), .items-table td:nth-child(4) { width: 90px; }
.items-table th:nth-child(5), .items-table td:nth-child(5) { width: 140px; }
.items-table th:nth-child(6), .items-table td:nth-child(6) { width: 130px; }
.items-table th:nth-child(7), .items-table td:nth-child(7) { width: 40px; }
.items-table th.num { padding-right: 12px; }
.items-table .amount { padding-right: 12px; font-weight: 500; }
.line-no { color: var(--ink-2); width: 32px; }
.icon-btn {
  width: 32px; height: 32px; border-radius: 8px; border: 1px solid transparent; background: none; cursor: pointer;
  font-size: 1.2rem; color: var(--ink-2);
}
.icon-btn:hover { background: var(--due-bg); color: var(--due); }

.total-box {
  display: flex; flex-direction: column; align-items: flex-end; gap: 2px;
  margin: 16px 0 6px; padding-top: 14px; border-top: 1px dashed var(--line);
}
.total-box .field { width: 260px; margin: 0; }
.big-input { font-size: 1.35rem; font-weight: 700; text-align: right; }
.big-num { font-size: 1.8rem; font-weight: 700; color: var(--pay); font-variant-numeric: tabular-nums; }

.alloc-table td { vertical-align: middle; }
.alloc-cell { display: flex; gap: 6px; justify-content: flex-end; align-items: center; }
.alloc-cell input { width: 140px; text-align: right; }
tr.picked td { background: var(--pay-bg); }
.chip {
  border: 1px solid #cfd4dc; background: #fff; border-radius: 999px; padding: 4px 12px; cursor: pointer;
  font: inherit; font-size: .82rem; color: var(--ink-2);
}
.chip:hover { border-color: var(--pay); color: var(--pay); }

.dropzone {
  position: relative; display: flex; align-items: center; justify-content: center; text-align: center;
  border: 2px dashed #c4cad4; border-radius: var(--radius); padding: 18px; background: #fafbfc;
  color: var(--ink-2); cursor: pointer; min-height: 70px;
}
.dropzone:hover, .dropzone.has-files { border-color: var(--buy); background: var(--buy-bg); color: var(--buy); }
.dropzone input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; height: 100%; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--buy); color: #fff; border: 1px solid transparent; border-radius: 8px;
  padding: 8px 16px; font: inherit; font-weight: 600; cursor: pointer; min-height: 40px; white-space: nowrap;
}
.btn:hover { filter: brightness(1.07); text-decoration: none; }
.btn-sm { min-height: 32px; padding: 4px 12px; font-size: .88rem; }
.btn-lg { min-height: 46px; padding: 10px 22px; }
.btn.block { width: 100%; }
.btn-pay { background: var(--pay); }
.btn-ghost { background: #fff; color: var(--ink); border-color: #cfd4dc; }
.btn-ghost:hover { background: var(--bg); filter: none; }
.btn-danger { background: #fff; color: var(--due); border-color: #e7b3ad; }
.btn-danger:hover { background: var(--due-bg); filter: none; }
.topbar .btn-sm { background: rgba(255, 255, 255, .12); border-color: rgba(255, 255, 255, .25); }
.topbar .btn-sm.btn-pay { background: var(--pay); border-color: transparent; }
.linkbtn { background: none; border: 0; padding: 0; font: inherit; color: var(--buy); cursor: pointer; }
.linkbtn:hover { text-decoration: underline; }
.linkbtn.danger { color: var(--due); font-size: .85rem; }
.inline-form { display: flex; gap: 6px; }
.inline-form input { width: 140px; min-height: 32px; padding: 4px 8px; }
.danger-zone { display: flex; justify-content: flex-end; }

/* ---------- Files ---------- */
.files { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; margin-bottom: 14px; }
.files > .muted { grid-column: 1 / -1; margin: 0; }
.file { display: flex; gap: 10px; border: 1px solid var(--line); border-radius: 8px; padding: 8px; align-items: center; }
.file-thumb {
  width: 64px; height: 64px; flex: none; border-radius: 6px; overflow: hidden; background: var(--bg);
  display: grid; place-items: center;
}
.file-thumb img { width: 100%; height: 100%; object-fit: cover; }
.file-ext { font-weight: 700; font-size: .8rem; color: var(--ink-2); }
.file-meta { display: flex; flex-direction: column; min-width: 0; font-size: .85rem; }
.file-name { font-weight: 500; font-size: .92rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.upload-more { display: flex; gap: 10px; align-items: stretch; }
.upload-more .dropzone { flex: 1; min-height: 46px; padding: 10px; }

/* ---------- Flash ---------- */
.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 14px; border: 1px solid; }
.flash.success { background: var(--pay-bg); border-color: #b5dec8; color: #0d5c35; }
.flash.error { background: var(--due-bg); border-color: #efc1bb; color: #8f2a1f; }
.flash.warn { background: var(--warn-bg); border-color: #f0dc9f; color: #6f4d00; }

/* ---------- Login / install ---------- */
.center-page { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth-card {
  width: 100%; max-width: 380px; background: #fff; border: 1px solid var(--line);
  border-radius: 14px; padding: 28px; box-shadow: 0 10px 30px rgba(16, 24, 40, .08);
}
.auth-card.wide { max-width: 560px; }
.auth-card h1 { margin-bottom: 18px; font-size: 1.35rem; }
textarea.code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .8rem; }

/* ---------- Responsive ---------- */
@media (max-width: 960px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
  .grid-2 { grid-template-columns: 1fr; }
  .grid-2 > .card { margin-bottom: 0; }
}
@media (max-width: 720px) {
  :root { font-size: 14.5px; }
  .topbar-in { gap: 8px 12px; padding: 8px 16px; }
  .nav { order: 3; width: 100%; overflow-x: auto; }
  .actions .btn-sm { display: none; }
  .wrap { padding: 16px 16px 48px; }
  .card { padding: 16px; }
  .table-wrap { margin: 0 -16px; padding: 0 16px; }
  .stats, .stats-3 { grid-template-columns: 1fr 1fr; gap: 10px; }
  .stat { padding: 12px 14px; }
  .stat-value { font-size: 1.3rem; }
  .form-grid { grid-template-columns: 1fr; }
  .span-2, .span-3 { grid-column: auto; }
  .total-box .field { width: 100%; }
  .upload-more { flex-direction: column; }
  .section-row input[type=search] { width: 100%; }
  .filters input, .filters select { flex: 1 1 140px; }

  /* ฟอร์มชำระบนมือถือ: แต่ละบิลเป็นการ์ด ไม่ต้องเลื่อนซ้ายขวา */
  .alloc-table thead { display: none; }
  .alloc-table, .alloc-table tbody { display: block; }
  .alloc-table tr.alloc-row {
    display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; padding: 12px 0;
    border-bottom: 1px solid var(--line-2);
  }
  .alloc-table tr.alloc-row[hidden] { display: none; }
  .alloc-table tr.alloc-row td { padding: 0 4px; border: 0; }
  .alloc-table td:nth-child(1) { grid-column: 1; grid-row: 1; font-size: .85rem; color: var(--ink-2); }
  .alloc-table td:nth-child(2) { grid-column: 1; grid-row: 2; }
  .alloc-table td:nth-child(3) { display: none; }
  .alloc-table td:nth-child(4) { grid-column: 2; grid-row: 1 / span 2; align-self: center; }
  .alloc-table td:nth-child(4)::before { content: 'คงค้าง'; display: block; font-size: .78rem; color: var(--ink-2); font-weight: 400; }
  .alloc-table td.alloc-cell { grid-column: 1 / -1; grid-row: 3; margin-top: 8px; }
  .alloc-table td.alloc-cell input { flex: 1; width: auto; }
  tr.picked { background: var(--pay-bg); }
}

/* ---------- กำหนดชำระ ---------- */
.due-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.due-row input[type=date] { width: 190px; flex: none; }
.chips { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.chip.on { background: var(--buy); border-color: var(--buy); color: #fff; }
.chip.credit:hover { border-color: var(--buy); color: var(--buy); }
.chip.credit.on:hover { color: #fff; }
.badge.open { background: #eef0f3; color: var(--ink-2); }
.badge.soon { background: var(--warn-bg); color: var(--warn); }
.warn-text { color: var(--warn); }
.hint.neg { color: var(--due); }
.alert-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  background: var(--due-bg); border: 1px solid #efc1bb; color: #8f2a1f;
  border-radius: var(--radius); padding: 12px 16px; margin-bottom: 20px;
}
.alert-bar.soon { background: var(--warn-bg); border-color: #f0dc9f; color: #6f4d00; }
.alert-bar strong { font-variant-numeric: tabular-nums; }
@media (max-width: 720px) {
  .due-row input[type=date] { width: 100%; }
}

/* ---------- บัญชี ---------- */
.account-sums { display: flex; gap: 8px; flex-wrap: wrap; padding: 12px 0 4px; }
.account-chip {
  display: inline-flex; gap: 8px; align-items: baseline; border: 1px solid var(--line); border-radius: 999px;
  padding: 4px 12px; color: var(--ink); font-size: .9rem; background: #fff;
}
.account-chip strong { font-variant-numeric: tabular-nums; }
.account-chip:hover { border-color: var(--pay); text-decoration: none; }
.account-chip.on { background: var(--pay-bg); border-color: var(--pay); }
.stats-3 .span-2 { grid-column: span 2; }
.stat-account { border-top-color: var(--navy-2); }
.small-value { font-size: 1.25rem; }
.change-account { margin-top: 8px; }
.change-account select { width: auto; min-height: 32px; padding: 4px 8px; }
.account-form input[name=name] { width: 180px; }
.account-form input[name=detail] { width: 280px; }

/* ---------- หน้าภาพรวม 3 ช่อง ---------- */
.wrap.wide { max-width: 1760px; }
.board { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.45fr) minmax(0, 1.1fr); gap: 16px; align-items: start; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.panel .table-wrap { margin: 0; padding: 0; max-height: 70vh; overflow: auto; }
.panel .empty { padding: 0 16px 12px; }
.panel-head {
  display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 0 12px 14px;
  color: var(--ink); text-align: center;
}
.panel-head:hover { text-decoration: none; filter: brightness(.98); }
.panel-title { align-self: stretch; color: #fff; font-weight: 600; padding: 6px; margin: 0 -12px 10px; }
.panel-value { font-size: 1.9rem; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.panel-sub { font-size: .88rem; color: var(--ink-2); }
.panel-buy .panel-head { background: var(--buy-bg); }
.panel-buy .panel-title { background: var(--navy-2); }
.panel-buy .panel-value { color: var(--buy); }
.panel-pay .panel-head { background: #fbecef; }
.panel-pay .panel-title { background: #c98aa0; }
.panel-pay .panel-value { color: var(--pay); }
.panel-due .panel-head { background: #fff4e8; }
.panel-due .panel-title { background: #b8322a; }
.panel-due .panel-value { color: var(--due); }
.table.compact { font-size: .88rem; }
.table.compact th { position: sticky; top: 0; z-index: 1; background: var(--navy); color: #fff; font-size: .8rem; }
.panel-pay .table.compact th { background: #c98aa0; }
.panel-due .table.compact th { background: #b8322a; }
.table.compact td { padding: 7px 8px; }
.table.compact th { padding: 7px 8px; }
@media (max-width: 1599px) {
  .board { grid-template-columns: 1fr; }
  .panel .table-wrap { max-height: none; }
}
@media (max-width: 720px) {
  .stats-3 .span-2 { grid-column: auto; }
  .account-form { flex-wrap: wrap; }
  .account-form input[name=name], .account-form input[name=detail] { width: 100%; }
}
.topbar-in.wide { max-width: 1760px; }
.table td.code { font-variant-numeric: tabular-nums; color: var(--ink-2); font-size: .85em; }
