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

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a { color: inherit; }

.sidebar-toggle, .theme-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
}
.sidebar-toggle:hover, .theme-toggle:hover { background: var(--surface-2, var(--bg)); }
.theme-toggle { display: flex; }

.money {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

/* ---------- App shell ---------- */
.app-shell {
  width: 100%;
  min-height: 100vh;
  padding-bottom: 84px;
  position: relative;
}

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 16px 8px;
}

.app-header h1 {
  font-size: 18px;
  margin: 0;
}

.household-name {
  font-size: 13px;
  color: var(--ink-muted);
}

.app-main { padding: 8px 16px 24px; }

.header-search { display: none; }
.header-profile {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  min-width: 44px;
  text-decoration: none;
  color: inherit;
  flex-shrink: 0;
}
.header-profile-name { display: none; }

.header-bell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  min-width: 44px;
  font-size: 20px;
  text-decoration: none;
  flex-shrink: 0;
}
.header-bell-badge {
  position: absolute;
  top: 4px;
  right: 4px;
  min-width: 16px;
  height: 16px;
  padding: 0 3px;
  border-radius: 8px;
  background: var(--danger);
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  line-height: 16px;
  text-align: center;
}

/* ---------- Bottom nav (mobile) ---------- */
.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  z-index: 20;
}

.bottom-nav a {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 10px 2px 8px;
  text-decoration: none;
  color: var(--ink-muted);
  font-size: 10px;
  white-space: nowrap;
  min-height: 44px;
}

.bottom-nav a.active { color: var(--primary); font-weight: 600; }
.bottom-nav .icon { font-size: 20px; line-height: 1; }

.fab {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: 74px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--primary);
  color: var(--primary-ink);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  text-decoration: none;
  box-shadow: 0 6px 16px rgba(23, 88, 63, 0.35);
  z-index: 21;
}

/* ---------- Sidebar (desktop) ---------- */
.sidebar { display: none; }

/* ---------- Cards ---------- */
.card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 16px;
  margin-bottom: 12px;
  border: 1px solid var(--border);
  box-shadow: var(--glow-shadow, none);
}

.card h2, .card h3 { margin-top: 0; }

.stat-row { display: flex; gap: 12px; flex-wrap: wrap; }
.stat {
  flex: 1;
  min-width: 140px;
  background: var(--surface);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  border: 1px solid var(--border);
}
.stat .label { font-size: 12px; color: var(--ink-muted); margin-bottom: 4px; }
.stat .value { font-size: 18px; }

/* Colorful stat tiles: gradient fill, white text, for the headline dashboard numbers */
.stat.gradient {
  color: #fff;
  border: none;
  box-shadow: 0 8px 20px -8px rgba(16, 32, 26, 0.35);
}
.stat.gradient .label { color: rgba(255, 255, 255, 0.85); }
.stat.gradient .value { color: #fff; }
.stat.income.gradient { background: var(--grad-income); }
.stat.expense.gradient { background: var(--grad-expense); }
.stat.balance.gradient { background: var(--grad-balance); }

/* Category / account icon chips */
.icon-chip {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  flex-shrink: 0;
  color: #fff;
}
.icon-chip.small { width: 26px; height: 26px; font-size: 12px; }
.icon-chip.large { width: 44px; height: 44px; font-size: 20px; }

/* ---------- Buttons & forms ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 10px 18px;
  border-radius: var(--radius-sm);
  border: none;
  background: var(--primary);
  color: var(--primary-ink);
  font-family: var(--font-ui);
  font-size: 15px;
  font-weight: 600;
  text-decoration: none;
  cursor: pointer;
}
.btn.secondary { background: var(--surface); color: var(--primary); border: 1px solid var(--primary); }
.btn.block { width: 100%; }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: 13px; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn.icon-round { width: 32px; height: 32px; min-height: 32px; padding: 0; border-radius: 50%; font-size: 16px; line-height: 1; flex-shrink: 0; }
.btn.icon-round.danger { background: var(--surface); color: var(--danger); border: 1px solid var(--danger); }

summary.no-marker { list-style: none; cursor: pointer; }
summary.no-marker::-webkit-details-marker { display: none; }

.field { margin-bottom: 16px; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.field .hint { font-size: 12px; color: var(--ink-muted); margin-top: 4px; }
.field .error { font-size: 12px; color: var(--danger); margin-top: 4px; }

.photo-picker { position: relative; }
.photo-picker-buttons { display: flex; flex-wrap: wrap; gap: 8px; }
.photo-picker-buttons .btn { flex: 1 1 140px; }
.photo-picker-filename:empty { display: none; }
/* Kept in the layout (not display:none) so the native "required" validation
   bubble still targets it correctly - just clipped down to nothing visually. */
.photo-picker-input {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

input[type=text], input[type=email], input[type=number], input[type=date], input[type=tel],
select, textarea {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface);
  font-family: var(--font-ui);
  font-size: 15px;
  color: var(--ink);
}

input[type=text]:focus, input[type=email]:focus, input[type=number]:focus, input[type=date]:focus,
input[type=tel]:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--safe-bg);
}

/* Native file input, restyled to match the app's buttons instead of the raw OS control */
input[type=file] {
  width: 100%;
  font-size: 13px;
  color: var(--ink-muted);
}
input[type=file]::file-selector-button {
  min-height: 40px;
  padding: 8px 16px;
  margin-right: 12px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--primary);
  color: var(--primary-ink);
  font-family: var(--font-ui);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
input[type=file]::file-selector-button:hover { opacity: 0.9; }
input[type=file]:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

.amount-input {
  font-family: var(--font-mono);
  font-size: 20px;
  font-weight: 600;
}

.type-toggle { display: flex; gap: 8px; margin-bottom: 16px; }
.type-toggle label {
  flex: 1;
  text-align: center;
  padding: 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  cursor: pointer;
  font-weight: 600;
}
.type-toggle input { display: none; }
.type-toggle input:checked + span { color: var(--primary-ink); }
.type-toggle label:has(input:checked) { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }

/* ---------- Badges & progress ---------- */
.badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 999px;
}
.badge.safe { background: var(--safe-bg); color: var(--primary); }
.badge.warning { background: var(--warning-bg); color: var(--gold); }
.badge.danger { background: var(--danger-bg); color: var(--danger); }
.badge.role-owner { background: #FBF1DC; color: var(--gold); }
.badge.role-admin { background: #E4EDFB; color: var(--cat-blue); }
.badge.role-member { background: var(--safe-bg); color: var(--primary); }
.badge.role-viewer { background: #EEEEEC; color: var(--ink-muted); }

.progress-bar {
  height: 8px;
  border-radius: 999px;
  background: var(--border);
  overflow: hidden;
  margin: 8px 0;
}
.progress-bar > span { display: block; height: 100%; background: var(--gold); }
.progress-bar.safe > span { background: var(--primary); }
.progress-bar.warning > span { background: var(--gold); }
.progress-bar.danger > span { background: var(--danger); }

/* ---------- Lists ---------- */
.list-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.list-item:last-child { border-bottom: none; }

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--primary);
  color: var(--primary-ink);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  flex-shrink: 0;
  object-fit: cover;
}

.list-item .meta { flex: 1; min-width: 0; }
.list-item .title { font-weight: 600; font-size: 14px; }
.list-item .subtitle { font-size: 12px; color: var(--ink-muted); }
.list-item .amount { font-size: 14px; white-space: nowrap; }
.list-item .amount.in { color: var(--primary); }
.list-item .amount.out { color: var(--danger); }

.empty-state { text-align: center; padding: 32px 16px; color: var(--ink-muted); }

.flash {
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  margin-bottom: 12px;
  font-size: 14px;
  background: var(--safe-bg);
  color: var(--primary);
}
.flash.error { background: var(--danger-bg); color: var(--danger); }

/* ---------- Auth (plain) layout ---------- */
.auth-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 28px 24px;
  border: 1px solid var(--border);
}
.auth-card h1 { font-size: 20px; margin-bottom: 4px; }
.auth-card p.subtitle { color: var(--ink-muted); margin-top: 0; margin-bottom: 24px; font-size: 14px; }
.dev-otp-banner {
  background: var(--warning-bg);
  color: var(--gold);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  font-size: 13px;
  margin-bottom: 16px;
  font-family: var(--font-mono);
}

/* ---------- Responsive ---------- */
@media (min-width: 481px) and (max-width: 1024px) {
  .app-main { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-content: start; padding: 8px 24px 24px; }
  .app-main > .full-width { grid-column: 1 / -1; }
}

@media (min-width: 1025px) {
  .app-shell { padding-bottom: 0; display: flex; align-items: flex-start; }
  .bottom-nav, .fab { display: none; }
  .sidebar-toggle { display: flex; }

  .sidebar {
    display: flex;
    flex-direction: column;
    width: 232px;
    flex-shrink: 0;
    min-height: 100vh;
    background: var(--surface-2, var(--surface));
    border-right: 1px solid var(--border);
    padding: 20px 14px;
    position: sticky;
    top: 0;
    overflow: hidden;
    white-space: nowrap;
    transition: width 0.2s ease, padding 0.2s ease, border-color 0.2s ease;
  }

  /* Collapsed: html.sidebar-collapsed is set by an inline <head> script
     (before paint, from localStorage) and toggled by #sidebar-toggle in
     app.js, so both stay in sync and there's no flash on page load. */
  html.sidebar-collapsed .sidebar {
    width: 0;
    padding-left: 0;
    padding-right: 0;
    border-right-color: transparent;
  }
  .sidebar-brand {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 6px 10px 24px;
    font-weight: 700;
    font-size: 16px;
  }
  .sidebar-brand .mark {
    width: 32px; height: 32px; border-radius: 9px;
    background: var(--primary); color: var(--primary-ink);
    display: flex; align-items: center; justify-content: center;
    font-family: var(--font-mono); font-size: 13px; font-weight: 700;
  }
  .sidebar a, .sidebar-logout button {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    border-radius: var(--radius-sm);
    text-decoration: none;
    color: var(--ink-muted);
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 4px;
    border: none;
    background: none;
    width: 100%;
    text-align: left;
    cursor: pointer;
    font-family: var(--font-ui);
  }
  .sidebar a .icon, .sidebar-logout .icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 22px;
    height: 22px;
    font-size: 18px;
    line-height: 1;
    flex-shrink: 0;
  }
  .sidebar a:hover { background: rgba(255, 255, 255, 0.04); color: var(--ink); }
  .sidebar a.active {
    background: var(--safe-bg);
    color: var(--primary);
    box-shadow: var(--glow-shadow, none);
  }
  .sidebar-logout { margin-top: auto; }
  .sidebar-logout button:hover { color: var(--danger); }

  .app-column { flex: 1; min-width: 0; }
  .app-header { padding: 24px 32px 8px; }
  .app-main {
    display: grid;
    grid-template-columns: repeat(12, 1fr);
    gap: 18px;
    align-content: start;
    padding: 16px 32px 32px;
  }
  .app-main > .full-width { grid-column: 1 / -1; }
  .app-main > .card:not(.full-width) { grid-column: span 6; }
  .app-main > .card.span-3 { grid-column: span 3; }
  .app-main > .card.span-4 { grid-column: span 4; }
  .app-main > .card.span-6 { grid-column: span 6; }
  .app-main > .card.span-8 { grid-column: span 8; }
  .app-main > .card.span-12 { grid-column: span 12; }

  .header-search {
    display: flex;
    align-items: center;
    gap: 8px;
    flex: 1;
    max-width: 360px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 0 16px;
  }
  .header-search input {
    border: none;
    background: none;
    min-height: 40px;
    padding: 0;
    color: var(--ink);
  }
  .header-search input:focus { outline: none; }
  .header-profile { display: flex; align-items: center; gap: 10px; min-width: 0; }
  .header-profile-name { display: block; font-size: 14px; font-weight: 600; }
}


/* ---------- Receipt / image modal (in-page popup, no new tab) ---------- */
.image-modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.image-modal[hidden] { display: none; }
.image-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
}
.image-modal-content {
  position: relative;
  max-width: min(90vw, 560px);
  max-height: 85vh;
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 12px;
  z-index: 1;
}
.image-modal-content img {
  display: block;
  max-width: 100%;
  max-height: calc(85vh - 24px);
  border-radius: var(--radius-sm);
  margin: 0 auto;
}
.camera-modal-content {
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}
.camera-modal-content video, .camera-modal-content canvas {
  max-width: 100%;
  max-height: 60vh;
  min-height: 0;
  border-radius: var(--radius-sm);
  margin: 0 auto;
  background: #000;
  flex-shrink: 1;
}
/* display:block only applies while NOT hidden, so JS toggling .hidden still
   works - an unconditional "display: block" here would beat the browser's
   own "[hidden] { display: none }" rule and both would stay visible at once. */
.camera-modal-content video:not([hidden]), .camera-modal-content canvas:not([hidden]) {
  display: block;
}
.camera-modal-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
  flex-shrink: 0;
}
.camera-modal-actions .btn { flex: 1; }
.image-modal-close {
  position: absolute;
  top: -14px;
  right: -14px;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  color: var(--primary-ink);
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

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

/* ---------- AI assistant chat ---------- */
.chat-messages {
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin-bottom: 4px;
}
.chat-bubble-row { display: flex; flex-direction: column; max-width: 78%; min-width: 0; }
.chat-bubble-row.user { align-self: flex-end; align-items: flex-end; }
.chat-bubble-row.assistant { align-self: flex-start; align-items: flex-start; }
.chat-bubble {
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.55;
  word-break: break-word;
  max-width: 100%;
  min-width: 0;
}
.chat-bubble p { margin: 0 0 8px; }
.chat-bubble p:last-child { margin-bottom: 0; }
.chat-bubble ul, .chat-bubble ol { margin: 0 0 8px; padding-left: 20px; }
.chat-bubble ul:last-child, .chat-bubble ol:last-child { margin-bottom: 0; }
.chat-bubble li { margin-bottom: 2px; }
.chat-bubble .chat-heading { display: block; font-size: 14px; margin: 0 0 6px; }
.chat-bubble .chat-hr { border: none; border-top: 1px solid rgba(127, 127, 127, 0.25); margin: 8px 0; }
.chat-table-wrap { max-width: 100%; overflow-x: auto; margin: 0 0 8px; }
.chat-table-wrap:last-child { margin-bottom: 0; }
.chat-bubble table.chat-table { border-collapse: collapse; font-size: 13px; }
.chat-bubble table.chat-table th, .chat-bubble table.chat-table td {
  padding: 4px 8px;
  border: 1px solid rgba(127, 127, 127, 0.25);
  text-align: left;
}
.chat-bubble table.chat-table th { background: rgba(127, 127, 127, 0.12); }
.chat-bubble code {
  font-family: var(--font-mono);
  font-size: 12.5px;
  background: rgba(127, 127, 127, 0.15);
  padding: 1px 4px;
  border-radius: 4px;
}
.chat-bubble-row.user .chat-bubble {
  background: var(--primary);
  color: var(--primary-ink);
  border-bottom-right-radius: 4px;
}
.chat-bubble-row.assistant .chat-bubble {
  background: var(--surface-2, var(--bg));
  color: var(--ink);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.chat-meta {
  font-size: 11px;
  color: var(--ink-muted);
  margin-top: 4px;
  padding: 0 4px;
}
.chat-speak-btn {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 12px;
  padding: 0 0 0 4px;
  line-height: 1;
  vertical-align: middle;
  opacity: 0.75;
}
.chat-speak-btn:hover { opacity: 1; }
.chat-speak-btn:disabled { cursor: default; }
.chat-input-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  margin-top: 14px;
}
.chat-input-row textarea { min-height: 44px; max-height: 140px; }
.chat-suggestions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 4px; }
.chat-suggestion-btn {
  font-size: 13px;
  padding: 8px 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
}
.chat-suggestion-btn:hover { border-color: var(--primary); color: var(--primary); }

.topic-chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0 4px; }
.topic-chip {
  display: inline-block;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 12.5px;
  font-weight: 600;
  padding: 7px 13px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--ink-muted);
  text-decoration: none;
  cursor: pointer;
}
.topic-chip:hover { border-color: var(--primary); color: var(--ink); }
.topic-chip.active { background: var(--primary); color: var(--primary-ink); border-color: var(--primary); }
.topic-chip.active:hover { color: var(--primary-ink); }
.topic-chip-new { border-style: dashed; }
