/* ─────────────────────────────────────────────────────────────────
   Blackout — paper-textured book pages with felt-tip redactions.
   ───────────────────────────────────────────────────────────────── */

:root {
  --paper:        #f1e7d2;
  --paper-deep:   #e6dabf;
  --ink:          #1a1410;
  --ink-soft:     #4a3f33;
  --accent:       #6f4a2a;
  --shadow-1:     0 30px 60px -22px rgba(40, 24, 8, 0.35);
  --shadow-2:     0 18px 36px -20px rgba(40, 24, 8, 0.25);
  --page-w:       640px;
  --page-pad-x:   72px;
  --page-pad-t:   84px;
  --page-pad-b:   104px;
  --line-h:       1.65;
  --font-size:    18px;
  --bar-bg:       rgba(245, 238, 222, 0.78);
  --bar-border:   rgba(74, 63, 51, 0.18);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: #d9caa6;
  background-image:
    radial-gradient(ellipse at top, #e2d2ad 0%, #c9b888 80%),
    repeating-linear-gradient(45deg, transparent 0 6px, rgba(0,0,0,0.018) 6px 7px);
  color: var(--ink);
  font-family: 'EB Garamond', Garamond, Georgia, 'Times New Roman', serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  min-height: 100vh;
}

/* ─── Control bar ─────────────────────────────────────────────── */

.controls {
  position: fixed;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 10px;
  background: var(--bar-bg);
  border: 1px solid var(--bar-border);
  border-radius: 14px;
  backdrop-filter: saturate(140%) blur(10px);
  -webkit-backdrop-filter: saturate(140%) blur(10px);
  box-shadow: 0 8px 24px -10px rgba(40, 24, 8, 0.25);
  font-size: 13px;
  letter-spacing: 0.02em;
  transition: transform 320ms cubic-bezier(0.4, 0.1, 0.3, 1), opacity 320ms ease;
  user-select: none;
}
/* Slide-up dismiss when idle. Hover/focus brings it back. */
.controls.idle { transform: translate(-50%, -120%); opacity: 0.85; }
.controls:hover, .controls:focus-within {
  transform: translateX(-50%) !important;
  opacity: 1 !important;
  transition-duration: 180ms;
}

.controls select,
.controls input,
.controls button {
  font-family: inherit;
  font-size: 13px;
  color: var(--ink);
  background: transparent;
  border: none;
  border-radius: 8px;
  padding: 6px 11px;
  cursor: pointer;
  letter-spacing: 0.02em;
  transition: background 120ms ease, color 120ms ease;
}
.controls button:hover,
.controls select:hover { background: rgba(74, 63, 51, 0.08); }
.controls button:active { background: rgba(74, 63, 51, 0.16); }
.controls button:focus-visible,
.controls select:focus-visible,
.controls input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.controls select {
  appearance: none;
  -webkit-appearance: none;
  padding-right: 22px;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='8' height='8' viewBox='0 0 8 8'><path d='M1 2.5l3 3 3-3' fill='none' stroke='%231a1410' stroke-width='1.2' stroke-linecap='round'/></svg>");
  background-repeat: no-repeat;
  background-position: right 8px center;
  max-width: 200px;
  text-overflow: ellipsis;
}

.page-nav {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 0 6px;
  border-left: 1px solid var(--bar-border);
  border-right: 1px solid var(--bar-border);
}
.page-nav button {
  padding: 4px 10px;
  font-size: 16px;
  line-height: 1;
}
.page-nav input {
  width: 44px;
  text-align: center;
  font-variant-numeric: tabular-nums;
  cursor: text;
  -moz-appearance: textfield;
}
.page-nav input::-webkit-outer-spin-button,
.page-nav input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
.page-total {
  color: var(--ink-soft);
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  padding-right: 4px;
}
.page-total::before { content: "/ "; }

/* Hide hint after first interaction. */
.hint {
  position: fixed;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10;
  font-size: 11.5px;
  letter-spacing: 0.08em;
  text-transform: lowercase;
  color: var(--ink-soft);
  background: var(--bar-bg);
  border: 1px solid var(--bar-border);
  border-radius: 999px;
  padding: 6px 14px;
  pointer-events: none;
  opacity: 0.78;
  transition: opacity 800ms ease;
  user-select: none;
}
.hint.hidden { opacity: 0; }

/* ─── Stage and page ─────────────────────────────────────────── */

.stage {
  display: flex;
  justify-content: center;
  /* Padding-top tracks the actual control bar height (set by JS via --controls-h)
     so the page never starts behind the controls — even when they wrap. */
  padding: calc(var(--controls-h, 56px) + 28px) 24px 64px;
  min-height: 100vh;
}

.page {
  position: relative;
  width: 100%;
  max-width: var(--page-w);
  min-height: calc(100vh - 160px);
  padding: var(--page-pad-t) var(--page-pad-x) var(--page-pad-b);
  background-color: var(--paper);
  background-image: url("assets/paper-texture.jpg");
  background-size: cover;
  background-position: center;
  background-blend-mode: multiply;
  border-radius: 2px;
  box-shadow: var(--shadow-1), var(--shadow-2);
  /* subtle paper grain when no texture image is available yet */
  background-repeat: no-repeat;
}
/* Faint warm vignette around the page edge — feels like paper, not a card. */
.page::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(ellipse at center, transparent 55%, rgba(60, 40, 18, 0.13) 100%);
  border-radius: inherit;
  mix-blend-mode: multiply;
}

.page-text {
  font-size: var(--font-size);
  line-height: var(--line-h);
  text-align: justify;
  hyphens: auto;
  -webkit-hyphens: auto;
  color: var(--ink);
}
.page-text p {
  margin: 0;
  text-indent: 1.5em;
}
.page-text p:first-child { text-indent: 0; }
.page-text p:first-child::first-line {
  font-variant-caps: small-caps;
  letter-spacing: 0.04em;
}

.page-number {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 36px;
  text-align: center;
  font-size: 13px;
  font-style: italic;
  color: var(--ink-soft);
  letter-spacing: 0.1em;
  font-variant-numeric: oldstyle-nums;
}

/* ─── Words ──────────────────────────────────────────────────── */

.word {
  position: relative;
  display: inline;
  cursor: pointer;
  border-radius: 1px;
  -webkit-tap-highlight-color: transparent;
}
/* Page-text fades in as a single layer rather than staggering 200+ words. */
.page-text {
  animation: text-in 240ms ease;
}
@keyframes text-in {
  from { opacity: 0; transform: translateY(3px); }
  to   { opacity: 1; transform: none; }
}

.word:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Hover preview: subtle shadow rect overlay (disabled on touch). */
@media (hover: hover) and (pointer: fine) {
  .word:hover:not(.blacked) {
    background: rgba(26, 20, 16, 0.07);
  }
}

/* The felt-tip blackout itself. The SVG is sized larger than the word so
   the displacement filter has room to wander past the text edges. */
.word .blackout {
  position: absolute;
  left: -4px;
  top: -3px;
  width: calc(100% + 8px);
  height: calc(100% + 7px);
  display: block;
  pointer-events: none;
  overflow: visible;
  /* Ink-pour: the rect is wiped on from the left. The transform set in JS
     (rotation + jitter) is overridden during the wipe and restored after. */
  clip-path: inset(0 100% 0 0);
  transition: clip-path 220ms cubic-bezier(0.2, 0.7, 0.2, 1);
  z-index: 1;
}
.word.blacked .blackout {
  clip-path: inset(0 0 0 0);
}

/* Hide the underlying text once the rect has covered it
   (avoids ghost-text peeking through the SVG filter edges). */
.word.blacked {
  color: transparent;
}

/* During drag, kill text selection so painting feels clean. */
.dragging, .dragging * {
  user-select: none !important;
  -webkit-user-select: none !important;
}

/* ─── Loading state ──────────────────────────────────────────── */

.page.loading .page-text { opacity: 0; transition: opacity 180ms ease; }

/* ─── Modal: add your own text ────────────────────────────────── */

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(40, 24, 8, 0.42);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  animation: fade-in 180ms ease;
}
.modal-backdrop[hidden] { display: none; }

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes pop-in {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to   { opacity: 1; transform: none; }
}

.modal {
  width: 100%;
  max-width: 560px;
  max-height: calc(100vh - 48px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background-color: var(--paper);
  background-image: url("assets/paper-texture.jpg");
  background-size: cover;
  background-blend-mode: multiply;
  border-radius: 6px;
  box-shadow: var(--shadow-1), var(--shadow-2);
  font-family: 'EB Garamond', Garamond, Georgia, serif;
  color: var(--ink);
  animation: pop-in 200ms cubic-bezier(0.2, 0.7, 0.2, 1);
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 22px 28px 12px;
  border-bottom: 1px solid rgba(74, 63, 51, 0.15);
}
.modal-head h2 {
  margin: 0;
  font-size: 22px;
  font-weight: 500;
  letter-spacing: 0.01em;
}
.modal-close {
  background: transparent;
  border: none;
  color: var(--ink-soft);
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  padding: 4px 10px;
  border-radius: 999px;
  transition: background 120ms ease;
  font-family: inherit;
}
.modal-close:hover { background: rgba(74, 63, 51, 0.08); }
.modal-close:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.modal-body {
  padding: 18px 28px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.modal-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.modal-field > span {
  font-size: 12.5px;
  letter-spacing: 0.08em;
  text-transform: lowercase;
  color: var(--ink-soft);
  font-style: italic;
}
.modal-field-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 4px;
}
.modal-field input,
.modal-field textarea {
  font-family: inherit;
  font-size: 16px;
  color: var(--ink);
  background: rgba(255, 252, 244, 0.4);
  border: 1px solid rgba(74, 63, 51, 0.22);
  border-radius: 3px;
  padding: 10px 12px;
  width: 100%;
  resize: vertical;
  line-height: 1.5;
  transition: border-color 120ms ease, background 120ms ease;
}
.modal-field input:focus,
.modal-field textarea:focus {
  outline: none;
  border-color: var(--accent);
  background: rgba(255, 252, 244, 0.7);
}
.modal-field textarea {
  min-height: 220px;
  max-height: 50vh;
  font-size: 14.5px;
  line-height: 1.55;
}
.modal-upload {
  font-size: 12px;
  letter-spacing: 0.06em;
  color: var(--accent);
  cursor: pointer;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid rgba(111, 74, 42, 0.4);
  transition: background 120ms ease;
}
.modal-upload:hover { background: rgba(111, 74, 42, 0.08); }
.modal-meta {
  font-size: 12px;
  color: var(--ink-soft);
  font-style: italic;
  text-align: right;
  padding: 0 2px;
}
.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 16px 28px 22px;
  border-top: 1px solid rgba(74, 63, 51, 0.15);
}
.modal-btn {
  font-family: inherit;
  font-size: 14px;
  padding: 8px 18px;
  border-radius: 999px;
  cursor: pointer;
  letter-spacing: 0.02em;
  transition: background 120ms ease, color 120ms ease, opacity 120ms ease;
  border: 1px solid transparent;
}
.modal-btn.ghost {
  background: transparent;
  color: var(--ink-soft);
}
.modal-btn.ghost:hover { background: rgba(74, 63, 51, 0.08); color: var(--ink); }
.modal-btn.primary {
  background: var(--ink);
  color: var(--paper);
  border-color: var(--ink);
}
.modal-btn.primary:hover { background: var(--ink-soft); border-color: var(--ink-soft); }
.modal-btn.primary:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.modal-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* Mute toggle: small icon-only button at the right end of the bar. */
.controls .mute-btn {
  font-size: 15px;
  padding: 4px 9px;
  letter-spacing: 0;
  color: var(--ink);
}
.controls .mute-btn.muted { color: var(--ink-soft); opacity: 0.6; }

/* Custom books get a × button next to them in the dropdown — but native
   <option> doesn't support markup. Instead, expose a "Remove this text"
   button beside the dropdown when a custom book is active. */
.controls .remove-custom {
  font-size: 13px;
  color: var(--ink-soft);
  padding: 4px 8px;
  border-radius: 999px;
  background: transparent;
  border: none;
  cursor: pointer;
  display: none;
}
.controls .remove-custom:hover { background: rgba(150, 40, 30, 0.12); color: #8a2a22; }
.controls.has-custom .remove-custom { display: inline-block; }

/* ─── Mobile ──────────────────────────────────────────────────── */

@media (max-width: 720px) {
  :root {
    --page-pad-x: 36px;
    --page-pad-t: 56px;
    --page-pad-b: 72px;
    --font-size: 17px;
  }
  .stage { padding-left: 12px; padding-right: 12px; padding-bottom: 40px; }
  .controls {
    top: 10px;
    gap: 2px;
    padding: 6px 8px;
    border-radius: 12px;
    width: calc(100vw - 20px);
    max-width: calc(100vw - 20px);
    flex-wrap: wrap;
    justify-content: center;
    font-size: 12px;
  }
  .controls select { max-width: 130px; font-size: 12px; padding: 4px 18px 4px 8px; }
  .controls button { padding: 4px 9px; font-size: 12px; }
  .controls .page-nav { padding: 0 4px; }
  .controls .page-nav input { width: 36px; }
  .controls.idle { transform: translate(-50%, -110%); }
  .hint { font-size: 10.5px; padding: 5px 12px; bottom: 10px; }
}
