:root {
  --primary: #4e342e;
  --primary-deep: #3b2620;
  --secondary: #fff3e0;
  --accent: #ff7043;
  --paper: #fdf6e9;
  --paper-dark: #f3e6cf;
  --ink: #3b2620;
  --muted: #8a6f5f;
  --wood: #a9713f;
  --wood-dark: #7d4e26;
  --green: #66bb6a;
  --blue: #42a5f5;
  --shelf-h: 26px;
  --tab-h: 76px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --ribbon: #d84315;
}

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

html, body { height: 100%; }

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--secondary);
  color: var(--ink);
  overscroll-behavior: none;
}

.app {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  background:
    linear-gradient(180deg, #e8d5b5 0%, #f0e2c8 100%);
}

/* ---- Bookshelf backdrop ---- */
.shelf-bg {
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
}
.plank {
  position: absolute;
  left: -4%;
  right: -4%;
  height: var(--shelf-h);
  background:
    repeating-linear-gradient(90deg, rgba(0,0,0,.06) 0 40px, transparent 40px 70px),
    linear-gradient(180deg, var(--wood), var(--wood-dark));
  border-radius: 4px;
  box-shadow: 0 6px 10px rgba(0,0,0,.28), inset 0 2px 0 rgba(255,255,255,.25);
}
.plank::after {
  content: "";
  position: absolute;
  top: 6px; left: 8px; right: 8px; height: 3px;
  background: linear-gradient(90deg, transparent, rgba(0,0,0,.18), transparent);
}
.plank-a { top: 12%; }
.plank-b { top: 38%; }
.plank-c { top: 64%; }
.plank-d { top: 88%; }

/* ---- Journal (main view) ---- */
.journal {
  position: relative;
  z-index: 1;
  flex: 1;
  overflow-y: auto;
  padding-top: calc(14px + var(--safe-t));
  padding-bottom: calc(var(--tab-h) + var(--safe-b) + 16px);
  -webkit-overflow-scrolling: touch;
}

.journal-skin {
  position: relative;
  margin: 0 10px;
  background:
    radial-gradient(circle at 90% 10%, rgba(255,255,255,.6) 0, transparent 35%),
    linear-gradient(180deg, var(--paper), var(--paper-dark));
  border-radius: 8px 30px 30px 8px;
  box-shadow: 0 8px 22px rgba(0,0,0,.35), inset 0 0 0 6px rgba(123,63,25,.15);
  padding: 20px 18px 26px;
  min-height: calc(100vh - var(--tab-h) - 40px);
}

/* ribbon tie on the journal */
.journal-skin::before,
.journal-skin::after {
  content: "";
  position: absolute;
  top: -10px;
  width: 30px;
  height: 26px;
  background: var(--ribbon);
  z-index: 1;
}
.journal-skin::before { left: 26px; transform: rotate(-18deg); border-radius: 3px 3px 8px 3px; }
.journal-skin::after { right: 26px; transform: rotate(18deg); border-radius: 3px 3px 3px 8px; }

.page-break {
  position: absolute;
  top: 8px; bottom: 12px; left: 46%;
  width: 3px;
  background: linear-gradient(90deg, rgba(123,63,25,.25), rgba(123,63,25,.05));
  z-index: 0;
  transform: rotate(-1deg);
}

.journal > * { position: relative; z-index: 2; }

/* ---- Shared UI ---- */
h1 { font-size: 22px; color: var(--primary); margin-bottom: 2px; }
h2 { font-size: 16px; color: var(--primary); margin: 18px 0 8px; }
.eyebrow { font-size: 11px; letter-spacing: 1.5px; text-transform: uppercase; color: var(--accent); font-weight: 700; }
.sub { font-size: 13px; color: var(--muted); margin-bottom: 12px; }

.btn {
  border: none;
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  color: var(--secondary);
  background: linear-gradient(180deg, var(--accent), #ef5b2a);
  box-shadow: 0 4px 10px rgba(216,67,21,.35);
  transition: transform .08s ease;
}
.btn:active { transform: scale(.97); }
.btn.ghost { background: var(--primary); box-shadow: 0 4px 10px rgba(0,0,0,.2); }
.btn-row { display: flex; gap: 10px; margin: 14px 0; }
.btn-row .btn { flex: 1; }

/* ---- Mugs (Home counters) ---- */
.mugs { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 6px; }
.mug {
  position: relative;
  border-radius: 0 0 10px 10px;
  padding: 12px 8px 14px;
  text-align: center;
  box-shadow: inset 0 -8px 0 rgba(0,0,0,.08);
}
.mug.milk { background: #f4efe6; border: 1px solid #d9cfbb; }
.mug.cocoa { background: #f4efe6; border: 1px solid #d9cfbb; }
.mug .top {
  position: absolute;
  top: -8px; left: -1px; right: -1px; height: 8px;
  border-radius: 10px 10px 0 0;
}
.mug.milk .top { background: #e8dfcf; }
.mug.cocoa .top { background: #e8dfcf; }
.mug-handle {
  position: absolute; top: 22px; width: 12px; height: 22px;
  border: 3px solid #d9cfbb; border-left: none; border-radius: 0 8px 8px 0;
}
.mug.milk .mug-handle { right: -8px; }
.mug.cocoa .mug-handle { left: -8px; border-left: 3px solid #d9cfbb; border-right: none; border-radius: 8px 0 0 8px; }
.mug-value {
  font-family: 'Georgia', serif;
  font-size: 34px;
  font-weight: 700;
  color: var(--primary);
  line-height: 1;
  text-shadow: 2px 2px 0 rgba(255,255,255,.6);
}
.mug.milk .mug-value { color: #7a5b3a; }
.mug.cocoa .mug-value { color: #5b3b1e; }
.mug .foam, .mug .powder {
  position: absolute; left: 6px; right: 6px; top: 4px; height: 30px;
  border-radius: 50%;
}
.mug.milk .foam {
  background: radial-gradient(circle at 30% 40%, #fff 0, #fff 45%, #f0e9d8 100%);
  -webkit-mask-image: radial-gradient(circle, #000 0, transparent 72%);
  mask-image: radial-gradient(circle, #000 0, transparent 72%);
}
.mug.cocoa .powder {
  background: radial-gradient(circle at 60% 50%, #6b4423 0, #4a2e14 60%, transparent 75%);
}
.mug-label { font-size: 11px; margin-top: 6px; color: var(--muted); font-weight: 600; }

/* ---- Recent / lists ---- */
.recent-list { list-style: none; margin-top: 4px; }
.recent-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 9px 6px;
  border-bottom: 1px dashed rgba(123,63,25,.2);
  font-size: 14px;
}
.recent-list .rp { font-weight: 700; color: var(--accent); }

/* ---- Bookmark log entries ---- */
.log-list { list-style: none; display: flex; flex-direction: column; gap: 10px; }
.bookmark {
  position: relative;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 12px;
  color: #fff;
  border-radius: 3px 4px 0 0;
  cursor: pointer;
  box-shadow: 0 3px 6px rgba(0,0,0,.25);
  transition: transform .12s ease, opacity .2s ease;
  touch-action: pan-y;
  transform: translateX(0);
}
.bookmark.green { background: linear-gradient(180deg, #7ecb7f, var(--green)); }
.bookmark.blue { background: linear-gradient(180deg, #64b5f6, var(--blue)); }
.bookmark::after {
  content: "";
  position: absolute;
  top: 100%; left: 0;
  width: 0; height: 0;
  border-left: 0 solid transparent;
  border-right: 24px solid transparent;
  border-top: 18px solid rgba(0,0,0,.18);
  opacity: .35;
}
.bm-favicon {
  width: 8px; height: 8px;
  background: var(--accent);
  border-radius: 50%;
  flex-shrink: 0;
}
.bookmark.green .bm-favicon { background: #d7f7d7; }
.bookmark.blue .bm-favicon { background: #d6ecff; }
.bm-main { flex: 1; min-width: 0; }
.bm-title {
  font-family: 'Segoe Script', 'Bradley Hand', 'Comic Sans MS', cursive;
  font-size: 16px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.bm-meta { font-size: 12px; opacity: .92; margin-top: 2px; }
.bm-stamp {
  font-family: 'Courier New', monospace;
  font-size: 15px;
  font-weight: 700;
  background: rgba(255,255,255,.25);
  padding: 3px 6px;
  border-radius: 4px;
  border: 1px dashed rgba(255,255,255,.5);
  flex-shrink: 0;
}
.bookmark.removing { opacity: 0; transform: translateX(-60%); }
.bookmark.swiping { transform: translateX(-40px); }
.delete-hint {
  position: absolute;
  right: 8px; top: 50%;
  transform: translateY(-50%);
  color: var(--accent);
  font-weight: 800;
  font-size: 11px;
  opacity: 0;
  background: rgba(255,255,255,.9);
  padding: 4px 6px;
  border-radius: 4px;
}
.bookmark.swiping .delete-hint { opacity: 1; }

/* ---- Search ---- */
.search {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #fff;
  border: 1px solid var(--primary);
  border-radius: 24px;
  padding: 8px 12px;
  margin-bottom: 12px;
}
.search input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  font-size: 14px;
  color: var(--ink);
}
.empty {
  text-align: center;
  color: var(--muted);
  font-size: 14px;
  padding: 24px 8px;
}

/* ---- Form ---- */
.form-field { margin-bottom: 14px; }
.form-field label { display: block; font-size: 12px; font-weight: 700; color: var(--primary); margin-bottom: 5px; }
.form-field input {
  width: 100%;
  border: 1px solid #d9c8ae;
  border-radius: 10px;
  padding: 12px;
  font-size: 16px;
  background: #fff;
  color: var(--ink);
}
.form-field input:focus { outline: 2px solid var(--accent); border-color: transparent; }
.err { color: #c62828; font-size: 12px; margin-top: 4px; min-height: 14px; }
.form-actions { display: flex; gap: 10px; margin-top: 8px; }
.form-actions .btn { flex: 1; }
.hidden { display: none; }
.pill {
  background: var(--primary);
  color: var(--secondary);
  border-radius: 20px;
  padding: 4px 12px;
  font-size: 12px;
  font-weight: 700;
}

/* ---- Shelf view ---- */
.shelf-book {
  position: absolute;
  bottom: 0;
  width: 38px;
  border-radius: 3px 3px 0 0;
  cursor: pointer;
  box-shadow: inset -6px 0 8px rgba(0,0,0,.3), inset 3px 0 4px rgba(255,255,255,.25), 0 4px 6px rgba(0,0,0,.3);
  transition: transform .1s ease;
}
.shelf-book:active { transform: translateY(2px); }
.shelf-book.active { background: linear-gradient(180deg, var(--accent), #e5501f); }
.shelf-book.idle { background: linear-gradient(180deg, #8d6e63, #6d4c41); }
.shelf-book .spine {
  position: absolute;
  top: 6px; left: 0; right: 0;
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  font-family: 'Segoe Script', 'Bradley Hand', cursive;
  font-size: 12px;
  font-weight: 700;
  color: rgba(255,255,255,.95);
  letter-spacing: 1px;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  max-height: calc(100% - 12px);
}
.shelf-stage {
  position: relative;
  height: 150px;
  background: linear-gradient(180deg, rgba(62,39,24,.06), rgba(62,39,24,.12));
  border-bottom: var(--shelf-h) solid var(--wood-dark);
  border-radius: 4px 4px 6px 6px;
  margin-top: 8px;
}
.shelf-stage .base-shelf {
  position: absolute;
  bottom: calc(-1 * var(--shelf-h));
  left: 0; right: 0;
  height: var(--shelf-h);
  background: repeating-linear-gradient(90deg, rgba(0,0,0,.08) 0 44px, transparent 44px 78px), linear-gradient(180deg, var(--wood), var(--wood-dark));
  border-radius: 4px;
  box-shadow: 0 4px 8px rgba(0,0,0,.3);
}
.empty-shelf {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 14px;
}

/* ---- Spine overlay ---- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(40,26,20,.55);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 40;
  padding: 0 12px calc(var(--tab-h) + var(--safe-b) + 12px);
}
.overlay-card {
  width: 100%;
  max-width: 420px;
  background: var(--paper);
  border-radius: 16px;
  padding: 18px;
  box-shadow: 0 -6px 30px rgba(0,0,0,.4);
}
.overlay-card h3 { font-family: 'Segoe Script', 'Bradley Hand', cursive; font-size: 20px; color: var(--primary); margin-bottom: 2px; }
.stat-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 14px 0; }
.stat {
  text-align: center;
  background: var(--secondary);
  border-radius: 10px;
  padding: 10px 4px;
  border: 1px solid #e8d5b5;
}
.stat .n { font-size: 22px; font-weight: 800; color: var(--primary); }
.stat .l { font-size: 11px; color: var(--muted); margin-top: 2px; }
.overlay-card .btn { width: 100%; }

/* ---- Tab bar (bookmarks) ---- */
.tabbar {
  position: fixed;
  left: 0; right: 0;
  bottom: 0;
  height: calc(var(--tab-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  z-index: 30;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  gap: 10px;
  background: linear-gradient(180deg, var(--wood), var(--wood-dark));
  box-shadow: 0 -4px 14px rgba(0,0,0,.4);
  border-top: 3px solid var(--primary-deep);
}
.tab {
  position: relative;
  border: none;
  background: transparent;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  width: 62px;
  height: 100%;
  padding-bottom: 8px;
  gap: 4px;
}
.tab label { font-size: 10px; font-weight: 700; letter-spacing: .5px; }
.bm {
  position: relative;
  width: 46px;
  height: 46px;
  border-radius: 3px 3px 6px 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
  font-weight: 800;
  color: rgba(255,255,255,.85);
  box-shadow: 0 4px 6px rgba(0,0,0,.3);
  transition: transform .18s ease, filter .18s ease;
  transform-origin: top center;
}
.bm::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 0;
  width: 0; height: 0;
  border-left: 0 solid transparent;
  border-right: 0 solid transparent;
  border-top: 14px solid rgba(0,0,0,.18);
  border-left-width: 23px;
  border-right-width: 23px;
  border-style: solid;
  border-color: transparent;
  border-top-color: rgba(0,0,0,.15);
}
/* per-tab satin colors */
.tab[data-tab="home"] .bm { background: linear-gradient(180deg, #8d6e63, #6d4c41); }
.tab[data-tab="log"] .bm { background: linear-gradient(180deg, #66bb6a, #43a047); }
.tab[data-tab="add"] .bm { background: linear-gradient(180deg, #ff7043, #e64a19); }
.tab[data-tab="shelf"] .bm { background: linear-gradient(180deg, #42a5f5, #1e88e5); }

.tab.active .bm {
  transform: translateY(-6px) scale(1.06);
  box-shadow: 0 10px 16px rgba(0,0,0,.4);
  filter: saturate(1.25) brightness(1.06);
}
.tab.active .bm::before {
  content: "";
  position: absolute;
  top: 4px; left: 6px; right: 10px; height: 16px;
  background: linear-gradient(180deg, rgba(255,255,255,.55), rgba(255,255,255,0));
  border-radius: 3px;
  pointer-events: none;
}
.tab.active label { color: var(--secondary); }
.tab:not(.active) label { color: rgba(255,243,224,.55); }
.tab:not(.active) .bm { filter: saturate(.55) brightness(.72); }

/* scrollbar */
.journal::-webkit-scrollbar { width: 6px; }
.journal::-webkit-scrollbar-thumb { background: rgba(78,52,46,.3); border-radius: 4px; }