:root {
  --bg: #f3f6f1;
  --surface: #ffffff;
  --surface-2: #f6f8f4;
  --text: #17231a;
  --muted: #64746a;
  --border: #e0e7dd;
  --primary: #166534;
  --primary-ink: #ffffff;
  --primary-soft: #dcfce7;
  --accent: #d9f750;
  --accent-ink: #2b3a00;
  --danger: #b42318;
  --danger-soft: #fee4e2;
  --warn-soft: #fef3c7;
  --warn-ink: #92400e;
  --pos: #15803d;
  --shadow: 0 1px 2px rgba(16, 40, 20, .06), 0 4px 16px rgba(16, 40, 20, .05);
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0e1410;
    --surface: #161e18;
    --surface-2: #1c261f;
    --text: #e6efe8;
    --muted: #93a598;
    --border: #2a372d;
    --primary: #4ade80;
    --primary-ink: #052e13;
    --primary-soft: #183a24;
    --accent: #d9f750;
    --accent-ink: #2b3a00;
    --danger: #f97066;
    --danger-soft: #3a1714;
    --warn-soft: #3a2a0c;
    --warn-ink: #fcd34d;
    --pos: #4ade80;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0e1410;
  --surface: #161e18;
  --surface-2: #1c261f;
  --text: #e6efe8;
  --muted: #93a598;
  --border: #2a372d;
  --primary: #4ade80;
  --primary-ink: #052e13;
  --primary-soft: #183a24;
  --danger: #f97066;
  --danger-soft: #3a1714;
  --warn-soft: #3a2a0c;
  --warn-ink: #fcd34d;
  --pos: #4ade80;
  --shadow: none;
}
:root { --bar: #14532d; --bar-ink: #ffffff; }

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  padding-bottom: env(safe-area-inset-bottom);
}
a { color: inherit; text-decoration: none; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 22px; }
h2 { font-size: 16px; display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
h3 { font-size: 14px; margin: 18px 0 8px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.muted { color: var(--muted); }
small { font-size: 12.5px; }

/* ---------- Top bar ---------- */
body::before { content: ""; position: fixed; inset: 0 0 auto; height: env(safe-area-inset-top, 0px); z-index: 11; background: var(--bar); }
.topbar {
  position: sticky; top: env(safe-area-inset-top, 0px); z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding-block: 10px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  background: var(--bar); color: var(--bar-ink);
}
.topbar { justify-content: flex-start; min-height: 56px; }
.topbar button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
.brand { font-weight: 800; font-size: 19px; display: flex; align-items: center; gap: 8px; letter-spacing: -.01em; }
.logo {
  width: 22px; height: 22px; border-radius: 50%; background: var(--accent);
  background-image: radial-gradient(circle, var(--bar) 1.6px, transparent 2px);
  background-size: 7px 7px; background-position: 1px 1px;
}
.back { width: 40px; height: 40px; margin-left: -8px; border-radius: 12px; display: grid; place-items: center; flex: none; }
.back svg { width: 24px; height: 24px; }
.back:active { background: rgba(255, 255, 255, .15); }
.tb-title { min-width: 0; line-height: 1.2; }
.tb-title b { display: block; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tb-title small { display: block; opacity: .75; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sync { margin-left: auto; flex: none; display: flex; align-items: center; gap: 6px; font-size: 12px; opacity: .85; }
.sync i { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); }
.sync.pending i { background: #fbbf24; }
.sync.error i { background: #f87171; }
.sync.error { opacity: 1; font-weight: 600; }
.sync.local { display: none; }
@media (max-width: 520px) { .sync.ok span, .sync.pending span { display: none; } }
button:focus-visible, .chip:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
button { -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
button:disabled { opacity: .5; cursor: default; }

/* ---------- Bottom tab bar ---------- */
.tabbar, .sumbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
  padding-top: 6px;
  padding-bottom: calc(6px + env(safe-area-inset-bottom, 0px));
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.tabbar-in { max-width: 480px; margin: 0 auto; display: flex; align-items: center; }
.tabbar button {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  font: inherit; font-size: 11.5px; font-weight: 500; color: var(--muted);
  background: none; border: 0; padding: 4px 0; min-height: 50px; cursor: pointer;
}
.tabbar button svg { width: 24px; height: 24px; }
.tabbar button.active { color: var(--primary); font-weight: 700; }
.tabbar .tab-add {
  flex: none; width: 58px; height: 58px; min-height: 0; margin: -26px 18px 0; padding: 0;
  border-radius: 50%; justify-content: center;
  background: var(--primary); color: var(--primary-ink);
  box-shadow: 0 6px 18px color-mix(in srgb, var(--primary) 45%, transparent);
  border: 4px solid var(--bg);
}
.tabbar .tab-add svg { width: 26px; height: 26px; }
.tabbar .tab-add:active { transform: scale(.94); }
body.in-session .tabbar { display: none; }

/* ---------- Sticky summary in a session ---------- */
.sumbar { padding-top: 10px; padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px)); }
.sumbar-in { max-width: 728px; margin: 0 auto; display: flex; align-items: center; gap: 12px; }
.sum-info { flex: 1; min-width: 0; text-align: left; font: inherit; color: var(--text); background: none; border: 0; padding: 0; cursor: pointer; line-height: 1.2; }
.sum-info small { display: block; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sum-info b { font-size: 20px; font-variant-numeric: tabular-nums; }
.sumbar .btn { min-height: 46px; padding: 0 18px; border-radius: 12px; }

main {
  max-width: 760px; margin: 0 auto;
  padding-top: 16px;
  padding-bottom: calc(100px + env(safe-area-inset-bottom, 0px));
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}

/* ---------- Modal ---------- */
#modal { position: fixed; inset: 0; z-index: 40; background: rgba(0, 0, 0, .45); display: grid; place-items: center; padding: 16px; }
#modal[hidden] { display: none; }
.modal-box { background: var(--surface); color: var(--text); border-radius: var(--radius); padding: 20px; width: 100%; max-width: 380px; box-shadow: 0 10px 40px rgba(0, 0, 0, .25); }
.modal-box p { margin: 0 0 4px; font-size: 16px; }
.btn.danger-fill { background: var(--danger); border-color: var(--danger); color: #fff; }

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 14px;
}
.badge {
  background: var(--primary-soft); color: var(--primary);
  font-size: 12px; font-weight: 700; padding: 1px 8px; border-radius: 999px;
}

/* ---------- Form ---------- */
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 10px; }
label > input, label > select { margin-top: 4px; }
input, select, textarea {
  width: 100%;
  font: inherit; color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 11px;
  outline: none;
  min-height: 44px;
  font-size: 16px; /* >=16px để iPhone không tự phóng to khi bấm vào ô nhập */
}
input:focus, select:focus { border-color: var(--primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 20%, transparent); }
input.invalid { border-color: var(--danger); background: var(--danger-soft); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.inline { display: flex; gap: 8px; margin-top: 10px; }
.inline input { flex: 1; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; cursor: pointer;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); color: var(--text);
  padding: 8px 14px; min-height: 44px; white-space: nowrap;
}
.btn:active { transform: scale(.98); }
.btn.block { margin-bottom: 8px; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.accent { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; }
.icon-btn {
  flex: none; width: 44px; height: 44px; border-radius: 10px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--muted); font-size: 15px;
}
.icon-btn:hover { color: var(--danger); border-color: var(--danger); }
.toolbar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 16px; }
.actions .btn { flex: 1; }

/* ---------- Chips ---------- */
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  font: inherit; font-size: 14px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  padding: 7px 14px; border-radius: 999px; min-height: 40px;
}
.chip:active { transform: scale(.96); }
.chip.on { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); font-weight: 600; }
.chip.on::before { content: "✓ "; }
.chip.add { border-style: dashed; color: var(--muted); background: transparent; }
.chip.add:hover { color: var(--primary); border-color: var(--primary); }
.chips.small .chip { font-size: 13.5px; padding: 5px 12px; min-height: 36px; }
.hint { font-size: 13px; color: var(--muted); margin: 8px 0 0; }
.hint.warn { background: var(--warn-soft); color: var(--warn-ink); padding: 8px 10px; border-radius: 10px; }

details { margin-top: 10px; }
summary { cursor: pointer; font-size: 13px; color: var(--muted); user-select: none; }
summary:hover { color: var(--primary); }
details[open] > summary { margin-bottom: 8px; }

.weights { display: grid; gap: 6px; }
.weights .row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.weights select { width: auto; min-width: 170px; }

/* ---------- Expenses ---------- */
.exp-list { display: grid; gap: 10px; margin-top: 12px; }
.exp { border: 1px solid var(--border); border-radius: 12px; padding: 10px; background: var(--surface-2); }
.exp input, .exp select { background: var(--surface); }
.exp-main { display: flex; gap: 8px; }
.exp-name { flex: 1.3; min-width: 0; }
.exp-amt { flex: 1; min-width: 0; text-align: right; font-weight: 700; font-variant-numeric: tabular-nums; }
.exp-sub { display: flex; gap: 10px; align-items: flex-start; flex-wrap: wrap; margin-top: 8px; }
.exp-sub label { margin: 0; flex: 1; min-width: 180px; }
.exp-sub details { flex-basis: 100%; margin-top: 2px; }

/* ---------- Result ---------- */
.result { border: 2px solid var(--primary); }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.stats div { background: var(--surface-2); border-radius: 12px; padding: 10px; }
.stats span { display: block; font-size: 12px; color: var(--muted); }
.stats b { font-size: 17px; font-variant-numeric: tabular-nums; }
.stats .hl { background: var(--accent); color: var(--accent-ink); }
.stats .hl span { color: inherit; opacity: .75; }

.row-between { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.row-between select { width: auto; }
.progress { height: 6px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin: 6px 0 4px; }
.progress > i { display: block; height: 100%; background: var(--primary); border-radius: 99px; transition: width .25s; }

.people { display: grid; gap: 2px; margin-top: 8px; }
.person { display: flex; align-items: center; gap: 12px; padding: 10px 4px; border-bottom: 1px solid var(--border); }
.person:last-child { border-bottom: 0; }
.person .pname { flex: 1; min-width: 0; }
.person .pname b { font-weight: 600; }
.person .pname small { display: block; color: var(--muted); }
.person .pamt { text-align: right; font-variant-numeric: tabular-nums; }
.person .pamt b { font-size: 16px; }
.person .pamt small { display: block; color: var(--muted); }
.person.done .pname b, .person.done .pamt b { text-decoration: line-through; opacity: .55; }
.pos { color: var(--pos); font-weight: 600; }
.tag { font-size: 11px; font-weight: 700; background: var(--warn-soft); color: var(--warn-ink); padding: 1px 6px; border-radius: 6px; }

.check { position: relative; width: 36px; height: 36px; flex: none; margin: 0; display: grid; place-items: center; }
.check input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; min-height: 0; }
.check span { width: 28px; height: 28px; border-radius: 8px; border: 2px solid var(--border); background: var(--surface); display: grid; place-items: center; }
.check input:checked + span { background: var(--primary); border-color: var(--primary); }
.check input:checked + span::after { content: "✓"; color: var(--primary-ink); font-weight: 800; font-size: 14px; }
.check input:focus-visible + span { box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 30%, transparent); }
.check.placeholder { font-size: 16px; }

.transfers { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.transfers li { display: flex; align-items: center; gap: 8px; background: var(--surface-2); border-radius: 10px; padding: 9px 12px; }
.transfers .arrow { color: var(--muted); }
.transfers .amt { margin-left: auto; font-weight: 700; font-variant-numeric: tabular-nums; }
.transfers li.done { opacity: .5; text-decoration: line-through; }

/* ---------- Home ---------- */
.hero { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 4px 0 16px; }
.hero p { margin: 4px 0 0; color: var(--muted); font-size: 14px; }
.month { font-size: 13px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 18px 4px 8px; display: flex; justify-content: space-between; }
.sess {
  width: 100%; font: inherit; color: inherit; text-align: left; cursor: pointer;
  display: flex; align-items: center; gap: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 12px 14px; margin-bottom: 8px;
}
.sess:hover { border-color: var(--primary); }
.sess-date { width: 50px; flex: none; text-align: center; background: var(--primary-soft); color: var(--primary); border-radius: 10px; padding: 6px 0; line-height: 1.1; }
.sess-date b { display: block; font-size: 20px; }
.sess-date span { font-size: 11px; font-weight: 600; }
.sess-info { flex: 1; min-width: 0; }
.sess-info b { display: block; }
.sess-info span { font-size: 13px; color: var(--muted); display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sess-amt { text-align: right; }
.sess-amt b { display: block; font-variant-numeric: tabular-nums; }
.pill { font-size: 11.5px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--warn-soft); color: var(--warn-ink); white-space: nowrap; }
.pill.ok { background: var(--primary-soft); color: var(--primary); }
.empty { text-align: center; padding: 36px 16px; }
.empty .big { font-size: 48px; }
.empty p { color: var(--muted); }

/* ---------- Members ---------- */
.mem { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.mem:last-child { border-bottom: 0; }
.mem input { flex: 1; }
.mem .stat { font-size: 12.5px; color: var(--muted); text-align: right; min-width: 110px; }

/* ---------- Toast ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(96px + env(safe-area-inset-bottom, 0px)); transform: translate(-50%, 20px);
  background: #14532d; color: #fff; padding: 10px 18px; border-radius: 999px;
  font-size: 14px; font-weight: 600; opacity: 0; pointer-events: none; transition: .2s; z-index: 50;
  max-width: calc(100% - 32px);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
@keyframes sheet { from { transform: translateY(40px); opacity: .4; } to { transform: none; opacity: 1; } }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

@media (max-width: 520px) {
  .card { padding: 14px; border-radius: 16px; }
  .hero { flex-direction: column; align-items: stretch; }
  h1 { font-size: 24px; }
  /* Hộp xác nhận trồi lên từ dưới */
  #modal { place-items: end stretch; padding: 0; }
  .modal-box {
    max-width: none; border-radius: 20px 20px 0 0;
    padding: 22px max(16px, env(safe-area-inset-right, 0px)) calc(16px + env(safe-area-inset-bottom, 0px)) max(16px, env(safe-area-inset-left, 0px));
    animation: sheet .2s ease-out;
  }
  .modal-box .btn { min-height: 50px; }
  .exp-sub label { min-width: 0; flex-basis: 100%; }
  .weights select { min-width: 0; }
  .stats b { font-size: 15px; }
  .mem .stat { min-width: 0; }
}
