/* Layout & components. All colours/radii/fonts/shadows come from theme.css tokens. */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  font-size: var(--fs-base);
  line-height: 1.4;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; touch-action: manipulation; }
input, select, textarea { font-size: max(16px, 1em); } /* >=16px prevents iOS zoom on focus */
a { color: var(--accent); text-decoration: none; }
[hidden] { display: none !important; }
svg { display: block; }

/* Numbers: always LTR, isolated from surrounding RTL text */
.amt { direction: ltr; unicode-bidi: isolate; font-family: var(--font-num); font-variant-numeric: tabular-nums; white-space: nowrap; }
.income { color: var(--income); }
.expense { color: var(--expense); }
.muted { color: var(--text-2); }
.small { font-size: var(--fs-sm); }
.xs { font-size: var(--fs-xs); }

/* ---------- app shell ---------- */
.app { min-height: 100dvh; }
.topbar {
  position: sticky; top: 0; z-index: 20;
  padding: calc(env(safe-area-inset-top) + 6px) max(16px, env(safe-area-inset-right)) 8px max(16px, env(safe-area-inset-left));
  background: var(--bg);
  display: flex; align-items: center; gap: 8px; min-height: 56px;
}
.topbar .title { font-size: var(--fs-xl); font-weight: var(--fw-bold); flex: 1; margin: 0; }
.icon-btn {
  width: 44px; height: 44px; border-radius: var(--radius-pill);
  display: inline-flex; align-items: center; justify-content: center; color: var(--text);
  flex: none;
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn svg { width: 24px; height: 24px; }

.month-switch { flex: 1; display: flex; align-items: center; justify-content: center; gap: 4px; }
.month-switch .label { font-size: var(--fs-lg); font-weight: var(--fw-semi); min-width: 130px; text-align: center; }
.month-switch .today-btn { font-size: var(--fs-xs); color: var(--accent); padding: 4px 8px; min-height: 28px; }
.topbar-spacer { width: 44px; flex: none; }

main.view {
  padding: 4px max(16px, env(safe-area-inset-right)) calc(96px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  max-width: 640px; margin: 0 auto;
}

/* ---------- tab bar ---------- */
.tabbar {
  position: fixed; inset-inline: 0; bottom: 0; z-index: 30;
  background: var(--tabbar-bg);
  border-top: 1px solid var(--tabbar-border);
  backdrop-filter: saturate(180%) blur(16px); -webkit-backdrop-filter: saturate(180%) blur(16px);
  padding-bottom: env(safe-area-inset-bottom);
  padding-inline: env(safe-area-inset-right) env(safe-area-inset-left);
}
.tabbar-inner { display: grid; grid-template-columns: repeat(5, 1fr); height: 60px; max-width: 640px; margin: 0 auto; }
.tab {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--tab-inactive); font-size: 11px; font-weight: var(--fw-semi); min-height: 44px;
}
.tab svg { width: 25px; height: 25px; }
.tab[aria-current="page"] { color: var(--tab-active); }
.fab-wrap { display: flex; justify-content: center; }
.fab {
  width: 60px; height: 60px; border-radius: var(--radius-pill);
  background: var(--fab-bg); color: var(--fab-icon);
  box-shadow: var(--shadow-fab);
  display: flex; align-items: center; justify-content: center;
  transform: translateY(-18px);
  transition: transform .15s ease;
}
.fab:active { transform: translateY(-18px) scale(.94); }
.fab svg { width: 30px; height: 30px; }

/* ---------- cards & sections ---------- */
.card { background: var(--card); border-radius: var(--radius-card); box-shadow: var(--shadow-card); padding: 16px; margin-bottom: 14px; }
.card.flush { padding: 4px 0; }
.section-head { display: flex; align-items: baseline; justify-content: space-between; margin: 20px 4px 8px; }
.section-head h2 { font-size: var(--fs-lg); margin: 0; font-weight: var(--fw-bold); }
.section-head .link { font-size: var(--fs-sm); color: var(--accent); min-height: 44px; display: inline-flex; align-items: center; padding-inline: 4px; margin-block: -12px; }

.hero { background: var(--hero-bg); border-radius: var(--radius-card); padding: 20px; color: var(--hero-text); box-shadow: var(--shadow-card); }
.hero .label { font-size: var(--fs-sm); }
.hero .big { font-size: var(--fs-hero); font-weight: var(--fw-bold); color: var(--hero-amount); line-height: 1.15; margin: 4px 0 16px; letter-spacing: -0.5px; }
.hero .row { display: grid; grid-template-columns: 1fr 1fr; border-top: 1px solid var(--hero-divider); padding-top: 12px; }
.hero .row > div + div { border-inline-start: 1px solid var(--hero-divider); padding-inline-start: 14px; }
.hero .row .v { font-size: var(--fs-lg); font-weight: var(--fw-semi); margin-top: 2px; display: block; }
.hero .v.inc { color: var(--hero-income); }
.hero .v.exp { color: var(--hero-expense); }

/* budget bars */
.bud { padding: 12px 16px; display: block; width: 100%; text-align: start; }
.bud + .bud { border-top: 1px solid var(--hairline); }
.bud .top { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.bud .name { flex: 1; font-weight: var(--fw-semi); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bud .pct { font-size: var(--fs-sm); font-weight: var(--fw-semi); color: var(--text-2); }
.bud .nums { display: flex; justify-content: space-between; font-size: var(--fs-xs); color: var(--text-2); margin-top: 6px; }
.bar { height: 8px; border-radius: var(--radius-pill); background: var(--track); overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: inherit; background: var(--bar-ok); transition: width .4s ease; }
.state-warning .bar > i { background: var(--warn); }
.state-warning .pct { color: var(--warn-text); }
.state-over .bar > i { background: var(--over); }
.state-over .pct { color: var(--over); }
.bud.nobudget .name { font-weight: 500; }
.bud .set { font-size: var(--fs-sm); color: var(--accent); }

/* category emoji badge */
.cat-ico {
  width: 40px; height: 40px; border-radius: var(--radius-md); flex: none;
  display: inline-flex; align-items: center; justify-content: center; font-size: 20px;
  background: color-mix(in srgb, var(--c, #94A3B8) 18%, transparent);
}
.cat-ico.sm { width: 32px; height: 32px; font-size: 16px; border-radius: var(--radius-sm); }

/* transaction rows */
.tx { display: flex; align-items: center; gap: 12px; padding: 10px 16px; min-height: 60px; width: 100%; text-align: start; }
.tx + .tx { border-top: 1px solid var(--hairline); }
.tx:active, .bud:active, .row-btn:active { background: var(--surface-2); }
.tx .mid { flex: 1; min-width: 0; }
.tx .t1 { font-weight: var(--fw-semi); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx .t2 { font-size: var(--fs-xs); color: var(--text-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx .a { font-weight: var(--fw-semi); }
.tx .rec-badge { font-size: 11px; color: var(--text-2); }
.day-head { display: flex; justify-content: space-between; align-items: baseline; margin: 16px 6px 6px; font-size: var(--fs-sm); color: var(--text-2); font-weight: var(--fw-semi); }

.empty { text-align: center; color: var(--text-2); padding: 28px 16px; }
.empty .em { font-size: 40px; display: block; margin-bottom: 6px; }

/* search & filters */
.search { position: relative; margin-bottom: 10px; }
.search input {
  width: 100%; height: 44px; border-radius: var(--radius-md); border: 0; background: var(--card);
  padding-inline: 40px 12px; outline: none; box-shadow: var(--shadow-card);
}
.search svg { position: absolute; inset-inline-start: 12px; top: 12px; width: 20px; height: 20px; color: var(--muted); pointer-events: none; }
.seg { display: flex; background: var(--surface-2); border-radius: var(--radius-sm); padding: 3px; gap: 3px; }
.card .seg, .sheet .seg { background: var(--surface-2); }
.view > .seg { background: var(--card); box-shadow: var(--shadow-card); }
.seg button { flex: 1; min-height: 38px; border-radius: 8px; font-size: var(--fs-sm); font-weight: var(--fw-semi); color: var(--text-2); }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }
.totals-line { display: flex; justify-content: space-between; gap: 8px; font-size: var(--fs-sm); color: var(--text-2); margin: 12px 6px 0; }

/* stat grid */
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.stat { background: var(--surface-2); border-radius: var(--radius-md); padding: 10px; }
.stat .k { font-size: var(--fs-xs); color: var(--text-2); }
.stat .v { font-weight: var(--fw-bold); font-size: var(--fs-base); display: block; margin-top: 2px; }

/* charts */
.chart-title { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.chart-title h3 { margin: 0; font-size: var(--fs-base); }
.donut-wrap { display: flex; justify-content: center; margin: 4px 0 14px; }
.donut-wrap svg { width: 210px; height: 210px; }
.donut-center-1 { font-size: 12px; fill: var(--text-2); }
.donut-center-2 { font-size: 20px; font-weight: 700; fill: var(--text); font-family: var(--font-num); }
.legend { list-style: none; margin: 0; padding: 0; }
.legend li { display: flex; align-items: center; gap: 10px; padding: 8px 0; }
.legend li + li { border-top: 1px solid var(--hairline); }
.legend .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--c); flex: none; }
.legend .nm { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.legend .pc { color: var(--text-2); font-size: var(--fs-sm); min-width: 42px; text-align: end; }
.trend svg { width: 100%; height: auto; }
.svg-grid { stroke: var(--hairline); stroke-width: 1; }
.svg-axis { fill: var(--text-2); font-size: 11px; font-family: var(--font); }
.svg-inc { fill: var(--income); }
.svg-exp { fill: var(--expense); }
.svg-bal { stroke: var(--accent); stroke-width: 2; fill: none; }
.svg-bal-dot { fill: var(--accent); }
.svg-cur { fill: var(--accent-soft); }
.chart-legend { display: flex; gap: 14px; justify-content: center; font-size: var(--fs-xs); color: var(--text-2); margin-top: 8px; }
.chart-legend span::before { content: ""; display: inline-block; width: 10px; height: 10px; border-radius: 3px; background: var(--c); margin-inline-end: 5px; vertical-align: -1px; }
.hbar-row { display: grid; grid-template-columns: 64px 1fr auto; gap: 10px; align-items: center; margin: 10px 0; font-size: var(--fs-sm); }
.hbar-row .bar { height: 12px; }
.hbar-row .bar > i.inc { background: var(--income); }
.hbar-row .bar > i.exp { background: var(--expense); }

/* settings lists */
.list-title { font-size: var(--fs-sm); color: var(--text-2); margin: 20px 8px 6px; font-weight: var(--fw-semi); }
.row-btn { display: flex; align-items: center; gap: 12px; width: 100%; min-height: 52px; padding: 8px 16px; text-align: start; }
.row-btn + .row-btn, .row-btn + .row-static, .row-static + .row-btn { border-top: 1px solid var(--hairline); }
.row-btn .grow, .row-static .grow { flex: 1; min-width: 0; }
.row-btn .sub { font-size: var(--fs-xs); color: var(--text-2); }
.row-btn.archived { opacity: .55; }
.row-btn .chev { color: var(--muted); width: 18px; height: 18px; }
.row-btn.danger { color: var(--danger); }
.row-btn.accent { color: var(--accent); font-weight: var(--fw-semi); }
.row-static { display: flex; align-items: center; gap: 12px; padding: 8px 16px; min-height: 52px; }
.tag { font-size: 11px; padding: 2px 8px; border-radius: var(--radius-pill); background: var(--surface-2); color: var(--text-2); }

/* switch */
.switch { position: relative; width: 51px; height: 31px; flex: none; }
.switch input { opacity: 0; position: absolute; inset: 0; margin: 0; cursor: pointer; z-index: 1; }
.switch span { position: absolute; inset: 0; background: var(--track); border-radius: var(--radius-pill); transition: background .2s; }
.switch span::after { content: ""; position: absolute; top: 2px; inset-inline-start: 2px; width: 27px; height: 27px; border-radius: 50%; background: #fff; box-shadow: 0 2px 4px rgba(0,0,0,.2); transition: transform .2s; }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(-20px); }
[dir="ltr"] .switch input:checked + span::after { transform: translateX(20px); }

/* ---------- bottom sheet ---------- */
.sheet-backdrop { position: fixed; inset: 0; background: var(--backdrop); z-index: 50; opacity: 0; transition: opacity .25s ease; }
.sheet-backdrop.open { opacity: 1; }
.sheet {
  position: fixed; inset-inline: 0; bottom: var(--kb, 0px); z-index: 51;
  background: var(--card); border-radius: var(--radius-sheet) var(--radius-sheet) 0 0;
  box-shadow: var(--shadow-sheet);
  max-height: calc(100dvh - env(safe-area-inset-top) - 24px - var(--kb, 0px));
  display: flex; flex-direction: column;
  transform: translateY(100%); transition: transform .3s cubic-bezier(.2,.8,.2,1);
  max-width: 640px; margin: 0 auto;
}
.sheet.open { transform: translateY(0); }
.sheet .grab { padding: 8px 0 2px; display: flex; justify-content: center; touch-action: none; }
.sheet .grab i { width: 38px; height: 5px; border-radius: 3px; background: var(--track); }
.sheet .sheet-head { display: flex; align-items: center; padding: 0 8px 4px 16px; touch-action: none; }
.sheet .sheet-head h2 { flex: 1; margin: 0; font-size: var(--fs-lg); }
.sheet .sheet-body {
  overflow-y: auto; -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
  padding: 8px max(16px, env(safe-area-inset-right)) calc(16px + env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}
body.sheet-open { overflow: hidden; }

/* forms */
.field { margin-bottom: 14px; }
.field > label, .field > .lbl { display: block; font-size: var(--fs-sm); color: var(--text-2); margin-bottom: 6px; font-weight: var(--fw-semi); }
.input, .field input[type="text"], .field input[type="date"], .field input[type="number"], .field input[type="password"], .field select, .field textarea {
  width: 100%; min-height: 48px; border-radius: var(--radius-md); border: 1px solid transparent;
  background: var(--surface-2); padding: 10px 12px; outline: none; font-size: 17px;
}
.field input:focus, .field select:focus, .input:focus { border-color: var(--accent); }
.field input[type="date"] { -webkit-appearance: none; appearance: none; display: block; text-align: start; }
.amount-input { display: flex; align-items: center; justify-content: center; gap: 6px; direction: ltr; background: var(--surface-2); border-radius: var(--radius-md); padding: 6px 14px; border: 1px solid transparent; }
.amount-input:focus-within { border-color: var(--accent); }
.amount-input .cur { font-size: 30px; font-weight: var(--fw-bold); color: var(--text-2); }
.amount-input input {
  border: 0; background: transparent; outline: none; width: 100%; min-width: 0;
  font-size: 40px; font-weight: var(--fw-bold); text-align: center; font-family: var(--font-num); padding: 4px 0;
  direction: ltr;
}
.amount-input.expense input { color: var(--text); }
.amount-input.income input { color: var(--income); }
.chips { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.chip {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: 64px; padding: 6px 4px; border-radius: var(--radius-md);
  background: var(--surface-2); font-size: 12px; line-height: 1.15; text-align: center; border: 2px solid transparent;
  word-break: break-word;
}
.chip .e { font-size: 22px; }
.chip[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); font-weight: var(--fw-semi); }
.quick-dates { display: flex; gap: 6px; margin-top: 6px; }
.quick-dates button { font-size: var(--fs-sm); padding: 6px 12px; min-height: 36px; border-radius: var(--radius-pill); background: var(--surface-2); color: var(--text-2); }
.quick-dates button[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); font-weight: var(--fw-semi); }
.btn {
  display: flex; align-items: center; justify-content: center; gap: 8px; width: 100%;
  min-height: 52px; border-radius: var(--radius-md); font-weight: var(--fw-bold); font-size: 17px;
  background: var(--accent); color: var(--on-accent);
}
.btn:disabled { opacity: .5; }
.btn:active { filter: brightness(.95); }
.btn.secondary { background: var(--surface-2); color: var(--text); }
.btn.danger { background: transparent; color: var(--danger); }
.btn-row { display: flex; gap: 10px; margin-top: 8px; }
.btn-row .btn { flex: 1; }
.form-error { color: var(--danger); font-size: var(--fs-sm); min-height: 20px; margin: 4px 0; }
.colors { display: flex; flex-wrap: wrap; gap: 10px; }
.colors button { width: 36px; height: 36px; border-radius: 50%; background: var(--c); border: 3px solid transparent; outline: 2px solid transparent; }
.colors button[aria-pressed="true"] { outline-color: var(--text); border-color: var(--card); }
.emoji-input { font-size: 26px !important; text-align: center; width: 72px !important; }
.hint { font-size: var(--fs-xs); color: var(--text-2); margin-top: 4px; }
.confirm-text { margin: 4px 0 16px; color: var(--text-2); }

/* ---------- login ---------- */
.login {
  min-height: 100dvh; display: flex; flex-direction: column; justify-content: center;
  padding: calc(env(safe-area-inset-top) + 24px) 24px calc(env(safe-area-inset-bottom) + 24px);
  max-width: 420px; margin: 0 auto;
}
.login .logo { width: 84px; height: 84px; border-radius: 22px; margin: 0 auto 16px; display: block; }
.login h1 { text-align: center; margin: 0 0 4px; font-size: 30px; }
.login p { text-align: center; color: var(--text-2); margin: 0 0 28px; }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- toast ---------- */
.toast {
  position: fixed; z-index: 80; inset-inline: 16px; bottom: calc(84px + env(safe-area-inset-bottom));
  margin: 0 auto; max-width: 400px; background: var(--text); color: var(--bg);
  border-radius: var(--radius-md); padding: 12px 16px; text-align: center; font-weight: var(--fw-semi);
  box-shadow: var(--shadow-toast); opacity: 0; transform: translateY(10px); transition: all .25s ease; pointer-events: none;
}
.toast.show { opacity: 1; transform: none; }
.toast.err { background: var(--danger); color: #fff; }

.loading { text-align: center; color: var(--muted); padding: 40px 0; }
.offline-banner { background: var(--warn); color: #1B1D29; text-align: center; font-size: var(--fs-xs); padding: 4px; font-weight: var(--fw-semi); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}
