:root {
  --bg: #F6F1EA;
  --bg-elevated: #FFFBF7;
  --card: #FFFFFF;
  --ink: #2C241E;
  --ink-soft: #6B5E55;
  --muted: #8A7E74;
  --line: #E8DFD6;
  --accent: #C45D3A;
  --accent-ink: #FFFFFF;
  --accent-soft: #F4E0D6;
  --star: #E0A14A;
  --star-off: #D9D0C7;
  --ok: #5B7F5A;
  --danger: #B54A3C;
  --danger-soft: #F8E4E0;
  --shadow: 0 10px 28px rgba(44, 36, 30, 0.07);
  --shadow-soft: 0 2px 10px rgba(44, 36, 30, 0.05);
  --radius: 22px;
  --radius-sm: 14px;
  --tap: 48px;
  --pad: 20px;
  --font: "SF Pro Text", "SF Pro Display", -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, "Helvetica Neue", sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Times New Roman", serif;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bot: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1A1714;
    --bg-elevated: #231F1B;
    --card: #2A2521;
    --ink: #F3EDE6;
    --ink-soft: #B3A89E;
    --muted: #8E837A;
    --line: #3A342F;
    --accent: #E08968;
    --accent-ink: #1A1714;
    --accent-soft: #3A2A24;
    --star: #E8B86A;
    --star-off: #4A433D;
    --ok: #8FBA88;
    --danger: #E07A6C;
    --danger-soft: #3A2421;
    --shadow: 0 10px 28px rgba(0, 0, 0, 0.28);
    --shadow-soft: 0 2px 10px rgba(0, 0, 0, 0.2);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
html { height: 100%; }
body {
  min-height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button, input, textarea, select {
  font: inherit;
  color: inherit;
}

img { max-width: 100%; display: block; }

h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.02em; }

#app {
  max-width: 720px;
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  position: relative;
}

/* Top bar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(14px + var(--safe-top)) var(--pad) 12px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
}

.topbar-sub { justify-content: flex-start; }
.topbar-sub h1 {
  font-size: 1.15rem;
  flex: 1;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 4px;
}
.topbar-spacer { width: var(--tap); flex: 0 0 var(--tap); }

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-icon {
  width: 40px; height: 40px; border-radius: 12px;
  box-shadow: var(--shadow-soft);
}
.brand h1 {
  font-family: var(--serif);
  font-size: 1.28rem;
  font-weight: 600;
  letter-spacing: -0.03em;
}
.subtitle {
  margin: 2px 0 0;
  font-size: 0.8rem;
  color: var(--ink-soft);
}

.icon-btn {
  width: var(--tap);
  height: var(--tap);
  border: 0;
  background: var(--card);
  color: var(--ink);
  border-radius: 16px;
  display: grid;
  place-items: center;
  cursor: pointer;
  box-shadow: var(--shadow-soft);
  flex: 0 0 var(--tap);
}
.icon-btn:active { transform: scale(0.97); }

.menu {
  position: absolute;
  top: calc(10px + var(--safe-top) + var(--tap));
  right: var(--pad);
  background: var(--card);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 6px;
  min-width: 220px;
  border: 1px solid var(--line);
  z-index: 30;
}
.menu button {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: 0;
  padding: 14px 14px;
  border-radius: 12px;
  cursor: pointer;
  font-size: 1rem;
}
.menu button:hover, .menu button:focus-visible { background: var(--accent-soft); }

/* Toolbar */
.toolbar {
  padding: 4px var(--pad) 8px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.search-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 0 14px;
  min-height: var(--tap);
  color: var(--muted);
}
.search-wrap input {
  border: 0;
  background: transparent;
  outline: none;
  width: 100%;
  min-height: 46px;
  color: var(--ink);
}
.search-wrap input::placeholder { color: var(--muted); }

.toolbar-row {
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
}

.seg {
  display: flex;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 3px;
  gap: 2px;
}
.seg-btn {
  border: 0;
  background: transparent;
  color: var(--ink-soft);
  min-height: 40px;
  padding: 0 14px;
  border-radius: 11px;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.92rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.seg-btn.is-active {
  background: var(--accent-soft);
  color: var(--accent);
}
.seg-btn:active { transform: scale(0.98); }

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.chip {
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink-soft);
  border-radius: 999px;
  padding: 10px 16px;
  min-height: 42px;
  white-space: nowrap;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9rem;
}
.chip.is-active {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--bg);
}

/* Meal cards */
.meal-grid, .meal-list {
  padding: 8px var(--pad) calc(120px + var(--safe-bot));
}

.meal-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.meal-card {
  display: flex;
  flex-direction: column;
  text-align: left;
  border: 0;
  padding: 0;
  background: var(--card);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow-soft);
  cursor: pointer;
  color: inherit;
  width: 100%;
  border: 1px solid color-mix(in srgb, var(--line) 70%, transparent);
}
.meal-card:active { transform: scale(0.985); }

.meal-thumb {
  position: relative;
  aspect-ratio: 1 / 0.92;
  background: var(--accent-soft);
  overflow: hidden;
}
.meal-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.meal-thumb.is-empty { display: grid; place-items: center; }
.thumb-placeholder { font-size: 2rem; opacity: 0.7; }

.fav-badge {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: color-mix(in srgb, var(--card) 88%, transparent);
  color: var(--accent);
  display: grid;
  place-items: center;
  font-size: 0.85rem;
  backdrop-filter: blur(8px);
}

.meal-info { padding: 10px 12px 14px; }
.meal-name {
  font-size: 0.98rem;
  line-height: 1.25;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.meal-meta {
  margin: 4px 0 6px;
  font-size: 0.78rem;
  color: var(--muted);
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.meal-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.meal-list .meal-card {
  flex-direction: row;
  align-items: stretch;
  min-height: 96px;
}
.meal-list .meal-thumb {
  width: 96px;
  flex: 0 0 96px;
  aspect-ratio: auto;
  min-height: 96px;
}
.meal-list .meal-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 12px 14px;
}
.meal-list .meal-name { -webkit-line-clamp: 1; font-size: 1.05rem; }

.stars { letter-spacing: 0.08em; color: var(--star-off); font-size: 0.95rem; line-height: 1; }
.stars .star.is-on { color: var(--star); }

.empty {
  grid-column: 1 / -1;
  text-align: center;
  padding: 48px 16px 24px;
  color: var(--ink-soft);
}
.empty-art { font-size: 3.2rem; margin-bottom: 8px; }
.empty h2 { color: var(--ink); font-family: var(--serif); font-weight: 600; margin-bottom: 8px; }
.empty p { margin: 0 auto; max-width: 28ch; }

/* FAB */
.fab {
  position: fixed;
  z-index: 25;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(20px + var(--safe-bot));
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 56px;
  padding: 0 22px 0 16px;
  border: 0;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 700;
  font-size: 1.02rem;
  box-shadow: 0 12px 28px color-mix(in srgb, var(--accent) 42%, transparent);
  cursor: pointer;
}
.fab:active { transform: translateX(-50%) scale(0.97); }
body[data-view="form"] .fab,
body[data-view="detail"] .fab,
body[data-view="settings"] .fab,
.fab[hidden] { display: none; }

/* Form */
.form {
  padding: 8px var(--pad) calc(40px + var(--safe-bot));
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.photo-block { display: flex; flex-direction: column; gap: 10px; }
.photo-previews {
  display: flex;
  gap: 10px;
  overflow-x: auto;
  padding-bottom: 2px;
}
.preview {
  position: relative;
  flex: 0 0 112px;
  width: 112px;
  height: 112px;
  border-radius: 16px;
  overflow: hidden;
  background: var(--accent-soft);
}
.preview img { width: 100%; height: 100%; object-fit: cover; }
.preview-remove {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 50%;
  background: color-mix(in srgb, var(--ink) 78%, transparent);
  color: var(--bg-elevated);
  font-size: 1.2rem;
  line-height: 1;
  cursor: pointer;
}
.photo-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.hint, .muted { margin: 0; color: var(--muted); font-size: 0.82rem; }

.field { display: flex; flex-direction: column; gap: 8px; }
fieldset.field {
  border: 0;
  margin: 0;
  padding: 0;
  min-inline-size: 0;
}
.field > span, .field > legend {
  font-size: 0.88rem;
  font-weight: 650;
  color: var(--ink-soft);
  padding: 0;
  float: none;
  width: auto;
}
.date-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
.date-row #field-date { flex: 1; }
.date-cal {
  position: relative;
  flex: 0 0 var(--tap);
  width: var(--tap);
  height: var(--tap);
  border-radius: 16px;
  border: 1px solid var(--line);
  background: var(--card);
  display: grid;
  place-items: center;
  color: var(--ink-soft);
  cursor: pointer;
}
.date-cal input[type="date"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
  width: 100%;
  height: 100%;
  border: 0;
  padding: 0;
}
.field input[type="text"],
.field input[type="date"],
.field input[type="number"],
.field textarea {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 16px;
  padding: 14px 16px;
  min-height: var(--tap);
  outline: none;
  resize: vertical;
}
.field textarea { min-height: 110px; line-height: 1.45; }
.field input:focus, .field textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent);
}

.star-picker {
  display: flex;
  gap: 6px;
  justify-content: space-between;
}
.star-btn {
  flex: 1;
  min-height: 64px;
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 16px;
  color: var(--star-off);
  font-size: 2rem;
  cursor: pointer;
  line-height: 1;
}
.star-btn.is-on { color: var(--star); background: color-mix(in srgb, var(--star) 14%, var(--card)); border-color: color-mix(in srgb, var(--star) 45%, var(--line)); }
.star-btn:active { transform: scale(0.96); }

.toggle-row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: var(--tap);
  cursor: pointer;
  user-select: none;
}
.toggle-row input { position: absolute; opacity: 0; pointer-events: none; }
.toggle-ui {
  width: 52px;
  height: 32px;
  border-radius: 999px;
  background: var(--line);
  position: relative;
  flex: 0 0 52px;
  transition: background 0.2s;
}
.toggle-ui::after {
  content: "";
  position: absolute;
  top: 3px; left: 3px;
  width: 26px; height: 26px;
  border-radius: 50%;
  background: var(--card);
  box-shadow: var(--shadow-soft);
  transition: transform 0.2s;
}
.toggle-row input:checked + .toggle-ui { background: var(--accent); }
.toggle-row input:checked + .toggle-ui::after { transform: translateX(20px); }
.toggle-row input:focus-visible + .toggle-ui { outline: 2px solid var(--accent); outline-offset: 3px; }

.form-error {
  margin: 0;
  color: var(--danger);
  background: var(--danger-soft);
  border-radius: 12px;
  padding: 12px 14px;
  font-weight: 600;
}
.form-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 52px;
  padding: 0 18px;
  border-radius: 16px;
  border: 1px solid transparent;
  font-weight: 700;
  cursor: pointer;
  text-align: center;
}
.btn:active { transform: scale(0.985); }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-secondary {
  background: var(--card);
  border-color: var(--line);
  color: var(--ink);
  font-weight: 650;
  min-height: 48px;
  font-size: 0.92rem;
}
.btn-ghost { background: transparent; color: var(--ink-soft); font-weight: 650; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger-ghost { background: transparent; color: var(--danger); font-weight: 650; }

/* Detail */
.detail { padding: 0 0 calc(28px + var(--safe-bot)); }
.gallery {
  margin: 0 var(--pad);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--accent-soft);
  aspect-ratio: 4 / 3;
  position: relative;
}
.gallery.is-empty { display: grid; place-items: center; color: var(--muted); }
.gallery-track {
  display: flex;
  overflow-x: auto;
  scroll-snap-type: x mandatory;
  height: 100%;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.gallery-track::-webkit-scrollbar { display: none; }
.gallery-track img {
  flex: 0 0 100%;
  width: 100%;
  height: 100%;
  object-fit: cover;
  scroll-snap-align: center;
}
.gallery-dots {
  position: absolute;
  left: 0; right: 0; bottom: 10px;
  display: flex;
  justify-content: center;
  gap: 6px;
}
.dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: color-mix(in srgb, #fff 45%, transparent);
}
.dot.is-on { background: #fff; }

.detail-card { padding: 20px var(--pad) 0; }
.detail-name { font-family: var(--serif); font-size: 1.7rem; font-weight: 600; letter-spacing: -0.03em; }
.detail-meta { margin: 6px 0 10px; color: var(--ink-soft); }
.detail-card .stars { font-size: 1.25rem; }
.detail-section { margin-top: 22px; }
.detail-section h3 {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 6px;
}
.detail-section p { margin: 0; white-space: pre-wrap; }
.detail-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 28px;
}

/* Toast & modal */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(88px + var(--safe-bot));
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--bg);
  padding: 12px 18px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 0.92rem;
  z-index: 50;
  box-shadow: var(--shadow);
  max-width: calc(100% - 40px);
  text-align: center;
}
body[data-view="form"] .toast,
body[data-view="detail"] .toast { bottom: calc(24px + var(--safe-bot)); }

.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: end center;
}
.modal[hidden] { display: none; }
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: color-mix(in srgb, #000 45%, transparent);
}
.modal-card {
  position: relative;
  width: min(100%, 520px);
  background: var(--card);
  border-radius: 24px 24px 0 0;
  padding: 24px var(--pad) calc(24px + var(--safe-bot));
  box-shadow: var(--shadow);
}
.modal-card h2 { font-family: var(--serif); font-size: 1.35rem; font-weight: 600; margin-bottom: 8px; }
.modal-card p { margin: 0 0 20px; color: var(--ink-soft); }
.modal-actions { display: flex; flex-direction: column; gap: 8px; }

@media (min-width: 640px) {
  .meal-grid { grid-template-columns: 1fr 1fr 1fr; }
  .modal { place-items: center; }
  .modal-card { border-radius: var(--radius); padding: 28px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

/* Sync status + author + settings */
.brand { align-items: flex-start; }
.brand > div { min-width: 0; flex: 1; }
.sync-status {
  display: inline-block;
  margin: 6px 0 0;
  font-size: 0.72rem;
  font-weight: 650;
  letter-spacing: 0.02em;
  color: var(--muted);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 8px;
  line-height: 1.3;
}
.sync-status[data-state="synced"] { color: var(--ok); }
.sync-status[data-state="syncing"] { color: var(--accent); }
.sync-status[data-state="error"] { color: var(--danger); }
.sync-status[data-state="offline"] { color: var(--muted); }

.author-tag {
  color: var(--ink-soft);
}

.settings-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 8px;
}
.settings-actions .btn-ghost {
  grid-column: 1 / -1;
  min-height: 44px;
}

#settings-code {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  letter-spacing: 0.04em;
}

#settings-key {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.85rem;
  min-height: 92px;
}
