/* Story Time — warm storybook theme, mobile-first. */
:root {
  --paper: #fbf3e4;
  --paper-2: #f3e6cc;
  --card: #fffaf0;
  --ink: #1f2f57;
  --ink-soft: #55607f;
  --line: #e4d4b2;
  --accent: #e8743b;
  --accent-ink: #ffffff;
  --gold: #f2a541;
  --ok: #3f8a5a;
  --danger: #c0392b;
  --radius: 18px;
  --gutter: 16px;
  --shadow: 0 2px 0 rgba(31, 47, 87, .06), 0 8px 24px rgba(31, 47, 87, .10);
  --font-ui: "Fredoka", ui-rounded, "Segoe UI", system-ui, sans-serif;
  --font-book: "Literata", Georgia, "Times New Roman", serif;
  color-scheme: light;
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100dvh;
  background: var(--paper);
  background-image: radial-gradient(circle at 20% 0%, #fff8ea 0, transparent 60%),
                    radial-gradient(circle at 100% 100%, var(--paper-2) 0, transparent 55%);
  background-attachment: fixed;
  color: var(--ink);
  font: 17px/1.45 var(--font-ui);
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}
html.reading, html.reading body { overflow: hidden; }
[hidden] { display: none !important; }
img { max-width: 100%; display: block; }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .4em; font-weight: 600; }
p { margin: 0 0 .8em; }
a { color: inherit; }
:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }

#app {
  max-width: 760px;
  margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 8px)
           calc(env(safe-area-inset-right) + var(--gutter))
           calc(env(safe-area-inset-bottom) + 32px)
           calc(env(safe-area-inset-left) + var(--gutter));
}

/* Top bar */
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 60px; margin-bottom: 12px; }
.brand { display: flex; align-items: center; gap: 10px; font-size: 1.7rem; margin: 0; }
.topbar .title { font-size: 1.25rem; margin: 0; text-align: center; flex: 1; }

/* Buttons */
.btn {
  appearance: none; border: 2px solid transparent; border-radius: 999px;
  display: inline-flex; align-items: center; justify-content: center; gap: .45em;
  min-height: 48px; padding: 10px 20px;
  font: 500 1rem/1.1 var(--font-ui); color: var(--ink); text-decoration: none; text-align: center;
  background: var(--card); cursor: pointer; user-select: none;
  transition: transform .12s ease, box-shadow .12s ease, background .12s;
}
.btn:active { transform: translateY(1px) scale(.98); }
.btn:disabled, .btn.is-disabled { opacity: .55; cursor: default; pointer-events: auto; }
.btn-primary { background: var(--accent); color: var(--accent-ink); box-shadow: 0 4px 0 #b9562a; }
.btn-primary:active { box-shadow: 0 2px 0 #b9562a; }
.btn-ghost { background: transparent; border-color: var(--line); }
.btn-accent { background: var(--gold); color: var(--ink); }
.btn-small { min-height: 40px; padding: 6px 14px; font-size: .9rem; }
.btn-big { min-height: 60px; font-size: 1.15rem; padding: 14px 24px; width: 100%; }
.btn-icon { width: 48px; height: 48px; min-height: 48px; padding: 0; font-size: 1.4rem; flex: none; }
.new-story { margin: 4px 0 24px; font-size: 1.25rem; }

/* Bookshelf */
.shelf { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
@media (min-width: 560px) { .shelf { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.book-card { position: relative; min-width: 0; }
.book-link { display: block; text-decoration: none; }
.cover {
  position: relative; aspect-ratio: 3 / 4; border-radius: 6px 14px 14px 6px; overflow: hidden;
  background: linear-gradient(135deg, #2b3f72, var(--ink)) center / cover no-repeat;
  box-shadow: var(--shadow), inset 6px 0 0 rgba(0, 0, 0, .18);
  display: grid; place-items: center;
}
.cover-placeholder { font-size: 2.6rem; opacity: .8; }
.book-title { font-size: 1rem; margin: 10px 0 2px; overflow-wrap: anywhere; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.book-meta { font-size: .85rem; color: var(--ink-soft); margin: 0; }
.badge {
  position: absolute; left: 8px; bottom: 8px; max-width: calc(100% - 16px);
  padding: 4px 10px; border-radius: 999px; font-size: .78rem; font-weight: 500;
  background: rgba(255, 250, 240, .94); color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.badge-busy::before { content: ""; display: inline-block; width: 8px; height: 8px; margin-right: 6px; border-radius: 50%; background: var(--gold); animation: pulse 1.2s infinite ease-in-out; vertical-align: 1px; }
.badge-error { background: #fde3dc; color: var(--danger); }
.card-menu-btn {
  position: absolute; top: 6px; right: 6px; width: 40px; height: 40px; border-radius: 50%; border: 0;
  background: rgba(255, 250, 240, .92); color: var(--ink); font-size: 1.3rem; line-height: 1; cursor: pointer; box-shadow: var(--shadow);
}
.card-menu {
  position: absolute; top: 50px; right: 6px; z-index: 5; min-width: 160px; max-width: calc(100vw - 32px);
  display: flex; flex-direction: column; padding: 6px; border-radius: 14px;
  background: var(--card); box-shadow: 0 10px 30px rgba(31, 47, 87, .22); border: 1px solid var(--line);
}
.card-menu > * {
  display: block; width: 100%; min-height: 44px; padding: 10px 14px; border: 0; border-radius: 10px;
  background: none; font: 500 1rem var(--font-ui); color: var(--ink); text-align: left; text-decoration: none; cursor: pointer;
}
.card-menu > *:hover, .card-menu > *:focus-visible { background: var(--paper-2); }
.card-menu .danger { color: var(--danger); }
.empty { grid-column: 1 / -1; text-align: center; padding: 32px 8px; color: var(--ink-soft); }
.empty h2 { color: var(--ink); }
.big-emoji { font-size: 3.2rem; margin-bottom: 8px; }

/* Panels */
.panel { background: var(--card); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); margin-bottom: 20px; }
.center { text-align: center; }
.row { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }
.muted { color: var(--ink-soft); }
.small { font-size: .88rem; }
.story-title { font-family: var(--font-book); font-size: 1.5rem; }
.panel .btn-big { margin-top: 8px; }

/* Writing animation */
.writing { padding: 36px 20px; }
.quill { position: relative; display: inline-flex; align-items: flex-end; gap: 6px; height: 72px; margin-bottom: 12px; }
.quill span { font-size: 3.2rem; animation: bob 2s ease-in-out infinite; }
.quill i { width: 9px; height: 9px; border-radius: 50%; background: var(--accent); margin-bottom: 14px; animation: dot 1.4s infinite ease-in-out; }
.quill i:nth-child(3) { animation-delay: .2s; background: var(--gold); }
.quill i:nth-child(4) { animation-delay: .4s; background: var(--ink); }
.writing-line { min-height: 1.5em; }
@keyframes bob { 0%, 100% { transform: translateY(0) rotate(-4deg); } 50% { transform: translateY(-8px) rotate(4deg); } }
@keyframes dot { 0%, 80%, 100% { transform: scale(.4); opacity: .4; } 40% { transform: scale(1); opacity: 1; } }
@keyframes pulse { 50% { opacity: .3; } }
@keyframes spin { to { transform: rotate(360deg); } }

.meter { height: 12px; border-radius: 999px; background: var(--paper-2); overflow: hidden; margin: 12px 0 8px; }
.meter span { display: block; height: 100%; background: linear-gradient(90deg, var(--gold), var(--accent)); border-radius: inherit; transition: width .4s ease; }

/* Page strip */
.page-strip { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
@media (min-width: 560px) { .page-strip { grid-template-columns: repeat(5, minmax(0, 1fr)); } }
.page-tile { display: flex; flex-direction: column; align-items: center; gap: 6px; min-width: 0; }
.thumb {
  width: 100%; aspect-ratio: 3 / 4; border-radius: 10px; background: var(--paper-2) center / cover no-repeat;
  border: 2px dashed var(--line); display: grid; place-items: center;
}
.is-done .thumb { border: 0; box-shadow: var(--shadow); }
.is-error .thumb { border-color: #f0b7a8; background-color: #fdeee9; }
.tile-icon { font-size: 1.5rem; opacity: .7; }
.tile-label { font-size: .85rem; color: var(--ink-soft); }
.spinner { width: 28px; height: 28px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin .9s linear infinite; display: inline-block; }

/* Form */
.note { background: #fff1d6; border-radius: 14px; padding: 12px 14px; font-size: .95rem; }
.card { border: 0; margin: 0 0 18px; padding: 16px; background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); min-width: 0; }
.card legend { float: left; width: 100%; padding: 0; margin-bottom: 8px; font-size: 1.15rem; font-weight: 600; }
.card legend + * { clear: both; }
.field { margin-bottom: 16px; }
.field:last-child { margin-bottom: 0; }
.field > label, .field > .label { display: block; font-weight: 500; margin-bottom: 6px; }
.hint { font-size: .85rem; color: var(--ink-soft); margin: -2px 0 6px; }
.req { color: var(--accent); }
input[type="text"], input[type="number"], select, textarea {
  width: 100%; min-height: 50px; padding: 12px 14px; border-radius: 14px; border: 2px solid var(--line);
  background: #fff; color: var(--ink); font: 400 1rem/1.35 var(--font-ui); /* >=16px avoids iOS zoom */
}
textarea { resize: vertical; min-height: 72px; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--ink); }
[aria-invalid="true"] { border-color: var(--danger) !important; }
.error-text { color: var(--danger); font-size: .9rem; margin: 6px 0 0; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.chip {
  min-height: 44px; padding: 8px 16px; border-radius: 999px; border: 2px solid var(--line);
  background: #fff; color: var(--ink); font: 500 .95rem var(--font-ui); cursor: pointer;
}
.chip[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: var(--paper); }
.segmented { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.segmented label { position: relative; }
.segmented input { position: absolute; opacity: 0; inset: 0; margin: 0; }
.segmented span {
  display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 58px;
  border-radius: 14px; border: 2px solid var(--line); background: #fff; font-weight: 500; cursor: pointer;
}
.segmented small { font-weight: 400; font-size: .78rem; color: var(--ink-soft); }
.segmented input:checked + span { background: var(--ink); border-color: var(--ink); color: var(--paper); }
.segmented input:checked + span small { color: var(--paper-2); }
.segmented input:focus-visible + span { outline: 3px solid var(--gold); outline-offset: 2px; }
.form-actions { display: grid; gap: 12px; position: sticky; bottom: 0; padding: 12px 0 calc(env(safe-area-inset-bottom) + 4px); background: linear-gradient(transparent, var(--paper) 30%); }
@media (min-width: 560px) { .form-actions { grid-template-columns: 1fr 1.4fr; } }

/* Reader */
.reader-view { position: fixed; inset: 0; z-index: 50; background: #241c16; }
.reader-host { position: absolute; inset: 0; }
.reader-close {
  position: absolute; z-index: 60;
  top: calc(env(safe-area-inset-top) + 10px); left: calc(env(safe-area-inset-left) + 10px);
  width: 44px; height: 44px; border-radius: 50%; border: 0;
  background: rgba(255, 250, 240, .88); color: var(--ink); font-size: 1.7rem; line-height: 1; cursor: pointer;
  box-shadow: 0 2px 10px rgba(0, 0, 0, .3);
}
.reader-error { position: absolute; inset: 0; display: grid; place-content: center; gap: 12px; padding: 24px; text-align: center; color: var(--paper); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition: none !important; }
}
