:root {
  --bg: var(--tg-theme-bg-color, #0e1014);
  --secondary-bg: var(--tg-theme-secondary-bg-color, #181b22);
  --text: var(--tg-theme-text-color, #f2f4f8);
  --hint: var(--tg-theme-hint-color, #8b94a3);
  --link: var(--tg-theme-link-color, #5bc0eb);
  --button: var(--tg-theme-button-color, #2ea3ff);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --destructive: var(--tg-theme-destructive-text-color, #ff5b5b);
  --success: #27c281;
  --warn: #f5a524;
  --radius: 16px;
  --radius-sm: 10px;
  --gap: 12px;
  --safe-bottom: env(safe-area-inset-bottom, 0);
}

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

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  overscroll-behavior: contain;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
}

#app[hidden] { display: none; }

main { padding: 8px var(--gap); padding-bottom: calc(96px + var(--safe-bottom)); }

/* ---------- Topbar ---------- */
.topbar {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  padding: 18px 16px 8px;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 5;
}
.brand { font-size: 24px; font-weight: 700; letter-spacing: 0.5px; }
.balance { font-size: 18px; font-weight: 600; color: var(--hint); font-variant-numeric: tabular-nums; }

.view { display: none; }
.view.active { display: block; }
.view-title { margin: 8px 4px 16px; font-size: 18px; font-weight: 600; }

/* ---------- Lists / Cards ---------- */
.list { display: flex; flex-direction: column; gap: 8px; margin-bottom: var(--gap); }

.card {
  background: var(--secondary-bg);
  border-radius: var(--radius);
  padding: 14px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  transition: transform 0.06s ease;
}
.card:active { transform: scale(0.99); }
.card .name { font-weight: 600; line-height: 1.25; }
.card .meta { font-size: 12px; color: var(--hint); margin-top: 4px; }
.card .amount { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.card .amount.debit { color: var(--destructive); }
.card .amount.credit { color: var(--success); }
.card .right { display: flex; align-items: center; gap: 8px; }

.cat-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 8px;
  font-size: 11px;
  font-weight: 600;
  background: rgba(46, 163, 255, 0.15);
  color: var(--button);
  margin-top: 4px;
  white-space: nowrap;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
}
.cat-pill.empty { background: rgba(139, 148, 163, 0.15); color: var(--hint); font-style: italic; }
.cat-pill.ai::before { content: "✨"; font-size: 10px; }
.cat-pill.income { background: rgba(39, 194, 129, 0.15); color: var(--success); }
.cat-pill.expense { background: rgba(255, 91, 91, 0.13); color: var(--destructive); }
.tx-card { cursor: pointer; }

/* ---------- Buttons ---------- */
.btn-primary, .btn-secondary {
  border: none;
  border-radius: var(--radius-sm);
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  padding: 14px 18px;
}
.btn-primary { background: var(--button); color: var(--button-text); }
.btn-secondary { background: var(--secondary-bg); color: var(--text); }
.btn-block { width: 100%; }

.btn-sync {
  background: transparent;
  color: var(--link);
  border: 1px solid var(--link);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  min-width: 36px;
}
.btn-sync:disabled { opacity: 0.5; cursor: wait; }

/* ---------- Forms ---------- */
.form { display: flex; flex-direction: column; gap: var(--gap); }
.form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
  color: var(--hint);
}
.form input, .form select {
  background: var(--secondary-bg);
  color: var(--text);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 13px 14px;
  font-size: 16px;
  width: 100%;
  font-family: inherit;
}
.form input:focus, .form select:focus { outline: none; border-color: var(--button); }
.form input::placeholder { color: var(--hint); opacity: 0.6; }

.row { display: flex; gap: var(--gap); }
.row > * { flex: 1; }

.checkbox-row {
  flex-direction: row !important;
  align-items: center;
  gap: 10px !important;
  font-size: 14px !important;
  color: var(--text) !important;
}
.checkbox-row input { width: auto; flex: 0; }

/* ---------- Dialog ---------- */
dialog {
  border: none;
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  padding: 18px;
  width: min(94vw, 420px);
  max-width: 100vw;
  max-height: 92vh;
  overflow-y: auto;
}
dialog::backdrop { background: rgba(0,0,0,.6); }
.dlg-title { margin: 0 0 6px; font-size: 18px; }

/* ---------- Hints ---------- */
.hint {
  color: var(--hint);
  font-size: 13px;
  line-height: 1.45;
  padding: 10px 12px;
  background: var(--secondary-bg);
  border-radius: var(--radius-sm);
}
.hint a { color: var(--link); }
.hint-warn { background: rgba(245, 165, 36, 0.15); color: var(--warn); }
.hint-info { background: rgba(46, 163, 255, 0.12); color: var(--text); }

/* ---------- Empty / muted ---------- */
.muted {
  color: var(--hint);
  font-size: 14px;
  text-align: center;
  padding: 32px 16px;
  background: var(--secondary-bg);
  border-radius: var(--radius);
  border: 1px dashed rgba(139, 148, 163, 0.25);
}
.muted-emoji { font-size: 36px; display: block; margin-bottom: 8px; opacity: 0.7; }

/* ---------- Bottom nav ---------- */
.bottomnav {
  position: fixed;
  left: 0; right: 0;
  bottom: 0;
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 8px 16px calc(8px + var(--safe-bottom));
  background: var(--secondary-bg);
  border-top: 1px solid rgba(255,255,255,0.05);
  z-index: 10;
}
.navbtn {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--hint);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 4px;
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
}
.navbtn .navico { font-size: 22px; }
.navbtn.active { color: var(--button); }
.navbtn.fab {
  flex: 0 0 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--button);
  color: var(--button-text);
  font-size: 28px;
  font-weight: 400;
  margin-top: -22px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}
.navbtn.fab.active { background: var(--button); color: var(--button-text); }

/* ---------- Settings ---------- */
.settings-section { margin-bottom: 20px; }
.settings-label {
  font-size: 12px;
  font-weight: 600;
  color: var(--hint);
  text-transform: uppercase;
  letter-spacing: 0.4px;
  margin: 8px 4px 8px;
}
.settings-card { display: block; padding: 14px; }
.settings-details {
  margin-top: 8px;
  padding: 10px 14px;
  background: var(--secondary-bg);
  border-radius: var(--radius-sm);
}
.settings-details summary {
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
  color: var(--link);
  padding: 4px 0;
}
.ai-status-row {
  display: flex; align-items: center; justify-content: space-between;
  font-size: 14px;
}
.ai-status-row .label { color: var(--hint); }
.ai-status-row .value { font-weight: 600; }
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 8px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}
.badge.byok { background: rgba(46, 163, 255, 0.18); color: var(--button); }
.badge.global { background: rgba(139, 148, 163, 0.2); color: var(--hint); }
.badge.none { background: rgba(245, 165, 36, 0.18); color: var(--warn); }

/* ---------- Categories tab ---------- */
.period-bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 4px 0 12px;
}
.period-arrow {
  background: var(--secondary-bg); border: none; color: var(--text);
  width: 36px; height: 36px; border-radius: 50%;
  font-size: 22px; cursor: pointer; line-height: 1;
}
.period-label {
  flex: 1;
  background: transparent; border: none; color: var(--text);
  font-size: 17px; font-weight: 600; text-align: center; cursor: pointer;
}

.totals-row { display: flex; gap: 8px; margin-bottom: 16px; }
.total-card {
  flex: 1;
  background: var(--secondary-bg);
  border: 2px solid transparent;
  color: var(--text);
  border-radius: var(--radius);
  padding: 14px 12px;
  cursor: pointer;
  transition: border-color 0.12s ease;
  font-family: inherit;
  text-align: left;
}
.total-card.active.expense { border-color: var(--destructive); }
.total-card.active.income { border-color: var(--success); }
.total-label { color: var(--hint); font-size: 13px; font-weight: 500; }
.total-value { font-size: 22px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.total-card.expense .total-value { color: var(--destructive); }
.total-card.income .total-value { color: var(--success); }

.cats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}
.cat-tile {
  background: var(--secondary-bg);
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 12px;
  display: flex; align-items: center; gap: 10px;
  cursor: pointer;
  font-family: inherit;
  text-align: left;
  color: var(--text);
}
.cat-tile:active { transform: scale(0.99); }
.cat-tile .ico {
  width: 44px; height: 44px; border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  font-size: 22px;
  background: rgba(46, 163, 255, 0.15);
  flex-shrink: 0;
}
.cat-tile.expense .ico { background: rgba(255, 91, 91, 0.15); }
.cat-tile.income .ico { background: rgba(39, 194, 129, 0.15); }
.cat-tile .body { flex: 1; min-width: 0; }
.cat-tile .name {
  font-size: 14px; font-weight: 600;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.cat-tile .amt {
  font-size: 13px; font-weight: 600; margin-top: 2px;
  font-variant-numeric: tabular-nums;
}
.cat-tile.expense .amt { color: var(--destructive); }
.cat-tile.income .amt { color: var(--success); }
.cat-tile.subcat { margin-left: 24px; background: rgba(255,255,255,0.02); }
.cat-tile.subcat .ico { font-size: 18px; width: 36px; height: 36px; }

.emoji-picker {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 6px;
  background: var(--secondary-bg);
  padding: 10px;
  border-radius: var(--radius-sm);
  max-height: 180px; overflow-y: auto;
}
.emoji-picker button {
  background: transparent; border: 2px solid transparent;
  font-size: 22px; padding: 6px; border-radius: 8px;
  cursor: pointer; line-height: 1;
}
.emoji-picker button.active { border-color: var(--button); background: rgba(46,163,255,0.15); }

/* ---------- Boot gate (initData missing) ---------- */
.boot-gate {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  background: var(--bg);
  z-index: 100;
}
.boot-card {
  background: var(--secondary-bg);
  border-radius: 20px;
  padding: 28px 22px;
  max-width: 380px;
  width: 100%;
  text-align: center;
}
.boot-emoji { font-size: 48px; margin-bottom: 8px; }
.boot-title { font-size: 28px; font-weight: 700; margin-bottom: 12px; letter-spacing: 0.5px; }
.boot-msg { font-size: 15px; color: var(--hint); margin-bottom: 16px; }
.boot-help {
  text-align: left;
  font-size: 14px;
  color: var(--text);
  line-height: 1.6;
  background: var(--bg);
  padding: 14px 16px;
  border-radius: var(--radius-sm);
}
.boot-help a { color: var(--link); font-weight: 600; }
.boot-help details { margin-top: 12px; }
.boot-help summary { cursor: pointer; color: var(--hint); font-size: 12px; }
.boot-help pre { font-size: 11px; color: var(--hint); white-space: pre-wrap; word-break: break-all; margin-top: 6px; }

/* ---------- Toaster ---------- */
.toaster {
  position: fixed;
  left: 50%; bottom: calc(96px + var(--safe-bottom));
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 50;
  pointer-events: none;
}
.toast {
  background: var(--secondary-bg);
  border: 1px solid rgba(255,255,255,0.08);
  color: var(--text);
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  box-shadow: 0 6px 16px rgba(0,0,0,0.4);
  max-width: 90vw;
  pointer-events: auto;
  animation: toast-in 0.18s ease-out;
}
.toast.error { border-color: var(--destructive); color: var(--destructive); }
.toast.success { border-color: var(--success); color: var(--success); }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
