:root {
  /* Warm, earthy palette — linen paper, moss, clay */
  --moss: #6d8a5f;
  --moss-deep: #55704a;
  --moss-tint: #eef1e8;
  --clay: #c0764e;
  --clay-tint: #f8efe6;
  --paper: #f6f2ea;
  --surface: #fffefb;
  --ink: #33302a;
  --ink-soft: #857c6d;
  --border: #e4ddd0;
  --brick: #ae5342;
  --radius: 16px;
  --shadow-soft: 0 2px 10px rgba(90, 74, 52, 0.06);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background-color: var(--paper);
  /* Barely-there warmth so large empty areas don't read as flat grey. */
  background-image:
    radial-gradient(circle at 12% 8%, rgba(192, 118, 78, 0.045), transparent 55%),
    radial-gradient(circle at 88% 92%, rgba(109, 138, 95, 0.05), transparent 55%);
  background-attachment: fixed;
  color: var(--ink);
  padding-bottom: env(safe-area-inset-bottom);
}

.hidden { display: none !important; }
.muted { color: var(--ink-soft); font-size: 0.9rem; }
.small { font-size: 0.8rem; }
.error { color: var(--brick); font-size: 0.9rem; }
.danger-text { color: var(--brick); }

.pill {
  display: inline-block;
  background: var(--moss);
  color: #fff;
  font-size: 0.75rem;
  padding: 3px 11px;
  border-radius: 999px;
  align-self: flex-start;
}
.pill.private { background: #6f6558; }

/* ---------- Auth screens ---------- */
.screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 34px 26px;
  width: 100%;
  max-width: 360px;
  box-shadow: 0 6px 26px rgba(90, 74, 52, 0.09);
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.auth-card h1 {
  margin: 0;
  text-align: center;
  font-size: 1.55rem;
  letter-spacing: -0.01em;
}
.auth-card > p { margin: 0; text-align: center; }
.auth-card label { display: flex; flex-direction: column; gap: 6px; font-size: 0.9rem; font-weight: 600; }
.auth-card input {
  padding: 12px;
  font-size: 1rem;
  border: 1px solid var(--border);
  border-radius: 11px;
  font-weight: 400;
  background: #fffdf9;
  color: var(--ink);
}
input:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 2px solid var(--moss);
  outline-offset: 1px;
}

/* ---------- Layout ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--paper);
  padding: 14px 16px 0;
  border-bottom: 1px solid var(--border);
}
.topbar-row { display: flex; align-items: center; justify-content: space-between; }
.topbar h1 {
  margin: 0 0 10px;
  font-size: 1.35rem;
  letter-spacing: -0.01em;
}
.icon-btn {
  background: none;
  border: none;
  font-size: 1.25rem;
  padding: 0 4px 10px;
  cursor: pointer;
  opacity: 0.75;
}
.tabs { display: flex; gap: 4px; }
.tab-btn {
  flex: 1;
  padding: 10px 4px;
  border: none;
  background: transparent;
  color: var(--ink-soft);
  font-weight: 600;
  font-size: 0.95rem;
  border-bottom: 3px solid transparent;
  cursor: pointer;
}
.tab-btn.active { color: var(--moss-deep); border-bottom-color: var(--moss); }

.view { padding: 16px; max-width: 900px; margin: 0 auto; }
.section-title { font-size: 1.05rem; margin: 26px 0 4px; letter-spacing: -0.01em; }
.section-title:first-child { margin-top: 0; }

/* ---------- Buttons ---------- */
button { font-family: inherit; cursor: pointer; }
button[type="submit"], #add-submit {
  background: var(--moss);
  color: #fff;
  border: none;
  padding: 14px;
  border-radius: 11px;
  font-size: 1rem;
  font-weight: 600;
  width: 100%;
}
button[type="submit"]:active { background: var(--moss-deep); }
button[type="submit"]:disabled { opacity: 0.55; }
button.secondary {
  background: var(--clay-tint);
  border: 1px solid #e8d5c4;
  color: #8a4f2e;
  padding: 12px 16px;
  border-radius: 11px;
  font-size: 0.95rem;
  font-weight: 600;
  width: 100%;
}
button.danger {
  background: var(--surface);
  color: var(--brick);
  border: 1px solid #e0c3bd;
  padding: 10px 16px;
  border-radius: 11px;
  font-weight: 600;
}
button.danger.full { width: 100%; margin-top: 12px; }
.link-btn {
  background: none;
  border: none;
  font-size: 0.85rem;
  padding: 4px;
  text-decoration: underline;
}

/* ---------- Filters ---------- */
.filters { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.filters select, .filters input {
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 11px;
  font-size: 0.85rem;
  background: var(--surface);
  color: var(--ink);
  max-width: 100%;
}
.filters input[type="search"] { flex: 1; min-width: 150px; }

/* ---------- Grid ---------- */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
}
.grid-item {
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  border: 1px solid var(--border);
  box-shadow: var(--shadow-soft);
  transition: transform 0.1s ease;
}
.grid-item:active { transform: scale(0.97); }
.thumb-wrap { position: relative; }
.thumb-wrap img {
  width: 100%;
  aspect-ratio: 1;
  object-fit: cover;
  display: block;
  background: #ece6da;
}
.badge {
  position: absolute;
  top: 6px;
  right: 6px;
  background: rgba(51, 48, 42, 0.72);
  color: #fff;
  font-size: 0.7rem;
  padding: 3px 8px;
  border-radius: 999px;
}
.badge.age {
  left: 6px;
  right: auto;
  background: rgba(192, 118, 78, 0.93);
  font-weight: 600;
}
.grid-item .meta { padding: 9px 11px 11px; }
.grid-item .meta .from { font-weight: 600; font-size: 0.9rem; }
.grid-item .meta .occasion { font-size: 0.78rem; color: var(--ink-soft); }

/* ---------- Forms ---------- */
.card-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
  background: var(--surface);
  padding: 20px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-soft);
}
.card-form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.9rem;
  font-weight: 600;
}
.card-form input, .card-form select, .card-form textarea {
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 11px;
  font-size: 1rem;
  font-family: inherit;
  font-weight: 400;
  background: #fffdf9;
  color: var(--ink);
}
.hint { font-size: 0.82rem; color: var(--ink-soft); margin: 8px 0 0; }
.card-form .hint { margin: -8px 0 0; }

.type-toggle {
  display: flex;
  gap: 6px;
  background: var(--moss-tint);
  padding: 4px;
  border-radius: 13px;
}
.type-btn {
  flex: 1;
  padding: 10px;
  border: none;
  background: transparent;
  border-radius: 10px;
  font-weight: 600;
  color: var(--ink-soft);
  font-size: 0.95rem;
}
.type-btn.active {
  background: var(--surface);
  color: var(--moss-deep);
  box-shadow: 0 1px 4px rgba(90, 74, 52, 0.12);
}

.photo-picker { cursor: pointer; }
.photo-picker input[type="file"] { display: none; }
.photo-preview {
  border: 2px dashed #d8cfbe;
  border-radius: var(--radius);
  aspect-ratio: 4/3;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--ink-soft);
  overflow: hidden;
  background: #f2ede3;
}
.photo-preview img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  transition: transform 0.2s ease;
}
/* A quarter turn puts the photo's long edge across the frame — shrink it so the
   whole card stays visible rather than running off the sides. */
.photo-preview img.quarter-turn { transform-origin: center; scale: 0.75; }

/* ---------- Strip of photos on one card ---------- */
.shot-strip {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 2px;
  -webkit-overflow-scrolling: touch;
}
.shot-strip.in-modal { padding: 12px 20px 0; }
.shot-thumb {
  position: relative;
  flex: 0 0 auto;
  width: 62px;
  height: 62px;
  padding: 0;
  border: 2px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: #ece6da;
}
.shot-thumb.active { border-color: var(--moss); }
.shot-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
/* The "+" tile that adds another photo — always last in the strip. */
.shot-thumb.add-tile {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  background: var(--moss-tint);
  border-style: dashed;
  border-color: var(--moss);
  color: var(--moss-deep);
}
.shot-thumb.add-tile .plus { font-size: 1.35rem; line-height: 1; font-weight: 600; }
.shot-thumb.add-tile .add-label { font-size: 0.62rem; font-weight: 600; letter-spacing: 0.02em; }
.shot-thumb.add-tile:active { background: #e3e9dc; }

.shot-thumb .num {
  position: absolute;
  bottom: 2px;
  right: 3px;
  background: rgba(51,48,42,0.75);
  color: #fff;
  font-size: 0.65rem;
  padding: 0 5px;
  border-radius: 999px;
}

.add-photo-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-weight: 600;
}

/* ---------- Gallery in the card view ---------- */
.detail-photo { position: relative; }
.nav-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(43, 37, 29, 0.55);
  color: #fff;
  border: none;
  width: 38px;
  height: 52px;
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
}
.nav-btn.prev { left: 0; border-radius: 0 8px 8px 0; }
.nav-btn.next { right: 0; border-radius: 8px 0 0 8px; }
.photo-counter {
  position: absolute;
  bottom: 8px;
  right: 10px;
  background: rgba(43, 37, 29, 0.7);
  color: #fff;
  font-size: 0.75rem;
  padding: 3px 9px;
  border-radius: 999px;
}

/* ---------- What the card said ---------- */
.reading-note {
  background: var(--moss-tint);
  color: var(--moss-deep);
  border-radius: 9px;
  padding: 9px 12px;
  font-size: 0.85rem;
}
.card-message {
  margin: 10px 0 0;
  padding: 12px 14px;
  background: #f4f0e6;
  border-left: 3px solid var(--clay);
  border-radius: 8px;
  font-style: italic;
  white-space: pre-wrap;
  font-size: 0.92rem;
}

.message-input {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fffdf9;
  color: var(--ink);
  font-family: inherit;
  font-size: 0.92rem;
  line-height: 1.5;
  resize: vertical;
}
#message-editor { margin-top: 10px; }
#message-block .link-btn { color: var(--moss-deep); padding: 6px 0 0; }

.rotate-row { display: flex; gap: 8px; }
.rotate-btn {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--ink);
  padding: 11px;
  border-radius: 11px;
  font-size: 0.9rem;
  font-weight: 600;
}
.rotate-btn:active { background: var(--moss-tint); }
.rotate-btn:disabled { opacity: 0.5; }

.age-preview {
  background: var(--moss-tint);
  border-left: 3px solid var(--moss);
  padding: 11px 13px;
  border-radius: 9px;
  font-size: 0.88rem;
  margin: 0;
  color: var(--moss-deep);
}
.age-preview.warn {
  background: #fbeeeb;
  border-left-color: var(--brick);
  color: var(--brick);
}

.switch-row {
  flex-direction: row !important;
  align-items: flex-start;
  gap: 12px !important;
  background: #f4f0e6;
  padding: 14px;
  border-radius: 12px;
  font-weight: 400 !important;
}
.switch-row input[type="checkbox"] {
  width: 22px;
  height: 22px;
  margin: 0;
  flex-shrink: 0;
  accent-color: var(--moss);
}
.switch-row strong { display: block; font-size: 0.92rem; }
.switch-row small { color: var(--ink-soft); font-size: 0.8rem; }

/* ---------- Lists (family screen) ---------- */
.list { display: flex; flex-direction: column; gap: 8px; margin: 12px 0; }
.list-row {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.list-row.editing { flex-direction: column; align-items: stretch; }
.person-actions { display: flex; gap: 4px; flex-shrink: 0; }
.person-editor { display: flex; flex-direction: column; gap: 8px; width: 100%; }
.person-editor input {
  padding: 11px;
  border: 1px solid var(--border);
  border-radius: 11px;
  font-size: 0.95rem;
  font-family: inherit;
  background: #fffdf9;
  color: var(--ink);
}
.person-editor .hint { margin: 2px 0 0; }

.inline-form { display: flex; gap: 8px; flex-wrap: wrap; }
.inline-form.stacked { flex-direction: column; }
.inline-form input {
  flex: 1;
  min-width: 120px;
  padding: 11px;
  border: 1px solid var(--border);
  border-radius: 11px;
  font-size: 0.95rem;
  font-family: inherit;
  background: var(--surface);
  color: var(--ink);
}
.inline-form button { width: auto; padding: 11px 20px; flex-shrink: 0; }
.inline-form.stacked button { width: 100%; }

/* ---------- Modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(43, 37, 29, 0.55);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 20;
}
.modal-content {
  background: var(--surface);
  width: 100%;
  max-width: 480px;
  border-radius: 20px 20px 0 0;
  max-height: 92vh;
  overflow-y: auto;
  position: relative;
  padding-bottom: 24px;
}
@media (min-width: 600px) {
  .modal { align-items: center; }
  .modal-content { border-radius: 20px; }
}
.modal-close {
  position: absolute;
  top: 10px;
  right: 10px;
  background: rgba(43, 37, 29, 0.55);
  color: #fff;
  border: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  font-size: 1rem;
  z-index: 2;
}
#detail-img { width: 100%; max-height: 58vh; object-fit: contain; background: #2b251d; display: block; }
.detail-fields { padding: 16px 20px 4px; display: flex; flex-direction: column; gap: 5px; }
.detail-head { display: flex; align-items: center; gap: 8px; justify-content: space-between; }
.detail-fields #detail-note { white-space: pre-wrap; margin-top: 8px; }
.detail-actions { display: flex; gap: 10px; padding: 16px 20px 0; }
.detail-actions button { flex: 1; }
