:root {
  --bg: #f5f6fa; --card: #ffffff; --fg: #14161d; --muted: #6b7280; --line: #e5e7eb;
  --brand: #4338ca; --brand-soft: #eef2ff; --ok: #059669; --warn: #b45309; --danger: #dc2626;
  --radius: 14px; --shadow: 0 1px 2px rgba(16,18,32,.06), 0 8px 24px rgba(16,18,32,.06);
  --safe-b: env(safe-area-inset-bottom, 0px);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1117; --card: #171a23; --fg: #e8eaf2; --muted: #9aa1b1; --line: #262a36;
    --brand: #818cf8; --brand-soft: #1e2140; --ok: #34d399; --warn: #fbbf24; --danger: #f87171;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.35);
  }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
/* กันหน้าจอเลื่อนออกด้านข้าง – ต่อให้มีข้อความยาวผิดปกติก็ต้องไม่ดันจนล้น */
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--fg); max-width: 100%; overflow-x: hidden; }
body {
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans Thai", "Sarabun", Roboto, sans-serif;
  overscroll-behavior-y: none;
  /* iOS ขยายตัวอักษรเองเมื่อผู้ใช้ตั้งขนาดตัวอักษรใหญ่ ทำให้กล่องบวมจนล้นจอ */
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}
/* ลูกของ flex/grid ต้องยุบได้ ไม่งั้นข้อความยาวจะดันกล่องจนล้น
   grid/flex item มี min-width:auto เป็นค่าเริ่มต้น = ยุบต่ำกว่าความกว้างเนื้อหาไม่ได้
   ถ้าตัวอักษรใหญ่ขึ้น (ผู้ใช้ตั้งขนาดใหญ่บน iOS) กล่องจะดันจนล้นจอ */
main > *, .card > *, .row > *, .spread > *, .mid, .grow, .exp > * { min-width: 0; }
.btn { min-width: 0; max-width: 100%; }
/* ปุ่มที่ข้อความยาวให้ตัดบรรทัดได้ ดีกว่าดันจนล้น */
.btn.block, .btn.big { white-space: normal; }
.btn > svg { flex: none; }
.card, main, #app > * { max-width: 100%; }
/* ข้อความยาวต่อเนื่อง (ชื่อร้านต่างประเทศ / URL) ให้ตัดบรรทัดได้ */
.name, .exp .mid, .warnbox, .notebox, .dupbox, .card { overflow-wrap: anywhere; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--brand); }

/* ---------- layout ---------- */
#app { max-width: 720px; margin: 0 auto; padding-bottom: calc(76px + var(--safe-b)); }
header.top {
  position: sticky; top: 0; z-index: 20; background: var(--card); border-bottom: 1px solid var(--line);
  padding: 10px 14px calc(10px); padding-top: calc(10px + env(safe-area-inset-top, 0px));
  display: flex; align-items: center; gap: 10px;
}
header.top .grow { flex: 1; min-width: 0; }
header.top .trip { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
header.top .sub { color: var(--muted); font-size: 12px; }
.avatar { width: 30px; height: 30px; border-radius: 50%; background: var(--brand-soft); object-fit: cover; }

main { padding: 14px; display: grid; gap: 14px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); }
.card h2 { margin: 0 0 10px; font-size: 15px; }
.row { display: flex; gap: 10px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.spread { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.right { text-align: right; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.hide { display: none !important; }

/* ---------- controls ---------- */
.btn {
  border: 1px solid var(--line); background: var(--card); border-radius: 12px;
  padding: 11px 14px; font-weight: 600; cursor: pointer; display: inline-flex;
  align-items: center; justify-content: center; gap: 8px;
}
a.btn { text-decoration: none; }
.btn:active { transform: scale(.985); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.block { width: 100%; }
.btn:disabled { opacity: .55; }
.btn.big { padding: 22px 16px; font-size: 17px; border-radius: 16px; }

label.field { display: block; }
label.field > span { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 4px; }
input, select, textarea {
  width: 100%; padding: 11px 12px; border-radius: 11px; border: 1px solid var(--line);
  background: var(--bg); outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--brand); }
input[inputmode="decimal"] { font-variant-numeric: tabular-nums; }

.chips { display: flex; flex-wrap: wrap; gap: 7px; }
.chip {
  border: 1px solid var(--line); background: var(--bg); border-radius: 999px;
  padding: 7px 12px; font-size: 13px; cursor: pointer;
}
.chip[aria-pressed="true"] { background: var(--brand); border-color: var(--brand); color: #fff; }

.badge { white-space: nowrap; font-size: 11.5px; padding: 3px 8px; border-radius: 999px; background: var(--brand-soft); color: var(--brand); font-weight: 600; }
.badge.warn { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }
.badge.ok { background: color-mix(in srgb, var(--ok) 18%, transparent); color: var(--ok); }
.badge.danger { background: color-mix(in srgb, var(--danger) 16%, transparent); color: var(--danger); }
.badge.grey { background: var(--bg); color: var(--muted); }

/* ---------- tabs ---------- */
nav.tabs {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
  background: var(--card); border-top: 1px solid var(--line);
  display: grid; grid-template-columns: repeat(4, 1fr);
  padding-bottom: var(--safe-b);
}
nav.tabs button {
  background: none; border: 0; padding: 9px 4px 10px; color: var(--muted);
  display: grid; gap: 2px; justify-items: center; font-size: 11.5px; cursor: pointer;
}
nav.tabs button[aria-selected="true"] { color: var(--brand); font-weight: 650; }
nav.tabs svg { width: 22px; height: 22px; }

/* ---------- expense list ---------- */
.exp { display: flex; gap: 11px; padding: 11px 0; border-bottom: 1px solid var(--line); align-items: center; }
.exp:last-child { border-bottom: 0; }
.exp .thumb { width: 46px; height: 46px; border-radius: 10px; object-fit: cover; background: var(--bg); flex: none; }
.exp .mid { flex: 1; min-width: 0; }
.exp .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.exp .amt { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }

/* ---------- bars ---------- */
.bar { height: 8px; border-radius: 999px; background: var(--line); overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--brand); border-radius: 999px; }
.bar.over > i { background: var(--danger); }

/* ---------- sheet / modal ---------- */
.sheet-bg { position: fixed; inset: 0; background: rgba(8,10,18,.5); z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  background: var(--card); width: 100%; max-width: 720px; max-height: 92vh; overflow-y: auto;
  border-radius: 18px 18px 0 0; padding: 0 0 calc(88px + var(--safe-b));
  animation: up .18s ease-out; position: relative;
}
/* หัว sheet ติดบนตลอด พร้อมปุ่มปิดมุมขวา */
.sheet-head {
  position: sticky; top: 0; z-index: 3; background: var(--card);
  border-bottom: 1px solid var(--line); padding: 14px 16px 12px;
  display: flex; align-items: flex-start; gap: 10px;
  border-radius: 18px 18px 0 0;
}
.sheet-head .grow { flex: 1; min-width: 0; }
.sheet-close {
  flex: none; width: 34px; height: 34px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--bg); font-size: 19px; line-height: 1; cursor: pointer; color: var(--muted);
  display: grid; place-items: center;
}
.sheet-close:active { transform: scale(.94); }
.sheet-body { padding: 14px 16px 0; }
/* ปุ่มบันทึกลอยติดล่าง ไม่ต้องเลื่อนหา */
.sheet-foot {
  position: sticky; bottom: 0; z-index: 3; background: var(--card);
  border-top: 1px solid var(--line); padding: 12px 16px calc(12px + var(--safe-b));
  display: grid; grid-template-columns: 1fr 2fr; gap: 10px;
  box-shadow: 0 -6px 18px rgba(16,18,32,.06);
}
@keyframes up { from { transform: translateY(18px); opacity: .6 } to { transform: none; opacity: 1 } }
.sheet h2 { margin: 2px 0 4px; font-size: 17px; }
.sheet .actions { display: grid; grid-template-columns: 1fr 2fr; gap: 10px; margin-top: 16px; }
.sheet h2 { margin: 0 0 2px; }
.preview { width: 100%; max-height: 30vh; object-fit: contain; border-radius: 12px; background: var(--bg); }

/* ---------- toast / status ---------- */
#toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(88px + var(--safe-b)); z-index: 90;
  background: #14161d; color: #fff; padding: 11px 16px; border-radius: 12px;
  box-shadow: var(--shadow); max-width: 90vw; text-align: center; font-size: 14px;
}
#toast.err { background: var(--danger); }
.status-strip { display: flex; gap: 8px; align-items: center; padding: 8px 14px; font-size: 12.5px; }
.status-strip.offline { background: color-mix(in srgb, var(--warn) 16%, transparent); color: var(--warn); }
.status-strip.sync { background: var(--brand-soft); color: var(--brand); }

.spinner { width: 18px; height: 18px; border: 2.5px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg) } }

.center-pad { display: grid; align-content: center; justify-items: center; text-align: center; gap: 14px; padding: 42px 20px; }
.warnbox { background: color-mix(in srgb, var(--warn) 14%, transparent); color: var(--warn); border-radius: 10px; padding: 9px 11px; font-size: 13px; }
table.tbl { width: 100%; border-collapse: collapse; font-size: 13.5px; }
table.tbl th, table.tbl td { text-align: left; padding: 7px 6px; border-bottom: 1px solid var(--line); }

/* ---------- admin dashboard ---------- */
body.dash-mode #app { max-width: 1180px; padding-bottom: 40px; }
.dash-head {
  position: sticky; top: 0; z-index: 20; background: var(--card); border-bottom: 1px solid var(--line);
  padding: 10px 14px; padding-top: calc(10px + env(safe-area-inset-top, 0px));
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
}
.dash-head h1 { font-size: 17px; margin: 0; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-end; }
.filters label.field { min-width: 130px; flex: 1 1 130px; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.kpi { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 13px; box-shadow: var(--shadow); }
.kpi .k { font-size: 12px; color: var(--muted); }
.kpi .v { font-size: 21px; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: 3px; line-height: 1.2; }
.kpi .v.sm { font-size: 17px; }
.kpi.alert { border-color: var(--warn); }
.kpi.alert .v { color: var(--warn); }
.dash-grid { display: grid; grid-template-columns: 1fr; gap: 14px; }
@media (min-width: 860px) { .dash-grid.two { grid-template-columns: 1fr 1fr; } }
.legend-dot { width: 10px; height: 10px; border-radius: 3px; display: inline-block; flex: none; }
.catrow { display: grid; grid-template-columns: auto 1fr auto; gap: 9px; align-items: center; padding: 7px 0; }
.catrow .lbl { min-width: 0; }
.catrow .lbl b { display: block; font-size: 13.5px; }
.catrow .lbl span { color: var(--muted); font-size: 11.5px; }
table.tbl.wide td, table.tbl.wide th { white-space: nowrap; }
table.tbl th { color: var(--muted); font-weight: 600; font-size: 12px; }
table.tbl td.num, table.tbl th.num { text-align: right; font-variant-numeric: tabular-nums; }
.scrollx { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.chartwrap { width: 100%; overflow-x: auto; }

.status-strip.imp { background: var(--warn); color: #fff; font-weight: 600; }
.status-strip.imp .btn { background: rgba(255,255,255,.95); color: #7a4a00; border-color: transparent; }

/* แถบงบค่าอาหารพร้อมเส้น 75% */
.fbar { position: relative; height: 10px; border-radius: 999px; background: var(--line); overflow: hidden; margin-top: 5px; }
.fbar > i { display: block; height: 100%; border-radius: 999px; transition: width .3s; }
.fbar .fmark { position: absolute; top: 0; bottom: 0; left: 75%; width: 2px; background: var(--fg); opacity: .35; }

/* กล่องเตือนสลิปซ้ำ – ต้องสะดุดตากว่า warnbox ปกติ */
.dupbox {
  background: color-mix(in srgb, var(--danger) 12%, transparent);
  border: 1.5px solid var(--danger); color: var(--danger);
  border-radius: 12px; padding: 11px 13px; margin-top: 8px; font-size: 13.5px; line-height: 1.55;
}
.dupbox .dupitem { margin-top: 6px; font-weight: 500; }
.dupbox .dupitem .muted { display: block; font-size: 11.5px; opacity: .85; font-weight: 400; }
.dupbox .dupnote { margin-top: 8px; font-size: 12px; font-weight: 400; opacity: .9; }

/* โน้ตข้อมูล ไม่ใช่คำเตือน – ใบเสร็จจากประเทศทรานซิต */
.notebox { background: var(--bg); color: var(--muted); border: 1px solid var(--line);
  border-radius: 10px; padding: 8px 11px; font-size: 12.5px; margin-top: 6px; }
