:root {
  --paper: #FBEEDC;
  --paper-deep: #F3E1C8;
  --card: #FFFBF3;
  --ink: #2E2419;
  --ink-soft: #6B5D4F;
  --plum: #3D2B56;
  --plum-light: #5B4478;
  --plum-pale: #EDE5F6;
  --gold: #D9A441;
  --gold-soft: #F3D8A0;
  --leaf: #4C9A6A;
  --coral: #E1636F;
  --line: rgba(46, 36, 25, 0.1);
  --shadow: 0 10px 24px -12px rgba(61, 43, 86, 0.28);
  --shadow-sm: 0 4px 10px -6px rgba(61, 43, 86, 0.22);
  --radius: 18px;
  --radius-sm: 12px;
  --font-display: 'Fraunces', Georgia, serif;
  --font-body: 'Figtree', system-ui, -apple-system, sans-serif;
}

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

/* Any rule that sets `display` on an element (e.g. .menu's `display: flex`)
   beats the browser's built-in [hidden] { display: none } rule, since
   author styles always win over the UA stylesheet regardless of the
   `hidden` attribute -- so toggling .hidden = true in JS silently fails to
   hide anything with its own authored display value. Same fix as Dost. */
[hidden] {
  display: none !important;
}

/* The app shell is a fixed-height (100dvh, not 100vh) 2-row grid: header,
   then content. Only the content row scrolls — the header is never
   position:fixed/sticky, so it can't jump when mobile Safari's address bar
   collapses/expands. Same technique as /root/personal-assistant (Dost). */
html, body {
  height: 100%;
  margin: 0;
  overscroll-behavior-y: none;
}

body {
  display: grid;
  grid-template-rows: auto 1fr;
  height: 100dvh;
  font-family: var(--font-body);
  color: var(--ink);
  background-color: var(--paper);
  background-image:
    radial-gradient(circle at 12% 8%, rgba(217, 164, 65, 0.14), transparent 40%),
    radial-gradient(circle at 88% 92%, rgba(61, 43, 86, 0.10), transparent 45%),
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='140' height='140'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.035'/%3E%3C/svg%3E");
}

h1, h2, h3 {
  font-family: var(--font-display);
  color: var(--plum);
  letter-spacing: -0.01em;
  margin: 0 0 4px;
}
h1 { font-size: 1.9rem; font-weight: 600; }
h2 { font-size: 1.35rem; font-weight: 600; margin-top: 4px; }
h3 { font-size: 1.05rem; font-weight: 600; color: var(--ink-soft); }

a { color: inherit; }

/* ---------- Top bar ---------- */

#topbar {
  position: relative;
  z-index: 30;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  padding-top: calc(14px + env(safe-area-inset-top));
  background: linear-gradient(135deg, var(--plum) 0%, var(--plum-light) 100%);
  box-shadow: var(--shadow);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  text-decoration: none;
  color: #fff;
}
.brand-mark {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  object-fit: cover;
  filter: drop-shadow(0 2px 3px rgba(0,0,0,0.25));
}
.brand-word {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.3rem;
  font-style: italic;
  letter-spacing: -0.01em;
}

.reader-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(255, 255, 255, 0.16);
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 999px;
  padding: 5px 14px 5px 5px;
  cursor: pointer;
  color: #fff;
  font-family: var(--font-body);
  transition: background 0.15s ease;
}
.reader-chip:active { background: rgba(255, 255, 255, 0.28); }
.reader-chip-avatar {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.75rem;
  color: #fff;
  flex-shrink: 0;
}
.reader-chip-name { font-weight: 600; font-size: 0.85rem; }

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

.menu-toggle {
  width: 38px;
  height: 38px;
  border: none;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.16);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  cursor: pointer;
  flex-shrink: 0;
}
.menu-toggle:active { background: rgba(255, 255, 255, 0.28); }
.menu-toggle span {
  display: block;
  width: 18px;
  height: 2px;
  border-radius: 2px;
  background: #fff;
  transition: transform 0.2s ease, opacity 0.2s ease;
}
.menu-toggle.open span:nth-child(1) { transform: translateY(6px) rotate(45deg); }
.menu-toggle.open span:nth-child(2) { opacity: 0; }
.menu-toggle.open span:nth-child(3) { transform: translateY(-6px) rotate(-45deg); }

/* ---------- Main content ---------- */

#app {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 20px 18px calc(28px + env(safe-area-inset-bottom));
  max-width: 640px;
  margin: 0 auto;
  width: 100%;
}

#app > section {
  animation: rise 0.4s cubic-bezier(0.16, 1, 0.3, 1) both;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---------- Popup (encouragement / rating) ---------- */

.popup-overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: rgba(46, 36, 25, 0.55);
  animation: fade-in 0.15s ease both;
}
.popup-card {
  width: 100%;
  max-width: 320px;
  background: var(--card);
  border-radius: var(--radius);
  padding: 28px 22px 22px;
  text-align: center;
  box-shadow: var(--shadow);
  animation: menu-in 0.22s cubic-bezier(0.16, 1, 0.3, 1) both;
}
.popup-icon { font-size: 2.4rem; margin-bottom: 10px; }
.popup-card p {
  font-family: var(--font-display);
  font-style: italic;
  font-size: 1.05rem;
  line-height: 1.5;
  color: var(--plum);
  margin: 0 0 20px;
}
.popup-dismiss {
  padding: 11px 24px;
  border: none;
  border-radius: 999px;
  background: var(--plum);
  color: #fff;
  font-weight: 700;
  font-size: 0.9rem;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
}
.popup-dismiss:active { transform: scale(0.97); }
.popup-dismiss:disabled { opacity: 0.4; cursor: default; }
.popup-dismiss--danger { background: var(--coral); margin-bottom: 4px; }

.popup-card--form { text-align: left; max-width: 340px; }
.popup-card--form h3 { margin: 0 0 12px; text-align: center; }
.popup-hint { font-size: 0.8rem; color: var(--ink-soft); text-align: center; margin: -4px 0 14px; }
.popup-card--form form { margin: 0 0 12px; }
.popup-card--form .entries { max-height: 44vh; overflow-y: auto; margin: 0 0 14px; }
.popup-text-btn {
  display: block;
  width: 100%;
  text-align: center;
  border: none;
  background: none;
  padding: 10px;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--ink-soft);
  cursor: pointer;
}
.popup-text-btn--danger { color: var(--coral); }

.rate-stars { display: flex; justify-content: center; gap: 6px; margin: 4px 0 22px; }
.rate-star {
  border: none;
  background: none;
  font-size: 2rem;
  line-height: 1;
  color: var(--line);
  cursor: pointer;
  padding: 2px;
  transition: transform 0.1s ease, color 0.1s ease;
}
.rate-star--filled { color: var(--gold); }
.rate-star:active { transform: scale(1.15); }

.rerate-btn {
  border: none;
  background: none;
  padding: 4px 0;
  margin: 10px 0 20px;
  cursor: pointer;
  font-size: 1.3rem;
}

/* ---------- Dropdown menu ---------- */

.menu-backdrop {
  position: fixed;
  inset: 0;
  z-index: 25;
  background: rgba(46, 36, 25, 0.35);
  animation: fade-in 0.15s ease both;
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

.menu {
  position: fixed;
  top: calc(70px + env(safe-area-inset-top));
  left: 14px;
  z-index: 26;
  width: 210px;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  animation: menu-in 0.18s cubic-bezier(0.16, 1, 0.3, 1) both;
  transform-origin: top left;
}
@keyframes menu-in {
  from { opacity: 0; transform: scale(0.92) translateY(-6px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}
.menu a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 12px;
  border-radius: var(--radius-sm);
  text-decoration: none;
  color: var(--ink);
  font-weight: 600;
  font-size: 0.92rem;
  transition: background 0.15s ease;
}
.menu a:active { background: var(--plum-pale); }
.menu a.active { background: var(--plum-pale); color: var(--plum); }
.menu-icon { font-size: 1.15rem; }

/* ---------- Reader picker ---------- */

.picker { text-align: center; padding-top: 12px; }
.picker h1 { margin-bottom: 6px; }
.picker .subtitle { color: var(--ink-soft); margin-bottom: 8px; font-size: 0.95rem; }

.reader-grid {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 18px;
  margin: 28px 0;
}
.reader-tile {
  width: 104px;
  border: none;
  background: none;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  font-family: var(--font-body);
  padding: 0;
}
.reader-tile-avatar {
  width: 84px;
  height: 84px;
  border-radius: 28px;
  color: #fff;
  font-family: var(--font-display);
  font-size: 2.1rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow);
  transition: transform 0.15s ease;
}
.reader-tile:active .reader-tile-avatar { transform: scale(0.94); }
.reader-tile span.reader-tile-name {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--ink);
}

#new-reader-form {
  max-width: 320px;
  margin: 20px auto 0;
}

.quote {
  max-width: 380px;
  margin: 44px auto 8px;
  padding: 0;
  border: none;
  position: relative;
}
.quote-mark {
  display: block;
  font-family: var(--font-display);
  font-size: 3.4rem;
  line-height: 0.5;
  color: var(--gold);
  opacity: 0.85;
  margin-bottom: 6px;
}
.quote p {
  font-family: var(--font-display);
  font-style: italic;
  font-weight: 500;
  font-size: 1.15rem;
  line-height: 1.45;
  color: var(--plum);
  margin: 0 0 10px;
}
.quote cite {
  display: block;
  font-style: normal;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.03em;
  color: var(--ink-soft);
  text-transform: uppercase;
}

/* ---------- Books ---------- */

.section-header { display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: 14px; }
.section-header h2 { margin: 0; display: flex; align-items: center; gap: 8px; }
.section-icon { font-size: 1.1rem; }
.count-pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--plum-pale);
  color: var(--plum);
  font-family: var(--font-body);
  font-size: 0.72rem;
  font-weight: 700;
}

.section-divider {
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 8px 0 26px;
  color: var(--gold);
  opacity: 0.7;
}
.section-divider::before, .section-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--line);
}
.section-divider span { padding: 0 12px; font-size: 0.9rem; }

.empty-state {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 4px;
  padding: 28px 16px;
  background: var(--card);
  border: 1.5px dashed var(--line);
  border-radius: var(--radius);
}
.empty-state-icon { font-size: 1.8rem; margin-bottom: 4px; }
.empty-state strong { font-family: var(--font-display); font-size: 0.95rem; color: var(--plum); }
.empty-state span { font-size: 0.8rem; color: var(--ink-soft); }

.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 10px;
  margin-bottom: 28px;
}
.book-card {
  background: var(--card);
  border-radius: var(--radius-sm);
  padding: 7px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 4px;
  box-shadow: var(--shadow-sm);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  animation: rise 0.4s cubic-bezier(0.16, 1, 0.3, 1) both;
}
.book-card:active { transform: scale(0.97); }
.book-card img {
  width: 100%;
  border-radius: 8px;
  aspect-ratio: 2 / 3;
  object-fit: cover;
  box-shadow: 0 4px 10px -4px rgba(46, 36, 25, 0.35);
}
.cover-placeholder {
  width: 100%;
  aspect-ratio: 2 / 3;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
  background: var(--plum-pale);
  border-radius: 8px;
}
.book-card strong {
  font-family: var(--font-display);
  font-size: 0.82rem;
  line-height: 1.2;
  color: var(--ink);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.book-card > span { font-size: 0.68rem; color: var(--ink-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rating { color: var(--gold); letter-spacing: 1px; font-size: 0.72rem; }
.rating .rating-empty { color: var(--line); }
.empty { color: var(--ink-soft); font-style: italic; font-size: 0.92rem; }

.btn {
  background: var(--plum);
  color: #fff;
  padding: 9px 16px;
  border-radius: 999px;
  text-decoration: none;
  font-weight: 600;
  font-size: 0.85rem;
  box-shadow: var(--shadow-sm);
  white-space: nowrap;
}

/* ---------- Book detail ---------- */

.book-detail-header { display: flex; gap: 14px; align-items: flex-start; margin-bottom: 18px; }
.book-detail-cover {
  width: 76px;
  flex-shrink: 0;
  border-radius: 9px;
  aspect-ratio: 2 / 3;
  object-fit: cover;
  box-shadow: 0 8px 16px -6px rgba(46, 36, 25, 0.4);
}
.book-detail-cover.cover-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.9rem;
  background: var(--plum-pale);
}
.book-detail-heading { flex: 1; min-width: 0; padding-top: 4px; }
.book-detail h2 {
  font-size: 1.55rem;
  line-height: 1.15;
  margin-bottom: 6px;
  letter-spacing: -0.015em;
}
.book-detail-heading p { color: var(--ink-soft); margin: 0; font-size: 0.85rem; }
.notes-link {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin-top: 8px;
  padding: 0;
  border: none;
  background: none;
  color: var(--plum);
  font-weight: 700;
  font-size: 0.8rem;
  cursor: pointer;
  text-decoration: underline;
  text-decoration-color: var(--gold);
  text-underline-offset: 3px;
}
.book-detail .rating { font-size: 1.3rem; margin: 10px 0 20px; }

.book-progress { margin: 0 0 16px; }
.book-progress-bar {
  height: 10px;
  border-radius: 999px;
  background: var(--paper-deep);
  overflow: hidden;
  box-shadow: inset 0 1px 2px rgba(46, 36, 25, 0.15);
}
.book-progress-fill {
  height: 100%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--gold) 0%, var(--plum-light) 100%);
  transition: width 0.5s cubic-bezier(0.16, 1, 0.3, 1);
}
.book-progress-label {
  display: block;
  margin-top: 6px;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--ink-soft);
}

.book-review {
  margin: 0 0 20px;
  padding: 14px 16px;
  background: var(--card);
  border-radius: var(--radius-sm);
  border-left: 3px solid var(--gold);
  font-family: var(--font-display);
  font-style: italic;
  font-size: 0.95rem;
  line-height: 1.5;
  color: var(--plum);
}

.entries { list-style: none; padding: 0; margin: 10px 0 0; display: flex; flex-direction: column; gap: 10px; }
.entries li {
  background: var(--card);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  box-shadow: var(--shadow-sm);
  font-size: 0.9rem;
  line-height: 1.5;
}
.entries li strong { color: var(--plum); font-family: var(--font-display); }
.entries li.empty { background: none; box-shadow: none; padding: 8px 0; }
.entries--pickable li { cursor: pointer; transition: transform 0.1s ease; }
.entries--pickable li:active { transform: scale(0.98); }

.cal-cell[data-date]:not(.cal-cell--0) { cursor: pointer; }
.cal-cell[data-date]:not(.cal-cell--0):active { transform: scale(0.92); }

#finish-btn {
  background: var(--gold);
  color: var(--plum);
  border: none;
  border-radius: 999px;
  padding: 10px 20px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  margin-top: 4px;
}

/* ---------- Badges ---------- */

.badge-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(132px, 1fr));
  gap: 14px;
}
.badge-tile {
  background: var(--card);
  border-radius: var(--radius-sm);
  padding: 16px 12px;
  text-align: center;
  box-shadow: var(--shadow-sm);
  border: 1px solid transparent;
}
.badge-tile.earned {
  border-color: var(--gold);
  background: linear-gradient(160deg, #FFFBF3 60%, var(--gold-soft) 160%);
}
.badge-tile.locked { opacity: 0.45; filter: grayscale(0.5); }
.badge-icon { font-size: 2.1rem; margin-bottom: 4px; }
.badge-tile strong { display: block; font-family: var(--font-display); font-size: 0.9rem; color: var(--ink); }
.badge-tile span { display: block; font-size: 0.72rem; color: var(--ink-soft); margin-top: 3px; line-height: 1.3; }

/* ---------- Progress ---------- */

.stats-row { display: flex; gap: 12px; margin: 18px 0 26px; }
.stats-row div {
  flex: 1;
  text-align: center;
  background: var(--card);
  border-radius: var(--radius-sm);
  padding: 14px 8px;
  box-shadow: var(--shadow-sm);
}
.stats-row strong { display: block; font-family: var(--font-display); font-size: 1.6rem; color: var(--plum); }
.stats-row span { font-size: 0.7rem; color: var(--ink-soft); text-transform: uppercase; letter-spacing: 0.04em; }
.stats-row--compact { margin: 16px 0; }
.stats-row--compact strong { font-size: 1.25rem; }

.cal-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 20px 0 10px;
}
.cal-nav h3 { margin: 0; color: var(--plum); }
.cal-nav-btn {
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 50%;
  background: var(--card);
  box-shadow: var(--shadow-sm);
  color: var(--plum);
  font-size: 1.2rem;
  line-height: 1;
  cursor: pointer;
}
.cal-nav-btn:active { transform: scale(0.92); }

.cal-weekdays {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  text-align: center;
  font-size: 0.68rem;
  font-weight: 700;
  color: var(--ink-soft);
  text-transform: uppercase;
  margin-bottom: 4px;
}

.cal-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  background: var(--card);
  border-radius: var(--radius);
  padding: 10px;
  box-shadow: var(--shadow-sm);
}
.cal-cell {
  aspect-ratio: 1;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  background: var(--paper-deep);
}
.cal-cell--blank { background: transparent; }
.cal-cell--0 { background: var(--paper-deep); }
.cal-cell--1 { background: #E4D2A8; }
.cal-cell--2 { background: #DDBB7A; }
.cal-cell--3 { background: var(--gold); }
.cal-cell--4 { background: var(--plum-light); }
.cal-cell--4 .cal-day, .cal-cell--4 .cal-pages { color: #fff; }
.cal-cell--today { outline: 2px solid var(--plum); outline-offset: -2px; }
.cal-day { font-size: 0.7rem; font-weight: 700; color: var(--ink); }
.cal-pages { font-size: 0.6rem; font-weight: 600; color: var(--ink-soft); }

/* ---------- Chat ---------- */

.chat-log {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: calc(100vh - 320px);
  overflow-y: auto;
  margin-bottom: 14px;
  padding: 4px 2px;
}
.chat-msg {
  padding: 10px 14px;
  border-radius: 16px;
  max-width: 82%;
  font-size: 0.9rem;
  line-height: 1.45;
  box-shadow: var(--shadow-sm);
}
.chat-msg.user { align-self: flex-end; background: var(--plum); color: #fff; border-bottom-right-radius: 4px; }
.chat-msg.assistant { align-self: flex-start; background: var(--card); border-bottom-left-radius: 4px; }

/* ---------- Forms ---------- */

form { display: flex; flex-direction: column; gap: 10px; margin: 14px 0; }
.field-label {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ink-soft);
  margin-bottom: -4px;
}
form input, form textarea {
  padding: 12px 14px;
  border: 1.5px solid var(--line);
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  font-family: var(--font-body);
  background: var(--card);
  color: var(--ink);
}
form input:focus, form textarea:focus {
  outline: none;
  border-color: var(--plum-light);
}
form textarea { resize: vertical; min-height: 64px; }
form button {
  padding: 12px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--plum);
  color: #fff;
  font-weight: 700;
  font-size: 0.92rem;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
}
form button:active { transform: scale(0.98); }

details summary {
  cursor: pointer;
  color: var(--ink-soft);
  font-size: 0.85rem;
  margin-top: 4px;
}

#search-results .book-card { flex-direction: row; align-items: center; }
#search-results .book-card img,
#search-results .cover-placeholder { width: 48px; flex-shrink: 0; }
