/* Regala una storia — stile editoriale, caldo, mobile-first */
:root {
  --ivory: #FFF9F0;
  --sage: #6F8B74;
  --sage-dark: #58715D;
  --sage-soft: #E4ECE5;
  --coral: #E78A73;
  --coral-dark: #C96F59;
  --coral-soft: #FBE6DF;
  --yellow: #F3D68A;
  --yellow-soft: #FBF1D3;
  --ink: #28332C;
  --ink-soft: #5B665F;
  --line: #EADFCF;
  --card: #FFFFFF;
  --shadow: 0 2px 10px rgba(40, 51, 44, 0.06), 0 1px 2px rgba(40, 51, 44, 0.04);
  --radius: 18px;
  --radius-sm: 12px;
  --font: "Avenir Next", "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  --serif: Georgia, "Iowan Old Style", "Palatino Linotype", "Book Antiqua", serif;
  --max: 720px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--ivory);
  color: var(--ink);
  font-family: var(--font);
  font-size: 17px;
  line-height: 1.5;
}
img, svg { max-width: 100%; display: block; }
a { color: var(--sage-dark); }
button { font: inherit; cursor: pointer; }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; line-height: 1.2; margin: 0 0 .4em; letter-spacing: -.01em; }
h1 { font-size: clamp(1.9rem, 6vw, 2.6rem); }
h2 { font-size: 1.45rem; }
h3 { font-size: 1.15rem; }
p { margin: 0 0 1em; }
.muted { color: var(--ink-soft); }
.small { font-size: .92rem; }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
[hidden] { display: none !important; }

.skip { position: absolute; left: -999px; top: 8px; background: var(--ink); color: #fff; padding: 8px 12px; border-radius: 8px; z-index: 100; }
.skip:focus { left: 8px; }

/* Layout */
.wrap { max-width: var(--max); margin: 0 auto; padding: 0 16px; }
.site-header { padding: 18px 0 8px; }
.brand { display: flex; align-items: center; gap: 12px; text-decoration: none; color: inherit; }
.brand svg { width: 44px; height: 44px; flex: none; }
.brand-title { font-family: var(--serif); font-size: 1.25rem; font-weight: 700; line-height: 1.1; }
.brand-sub { font-size: .85rem; color: var(--ink-soft); }

.nav { display: flex; gap: 6px; overflow-x: auto; padding: 10px 0 6px; scrollbar-width: none; }
.nav::-webkit-scrollbar { display: none; }
.nav a {
  flex: none; text-decoration: none; color: var(--ink-soft); padding: 8px 14px; border-radius: 999px;
  border: 1px solid transparent; font-weight: 600; font-size: .95rem; min-height: 40px; display: inline-flex; align-items: center;
}
.nav a[aria-current="page"] { background: var(--card); color: var(--ink); border-color: var(--line); box-shadow: var(--shadow); }

main { padding: 8px 0 60px; }
.view { animation: fade .25s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px);} to { opacity: 1; transform: none; } }

/* Hero */
.hero { padding: 10px 0 6px; }
.hero h1 { margin-bottom: .25em; }
.tagline { font-family: var(--serif); font-style: italic; color: var(--ink-soft); font-size: 1.1rem; margin-bottom: 1em; }
.hero-art { margin: 6px auto 14px; width: min(260px, 70%); }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 14px 0; }
.stat { background: var(--card); border-radius: var(--radius-sm); padding: 12px 10px; box-shadow: var(--shadow); text-align: center; }
.stat b { display: block; font-size: 1.7rem; font-family: var(--serif); line-height: 1.1; }
.stat span { font-size: .8rem; color: var(--ink-soft); display: block; }
.stat.purchased b { color: var(--sage-dark); }
.stat.available b { color: var(--coral-dark); }

.progress { margin: 6px 0 18px; }
.progress-bar { height: 12px; background: #F1E8D8; border-radius: 999px; overflow: hidden; }
.progress-fill { height: 100%; background: linear-gradient(90deg, var(--sage), var(--sage-dark)); border-radius: 999px; transition: width .6s ease; }
.progress-label { font-size: .88rem; color: var(--ink-soft); margin-top: 6px; display: flex; justify-content: space-between; }

.cta-row { display: grid; gap: 10px; margin: 8px 0 22px; }
@media (min-width: 480px) { .cta-row { grid-template-columns: 1fr 1fr; } }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 52px; padding: 12px 20px; border-radius: 14px; border: 2px solid transparent;
  font-weight: 700; font-size: 1.05rem; text-decoration: none; transition: transform .08s ease, background .15s ease;
  width: 100%;
}
.btn:active { transform: scale(.98); }
.btn:focus-visible, a:focus-visible, button:focus-visible, input:focus-visible, select:focus-visible { outline: 3px solid var(--yellow); outline-offset: 2px; }
.btn-primary { background: var(--coral); color: #fff; }
.btn-primary:hover { background: var(--coral-dark); }
.btn-secondary { background: var(--sage); color: #fff; }
.btn-secondary:hover { background: var(--sage-dark); }
.btn-ghost { background: var(--card); color: var(--ink); border-color: var(--line); }
.btn-ghost:hover { border-color: var(--sage); }
.btn-sm { min-height: 42px; padding: 8px 14px; font-size: .95rem; border-radius: 11px; width: auto; }
.btn-link { background: none; border: 0; color: var(--sage-dark); text-decoration: underline; padding: 6px 2px; min-height: 40px; }
.btn[disabled], .btn[aria-disabled="true"] { opacity: .55; cursor: not-allowed; }

/* Cards */
.card { background: var(--card); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); border: 1px solid var(--line); }
.section { margin: 18px 0; }
.section h2 { margin-bottom: 8px; }

/* Book list */
.toolbar { display: grid; gap: 10px; margin-bottom: 14px; }
.search { position: relative; }
.search input {
  width: 100%; min-height: 50px; padding: 10px 14px 10px 42px; border-radius: 14px; border: 1px solid var(--line);
  background: var(--card); font: inherit; color: var(--ink);
}
.search svg { position: absolute; left: 14px; top: 50%; transform: translateY(-50%); width: 20px; height: 20px; color: var(--ink-soft); }
.chips { display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; border: 1px solid var(--line); background: var(--card); color: var(--ink-soft); border-radius: 999px;
  padding: 8px 14px; min-height: 40px; font-weight: 600; font-size: .92rem;
}
.chip[aria-pressed="true"] { background: var(--ink); color: #fff; border-color: var(--ink); }

.book-list { display: grid; gap: 12px; }
.book { display: grid; grid-template-columns: 56px 1fr; gap: 14px; align-items: start; }
.book-icon { width: 56px; height: 72px; }
.book h3 { margin-bottom: 2px; font-size: 1.1rem; }
.book .meta { color: var(--ink-soft); font-size: .92rem; margin-bottom: 8px; }
.book-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.status {
  display: inline-flex; align-items: center; gap: 6px; font-size: .82rem; font-weight: 700; padding: 4px 10px; border-radius: 999px;
  letter-spacing: .01em;
}
.status::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.status.available { background: var(--coral-soft); color: var(--coral-dark); }
.status.reserved { background: var(--yellow-soft); color: #8A6A1A; }
.status.purchased { background: var(--sage-soft); color: var(--sage-dark); }
.book.is-mine { border-color: var(--sage); box-shadow: 0 0 0 2px var(--sage-soft), var(--shadow); }
.empty { text-align: center; padding: 30px 10px; color: var(--ink-soft); }

/* Draw a story */
.draw-stage { display: flex; flex-direction: column; align-items: center; text-align: center; padding: 10px 0; }
.deck { position: relative; width: min(280px, 78vw); aspect-ratio: 5 / 7; margin: 12px auto 20px; perspective: 1200px; }
.deck .layer { position: absolute; inset: 0; border-radius: 20px; background: var(--card); border: 1px solid var(--line); box-shadow: var(--shadow); }
.deck .layer:nth-child(1) { transform: rotate(-5deg) translate(-6px, 6px); }
.deck .layer:nth-child(2) { transform: rotate(4deg) translate(6px, 3px); }
.flipcard { position: absolute; inset: 0; transform-style: preserve-3d; transition: transform .9s cubic-bezier(.2,.7,.2,1); }
.flipcard.is-flipped { transform: rotateY(180deg); }
.face {
  position: absolute; inset: 0; border-radius: 20px; backface-visibility: hidden; -webkit-backface-visibility: hidden;
  display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 20px; overflow: hidden;
}
.face.back { background: linear-gradient(160deg, var(--sage) 0%, var(--sage-dark) 100%); color: #fff; border: 1px solid var(--sage-dark); }
.face.back svg { width: 60%; }
.face.back::after {
  content: ""; position: absolute; inset: 10px; border: 1.5px dashed rgba(255,255,255,.45); border-radius: 14px; pointer-events: none;
}
.face.front { background: var(--card); border: 1px solid var(--line); transform: rotateY(180deg); color: var(--ink); justify-content: space-between; text-align: center; }
.face.front .kicker { font-size: .78rem; text-transform: uppercase; letter-spacing: .08em; color: var(--coral-dark); font-weight: 700; }
.face.front h3 { font-size: 1.35rem; margin: 6px 0 4px; }
.face.front .meta { color: var(--ink-soft); font-size: .92rem; }
.face.front .star { width: 44px; color: var(--yellow); }
.deck.is-shuffling .flipcard { animation: shuffle .9s ease; }
.deck.is-shuffling .layer:nth-child(1) { animation: wiggle1 .9s ease; }
.deck.is-shuffling .layer:nth-child(2) { animation: wiggle2 .9s ease; }
@keyframes shuffle {
  0% { transform: translateX(0) rotate(0); } 25% { transform: translateX(-40px) rotate(-6deg); }
  50% { transform: translateX(40px) rotate(6deg); } 75% { transform: translateX(-20px) rotate(-3deg); } 100% { transform: none; }
}
@keyframes wiggle1 { 0%,100% { transform: rotate(-5deg) translate(-6px, 6px);} 50% { transform: rotate(-12deg) translate(-26px, 10px);} }
@keyframes wiggle2 { 0%,100% { transform: rotate(4deg) translate(6px, 3px);} 50% { transform: rotate(12deg) translate(26px, 6px);} }
.reveal { font-family: var(--serif); font-style: italic; font-size: 1.15rem; color: var(--sage-dark); min-height: 1.6em; margin: 0 0 12px; }
.draw-actions { display: grid; gap: 10px; width: 100%; max-width: 420px; }

@media (prefers-reduced-motion: reduce) {
  .flipcard, .progress-fill, .view, .btn { transition: none !important; animation: none !important; }
  .deck.is-shuffling .flipcard, .deck.is-shuffling .layer { animation: none !important; }
}

/* Dialog */
dialog {
  border: 0; border-radius: 20px; padding: 0; max-width: min(520px, calc(100vw - 32px)); width: 100%;
  background: var(--card); color: var(--ink); box-shadow: 0 20px 60px rgba(40,51,44,.25);
}
dialog::backdrop { background: rgba(40,51,44,.45); }
.dialog-body { padding: 22px 20px 20px; }
.dialog-actions { display: grid; gap: 10px; margin-top: 16px; }
@media (min-width: 420px) { .dialog-actions.two { grid-template-columns: 1fr 1fr; } }

.code-box {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 1.25rem; letter-spacing: .06em;
  background: var(--yellow-soft); border: 1px dashed #D9BE6B; border-radius: 12px; padding: 12px; text-align: center; word-break: break-all;
  user-select: all;
}
.notice { background: var(--yellow-soft); border-left: 4px solid var(--yellow); padding: 10px 12px; border-radius: 10px; font-size: .95rem; }
.notice.ok { background: var(--sage-soft); border-color: var(--sage); }
.notice.warn { background: var(--coral-soft); border-color: var(--coral); }

/* Toast + loading */
.toast {
  position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%); background: var(--ink); color: #fff;
  padding: 12px 16px; border-radius: 12px; max-width: calc(100vw - 32px); z-index: 50; box-shadow: var(--shadow); font-size: .95rem;
}
.spinner { width: 28px; height: 28px; border: 3px solid var(--line); border-top-color: var(--sage); border-radius: 50%; animation: spin .8s linear infinite; margin: 20px auto; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Shops */
.shop { display: grid; gap: 2px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.shop:last-child { border-bottom: 0; }
.shop b { font-size: 1.02rem; }
.links { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.links a { display: inline-flex; align-items: center; min-height: 40px; padding: 6px 12px; border: 1px solid var(--line); border-radius: 999px; background: var(--card); text-decoration: none; color: var(--ink); font-weight: 600; font-size: .92rem; }
.links a:hover { border-color: var(--sage); }

/* Forms */
label { display: block; font-weight: 600; margin-bottom: 6px; }
input[type="text"], input[type="password"], select {
  width: 100%; min-height: 48px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 12px; background: var(--card); font: inherit; color: var(--ink);
}
.row { display: flex; gap: 8px; align-items: stretch; }
.row input { flex: 1; }

footer { color: var(--ink-soft); font-size: .85rem; padding: 20px 0 30px; border-top: 1px solid var(--line); margin-top: 20px; }
footer a { color: inherit; }
