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

:root {
  --equipment: #ea580c;
  --ground: #7c3aed;
  --flight: #2563eb;
  --primary: #1e40af;
  --bg: #f1f5f9;
  --card: #ffffff;
  --text: #1e293b;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --danger: #dc2626;
  --nav-height: 60px;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* #app is full-width always; content inside constrains itself */
#app { min-height: 100vh; }

/* ── Top nav ─────────────────────────────────────────────────────────────── */

.top-nav {
  display: none;
  background: linear-gradient(135deg, #1e3a8a, #1d4ed8);
  width: 100%;
  height: 60px;
  position: sticky;
  top: 0;
  z-index: 100;
}

.top-nav-inner {
  max-width: 1280px;
  margin: 0 auto;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 2rem;
}

.top-nav-brand { font-size: 1.05rem; font-weight: 700; color: white; }

.top-nav-links { display: flex; align-items: center; gap: 0.25rem; }

.top-nav-link {
  padding: 0.4rem 0.875rem;
  border-radius: 8px;
  border: none;
  background: transparent;
  color: rgba(255,255,255,0.7);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.15s;
}

.top-nav-link:hover { color: white; background: rgba(255,255,255,0.1); }
.top-nav-link.active { color: white; background: rgba(255,255,255,0.18); font-weight: 600; }

.top-nav-add {
  margin-left: 0.5rem;
  padding: 0.45rem 1.1rem;
  border-radius: 8px;
  border: 1.5px solid rgba(255,255,255,0.5);
  background: transparent;
  color: white;
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.15s;
}

.top-nav-add:hover { background: rgba(255,255,255,0.12); border-color: white; }

/* ── Header (gradient total banner) ─────────────────────────────────────── */

.header {
  background: linear-gradient(135deg, #1e3a8a, #1d4ed8);
  color: white;
  padding: 1.25rem 1rem 0;
  overflow: hidden;
}

.header-top { padding-bottom: 0.625rem; }

.header-mobile-title {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.625rem;
}

.header-mobile-title h1 { font-size: 1.125rem; font-weight: 600; }
.header .total-label { font-size: 0.75rem; opacity: 0.75; }
.header .total-amount { font-size: 2.25rem; font-weight: 700; letter-spacing: -0.5px; margin-top: 0.125rem; }

/* ── Header chart ────────────────────────────────────────────────────────── */

.header-meta-row {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 0.5rem;
}

.header-range-btns { display: flex; gap: 0.25rem; padding-bottom: 0.3rem; }

.range-btn {
  padding: 0.2rem 0.55rem;
  border-radius: 6px;
  border: 1.5px solid rgba(255,255,255,0.35);
  background: transparent;
  color: rgba(255,255,255,0.6);
  font-size: 0.72rem;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.15s;
}

.range-btn.active { background: rgba(255,255,255,0.18); border-color: rgba(255,255,255,0.8); color: white; }
.range-btn:hover:not(.active) { color: white; border-color: rgba(255,255,255,0.55); }

.header-chart-wrap {
  height: 100px;
  margin: 0.5rem -1rem 0;
  position: relative;
}

/* ── Content wrapper ─────────────────────────────────────────────────────── */

.content {
  padding: 1rem;
  padding-bottom: calc(var(--nav-height) + env(safe-area-inset-bottom, 0px) + 1rem);
}

/* ── Cards ───────────────────────────────────────────────────────────────── */

.card {
  background: var(--card);
  border-radius: 16px;
  padding: 1rem;
  box-shadow: 0 1px 4px rgba(0,0,0,0.07);
  margin-bottom: 0.75rem;
}

.card-title {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 0.875rem;
}

/* ── Category grid ───────────────────────────────────────────────────────── */

.category-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.625rem;
  margin-bottom: 0.75rem;
}

.category-card {
  background: var(--card);
  border-radius: 14px;
  padding: 0.875rem 0.5rem;
  text-align: center;
  box-shadow: 0 1px 4px rgba(0,0,0,0.07);
  cursor: pointer;
  transition: transform 0.15s;
  border-top: 3px solid transparent;
  -webkit-tap-highlight-color: transparent;
}

.category-card:active { transform: scale(0.96); }
.category-card.equipment    { border-top-color: var(--equipment); }
.category-card.ground_school { border-top-color: var(--ground); }
.category-card.flight_lesson { border-top-color: var(--flight); }

.cat-icon { font-size: 1.375rem; margin-bottom: 0.25rem; }
.cat-label { font-size: 0.6rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--text-muted); margin-bottom: 0.25rem; }
.cat-amount { font-size: 0.95rem; font-weight: 700; color: var(--text); }

/* ── Dashboard grid ──────────────────────────────────────────────────────── */

.dashboard-grid { display: contents; }
.dashboard-left, .dashboard-right { display: contents; }

/* ── Expense list ────────────────────────────────────────────────────────── */

.expense-list { display: flex; flex-direction: column; }

.expense-item {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.expense-item:last-child { border-bottom: none; }

.expense-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.125rem;
  flex-shrink: 0;
}

.expense-icon.equipment    { background: #fff7ed; }
.expense-icon.ground_school { background: #f5f3ff; }
.expense-icon.flight_lesson { background: #eff6ff; }

.expense-info { flex: 1; min-width: 0; }
.expense-desc { font-size: 0.9rem; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.expense-meta { font-size: 0.72rem; color: var(--text-muted); margin-top: 0.125rem; }

.expense-amount { font-weight: 700; font-size: 0.95rem; white-space: nowrap; flex-shrink: 0; }
.expense-amount.equipment    { color: var(--equipment); }
.expense-amount.ground_school { color: var(--ground); }
.expense-amount.flight_lesson { color: var(--flight); }

.btn-delete {
  background: none;
  border: none;
  color: #cbd5e1;
  cursor: pointer;
  padding: 0.375rem;
  font-size: 1rem;
  transition: color 0.15s;
  flex-shrink: 0;
}

.btn-delete:hover, .btn-delete:active { color: var(--danger); }

.view-all { display: block; text-align: center; color: var(--primary); font-size: 0.875rem; font-weight: 500; padding: 0.75rem; cursor: pointer; }

/* ── Empty / loading ─────────────────────────────────────────────────────── */

.empty-state { text-align: center; padding: 2rem 1rem; color: var(--text-muted); }
.empty-icon { font-size: 2.5rem; margin-bottom: 0.625rem; }
.empty-state p { font-size: 0.875rem; }
.loading { text-align: center; padding: 3rem 1rem; color: var(--text-muted); font-size: 0.9rem; }
.error-banner { background: #fee2e2; color: var(--danger); padding: 0.75rem 1rem; border-radius: 10px; font-size: 0.875rem; margin-bottom: 0.75rem; }

/* ── Bottom nav (mobile) ─────────────────────────────────────────────────── */

.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(var(--nav-height) + env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  background: var(--card);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  z-index: 100;
}

.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  cursor: pointer;
  padding: 0.5rem 0;
  border: none;
  background: none;
  color: var(--text-muted);
  font-size: 0.58rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-family: inherit;
  transition: color 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.nav-item.active { color: var(--primary); }
.nav-icon { font-size: 1.2rem; }
.nav-add-tab { color: var(--primary); }

/* ── History view ────────────────────────────────────────────────────────── */

.history-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 0.75rem; }
.history-header h2 { font-size: 1.1rem; font-weight: 600; }

.data-actions { display: flex; gap: 0.5rem; }

.btn-secondary {
  padding: 0.4rem 0.875rem;
  border-radius: 8px;
  border: 1.5px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.15s;
}

.btn-secondary:hover { border-color: var(--primary); color: var(--primary); }

.btn-export {
  padding: 0.4rem 0.875rem;
  border-radius: 8px;
  border: 1.5px solid var(--primary);
  background: transparent;
  color: var(--primary);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.15s;
}

.btn-export:active { background: var(--primary); color: white; }

.filter-tabs { display: flex; gap: 0.5rem; overflow-x: auto; padding-bottom: 0.625rem; -webkit-overflow-scrolling: touch; scrollbar-width: none; margin-bottom: 0.75rem; }
.filter-tabs::-webkit-scrollbar { display: none; }

.filter-tab {
  padding: 0.4rem 0.875rem;
  border-radius: 20px;
  border: 1.5px solid var(--border);
  background: var(--card);
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  font-family: inherit;
  color: var(--text-muted);
  transition: all 0.15s;
  flex-shrink: 0;
}

.filter-tab.active { background: var(--primary); border-color: var(--primary); color: white; }

/* ── Add / Edit form ─────────────────────────────────────────────────────── */

.form-wrap { display: contents; }

.form-header {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 1rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  position: sticky;
  top: 0;
  z-index: 10;
}

.btn-back { background: none; border: none; cursor: pointer; font-size: 1.375rem; color: var(--primary); padding: 0.25rem; display: flex; align-items: center; line-height: 1; }
.form-header h2 { font-size: 1.1rem; font-weight: 600; }

.form-content {
  padding: 1rem;
  padding-bottom: calc(5rem + env(safe-area-inset-bottom, 0px));
}

.form-group { margin-bottom: 1.25rem; }

.form-group label {
  display: block;
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.5rem;
}

.form-group input,
.form-group select,
.form-group textarea {
  width: 100%;
  padding: 0.75rem 0.875rem;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  font-size: 1rem;
  font-family: inherit;
  color: var(--text);
  background: var(--card);
  transition: border-color 0.15s;
  -webkit-appearance: none;
  appearance: none;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(30,64,175,0.1);
}

.form-group textarea { resize: vertical; min-height: 80px; }

.category-selector { display: grid; grid-template-columns: repeat(3, 1fr); gap: 0.5rem; }

.cat-btn {
  padding: 0.75rem 0.25rem;
  border: 2px solid var(--border);
  border-radius: 10px;
  background: var(--card);
  cursor: pointer;
  text-align: center;
  transition: all 0.15s;
  font-family: inherit;
  -webkit-tap-highlight-color: transparent;
}

.cat-btn-icon { font-size: 1.25rem; display: block; margin-bottom: 0.25rem; }
.cat-btn-label { font-size: 0.6rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.03em; color: var(--text-muted); }

.cat-btn.selected.equipment    { border-color: var(--equipment); background: #fff7ed; }
.cat-btn.selected.ground_school { border-color: var(--ground); background: #f5f3ff; }
.cat-btn.selected.flight_lesson { border-color: var(--flight); background: #eff6ff; }

.input-prefix { position: relative; }
.input-prefix::before { content: '$'; position: absolute; left: 0.875rem; top: 50%; transform: translateY(-50%); color: var(--text-muted); font-size: 1rem; font-weight: 500; pointer-events: none; }
.input-prefix input { padding-left: 1.625rem; }

.btn-submit {
  position: fixed;
  bottom: calc(1rem + env(safe-area-inset-bottom, 0px));
  left: 50%;
  transform: translateX(-50%);
  width: calc(100% - 2rem);
  max-width: 568px;
  padding: 1rem;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: 14px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  box-shadow: 0 4px 14px rgba(30,64,175,0.35);
  transition: opacity 0.15s;
  z-index: 10;
}

.btn-submit:active, .btn-submit:disabled { opacity: 0.75; }

/* ── Insights view ───────────────────────────────────────────────────────── */

.page-title { font-size: 1.25rem; font-weight: 700; margin-bottom: 1rem; }

.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0.75rem; margin-bottom: 0.75rem; }

.stat-card {
  background: var(--card);
  border-radius: 16px;
  padding: 1rem;
  box-shadow: 0 1px 4px rgba(0,0,0,0.07);
}

.stat-label { font-size: 0.7rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-muted); margin-bottom: 0.25rem; }
.stat-value { font-size: 1.5rem; font-weight: 700; letter-spacing: -0.5px; }
.stat-change { font-size: 0.75rem; color: var(--text-muted); margin-top: 0.25rem; }

.chart-legend { display: flex; gap: 1rem; justify-content: center; margin-top: 0.75rem; flex-wrap: wrap; }
.legend-item { display: flex; align-items: center; gap: 0.375rem; font-size: 0.78rem; color: var(--text-muted); }
.legend-dot { width: 10px; height: 10px; border-radius: 3px; flex-shrink: 0; }
.legend-dot.equipment    { background: var(--equipment); }
.legend-dot.ground_school { background: var(--ground); }
.legend-dot.flight_lesson { background: var(--flight); }

.breakdown-row { display: flex; align-items: center; gap: 0.75rem; margin-bottom: 0.75rem; }
.breakdown-row:last-child { margin-bottom: 0; }
.breakdown-label { display: flex; align-items: center; gap: 0.375rem; font-size: 0.8rem; font-weight: 500; width: 110px; flex-shrink: 0; }
.breakdown-bar-wrap { flex: 1; }
.breakdown-bar { height: 8px; background: var(--border); border-radius: 4px; overflow: hidden; }
.breakdown-fill { height: 100%; border-radius: 4px; transition: width 0.4s ease; }
.breakdown-fill.equipment    { background: var(--equipment); }
.breakdown-fill.ground_school { background: var(--ground); }
.breakdown-fill.flight_lesson { background: var(--flight); }
.breakdown-amount { font-size: 0.85rem; font-weight: 700; white-space: nowrap; width: 54px; text-align: right; flex-shrink: 0; }
.breakdown-tap { cursor: pointer; -webkit-tap-highlight-color: transparent; }
.breakdown-tap:hover { opacity: 0.7; }

.seg-control {
  display: flex;
  gap: 2px;
  padding: 3px;
  background: var(--border);
  border-radius: 8px;
  flex-shrink: 0;
}

.seg-btn {
  padding: 0.3rem 0.75rem;
  border-radius: 5px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.15s;
  line-height: 1.3;
  -webkit-tap-highlight-color: transparent;
}

.seg-btn.active {
  background: white;
  color: var(--primary);
  box-shadow: 0 1px 3px rgba(0,0,0,0.12);
}

.insights-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.875rem;
}

.mode-toggle {
  display: inline-flex;
  gap: 2px;
  padding: 3px;
  background: var(--border);
  border-radius: 8px;
  border: none;
  cursor: pointer;
  font-family: inherit;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
}

.mode-toggle span {
  display: inline-block;
  padding: 0.25rem 0.625rem;
  border-radius: 5px;
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--text-muted);
  transition: background 0.15s, color 0.15s, box-shadow 0.15s;
  line-height: 1.3;
  pointer-events: none;
}

.mode-toggle span.active {
  background: white;
  color: var(--primary);
  box-shadow: 0 1px 3px rgba(0,0,0,0.12);
}
.breakdown-amount.equipment    { color: var(--equipment); }
.breakdown-amount.ground_school { color: var(--ground); }
.breakdown-amount.flight_lesson { color: var(--flight); }

.stats-list { display: flex; flex-direction: column; }
.stats-row { display: flex; align-items: center; justify-content: space-between; padding: 0.625rem 0; border-bottom: 1px solid var(--border); }
.stats-row:last-child { border-bottom: none; }
.stats-label { font-size: 0.875rem; color: var(--text-muted); }
.stats-val { font-size: 0.95rem; font-weight: 700; }

/* ── Toast ───────────────────────────────────────────────────────────────── */

.toast {
  position: fixed;
  bottom: calc(var(--nav-height) + env(safe-area-inset-bottom, 0px) + 1rem);
  left: 50%;
  transform: translateX(-50%);
  background: #1e293b;
  color: white;
  padding: 0.75rem 1.25rem;
  border-radius: 10px;
  font-size: 0.875rem;
  font-weight: 500;
  white-space: nowrap;
  z-index: 200;
  animation: slideUp 0.2s ease;
  pointer-events: none;
}

@keyframes slideUp {
  from { transform: translateX(-50%) translateY(16px); opacity: 0; }
  to   { transform: translateX(-50%) translateY(0);   opacity: 1; }
}

/* ── Tablet / Desktop (≥ 768px) ──────────────────────────────────────────── */

@media (min-width: 768px) {
  :root { --nav-height: 0px; }

  .top-nav { display: block; }
  .bottom-nav { display: none !important; }
  .header-mobile-title { display: none; }

  /* Header spans full viewport width */
  .header { padding: 2rem 2.5rem 0; }
  .header .total-amount { font-size: 2.75rem; }

  .header-chart-wrap { height: 140px; margin-right: -2.5rem; margin-left: -2.5rem; }

  /* All page content is constrained and centered */
  .content {
    max-width: 1280px;
    margin: 0 auto;
    padding: 1.5rem 2rem 2rem;
  }

  /* Dashboard: 2-column layout */
  .dashboard-grid {
    display: grid;
    grid-template-columns: 260px 1fr;
    gap: 1.25rem;
    align-items: start;
  }

  .dashboard-left  { display: block; }
  .dashboard-right { display: block; }

  /* Category cards stack vertically on the left */
  .category-grid { grid-template-columns: 1fr; margin-bottom: 0; }

  .category-card {
    display: flex;
    align-items: center;
    text-align: left;
    gap: 0.875rem;
    padding: 1rem 1.125rem;
    border-top: none;
    border-left: 4px solid transparent;
  }

  .category-card.equipment    { border-left-color: var(--equipment); }
  .category-card.ground_school { border-left-color: var(--ground); }
  .category-card.flight_lesson { border-left-color: var(--flight); }

  .cat-icon { font-size: 1.5rem; margin-bottom: 0; flex-shrink: 0; }
  .cat-label { margin-bottom: 0.125rem; }
  .cat-amount { font-size: 1.125rem; }

  /* Add/edit form: centered card on desktop */
  .form-wrap {
    display: block;
    max-width: 620px;
    margin: 2rem auto;
    background: var(--card);
    border-radius: 20px;
    box-shadow: 0 4px 24px rgba(0,0,0,0.08);
    overflow: hidden;
  }

  .form-header { position: static; }

  .form-content { padding: 1.5rem 2rem 2rem; }

  /* Submit button: regular flow on desktop */
  .btn-submit {
    position: static;
    transform: none;
    width: 100%;
    margin-top: 0.5rem;
    border-radius: 10px;
    box-shadow: none;
  }

  /* Insights: wider stat cards */
  .stat-value { font-size: 1.75rem; }
  .breakdown-label { width: 130px; }
  .breakdown-amount { width: 60px; }

  .toast { bottom: 1.5rem; }
}
