:root {
  --sienna: #b8860b;
  --sienna-deep: #96690a;
  --accent: #8b4513;
  --linen: #f5f5dc;
  --linen-2: #efe9cf;
  --paper: #fbf7ea;
  --ink: #2e2418;
  --ink-soft: #6b5a44;
  --line: #d9c9a8;
  --danger: #a33a26;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-top: env(safe-area-inset-top, 0px);
  --shadow-card: 0 2px 0 #d6c39d, 0 10px 22px -14px rgba(70, 48, 20, .55);
  --shadow-raised: 0 1px 0 #fff inset, 0 3px 0 #e0cfae, 0 14px 26px -18px rgba(70, 48, 20, .6);
  --font-hand: "Segoe Print", "Bradley Hand", "Comic Sans MS", "Chalkboard SE", cursive;
  --font-body: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
}

body {
  font-family: var(--font-body);
  color: var(--ink);
  background-color: var(--linen);
  background-image:
    repeating-linear-gradient(90deg, rgba(139, 69, 19, .035) 0 1px, transparent 1px 7px),
    repeating-linear-gradient(0deg, rgba(139, 69, 19, .03) 0 1px, transparent 1px 9px),
    radial-gradient(120% 60% at 50% 0%, #fdf9ea 0%, #f3ebcd 55%, #e9dfbc 100%);
  background-attachment: fixed;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

.app-shell {
  max-width: 520px;
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-bottom: calc(96px + var(--safe-bottom));
}

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  padding: calc(var(--safe-top) + 12px) 16px 12px;  background: linear-gradient(180deg, rgba(245, 245, 220, .98), rgba(245, 245, 220, .92));
  backdrop-filter: blur(6px);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 6px 16px -14px rgba(70, 48, 20, .7);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-mark {
  width: 44px;
  height: 40px;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  color: var(--accent);
  background: var(--linen-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow-card);
}

.brand-mark svg { width: 34px; height: 26px; }

.brand h1 {
  margin: 0;
  font-family: var(--font-hand);
  font-size: 22px;
  line-height: 1.05;
  letter-spacing: .3px;
  color: var(--accent);
}

.tagline {
  margin: 2px 0 0;
  font-size: 10.5px;
  letter-spacing: 1.6px;
  text-transform: uppercase;
  color: var(--ink-soft);
}

.ledger-tag {
  margin-left: auto;
  font-family: var(--font-hand);
  font-size: 13px;
  color: var(--sienna-deep);
  border: 1px dashed var(--sienna);
  border-radius: 999px;
  padding: 3px 10px;
  transform: rotate(2deg);
}

/* ---------- Views ---------- */
.views { flex: 1; padding: 16px 14px 28px; }

.view { animation: page-in .22s ease-out; }
.view[hidden] { display: none; }

@keyframes page-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

.page-head { margin: 2px 2px 12px; }

.page-title {
  margin: 0;
  font-family: var(--font-hand);
  font-size: 27px;
  font-weight: 600;
  color: var(--accent);
  letter-spacing: .2px;
}

.page-title::after {
  content: "";
  display: block;
  width: 54px;
  height: 3px;
  margin-top: 5px;
  border-radius: 2px;
  background: linear-gradient(90deg, var(--sienna), rgba(184, 134, 11, 0));
}

.page-note {
  margin: 6px 0 0;
  font-size: 12.5px;
  color: var(--ink-soft);
  font-style: italic;
}

.search-wrap { margin-bottom: 14px; }

.field {
  width: 100%;
  font-family: var(--font-body);
  font-size: 16px;
  color: var(--ink);
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 11px 12px;
  box-shadow: inset 0 1px 2px rgba(70, 48, 20, .08);
  transition: border-color .15s, box-shadow .15s;
}

.field:focus {
  outline: none;
  border-color: var(--sienna);
  box-shadow: 0 0 0 3px rgba(184, 134, 11, .2), inset 0 1px 2px rgba(70, 48, 20, .06);
}

.search-field { padding-left: 36px; }

.search-wrap { position: relative; }
.search-wrap::before {
  content: "⌕";
  position: absolute;
  left: 12px;
  top: 9px;
  font-size: 18px;
  color: var(--sienna-deep);
  pointer-events: none;
}

/* ---------- Segmented logbook grid ---------- */
.logbook {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  align-items: start;
  border-radius: 14px;
  padding: 2px;
  background:
    linear-gradient(90deg, transparent calc(50% - 1px), rgba(139, 69, 19, .18) calc(50% - 1px), rgba(139, 69, 19, .18) calc(50% + 1px), transparent calc(50% + 1px));
}

.log-card {
  position: relative;
  display: flex;
  flex-direction: column;
  min-height: 168px;
  padding: 10px 10px 9px;
  background: var(--paper);
  border: 1px solid #e2d3b2;
  border-radius: 4px 4px 8px 4px;
  box-shadow: var(--shadow-card);
  background-image: repeating-linear-gradient(0deg, transparent 0 25px, rgba(139, 69, 19, .055) 25px 26px);
}

.log-card::after {
  content: "";
  position: absolute;
  right: 0;
  bottom: 0;
  border-style: solid;
  border-width: 0 0 14px 14px;
  border-color: transparent transparent #e7d8b6 transparent;
  border-radius: 0 0 7px 0;
}

.log-card.pinned {
  border-color: var(--sienna);
  box-shadow: 0 2px 0 #d6b96f, 0 0 0 2px rgba(184, 134, 11, .16), 0 12px 24px -16px rgba(70, 48, 20, .6);
}

.log-card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}

.log-index {
  font-family: var(--font-hand);
  font-size: 11px;
  color: var(--sienna-deep);
  letter-spacing: .5px;
}

.pin-btn {
  border: none;
  background: transparent;
  font-size: 17px;
  line-height: 1;
  color: #c9b98f;
  padding: 2px 4px;
  cursor: pointer;
  border-radius: 6px;
}

.pin-btn[aria-pressed="true"] { color: var(--sienna); text-shadow: 0 1px 0 #fff; }

.log-title {
  margin: 0 0 8px;
  font-size: 15px;
  line-height: 1.25;
  font-weight: 700;
  color: var(--ink);
  overflow-wrap: anywhere;
}

.log-meta {
  display: flex;
  gap: 10px;
  margin: 0 0 6px;
}

.log-meta div {
  flex: 1;
  border-top: 1px dashed rgba(139, 69, 19, .3);
  padding-top: 3px;
}

.log-meta dt {
  font-family: var(--font-hand);
  font-size: 10.5px;
  color: var(--sienna-deep);
}

.log-meta dd {
  margin: 1px 0 0;
  font-size: 13px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.log-mod {
  margin: 0 0 8px;
  font-size: 11.5px;
  line-height: 1.4;
  color: var(--ink-soft);
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
  overflow-wrap: anywhere;
}

.log-mod.is-empty { font-style: italic; opacity: .6; }

.log-card-actions {
  display: flex;
  gap: 4px;
  margin-top: auto;
  padding-top: 6px;
}

/* ---------- Buttons ---------- */
.btn {
  font-family: var(--font-body);
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
  background: linear-gradient(180deg, #fdf9ee, #f1e7cd);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 12px;
  cursor: pointer;
  box-shadow: 0 2px 0 #d8c6a2;
  transition: transform .08s, box-shadow .08s;
}

.btn:active { transform: translateY(1px); box-shadow: 0 1px 0 #d8c6a2; }

.btn.primary {
  color: #fffaf0;
  background: linear-gradient(180deg, var(--sienna), var(--sienna-deep));
  border-color: var(--sienna-deep);
  box-shadow: 0 2px 0 #6f4d05;
}

.btn.primary:active { box-shadow: 0 1px 0 #6f4d05; }

.btn.ghost { background: transparent; box-shadow: none; border-style: dashed; }
.btn.danger { color: var(--danger); }
.btn.tiny { font-size: 11px; padding: 5px 6px; flex: 1; box-shadow: 0 1.5px 0 #e0cfae; }

/* ---------- Stacked / history list ---------- */
.stack { display: flex; flex-direction: column; gap: 12px; }

.hist-card {
  position: relative;
  padding: 12px 13px 13px;
  background: var(--paper);
  border: 1px solid #e2d3b2;
  border-left: 4px solid var(--sienna);
  border-radius: 4px 10px 10px 4px;
  box-shadow: var(--shadow-raised);
}

.hist-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}

.hist-title {
  margin: 0;
  font-family: var(--font-hand);
  font-size: 16px;
  color: var(--ink);
  line-height: 1.25;
}

.hist-rating {
  flex: 0 0 auto;
  font-size: 12px;
  font-weight: 700;
  color: var(--accent);
  background: var(--linen);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px 8px;
  font-variant-numeric: tabular-nums;
}

.hist-mod {
  margin: 8px 0 6px;
  font-size: 13.5px;
  line-height: 1.5;
  color: #3d3226;
  border-left: 2px solid rgba(184, 134, 11, .45);
  padding-left: 9px;
}

.hist-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 12px;
  font-size: 11px;
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
}

.hist-meta button {
  background: none;
  border: none;
  padding: 0;
  color: var(--sienna-deep);
  text-decoration: underline;
  font: inherit;
  cursor: pointer;
}

.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 9px; }

.chip {
  font-size: 11px;
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: rgba(251, 247, 234, .8);
  color: var(--ink-soft);
  cursor: pointer;
}

.chip[aria-pressed="true"] {
  background: var(--sienna);
  border-color: var(--sienna-deep);
  color: #fffaf0;
}

/* ---------- Favorites grid ---------- */
.fav-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.fav-card {
  position: relative;
  padding: 12px 11px;
  background: linear-gradient(180deg, #fdfaf0, #f6eed8);
  border: 1px solid #dcc79c;
  border-radius: 10px;
  box-shadow: var(--shadow-raised);
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 150px;
}

.fav-card::before {
  content: "★";
  position: absolute;
  top: -9px;
  left: 10px;
  width: 22px;
  height: 22px;
  display: grid;
  place-items: center;
  font-size: 12px;
  color: #fffaf0;
  background: var(--sienna);
  border: 1px solid var(--sienna-deep);
  border-radius: 50% 50% 50% 0;
  box-shadow: 0 2px 4px rgba(70, 48, 20, .3);
}

.fav-title {
  margin: 6px 0 0;
  font-family: var(--font-hand);
  font-size: 16px;
  line-height: 1.25;
  color: var(--accent);
}

.fav-stars { font-size: 12px; letter-spacing: 2px; color: var(--sienna); }
.fav-note { font-size: 11.5px; color: var(--ink-soft); line-height: 1.4; flex: 1; }
.fav-serves { font-size: 11px; color: var(--ink-soft); font-variant-numeric: tabular-nums; }

.fav-actions { display: flex; gap: 5px; }

/* ---------- Form ---------- */
.recipe-card {
  background: var(--paper);
  border: 1px solid #e2d3b2;
  border-radius: 6px;
  box-shadow: var(--shadow-raised);
  position: relative;
}

.recipe-card::before {
  content: "";
  position: absolute;
  inset: 5px 5px auto;
  height: 1px;
  background: repeating-linear-gradient(90deg, rgba(139, 69, 19, .28) 0 6px, transparent 6px 11px);
  pointer-events: none;
}

.form-card { padding: 18px 15px 16px; }

.field-label {
  display: block;
  margin: 14px 0 6px;
  font-family: var(--font-hand);
  font-size: 15px;
  color: var(--accent);
}

.form-card > .field-label:first-of-type { margin-top: 4px; }

.req { color: var(--danger); font-family: var(--font-body); }
.opt { font-family: var(--font-body); font-size: 10px; text-transform: uppercase; letter-spacing: .8px; color: var(--ink-soft); }

.field-error {
  margin: 5px 0 0;
  font-size: 11.5px;
  color: var(--danger);
  min-height: 0;
}

.field-error:empty { display: none; }

.field.invalid { border-color: var(--danger); box-shadow: 0 0 0 3px rgba(163, 58, 38, .14); }

textarea.field { resize: vertical; min-height: 74px; line-height: 1.45; }

.hint { margin: 5px 0 0; font-size: 11px; color: var(--ink-soft); font-style: italic; }

.stepper { display: flex; align-items: stretch; gap: 8px; }

.stepper-btn {
  width: 48px;
  flex: 0 0 auto;
  font-size: 22px;
  line-height: 1;
  color: var(--accent);
  background: linear-gradient(180deg, #fdf9ee, #efe4c7);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 2px 0 #d8c6a2;
  cursor: pointer;
}

.stepper-btn:active { transform: translateY(1px); box-shadow: 0 1px 0 #d8c6a2; }

.stepper-input { flex: 1; min-width: 0; text-align: center; font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; }

.rating-field { border: none; margin: 16px 0 0; padding: 0; }

.rating-field legend { padding: 0; margin-bottom: 8px; }

.rating { display: flex; gap: 6px; }

.rating button {
  flex: 1;
  min-height: 46px;
  font-size: 16px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--ink-soft);
  background: linear-gradient(180deg, #fdfaf0, #f0e6cc);
  border: 1px solid var(--line);
  border-radius: 9px;
  cursor: pointer;
  transition: background .12s, color .12s, transform .08s;
}

.rating button.on {
  color: #fffaf0;
  background: linear-gradient(180deg, var(--sienna), var(--sienna-deep));
  border-color: var(--sienna-deep);
  box-shadow: 0 2px 0 #6f4d05;
  transform: translateY(-1px);
}

.rating-caption { margin: 7px 0 0; font-size: 11.5px; color: var(--sienna-deep); font-style: italic; min-height: 15px; }

.toggle-line {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 16px 0 0;
  padding: 10px 11px;
  font-size: 13.5px;
  background: rgba(245, 245, 220, .7);
  border: 1px dashed var(--line);
  border-radius: 9px;
  cursor: pointer;
}

.toggle-line input { position: absolute; opacity: 0; width: 1px; height: 1px; }

.toggle-box {
  width: 22px;
  height: 22px;
  flex: 0 0 auto;
  border: 1.5px solid var(--sienna);
  border-radius: 5px;
  background: var(--paper);
  position: relative;
}

.toggle-line input:checked + .toggle-box { background: var(--sienna); }

.toggle-line input:checked + .toggle-box::after {
  content: "";
  position: absolute;
  left: 6px;
  top: 2px;
  width: 6px;
  height: 12px;
  border: solid #fffaf0;
  border-width: 0 2px 2px 0;
  transform: rotate(42deg);
}

.toggle-line input:focus-visible + .toggle-box { box-shadow: 0 0 0 3px rgba(184, 134, 11, .3); }

.form-actions { display: flex; gap: 9px; margin-top: 18px; }
.form-actions .primary { flex: 2; padding: 13px; font-size: 15px; }
.form-actions .ghost { flex: 1; }

.form-banner {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 9px 8px 9px 11px;
  margin: 0 0 6px;
  font-size: 12.5px;
  line-height: 1.4;
  border-radius: 8px;
  background: #f6e9c9;
  border: 1px solid #e0c78e;
  color: var(--accent);
}

.form-banner[hidden] { display: none; }

.form-banner > span { flex: 1; min-width: 0; }

.banner-dismiss {
  flex: 0 0 auto;
  width: 24px;
  height: 24px;
  margin-top: -2px;
  border: none;
  border-radius: 50%;
  background: rgba(139, 69, 19, .08);
  color: var(--sienna-deep);
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
}

/* ---------- Empty states ---------- */
.empty-state { padding: 14px 0 6px; }

.empty-card {
  text-align: center;
  padding: 28px 20px;
  background: rgba(251, 247, 234, .75);
  border: 1px dashed #cdb98e;
  border-radius: 12px;
}

.empty-mark {
  width: 52px;
  height: 52px;
  margin: 0 auto 10px;
  display: grid;
  place-items: center;
  font-family: var(--font-hand);
  font-size: 26px;
  color: var(--sienna-deep);
  background: var(--linen);
  border: 1px solid var(--line);
  border-radius: 50%;
  box-shadow: var(--shadow-card);
}

.empty-card h3 { margin: 0 0 6px; font-family: var(--font-hand); font-size: 19px; color: var(--accent); }
.empty-card p { margin: 0 auto 14px; max-width: 30ch; font-size: 12.5px; line-height: 1.5; color: var(--ink-soft); }

/* ---------- Tab bar ---------- */
.tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 30;
  display: flex;
  max-width: 520px;
  margin: 0 auto;
  padding: 7px 8px calc(7px + var(--safe-bottom));
  background: linear-gradient(180deg, #f8f3e1, #efe7cd);
  border-top: 1px solid var(--line);
  box-shadow: 0 -8px 20px -16px rgba(70, 48, 20, .8);
}

.tab {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 2px;
  border: none;
  background: transparent;
  color: #8a7758;
  border-radius: 10px;
  cursor: pointer;
  min-width: 0;
}

.tab-glyph { font-size: 17px; line-height: 1; }

.tab-label {
  font-size: 10px;
  letter-spacing: .2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.tab[aria-current="page"] {
  color: var(--accent);
  background: linear-gradient(180deg, #fdf8e8, #f3e8cf);
  box-shadow: 0 2px 0 #ddcaa4;
}

.tab[aria-current="page"] .tab-glyph { color: var(--sienna); transform: translateY(-1px); }
.tab[aria-current="page"] .tab-label { font-weight: 700; }

/* ---------- Sheet ---------- */
.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(46, 36, 24, .42);
  animation: fade-in .18s ease-out;
}

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 50;
  max-width: 520px;
  max-height: 82vh;
  margin: 0 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px 16px calc(24px + var(--safe-bottom));
  background: var(--paper);
  border-radius: 18px 18px 0 0;
  box-shadow: 0 -14px 40px -20px rgba(46, 36, 24, .8);
  animation: sheet-up .24s cubic-bezier(.2, .8, .3, 1);
}

@keyframes sheet-up { from { transform: translateY(100%); } to { transform: none; } }

.sheet-grip { width: 40px; height: 4px; margin: 4px auto 12px; border-radius: 2px; background: #d8c7a2; }

.sheet h3 { margin: 0 0 4px; font-family: var(--font-hand); font-size: 22px; color: var(--accent); }

.sheet-sub { margin: 0 0 12px; font-size: 12px; color: var(--ink-soft); font-style: italic; }

.sheet-rows { display: grid; gap: 9px; margin: 12px 0; }

.sheet-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding-bottom: 8px;
  border-bottom: 1px dashed rgba(139, 69, 19, .25);
}

.sheet-row dt { font-family: var(--font-hand); font-size: 13.5px; color: var(--sienna-deep); }
.sheet-row dd { margin: 0; font-size: 14px; font-weight: 600; text-align: right; overflow-wrap: anywhere; }
.sheet-row dd.long { font-weight: 400; font-size: 13.5px; line-height: 1.45; }

.sheet-actions { display: flex; gap: 8px; margin-top: 16px; }
.sheet-actions .btn { flex: 1; padding: 11px; }

.sheet-close {
  width: 100%;
  margin-top: 9px;
  padding: 10px;
  border: none;
  background: transparent;
  color: var(--ink-soft);
  font-size: 13px;
  text-decoration: underline;
  cursor: pointer;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(84px + var(--safe-bottom));
  z-index: 60;
  transform: translateX(-50%);
  max-width: 88vw;
  padding: 10px 16px;
  font-size: 12.5px;
  color: #fffaf0;
  background: #4a3826;
  border: 1px solid #2e2418;
  border-radius: 999px;
  box-shadow: 0 10px 24px -14px rgba(0, 0, 0, .7);
  animation: toast-in .2s ease-out;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* ---------- Larger screens ---------- */
@media (min-width: 560px) {
  .logbook { gap: 14px; }
  .views { padding: 18px 18px 30px; }
}

@media (max-width: 340px) {
  .logbook, .fav-grid { grid-template-columns: 1fr; }
  .logbook { background: none; }
  .tab-label { font-size: 9px; }
}

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