/* ============================================================
   Charlie App — Mobile-first CSS
   Dark navy theme · Inter font
   ============================================================ */

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

:root {
  --navy:     #0F2027;
  --navy2:    #1E3A5F;
  --navy3:    #2C5364;
  --blue:     #3B82F6;
  --teal:     #10B981;
  --amber:    #F59E0B;
  --red:      #EF4444;
  --purple:   #8B5CF6;
  --text:     #1A2634;
  --text-2:   #64748B;
  --text-3:   #94A3B8;
  --border:   #E2E8F0;
  --bg:       #F8F9FB;
  --white:    #FFFFFF;
  --card:     #FFFFFF;
  --radius:   14px;
  --radius-sm:8px;
  --shadow:   0 2px 12px rgba(0,0,0,0.06);
  --shadow-lg:0 8px 30px rgba(0,0,0,0.12);
  --nav-h:    64px;
  --safe-t:   env(safe-area-inset-top, 0px);
  --safe-b:   env(safe-area-inset-bottom, 0px);
}

html { height: 100%; -webkit-tap-highlight-color: transparent; }
body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100%;
  overscroll-behavior: none;
  padding-top: var(--safe-t);
}

/* ── Typography ─────────────────────────────────────────────── */
h1 { font-size: 1.5rem; font-weight: 800; letter-spacing: -0.02em; }
h2 { font-size: 1.1rem; font-weight: 700; }
h3 { font-size: 0.95rem; font-weight: 700; }
p  { font-size: 0.9rem; line-height: 1.5; color: var(--text-2); }

/* ── Layout ─────────────────────────────────────────────────── */
#app { display: flex; flex-direction: column; min-height: 100vh; }

.page {
  flex: 1;
  padding: 1rem 1rem calc(var(--nav-h) + var(--safe-b) + 1rem);
  max-width: 768px;
  margin: 0 auto;
  width: 100%;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.2rem;
  padding-top: 0.5rem;
}

/* ── Cards ──────────────────────────────────────────────────── */
.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.1rem 1.2rem;
  margin-bottom: 1rem;
}

.card-sm { padding: 0.8rem 1rem; border-radius: var(--radius-sm); }

/* ── Buttons ────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.55rem 1.1rem;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  font-weight: 600;
  border: none;
  cursor: pointer;
  transition: opacity 0.15s, transform 0.1s;
  text-decoration: none;
  white-space: nowrap;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.5; pointer-events: none; }

.btn-primary   { background: var(--navy2); color: #fff; }
.btn-teal      { background: var(--teal);  color: #fff; }
.btn-amber     { background: var(--amber); color: #fff; }
.btn-red       { background: var(--red);   color: #fff; }
.btn-outline   { background: transparent; color: var(--navy2); border: 1.5px solid var(--navy2); }
.btn-ghost     { background: var(--bg); color: var(--text); border: 1px solid var(--border); }
.btn-icon      { padding: 0.5rem; border-radius: 50%; background: var(--bg); border: 1px solid var(--border); }

.btn-lg { padding: 0.75rem 1.5rem; font-size: 1rem; border-radius: 12px; }
.btn-full { width: 100%; justify-content: center; }

/* ── Floating Action Button ─────────────────────────────────── */
.fab {
  position: fixed;
  bottom: calc(var(--nav-h) + var(--safe-b) + 1rem);
  right: 1.2rem;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--navy2);
  color: #fff;
  font-size: 1.6rem;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-lg);
  border: none;
  cursor: pointer;
  z-index: 200;
  transition: transform 0.2s;
}
.fab:active { transform: scale(0.92); }

/* ── Bottom Navigation ──────────────────────────────────────── */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0; right: 0;
  height: calc(var(--nav-h) + var(--safe-b));
  background: var(--white);
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: space-around;
  align-items: flex-start;
  padding: 0.5rem 0 var(--safe-b);
  z-index: 100;
}

.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 0.3rem 0;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-3);
  transition: color 0.15s;
  font-size: 0.65rem;
  font-weight: 600;
  text-align: center;
}
.nav-item .nav-icon { font-size: 1.3rem; }
.nav-item.active { color: var(--navy2); }

/* ── Context toggle (Personal ↔ Business) ───────────────────── */
.context-toggle {
  display: flex;
  gap: 0.5rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 50px;
  padding: 3px;
  margin-bottom: 1rem;
}
.context-btn {
  flex: 1;
  padding: 0.35rem 0.8rem;
  border-radius: 50px;
  font-size: 0.78rem;
  font-weight: 700;
  border: none;
  cursor: pointer;
  background: transparent;
  color: var(--text-2);
  transition: all 0.2s;
}
.context-btn.active {
  background: var(--navy2);
  color: #fff;
}

/* ── KPI Cards ──────────────────────────────────────────────── */
.kpi-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; margin-bottom: 1rem; }
.kpi-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
}
.kpi-label { font-size: 0.72rem; color: var(--text-2); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 0.3rem; }
.kpi-value { font-size: 1.3rem; font-weight: 800; color: var(--text); letter-spacing: -0.02em; }
.kpi-value.income { color: var(--teal); }
.kpi-value.expense { color: var(--red); }
.kpi-value.warning { color: var(--amber); }
.kpi-sub { font-size: 0.72rem; color: var(--text-3); margin-top: 2px; }

/* ── Transaction List Items ─────────────────────────────────── */
.txn-list { list-style: none; }
.txn-item {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--bg);
  background: var(--card);
  cursor: pointer;
  transition: background 0.1s;
}
.txn-item:first-child { border-radius: var(--radius) var(--radius) 0 0; }
.txn-item:last-child  { border-radius: 0 0 var(--radius) var(--radius); border-bottom: none; }
.txn-item:only-child  { border-radius: var(--radius); }
.txn-item:active      { background: var(--bg); }

.txn-icon {
  width: 40px; height: 40px;
  border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.1rem;
  flex-shrink: 0;
}
.txn-body { flex: 1; min-width: 0; }
.txn-name { font-size: 0.88rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.txn-meta { font-size: 0.74rem; color: var(--text-2); margin-top: 1px; }
.txn-amount { font-size: 0.92rem; font-weight: 700; white-space: nowrap; }
.txn-amount.income  { color: var(--teal); }
.txn-amount.expense { color: var(--red); }
.txn-amount.transfer{ color: var(--blue); }

/* ── Forms ──────────────────────────────────────────────────── */
.form-group { margin-bottom: 1rem; }
.form-label { display: block; font-size: 0.8rem; font-weight: 600; color: var(--text-2); margin-bottom: 0.35rem; }
.form-input, .form-select, .form-textarea {
  width: 100%;
  padding: 0.6rem 0.8rem;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
  font-family: inherit;
  color: var(--text);
  background: var(--white);
  transition: border-color 0.15s;
  -webkit-appearance: none;
}
.form-input:focus, .form-select:focus, .form-textarea:focus {
  outline: none;
  border-color: var(--blue);
}
.form-input.error { border-color: var(--red); }
.form-error { font-size: 0.75rem; color: var(--red); margin-top: 0.25rem; }
.form-textarea { resize: vertical; min-height: 80px; }
.form-row { display: flex; gap: 0.75rem; }
.form-row .form-group { flex: 1; }

/* ── Status Badges ──────────────────────────────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.15rem 0.55rem;
  border-radius: 50px;
  font-size: 0.68rem;
  font-weight: 700;
}
.badge-paid     { background: #D1FAE5; color: #065F46; }
.badge-partial  { background: #FEF3C7; color: #92400E; }
.badge-overdue  { background: #FEE2E2; color: #B91C1C; }
.badge-draft    { background: #F1F5F9; color: #475569; }
.badge-sent     { background: #DBEAFE; color: #1D4ED8; }
.badge-void     { background: #F1F5F9; color: #94A3B8; }
.badge-vip      { background: #FEF3C7; color: #92400E; }
.badge-new      { background: #D1FAE5; color: #065F46; }
.badge-regular  { background: #DBEAFE; color: #1D4ED8; }

/* ── Modals & Sheets ────────────────────────────────────────── */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex; align-items: flex-end; justify-content: center;
  z-index: 500;
  opacity: 0; pointer-events: none;
  transition: opacity 0.2s;
}
.modal-overlay.open { opacity: 1; pointer-events: all; }

.sheet {
  background: var(--white);
  border-radius: 20px 20px 0 0;
  padding: 0.8rem 1.2rem 1.5rem;
  width: 100%;
  max-width: 640px;
  max-height: 92vh;
  overflow-y: auto;
  transform: translateY(100%);
  transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}
.modal-overlay.open .sheet { transform: translateY(0); }

.sheet-handle {
  width: 40px; height: 4px;
  border-radius: 2px;
  background: var(--border);
  margin: 0 auto 1rem;
}
.sheet-title { font-size: 1rem; font-weight: 700; margin-bottom: 1.2rem; }

/* ── Offline Banner ─────────────────────────────────────────── */
.offline-banner {
  position: fixed; top: var(--safe-t); left: 0; right: 0;
  background: var(--amber);
  color: #fff;
  text-align: center;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0.4rem 1rem;
  z-index: 1000;
  transform: translateY(-100%);
  transition: transform 0.3s;
}
.offline-banner.show { transform: translateY(0); }

/* ── Empty state ─────────────────────────────────────────────── */
.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--text-3);
}
.empty-state .empty-icon { font-size: 3rem; margin-bottom: 0.8rem; }
.empty-state h3 { font-size: 1rem; color: var(--text-2); margin-bottom: 0.4rem; }
.empty-state p  { font-size: 0.85rem; }

/* ── Loading spinner ────────────────────────────────────────── */
.spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--navy2);
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
  margin: 2rem auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Skeleton loaders ── */
.skeleton {
  background: linear-gradient(90deg, var(--border) 25%, color-mix(in srgb, var(--border) 40%, var(--card)) 50%, var(--border) 75%);
  background-size: 200% 100%;
  animation: skel-shimmer 1.4s linear infinite;
  border-radius: 8px;
}
.skeleton-line { height: 14px; margin-bottom: 10px; }
.skeleton-line.tall { height: 22px; }
.skeleton-line.sm { height: 10px; }
.skeleton-line.w25 { width: 25%; } .skeleton-line.w40 { width: 40%; }
.skeleton-line.w60 { width: 60%; } .skeleton-line.w80 { width: 80%; }
.skeleton-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1rem;
  margin-bottom: 0.75rem;
}
.skeleton-row { display: flex; align-items: center; gap: 0.75rem; padding: 0.75rem 0; border-bottom: 1px solid var(--border); }
.skeleton-row .skel-dot { width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0; }
.skeleton-row .skel-text { flex: 1; }
@keyframes skel-shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

/* ── Focus-visible (WCAG 2.4.11) ── */
:focus-visible {
  outline: 2px solid var(--teal);
  outline-offset: 2px;
  border-radius: 4px;
}
:focus:not(:focus-visible) { outline: none; }

/* ── Section headers ─────────────────────────────────────────── */
.section-title {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text-2);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin: 1.2rem 0 0.6rem;
  padding: 0 0.2rem;
}

/* ── Progress bar ────────────────────────────────────────────── */
.progress-bar { height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; }
.progress-fill { height: 100%; border-radius: 3px; background: var(--teal); transition: width 0.5s; }
.progress-fill.warning { background: var(--amber); }
.progress-fill.danger  { background: var(--red); }

/* ── Accent color (user-configurable, applied via JS) ────────── */
:root { --accent: #1E3A5F; }
.btn-primary  { background: var(--accent); color: #fff; }
.nav-item.active { color: var(--accent); }
.context-btn.active { background: var(--accent); color: #fff; }
.fab { background: var(--accent); }
.spinner { border-top-color: var(--accent); }
.progress-fill { background: var(--accent); }

/* ── Charts ──────────────────────────────────────────────────── */
.chart-container { position: relative; height: 200px; margin: 0.5rem 0; }
.bar-chart { display: flex; align-items: flex-end; gap: 4px; height: 160px; padding: 0 0.2rem; }
.bar-group { display: flex; gap: 2px; align-items: flex-end; flex: 1; min-width: 0; }
.bar { border-radius: 3px 3px 0 0; min-height: 2px; transition: height 0.4s ease; }
.bar.income  { background: var(--teal); }
.bar.expense { background: var(--red); opacity: 0.8; }
.bar.revenue { background: var(--accent); }
.chart-labels { display: flex; gap: 4px; margin-top: 4px; }
.chart-label { flex: 1; text-align: center; font-size: 0.6rem; color: var(--text-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── Dark mode ───────────────────────────────────────────────── */
[data-theme="dark"] {
  --navy:   #0d1117;
  --navy2:  #1e3a5f;
  --text:   #e2e8f0;
  --text-2: #94a3b8;
  --text-3: #64748b;
  --border: #2d3748;
  --bg:     #1a202c;
  --white:  #1a202c;
  --card:   #242d3b;
  --shadow: 0 2px 12px rgba(0,0,0,0.3);
  --shadow-lg: 0 8px 30px rgba(0,0,0,0.5);
}
[data-theme="dark"] .form-input,
[data-theme="dark"] .form-select,
[data-theme="dark"] .form-textarea { background: #2d3748; color: var(--text); border-color: var(--border); }
[data-theme="dark"] .btn-ghost { background: #2d3748; color: var(--text); border-color: var(--border); }
[data-theme="dark"] .badge-draft,
[data-theme="dark"] .badge-void { background: #2d3748; color: var(--text-2); }
[data-theme="dark"] .bottom-nav { background: #1e2533; border-top-color: var(--border); }
[data-theme="dark"] .modal-overlay { background: rgba(0,0,0,0.7); }
[data-theme="dark"] .sheet { background: #242d3b; }

/* ── Category icon grid (transaction quick-add) ─────────────── */
.cat-icon-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  max-height: 180px;
  overflow-y: auto;
  padding: 4px 2px;
}
.cat-icon-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 6px 4px;
  border-radius: var(--radius-sm);
  border: 1.5px solid var(--border);
  background: var(--card);
  cursor: pointer;
  font-size: 1.4rem;
  transition: border-color 0.15s, background 0.15s;
  text-align: center;
  min-width: 0;
}
.cat-icon-item span:last-child {
  font-size: 0.6rem;
  font-weight: 600;
  color: var(--text-2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}
.cat-icon-item:active,
.cat-icon-item.selected {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 10%, var(--card));
}

/* ── Emoji picker grid (category creation) ───────────────────── */
.emoji-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 0.4rem;
}
.emoji-btn {
  width: 38px;
  height: 38px;
  font-size: 1.3rem;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--card);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.15s;
}
.emoji-btn.selected { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, var(--card)); }

/* ── Toggle switch ───────────────────────────────────────────── */
.toggle-wrap { display: flex; align-items: center; justify-content: space-between; padding: 0.5rem 0; }
.toggle { position: relative; display: inline-block; width: 44px; height: 24px; flex-shrink: 0; }
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle-slider { position: absolute; inset: 0; background: var(--border); border-radius: 24px; cursor: pointer; transition: background 0.2s; }
.toggle-slider:before { content: ''; position: absolute; width: 18px; height: 18px; left: 3px; bottom: 3px; background: #fff; border-radius: 50%; transition: transform 0.2s; }
.toggle input:checked + .toggle-slider { background: var(--accent); }
.toggle input:checked + .toggle-slider:before { transform: translateX(20px); }

/* ── Color picker swatch ─────────────────────────────────────── */
.color-swatch { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-top: 0.4rem; }
.color-swatch-item { width: 32px; height: 32px; border-radius: 50%; cursor: pointer; border: 3px solid transparent; transition: border-color 0.15s, transform 0.1s; }
.color-swatch-item.selected { border-color: var(--text); transform: scale(1.1); }

/* ── Billing tabs ────────────────────────────────────────────── */
.billing-tabs {
  display: flex;
  border-bottom: 2px solid var(--border);
  margin-bottom: 1rem;
}
.billing-tab {
  flex: 1;
  padding: 0.6rem 0;
  font-size: 0.85rem;
  font-weight: 600;
  background: none;
  border: none;
  border-bottom: 3px solid transparent;
  margin-bottom: -2px;
  cursor: pointer;
  color: var(--text-2);
  transition: color 0.15s, border-color 0.15s;
}
.billing-tab.active { color: var(--accent); border-bottom-color: var(--accent); }

/* ── Receipt upload ──────────────────────────────────────────── */
.receipt-upload-label {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.65rem;
  border: 2px dashed var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-2);
  transition: border-color 0.15s;
}
.receipt-upload-label:active { border-color: var(--accent); }
.receipt-preview-img {
  max-width: 100%;
  max-height: 180px;
  border-radius: var(--radius-sm);
  display: block;
  margin: 0.5rem auto 0;
  object-fit: contain;
}

/* ── Account / filter chips ─────────────────────────────────── */
.filter-chip {
  flex-shrink: 0;
  padding: 0.3rem 0.75rem;
  border-radius: 999px;
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--text-2);
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
  white-space: nowrap;
}
.filter-chip.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

/* ── Trend months selector ───────────────────────────────────── */
.trend-months-btn {
  padding: 0.2rem 0.55rem;
  border-radius: 6px;
  border: 1.5px solid var(--border);
  background: none;
  color: var(--text-2);
  font-size: 0.72rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.trend-months-btn.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

/* ── Deleted transaction ─────────────────────────────────────── */
.txn-deleted { opacity: 0.65; }
.badge-deleted { background: #FEE2E2; color: #B91C1C; }
.info-banner {
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius-sm);
  background: #FEF3C7;
  color: #92400E;
  font-size: 0.8rem;
  font-weight: 600;
}

/* ── Notification badge on More nav button ───────────────────── */
.notif-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border-radius: 999px;
  background: #EF4444;
  color: #fff;
  font-size: 0.6rem;
  font-weight: 800;
  margin-left: 2px;
  vertical-align: middle;
  line-height: 1;
}
.notif-unread { background: color-mix(in srgb, var(--accent) 6%, var(--surface)); }

/* ── Responsive ──────────────────────────────────────────────── */
@media (min-width: 640px) {
  .page { padding: 1.5rem 2rem calc(var(--nav-h) + 1.5rem); }
  .kpi-grid { grid-template-columns: repeat(3, 1fr); }
  .sheet { border-radius: 20px; margin-bottom: 1rem; }
  .modal-overlay { align-items: center; }
}

@media (min-width: 768px) {
  .kpi-grid { grid-template-columns: repeat(4, 1fr); }
}

/* PWA update banner (memory/reference_pwa_update_pattern.md) */
#update-banner {
  position: fixed; top: 0; left: 0; right: 0; z-index: 1000;
  background: var(--primary, #6366F1); color: #fff;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 14px 10px;
  display: flex; align-items: center; gap: 12px;
  font-size: .85rem; font-weight: 600;
  box-shadow: 0 4px 16px rgba(0, 0, 0, .25);
  animation: ub-in .25s ease-out;
}
#update-banner .ub-text { flex: 1; min-width: 0; }
#update-banner .ub-btn {
  background: #fff; color: var(--primary, #6366F1); border: 0;
  padding: 7px 16px; border-radius: 50px;
  font: inherit; font-weight: 700; cursor: pointer;
}
#update-banner .ub-x {
  background: transparent; border: 0; color: #fff; opacity: .7;
  font-size: 1.4rem; line-height: 1; cursor: pointer; padding: 0 6px;
}
@keyframes ub-in { from { transform: translateY(-100%); } to { transform: translateY(0); } }
