:root {
  --bg: #f4f5f2;
  --surface: #ffffff;
  --surface-2: #f0f1ed;
  --text: #1c1f1d;
  --text-2: #5d635f;
  --text-3: #8b918d;
  --border: rgba(20, 30, 25, 0.12);
  --border-strong: rgba(20, 30, 25, 0.24);
  --accent: #0f766e;
  --accent-bg: #e0f2ef;
  --accent-text: #0b5953;
  --on-accent: #ffffff;
  --pos: #217a3c;
  --pos-bg: #e5f3e8;
  --neg: #b4352f;
  --neg-bg: #fbe9e7;
  --warn: #8a5a00;
  --warn-bg: #fdf1d8;
  --radius: 10px;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.12);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans Thai", "Sarabun", "Thonburi", sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111413;
    --surface: #1a1e1c;
    --surface-2: #232825;
    --text: #e9ece9;
    --text-2: #a7aea9;
    --text-3: #7c847f;
    --border: rgba(255, 255, 255, 0.1);
    --border-strong: rgba(255, 255, 255, 0.22);
    --accent: #2bb3a5;
    --accent-bg: #133b37;
    --accent-text: #8ee0d6;
    --on-accent: #06201d;
    --pos: #6fd08a;
    --pos-bg: #173322;
    --neg: #ff8f86;
    --neg-bg: #3a1d1b;
    --warn: #f3c46b;
    --warn-bg: #3a2e13;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
.hidden { display: none !important; }
.boot { padding: 40vh 16px 0; text-align: center; color: var(--text-3); }

/* --- Layout ------------------------------------------------------------ */
.page { max-width: 640px; margin: 0 auto; padding: 0 16px calc(96px + env(safe-area-inset-bottom)); }
.appbar {
  position: sticky; top: 0; z-index: 5;
  background: var(--bg);
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  border-bottom: 1px solid var(--border);
}
.appbar-inner { max-width: 640px; margin: 0 auto; display: flex; align-items: center; gap: 10px; min-height: 40px; }
.appbar h1 { font-size: 18px; font-weight: 600; margin: 0; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.appbar .sub { font-size: 12px; color: var(--text-2); font-weight: 400; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 50%;
  border: none; background: transparent; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  color: var(--text-2); font-size: 18px; flex-shrink: 0;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn svg { width: 22px; height: 22px; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 4px 4px calc(4px + env(safe-area-inset-bottom));
}
.tabbar-inner { max-width: 640px; margin: 0 auto; display: grid; grid-template-columns: repeat(5, 1fr); }
.tab {
  border: none; background: none; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 6px 2px; font-size: 11px; color: var(--text-3); position: relative;
}
.tab svg { width: 22px; height: 22px; }
.tab.active { color: var(--accent); }
.tab .dot {
  position: absolute; top: 2px; left: 50%; margin-left: 6px;
  min-width: 16px; height: 16px; padding: 0 4px; border-radius: 8px;
  background: var(--neg); color: #fff; font-size: 10px; line-height: 16px; text-align: center;
}

h2 { font-size: 15px; font-weight: 600; margin: 22px 0 8px; }
h2 .muted { font-weight: 400; }
.muted { color: var(--text-2); }
.small { font-size: 13px; }
.tiny { font-size: 12px; }
.row { display: flex; align-items: center; gap: 8px; }
.row.between { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.ellipsis { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.num { font-variant-numeric: tabular-nums; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }

/* --- Cards / lists ------------------------------------------------------ */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px 16px;
}
.card + .card { margin-top: 10px; }
.metric { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 14px 16px; }
.metric .label { font-size: 12px; color: var(--text-2); }
.metric .value { font-size: 26px; font-weight: 600; line-height: 1.25; }
.metrics { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 14px; }
.metrics .metric.wide { grid-column: 1 / -1; }

.list { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; overflow: hidden; }
.list-item {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px; border-bottom: 1px solid var(--border);
  background: none; border-left: none; border-right: none; border-top: none;
  width: 100%; text-align: left; cursor: default;
}
button.list-item { cursor: pointer; }
button.list-item:hover { background: var(--surface-2); }
.list-item:last-child { border-bottom: none; }
.list-item .title { font-weight: 500; }
.list-item .meta { font-size: 12px; color: var(--text-2); }
.list-item .amt { text-align: right; white-space: nowrap; }
.list-item .amt .sub { font-size: 12px; color: var(--text-2); }
.date-head { font-size: 12px; color: var(--text-2); margin: 16px 4px 6px; }

.av {
  width: 32px; height: 32px; border-radius: 50%; flex-shrink: 0;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 600; overflow: hidden;
}
.av img { width: 100%; height: 100%; object-fit: cover; }
.av.sm { width: 24px; height: 24px; font-size: 11px; }
.c0 { background: #e4e1fb; color: #3b3486; }
.c1 { background: #d7f2e8; color: #0a5141; }
.c2 { background: #fbe3da; color: #74290f; }
.c3 { background: #fbe1ea; color: #73223d; }
.c4 { background: #dbe9fa; color: #0c447c; }
.c5 { background: #f8ebd2; color: #633806; }
.c6 { background: #e5efd6; color: #27500a; }
.c7 { background: #ebe9e2; color: #444441; }
.cat { width: 36px; height: 36px; border-radius: 10px; background: var(--surface-2); display: inline-flex; align-items: center; justify-content: center; font-size: 18px; flex-shrink: 0; }

.bar { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--accent); border-radius: 4px; }

.banner {
  border-radius: var(--radius); padding: 10px 12px; font-size: 13px;
  display: flex; gap: 8px; align-items: flex-start;
}
.banner.info { background: var(--accent-bg); color: var(--accent-text); }
.banner.warn { background: var(--warn-bg); color: var(--warn); }
.banner.err { background: var(--neg-bg); color: var(--neg); }
.banner.ok { background: var(--pos-bg); color: var(--pos); }
button.banner { border: none; width: 100%; text-align: left; cursor: pointer; }

.pill {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 11px; padding: 2px 8px; border-radius: 999px; white-space: nowrap;
  background: var(--surface-2); color: var(--text-2);
}
.pill.accent { background: var(--accent-bg); color: var(--accent-text); }
.pill.warn { background: var(--warn-bg); color: var(--warn); }
.pill.err { background: var(--neg-bg); color: var(--neg); }
.pill.ok { background: var(--pos-bg); color: var(--pos); }

/* --- Controls ----------------------------------------------------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 42px; padding: 0 16px; border-radius: var(--radius);
  border: 1px solid var(--border-strong); background: var(--surface);
  cursor: pointer; font-weight: 500; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--neg); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.sm { height: 32px; padding: 0 10px; font-size: 13px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.55; cursor: progress; }
.btn svg { width: 18px; height: 18px; }
.btn-row { display: flex; gap: 8px; }
.btn-row > .btn { flex: 1; }

label.field { display: block; }
label.field > span, .field-label { display: block; font-size: 12px; color: var(--text-2); margin-bottom: 4px; }
input[type=text], input[type=number], input[type=email], input[type=date], input[type=datetime-local], select, textarea {
  width: 100%; height: 42px; padding: 0 12px;
  border: 1px solid var(--border-strong); border-radius: var(--radius);
  background: var(--surface); outline: none;
}
textarea { height: auto; min-height: 64px; padding: 10px 12px; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-bg); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid-cur { display: grid; grid-template-columns: 110px 1fr; gap: 10px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  height: 34px; padding: 0 12px 0 4px; border-radius: 999px;
  border: 1px solid var(--border-strong); background: var(--surface);
  cursor: pointer; font-size: 14px;
}
.chip.plain { padding: 0 12px; }
.chip.on { background: var(--accent-bg); border-color: var(--accent); color: var(--accent-text); }
.chip.off { color: var(--text-3); text-decoration: line-through; }
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; background: var(--surface-2); border-radius: var(--radius); padding: 3px; }
.seg button { border: none; background: none; height: 34px; border-radius: 8px; cursor: pointer; font-size: 14px; color: var(--text-2); }
.seg button.on { background: var(--surface); color: var(--text); font-weight: 500; box-shadow: 0 1px 3px rgba(0,0,0,0.12); }

.share-row { display: flex; align-items: center; gap: 8px; padding: 6px 0; }
.share-row input { width: 110px; height: 36px; text-align: right; }

.fab {
  position: fixed; right: 16px; bottom: calc(76px + env(safe-area-inset-bottom)); z-index: 7;
  height: 52px; padding: 0 20px; border-radius: 26px; border: none;
  background: var(--accent); color: var(--on-accent); font-weight: 600;
  display: inline-flex; align-items: center; gap: 8px; box-shadow: var(--shadow); cursor: pointer;
}
.fab svg { width: 20px; height: 20px; }

/* --- Slips -------------------------------------------------------------- */
.slip-thumb {
  width: 48px; height: 64px; border-radius: 6px; object-fit: cover; flex-shrink: 0;
  background: var(--surface-2); border: 1px solid var(--border);
}
.slip-preview { width: 100%; max-height: 260px; object-fit: contain; border-radius: var(--radius); background: var(--surface-2); cursor: zoom-in; }
.dropzone {
  border: 1.5px dashed var(--border-strong); border-radius: 14px; padding: 18px; text-align: center;
  background: var(--surface); cursor: pointer;
}
.dropzone.drag { border-color: var(--accent); background: var(--accent-bg); }
.progress { height: 4px; background: var(--surface-2); border-radius: 2px; overflow: hidden; margin-top: 8px; }
.progress > span { display: block; height: 100%; background: var(--accent); transition: width 0.2s; }

/* --- Modal -------------------------------------------------------------- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, 0.45);
  display: flex; align-items: flex-end; justify-content: center;
}
.modal {
  background: var(--bg); width: 100%; max-width: 560px; max-height: 92vh; overflow-y: auto;
  border-radius: 18px 18px 0 0; padding: 0 16px calc(16px + env(safe-area-inset-bottom));
  animation: up 0.18s ease-out;
}
@media (min-width: 640px) {
  .modal-backdrop { align-items: center; }
  .modal { border-radius: 18px; }
}
@keyframes up { from { transform: translateY(24px); opacity: 0.6; } to { transform: none; opacity: 1; } }
.modal-head {
  position: sticky; top: 0; background: var(--bg); z-index: 1;
  display: flex; align-items: center; gap: 8px; padding: 14px 0 10px;
}
.modal-head h3 { margin: 0; font-size: 17px; flex: 1; }
.modal-foot { position: sticky; bottom: calc(-16px - env(safe-area-inset-bottom)); background: var(--bg); padding: 12px 0 0; }
.lightbox { position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,0.9); display: flex; align-items: center; justify-content: center; }
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; }

.toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); z-index: 40;
  transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px;
  font-size: 14px; max-width: calc(100% - 32px); transition: all 0.2s;
}
.toast.show { opacity: 1; transform: translateX(-50%); }
.toast.err { background: var(--neg); color: #fff; }

/* --- Login -------------------------------------------------------------- */
.login { max-width: 380px; margin: 0 auto; padding: 18vh 24px 40px; text-align: center; }
.login .logo { width: 72px; height: 72px; margin: 0 auto 12px; border-radius: 20px; background: var(--accent); color: var(--on-accent); display: flex; align-items: center; justify-content: center; }
.login .logo svg { width: 40px; height: 40px; }
.login h1 { font-size: 24px; margin: 0 0 4px; }
.login p { color: var(--text-2); margin: 0 0 28px; }
.gbtn { display: flex; justify-content: center; min-height: 44px; }
.empty { text-align: center; padding: 32px 16px; color: var(--text-2); }
.empty .big { font-size: 40px; margin-bottom: 4px; }
code.inline { background: var(--surface-2); padding: 1px 6px; border-radius: 6px; font-size: 13px; word-break: break-all; }
