:root {
  --teal: #0d9488;
  --teal-dark: #0f766e;
  --teal-light: #ccfbf1;
  --bg: #f1f5f9;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --income: #059669;
  --expense: #dc2626;
  --danger: #dc2626;
  --radius: 16px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08), 0 4px 12px rgba(15, 23, 42, 0.05);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
  padding-bottom: 92px;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.center { text-align: center; }

/* ---------- Header ---------- */
.app-header {
  background: linear-gradient(135deg, var(--teal-dark), var(--teal));
  color: #fff;
  padding: 18px 18px 14px;
  border-radius: 0 0 22px 22px;
  box-shadow: var(--shadow);
}
.header-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.app-header h1 { font-size: 22px; font-weight: 700; letter-spacing: -0.3px; }
.header-sub { font-size: 12.5px; opacity: 0.85; margin-top: 2px; }

.period-nav { display: flex; align-items: center; gap: 8px; margin-top: 14px; }
.nav-arrow {
  width: 36px; height: 36px; border-radius: 50%;
  border: none; background: rgba(255,255,255,0.18); color: #fff;
  font-size: 22px; line-height: 1; cursor: pointer; flex-shrink: 0;
}
.nav-arrow:active { background: rgba(255,255,255,0.32); }
.period-label-wrap { flex: 1; text-align: center; min-width: 0; }
#periodLabel { display: block; font-size: 17px; font-weight: 700; }
.period-sub { display: block; font-size: 11.5px; opacity: 0.85; }

/* ---------- Buttons ---------- */
.btn {
  border: 1px solid var(--border); background: #fff; color: var(--text);
  border-radius: 12px; padding: 10px 14px; font-size: 14px; font-weight: 600;
  cursor: pointer;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--teal); border-color: var(--teal); color: #fff; }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-light { background: rgba(255,255,255,0.16); border-color: rgba(255,255,255,0.35); color: #fff; }
.btn-small { padding: 7px 12px; font-size: 13px; border-radius: 10px; background: rgba(255,255,255,0.16); border-color: rgba(255,255,255,0.35); color: #fff; }
.link-btn { background: none; border: none; color: var(--teal); font-size: 13.5px; font-weight: 600; cursor: pointer; }
.btn-row { display: flex; gap: 10px; margin-top: 12px; flex-wrap: wrap; }
.btn-row.end { justify-content: flex-end; }
.btn-row .btn { flex: 1; }
.btn-row.end .btn { flex: 0 1 auto; }

/* ---------- Layout ---------- */
main { padding: 14px 14px 0; max-width: 640px; margin: 0 auto; }
.view { display: none; }
.view.active { display: block; animation: fadeIn 0.18s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.card {
  background: var(--card); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; margin-bottom: 14px;
}
.card h2 { font-size: 16px; font-weight: 700; margin-bottom: 10px; }
.card-title { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; }
.card-title h2 { margin-bottom: 0; }
.card.danger { border: 1px solid #fecaca; }
.pill {
  font-size: 12px; font-weight: 700; padding: 4px 10px; border-radius: 999px;
  background: var(--teal-light); color: var(--teal-dark); white-space: nowrap;
}
.pill.over { background: #fee2e2; color: var(--expense); }

/* ---------- Balance card ---------- */
.balance-card { background: linear-gradient(135deg, var(--teal-dark), var(--teal)); color: #fff; }
.bal-row { display: flex; justify-content: space-between; align-items: baseline; }
.bal-row span { font-size: 13px; opacity: 0.85; }
.bal-row strong { font-size: 26px; font-weight: 800; letter-spacing: -0.5px; }
.bal-grid { display: flex; gap: 12px; margin-top: 14px; }
.bal-item { flex: 1; background: rgba(255,255,255,0.14); border-radius: 12px; padding: 10px 12px; }
.bal-item span { display: block; font-size: 12px; opacity: 0.85; }
.bal-item strong { font-size: 16px; }

/* ---------- Progress ---------- */
.progress { height: 10px; background: #e2e8f0; border-radius: 999px; overflow: hidden; margin: 8px 0; }
.progress-fill { height: 100%; background: var(--teal); border-radius: 999px; transition: width 0.3s ease; }
.progress-fill.over { background: var(--expense); }
.progress-fill.warn { background: #f59e0b; }

/* ---------- Donut ---------- */
.donut-wrap { position: relative; width: 220px; height: 220px; margin: 4px auto; }
.donut-wrap canvas { width: 220px; height: 220px; }
.donut-center {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; pointer-events: none; text-align: center;
}
.donut-center span { font-size: 12px; color: var(--muted); }
.donut-center strong { font-size: 17px; }
.legend { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.legend-row { display: flex; align-items: center; gap: 8px; font-size: 13.5px; }
.legend-dot { width: 12px; height: 12px; border-radius: 4px; flex-shrink: 0; }
.legend-row .amt { margin-left: auto; font-weight: 700; white-space: nowrap; }
.legend-row .pct { color: var(--muted); font-size: 12px; width: 42px; text-align: right; }

/* ---------- Transactions ---------- */
.tx-group-title { font-size: 12.5px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: 0.4px; margin: 14px 0 6px; }
.tx-row {
  display: flex; align-items: center; gap: 12px; width: 100%;
  background: none; border: none; border-bottom: 1px solid var(--border);
  padding: 11px 2px; cursor: pointer; text-align: left; font: inherit; color: inherit;
}
.tx-row:last-child { border-bottom: none; }
.tx-dot { width: 42px; height: 42px; border-radius: 12px; flex-shrink: 0; display: flex; align-items: center; justify-content: center; font-size: 19px; }
.tx-info { flex: 1; min-width: 0; }
.tx-name { font-weight: 600; font-size: 14.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tx-note { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tx-amount { font-weight: 700; font-size: 14.5px; white-space: nowrap; }
.tx-amount.income { color: var(--income); }
.tx-amount.expense { color: var(--expense); }
.tx-date { font-size: 11.5px; color: var(--muted); text-align: right; }

.filters { display: flex; gap: 8px; margin-bottom: 12px; }
.chip {
  border: 1px solid var(--border); background: #fff; color: var(--text);
  border-radius: 999px; padding: 8px 16px; font-size: 13.5px; font-weight: 600; cursor: pointer;
}
.chip.active { background: var(--teal); border-color: var(--teal); color: #fff; }

/* ---------- Forms ---------- */
.field { display: block; font-size: 13.5px; font-weight: 600; margin-bottom: 12px; }
.field input, .field select, input[type="number"], input[type="text"], input[type="date"], select {
  display: block; width: 100%; margin-top: 6px; padding: 11px 12px;
  border: 1px solid var(--border); border-radius: 12px; font-size: 15px;
  background: #fff; color: var(--text); font-family: inherit;
}
.field input:focus, input:focus, select:focus { outline: 2px solid var(--teal-light); border-color: var(--teal); }
.input-row { display: flex; gap: 10px; align-items: flex-end; }
.input-row input, .input-row select { flex: 1; }
.input-row .btn { flex-shrink: 0; }
.radio { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 14.5px; margin: 10px 0; cursor: pointer; }
.radio input { width: 18px; height: 18px; accent-color: var(--teal); }

.type-toggle { display: flex; gap: 8px; margin-bottom: 14px; }
.type-btn {
  flex: 1; padding: 11px; border-radius: 12px; border: 1px solid var(--border);
  background: #fff; font-size: 14.5px; font-weight: 700; cursor: pointer; color: var(--muted);
}
.type-btn.active[data-type="expense"], #typeExpense.active { background: #fee2e2; border-color: var(--expense); color: var(--expense); }
.type-btn.active[data-type="income"], #typeIncome.active { background: #d1fae5; border-color: var(--income); color: var(--income); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 4px 0 12px; }
.cat-chip {
  border: 1.5px solid var(--border); background: #fff; border-radius: 999px;
  padding: 7px 12px; font-size: 13px; font-weight: 600; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px;
}
.cat-chip .dot { width: 10px; height: 10px; border-radius: 50%; }
.cat-chip.active { border-color: var(--teal); background: var(--teal-light); }

.color-swatches { display: flex; gap: 8px; margin: 8px 0 12px; flex-wrap: wrap; }
.swatch { width: 30px; height: 30px; border-radius: 50%; border: 2px solid transparent; cursor: pointer; }
.swatch.active { border-color: var(--text); transform: scale(1.12); }
.cat-list { margin-top: 12px; display: flex; flex-direction: column; gap: 6px; }
.cat-list-row { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.cat-list-row .dot { width: 12px; height: 12px; border-radius: 50%; }
.cat-list-row .del { margin-left: auto; background: none; border: none; color: var(--danger); font-weight: 700; cursor: pointer; font-size: 13px; }

.backup-status { font-weight: 700; font-size: 14px; }
.backup-status.ok { color: var(--income); }
.backup-status.fallback, .backup-status.failed { color: var(--expense); }

/* ---------- Bottom nav + FAB ---------- */
.bottom-nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
  display: flex; background: #fff; border-top: 1px solid var(--border);
  padding: 6px 4px calc(8px + env(safe-area-inset-bottom));
}
.nav-item {
  flex: 1; background: none; border: none; display: flex; flex-direction: column;
  align-items: center; gap: 2px; font-size: 11px; font-weight: 600; color: var(--muted);
  cursor: pointer; padding: 4px 0;
}
.nav-item svg { width: 23px; height: 23px; fill: currentColor; }
.nav-item.active { color: var(--teal); }

.fab {
  position: fixed; right: 18px; bottom: 86px; z-index: 41;
  width: 58px; height: 58px; border-radius: 50%; border: none;
  background: var(--teal); color: #fff; font-size: 32px; line-height: 1;
  box-shadow: 0 4px 14px rgba(13, 148, 136, 0.45); cursor: pointer;
}
.fab:active { transform: scale(0.94); }

/* ---------- Sheet / overlay / toast ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(15, 23, 42, 0.45); z-index: 50;
}
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 51;
  background: #fff; border-radius: 22px 22px 0 0; padding: 20px 18px calc(20px + env(safe-area-inset-bottom));
  max-width: 640px; margin: 0 auto; max-height: 88vh; overflow-y: auto;
  box-shadow: 0 -4px 24px rgba(15, 23, 42, 0.18);
}
.sheet h2 { font-size: 18px; margin-bottom: 14px; }
.confirm {
  position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%); z-index: 52;
  background: #fff; border-radius: 18px; padding: 20px; width: calc(100% - 48px); max-width: 420px;
  box-shadow: 0 8px 32px rgba(15, 23, 42, 0.25);
}
.confirm h2 { font-size: 17px; margin-bottom: 8px; }
.confirm p { color: var(--muted); font-size: 14px; margin-bottom: 6px; }
.toast {
  position: fixed; left: 50%; bottom: 100px; transform: translateX(-50%); z-index: 60;
  background: var(--text); color: #fff; padding: 11px 18px; border-radius: 999px;
  font-size: 13.5px; font-weight: 600; box-shadow: 0 4px 16px rgba(0,0,0,0.25);
  max-width: calc(100% - 32px); text-align: center;
}

/* ---------- Animated opening screen ---------- */
.splash {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, #0f766e 0%, #0d9488 55%, #14b8a6 100%);
  transition: opacity .45s ease, visibility .45s ease;
  /* Safety net: even if JS never runs, the splash fades away on its own. */
  animation: splashAutoHide .5s ease 2.4s forwards;
}
.splash.hide {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}
.splash-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0 24px;
  text-align: center;
}
.splash-logo-wrap {
  position: relative;
  overflow: hidden;
  border-radius: 36px;
  box-shadow: 0 18px 40px rgba(0, 0, 0, .35);
  animation: splashPop .7s cubic-bezier(.2, 1.4, .4, 1) both;
}
.splash-logo-wrap::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  width: 45%;
  background: linear-gradient(105deg, transparent, rgba(255, 255, 255, .35), transparent);
  transform: translateX(-160%) skewX(-12deg);
  animation: splashShine .8s ease .55s both;
}
.splash-logo {
  display: block;
  width: 148px;
  height: 148px;
}
.splash-title {
  margin-top: 22px;
  color: #fff;
  font-size: 30px;
  font-weight: 800;
  letter-spacing: .2px;
  animation: splashRise .5s ease .35s both;
}
.splash-tag {
  margin-top: 6px;
  color: rgba(255, 255, 255, .85);
  font-size: 15px;
  animation: splashRise .5s ease .55s both;
}
@keyframes splashPop {
  0% { transform: scale(.5) rotate(-6deg); opacity: 0; }
  60% { transform: scale(1.07) rotate(1.5deg); opacity: 1; }
  100% { transform: scale(1) rotate(0deg); opacity: 1; }
}
@keyframes splashShine {
  to { transform: translateX(320%) skewX(-12deg); }
}
@keyframes splashRise {
  from { transform: translateY(12px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}
@keyframes splashAutoHide {
  to { opacity: 0; visibility: hidden; }
}
@media (prefers-reduced-motion: reduce) {
  .splash-logo-wrap, .splash-title, .splash-tag { animation: none; }
  .splash { animation: splashAutoHide .3s ease 1s forwards; }
}
