*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
:root {
  --blue: #2563eb; --green: #059669; --amber: #d97706; --purple: #7c3aed; --teal: #0891b2;
  --gray-50: #f8fafc; --gray-100: #f1f5f9; --gray-200: #e2e8f0; --gray-300: #cbd5e1;
  --gray-400: #94a3b8; --gray-500: #64748b; --gray-600: #475569; --gray-700: #334155;
  --gray-800: #1e293b; --gray-900: #0f172a;
  --radius: 12px; --radius-sm: 8px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
}
html, body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--gray-50); color: var(--gray-800); font-size: 16px;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  padding-top: var(--safe-top);
}
body { min-height: 100dvh; display: flex; flex-direction: column; }

/* ─── Auth Overlay (session check) ─── */
.auth-overlay {
  position: fixed; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  background: var(--gray-50); z-index: 500;
}
.auth-overlay .auth-spinner { font-size: 48px; animation: bounce 1s infinite; }
@keyframes bounce { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-10px); } }
.auth-overlay p { color: var(--gray-500); margin-top: 12px; font-size: 14px; }

/* ─── Auth Page ─── */
.auth-container {
  display: flex; align-items: center; justify-content: center;
  min-height: 100dvh; padding: 20px;
}
.auth-card {
  background: white; border-radius: var(--radius); padding: 32px 24px;
  max-width: 380px; width: 100%; box-shadow: 0 4px 24px rgba(0,0,0,0.08);
}
.auth-card h1 { margin-bottom: 4px; text-align: center; }
.auth-sub { text-align: center; color: var(--gray-500); font-size: 14px; margin-bottom: 24px; }
.auth-tabs {
  display: flex; border: 1px solid var(--gray-200); border-radius: var(--radius-sm);
  margin-bottom: 24px; overflow: hidden;
}
.auth-tabs button {
  flex: 1; padding: 10px; border: none; background: transparent;
  font-size: 14px; font-weight: 600; cursor: pointer; color: var(--gray-500);
  transition: all 0.15s;
}
.auth-tabs button.active { background: var(--blue); color: white; }
.auth-error { color: #ef4444; font-size: 13px; margin-bottom: 12px; min-height: 20px; }
.auth-footer { text-align: center; color: var(--gray-400); font-size: 12px; margin-top: 20px; }

/* ─── Top Bar ─── */
.top-bar {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 16px; background: white; border-bottom: 1px solid var(--gray-200);
  z-index: 50;
}
.top-left { display: flex; align-items: center; gap: 8px; }
.top-user { font-size: 13px; font-weight: 600; color: var(--gray-600); }
.top-right { display: flex; gap: 4px; }
.icon-btn {
  background: none; border: none; font-size: 18px; cursor: pointer;
  padding: 4px 8px; border-radius: 6px; line-height: 1;
  transition: background 0.15s;
}
.icon-btn:hover { background: var(--gray-100); }

/* ─── Navigation (Tab Bar) ─── */
nav.tabs {
  position: fixed; bottom: 0; left: 0; right: 0;
  display: flex; background: white; border-top: 1px solid var(--gray-200);
  padding-bottom: var(--safe-bottom); z-index: 100;
}
nav.tabs button {
  flex: 1; padding: 8px 4px 6px; border: none; background: transparent;
  font-size: 11px; color: var(--gray-400); cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  transition: color 0.15s; -webkit-tap-highlight-color: transparent;
}
nav.tabs button .icon { font-size: 22px; line-height: 1; }
nav.tabs button.active { color: var(--blue); font-weight: 600; }

/* ─── Screens ─── */
.screen { display: none; flex: 1; padding: 16px 16px calc(80px + var(--safe-bottom)); overflow-y: auto; }
.screen.active { display: block; }

/* ─── Header ─── */
h1 { font-size: 24px; font-weight: 700; margin-bottom: 4px; }
h2 { font-size: 18px; font-weight: 600; margin-bottom: 12px; }
.subtitle { color: var(--gray-500); font-size: 14px; margin-bottom: 20px; }

/* ─── Quick Trip Buttons ─── */
.trip-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 24px;
}
.trip-btn {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: 16px 8px; border: none; border-radius: var(--radius);
  color: white; font-size: 14px; font-weight: 600; cursor: pointer;
  min-height: 80px; transition: transform 0.1s, opacity 0.1s;
  -webkit-tap-highlight-color: transparent; position: relative;
}
.trip-btn .dist { font-size: 24px; font-weight: 800; margin-top: 4px; }
.trip-btn .unit { font-size: 12px; opacity: 0.8; }
.trip-btn:active { transform: scale(0.96); opacity: 0.9; }

.add-btn {
  border: 2px dashed var(--gray-300); background: white; color: var(--gray-500);
  font-size: 13px; cursor: pointer; border-radius: var(--radius);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  min-height: 80px; gap: 4px;
}
.add-btn .plus { font-size: 28px; font-weight: 300; line-height: 1; }

/* ─── Forms ─── */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 13px; font-weight: 600; color: var(--gray-600); margin-bottom: 4px; }
.form-group input, .form-group select, .form-group textarea {
  width: 100%; padding: 12px 14px; border: 1px solid var(--gray-300); border-radius: var(--radius-sm);
  font-size: 16px; background: white; color: var(--gray-800);
  -webkit-appearance: none; appearance: none;
}
.form-group textarea { resize: vertical; min-height: 60px; font-family: inherit; }
.form-group input:focus, .form-group select:focus, .form-group textarea:focus {
  outline: none; border-color: var(--blue); box-shadow: 0 0 0 3px rgba(37,99,235,0.15);
}
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* ─── Buttons ─── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 12px 24px; border-radius: var(--radius-sm); border: none;
  font-size: 16px; font-weight: 600; cursor: pointer; width: 100%;
  transition: opacity 0.15s; -webkit-tap-highlight-color: transparent;
}
.btn:active { opacity: 0.8; }
.btn-primary { background: var(--blue); color: white; }
.btn-danger { background: #ef4444; color: white; }
.btn-outline { background: white; color: var(--gray-600); border: 1px solid var(--gray-300); }
.btn-sm { padding: 8px 16px; font-size: 14px; width: auto; }

.btn-group { display: flex; gap: 8px; margin-top: 8px; }

/* ─── Cards / Lists ─── */
.card {
  background: white; border-radius: var(--radius); padding: 16px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06); margin-bottom: 8px;
}
.card-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 4px; }
.card-title { font-weight: 600; font-size: 15px; }
.card-sub { color: var(--gray-500); font-size: 13px; }
.card-amount { font-size: 18px; font-weight: 700; }
.card-actions { display: flex; gap: 6px; margin-top: 8px; }
.card-actions button {
  padding: 4px 10px; border: 1px solid var(--gray-300); border-radius: 6px;
  background: white; font-size: 12px; cursor: pointer; color: var(--gray-600);
}
.card-actions button.danger { color: #ef4444; border-color: #fca5a5; }

.tag {
  display: inline-block; padding: 2px 8px; border-radius: 4px;
  font-size: 11px; font-weight: 600; color: white;
}

/* ─── Summary Stats ─── */
.stats-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 20px;
}
.stat-card {
  background: white; border-radius: var(--radius); padding: 16px; text-align: center;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}
.stat-card .stat-value { font-size: 28px; font-weight: 800; color: var(--blue); }
.stat-card .stat-label { font-size: 12px; color: var(--gray-500); margin-top: 2px; }
.stat-card .stat-sub { font-size: 14px; color: var(--green); font-weight: 600; }

/* ─── Preset Editor ─── */
.preset-item {
  display: flex; align-items: center; gap: 12px;
  padding: 12px; background: white; border-radius: var(--radius-sm);
  margin-bottom: 6px; box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}
.preset-item .color-dot { width: 12px; height: 12px; border-radius: 50%; flex-shrink: 0; }
.preset-item .info { flex: 1; }
.preset-item .info .name { font-weight: 600; font-size: 15px; }
.preset-item .info .detail { font-size: 12px; color: var(--gray-500); }
.preset-item .actions { display: flex; gap: 4px; }
.preset-item .actions button {
  padding: 4px 8px; border: none; background: transparent; font-size: 16px; cursor: pointer;
  color: var(--gray-400);
}

/* ─── Modal ─── */
.modal-overlay {
  display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.4);
  z-index: 200; align-items: flex-end; justify-content: center;
}
.modal-overlay.show { display: flex; }
.modal {
  background: white; border-radius: var(--radius) var(--radius) 0 0;
  width: 100%; max-width: 500px; max-height: 85vh; overflow-y: auto;
  padding: 20px 16px calc(20px + var(--safe-bottom));
  animation: slideUp 0.25s ease;
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
.modal h2 { margin-bottom: 16px; }

/* ─── Toast ─── */
.toast {
  position: fixed; top: calc(50px + var(--safe-top)); left: 50%; transform: translateX(-50%);
  background: var(--gray-800); color: white; padding: 10px 20px; border-radius: 20px;
  font-size: 14px; font-weight: 500; z-index: 300;
  opacity: 0; transition: opacity 0.3s; pointer-events: none;
}
.toast.show { opacity: 1; }

/* ─── Receipt Email Config ─── */
.config-box {
  background: white; border-radius: var(--radius); padding: 16px; margin-bottom: 16px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}
.config-box .label { font-size: 12px; color: var(--gray-500); text-transform: uppercase; letter-spacing: 0.5px; }
.config-box .value { font-size: 16px; font-weight: 600; margin-top: 4px; word-break: break-all; font-family: monospace; }

/* ─── Empty State ─── */
.empty-state { text-align: center; padding: 40px 20px; color: var(--gray-400); }
.empty-state .big-icon { font-size: 48px; margin-bottom: 12px; }

/* ─── Dark Mode (manual toggle via .dark class) ─── */
body.dark {
  --gray-50: #0f172a; --gray-100: #1e293b; --gray-200: #334155;
  --gray-300: #475569; --gray-800: #e2e8f0; --gray-600: #94a3b8;
  --gray-500: #64748b; --gray-400: #475569; --gray-700: #cbd5e1;
  background: var(--gray-50); color: var(--gray-800);
}
body.dark nav.tabs { background: var(--gray-100); border-color: var(--gray-200); }
body.dark nav.tabs button { color: var(--gray-500); }
body.dark nav.tabs button.active { color: #60a5fa; }
body.dark .card, body.dark .stat-card, body.dark .config-box, body.dark .preset-item { background: var(--gray-100); }
body.dark .modal { background: var(--gray-100); }
body.dark .top-bar { background: var(--gray-100); border-color: var(--gray-200); }
body.dark .form-group input, body.dark .form-group select, body.dark .form-group textarea {
  background: var(--gray-50); color: var(--gray-800); border-color: var(--gray-200);
}
body.dark .btn-outline { background: transparent; color: var(--gray-600); border-color: var(--gray-200); }
body.dark .add-btn { background: var(--gray-100); border-color: var(--gray-200); }
body.dark .card-actions button { background: var(--gray-50); border-color: var(--gray-200); color: var(--gray-500); }
body.dark .toast { background: var(--gray-200); color: var(--gray-50); }
body.dark .auth-card { background: var(--gray-100); }
body.dark .auth-overlay { background: var(--gray-50); }
body.dark .icon-btn:hover { background: var(--gray-200); }

/* Also respect system preference when no manual toggle set */
@media (prefers-color-scheme: dark) {
  body:not(.light):not(.dark) {
    --gray-50: #0f172a; --gray-100: #1e293b; --gray-200: #334155;
    --gray-300: #475569; --gray-800: #e2e8f0; --gray-600: #94a3b8;
    --gray-500: #64748b; --gray-400: #475569; --gray-700: #cbd5e1;
    background: var(--gray-50); color: var(--gray-800);
  }
  body:not(.light):not(.dark) nav.tabs { background: var(--gray-100); border-color: var(--gray-200); }
  body:not(.light):not(.dark) nav.tabs button { color: var(--gray-500); }
  body:not(.light):not(.dark) nav.tabs button.active { color: #60a5fa; }
  body:not(.light):not(.dark) .card, body:not(.light):not(.dark) .stat-card,
  body:not(.light):not(.dark) .config-box, body:not(.light):not(.dark) .preset-item { background: var(--gray-100); }
  body:not(.light):not(.dark) .modal { background: var(--gray-100); }
  body:not(.light):not(.dark) .top-bar { background: var(--gray-100); border-color: var(--gray-200); }
  body:not(.light):not(.dark) .form-group input, body:not(.light):not(.dark) .form-group select,
  body:not(.light):not(.dark) .form-group textarea { background: var(--gray-50); color: var(--gray-800); border-color: var(--gray-200); }
  body:not(.light):not(.dark) .btn-outline { background: transparent; color: var(--gray-600); border-color: var(--gray-200); }
  body:not(.light):not(.dark) .add-btn { background: var(--gray-100); border-color: var(--gray-200); }
  body:not(.light):not(.dark) .card-actions button { background: var(--gray-50); border-color: var(--gray-200); color: var(--gray-500); }
  body:not(.light):not(.dark) .toast { background: var(--gray-200); color: var(--gray-50); }
  body:not(.light):not(.dark) .auth-card { background: var(--gray-100); }
  body:not(.light):not(.dark) .auth-overlay { background: var(--gray-50); }
  body:not(.light):not(.dark) .icon-btn:hover { background: var(--gray-200); }
}

/* ─── Responsive ─── */
@media (min-width: 600px) {
  .trip-grid { grid-template-columns: repeat(4, 1fr); }
  .stats-grid { grid-template-columns: repeat(4, 1fr); }
  .screen { max-width: 600px; margin: 0 auto; }
}
