:root {
  --text-xs: clamp(0.75rem, 0.7rem + 0.25vw, 0.875rem);
  --text-sm: clamp(0.875rem, 0.8rem + 0.35vw, 1rem);
  --text-base: clamp(1rem, 0.95rem + 0.25vw, 1.125rem);
  --text-lg: clamp(1.125rem, 1rem + 0.75vw, 1.5rem);
  --text-xl: clamp(1.5rem, 1.2rem + 1.25vw, 2.25rem);
  --text-script: clamp(1.1875rem, 1.02rem + 0.7vw, 1.5rem);

  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.25rem;
  --space-6: 1.5rem;
  --space-8: 2rem;
  --space-10: 2.5rem;
  --space-12: 3rem;
  --space-16: 4rem;
  --space-20: 5rem;

  --radius-sm: 0.375rem;
  --radius-md: 0.5rem;
  --radius-lg: 0.75rem;
  --radius-xl: 1rem;
  --radius-full: 9999px;

  --transition-interactive: 180ms cubic-bezier(0.16, 1, 0.3, 1);
  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);

  --font-display: 'Zodiak', 'Iowan Old Style', Georgia, serif;
  --font-body: 'Satoshi', 'Avenir Next', 'Segoe UI', sans-serif;

  --content-narrow: 640px;
  --content-default: 880px;

  --transport-h: 8rem;
}

:root,
[data-theme='light'] {
  --color-bg: #f3ece1;
  --color-surface: #faf6ee;
  --color-surface-2: #fffdf8;
  --color-surface-offset: #ebe3d4;
  --color-border: oklch(0.32 0.03 70 / 0.12);
  --color-text: #2a2318;
  --color-text-muted: #72685a;
  --color-text-faint: #74695b;
  --color-text-inverse: #fff8ec;
  --color-primary: #9a4a12;
  --color-primary-hover: #7c3a0c;
  --color-primary-active: #5e2b08;
  --color-cue: #e8a04a;
  --color-cue-ink: #2a1a08;
  --color-spoken: #8a8072;
  --color-success: #3f6b28;
  --color-error: #8a2f24;
  --shadow-sm: 0 1px 2px oklch(0.28 0.03 70 / 0.06);
  --shadow-md: 0 8px 24px oklch(0.28 0.03 70 / 0.1);
}

[data-theme='dark'] {
  --color-bg: #161310;
  --color-surface: #1e1a16;
  --color-surface-2: #26211b;
  --color-surface-offset: #2e2820;
  --color-border: oklch(0.9 0.03 80 / 0.1);
  --color-text: #ebe4d8;
  --color-text-muted: #9a9084;
  --color-text-faint: #8a8278;
  --color-text-inverse: #1a140c;
  --color-primary: #e8a04a;
  --color-primary-hover: #f0b56a;
  --color-primary-active: #c7842e;
  --color-cue: #e8a04a;
  --color-cue-ink: #1a140c;
  --color-spoken: #7a7368;
  --color-success: #8fbf6a;
  --color-error: #e08b7a;
  --shadow-sm: 0 1px 2px oklch(0 0 0 / 0.28);
  --shadow-md: 0 10px 28px oklch(0 0 0 / 0.36);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --color-bg: #161310;
    --color-surface: #1e1a16;
    --color-surface-2: #26211b;
    --color-surface-offset: #2e2820;
    --color-border: oklch(0.9 0.03 80 / 0.1);
    --color-text: #ebe4d8;
    --color-text-muted: #9a9084;
    --color-text-faint: #6e665c;
    --color-text-inverse: #1a140c;
    --color-primary: #e8a04a;
    --color-primary-hover: #f0b56a;
    --color-primary-active: #c7842e;
    --color-cue: #e8a04a;
    --color-cue-ink: #1a140c;
    --color-spoken: #7a7368;
    --color-success: #8fbf6a;
    --color-error: #e08b7a;
    --shadow-sm: 0 1px 2px oklch(0 0 0 / 0.28);
    --shadow-md: 0 10px 28px oklch(0 0 0 / 0.36);
  }
}

.skip-link {
  position: absolute;
  left: var(--space-4);
  top: var(--space-4);
  z-index: 20;
  padding: var(--space-2) var(--space-4);
  background: var(--color-primary);
  color: var(--color-text-inverse);
  border-radius: var(--radius-md);
  text-decoration: none;
  font-size: var(--text-sm);
  transform: translateY(-200%);
}

.skip-link:focus {
  transform: translateY(0);
}

[hidden] {
  display: none !important;
}

.app {
  min-height: 100dvh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  padding: var(--space-4) var(--space-4) var(--space-3);
  max-width: var(--content-default);
  margin-inline: auto;
  width: 100%;
  flex-shrink: 0;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: var(--space-1);
}

.history-btn svg {
  width: 20px;
  height: 20px;
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-width: 0;
  color: inherit;
  text-decoration: none;
}

.brand-mark {
  width: 28px;
  height: 28px;
  flex-shrink: 0;
  color: var(--color-primary);
}

.brand-copy {
  display: flex;
  flex-direction: column;
  gap: 0;
  min-width: 0;
}

.brand-name {
  font-family: var(--font-display);
  font-size: 1.5rem;
  font-weight: 500;
  letter-spacing: -0.02em;
  line-height: 1.1;
}

.icon-btn {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
}

.icon-btn:hover {
  background: var(--color-surface-offset);
  color: var(--color-text);
}

.icon-btn:active {
  transform: translateY(1px);
}

.icon-btn svg {
  width: 20px;
  height: 20px;
}

.main {
  flex: 1;
  min-height: 0;
  width: 100%;
  max-width: var(--content-default);
  margin-inline: auto;
  padding: 0 var(--space-4) var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.composer {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-xl);
  padding: var(--space-4);
  box-shadow: var(--shadow-sm);
}

.composer.is-drop {
  border-color: color-mix(in oklab, var(--color-primary) 55%, var(--color-border));
  box-shadow: 0 0 0 3px oklch(from var(--color-primary) l c h / 0.16);
}

/* While rehearsing, the composer is a plain line above the stage rather than a second card. */
.composer.is-collapsed {
  flex: 0 0 auto;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: 0 var(--space-1);
  background: transparent;
  border-color: transparent;
  box-shadow: none;
}

.composer-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-3);
  margin-bottom: var(--space-3);
}

.composer-label {
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--color-text);
}

.composer-hint.is-over {
  color: var(--color-error);
  font-weight: 500;
}

.composer-hint {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

.script-input {
  flex: 1;
  width: 100%;
  min-height: 9.5rem;
  resize: none;
  border: 1px solid var(--color-border);
  border-radius: calc(var(--radius-xl) - var(--space-4));
  background: var(--color-surface-2);
  padding: var(--space-4);
  line-height: 1.55;
  color: var(--color-text);
}

.script-input::placeholder {
  color: var(--color-text-faint);
}

.script-input:focus {
  outline: none;
  border-color: color-mix(in oklab, var(--color-primary) 55%, var(--color-border));
  box-shadow: 0 0 0 3px oklch(from var(--color-primary) l c h / 0.16);
}

.composer-actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  margin-top: var(--space-3);
}

.file-input {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.composer-status {
  margin-top: var(--space-2);
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

.composer-status.is-error {
  color: var(--color-error);
}

.btn {
  min-height: 44px;
  padding: 0 var(--space-4);
  border-radius: var(--radius-md);
  font-size: var(--text-sm);
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
}

.btn:active {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  transform: none;
}

.btn-primary {
  background: var(--color-primary);
  color: var(--color-text-inverse);
}

.btn-primary:hover:not(:disabled) {
  background: var(--color-primary-hover);
}

.btn-primary:active:not(:disabled) {
  background: var(--color-primary-active);
}

.btn-ghost {
  color: var(--color-text-muted);
  background: transparent;
}

.btn-ghost:hover:not(:disabled) {
  background: var(--color-surface-offset);
  color: var(--color-text);
}

.collapsed-meta {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
  min-width: 0;
}

.collapsed-meta strong {
  color: var(--color-text);
  font-weight: 500;
}

.stage {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: auto;
  scroll-padding-top: var(--space-6);
  scroll-padding-bottom: var(--space-10);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-xl);
  padding: var(--space-6) var(--space-5);
  box-shadow: var(--shadow-sm);
}

.stage::after {
  content: '';
  pointer-events: none;
  position: absolute;
  inset: 0;
  border-radius: inherit;
  opacity: 0.035;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

.stage-fade {
  pointer-events: none;
  position: sticky;
  bottom: 0;
  z-index: 2;
  height: var(--space-12);
  margin-top: calc(var(--space-12) * -1);
  background: linear-gradient(to bottom, transparent, var(--color-surface));
}

.script {
  position: relative;
  z-index: 1;
  font-size: var(--text-script);
  line-height: 1.6;
  font-weight: 400;
  letter-spacing: -0.011em;
  max-width: 44ch;
  padding-bottom: var(--space-6);
}

.paragraph {
  margin-bottom: var(--space-6);
}

/* A block of up to a few hundred words (see renderScript). Off-screen blocks skip layout and
   painting, which keeps very long scripts fast. The padding/negative margin leave room so the
   highlighted word's outline isn't clipped at a block edge. */
.chunk {
  display: block;
  content-visibility: auto;
  contain-intrinsic-size: auto 10em;
  padding: 0.15em;
  margin: -0.15em;
}

.paragraph:last-child {
  margin-bottom: 0;
}

.word {
  display: inline;
  border-radius: 0.28em;
  padding: 0.04em 0.08em;
  color: var(--color-text);
  cursor: pointer;
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
}

.word.is-spoken {
  color: var(--color-spoken);
}

.word.is-current {
  background: var(--color-cue);
  color: var(--color-cue-ink);
  box-shadow: 0 0 0 1px var(--color-cue);
}

.word.is-done {
  color: var(--color-spoken);
}

@media (hover: hover) {
  .word:hover {
    background: color-mix(in oklab, var(--color-cue) 28%, transparent);
  }

  .word.is-current:hover {
    background: var(--color-cue);
  }
}

.complete-banner {
  position: relative;
  z-index: 1;
  margin-bottom: var(--space-5);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-md);
  background: color-mix(in oklab, var(--color-success) 12%, var(--color-surface-2));
  color: var(--color-success);
  font-size: var(--text-sm);
  font-weight: 500;
}

.transport {
  flex-shrink: 0;
  z-index: 10;
  background: color-mix(in oklab, var(--color-surface) 94%, transparent);
  border-top: 1px solid var(--color-border);
}

.transport-inner {
  max-width: var(--content-default);
  margin-inline: auto;
  padding: var(--space-3) var(--space-4) var(--space-4);
  display: grid;
  gap: var(--space-3);
}

.progress {
  height: 3px;
  width: 100%;
  background: var(--color-surface-offset);
  border-radius: var(--radius-full);
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  width: 0%;
  background: var(--color-primary);
  border-radius: inherit;
  transition: width 120ms linear;
}

.transport-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: var(--space-2);
}

.controls {
  display: flex;
  align-items: center;
  gap: var(--space-2);
}

.play-btn {
  width: 48px;
  height: 48px;
  border-radius: var(--radius-full);
  background: var(--color-primary);
  color: var(--color-text-inverse);
  display: grid;
  place-items: center;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
}

.play-btn:hover:not(:disabled) {
  background: var(--color-primary-hover);
}

.play-btn:active:not(:disabled) {
  transform: scale(0.97);
}

.play-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.play-btn svg {
  width: 22px;
  height: 22px;
}

.stats {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0;
  min-width: 0;
}

.stats-line {
  display: flex;
  align-items: baseline;
  gap: var(--space-3);
}

.reset-btn {
  min-height: 44px;
  /* Full 44px tap area, but it doesn't make the line taller (keeps the stats centred). */
  margin-block: -10px;
  padding-inline: 0;
  color: var(--color-text-muted);
}

.stats-primary {
  font-variant-numeric: tabular-nums;
  font-size: var(--text-sm);
  font-weight: 500;
}

.stats-secondary span {
  white-space: nowrap;
}

.stats-secondary {
  font-variant-numeric: tabular-nums;
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

.pace-wrap {
  justify-self: end;
  display: flex;
  justify-content: flex-end;
}

/* 44px buttons + 2px padding = 48px, the same height as the play button. */
.pace {
  display: flex;
  gap: var(--space-1);
  padding: 2px;
  background: var(--color-surface-offset);
  border-radius: var(--radius-full);
}

.pace-btn {
  min-height: 44px;
  min-width: 56px;
  padding: 0 var(--space-3);
  border-radius: var(--radius-full);
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--color-text-muted);
  font-variant-numeric: tabular-nums;
}

.pace-btn[aria-pressed='true'] {
  background: var(--color-primary);
  color: var(--color-text-inverse);
  box-shadow: none;
}

.pace-btn:hover:not([aria-pressed='true']) {
  color: var(--color-text);
}

.pace-btn:focus {
  outline: none;
}

.pace-btn:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* Custom pace: a compact inset field with a pencil, so it reads as "type a number here"
   rather than a third preset. */
.pace-custom {
  display: flex;
  align-items: center;
  gap: 0.25rem;
  min-height: 44px;
  padding: 0 0.6rem;
  border: 1px solid color-mix(in oklab, var(--color-text) 22%, transparent);
  border-radius: var(--radius-full);
  background: var(--color-surface-2);
  color: var(--color-text-muted);
  cursor: text;
}

.pace-custom:hover {
  border-color: color-mix(in oklab, var(--color-primary) 60%, transparent);
  color: var(--color-text);
}

.pace-custom:focus-within {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px oklch(from var(--color-primary) l c h / 0.2);
  color: var(--color-text);
}

.pace-custom:has(.is-active) {
  background: var(--color-primary);
  border-color: var(--color-primary);
  color: var(--color-text-inverse);
}

.pace-custom-icon {
  width: 15px;
  height: 15px;
  flex-shrink: 0;
}

.pace-custom-input {
  width: 2.1rem;
  min-height: 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  font-family: inherit;
  font-size: var(--text-xs);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  text-align: center;
  -moz-appearance: textfield;
  appearance: textfield;
}

.pace-custom-input::-webkit-outer-spin-button,
.pace-custom-input::-webkit-inner-spin-button {
  appearance: none;
  margin: 0;
}

.pace-custom-input::placeholder {
  color: var(--color-text-muted);
  font-weight: 500;
  opacity: 1;
}

.pace-custom:has(.is-active) .pace-custom-input::placeholder {
  color: var(--color-text-inverse);
}

.pace-custom-input:focus,
.pace-custom-input:focus-visible {
  outline: none;
}

/* Shortcut keycaps sit on the controls they trigger (desktop only). */
.key-hint {
  display: none;
  min-width: 1.6em;
  padding: 0.05em 0.45em;
  border: 1px solid var(--color-border);
  border-bottom-width: 2px;
  border-radius: var(--radius-sm);
  background: var(--color-surface-2);
  color: var(--color-text-muted);
  font-family: inherit;
  font-size: 0.7rem;
  font-weight: 500;
  line-height: 1.4;
  text-align: center;
}

.jump-touch {
  display: none;
}

.composer.is-collapsed [data-edit] {
  white-space: nowrap;
}

/* Narrow screens: the tap hint gets its own small line under the word count. */
@media (max-width: 719px) {
  .jump-hint {
    display: block;
    font-size: var(--text-xs);
  }

  .jump-sep {
    display: none;
  }
}

@media (hover: none) {
  .jump-pointer {
    display: none;
  }

  .jump-touch {
    display: inline;
  }
}

@media (min-width: 720px) {
  .site-header,
  .main,
  .transport-inner {
    padding-left: var(--space-8);
    padding-right: var(--space-8);
  }

  .stage {
    padding: var(--space-8) var(--space-10);
  }

  .script-input {
    min-height: 8rem;
  }

  .key-hint {
    display: inline-block;
  }

  .pace-wrap {
    justify-self: end;
    display: flex;
    align-items: center;
    gap: var(--space-3);
  }
}

@media (min-width: 960px) {
  .main {
    padding-bottom: var(--space-4);
  }
}

@media (max-width: 719px) {
  .script {
    font-size: 1.1875rem;
    line-height: 1.55;
    padding-bottom: var(--space-10);
  }

  /* Phones: one row. Stats left, play centred, pace right (the 120 preset is dropped to make
     room; any pace can still be typed into the custom field). Equal side columns keep play centred. */
  .transport-inner {
    padding-top: var(--space-2);
    padding-bottom: var(--space-3);
    gap: var(--space-2);
  }

  .transport-row {
    grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
    column-gap: var(--space-2);
  }

  .stats-line {
    column-gap: var(--space-2);
  }

  .reset-btn {
    min-height: 44px;
  }

  .controls {
    grid-column: 2;
  }

  .pace-wrap {
    grid-column: 3;
    justify-self: end;
  }

  .pace-btn-wide-only {
    display: none;
  }

  /* Match the stats' text size so the right side doesn't look undersized. */
  .pace-btn,
  .pace-custom-input {
    font-size: var(--text-sm);
  }

  .pace-btn {
    min-width: 44px;
    padding: 0 0.45rem;
    -webkit-tap-highlight-color: transparent;
  }

  .pace-custom {
    padding: 0 0.5rem;
  }

  .pace-custom-input {
    width: 2rem;
    -webkit-tap-highlight-color: transparent;
  }
}

.history {
  width: min(560px, calc(100vw - var(--space-8)));
  max-height: min(34rem, calc(100dvh - var(--space-8)));
  margin: auto;
  padding: var(--space-4) var(--space-5) var(--space-5);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-xl);
  background: var(--color-surface);
  color: var(--color-text);
  box-shadow: var(--shadow-md);
  overflow: auto;
}

.history[open] {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.history::backdrop {
  background: oklch(0.2 0.02 70 / 0.5);
}

.history-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
}

.history-head h2 {
  font-family: var(--font-display);
  font-size: var(--text-lg);
  font-weight: 500;
  letter-spacing: -0.02em;
}

.history-status {
  font-size: var(--text-sm);
  color: var(--color-text-muted);
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.history-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
}

.history-open {
  flex: 1;
  min-width: 0;
  min-height: 44px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-md);
  text-align: left;
}

.history-open:hover {
  background: var(--color-surface-offset);
}

.history-name {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: var(--text-sm);
  font-weight: 500;
}

.history-meta {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

.history-delete:hover:not(:disabled) {
  color: var(--color-error);
}

@media (max-width: 719px) {
  .history-btn {
    padding-inline: var(--space-3);
  }
}

.history-note {
  font-size: var(--text-xs);
  color: var(--color-text-muted);
}

/* Narrowest phones (e.g. 320px): trim the pace selector so it clears the play button. */
@media (max-width: 359px) {
  .transport-row {
    column-gap: var(--space-1);
  }

  .pace {
    gap: 2px;
  }

  .pace-custom {
    padding: 0 0.4rem;
  }

  .pace-custom-input {
    width: 1.7rem;
  }
}
