:root {
  --bg: #f4f5f7;
  --surface: #fff;
  --text: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --primary: #2563eb;
  --danger: #dc2626;
  --ok: #16a34a;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #1a1d23;
    --text: #f3f4f6;
    --muted: #9ca3af;
    --line: #2d313a;
    color-scheme: dark;
  }
}

/* Status colors: --c accent, --sb pill background, --sf pill text */
.s-new         { --c: #2563eb; --sb: #dbeafe; --sf: #1e40af; }
.s-en_route    { --c: #7c3aed; --sb: #ede9fe; --sf: #5b21b6; }
.s-in_progress { --c: #eab308; --sb: #fef9c3; --sf: #854d0e; }
.s-ready       { --c: #16a34a; --sb: #dcfce7; --sf: #166534; }

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
a { color: inherit; text-decoration: none; }
h1, h2 { margin: 0; }

/* ---------- top bar & nav ---------- */
.top {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  padding: calc(8px + env(safe-area-inset-top, 0px)) 16px 8px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.top h1 { flex: 1; font-size: 20px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  min-width: 44px; min-height: 44px; margin-left: -10px;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 24px; border-radius: 10px;
}
.link-btn {
  border: 0; background: none; color: var(--muted);
  font: inherit; font-size: 14px; padding: 10px 0; cursor: pointer;
  max-width: 50%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(3, 1fr);
  padding-bottom: var(--safe-b);
  background: var(--surface);
  border-top: 1px solid var(--line);
}
.bottom-nav a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: 62px; font-size: 13px; color: var(--muted);
}
.bottom-nav a span { font-size: 22px; line-height: 1; filter: grayscale(1); opacity: .7; }
.bottom-nav a.on { color: var(--primary); font-weight: 600; }
.bottom-nav a.on span { filter: none; opacity: 1; }

.content { max-width: 720px; margin: 0 auto; padding: 16px 16px calc(110px + var(--safe-b)); }
.has-nav .content { padding-bottom: calc(170px + var(--safe-b)); }

/* ---------- controls ---------- */
input, select, textarea {
  width: 100%; min-height: 52px; padding: 12px 14px;
  font: inherit; color: var(--text);
  background: var(--surface);
  border: 1.5px solid var(--line); border-radius: 12px;
  outline: none; appearance: none;
}
select {
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 20px) 50%, calc(100% - 14px) 50%;
  background-size: 6px 6px; background-repeat: no-repeat; padding-right: 40px;
}
textarea { min-height: 96px; resize: vertical; }
input:focus, select:focus, textarea:focus { border-color: var(--primary); }
/* Read-only fields for masters: keep text fully legible */
input:disabled, select:disabled, textarea:disabled {
  background: var(--bg); color: var(--text); -webkit-text-fill-color: var(--text); opacity: 1; border-style: dashed;
}
.st:disabled:not(.on) { opacity: .45; }
[hidden] { display: none !important; }
.btn.pay { background: var(--ok); border-color: var(--ok); color: #fff; }
.banner {
  padding: 14px 16px; border-radius: var(--radius);
  background: #dcfce7; color: #166534; font-weight: 600;
}
label { display: flex; flex-direction: column; gap: 6px; font-size: 15px; color: var(--muted); }

.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 52px; padding: 0 18px;
  font: inherit; font-weight: 600; color: var(--text);
  background: var(--surface);
  border: 1.5px solid var(--line); border-radius: 12px; cursor: pointer;
}
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.big { width: 100%; min-height: 58px; font-size: 18px; }
.btn.danger { width: 100%; background: transparent; color: var(--danger); border-color: currentColor; }
.btn:disabled { opacity: .6; }

.with-btn { display: flex; gap: 8px; }
.with-btn input { flex: 1; min-width: 0; }
.with-btn .btn { flex: none; color: var(--ok); border-color: currentColor; }

.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 8px; }
.seg button {
  min-height: 52px; padding: 0 10px;
  font: inherit; font-weight: 600; color: var(--text);
  background: var(--surface);
  border: 1.5px solid var(--line); border-radius: 12px; cursor: pointer;
}
.seg button.on { background: var(--primary); border-color: var(--primary); color: #fff; }
.seg [data-v="true"].on { background: var(--ok); border-color: var(--ok); }
.seg[data-name="active"] [data-v="false"].on { background: var(--danger); border-color: var(--danger); }
.seg button:disabled { opacity: .5; cursor: default; }

/* ---------- list ---------- */
.chips {
  display: flex; gap: 8px; overflow-x: auto;
  margin: 0 -16px; padding: 12px 16px;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  --c: var(--text);
  flex: none; display: inline-flex; align-items: center; gap: 6px;
  min-height: 44px; padding: 0 14px;
  font: inherit; font-size: 15px; color: var(--text);
  background: var(--surface);
  border: 1.5px solid var(--line); border-radius: 999px; cursor: pointer;
}
.chip[class*="s-"]::before { content: ""; width: 10px; height: 10px; border-radius: 50%; background: var(--c); }
.chip.s-active::before, .chip.s-all::before, .chip.s-month::before { display: none; }
.chip b { color: var(--muted); font-weight: 600; }
.chip.on { background: var(--c); border-color: var(--c); color: #fff; }
.chip.s-active.on, .chip.s-all.on, .chip.s-month.on { background: var(--text); border-color: var(--text); color: var(--bg); }
.chip.on b { color: inherit; opacity: .8; }
.chip.on::before { background: currentColor; }
.chip.s-in_progress.on { color: #422006; }

.list { display: flex; flex-direction: column; gap: 10px; }
.card {
  display: block; padding: 14px 16px;
  background: var(--surface);
  border-radius: var(--radius);
  border-left: 6px solid var(--c, var(--line));
  box-shadow: 0 1px 2px rgb(0 0 0 / .06);
}
.card:active { transform: scale(.99); }
.card .title { font-size: 18px; font-weight: 600; margin: 4px 0 2px; }
.row { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.mt { margin-top: 8px; }
.num-badge { color: var(--text); font-weight: 700; }
.muted { color: var(--muted); font-size: 15px; }
.small { font-size: 14px; margin-top: 4px; }
.empty { padding: 48px 16px; text-align: center; color: var(--muted); }

.pill {
  padding: 4px 10px; border-radius: 999px;
  background: var(--sb); color: var(--sf);
  font-size: 13px; font-weight: 600; white-space: nowrap;
}
.tag { padding: 3px 8px; border-radius: 8px; font-size: 13px; font-weight: 600; white-space: nowrap; }
.tag.paid { background: #dcfce7; color: #166534; }
.tag.unpaid { background: #f3f4f6; color: #6b7280; }
.tag.due { background: #ffedd5; color: #9a3412; }
.dot {
  display: inline-block; width: 10px; height: 10px; margin-right: 8px;
  border-radius: 50%; background: var(--danger); vertical-align: middle;
  animation: pulse 1.5s infinite;
}
@keyframes pulse { 50% { opacity: .3; } }

.fab {
  position: fixed; z-index: 9;
  left: 16px; right: 16px; bottom: calc(16px + var(--safe-b));
  max-width: 688px; margin: 0 auto;
  display: flex; align-items: center; justify-content: center;
  min-height: 58px; border-radius: 16px;
  background: var(--primary); color: #fff;
  font-size: 18px; font-weight: 700;
  box-shadow: 0 6px 20px rgb(37 99 235 / .35);
}
.has-nav .fab { bottom: calc(78px + var(--safe-b)); }

/* ---------- forms ---------- */
.form, .stack { display: flex; flex-direction: column; gap: 14px; }
section {
  display: flex; flex-direction: column; gap: 12px;
  padding: 16px; background: var(--surface); border-radius: var(--radius);
}
section h2 { font-size: 13px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.static { padding: 14px; background: var(--bg); border-radius: 12px; }
.fin { display: flex; flex-direction: column; padding: 4px 14px; background: var(--bg); border-radius: 12px; }
.fin div { display: flex; justify-content: space-between; align-items: center; padding: 8px 0; }
.fin span { color: var(--muted); }
.fin b { font-variant-numeric: tabular-nums; }
.fin .net { border-top: 1px solid var(--line); font-size: 18px; }
.fin .net span { color: var(--text); font-weight: 600; }
.fin .net b { font-size: 22px; color: var(--ok); }
.fin .net.bad b { color: var(--danger); }

.status-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
@media (min-width: 560px) { .status-grid { grid-template-columns: repeat(4, 1fr); } }
.st {
  min-height: 56px; padding: 0 8px;
  font: inherit; font-size: 16px; font-weight: 600; color: var(--text);
  background: var(--surface);
  border: 2px solid var(--c); border-radius: 12px; cursor: pointer;
}
.st.on { background: var(--c); color: #fff; }
.st.s-in_progress.on { color: #422006; }

.savebar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 9;
  padding: 12px 16px calc(12px + var(--safe-b));
  background: var(--surface); border-top: 1px solid var(--line);
}
.savebar .btn { display: flex; max-width: 688px; margin: 0 auto; }

/* ---------- dashboard ---------- */
.tiles { display: grid; gap: 10px; margin-bottom: 14px; }
.tile { padding: 16px; background: var(--surface); border-radius: var(--radius); }
.tile.accent { background: var(--primary); color: #fff; }
.tile.accent .k, .tile.accent .muted { color: rgb(255 255 255 / .85); }
.tile .k { font-size: 15px; color: var(--muted); }
.tile .v { font-size: 30px; font-weight: 700; margin: 4px 0; font-variant-numeric: tabular-nums; }
.period { margin-bottom: 14px; }
.plan { margin-bottom: 10px; }
.plan .of { font-size: 16px; font-weight: 500; opacity: .85; }
.bar { height: 10px; margin: 10px 0 6px; border-radius: 999px; background: rgb(255 255 255 / .3); overflow: hidden; }
.bar i { display: block; height: 100%; border-radius: inherit; background: #fff; }
.link-btn.light { color: #fff; max-width: none; text-decoration: underline; }
.inline-form { display: flex; gap: 8px; margin-top: 12px; }
.inline-form input { flex: 1; min-width: 0; }
.inline-form .btn { flex: none; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 10px; }
.grid2 .tile .v { font-size: 22px; }
.tile .v.neg { color: var(--danger); }
.exp-list { display: flex; flex-direction: column; }
.exp { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 10px 0; border-top: 1px solid var(--line); }
.exp .icon-btn { margin: 0; font-size: 18px; color: var(--muted); }
.section-title { margin: 22px 0 10px; font-size: 13px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; color: var(--muted); }
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-top: 10px; }
@media (min-width: 560px) { .stats { grid-template-columns: repeat(4, 1fr); } }
.stats div { display: flex; flex-direction: column; gap: 2px; padding: 10px 12px; background: var(--bg); border-radius: 10px; }
.stats .muted { font-size: 13px; }
.stats b { font-size: 18px; font-variant-numeric: tabular-nums; }

/* ---------- login & toast ---------- */
.login { max-width: 380px; margin: 0 auto; padding: 12vh 16px 32px; text-align: center; }
.login img { border-radius: 18px; }
.login h1 { margin: 12px 0 28px; font-size: 26px; }
.login .stack { text-align: left; }

.toast {
  position: fixed; z-index: 100; left: 16px; right: 16px;
  top: calc(12px + env(safe-area-inset-top, 0px));
  max-width: 480px; margin: 0 auto; padding: 14px 16px;
  background: #111827; color: #fff; border-radius: 12px;
  font-weight: 600; text-align: center;
  box-shadow: 0 8px 24px rgb(0 0 0 / .25);
  opacity: 0; transform: translateY(-12px); pointer-events: none;
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: none; }
.toast.error { background: var(--danger); }
.toast.ok { background: var(--ok); }
.toast.info { background: var(--primary); }

/* analytics periods & ledger */
.chip-select {
  flex: none; width: auto; min-height: 44px; padding: 0 36px 0 14px;
  font-size: 15px; border-radius: 999px;
}
.chip-select.on { border-color: var(--text); font-weight: 600; }
.chip.on:not([class*="s-"]) { background: var(--text); border-color: var(--text); color: var(--bg); }
.close-month { width: 100%; margin-bottom: 10px; }
.banner .link-btn { color: inherit; text-decoration: underline; padding: 4px 0; max-width: none; }
.amt-in { color: var(--ok); }
.amt-out { color: var(--danger); }
.search-info { padding: 12px 0; color: var(--muted); font-size: 15px; }
input[type="search"]::-webkit-search-cancel-button { -webkit-appearance: searchfield-cancel-button; }

/* master levels */
.lv-bronze   { --lc: #b45309; --lb: #fdebd8; }
.lv-silver   { --lc: #64748b; --lb: #eef1f5; }
.lv-gold     { --lc: #ca8a04; --lb: #fef6d8; }
.lv-platinum { --lc: #0e7490; --lb: #dcf6fb; }
.tile.level { margin: 10px 0; border: 2px solid var(--lc); }
.tile.level .v { color: var(--lc); }
.tile.level .bar { background: var(--lb); }
.tile.level .bar i { background: var(--lc); }
.levels { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-top: 12px; }
.levels > div {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 4px; border-radius: 10px; background: var(--bg); text-align: center;
  opacity: .55;
}
.levels > div.done { opacity: .85; }
.levels > div.on { opacity: 1; background: var(--lb); outline: 2px solid var(--lc); color: #111827; }
.levels span { font-size: 22px; line-height: 1; }
.levels b { font-size: 13px; }
.levels small { font-size: 11px; color: var(--muted); white-space: nowrap; }
.levels > div.on small { color: #374151; }
.lv-badge {
  flex: none; padding: 4px 10px; border-radius: 999px;
  background: var(--lb); color: var(--lc); border: 1.5px solid var(--lc);
  font-size: 13px; font-weight: 700; white-space: nowrap;
}
.month-head {
  display: flex; align-items: center; gap: 8px;
  margin: 14px 0 2px; font-size: 15px; font-weight: 700; color: var(--text);
}
.month-head:first-child { margin-top: 2px; }
.month-head span {
  padding: 1px 8px; border-radius: 999px; background: var(--line);
  font-size: 13px; font-weight: 600; color: var(--muted);
}
.more-btn { width: 100%; margin-top: 6px; }

/* avatars & profile */
.avatar {
  flex: none; display: inline-flex; align-items: center; justify-content: center;
  border-radius: 50%; object-fit: cover; background: var(--line);
  color: #fff; font-weight: 700; letter-spacing: .02em; vertical-align: middle;
}
.who { display: flex; align-items: center; gap: 10px; min-width: 0; }
.who .title { margin: 0; }
.link-btn:has(.avatar) { display: inline-flex; align-items: center; gap: 8px; }
.link-btn span { overflow: hidden; text-overflow: ellipsis; }
.exec { display: flex; align-items: center; gap: 6px; }
.profile { flex-direction: row; align-items: center; gap: 16px; }
.profile-actions { display: flex; flex-direction: column; gap: 8px; flex: 1; min-width: 0; }
.profile-actions .btn { min-height: 44px; }
.btn.link-danger { color: var(--danger); border-color: transparent; background: transparent; min-height: 36px; }
.carried { display: inline-block; margin: 2px 0 4px; padding: 2px 8px; border-radius: 8px; background: #ede9fe; color: #5b21b6; font-size: 13px; font-weight: 600; }
