:root {
  --bg: #f1f5f9;
  --card: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --sidebar: #0f172a;
  --sidebar-ink: #cbd5e1;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --green: #059669;
  --red: #dc2626;
  --amber: #d97706;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 4px 14px rgba(15, 23, 42, .05);
}

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

html, body { height: 100%; }

body {
  font-family: 'Segoe UI', system-ui, -apple-system, Roboto, 'Helvetica Neue', sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.45;
}

.app { display: flex; min-height: 100vh; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--sidebar);
  color: var(--sidebar-ink);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand { display: flex; align-items: center; gap: 10px; padding: 20px 18px; border-bottom: 1px solid rgba(255,255,255,.08); }
.brand-icon { font-size: 26px; }
.brand-name { font-weight: 700; color: #fff; font-size: 15px; letter-spacing: .2px; }
.brand-sub { font-size: 11px; color: #7c8aa0; text-transform: uppercase; letter-spacing: .8px; }

.nav { padding: 14px 10px; display: flex; flex-direction: column; gap: 4px; flex: 1; }
.nav-btn {
  display: flex; align-items: center; gap: 10px;
  background: none; border: none; color: var(--sidebar-ink);
  font-size: 14px; padding: 10px 12px; border-radius: 9px;
  cursor: pointer; text-align: left; transition: background .15s, color .15s;
}
.nav-btn:hover { background: rgba(255,255,255,.07); color: #fff; }
.nav-btn.active { background: var(--primary); color: #fff; font-weight: 600; box-shadow: inset 3px 0 0 #93c5fd; }

.sidebar-foot { padding: 16px; border-top: 1px solid rgba(255,255,255,.08); }
.net-worth-box { background: rgba(255,255,255,.06); border-radius: 10px; padding: 12px; }
.net-label { font-size: 11px; text-transform: uppercase; letter-spacing: .8px; color: #7c8aa0; margin-bottom: 4px; }
.net-value { font-size: 20px; font-weight: 700; color: #fff; }
.clock { font-size: 11px; color: #7c8aa0; margin-top: 10px; }

/* ---------- Main ---------- */
.main { flex: 1; padding: 26px 30px 60px; max-width: 1280px; margin: 0 auto; width: 100%; min-width: 0; }

.view { display: none; }
.view:not(.active) { display: none !important; }
.view.active { display: block; animation: fadeIn .2s ease; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.view-head { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin-bottom: 20px; flex-wrap: wrap; }
.view-head h1 { font-size: 22px; font-weight: 700; }
.head-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

/* ---------- Cards ---------- */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px 20px; }
.card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; gap: 10px; flex-wrap: wrap; }
.card-head h2 { font-size: 15px; font-weight: 700; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; margin-bottom: 18px; }
.stack { display: flex; flex-direction: column; gap: 18px; }

/* ---------- Buttons & inputs ---------- */
.btn {
  border: none; border-radius: 9px; padding: 9px 16px;
  font-size: 13.5px; font-weight: 600; cursor: pointer;
  display: inline-flex; align-items: center; gap: 6px;
  transition: background .15s, transform .05s;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.ghost { background: #eef2f7; color: var(--ink); }
.btn.ghost:hover { background: #e2e8f0; }
.btn.danger { background: #fef2f2; color: var(--red); }
.btn.danger:hover { background: #fee2e2; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

.icon-btn { background: none; border: none; font-size: 15px; cursor: pointer; color: var(--muted); padding: 4px 8px; border-radius: 6px; }
.icon-btn:hover { background: #f1f5f9; color: var(--ink); }

.input {
  border: 1.5px solid var(--line); border-radius: 9px;
  padding: 8px 12px; font-size: 13.5px; font-family: inherit;
  background: #fff; color: var(--ink); outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37, 99, 235, .12); }
.select-sm { padding: 5px 9px; font-size: 12.5px; }
.color-input { padding: 2px; width: 46px; height: 36px; cursor: pointer; }

/* ---------- Table ---------- */
.filters { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; align-items: center; }
.table-scroll { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.table th {
  text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .6px;
  color: var(--muted); padding: 8px 10px; border-bottom: 2px solid var(--line);
  white-space: nowrap;
}
.table td { padding: 9px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.table tbody tr:hover { background: #f8fafc; }
.ta-r { text-align: right !important; }
.amt-in { color: var(--green); font-weight: 700; white-space: nowrap; }
.amt-out { color: var(--red); font-weight: 700; white-space: nowrap; }
.bal-cell { color: var(--muted); font-size: 12.5px; white-space: nowrap; }
.table-foot { display: flex; justify-content: space-between; gap: 10px; padding: 12px 4px 2px; font-size: 13px; color: var(--muted); flex-wrap: wrap; }

/* ---------- Month group rows ---------- */
.month-group td {
  background: #f1f5f9; cursor: pointer; user-select: none;
  padding: 7px 10px; border-top: 2px solid var(--line);
}
.month-group:hover td { background: #e8eef5; }
.mg-toggle { font-weight: 700; font-size: 12.5px; color: var(--ink); }
.mg-count { font-weight: 400; font-size: 12px; color: var(--muted); margin-left: 6px; }
.mg-sum { float: right; font-weight: 400; font-size: 12px; }
.year-group td { background: #e2e8f0; border-top: 2px solid #cbd5e1; }
.year-group:hover td { background: #d8e0ea; }
.year-group .mg-toggle { font-size: 13.5px; letter-spacing: .5px; }

/* ---------- Accounts grid ---------- */
.acc-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; }
.acc-card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; cursor: pointer; border-top: 4px solid var(--acc-color, var(--primary)); transition: transform .12s, box-shadow .12s; }
.acc-card:hover { transform: translateY(-2px); box-shadow: 0 6px 20px rgba(15, 23, 42, .12); }
.acc-card.selected { outline: 2.5px solid var(--acc-color, var(--primary)); outline-offset: -2.5px; box-shadow: 0 6px 20px rgba(15, 23, 42, .16); }
.acc-edit-btn:hover { background: #eef2f7; border-radius: 7px; }
.acc-top { display: flex; justify-content: space-between; align-items: flex-start; }
.acc-emoji { font-size: 30px; }
.acc-name { font-weight: 700; font-size: 16px; margin-top: 8px; }
.acc-bal { font-size: 22px; font-weight: 700; margin-top: 2px; }
.acc-note { font-size: 12px; color: var(--muted); margin-top: 6px; min-height: 16px; }
.acc-stats { display: flex; gap: 14px; margin-top: 12px; font-size: 12px; color: var(--muted); }
.acc-stats b { color: var(--ink); }

/* ---------- Statement panel (inside accounts view) ---------- */
.statement-panel { margin-top: 18px; }
.statement-controls { margin-bottom: 12px; }
.statement-controls .head-actions { justify-content: flex-end; }
.statement-controls .input { min-width: 150px; }
.statement { margin-bottom: 14px; }
.statement-below { display: flex; flex-direction: column; gap: 14px; }
.statement-below .sum-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.statement-below .chart-wrap { height: 240px; }

.statement {
  background: #0f172a; border-radius: 12px; padding: 16px 18px;
  max-height: 480px; overflow-y: auto; color: #e2e8f0;
}
.st-titlebar {
  display: flex; justify-content: space-between; align-items: baseline; gap: 10px;
  padding-bottom: 10px; margin-bottom: 10px; border-bottom: 1px solid #334155;
}
.st-acc { font-weight: 800; font-size: 15px; color: #fff; }
.st-month { font-size: 12px; color: #94a3b8; text-transform: uppercase; letter-spacing: .8px; }

.st-row {
  display: flex; align-items: baseline; gap: 8px;
  font-variant-numeric: tabular-nums;
  font-family: 'Cascadia Code', 'Consolas', 'SF Mono', Menlo, monospace;
  font-size: 13px; line-height: 1.5;
}
.st-opening, .st-closing { font-weight: 700; }
.st-opening { color: #fbbf24; padding: 2px 0 8px; border-bottom: 1px dashed #334155; margin-bottom: 4px; }
.st-closing { color: #fbbf24; border-top: 1px dashed #334155; margin-top: 6px; padding-top: 10px; }
.st-row .st-label { color: #cbd5e1; font-family: 'Segoe UI', system-ui, sans-serif; font-size: 12.5px; }
.st-opening .st-amt, .st-closing .st-amt { margin-left: auto; font-size: 14.5px; }

.st-rows { display: flex; flex-direction: column; gap: 1px; }
.st-entry .st-sign { width: 14px; text-align: center; flex-shrink: 0; font-weight: 700; }
.st-entry.st-in .st-sign { color: #34d399; }
.st-entry.st-out .st-sign { color: #f87171; }
.st-entry .st-amt { width: 84px; text-align: right; flex-shrink: 0; }
.st-entry.st-in .st-amt { color: #34d399; font-weight: 600; }
.st-entry.st-out .st-amt { color: #f87171; font-weight: 600; }
.st-entry .st-desc {
  flex: 1; min-width: 0; color: #e2e8f0;
  font-family: 'Segoe UI', system-ui, sans-serif; font-size: 12.5px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.st-entry .st-desc i { color: #64748b; }
.st-entry .st-date { color: #64748b; font-size: 11.5px; flex-shrink: 0; }
.st-empty { color: #64748b; font-size: 12.5px; padding: 14px 0; text-align: center; font-family: 'Segoe UI', system-ui, sans-serif; }

.statement::-webkit-scrollbar { width: 8px; }
.statement::-webkit-scrollbar-thumb { background: #334155; border-radius: 4px; }

.sum-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.sum-box { background: #f8fafc; border-radius: 10px; padding: 12px 14px; }
.sum-box .s-label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.sum-box .s-value { font-size: 17px; font-weight: 700; margin-top: 2px; }

/* ---------- Charts ---------- */
.chart-wrap { position: relative; height: 280px; }

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(15, 23, 42, .55);
  display: none; align-items: center; justify-content: center; z-index: 50; padding: 20px;
}
.modal-overlay.open { display: flex; animation: fadeIn .15s ease; }
.modal { background: #fff; border-radius: 16px; width: 560px; max-width: 100%; max-height: 92vh; overflow-y: auto; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal-sm { width: 460px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 22px; border-bottom: 1px solid var(--line); }
.modal-head h2 { font-size: 16px; }
.modal-body { padding: 20px 22px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field > span { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.span-2 { grid-column: span 2; }

.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 0; background: #eef2f7; border-radius: 10px; padding: 4px; margin-bottom: 16px; }
.seg-btn { border: none; background: none; padding: 9px; font-size: 13.5px; font-weight: 600; color: var(--muted); border-radius: 8px; cursor: pointer; transition: all .15s; }
.seg-btn.active[data-dir="out"] { background: var(--red); color: #fff; }
.seg-btn.active[data-dir="in"] { background: var(--green); color: #fff; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(80px);
  background: var(--ink); color: #fff; padding: 11px 22px; border-radius: 10px;
  font-size: 13.5px; font-weight: 600; box-shadow: 0 8px 30px rgba(0,0,0,.25);
  opacity: 0; transition: all .25s ease; z-index: 100; max-width: 90vw;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { background: var(--red); }

/* ---------- Login ---------- */
.login-wrap {
  position: fixed; inset: 0; z-index: 200;
  background: linear-gradient(135deg, #0f172a 0%, #1e293b 55%, #0b1329 100%);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.login-card {
  background: #fff; border-radius: 18px; padding: 34px 34px 28px;
  width: 360px; max-width: 100%; box-shadow: 0 24px 80px rgba(0,0,0,.4);
  text-align: center; animation: fadeIn .25s ease;
}
.login-logo { font-size: 44px; }
.login-card h1 { font-size: 21px; margin-top: 6px; }
.login-sub { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 1px; margin: 4px 0 22px; }
.login-card form { text-align: left; }
.login-btn { width: 100%; justify-content: center; margin-top: 18px; padding: 11px; font-size: 14.5px; }
.login-err { color: var(--red); font-size: 13px; margin-top: 12px; min-height: 16px; text-align: center; }
.logout-btn { width: 100%; justify-content: center; margin-top: 10px; font-size: 12.5px; padding: 7px; }

/* ---------- Misc ---------- */
.muted { color: var(--muted); font-size: 13px; }
.empty { text-align: center; color: var(--muted); padding: 30px 10px; font-size: 13.5px; }
.card > p { margin-bottom: 12px; }

/* ---------- Mobile topbar & tabbar (hidden on desktop) ---------- */
.mobile-topbar { display: none; }
.tabbar { display: none; }

@media (max-width: 768px) {
  /* layout */
  .app { display: block; min-height: 100vh; }
  .main { padding: 14px 12px calc(86px + env(safe-area-inset-bottom, 0px)); }

  /* topbar */
  .mobile-topbar {
    display: flex; align-items: center; gap: 10px;
    background: var(--sidebar); color: #fff;
    padding: 10px 14px; padding-top: calc(10px + env(safe-area-inset-top, 0px));
    position: sticky; top: 0; z-index: 40;
  }
  .mt-center { flex: 1; min-width: 0; }
  .mt-title { font-size: 14px; font-weight: 700; }
  .mt-balance { font-size: 17px; font-weight: 800; }
  .mt-logout { color: #cbd5e1; font-size: 20px; padding: 6px 10px; }

  /* hide desktop sidebar, show tabbar */
  .sidebar { display: none; }
  .tabbar {
    display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
    background: #fff; border-top: 1px solid var(--line);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    box-shadow: 0 -4px 20px rgba(15, 23, 42, .08);
  }
  .tab-btn {
    flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
    background: none; border: none; padding: 8px 4px 7px; position: relative;
    font-size: 21px; line-height: 1; cursor: pointer;
  }
  .tab-btn span { font-size: 10.5px; font-weight: 600; color: var(--muted); letter-spacing: .3px; }
  .tab-btn.active span { color: var(--primary); font-weight: 700; }
  .tab-btn.active::before {
    content: ''; position: absolute; top: 0; left: 18%; right: 18%; height: 3px;
    background: var(--primary); border-radius: 0 0 4px 4px;
  }

  /* view heads */
  .view-head { margin-bottom: 14px; }
  .view-head h1 { font-size: 19px; }
  .head-actions { width: 100%; }
  .head-actions .btn { flex: 1; justify-content: center; }

  /* cards & grids */
  .card { padding: 14px; border-radius: 12px; }
  .grid-2 { gap: 12px; margin-bottom: 12px; }
  .acc-grid { grid-template-columns: 1fr; gap: 12px; }
  .acc-card { padding: 14px; }

  /* filters become a tight 2-col grid */
  .filters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 10px; }
  .filters .input, .filters .btn { width: 100%; }
  .filters #fSearch { grid-column: span 2; }
  .filters #fClear { grid-column: span 2; }

  /* table: smaller text, scroll */
  .table-scroll { overflow-x: auto; margin: 0 -14px; padding: 0 14px; }
  .table { font-size: 12.5px; }
  .table th, .table td { padding: 7px 8px; }

  /* modals become bottom sheets */
  .modal-overlay { padding: 0; align-items: flex-end; }
  .modal {
    width: 100%; max-width: 100%; border-radius: 18px 18px 0 0;
    max-height: 92vh; padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  .modal-head { padding: 14px 16px; position: sticky; top: 0; background: #fff; z-index: 2; }
  .modal-body { padding: 16px; }
  .modal-foot { margin-top: 16px; }
  .modal-foot .btn { flex: 1; justify-content: center; padding: 12px; }
  .modal-foot #accDeleteBtn { flex: 0 1 auto; }

  /* forms single column */
  .form-grid { grid-template-columns: 1fr; gap: 12px; }
  .span-2 { grid-column: span 1; }
  .input { font-size: 16px; padding: 10px 12px; } /* 16px stops iOS zoom-on-focus */

  /* statement panel */
  .statement-panel { overflow: hidden; }
  .statement-controls { align-items: stretch; gap: 10px; }
  .statement-controls h2 { width: 100%; }
  .statement-controls .head-actions {
    display: grid; grid-template-columns: repeat(2, minmax(0, 1fr));
    width: 100%; gap: 8px;
  }
  .statement-controls .input {
    width: 100%; min-width: 0; max-width: 100%;
  }
  .statement-controls .btn {
    grid-column: 1 / -1; width: 100%; min-width: 0;
  }
  .statement {
    padding: 12px; max-height: none; overflow-y: visible;
    border-radius: 10px;
  }
  .st-titlebar { align-items: center; }
  .st-row { align-items: flex-start; gap: 6px; }
  .st-entry .st-date { display: none; } /* date hidden on phones, note gets room */
  .st-entry .st-amt { width: 68px; font-size: 12px; }
  .st-entry .st-sign { width: 12px; }
  .st-entry .st-desc {
    white-space: normal; overflow: visible; text-overflow: clip;
    overflow-wrap: anywhere; line-height: 1.35;
  }
  .st-acc { font-size: 14px; }
  .statement-below .chart-wrap { height: 180px; }
  .statement-below .sum-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px;
  }
  .statement-below .sum-box:last-child { grid-column: 1 / -1; }
  .sum-box { padding: 10px 12px; }
  .sum-box .s-value { font-size: 15px; overflow-wrap: anywhere; }

  /* toast above tabbar */
  .toast { bottom: calc(90px + env(safe-area-inset-bottom, 0px)); font-size: 12.5px; padding: 10px 18px; }

  .mg-sum { float: none; display: block; margin-top: 2px; }
  .month-group td, .year-group td { padding: 9px 10px; }
}
