:root {
  --bg: #f4f5f9;
  --card: #ffffff;
  --text: #15171c;
  --muted: #6b7180;
  --line: #e4e6ec;
  --accent: #2f6bff;
  --accent-soft: #e6eeff;
  --ok: #1a9e5c;
  --ok-soft: #e2f6eb;
  --warn: #d98a00;
  --warn-soft: #fff3dc;
  --bad: #e0352b;
  --bad-soft: #fde6e4;
  --shadow: 0 1px 2px rgba(20, 24, 40, .06), 0 4px 16px rgba(20, 24, 40, .06);
  --radius: 16px;
  --tabbar-h: 58px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card: #1c1c1f;
    --text: #f2f3f5;
    --muted: #9a9ea8;
    --line: #2c2c31;
    --accent: #5b8cff;
    --accent-soft: #1b2643;
    --ok: #34c77b;
    --ok-soft: #12301f;
    --warn: #f0a92a;
    --warn-soft: #36290c;
    --bad: #ff5a4f;
    --bad-soft: #3a1513;
    --shadow: none;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body { margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Sukhumvit Set", "Thonburi", "Noto Sans Thai", "Leelawadee UI", system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  min-height: 100vh;
  min-height: 100dvh;
  padding-top: calc(var(--safe-top) + 56px);
  padding-bottom: calc(var(--safe-bottom) + var(--tabbar-h) + 24px);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: none;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }

/* Top bar */
.topbar {
  position: fixed; inset: 0 0 auto 0; z-index: 10;
  padding: var(--safe-top) 8px 0;
  height: calc(var(--safe-top) + 56px);
  display: flex; align-items: center; justify-content: space-between;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(18px);
  backdrop-filter: saturate(180%) blur(18px);
  border-bottom: 0.5px solid var(--line);
}
.month-title {
  border: 0; background: none; font-size: 18px; font-weight: 700; padding: 8px 12px;
}
.icon-btn {
  width: 44px; height: 44px; border: 0; border-radius: 22px; background: none;
  font-size: 30px; line-height: 1; color: var(--accent);
}
.icon-btn:active { background: var(--accent-soft); }
.topbar.hide-nav .icon-btn { visibility: hidden; }

main { padding: 12px 16px; max-width: 640px; margin: 0 auto; }

/* Cards */
.card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px; margin-bottom: 14px;
}
.section-title {
  font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase;
  letter-spacing: .02em; margin: 18px 4px 8px;
}

.summary .big { font-size: 30px; font-weight: 800; letter-spacing: -.01em; }
.summary .label { color: var(--muted); font-size: 14px; }
.summary .row { display: flex; gap: 10px; margin-top: 14px; }
.summary .stat { flex: 1; border-radius: 12px; padding: 10px 12px; }
.summary .stat b { display: block; font-size: 17px; }
.summary .stat span { font-size: 12.5px; color: var(--muted); }
.stat.paid { background: var(--ok-soft); }
.stat.paid b { color: var(--ok); }
.stat.left { background: var(--warn-soft); }
.stat.left b { color: var(--warn); }
.progress { height: 8px; background: var(--line); border-radius: 4px; overflow: hidden; margin-top: 14px; }
.progress > div { height: 100%; background: var(--ok); border-radius: 4px; transition: width .3s; }

.halves { display: flex; gap: 10px; }
.halves .card { flex: 1; margin-bottom: 0; padding: 12px 14px; }
.halves .label { font-size: 12.5px; color: var(--muted); }
.halves b { font-size: 17px; }

.alert {
  border-radius: 12px; padding: 12px 14px; margin-bottom: 14px; font-size: 14.5px;
  display: flex; gap: 10px; align-items: flex-start;
}
.alert.bad { background: var(--bad-soft); color: var(--bad); }
.alert.warn { background: var(--warn-soft); color: var(--warn); }

/* Bill list */
.list { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.item {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px; width: 100%;
  border: 0; background: none; text-align: left; border-bottom: 0.5px solid var(--line);
}
.item:last-child { border-bottom: 0; }
.item:active { background: var(--accent-soft); }
.item .ico {
  width: 40px; height: 40px; border-radius: 12px; flex: 0 0 40px;
  display: grid; place-items: center; font-size: 21px;
}
.item .main { flex: 1; min-width: 0; }
.item .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .sub { font-size: 13px; color: var(--muted); }
.item .right { text-align: right; flex: 0 0 auto; }
.item .amt { font-weight: 700; font-variant-numeric: tabular-nums; }
.item.paid .name, .item.paid .amt { color: var(--muted); }
.item.paid .amt { text-decoration: line-through; }
.check {
  width: 30px; height: 30px; border-radius: 15px; border: 2px solid var(--line);
  flex: 0 0 30px; display: grid; place-items: center; font-size: 16px; color: #fff; background: none;
}
.check.on { background: var(--ok); border-color: var(--ok); }

.badge { display: inline-block; font-size: 11.5px; font-weight: 600; padding: 1px 7px; border-radius: 8px; margin-top: 2px; }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.muted { background: var(--line); color: var(--muted); }

.empty { text-align: center; color: var(--muted); padding: 40px 20px; }
.empty .e-ico { font-size: 44px; margin-bottom: 8px; }

/* Calendar */
.cal { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 10px; }
.cal-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 2px; }
.cal-dow { text-align: center; font-size: 12px; color: var(--muted); padding: 4px 0 6px; }
.cal-day {
  aspect-ratio: 1 / 1.05; border: 0; background: none; border-radius: 10px;
  display: flex; flex-direction: column; align-items: center; justify-content: flex-start;
  padding-top: 6px; font-size: 15px; font-variant-numeric: tabular-nums;
}
.cal-day.blank { visibility: hidden; }
.cal-day.today .n { background: var(--accent); color: #fff; }
.cal-day.sel { background: var(--accent-soft); }
.cal-day .n { width: 28px; height: 28px; border-radius: 14px; display: grid; place-items: center; }
.dots { display: flex; gap: 3px; margin-top: 3px; height: 6px; }
.dot { width: 6px; height: 6px; border-radius: 3px; background: var(--accent); }
.dot.paid { background: var(--ok); }
.dot.over { background: var(--bad); }
.dot.soon { background: var(--warn); }
.legend { display: flex; gap: 14px; justify-content: center; font-size: 12px; color: var(--muted); margin-top: 8px; flex-wrap: wrap; }
.legend span { display: inline-flex; align-items: center; gap: 5px; }

/* Tab bar */
.tabbar {
  position: fixed; inset: auto 0 0 0; z-index: 10;
  display: flex; padding-bottom: var(--safe-bottom);
  background: color-mix(in srgb, var(--card) 85%, transparent);
  -webkit-backdrop-filter: saturate(180%) blur(18px);
  backdrop-filter: saturate(180%) blur(18px);
  border-top: 0.5px solid var(--line);
}
.tabbar button {
  flex: 1; height: var(--tabbar-h); border: 0; background: none; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; font-size: 11px;
}
.tabbar .ti { font-size: 21px; filter: grayscale(1); opacity: .7; }
.tabbar button.active { color: var(--accent); font-weight: 600; }
.tabbar button.active .ti { filter: none; opacity: 1; }

.fab {
  position: fixed; z-index: 11; right: 18px; bottom: calc(var(--safe-bottom) + var(--tabbar-h) + 16px);
  width: 58px; height: 58px; border-radius: 29px; border: 0;
  background: var(--accent); color: #fff; font-size: 30px; line-height: 1;
  box-shadow: 0 6px 20px rgba(47, 107, 255, .4);
}
.fab:active { transform: scale(.94); }
.fab[hidden] { display: none; }

/* Sheet */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, .35);
  animation: fade .2s ease;
}
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21;
  max-height: calc(100dvh - var(--safe-top) - 24px); overflow-y: auto; -webkit-overflow-scrolling: touch;
  background: var(--bg); border-radius: 20px 20px 0 0;
  padding: 8px 16px calc(var(--safe-bottom) + 20px);
  animation: up .25s cubic-bezier(.2, .9, .3, 1);
  max-width: 640px; margin: 0 auto;
}
.sheet-backdrop[hidden], .sheet[hidden], .toast[hidden] { display: none; }
.grabber { width: 38px; height: 5px; border-radius: 3px; background: var(--line); margin: 0 auto 10px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.sheet-head h2 { font-size: 18px; margin: 0; }
.link-btn { border: 0; background: none; color: var(--accent); font-size: 16px; padding: 8px 4px; }
.link-btn.strong { font-weight: 700; }

@keyframes up { from { transform: translateY(100%); } to { transform: none; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

/* Forms */
.form { background: var(--card); border-radius: 14px; overflow: hidden; margin-bottom: 14px; }
.field { display: flex; align-items: center; gap: 10px; padding: 0 14px; min-height: 50px; border-bottom: 0.5px solid var(--line); }
.field:last-child { border-bottom: 0; }
.field > label { flex: 0 0 auto; min-width: 96px; }
.field input, .field select, .field textarea {
  flex: 1; min-width: 0; border: 0; background: none; padding: 12px 0; text-align: right;
  font-size: 16px; /* >=16px prevents iOS zoom */ outline: none;
}
.field select { direction: rtl; -webkit-appearance: none; appearance: none; color: var(--accent); }
.field textarea { text-align: left; resize: none; min-height: 70px; }
.field.col { flex-direction: column; align-items: stretch; padding: 10px 14px; }
.field.col > label { min-width: 0; font-size: 13px; color: var(--muted); }
.hint { font-size: 12.5px; color: var(--muted); margin: -6px 6px 14px; }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.chip {
  border: 1.5px solid var(--line); background: var(--card); border-radius: 18px;
  padding: 6px 12px; font-size: 14.5px;
}
.chip.on { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); font-weight: 600; }

.seg { display: flex; background: var(--line); border-radius: 10px; padding: 2px; margin-bottom: 14px; }
.seg button { flex: 1; border: 0; background: none; border-radius: 8px; padding: 7px 4px; font-size: 14.5px; }
.seg button.on { background: var(--card); font-weight: 600; box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }

.switch { position: relative; width: 51px; height: 31px; flex: 0 0 51px; }
.switch input { opacity: 0; width: 0; height: 0; position: absolute; }
.switch span { position: absolute; inset: 0; border-radius: 16px; background: var(--line); transition: .2s; }
.switch span::after {
  content: ""; position: absolute; left: 2px; top: 2px; width: 27px; height: 27px; border-radius: 14px;
  background: #fff; box-shadow: 0 2px 4px rgba(0, 0, 0, .2); transition: .2s;
}
.switch input:checked + span { background: var(--ok); }
.switch input:checked + span::after { transform: translateX(20px); }

.btn {
  display: block; width: 100%; border: 0; border-radius: 14px; padding: 14px; font-size: 17px; font-weight: 600;
  background: var(--accent); color: #fff; margin-bottom: 10px;
}
.btn:active { opacity: .85; }
.btn.ok { background: var(--ok); }
.btn.ghost { background: var(--card); color: var(--accent); }
.btn.danger { background: var(--card); color: var(--bad); }
.btn.small { padding: 10px; font-size: 15px; }

.detail-head { text-align: center; margin: 4px 0 16px; }
.detail-head .ico { width: 64px; height: 64px; border-radius: 18px; display: grid; place-items: center; font-size: 34px; margin: 0 auto 8px; }
.detail-head h3 { margin: 0; font-size: 21px; }
.detail-head .sub { color: var(--muted); font-size: 14px; }

.kv { display: flex; justify-content: space-between; padding: 12px 14px; border-bottom: 0.5px solid var(--line); }
.kv:last-child { border-bottom: 0; }
.kv span:first-child { color: var(--muted); }

.history { font-size: 14px; }
.history .kv { padding: 9px 14px; }

.toast {
  position: fixed; z-index: 30; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--safe-bottom) + var(--tabbar-h) + 90px);
  background: rgba(30, 30, 34, .92); color: #fff; padding: 10px 18px; border-radius: 20px; font-size: 14.5px;
  max-width: calc(100% - 32px); text-align: center; animation: fade .2s;
}

.install-tip { font-size: 14px; line-height: 1.6; }
.install-tip ol { margin: 6px 0 0; padding-left: 20px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
