/* ============================================================
   Voice Comparison Site — Blind Reading Room
   DESIGN.md: scholarly critical edition, NYRB register
   EB Garamond (display) + Source Serif 4 (body), ivory paper
   ============================================================ */

/* ── Reset ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 100%; scroll-behavior: smooth; }
body { font-family: var(--font-body); background: var(--paper); color: var(--ink); }
/* The HTML [hidden] attribute must always win over any display: rules below. */
[hidden] { display: none !important; }

/* ── Design tokens ── */
:root {
  /* Paper and Ink */
  --paper:       #f8f4ec;
  --paper-deep:  #f0ebdd;
  --paper-edge:  #e1d9c3;
  --paper-glow:  #fcfaf3;

  --ink:         #1a1d1c;
  --ink-body:    #2b302d;
  --ink-soft:    #56605a;
  --ink-faint:   #8a8f87;
  --ink-ghost:   #b8bcb3;

  /* Annotation ink */
  --mark-approve:      #7a2820;
  --mark-approve-fade: rgba(122, 40, 32, 0.14);
  --mark-reserve:      #314a3b;
  --mark-reserve-fade: rgba(49, 74, 59, 0.12);
  --mark-note:         #8e4f17;

  /* Selection */
  --selection: rgba(122, 40, 32, 0.08);

  /* Typography */
  --font-display: 'EB Garamond', 'Garamond', 'Cormorant Garamond', 'Georgia', serif;
  --font-body:    'Source Serif 4', 'Source Serif Pro', 'Georgia', 'Times New Roman', serif;
  --font-mark:    'Source Serif 4', 'Georgia', serif;
  --font-rail:    'EB Garamond', 'Georgia', serif;

  /* Type scale */
  --text-letter:  clamp(3.6rem, 8vw, 6.4rem);
  --text-title:   clamp(1.6rem, 2.6vw, 2.0rem);
  --text-opener:  clamp(1.1rem, 1.4vw, 1.2rem);
  --text-body:    clamp(1.0625rem, 1.2vw, 1.125rem);
  --text-meta:    0.875rem;
  --text-rail:    0.8125rem;
  --text-mark:    0.75rem;

  /* Weights */
  --w-regular: 400;
  --w-medium:  500;
  --w-semi:    600;

  /* Line heights */
  --lh-display: 1.1;
  --lh-title:   1.25;
  --lh-opener:  1.55;
  --lh-body:    1.72;
  --lh-meta:    1.4;

  /* Letter spacing */
  --ls-display:   -0.01em;
  --ls-body:       0em;
  --ls-smallcaps:  0.12em;
  --ls-rail:       0.04em;

  /* Spacing */
  --space-xs:  0.25rem;
  --space-sm:  0.5rem;
  --space-md:  1rem;
  --space-lg:  1.5rem;
  --space-xl:  2rem;
  --space-2xl: 3rem;
  --space-3xl: 4.5rem;
  --space-4xl: 7rem;
  --space-5xl: 11rem;

  /* Layout */
  --col-reading:  min(64ch, calc(100vw - 2 * var(--gutter)));
  --col-wide:     min(78ch, calc(100vw - 2 * var(--gutter)));
  --gutter:       clamp(1.5rem, 4vw, 3rem);
  --gutter-rail:  clamp(2.25rem, 6vw, 4.5rem);
  --rail-width:   clamp(2.75rem, 5vw, 4rem);

  /* Animation */
  --ease-out:      cubic-bezier(0.16, 1, 0.3, 1);
  --ease-out-soft: cubic-bezier(0.25, 1, 0.5, 1);
  --ease-in-out:   cubic-bezier(0.65, 0, 0.35, 1);

  --dur-instant: 100ms;
  --dur-fast:    140ms;
  --dur-normal:  280ms;
  --dur-slow:    520ms;
}

/* ── Text selection ── */
::selection {
  background: var(--selection);
  color: inherit;
}

/* ── Skip link ── */
.skip-link {
  position: absolute;
  top: -100%;
  left: var(--space-md);
  background: var(--paper);
  color: var(--ink);
  padding: var(--space-sm) var(--space-md);
  border: 1px solid var(--paper-edge);
  font-family: var(--font-body);
  font-size: var(--text-meta);
  z-index: 200;
  text-decoration: none;
}
.skip-link:focus { top: var(--space-sm); }

/* ── Rails (desktop) ── */
.rail-left,
.rail-right {
  position: fixed;
  top: 50%;
  transform: translateY(-50%);
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  list-style: none;
}

.rail-left {
  left: var(--gutter);
  align-items: flex-start;
}

.rail-right {
  right: var(--gutter);
  align-items: flex-end;
}

/* Rail letter buttons */
.rail-letter {
  font-family: var(--font-rail);
  font-size: var(--text-rail);
  font-weight: var(--w-regular);
  letter-spacing: var(--ls-rail);
  color: var(--ink-ghost);
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  transition: color var(--dur-normal) var(--ease-out-soft);
  line-height: 1.2;
}
.rail-letter:hover { color: var(--ink-soft); }
.rail-letter.is-passed { color: var(--ink-soft); }
.rail-letter.is-current { color: var(--ink); }

/* Right rail counts */
.rail-count-marks {
  font-family: var(--font-display);
  font-size: var(--text-title);
  font-weight: var(--w-medium);
  color: var(--mark-approve);
  line-height: 1.1;
  min-width: 2ch;
  text-align: right;
}
.rail-count-notes {
  font-family: var(--font-display);
  font-size: var(--text-title);
  font-weight: var(--w-medium);
  font-style: italic;
  color: var(--mark-note);
  line-height: 1.1;
  min-width: 2ch;
  text-align: right;
}

/* ── Main reading column ── */
main {
  /* Offset right of centre to make room for left rail */
  margin: 0 auto 0 calc(var(--gutter-rail) + var(--rail-width));
  max-width: var(--col-reading);
  padding-bottom: 5rem; /* room for bottom strip */
}

/* ── Intro ── */
.intro {
  padding: clamp(5rem, 10vh, 8rem) 0 clamp(3rem, 6vh, 5rem);
}

.site-title {
  font-family: var(--font-display);
  font-size: var(--text-title);
  font-weight: var(--w-medium);
  line-height: var(--lh-title);
  letter-spacing: var(--ls-display);
  color: var(--ink);
}

/* Title rule: width follows the title text, not full-width */
.title-rule {
  height: 1px;
  background: var(--paper-edge);
  margin: var(--space-sm) 0 var(--space-lg);
  /* JS sets exact width; fallback is 100% */
  width: 100%;
  max-width: 30ch;
}

.site-tagline {
  font-family: var(--font-body);
  font-size: var(--text-body);
  font-style: italic;
  color: var(--ink-body);
  line-height: var(--lh-body);
  max-width: var(--col-reading);
  margin-bottom: var(--space-xl);
}

/* "How it works" disclosure */
.how-it-works-toggle {
  display: inline-flex;
  align-items: center;
  gap: var(--space-xs);
  font-family: var(--font-body);
  font-size: var(--text-meta);
  color: var(--ink-soft);
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
  margin-bottom: var(--space-md);
  text-decoration: underline;
  text-underline-offset: 0.2em;
  text-decoration-color: var(--paper-edge);
  transition: color var(--dur-fast) var(--ease-out-soft);
}
.how-it-works-toggle:hover { color: var(--ink); }
.how-it-works-toggle[aria-expanded="true"] .how-arrow {
  transform: rotate(180deg);
}
.how-arrow {
  display: inline-block;
  transition: transform var(--dur-normal) var(--ease-in-out);
  font-size: 0.6em;
}

.how-it-works-body {
  display: grid;
  grid-template-rows: 0fr;
  transition: grid-template-rows var(--dur-normal) var(--ease-in-out);
  overflow: hidden;
}
.how-it-works-body.is-open { grid-template-rows: 1fr; }
.how-it-works-inner {
  overflow: hidden;
  padding-bottom: var(--space-sm);
}
.how-it-works-inner p {
  font-family: var(--font-body);
  font-size: var(--text-meta);
  color: var(--ink-soft);
  line-height: var(--lh-body);
  max-width: 55ch;
  margin-bottom: var(--space-sm);
}

/* Scroll hint arrow */
.scroll-hint {
  margin-top: var(--space-3xl);
  color: var(--ink-soft);
  font-size: 1.25rem;
  animation: hint-bob 3.2s ease-in-out infinite;
  display: inline-block;
}
@keyframes hint-bob {
  0%, 100% { transform: translateY(0); }
  50%       { transform: translateY(4px); }
}

/* ── Version articles ── */
.version-section {
  padding: clamp(4rem, 8vh, 7rem) 0 clamp(3rem, 6vh, 5rem);
  border-top: 1px solid var(--paper-edge);
  position: relative;

  /* Entrance animation */
  opacity: 0;
  transform: translateY(12px);
  transition:
    opacity var(--dur-slow) var(--ease-out),
    transform var(--dur-slow) var(--ease-out);
}
.version-section.is-visible {
  opacity: 1;
  transform: translateY(0);
}

/* The display letter (A–N) */
.version-letter {
  font-family: var(--font-display);
  font-size: var(--text-letter);
  font-weight: var(--w-medium);
  font-style: italic;
  line-height: var(--lh-display);
  letter-spacing: var(--ls-display);
  color: var(--ink);
  display: block;
  margin-bottom: var(--space-md);
}

/* Version heading: "Version H — Rendering VIII" */
.version-heading {
  font-family: var(--font-display);
  font-size: var(--text-title);
  font-weight: var(--w-medium);
  line-height: var(--lh-title);
  letter-spacing: var(--ls-display);
  color: var(--ink);
  margin-bottom: var(--space-sm);
}

.version-rule {
  height: 1px;
  background: var(--paper-edge);
  margin-bottom: var(--space-sm);
  max-width: 28ch;
}

/* Version meta: "Approximately 1,500 words. Annotation open." */
.version-meta {
  font-family: var(--font-body);
  font-size: var(--text-meta);
  font-style: italic;
  color: var(--ink-soft);
  line-height: var(--lh-meta);
  margin-bottom: var(--space-2xl);
}

/* Article body */
.version-body {
  max-width: var(--col-reading);
  position: relative;
}

.version-body p {
  font-family: var(--font-body);
  font-size: var(--text-body);
  font-weight: var(--w-regular);
  line-height: var(--lh-body);
  color: var(--ink-body);
  margin-bottom: var(--space-lg);
  max-width: 64ch;
  word-spacing: 0.01em;
}

.version-body h3,
.version-body h4,
.version-body h5 {
  font-family: var(--font-display);
  font-size: var(--text-opener);
  font-weight: var(--w-medium);
  color: var(--ink);
  margin: var(--space-2xl) 0 var(--space-md);
  line-height: var(--lh-title);
  letter-spacing: var(--ls-display);
}

.version-body strong {
  font-weight: var(--w-medium);
  color: var(--ink);
}

.version-body em {
  font-style: italic;
}

/* First paragraph opener treatment:
   First word in small-caps, letter-spaced */
.version-body .opener-word {
  font-variant: small-caps;
  letter-spacing: var(--ls-smallcaps);
  font-weight: var(--w-medium);
}

/* ── Marks / annotations ── */

/* Approval mark: thin red-ink underline */
.mark-approve {
  text-decoration: underline;
  text-decoration-color: var(--mark-approve);
  text-decoration-thickness: 1.5px;
  text-underline-offset: 0.24em;
  cursor: pointer;
}

/* Reserve mark: crosshatch above text, not through it */
.mark-reserve {
  position: relative;
  cursor: pointer;
}
.mark-reserve::before {
  content: '';
  position: absolute;
  top: -3px;
  left: 0;
  right: 0;
  height: 1.5px;
  background-image: repeating-linear-gradient(
    to right,
    var(--mark-reserve) 0,
    var(--mark-reserve) 4px,
    transparent 4px,
    transparent 7px
  );
  pointer-events: none;
}

/* Marginal note indicator */
.margin-indicator {
  position: absolute;
  right: calc(-1 * var(--space-xl));
  width: 6px;
  height: 6px;
  background: var(--mark-note);
  cursor: pointer;
}

/* ── Popups ── */
.mark-popup,
.note-popup,
.remove-popup {
  position: fixed;
  z-index: 100;
  background: var(--paper-glow);
  border: 1px solid var(--paper-edge);
  border-radius: 3px;
  box-shadow: 0 4px 16px rgba(26, 29, 28, 0.08);
  padding: 0.5rem 0.75rem;
  display: flex;
  align-items: center;
  gap: 0;
  opacity: 0;
  transform: translateY(4px);
  transition:
    opacity var(--dur-fast) var(--ease-out),
    transform var(--dur-fast) var(--ease-out);
  pointer-events: none;
}
.mark-popup.is-visible,
.note-popup.is-visible,
.remove-popup.is-visible {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

.popup-btn {
  font-family: var(--font-mark);
  font-size: var(--text-mark);
  font-weight: var(--w-medium);
  font-variant: small-caps;
  letter-spacing: var(--ls-smallcaps);
  color: var(--ink-body);
  background: none;
  border: none;
  cursor: pointer;
  padding: 0.2rem 0.5rem;
  transition: color var(--dur-instant) var(--ease-out-soft);
  line-height: 1.4;
  white-space: nowrap;
}
.popup-mark:hover  { color: var(--mark-approve); }
.popup-flag:hover  { color: var(--mark-reserve); }
.popup-note:hover  { color: var(--mark-note); }
.remove-popup .popup-btn:hover { color: var(--mark-approve); }

.popup-divider {
  display: block;
  width: 1px;
  height: 1rem;
  background: var(--paper-edge);
  flex-shrink: 0;
}

/* Popup tail: 6x6 square rotated 45deg */
.popup-tail {
  position: absolute;
  bottom: -4px;
  left: 50%;
  transform: translateX(-50%) rotate(45deg);
  width: 6px;
  height: 6px;
  background: var(--paper-glow);
  border-right: 1px solid var(--paper-edge);
  border-bottom: 1px solid var(--paper-edge);
  pointer-events: none;
}

/* Note popup */
.note-popup {
  flex-direction: column;
  align-items: stretch;
  gap: var(--space-sm);
  padding: 0.75rem;
  min-width: 240px;
}
.note-textarea {
  font-family: var(--font-body);
  font-size: var(--text-meta);
  font-style: italic;
  color: var(--ink-body);
  background: var(--paper-deep);
  border: 1px solid var(--paper-edge);
  border-radius: 2px;
  padding: var(--space-sm);
  resize: vertical;
  min-height: 60px;
  line-height: var(--lh-body);
  width: 100%;
}
.note-textarea:focus {
  outline: none;
  border-color: var(--ink-soft);
}
.note-textarea::placeholder { color: var(--ink-faint); }

.note-actions {
  display: flex;
  gap: var(--space-sm);
  justify-content: flex-end;
}
.note-confirm,
.note-cancel {
  font-family: var(--font-body);
  font-size: var(--text-meta);
  color: var(--ink-soft);
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
  text-decoration: underline;
  text-underline-offset: 0.2em;
  text-decoration-color: var(--paper-edge);
}
.note-confirm:hover { color: var(--mark-approve); }
.note-cancel:hover  { color: var(--ink); }

/* ── Submit section ── */
.submit-section {
  padding: clamp(4rem, 8vh, 6rem) 0 clamp(6rem, 12vh, 10rem);
  border-top: 1px solid var(--paper-edge);
}

.submit-heading {
  font-family: var(--font-display);
  font-size: var(--text-title);
  font-weight: var(--w-medium);
  color: var(--ink);
  line-height: var(--lh-title);
  letter-spacing: var(--ls-display);
  margin-bottom: var(--space-sm);
}

.submit-rule {
  height: 1px;
  background: var(--paper-edge);
  max-width: 8ch;
  margin-bottom: var(--space-xl);
}

.submit-body {
  font-family: var(--font-body);
  font-size: var(--text-body);
  color: var(--ink-body);
  line-height: var(--lh-body);
  max-width: 52ch;
  margin-bottom: var(--space-xl);
}

.name-label,
.context-label {
  display: block;
  font-family: var(--font-body);
  font-size: var(--text-meta);
  color: var(--ink-soft);
  font-style: italic;
  margin-bottom: var(--space-xs);
  margin-top: var(--space-lg);
}

.name-input,
.context-input {
  display: block;
  width: 100%;
  max-width: 40ch;
  font-family: var(--font-body);
  font-size: var(--text-body);
  color: var(--ink);
  background: transparent;
  border: none;
  border-bottom: 1px solid var(--paper-edge);
  padding: var(--space-xs) 0;
  margin-bottom: var(--space-sm);
  outline: none;
  transition: border-color var(--dur-fast) var(--ease-out-soft);
}
.name-input:focus,
.context-input:focus {
  border-bottom-color: var(--ink-soft);
}
.name-input::placeholder,
.context-input::placeholder {
  color: var(--ink-faint);
  font-style: italic;
}

/* Submit link — text-link style, not a button */
.submit-link {
  display: inline-block;
  font-family: var(--font-display);
  font-size: var(--text-title);
  font-weight: var(--w-regular);
  color: var(--ink-ghost);
  background: none;
  border: none;
  cursor: not-allowed;
  padding: 0;
  margin-top: var(--space-xl);
  text-decoration: none;
  transition: color var(--dur-fast) var(--ease-out-soft);
  letter-spacing: var(--ls-display);
}
.submit-link:not(:disabled) {
  color: var(--ink);
  cursor: pointer;
}
.submit-link:not(:disabled):hover {
  text-decoration: underline;
  text-underline-offset: 0.2em;
}

/* ── Bottom strip ── */
.bottom-strip {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 52px;
  background: rgba(240, 235, 221, 0.92);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-top: 1px solid var(--paper-edge);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 var(--gutter);
  z-index: 40;
}

.strip-meta {
  font-family: var(--font-body);
  font-size: var(--text-meta);
  font-style: italic;
  color: var(--ink-soft);
}

.strip-submit {
  font-family: var(--font-body);
  font-size: var(--text-meta);
  color: var(--ink-ghost);
  background: none;
  border: none;
  cursor: not-allowed;
  padding: 0;
  text-decoration: underline;
  text-underline-offset: 0.2em;
  text-decoration-color: transparent;
  transition: color var(--dur-fast) var(--ease-out-soft);
}
.strip-submit:not(:disabled) {
  color: var(--ink);
  cursor: pointer;
  text-decoration-color: var(--paper-edge);
}
.strip-submit:not(:disabled):hover {
  text-decoration-color: var(--ink-soft);
}

/* ── Thank-you screen ── */
.thankyou-screen {
  position: fixed;
  inset: 0;
  background: var(--paper);
  z-index: 150;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--gutter);
  transition: background var(--dur-slow) var(--ease-out);
}
.thankyou-screen:not([hidden]).paper-warm {
  background: var(--paper-glow);
}

.thankyou-inner {
  max-width: var(--col-reading);
  width: 100%;
}

.thankyou-heading {
  font-family: var(--font-display);
  font-size: var(--text-title);
  font-weight: var(--w-medium);
  color: var(--ink);
  margin-bottom: var(--space-sm);
}

.thankyou-rule {
  height: 1px;
  background: var(--paper-edge);
  max-width: 10ch;
  margin-bottom: var(--space-xl);
}

.thankyou-inner p {
  font-family: var(--font-body);
  font-size: var(--text-body);
  color: var(--ink-body);
  line-height: var(--lh-body);
  max-width: 52ch;
  margin-bottom: var(--space-lg);
}

.thankyou-sig {
  font-family: var(--font-display);
  font-style: italic;
  color: var(--ink-soft);
  text-align: right;
  max-width: 52ch;
}

/* ── Loading / utility ── */
.loading-msg {
  font-family: var(--font-body);
  font-style: italic;
  color: var(--ink-faint);
  padding: var(--space-4xl) 0;
  text-align: left;
}

/* ── Reduced motion ── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
  .mark-popup, .note-popup, .remove-popup {
    transform: none !important;
    opacity: 1 !important;
  }
  .version-section {
    opacity: 1 !important;
    transform: none !important;
  }
  .scroll-hint { animation: none; }
}

/* ── Tablet (768px–1023px) ── */
@media (max-width: 1023px) {
  main {
    margin: 0 auto;
    max-width: var(--col-reading);
    padding-left: var(--gutter);
    padding-right: var(--gutter);
  }

  .rail-left {
    /* Collapse to thin dotted spine */
    left: 0;
    top: 0;
    bottom: 0;
    transform: none;
    width: 12px;
    height: 100%;
    flex-direction: column;
    justify-content: center;
    gap: 8px;
    padding: var(--space-2xl) 0;
    align-items: center;
  }

  .rail-letter {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--ink-ghost);
    color: transparent;
    font-size: 0;
    overflow: hidden;
    padding: 0;
  }
  .rail-letter.is-passed { background: var(--ink-soft); }
  .rail-letter.is-current {
    background: var(--ink);
    width: 8px;
    height: 8px;
  }

  .rail-right { display: none; }
}

/* ── Mobile (<768px) ── */
@media (max-width: 767px) {
  main {
    margin: 0 auto;
    padding-left: var(--gutter);
    padding-right: var(--gutter);
  }

  .rail-left {
    width: 6px;
    gap: 5px;
  }
  .rail-letter {
    width: 4px;
    height: 4px;
  }
  .rail-letter.is-current {
    width: 6px;
    height: 6px;
  }

  .version-letter {
    font-size: clamp(2.8rem, 14vw, 3.6rem);
  }

  .popup-btn {
    font-size: var(--text-body);
    min-height: 44px;
    padding: 0.5rem 0.75rem;
  }

  .note-popup { min-width: 200px; }

  .strip-meta { font-size: 0.75rem; }
}
