/* ── Reset ──────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

/* ── Tokens ─────────────────────────────────────────────── */
:root {
  --chrome:        #18130e;   /* warm dark — matches book ink */
  --chrome-2:      #26201a;
  --chrome-3:      #352e27;
  --border:        rgba(255,255,255,0.08);
  --text:          #e8e3da;
  --text-soft:     #8a847c;
  --paper:         #f4efe4;   /* matches book page */
  --toolbar-h:     50px;
  --shadow:        0 6px 32px rgba(0,0,0,0.6);
}

/* ── Root ───────────────────────────────────────────────── */
html, body {
  height: 100%;
  width: 100%;
  overflow: hidden;
  background: var(--chrome);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 14px;
  -webkit-font-smoothing: antialiased;
}

/* Shell */
#shell {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100vw;
}

/* ── Toolbar ────────────────────────────────────────────── */
#toolbar {
  flex: 0 0 var(--toolbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  background: var(--chrome);
  border-bottom: 1px solid var(--border);
  z-index: 20;
  gap: 16px;
  user-select: none;
}

.tb-edge {
  flex: 1;
  display: flex;
  align-items: center;
}

.tb-right { justify-content: flex-end; }

.tb-center {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-shrink: 0;
}

#book-title {
  font-style: italic;
  font-size: 13px;
  letter-spacing: 0.04em;
  color: var(--text-soft);
}

#page-info {
  font-size: 12px;
  color: var(--text-soft);
  min-width: 90px;
  text-align: center;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
}

#toolbar button {
  appearance: none;
  background: none;
  border: none;
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
  padding: 6px 10px;
  border-radius: 4px;
  transition: background 0.12s;
  line-height: 1;
  touch-action: manipulation;
}

#toolbar button:hover  { background: var(--chrome-2); }
#toolbar button:active { background: var(--chrome-3); }

#toolbar button:disabled {
  opacity: 0.25;
  cursor: default;
}
#toolbar button:disabled:hover { background: none; }

#btn-download {
  font-size: 11px;
  letter-spacing: 0.06em;
  color: var(--text-soft);
  text-decoration: none;
  padding: 5px 10px;
  border-radius: 4px;
  transition: background 0.12s, color 0.12s;
  white-space: nowrap;
}
#btn-download:hover {
  background: var(--chrome-2);
  color: var(--text);
}

/* ── Stage ──────────────────────────────────────────────── */
#stage {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  padding: 12px 12px 18px;
}

/* ── Spread (two-up on desktop) ─────────────────────────── */
#spread {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0;
  height: 100%;
  max-height: 100%;
  position: relative;
}

#gutter {
  width: 2px;
  background: var(--chrome-3);
  align-self: stretch;
  flex-shrink: 0;
  opacity: 0;  /* hidden until both pages present */
  transition: opacity 0.2s;
}

#spread.two-up #gutter { opacity: 1; }

#spread canvas {
  display: block;
  box-shadow: var(--shadow);
  background: var(--paper);
  max-height: calc(100vh - var(--toolbar-h) - 24px);
  object-fit: contain;
  transition: opacity 0.15s;
}

#spread canvas.hidden {
  display: none;
}

/* Click zones (desktop — left/right thirds) */
.zone {
  position: absolute;
  top: 0;
  height: 100%;
  width: 20%;
  background: none;
  border: none;
  cursor: pointer;
  opacity: 0;
  z-index: 10;
  touch-action: manipulation;
}

#zone-prev { left: 0; cursor: w-resize; }
#zone-next { right: 0; cursor: e-resize; }

/* ── Loading overlay ────────────────────────────────────── */
#loading-state {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--chrome);
  z-index: 100;
  transition: opacity 0.3s;
  pointer-events: none;
}

body:not(.loading) #loading-state {
  opacity: 0;
}

#loading-text {
  font-size: 13px;
  letter-spacing: 0.1em;
  color: var(--text-soft);
}

/* ── Mobile ─────────────────────────────────────────────── */
@media (max-width: 767px) {
  :root { --toolbar-h: 46px; }

  #toolbar { padding: 0 12px; gap: 8px; }
  #book-title { display: none; }
  #page-info { min-width: 70px; }

  #toolbar button { font-size: 20px; padding: 6px 8px; }

  #stage {
    padding: 8px 0;
    align-items: flex-start;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  #spread {
    flex-direction: column;
    height: auto;
    width: 100%;
    gap: 0;
  }

  #gutter { display: none; }

  #spread canvas {
    width: 100% !important;
    height: auto !important;
    max-height: none;
    box-shadow: none;
  }

  .zone { display: none; }
}

/* ── Password gate ─────────────────────────────────────── */
#gate {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--chrome);
  z-index: 200;
}
body:not(.locked) #gate { display: none; }
body.locked #shell { display: none; }

#gate-form {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: 36px 40px;
  max-width: 360px;
  width: calc(100% - 40px);
}

.gate-title {
  font-family: 'Cormorant Garamond', Georgia, serif;
  font-weight: 300;
  font-size: 42px;
  letter-spacing: 0.01em;
  color: var(--text);
  margin-bottom: 0;
}

.gate-subtitle {
  font-style: italic;
  font-size: 13px;
  letter-spacing: 0.08em;
  color: var(--text-soft);
  margin-bottom: 18px;
}

#gate-pw {
  width: 100%;
  background: var(--chrome-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 12px 14px;
  font-size: 15px;
  color: var(--text);
  font-family: inherit;
  letter-spacing: 0.03em;
  outline: none;
  transition: border-color 0.12s;
}
#gate-pw:focus { border-color: rgba(255,255,255,0.24); }

#gate-submit {
  appearance: none;
  width: 100%;
  background: var(--chrome-3);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 11px 16px;
  font-size: 13px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--text);
  cursor: pointer;
  transition: background 0.12s;
  font-family: inherit;
}
#gate-submit:hover  { background: #443c33; }
#gate-submit:active { background: #504638; }

#gate-error {
  font-size: 12px;
  letter-spacing: 0.04em;
  color: #c48a7a;
  min-height: 16px;
  text-align: center;
}

/* ── Keyboard hint (desktop only) ──────────────────────── */
@media (min-width: 768px) {
  #stage::after {
    content: '← → or click edges to turn';
    position: absolute;
    bottom: 8px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 10px;
    letter-spacing: 0.08em;
    color: var(--text-soft);
    opacity: 0.5;
    pointer-events: none;
    white-space: nowrap;
  }
}
