/* ============================================================
   BookNook — playful styles for small readers
   ============================================================ */

:root {
  --bg: #fff7ec;
  --bg-spot: #ffeed6;
  --ink: #3d2c29;
  --ink-soft: #7a6560;
  --card: #ffffff;
  --coral: #ff6f61;
  --coral-dark: #e5533d;
  --teal: #2ec4b6;
  --teal-dark: #1fa396;
  --sun: #ffbf3f;
  --grape: #9b5de5;
  --sky: #4cc9f0;
  --shadow: 0 6px 0 rgba(61, 44, 41, 0.12);
  --shadow-press: 0 2px 0 rgba(61, 44, 41, 0.12);
  --radius: 22px;
  --font: "Baloo 2", "Comic Sans MS", "Chalkboard SE", ui-rounded,
          "Segoe UI", system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  background-image:
    radial-gradient(var(--bg-spot) 12%, transparent 13%),
    radial-gradient(var(--bg-spot) 12%, transparent 13%);
  background-size: 48px 48px;
  background-position: 0 0, 24px 24px;
  color: var(--ink);
  min-height: 100dvh;
  padding-bottom: env(safe-area-inset-bottom);
}

button { font-family: inherit; color: inherit; }

/* ---------- top bar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 14px;
  padding-top: calc(10px + env(safe-area-inset-top));
  background: rgba(255, 247, 236, 0.92);
  backdrop-filter: blur(6px);
  border-bottom: 3px dashed rgba(61, 44, 41, 0.12);
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
  border: none;
  background: none;
  font-size: 24px;
  font-weight: 800;
  cursor: pointer;
  padding: 6px;
  border-radius: 14px;
}
.logo-emoji { font-size: 28px; }
.logo-text em { font-style: normal; color: var(--coral); }

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

.kid-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 3px solid var(--ink);
  background: var(--card);
  border-radius: 999px;
  padding: 6px 14px 6px 8px;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: var(--shadow);
  min-height: 48px;
}
.kid-chip:active { transform: translateY(3px); box-shadow: var(--shadow-press); }
.kid-chip-avatar {
  font-size: 22px;
  background: var(--bg-spot);
  border-radius: 50%;
  width: 36px; height: 36px;
  display: grid; place-items: center;
}

.icon-btn {
  border: 3px solid var(--ink);
  background: var(--card);
  border-radius: 16px;
  width: 48px; height: 48px;
  font-size: 22px;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.icon-btn:active { transform: translateY(3px); box-shadow: var(--shadow-press); }

/* ---------- layout ---------- */

#main {
  max-width: 1000px;
  margin: 0 auto;
  padding: 18px 16px 120px;
}

.view-title {
  font-size: clamp(26px, 5vw, 38px);
  margin: 8px 0 18px;
  letter-spacing: 0.5px;
}

/* ---------- buttons ---------- */

.btn {
  border: 3px solid var(--ink);
  border-radius: var(--radius);
  padding: 12px 22px;
  font-size: 18px;
  font-weight: 800;
  cursor: pointer;
  box-shadow: var(--shadow);
  min-height: 52px;
  transition: transform 0.06s ease;
}
.btn:active { transform: translateY(4px); box-shadow: var(--shadow-press); }

.btn-primary { background: var(--coral); color: #fff; text-shadow: 0 1px 0 rgba(0,0,0,0.15); }
.btn-primary:hover { background: var(--coral-dark); }
.btn-secondary { background: var(--sun); }
.btn-teal { background: var(--teal); color: #fff; }
.btn-ghost { background: var(--card); }
.btn-danger-ghost { background: var(--card); color: var(--coral-dark); }
.btn-big { width: 100%; font-size: 20px; min-height: 60px; margin-top: 16px; }

.back-btn {
  border: 3px solid var(--ink);
  background: var(--card);
  border-radius: 999px;
  padding: 8px 18px;
  font-size: 16px;
  font-weight: 800;
  cursor: pointer;
  box-shadow: var(--shadow);
  min-height: 48px;
  margin-bottom: 6px;
}
.back-btn:active { transform: translateY(3px); box-shadow: var(--shadow-press); }

/* ---------- kid picker ---------- */

.kid-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 14px;
  margin-bottom: 26px;
}

.kid-card {
  border: 3px solid var(--ink);
  border-radius: var(--radius);
  background: var(--card);
  padding: 18px 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  box-shadow: var(--shadow);
  font-weight: 800;
  font-size: 18px;
}
.kid-card:active { transform: translateY(4px); box-shadow: var(--shadow-press); }
.kid-card-wrap { position: relative; display: grid; }
.kid-card-wrap .kid-card { width: 100%; height: 100%; }
.kid-edit-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 48px;
  height: 48px;
  border: none;
  background: none;
  border-radius: 14px;
  font-size: 20px;
  cursor: pointer;
  display: grid;
  place-items: center;
}
.kid-edit-btn:active { transform: translateY(2px); }
.kid-card .kid-avatar {
  font-size: 44px;
  width: 76px; height: 76px;
  display: grid; place-items: center;
  border-radius: 50%;
}

.new-kid-card {
  border: 3px dashed var(--ink);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.75);
  padding: 20px;
  max-width: 560px;
}
.new-kid-title { margin: 0 0 12px; font-size: 22px; }

.field-label {
  display: block;
  font-weight: 800;
  margin: 14px 0 6px;
  color: var(--ink-soft);
}

.text-input {
  width: 100%;
  border: 3px solid var(--ink);
  border-radius: 16px;
  padding: 14px 16px;
  font-size: 18px;
  font-family: inherit;
  background: var(--card);
  min-height: 54px;
}
.text-input:focus { outline: 4px solid var(--sky); outline-offset: 2px; }

.avatar-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
.avatar-option {
  font-size: 30px;
  width: 58px; height: 58px;
  border: 3px solid transparent;
  border-radius: 18px;
  background: var(--card);
  cursor: pointer;
  display: grid; place-items: center;
}
.avatar-option[aria-checked="true"] {
  border-color: var(--ink);
  background: var(--sun);
  box-shadow: var(--shadow);
}

/* ---------- library ---------- */

.library-head { display: flex; align-items: baseline; gap: 14px; flex-wrap: wrap; }
.library-count { color: var(--ink-soft); font-weight: 700; margin: 0 0 14px; }

.book-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 18px;
}

.book-tile {
  border: none;
  background: none;
  padding: 0;
  cursor: pointer;
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.book-tile:active .cover { transform: translateY(4px) rotate(0deg); box-shadow: var(--shadow-press); }

.cover {
  width: 100%;
  aspect-ratio: 2 / 3;
  border: 3px solid var(--ink);
  border-radius: 16px;
  box-shadow: var(--shadow);
  object-fit: cover;
  background: var(--card);
  display: block;
  transition: transform 0.08s ease;
}
.book-tile:nth-child(3n) .cover { transform: rotate(-1.2deg); }
.book-tile:nth-child(3n+1) .cover { transform: rotate(1.2deg); }

.cover-placeholder {
  width: 100%;
  aspect-ratio: 2 / 3;
  border: 3px solid var(--ink);
  border-radius: 16px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px;
  text-align: center;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0,0,0,0.25);
  font-weight: 800;
  overflow: hidden;
}
.cover-placeholder .ph-emoji { font-size: 34px; }
.cover-placeholder .ph-title {
  font-size: 15px;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.book-tile-title {
  font-weight: 800;
  font-size: 15px;
  line-height: 1.25;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.book-tile-notes {
  font-size: 13px;
  color: var(--ink-soft);
  font-weight: 700;
}

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--ink-soft);
}
.empty-emoji { font-size: 72px; margin-bottom: 6px; }
.empty-state h2 { color: var(--ink); margin: 6px 0; }

.fab {
  position: fixed;
  right: 18px;
  bottom: calc(18px + env(safe-area-inset-bottom));
  z-index: 15;
  display: flex;
  align-items: center;
  gap: 10px;
  border: 3px solid var(--ink);
  border-radius: 999px;
  background: var(--teal);
  color: #fff;
  text-shadow: 0 1px 0 rgba(0,0,0,0.15);
  font-size: 19px;
  font-weight: 800;
  padding: 14px 24px;
  min-height: 60px;
  cursor: pointer;
  box-shadow: 0 8px 0 rgba(61, 44, 41, 0.18);
}
.fab:active { transform: translateY(5px); box-shadow: var(--shadow-press); }
.fab-plus { font-size: 22px; }

/* ---------- search ---------- */

.search-bar {
  display: flex;
  gap: 10px;
  margin-bottom: 14px;
}
.search-input { flex: 1; min-width: 0; }
.search-go { flex-shrink: 0; }

.search-status {
  font-weight: 800;
  color: var(--ink-soft);
  min-height: 28px;
  margin-bottom: 8px;
  font-size: 17px;
}

.result-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.result-card {
  display: flex;
  gap: 14px;
  align-items: center;
  border: 3px solid var(--ink);
  border-radius: var(--radius);
  background: var(--card);
  padding: 12px;
  box-shadow: var(--shadow);
}
.result-cover {
  width: 64px;
  height: 96px;
  flex-shrink: 0;
  border: 2px solid var(--ink);
  border-radius: 10px;
  object-fit: cover;
  background: var(--bg-spot);
}
.result-cover-ph {
  width: 64px; height: 96px;
  flex-shrink: 0;
  border: 2px solid var(--ink);
  border-radius: 10px;
  display: grid; place-items: center;
  font-size: 26px;
  color: #fff;
}
.result-info { flex: 1; min-width: 0; }
.result-title { font-weight: 800; font-size: 17px; margin: 0 0 2px; }
.result-author { color: var(--ink-soft); font-weight: 700; font-size: 15px; margin: 0; }
.result-year { color: var(--ink-soft); font-size: 13px; }
.result-add {
  flex-shrink: 0;
  font-size: 15px;
  padding: 10px 14px;
  min-height: 48px;
}
.result-add[disabled] {
  background: var(--bg-spot);
  color: var(--ink-soft);
  cursor: default;
  box-shadow: none;
  transform: none;
}

/* ---------- book detail ---------- */

.book-hero {
  display: flex;
  gap: 20px;
  align-items: flex-start;
  margin: 14px 0 24px;
}
.book-hero .cover,
.book-hero .cover-placeholder {
  width: min(38vw, 190px);
  flex-shrink: 0;
}
.book-hero-info h1 {
  font-size: clamp(22px, 4.5vw, 34px);
  margin: 4px 0 6px;
  line-height: 1.15;
}
.book-hero-info .book-author {
  color: var(--ink-soft);
  font-weight: 800;
  font-size: 18px;
  margin: 0 0 6px;
}
.book-hero-info .book-year {
  display: inline-block;
  background: var(--sun);
  border: 2px solid var(--ink);
  border-radius: 999px;
  padding: 3px 12px;
  font-weight: 800;
  font-size: 14px;
}

.hero-rating {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  font-weight: 800;
}
.hero-rating-num { color: var(--ink-soft); font-size: 15px; }

/* ---------- stars ---------- */

.stars { letter-spacing: 2px; font-size: 20px; line-height: 1; }
.stars .star { color: rgba(61, 44, 41, 0.2); }
.stars .star.on { color: var(--sun); text-shadow: 0 1px 0 rgba(61, 44, 41, 0.35); }

.rating-picker {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  border: 3px dashed rgba(61, 44, 41, 0.25);
  border-radius: 16px;
  padding: 10px 14px;
  margin-bottom: 16px;
  background: #fffdf7;
}
.rating-picker-label { font-weight: 800; }
.rating-picker-value { color: var(--ink-soft); font-weight: 800; font-size: 15px; }
.star-row { display: flex; gap: 2px; }
.star-btn {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 34px;
  line-height: 1;
  padding: 4px 3px;
  color: rgba(61, 44, 41, 0.2);
  transition: transform 0.06s ease;
}
.star-btn.on { color: var(--sun); text-shadow: 0 2px 0 rgba(61, 44, 41, 0.3); }
.star-btn:active { transform: scale(1.25); }

.notes-title { font-size: 24px; margin: 0 0 12px; }

.note-editor {
  border: 3px solid var(--ink);
  border-radius: var(--radius);
  background: var(--card);
  padding: 16px;
  box-shadow: var(--shadow);
  margin-bottom: 18px;
}
.note-editor-head {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 800;
  margin-bottom: 10px;
  font-size: 17px;
}
.note-editor-head .kid-avatar-sm {
  font-size: 22px;
  width: 40px; height: 40px;
  border-radius: 50%;
  display: grid; place-items: center;
}
.note-section { margin-bottom: 18px; }
.note-section-title {
  display: block;
  font-weight: 800;
  font-size: 18px;
  margin-bottom: 2px;
}
.note-section-hint {
  color: var(--ink-soft);
  font-weight: 700;
  font-size: 14px;
  margin: 0 0 8px;
}
.note-section .note-textarea { min-height: 110px; }
/* The story summary needs room to write — show ~8 lines before scrolling. */
.note-section .note-textarea-tall { min-height: 240px; }

/* ---------- scene drawing pad ---------- */

.draw-wrap {
  border: 3px solid var(--ink);
  border-radius: 16px;
  overflow: hidden;
  background: #fff;
  margin-bottom: 8px;
}
#draw-canvas {
  display: block;
  width: 100%;
  height: auto;
  touch-action: none;
  cursor: crosshair;
}
.draw-tools {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}
.swatch {
  width: 38px; height: 38px;
  border: 3px solid transparent;
  border-radius: 50%;
  cursor: pointer;
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, 0.6);
}
.swatch.on {
  border-color: var(--ink);
  transform: scale(1.15);
  box-shadow: var(--shadow-press);
}
.draw-tool {
  width: 42px; height: 42px;
  border: 3px solid transparent;
  border-radius: 12px;
  background: var(--bg-spot);
  font-size: 20px;
  cursor: pointer;
  display: grid; place-items: center;
}
.draw-tool.on {
  border-color: var(--ink);
  background: var(--sun);
  box-shadow: var(--shadow-press);
}

.note-textarea {
  width: 100%;
  min-height: 120px;
  border: 3px solid var(--ink);
  border-radius: 16px;
  padding: 12px 14px;
  font-size: 17px;
  font-family: inherit;
  resize: vertical;
  background: #fffdf7;
}
.note-textarea:focus { outline: 4px solid var(--sky); outline-offset: 2px; }
.note-save-row {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 10px;
}

.note-list { display: flex; flex-direction: column; gap: 12px; }

.note-card {
  border: 3px solid var(--ink);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  background: var(--card);
}
.note-card-head {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 800;
  margin-bottom: 6px;
}
.note-card-head .kid-avatar-sm {
  font-size: 20px;
  width: 36px; height: 36px;
  border-radius: 50%;
  display: grid; place-items: center;
}
.note-card-date { margin-left: auto; font-size: 13px; color: var(--ink-soft); }
.note-card-head .stars { font-size: 16px; }
.note-card-label {
  margin: 12px 0 2px;
  font-weight: 800;
  font-size: 14px;
  color: var(--ink-soft);
}
.note-drawing {
  width: 100%;
  max-width: 420px;
  border: 3px solid var(--ink);
  border-radius: 14px;
  display: block;
  background: #fff;
}
.note-card-text {
  margin: 0;
  font-size: 17px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.book-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 26px;
}

/* ---------- modal ---------- */

.modal-backdrop[hidden] { display: none; }

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(61, 44, 41, 0.45);
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 20px;
}
.modal {
  background: var(--bg);
  border: 4px solid var(--ink);
  border-radius: 26px;
  box-shadow: 0 10px 0 rgba(61, 44, 41, 0.25);
  padding: 22px;
  width: 100%;
  max-width: 440px;
  max-height: 85dvh;
  overflow-y: auto;
}
.modal h2 { margin: 0 0 10px; font-size: 24px; }
.modal p { color: var(--ink-soft); font-weight: 700; line-height: 1.4; }
.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 18px;
}
.modal-actions .btn { width: 100%; }

/* ---------- toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(96px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 60;
  background: var(--ink);
  color: #fff;
  font-weight: 800;
  font-size: 17px;
  padding: 14px 24px;
  border-radius: 999px;
  box-shadow: 0 6px 16px rgba(0,0,0,0.25);
  max-width: min(90vw, 480px);
  text-align: center;
  animation: toast-pop 0.25s ease;
}
@keyframes toast-pop {
  from { transform: translateX(-50%) translateY(20px); opacity: 0; }
  to   { transform: translateX(-50%) translateY(0); opacity: 1; }
}

/* ---------- add-to-home-screen nudge ---------- */

.install-nudge[hidden] { display: none; }

.install-nudge {
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: calc(12px + env(safe-area-inset-bottom));
  z-index: 45; /* over page content, under the modal backdrop and toast */
  margin: 0 auto;
  max-width: 460px;
  background: var(--bg);
  border: 4px solid var(--ink);
  border-radius: 22px;
  box-shadow: 0 10px 0 rgba(61, 44, 41, 0.25);
  padding: 18px 18px 14px;
  animation: nudge-up 0.3s ease;
}
@keyframes nudge-up {
  from { transform: translateY(24px); opacity: 0; }
  to   { transform: translateY(0); opacity: 1; }
}
.nudge-head {
  display: flex;
  align-items: center;
  gap: 10px;
}
.nudge-emoji { font-size: 34px; }
.nudge-title {
  margin: 0;
  font-size: 20px;
  line-height: 1.2;
}
.nudge-sub {
  margin: 8px 0 0;
  color: var(--ink-soft);
  font-weight: 700;
  line-height: 1.4;
}
.nudge-steps {
  margin: 12px 0 0;
  padding-left: 26px;
  color: var(--ink-soft);
  font-weight: 700;
  line-height: 1.5;
}
.nudge-steps li { margin: 6px 0; }
.nudge-key {
  display: inline-grid;
  place-items: center;
  min-width: 30px;
  height: 30px;
  border: 2px solid var(--ink);
  border-radius: 8px;
  background: #fff;
  font-size: 16px;
  vertical-align: middle;
}
.nudge-actions {
  display: flex;
  gap: 10px;
  margin-top: 14px;
}
.nudge-actions .btn { flex: 1; }

/* ---------- version tag ---------- */

/* Bottom-left: the "find a book" FAB owns the bottom-right corner. */
.version-tag {
  position: fixed;
  left: 8px;
  bottom: calc(4px + env(safe-area-inset-bottom));
  z-index: 5;
  font-size: 12px;
  font-weight: 700;
  color: rgba(61, 44, 41, 0.35);
  pointer-events: none;
  user-select: none;
}

/* ---------- loading spinner ---------- */

.spinner {
  display: inline-block;
  width: 22px; height: 22px;
  border: 4px solid var(--bg-spot);
  border-top-color: var(--coral);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  vertical-align: middle;
  margin-right: 8px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- bigger screens (tablets) ---------- */

@media (min-width: 700px) {
  .book-grid { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 24px; }
  .kid-grid { grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); }
  .result-add { font-size: 17px; }
  #main { padding-left: 28px; padding-right: 28px; }
}

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