/* Baby Record - mobile-first styles.
   Designed for iPhone / iPad / Android. Uses safe-area insets so content
   clears the notch and home indicator. */

:root {
  --pink: #f7a8b8;
  --pink-dark: #e87f95;
  --bg: #fff7f9;
  --card: #ffffff;
  --text: #3a2c30;
  --muted: #8a7b80;
  --line: #f0dfe4;
  --blue: #7fb8d8;
  --green: #8fd6a8;
  --danger: #e06a6a;
  --shadow: 0 2px 10px rgba(180, 120, 140, 0.12);
  --radius: 16px;
  --tabbar-h: 60px;
}

/* ---------- Dark theme ---------- */
:root[data-theme="dark"] {
  --pink-dark: #f3a0b5;
  --bg: #16121a;
  --card: #221b27;
  --text: #f0e8ee;
  --muted: #a89aa8;
  --line: #34293a;
  --danger: #e87a7a;
  --shadow: 0 2px 12px rgba(0, 0, 0, 0.45);
}
:root[data-theme="dark"] input,
:root[data-theme="dark"] select,
:root[data-theme="dark"] textarea,
:root[data-theme="dark"] .btn.secondary,
:root[data-theme="dark"] .seg,
:root[data-theme="dark"] .action-btn,
:root[data-theme="dark"] .cat-chip { background: #2c2430; }
:root[data-theme="dark"] .seg button.active { background: #3a3040; }
:root[data-theme="dark"] .action-btn:active { background: #34293a; }
:root[data-theme="dark"] .tabbar { background: #1d1722; box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.45); }
:root[data-theme="dark"] .modal,
:root[data-theme="dark"] .viewer-info { background: var(--card); color: var(--text); }
:root[data-theme="dark"] .entry-card,
:root[data-theme="dark"] .chart { background: #1d1722; }
:root[data-theme="dark"] .chip,
:root[data-theme="dark"] .vi-link { background: #3a2630; }
:root[data-theme="dark"] .item-thumb,
:root[data-theme="dark"] .avatar,
:root[data-theme="dark"] .gallery img { background: #2c2430; }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC",
    "Microsoft JhengHei", "Noto Sans TC", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: contain;
}

.app { min-height: 100vh; }

/* ---------- Layout ---------- */
.screen {
  padding: calc(env(safe-area-inset-top) + 12px) 16px
    calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
  max-width: 760px;
  margin: 0 auto;
  animation: fade 0.2s ease;
}
@keyframes fade { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; } }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 14px;
}
.topbar h1 { font-size: 1.3rem; margin: 0; }

/* ---------- Cards ---------- */
.card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 14px;
}
.card h2 { margin: 0 0 10px; font-size: 1.05rem; }
.muted { color: var(--muted); font-size: 0.85rem; }

.row { display: flex; align-items: center; gap: 10px; }
.row.between { justify-content: space-between; }
.dt-field { gap: 8px; }
.dt-field .dt-date { flex: 1 1 auto; min-width: 0; }
.tt-wrap { display: flex; align-items: center; gap: 4px; flex: 0 0 auto; }
.tt-wrap .tt-sel {
  width: auto; min-width: 60px; padding: 12px 6px; text-align: center;
  font-variant-numeric: tabular-nums;
}
.tt-sep { font-weight: 700; color: var(--muted); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid-metrics { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
@media (min-width: 560px) { .grid-metrics { grid-template-columns: repeat(4, 1fr); } }

/* ---------- Metric tiles ---------- */
.metric {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
  text-align: center;
}
.metric .ico { font-size: 1.6rem; }
.metric .val { font-size: 1.35rem; font-weight: 700; margin-top: 4px; }
.metric .lbl { color: var(--muted); font-size: 0.8rem; }
.metric .time { color: var(--muted); font-size: 0.72rem; margin-top: 2px; }

/* ---------- Forms ---------- */
label { display: block; font-size: 0.85rem; color: var(--muted); margin: 10px 0 4px; }
input, select, textarea {
  width: 100%;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  font-size: 1rem;
  background: #fff;
  color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--pink); border-color: var(--pink); }
textarea { min-height: 70px; resize: vertical; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 13px 18px;
  border: none;
  border-radius: 12px;
  background: var(--pink);
  color: #fff;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  width: 100%;
  transition: transform 0.05s ease, background 0.15s ease;
}
.btn:active { transform: scale(0.98); }
.btn.secondary { background: #fff; color: var(--text); border: 1px solid var(--line); }
.btn.blue { background: var(--blue); }
.btn.green { background: var(--green); color: #1c4a2d; }
.btn.danger { background: var(--danger); }
.btn.small { width: auto; padding: 10px 14px; font-size: 0.85rem; min-height: 40px; }
.btn:disabled { opacity: 0.5; }

.btn-row { display: flex; gap: 10px; margin-top: 12px; }
.btn-row .btn { flex: 1; }

.seg { display: flex; gap: 6px; background: #fbe9ee; padding: 4px; border-radius: 12px; }
.seg button {
  flex: 1; border: none; background: transparent; padding: 9px; border-radius: 9px;
  font-size: 0.9rem; color: var(--muted); cursor: pointer;
}
.seg button.active { background: #fff; color: var(--text); font-weight: 600; box-shadow: var(--shadow); }

.chip {
  display: inline-block; padding: 3px 9px; border-radius: 999px;
  font-size: 0.72rem; background: #fbe9ee; color: var(--pink-dark);
}
.chip.ocr { background: #e6f1f8; color: #3f7fa3; }
.chip.voice { background: #e9f7ee; color: #3f8a5b; }

/* ---------- List items ---------- */
.item {
  display: flex; align-items: center; justify-content: space-between;
  padding: 11px 0; border-bottom: 1px solid var(--line); gap: 10px;
}
.item:last-child { border-bottom: none; }
.item .meta { font-size: 0.78rem; color: var(--muted); }
.item-body { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; cursor: pointer; }
.item-thumb { width: 46px; height: 46px; border-radius: 8px; object-fit: cover; flex: none; background: #f3e6ea; }
.attach-thumb { max-width: 160px; border-radius: 10px; margin: 6px 0; display: block; }

/* ---------- Baby header (avatar + age) ---------- */
.baby-header { display: flex; align-items: center; gap: 12px; }
.avatar {
  width: 56px; height: 56px; border-radius: 50%; object-fit: cover;
  background: #f3e6ea; flex: none;
}
.avatar-ph { display: flex; align-items: center; justify-content: center; font-size: 1.9rem; }
.baby-name { font-size: 1.1rem; font-weight: 700; }

/* ---------- Quick-action grid (Log view) ---------- */
.action-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-top: 6px;
}
@media (min-width: 480px) { .action-grid { grid-template-columns: repeat(5, 1fr); } }
.action-btn {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 4px; padding: 12px 4px; min-height: 68px;
  border: 1px solid var(--line); border-radius: 14px;
  background: #fff; color: var(--text); font-size: 0.72rem; cursor: pointer;
  transition: transform 0.05s ease, background 0.15s ease;
}
.action-btn:active { transform: scale(0.96); background: #fdeef2; }
.action-btn .ai { font-size: 1.5rem; }

/* ---------- Charts ---------- */
.chart { display: block; width: 100%; height: auto; background: #fffdfe; border-radius: 10px; }

/* ---------- Gallery ---------- */
.cat-chips {
  display: flex; gap: 6px; overflow-x: auto; padding-bottom: 8px; margin-bottom: 10px;
  -webkit-overflow-scrolling: touch;
}
.cat-chip {
  flex: none; padding: 7px 12px; border-radius: 999px; border: 1px solid var(--line);
  background: #fff; color: var(--text); font-size: 0.8rem; cursor: pointer; white-space: nowrap;
}
.cat-chip.active { background: var(--pink); color: #fff; border-color: var(--pink); }

.gallery { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
@media (min-width: 480px) { .gallery { grid-template-columns: repeat(4, 1fr); } }
@media (min-width: 720px) { .gallery { grid-template-columns: repeat(6, 1fr); } }
.gallery img {
  width: 100%; aspect-ratio: 1 / 1; object-fit: cover; border-radius: 10px;
  background: #f3e6ea; cursor: pointer;
}

/* ---------- Full-screen photo viewer ---------- */
/* The image fills the whole screen; the top bar, nav arrows and info sheet
   float on top as overlays so the photo is always large and centred. */
.viewer { position: fixed; inset: 0; z-index: 90; background: #000; }
.viewer-img-wrap {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  overflow: hidden; touch-action: none;
}
.viewer-img {
  width: 100%; height: 100%; object-fit: contain;
  user-select: none; -webkit-user-drag: none; transform-origin: center center; will-change: transform;
}
.viewer-top {
  position: absolute; top: 0; left: 0; right: 0; z-index: 2; pointer-events: none;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) 14px 24px; color: #fff;
  background: linear-gradient(to bottom, rgba(0, 0, 0, 0.55), rgba(0, 0, 0, 0));
}
.viewer-top > * { pointer-events: auto; }
.viewer-counter { font-size: 0.9rem; opacity: 0.95; text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5); }
.viewer-x {
  background: rgba(0, 0, 0, 0.4); color: #fff; border: 1px solid rgba(255, 255, 255, 0.25);
  width: 36px; height: 36px; border-radius: 50%; font-size: 1.05rem; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.viewer-nav {
  position: absolute; top: 50%; transform: translateY(-50%); z-index: 2;
  width: 44px; height: 44px; border-radius: 50%;
  background: rgba(0, 0, 0, 0.38); color: #fff; border: 1px solid rgba(255, 255, 255, 0.25);
  font-size: 1.5rem; line-height: 1; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.viewer-nav.prev { left: 10px; }
.viewer-nav.next { right: 10px; }
.viewer-info {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 2;
  background: #fff; color: var(--text);
  padding: 14px 16px calc(env(safe-area-inset-bottom) + 14px);
  border-radius: 18px 18px 0 0; max-height: 48vh; overflow-y: auto;
  box-shadow: 0 -10px 30px rgba(0, 0, 0, 0.30);
}
.viewer-top, .viewer-info, .viewer-nav { transition: opacity 0.2s ease; }
.viewer.immersive .viewer-top,
.viewer.immersive .viewer-info,
.viewer.immersive .viewer-nav { opacity: 0; pointer-events: none; }
.vi-date { font-weight: 700; font-size: 0.98rem; }
.vi-link {
  display: inline-block; margin-top: 8px; font-size: 0.85rem;
  background: #fbe9ee; color: var(--pink-dark); padding: 4px 11px; border-radius: 999px;
}
.vi-caption { flex: 1; }
.vi-meta { color: var(--muted); font-size: 0.78rem; margin-top: 8px; }

/* ---------- Camera ---------- */
.camera-wrap { position: relative; background: #000; border-radius: var(--radius); overflow: hidden; }
.camera-wrap video, .camera-wrap canvas, .camera-wrap img.preview {
  width: 100%; display: block; max-height: 70vh; object-fit: contain; background: #000;
}
.preview-fallback {
  display: flex; align-items: center; justify-content: center; min-height: 180px;
  color: #fff; background: #000; font-size: 0.9rem; text-align: center; padding: 20px;
}
.camera-controls {
  position: absolute; bottom: 12px; left: 0; right: 0;
  display: flex; align-items: center; justify-content: center; gap: 18px;
  z-index: 2; /* stay above the .preview-mirror canvas overlay (z-index:1) */
}
.shutter {
  width: 64px; height: 64px; border-radius: 50%; border: 4px solid #fff;
  background: var(--pink); cursor: pointer;
}
.cam-icon-btn {
  width: 44px; height: 44px; border-radius: 50%; border: none;
  background: rgba(255,255,255,0.85); font-size: 1.2rem; cursor: pointer;
}
.zoom-wrap {
  position: absolute; left: 16px; right: 16px; bottom: 84px; z-index: 2;
  display: flex; flex-direction: column; gap: 8px;
}
.zoom-wrap.hidden { display: none; }
.zoom-chips { display: flex; justify-content: center; gap: 8px; }
.zoom-chips.hidden { display: none; }
.zoom-chip {
  min-width: 40px; height: 30px; border-radius: 999px; border: none;
  background: rgba(0,0,0,0.5); color: #fff; font-size: 0.8rem; font-weight: 600;
  cursor: pointer; font-variant-numeric: tabular-nums;
}
.zoom-chip.active { background: var(--pink); }
.zoom-row {
  display: flex; align-items: center; gap: 10px;
  background: rgba(0,0,0,0.35); border-radius: 999px; padding: 6px 14px;
}
.zoom-label { color: #fff; font-size: 0.8rem; min-width: 42px; text-align: center; font-variant-numeric: tabular-nums; }
.zoom-slider { flex: 1; accent-color: #fff; height: 4px; }
.lens-wrap {
  position: absolute; top: 12px; left: 0; right: 0; z-index: 2;
  display: flex; justify-content: center; gap: 8px;
}
.lens-wrap.hidden { display: none; }
.lens-chip {
  min-width: 40px; height: 34px; padding: 0 8px; border-radius: 999px; border: none;
  background: rgba(0,0,0,0.45); color: #fff; font-size: 0.82rem; font-weight: 600; cursor: pointer;
  font-variant-numeric: tabular-nums;
}
.lens-chip.active { background: var(--pink); }
.lens-edit { background: rgba(255,255,255,0.28); }
.lens-editor {
  position: absolute; top: 54px; left: 0; right: 0; z-index: 2;
  display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; padding: 0 8px;
}
.lens-editor.hidden { display: none; }
.lens-opt {
  min-width: 40px; height: 30px; border-radius: 999px; border: none;
  background: rgba(0,0,0,0.6); color: #fff; font-size: 0.78rem; font-weight: 600; cursor: pointer;
}
.camera-wrap.cam-busy::after {
  content: '⏳'; position: absolute; inset: 0; z-index: 3;
  display: flex; align-items: center; justify-content: center;
  font-size: 2rem; background: rgba(0,0,0,0.5);
}

/* ---------- Tab bar ---------- */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 50;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: flex; background: #fff; border-top: 1px solid var(--line);
  box-shadow: 0 -2px 10px rgba(180,120,140,0.08);
}
.tabbar button {
  flex: 1; border: none; background: transparent; color: var(--muted);
  font-size: 0.68rem; cursor: pointer; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 2px;
}
.tabbar button .ti { font-size: 1.35rem; }
.tabbar button.active { color: var(--pink-dark); }

/* ---------- Splash / centred ---------- */
.splash, .centered {
  min-height: 100vh; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 10px; color: var(--muted);
}
.splash-logo { font-size: 3rem; }

.auth-card { max-width: 380px; margin: 0 auto; }
.brand { text-align: center; margin-bottom: 18px; }
.brand .logo { font-size: 3rem; }
.brand h1 { margin: 6px 0 0; }

/* ---------- Toast ---------- */
#toast {
  position: fixed; left: 50%;
  bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 24px);
  transform: translateX(-50%); background: #3a2c30; color: #fff;
  padding: 10px 18px; border-radius: 999px; font-size: 0.85rem; z-index: 100;
  opacity: 0; transition: opacity 0.2s ease; pointer-events: none; max-width: 90%;
}
#toast.show { opacity: 0.96; }
#toast.error { background: var(--danger); }

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,0.55); z-index: 80;
  display: flex; align-items: center; justify-content: center;
  padding: max(16px, env(safe-area-inset-top)) 14px max(16px, env(safe-area-inset-bottom));
}
.modal {
  background: #fff; border-radius: var(--radius); padding: 16px;
  max-width: 480px; width: 100%; max-height: 90vh; overflow: auto;
}
.modal img { width: 100%; border-radius: 12px; }

.entry-card { border: 1px solid var(--line); background: #fffdfe; }
.entry-card.excluded { opacity: 0.45; }

/* History day section headers */
.day-head {
  font-weight: 700; font-size: 0.9rem; color: var(--pink-dark);
  margin: 12px 0 4px; padding-top: 8px; border-top: 1px solid var(--line);
}
.day-head:first-child { border-top: none; padding-top: 0; margin-top: 0; }

/* ---------- Printable health report ---------- */
.report-overlay {
  position: fixed; inset: 0; z-index: 95; background: rgba(0, 0, 0, 0.55);
  display: flex; flex-direction: column; align-items: center; overflow-y: auto;
  padding: 16px 12px 90px;
}
.report {
  background: #fff; color: #222; width: 100%; max-width: 720px;
  border-radius: 12px; padding: 24px;
}
.report h1 { margin: 0 0 2px; font-size: 1.5rem; }
.report h3 { margin: 18px 0 4px; border-bottom: 2px solid #f0dfe4; padding-bottom: 4px; }
.report .rep-sub { color: #666; font-size: 0.85rem; margin: 0 0 8px; }
.report .rep-digest { background: #faf3f5; border-radius: 8px; padding: 10px 12px; font-size: 0.92rem; }
.report .rep-foot { color: #999; font-size: 0.75rem; margin-top: 18px; }
.report table { width: 100%; border-collapse: collapse; margin-top: 6px; font-size: 0.9rem; }
.report th, .report td { text-align: left; padding: 6px 8px; border-bottom: 1px solid #eee; }
.report th { color: #888; font-weight: 600; }
.report-controls {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 96;
  display: flex; gap: 10px; padding: 12px 16px calc(env(safe-area-inset-bottom) + 12px);
  background: var(--card); box-shadow: 0 -6px 20px rgba(0, 0, 0, 0.25);
}
.report-controls .btn { flex: 1; }
@media print {
  body > *:not(.report-overlay) { display: none !important; }
  .report-overlay { position: absolute; inset: 0; background: #fff; padding: 0; overflow: visible; }
  .report { max-width: none; border-radius: 0; padding: 0; }
  .report-controls { display: none !important; }
}

/* ---------- Settings preference rows ---------- */
.pref-block { padding: 10px 0; border-bottom: 1px solid var(--line); }
.pref-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 0; border-bottom: 1px solid var(--line);
}
.card > .pref-block:last-child, .card > .pref-row:last-child { border-bottom: none; padding-bottom: 0; }
.pref-row .pref-label { font-weight: 600; font-size: 0.95rem; }
.pref-block > .pref-label { margin-bottom: 6px; color: var(--muted); font-weight: 600; font-size: 0.85rem; }

/* ---------- Reminders ---------- */
.due-card { border: 1.5px solid var(--pink); }
.switch { position: relative; display: inline-block; width: 44px; height: 26px; flex: none; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .slider { position: absolute; inset: 0; background: var(--line); border-radius: 999px; transition: 0.2s; cursor: pointer; }
.switch .slider::before {
  content: ''; position: absolute; width: 20px; height: 20px; left: 3px; top: 3px;
  background: #fff; border-radius: 50%; transition: 0.2s; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}
.switch input:checked + .slider { background: var(--pink); }
.switch input:checked + .slider::before { transform: translateX(18px); }

/* ---------- Checklist (vaccines / milestones) ---------- */
.check-done { color: var(--muted); text-decoration: line-through; }
.check-due { color: var(--danger); font-weight: 600; }

/* ---------- Voice recording ---------- */
.btn.recording { background: var(--danger); animation: rec-pulse 1s ease-in-out infinite; }
@keyframes rec-pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.6; } }

/* ---------- Fever indicator ---------- */
.temp-fever { color: var(--danger); font-weight: 700; }
.temp-warm { color: #d98a00; font-weight: 700; }

/* ---------- Live timer ---------- */
.timer-running { border: 1.5px solid var(--pink); }
.timer-type { font-weight: 700; }
.timer-clock {
  font-size: 1.7rem; font-weight: 800; color: var(--pink-dark);
  font-variant-numeric: tabular-nums; letter-spacing: 1px;
}

.hidden { display: none !important; }
.spinner {
  width: 26px; height: 26px; border: 3px solid var(--line);
  border-top-color: var(--pink); border-radius: 50%; animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
