:root {
  --wall-top: #e4e1dc;
  --wall-bottom: #c9c5be;
  --floor-a: #d9c7aa;
  --floor-b: #cdb995;
  --ink: #23221f;
  --muted: #6f6a62;
  --panel: rgba(250, 248, 244, 0.97);
  --chrome: rgba(35, 34, 31, 0.78);
  --chrome-hover: rgba(35, 34, 31, 0.92);
  --accent: #8b7444;
  --radius: 14px;
  --toolbar-h: 56px;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body { background: var(--wall-bottom); -webkit-tap-highlight-color: transparent; }
button, input, a { font: inherit; color: inherit; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

.app {
  position: fixed; inset: 0;
  display: grid; grid-template-rows: auto 1fr auto;
  /* wall + floor backdrop, like a book on display */
  background:
    linear-gradient(to bottom, transparent 0 78%, rgba(0,0,0,.10) 78%, transparent 78.6%),
    repeating-linear-gradient(90deg, var(--floor-a) 0 46px, var(--floor-b) 46px 47px, var(--floor-a) 47px 92px) bottom / 100% 22% no-repeat,
    radial-gradient(120% 90% at 50% 30%, var(--wall-top), var(--wall-bottom));
}

/* ---------- top bar ---------- */
.topbar { display: flex; align-items: center; padding: 14px 20px 0; min-height: 44px; }
.title { margin: 0; font-size: 14px; font-weight: 600; line-height: 1.3; letter-spacing: .02em; color: var(--ink); opacity: .85;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- stage ---------- */
.stage { position: relative; min-height: 0; }
.book-wrap { position: absolute; inset: 12px 84px 18px; } /* absolute so the engine measures a real height */
.book { width: 100%; height: 100%; }
.book .page { background: #fbf9f5; overflow: hidden; }
.book:not(.stf__parent) > .page { display: none; } /* hide raw pages until the engine takes over */
.book .page img { display: block; width: 100%; height: 100%; object-fit: cover; user-select: none; -webkit-user-drag: none; }
.book .page .ph { position: absolute; inset: 0; background: linear-gradient(110deg, #f4f1eb 30%, #ebe6dd 50%, #f4f1eb 70%) 0 0 / 300% 100%; animation: shimmer 1.4s linear infinite; }
@keyframes shimmer { to { background-position: -150% 0; } }
.stf__parent { filter: drop-shadow(0 18px 22px rgba(30, 25, 15, .28)); }

.loader { position: absolute; inset: 0; display: grid; place-content: center; gap: 12px; justify-items: center;
  color: var(--muted); font-size: 13px; pointer-events: none; transition: opacity .3s; }
.loader span { width: 28px; height: 28px; border: 2px solid rgba(0,0,0,.15); border-top-color: var(--ink); border-radius: 50%; animation: spin .8s linear infinite; }
.loader.done { opacity: 0; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- WebGL engine ---------- */
.curl-canvas { position: absolute; inset: 0; display: block; touch-action: none; -webkit-user-select: none; user-select: none; }
.curl-overlay { position: absolute; inset: 0; pointer-events: none; }

/* ---------- hotspots ---------- */
.hotspots { position: absolute; inset: 0; pointer-events: none; }
.hotspot { position: absolute; pointer-events: auto; border-radius: 8px; cursor: pointer;
  outline: 1.5px solid transparent; transition: background .2s, outline-color .2s; }
.hotspot:hover, .hotspot:focus-visible { background: rgba(139, 116, 68, .12); outline-color: rgba(139, 116, 68, .65); }
.hotspot::after { content: attr(data-label); position: absolute; left: 50%; bottom: calc(100% + 6px); transform: translateX(-50%);
  background: var(--ink); color: #fff; font-size: 11px; padding: 5px 8px; border-radius: 6px; white-space: nowrap;
  opacity: 0; pointer-events: none; transition: opacity .15s; }
.hotspot:hover::after { opacity: 1; }
.app.show-hotspots .hotspot { animation: hint 1.6s ease-out 2; }
@keyframes hint { 0%, 100% { background: transparent; } 40% { background: rgba(139, 116, 68, .2); } }

/* ---------- side navigation ---------- */
.side { position: absolute; top: 50%; transform: translateY(-50%); display: flex; flex-direction: column; gap: 10px; z-index: 5; }
.side-left { left: 18px; }
.side-right { right: 18px; }
.nav-btn { width: 44px; height: 52px; display: grid; place-items: center; border: 0; border-radius: 6px; cursor: pointer;
  background: var(--chrome); color: #fff; transition: background .15s, opacity .2s, transform .15s; }
.nav-btn.small { height: 40px; }
.nav-btn:hover { background: var(--chrome-hover); }
.nav-btn:active { transform: scale(.96); }
.nav-btn:disabled { opacity: .25; cursor: default; }
.nav-btn svg { width: 22px; height: 22px; stroke-width: 2; }

/* ---------- toolbar ---------- */
.toolbar { justify-self: center; margin: 0 12px 16px; display: flex; align-items: center; gap: 2px; padding: 6px 8px;
  background: var(--chrome); color: #fff; border-radius: 999px; backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  box-shadow: 0 8px 24px rgba(0,0,0,.18); }
.toolbar button, .toolbar a { width: 40px; height: 40px; display: grid; place-items: center; border: 0; border-radius: 50%;
  background: transparent; cursor: pointer; text-decoration: none; transition: background .15s; }
.toolbar button:hover, .toolbar a:hover, .toolbar .on { background: rgba(255,255,255,.14); }
.toolbar .sep { width: 1px; height: 22px; background: rgba(255,255,255,.2); margin: 0 6px; }
.counter { display: flex; align-items: center; gap: 6px; font-size: 13px; padding: 0 6px; }
.counter input { width: 8ch; min-width: 42px; height: 30px; text-align: center; border: 1px solid rgba(255,255,255,.25); border-radius: 8px;
  background: rgba(255,255,255,.08); color: #fff; }
.counter input:focus { outline: none; border-color: #fff; }
.counter b { font-weight: 600; }
.mobile-only { display: none !important; }

/* ---------- drawers ---------- */
.drawer { position: fixed; top: 0; bottom: 0; left: 0; width: min(340px, 88vw); z-index: 20; background: var(--panel);
  box-shadow: 8px 0 32px rgba(0,0,0,.18); display: flex; flex-direction: column; animation: slidein .22s ease-out; }
.drawer[hidden] { display: none; }
@keyframes slidein { from { transform: translateX(-24px); opacity: 0; } }
.drawer-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 16px 10px 20px; }
.drawer-head h2 { margin: 0; font-size: 15px; font-weight: 600; }
.drawer-head button { border: 0; background: none; cursor: pointer; padding: 6px; border-radius: 8px; }
.drawer-head button:hover { background: rgba(0,0,0,.06); }

.thumbs { overflow-y: auto; padding: 6px 16px 24px; display: grid; grid-template-columns: 1fr 1fr; gap: 14px 12px; }
.thumb { border: 0; padding: 0; background: none; cursor: pointer; text-align: center; font-size: 11px; color: var(--muted); }
.thumb .t-img { aspect-ratio: var(--page-ratio, 0.707); background: #eee8de; border-radius: 3px; overflow: hidden;
  box-shadow: 0 2px 8px rgba(0,0,0,.12); outline: 2px solid transparent; outline-offset: 2px; transition: outline-color .15s, transform .15s; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb:hover .t-img { transform: translateY(-2px); }
.thumb.current .t-img { outline-color: var(--accent); }
.thumb span { display: block; margin-top: 6px; }

.search-form { padding: 0 16px 12px; }
.search-form input { width: 100%; height: 40px; padding: 0 12px; border: 1px solid #d8d1c4; border-radius: 10px; background: #fff; }
.search-form input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.results { list-style: none; margin: 0; padding: 0 8px 24px; overflow-y: auto; }
.results li button { width: 100%; text-align: left; border: 0; background: none; cursor: pointer; padding: 10px 12px; border-radius: 10px; font-size: 13px; line-height: 1.45; }
.results li button:hover { background: rgba(0,0,0,.05); }
.results .pg { display: block; font-size: 11px; font-weight: 600; color: var(--accent); letter-spacing: .06em; text-transform: uppercase; margin-bottom: 2px; }
.results mark { background: #f1e2b8; color: inherit; border-radius: 2px; }
.results .empty { padding: 12px; color: var(--muted); font-size: 13px; }

/* ---------- zoom ---------- */
.zoom { position: fixed; inset: 0; z-index: 30; background: rgba(28, 27, 24, .94); }
.zoom[hidden] { display: none; }
.zoom-canvas { position: absolute; inset: 0; overflow: hidden; cursor: grab; touch-action: none; }
.zoom-canvas.dragging { cursor: grabbing; }
.zoom-inner { position: absolute; left: 0; top: 0; display: flex; transform-origin: 0 0; will-change: transform; }
.zoom-inner img { display: block; height: 100%; user-select: none; -webkit-user-drag: none; }
.zoom-bar { position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%); display: flex; align-items: center; gap: 4px;
  padding: 6px 8px; border-radius: 999px; background: rgba(255,255,255,.12); color: #fff; }
.zoom-bar button { width: 40px; height: 40px; border: 0; border-radius: 50%; background: transparent; cursor: pointer; display: grid; place-items: center; }
.zoom-bar button:hover { background: rgba(255,255,255,.14); }
.zoom-bar span { min-width: 52px; text-align: center; font-size: 13px; }

/* ---------- toast ---------- */
.toast { position: fixed; left: 50%; bottom: calc(var(--toolbar-h) + 34px); transform: translate(-50%, 10px); z-index: 40;
  background: var(--ink); color: #fff; font-size: 13px; padding: 9px 14px; border-radius: 10px; opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- small screens ---------- */
@media (max-width: 720px) {
  .book-wrap { inset: 6px 8px 10px; }
  .side { display: none; }
  .mobile-only { display: grid !important; }
  .toolbar { margin: 0 8px 10px; padding: 4px 6px; max-width: calc(100vw - 16px); }
  .toolbar button, .toolbar a { width: 34px; height: 36px; }
  .toolbar .sep { display: none; }
  .counter { gap: 4px; padding: 0 2px; white-space: nowrap; }
  .counter input { width: 38px; }
  .toolbar [data-act="sound"], .toolbar [data-act="fullscreen"] { display: none; }
  .topbar { padding: 10px 14px 0; min-height: 34px; }
}

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

/* ---- swatch card overlay (warm, bright) ---- */
.swatch-card { position: fixed; inset: 0; z-index: 60; display: grid; place-items: center; padding: 16px;
  background: rgba(239, 234, 226, .94); backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px); }
.swatch-card[hidden] { display: none; }
.sc-panel { position: relative; display: flex; flex-direction: column; align-items: center; gap: 12px;
  max-width: min(560px, 100%); max-height: 100%; }
.sc-img { display: block; max-width: 100%; max-height: calc(100vh - 150px); width: auto; height: auto;
  border-radius: 8px; background: #fff; box-shadow: 0 18px 50px rgba(80, 64, 36, .22); }
.sc-close { position: absolute; top: -6px; right: -6px; width: 44px; height: 44px; border-radius: 50%;
  border: 1px solid #d9d2c5; background: #fff; color: #25241f; cursor: pointer; display: grid; place-items: center;
  box-shadow: 0 6px 18px rgba(80, 64, 36, .18); }
.sc-close svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.sc-close:hover { background: #f6f2ea; }
.sc-actions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.sc-btn { padding: 11px 16px; border-radius: 10px; font-size: 14px; font-weight: 600; text-decoration: none;
  border: 1px solid #d9d2c5; background: #fff; color: #25241f; }
.sc-btn.sc-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.sc-hint { margin: 0; font-size: 12px; color: #7a746c; text-align: center; }
