:root {
  --bg: #f4f6f9;
  --bg2: #eef1f6;
  --card: #ffffff;
  --card2: #f8fafc;
  --border: #e2e8f0;
  --text: #1e293b;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-dark: #1e40af;
  --primary2: #3b82f6;
  --primary-soft: rgba(37, 99, 235, 0.1);
  --accent: #1d4ed8;
  --success: #16a34a;
  --danger: #dc2626;
  --radius: 16px;
  --shadow: 0 4px 20px rgba(15, 23, 42, 0.06);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --font: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }
html {
  margin: 0; padding: 0;
  background: var(--bg);
  height: auto;
  overflow-y: auto;
  overflow-x: hidden;
}
body {
  margin: 0; padding: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-tap-highlight-color: transparent;
}
img { max-width: 100%; display: block; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: none; background: none; }
.hidden { display: none !important; }

.app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  padding-top: calc(64px + var(--safe-t));
  padding-bottom: calc(72px + var(--safe-b));
}

/* Topbar */
.topbar {
  position: fixed;
  top: 0;
  left: max(0px, calc(50% - 240px));
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: min(480px, 100%);
  padding: calc(10px + var(--safe-t)) 16px 10px;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.04);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark {
  width: 40px; height: 40px; border-radius: 12px;
  background: var(--primary-soft);
  display: flex; align-items: center; justify-content: center;
  font-size: 1.3rem;
}
.brand-name { font-weight: 800; font-size: 0.95rem; line-height: 1.2; color: var(--text); }
.brand-sub { font-size: 0.72rem; color: var(--muted); }
.icon-btn {
  width: 40px; height: 40px; border-radius: 10px;
  background: var(--bg2); color: var(--text);
  font-size: 1.2rem; display: flex; align-items: center; justify-content: center;
}

/* Views */
.view { display: none; padding: 16px; }
.view.active { display: block; }

/* Streak */
.streak-card {
  background: linear-gradient(135deg, var(--primary-dark), var(--primary));
  color: #fff;
  border-radius: var(--radius);
  padding: 20px;
  text-align: center;
  margin-bottom: 16px;
  box-shadow: 0 8px 24px rgba(37, 99, 235, 0.25);
}
.streak-num { font-size: 2.8rem; font-weight: 800; line-height: 1; }
.streak-label { font-size: 0.95rem; opacity: 0.95; margin-top: 4px; }
.streak-hint { font-size: 0.8rem; opacity: 0.85; margin-top: 6px; }

/* Quick actions */
.quick-actions {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}
.qa-btn {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 6px;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  font-size: 0.75rem; font-weight: 600; color: var(--text);
  box-shadow: var(--shadow);
  transition: background 0.15s, transform 0.1s;
}
.qa-btn:active { transform: scale(0.96); background: var(--bg2); }
.qa-ico { font-size: 1.4rem; }

/* Cards */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}
.card-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 12px;
}
.card-head h2 { font-size: 1.05rem; margin: 0; color: var(--text); }
.link-btn {
  color: var(--primary);
  font-weight: 600; font-size: 0.9rem;
  padding: 4px 8px;
}
.muted { color: var(--muted); font-size: 0.88rem; line-height: 1.45; }

/* Entry list */
.entry-list { display: flex; flex-direction: column; gap: 8px; }
.entry-item {
  display: flex; gap: 12px; align-items: flex-start;
  padding: 12px;
  background: var(--bg2);
  border-radius: 12px;
  border: 1px solid transparent;
}
.entry-item:active { border-color: var(--border); }
.entry-ico {
  width: 40px; height: 40px; border-radius: 10px;
  background: var(--primary-soft);
  display: flex; align-items: center; justify-content: center;
  font-size: 1.2rem; flex-shrink: 0;
}
.entry-body { flex: 1; min-width: 0; }
.entry-title { font-weight: 700; font-size: 0.95rem; color: var(--text); }
.entry-meta { font-size: 0.78rem; color: var(--muted); margin-top: 2px; }
.entry-notes { font-size: 0.85rem; color: var(--text); margin-top: 4px; opacity: 0.9; }
.entry-actions { display: flex; gap: 4px; flex-shrink: 0; }
.entry-actions button {
  width: 32px; height: 32px; border-radius: 8px;
  background: var(--card); border: 1px solid var(--border);
  font-size: 0.85rem;
  display: flex; align-items: center; justify-content: center;
}
.empty-msg {
  text-align: center; color: var(--muted); font-size: 0.9rem;
  padding: 16px 8px; display: none;
}
.empty-msg.show { display: block; }

/* Form */
.fld {
  display: block; margin-bottom: 14px;
  font-size: 0.85rem; font-weight: 600; color: var(--muted);
}
.fld input, .fld select, .fld textarea {
  display: block; width: 100%; margin-top: 6px;
  padding: 12px 14px;
  background: var(--card);
  border: 1.5px solid var(--border);
  border-radius: 12px;
  color: var(--text);
  font-size: 1rem;
}
.fld input:focus, .fld select:focus, .fld textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
.photo-preview {
  margin: 8px 0 14px;
  border-radius: 12px; overflow: hidden;
  max-height: 180px;
}
.photo-preview img { width: 100%; object-fit: cover; max-height: 180px; }

/* Buttons */
.btn-primary, .btn-secondary, .btn-outline, .btn-danger {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 12px 16px; border-radius: 12px;
  font-weight: 700; font-size: 0.95rem;
  text-decoration: none;
}
.btn-primary {
  background: var(--primary); color: #fff;
  box-shadow: 0 4px 14px rgba(37, 99, 235, 0.3);
}
.btn-primary:active { opacity: 0.9; }
.btn-secondary { background: var(--primary-soft); color: var(--primary-dark); }
.btn-outline {
  background: var(--card); color: var(--text);
  border: 1.5px solid var(--border);
}
.btn-danger {
  background: #fef2f2; color: var(--danger);
  margin-top: 10px; width: 100%;
  border: 1px solid #fecaca;
}
.btn-block { width: 100%; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-row > * { flex: 1 1 auto; }

.filter-select {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px 10px;
  font-size: 0.85rem;
  color: var(--text);
}

.danger-zone { border-color: #fecaca; }

/* Bottom nav */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: max(0px, calc(50% - 240px));
  width: min(480px, 100%);
  display: flex;
  background: rgba(255, 255, 255, 0.97);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
  padding: 6px 8px calc(6px + var(--safe-b));
  z-index: 50;
  box-shadow: 0 -2px 12px rgba(15, 23, 42, 0.04);
}
.nav-item {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 4px;
  font-size: 0.65rem; font-weight: 600;
  color: var(--muted);
  border-radius: 10px;
}
.nav-item.active { color: var(--primary); background: var(--primary-soft); }
.nav-ico { font-size: 1.15rem; }

/* Toast */
.toast {
  position: fixed;
  bottom: calc(80px + var(--safe-b));
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: #fff;
  padding: 12px 20px;
  border-radius: 12px;
  font-size: 0.9rem; font-weight: 600;
  box-shadow: 0 8px 24px rgba(15, 23, 42, 0.2);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.25s;
  z-index: 100;
  max-width: 90%;
  text-align: center;
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* Person bar */
.person-bar {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 10px 16px 0;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.person-bar::-webkit-scrollbar { display: none; }
.person-chip {
  flex-shrink: 0;
  padding: 6px 14px;
  border-radius: 999px;
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 600;
  box-shadow: var(--shadow);
}
.person-chip.active {
  background: var(--primary-soft);
  border-color: var(--primary);
  color: var(--primary-dark);
}

.qa-row2 { margin-top: -8px; margin-bottom: 16px; }

.extra-fields {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  margin-bottom: 14px;
}
.extra-fields .fld:last-child { margin-bottom: 0; }

.check-row {
  display: flex !important;
  flex-direction: row !important;
  align-items: flex-start;
  gap: 10px;
  font-weight: 500 !important;
  color: var(--text) !important;
  cursor: pointer;
}
.check-row input { margin-top: 3px; flex-shrink: 0; width: auto !important; }

.entry-extra {
  font-size: 0.82rem;
  color: var(--primary);
  margin-top: 3px;
}
.entry-photo { margin-top: 6px; }
.entry-photo img {
  max-width: 100%;
  border-radius: 8px;
  max-height: 100px;
  object-fit: cover;
}
.pdf-badge {
  background: var(--primary-soft);
  color: var(--primary-dark);
  padding: 10px 14px;
  border-radius: 10px;
  font-weight: 600;
  font-size: 0.9rem;
}

/* Brand logo */
.brand-logo {
  width: 40px; height: 40px; border-radius: 12px;
  object-fit: cover; flex-shrink: 0;
}
.topbar-actions { display: flex; gap: 6px; align-items: center; }

/* Summary cards */
.summary-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 14px;
}
.sum-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 8px;
  text-align: center;
  box-shadow: var(--shadow);
}
.sum-val {
  font-size: 1.05rem;
  font-weight: 800;
  color: var(--primary-dark);
  line-height: 1.2;
}
.sum-lbl {
  font-size: 0.7rem;
  color: var(--muted);
  margin-top: 4px;
  font-weight: 600;
}

/* Gastos */
.gasto-total {
  font-size: 2rem;
  font-weight: 800;
  color: var(--primary-dark);
  text-align: center;
  margin: 8px 0 4px;
}
.breakdown { margin-top: 14px; }
.break-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-top: 1px solid var(--border);
  font-size: 0.9rem;
  gap: 12px;
}
.break-row span { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.break-row strong { color: var(--text); flex-shrink: 0; }

/* Cookie banner */
.cookie-banner {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 200;
  padding: 12px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  background: rgba(15, 23, 42, 0.55);
  backdrop-filter: blur(4px);
}
.cookie-inner {
  max-width: 480px;
  margin: 0 auto;
  background: #fff;
  border-radius: 16px;
  padding: 16px 18px;
  box-shadow: 0 12px 40px rgba(15, 23, 42, 0.25);
  border: 1px solid var(--border);
}
.cookie-inner p {
  font-size: 0.85rem;
  color: var(--text);
  line-height: 1.45;
  margin: 0 0 12px;
}
.cookie-inner a { color: var(--primary); font-weight: 600; }
.cookie-actions { display: flex; justify-content: flex-end; }

/* Onboarding */
.onboarding {
  padding: 12px 16px 0;
}
.onb-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 20px;
  box-shadow: var(--shadow);
  text-align: center;
  margin-bottom: 12px;
}
.onb-ico { font-size: 2.2rem; margin-bottom: 8px; }
.onb-card h3 { font-size: 1.15rem; margin: 0 0 8px; color: var(--primary-dark); }
.onb-card p { color: var(--muted); font-size: 0.9rem; margin: 0 0 16px; line-height: 1.45; }
.onb-actions { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.onb-dots { display: flex; justify-content: center; gap: 6px; margin-top: 14px; }
.onb-dots span {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--border);
}
.onb-dots span.active { background: var(--primary); }

/* Alerts */
.alerts { padding: 0 16px; }
.alert-item {
  display: flex; gap: 10px; align-items: flex-start;
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  border-left: 4px solid var(--primary);
  border-radius: 12px;
  padding: 12px 14px;
  margin-bottom: 8px;
  font-size: 0.88rem;
  color: var(--text);
}
.alert-item.warn {
  background: #fffbeb;
  border-color: #fde68a;
  border-left-color: #f59e0b;
}
.alert-item .alert-dismiss {
  margin-left: auto;
  flex-shrink: 0;
  color: var(--muted);
  font-size: 1.1rem;
  padding: 0 4px;
}

/* Lightbox */
.lightbox {
  position: fixed; inset: 0; z-index: 300;
  background: rgba(15, 23, 42, 0.85);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.lightbox.hidden { display: none !important; }
.lightbox-close {
  position: absolute; top: 16px; right: 16px;
  width: 44px; height: 44px; border-radius: 50%;
  background: #fff; color: #0f172a;
  font-size: 1.6rem; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  z-index: 2;
}
.lightbox-body {
  max-width: 100%; max-height: 90vh;
  overflow: auto;
}
.lightbox-body img {
  max-width: 100%; max-height: 85vh;
  border-radius: 12px;
  margin: 0 auto;
}
.lightbox-body .pdf-full {
  background: #fff; color: var(--text);
  padding: 24px; border-radius: 12px; text-align: center;
  max-width: 360px; margin: 0 auto;
}
.lightbox-body iframe {
  width: min(100vw - 40px, 480px);
  height: 70vh;
  border: 0; border-radius: 12px; background: #fff;
}
.entry-photo { cursor: pointer; }
.entry-photo img:hover { opacity: 0.9; }

/* Offline bar */
.offline-bar {
  background: #1e293b; color: #fff;
  text-align: center; font-size: 0.8rem; font-weight: 600;
  padding: 6px 12px;
  position: sticky; top: 0; z-index: 60;
}

.gcal-box {
  background: #eff6ff;
  border: 1.5px solid #93c5fd;
  border-radius: 12px;
  padding: 14px;
  margin-top: 4px;
}
.gcal-box .btn-secondary {
  background: #dbeafe;
  color: #1e40af;
}
.extra-fields textarea {
  min-height: 100px;
  resize: vertical;
  line-height: 1.45;
}
#f-rec-detail {
  min-height: 120px;
}

.photo-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 8px;
}
.photo-act {
  width: 100%;
  padding: 12px 10px;
  font-size: 0.9rem;
}
.photo-preview .link-btn {
  display: block;
  margin-top: 8px;
  text-align: center;
}
@media (max-width: 380px) {
  .photo-actions { grid-template-columns: 1fr; }
}


/* Entry actions more visible */
.entry-item {
  flex-wrap: wrap;
}
.entry-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  width: 100%;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  justify-content: flex-end;
}
.entry-actions .act-btn,
.entry-actions button {
  width: auto !important;
  height: auto !important;
  min-height: 36px;
  padding: 8px 12px !important;
  border-radius: 10px !important;
  font-size: 0.82rem !important;
  font-weight: 600;
  background: var(--card) !important;
  border: 1px solid var(--border) !important;
  color: var(--text);
}
.entry-actions .act-edit {
  background: #eff6ff !important;
  border-color: #93c5fd !important;
  color: #1e40af !important;
}
.entry-actions .act-gcal {
  background: #ecfdf5 !important;
  border-color: #6ee7b7 !important;
  color: #065f46 !important;
}
.entry-actions .act-del {
  color: #b91c1c !important;
}

.gcal-title {
  font-weight: 800;
  font-size: 1rem;
  color: #1e40af;
  margin: 0 0 10px;
}
.gcal-box {
  margin-top: 12px;
}
