:root {
  --brand: #0891b2; --brand-ink: #0e7490; --brand-soft: #e0f6fa;
  --bg: #f3f8fa; --card: #ffffff; --ink: #0f1f26; --muted: #5f7480; --line: #dce8ec;
  --ok: #15803d; --ok-soft: #dcfce7; --bad: #dc2626; --bad-soft: #fee2e2;
  --warn: #b45309; --warn-soft: #fef3c7; --info: #4f46e5; --info-soft: #e8e7fd;
  --radius: 14px; --shadow: 0 1px 2px rgba(0,0,0,.04), 0 4px 16px rgba(0,0,0,.05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --brand: #22b8d6; --brand-ink: #67e0f2; --brand-soft: #0c2a33;
    --bg: #0b1418; --card: #121e23; --ink: #eef6f8; --muted: #93a9b2; --line: #1f3038;
    --ok: #4ade80; --ok-soft: #14301f; --bad: #f87171; --bad-soft: #3a1717;
    --warn: #fbbf24; --warn-soft: #3a2a0c; --info: #a5b4fc; --info-soft: #1e1d45;
    --shadow: none; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font-family: "IBM Plex Sans Thai", system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 15px; line-height: 1.55; -webkit-font-smoothing: antialiased;
}
a { color: var(--brand-ink); }
.wrap { max-width: 760px; margin: 0 auto; padding: 16px 16px 96px; }
.wrap.wide { max-width: 1280px; }

/* top bar */
.topbar { background: var(--card); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20; }
.topbar .in { max-width: 1280px; margin: 0 auto; padding: 10px 16px; display: flex; align-items: center; gap: 12px; }
.logo { display: flex; align-items: center; gap: 10px; font-weight: 700; text-decoration: none; color: var(--ink); }
.logo .mark { width: 32px; height: 32px; border-radius: 9px; background: var(--brand); color: #fff; display: grid; place-items: center; font-size: 17px; }
.logo small { display: block; font-weight: 400; color: var(--muted); font-size: 12px; line-height: 1.2; }
.logo { white-space: nowrap; flex: none; }
@media (max-width: 720px) {
  .topbar .in { flex-wrap: wrap; row-gap: 6px; }
  .topbar nav { order: 3; width: 100%; margin-left: 0; flex-wrap: nowrap; overflow-x: auto; }
  .topbar nav a { white-space: nowrap; }
  #who { margin-left: auto; }
}
.topbar nav { margin-left: auto; display: flex; gap: 4px; flex-wrap: wrap; }
.topbar nav a { text-decoration: none; color: var(--muted); padding: 6px 10px; border-radius: 8px; font-size: 14px; }
.topbar nav a.on, .topbar nav a:hover { background: var(--brand-soft); color: var(--brand-ink); }

h1 { font-size: 24px; margin: 8px 0 4px; }
h2 { font-size: 17px; margin: 0 0 12px; display: flex; align-items: center; gap: 8px; }
h2 .n { width: 24px; height: 24px; border-radius: 50%; background: var(--brand); color: #fff; font-size: 13px; display: grid; place-items: center; flex: none; }
.sub { color: var(--muted); margin: 0 0 16px; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; margin-bottom: 14px; box-shadow: var(--shadow); }
.grid { display: grid; gap: 12px; grid-template-columns: 1fr 1fr; }
.grid .full { grid-column: 1 / -1; }
@media (max-width: 600px) { .grid { grid-template-columns: 1fr; } }

label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 4px; }
label .req { color: var(--bad); }
label .opt { color: var(--muted); font-weight: 400; }
input, select, textarea {
  width: 100%; font: inherit; color: var(--ink); background: var(--card);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; min-height: 44px;
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: -1px; border-color: transparent; }
input[readonly] { background: var(--bg); color: var(--muted); }
input:disabled, select:disabled { opacity: .55; }
.hint { font-size: 12.5px; color: var(--muted); margin-top: 4px; }

.btn {
  font: inherit; font-weight: 600; border: 1px solid var(--line); background: var(--card); color: var(--ink);
  border-radius: 10px; padding: 9px 14px; min-height: 40px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px; justify-content: center;
}
.btn:hover { border-color: var(--muted); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { filter: brightness(.95); }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.bad { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn.ghost { background: transparent; }
.btn.sm { padding: 5px 10px; min-height: 32px; font-size: 13px; }
.btn.lg { padding: 14px 20px; font-size: 16px; width: 100%; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* chips (ปุ่มเลือก) */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  font: inherit; font-size: 14px; border: 1.5px solid var(--line); background: var(--card); color: var(--ink);
  border-radius: 999px; padding: 7px 14px; cursor: pointer; display: inline-flex; align-items: center; gap: 6px;
}
.chip .dot { width: 10px; height: 10px; border-radius: 50%; }
.chip.on { border-color: var(--brand); background: var(--brand-soft); color: var(--brand-ink); font-weight: 600; }

.pick-cards { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
@media (max-width: 600px) { .pick-cards { grid-template-columns: 1fr; } }
.pick-card { border: 1.5px solid var(--line); border-radius: 12px; padding: 12px; cursor: pointer; background: var(--card); text-align: left; font: inherit; color: var(--ink); }
.pick-card.on { border-color: var(--brand); background: var(--brand-soft); }
.pick-card b { display: block; }

/* items */
.items { width: 100%; border-collapse: collapse; }
.items th { text-align: left; font-size: 12.5px; color: var(--muted); font-weight: 600; padding: 0 6px 6px; }
.items td { padding: 4px; vertical-align: top; }
.items td.amt { text-align: right; padding-top: 14px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.items .x { border: 0; background: transparent; color: var(--muted); font-size: 20px; cursor: pointer; padding: 8px; }
@media (max-width: 600px) {
  .items thead { display: none; }
  .items tr { display: grid; grid-template-columns: 1fr 1fr auto; gap: 6px; padding: 10px 0; border-bottom: 1px dashed var(--line); }
  .items td { padding: 0; }
  .items td.desc { grid-column: 1 / -1; }
  .items td.amt { grid-column: 1 / 3; text-align: left; padding: 0; }
}
.total-bar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--line); flex-wrap: wrap; }
.total-bar .amt { font-size: 26px; font-weight: 700; font-variant-numeric: tabular-nums; }

.badge { display: inline-flex; align-items: center; gap: 4px; border-radius: 999px; padding: 2px 10px; font-size: 12.5px; font-weight: 600; white-space: nowrap; }
.b-pending { background: var(--warn-soft); color: var(--warn); }
.b-paid { background: var(--ok-soft); color: var(--ok); }
.b-failed { background: var(--bad-soft); color: var(--bad); }
.b-cancelled { background: var(--line); color: var(--muted); }
.b-petty { background: var(--info-soft); color: var(--info); }
.b-company { background: var(--brand-soft); color: var(--brand-ink); }
.b-bill-ok { background: var(--ok-soft); color: var(--ok); }
.b-bill-no { background: var(--bad-soft); color: var(--bad); }
.b-dup { background: var(--bad); color: #fff; }

.notice { border-radius: 12px; padding: 12px 14px; font-size: 14px; }
.notice.info { background: var(--info-soft); color: var(--info); }
.notice.warn { background: var(--warn-soft); color: var(--warn); }
.notice.bad { background: var(--bad-soft); color: var(--bad); }
.notice.ok { background: var(--ok-soft); color: var(--ok); }

/* uploads */
.drop { border: 1.5px dashed var(--line); border-radius: 12px; padding: 14px; text-align: center; cursor: pointer; color: var(--muted); display: block; font-weight: 400; }
.drop:hover { border-color: var(--brand); color: var(--brand-ink); }
.drop input { display: none; }
.thumbs { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.thumb { position: relative; width: 84px; height: 84px; border-radius: 10px; overflow: hidden; border: 1px solid var(--line); background: var(--bg); display: grid; place-items: center; font-size: 12px; color: var(--muted); text-align: center; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb button { position: absolute; top: 3px; right: 3px; width: 22px; height: 22px; border-radius: 50%; border: 0; background: rgba(0,0,0,.6); color: #fff; cursor: pointer; line-height: 1; }
.thumb a { display: block; width: 100%; height: 100%; color: inherit; text-decoration: none; display: grid; place-items: center; }

.clock { display: inline-flex; align-items: center; gap: 8px; background: var(--card); border: 1px solid var(--line); border-radius: 999px; padding: 6px 14px; font-size: 14px; }
.clock b { font-variant-numeric: tabular-nums; }

.sticky-submit { position: fixed; left: 0; right: 0; bottom: 0; background: var(--card); border-top: 1px solid var(--line); padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); z-index: 15; }
.sticky-submit .in { max-width: 760px; margin: 0 auto; display: flex; gap: 12px; align-items: center; }
.sticky-submit .sum { flex: 1; font-size: 13px; color: var(--muted); }
.sticky-submit .sum b { display: block; font-size: 19px; color: var(--ink); font-variant-numeric: tabular-nums; }
.sticky-submit .btn { width: auto; min-width: 160px; }

/* modal */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 50; display: flex; align-items: flex-end; justify-content: center; padding: 0; }
@media (min-width: 700px) { .modal-bg { align-items: center; padding: 24px; } }
.modal { background: var(--card); color: var(--ink); width: 100%; max-width: 560px; max-height: 92vh; overflow: auto; border-radius: 18px 18px 0 0; padding: 20px; }
@media (min-width: 700px) { .modal { border-radius: 18px; } }
.modal.wide { max-width: 980px; }
.modal-head { display: flex; align-items: flex-start; gap: 12px; margin-bottom: 12px; }
.modal-head h3 { margin: 0; font-size: 19px; flex: 1; }
.modal .close { border: 0; background: var(--bg); border-radius: 50%; width: 34px; height: 34px; font-size: 18px; cursor: pointer; color: var(--ink); }
.row-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; margin-top: 16px; }

.toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 100; font-size: 14px; max-width: calc(100% - 32px); box-shadow: 0 6px 24px rgba(0,0,0,.2); }
.toast.err { background: var(--bad); color: #fff; }

.spinner { width: 18px; height: 18px; border-radius: 50%; border: 2.5px solid currentColor; border-right-color: transparent; animation: spin .7s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.num { font-variant-numeric: tabular-nums; }
.right { text-align: right; }
.flex { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.sp { flex: 1; }

/* dashboard */
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; margin-bottom: 14px; }
@media (max-width: 900px) { .stats { grid-template-columns: 1fr 1fr; } }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; cursor: pointer; text-align: left; font: inherit; color: var(--ink); }
.stat .k { font-size: 13px; color: var(--muted); }
.stat .v { font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .s { font-size: 12.5px; color: var(--muted); font-variant-numeric: tabular-nums; }
.filters { display: grid; grid-template-columns: 2fr repeat(5, 1fr); gap: 8px; margin-bottom: 12px; }
@media (max-width: 1000px) { .filters { grid-template-columns: 1fr 1fr; } .filters .q { grid-column: 1 / -1; } }
.tbl-wrap { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); overflow: auto; }
table.list { width: 100%; border-collapse: collapse; font-size: 14px; }
table.list th { text-align: left; font-size: 12.5px; color: var(--muted); font-weight: 600; padding: 10px; border-bottom: 1px solid var(--line); white-space: nowrap; position: sticky; top: 0; background: var(--card); }
table.list td { padding: 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
table.list tr.r { cursor: pointer; }
table.list tr.r:hover td { background: var(--brand-soft); }
table.list tr.overdue td:first-child { box-shadow: inset 3px 0 0 var(--bad); }
.clip { max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.kv { display: grid; grid-template-columns: 140px 1fr; gap: 6px 12px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }
@media (max-width: 600px) { .kv { grid-template-columns: 110px 1fr; } }
.sec { border-top: 1px solid var(--line); padding-top: 14px; margin-top: 14px; }
.sec h4 { margin: 0 0 8px; font-size: 14px; }
.copy { border: 0; background: var(--bg); color: var(--ink); border-radius: 6px; font-size: 12px; padding: 2px 8px; cursor: pointer; margin-left: 6px; }
.bill-toggle { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.bill-toggle button { font: inherit; font-weight: 600; padding: 12px; border-radius: 12px; cursor: pointer; border: 2px solid var(--line); background: var(--card); color: var(--ink); }
.bill-toggle .g.on { background: var(--ok); border-color: var(--ok); color: #fff; }
.bill-toggle .r.on { background: var(--bad); border-color: var(--bad); color: #fff; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 14px; }
.tabs button { font: inherit; border: 0; background: none; padding: 10px 14px; cursor: pointer; color: var(--muted); border-bottom: 2px solid transparent; }
.tabs button.on { color: var(--brand-ink); border-bottom-color: var(--brand); font-weight: 600; }
.mini-list { display: grid; gap: 6px; }
.mini-list .it { display: flex; gap: 8px; align-items: center; }
.mini-list .it input { flex: 1; }
.login { max-width: 380px; margin: 10vh auto; }
