/* ==========================================================================
   Knoti — tokens
   ========================================================================== */

:root {
  color-scheme: light;
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #efede8;
  --border: #dcd9d2;
  --text: #1d1d1f;
  --text-muted: #6b6a66;
  --accent: #2f5bd3;
  --accent-text: #ffffff;
  --pivot: #d32f2f;
  --reticle: #c9c5bc;
  --error: #b3261e;
  --focus: #2f5bd3;

  --radius: 10px;
  --radius-lg: 16px;
  --gutter: 16px;
  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", Ubuntu, sans-serif;
  --font-word: "Segoe UI", Roboto, "Noto Sans", "Helvetica Neue", Arial, system-ui, sans-serif;
  --font-mono: ui-monospace, "SF Mono", "Cascadia Mono", "JetBrains Mono", Menlo, monospace;
  /* Where the focus letter sits across the stage: left of centre, so the
     longer right-hand part of a word has room. Keep in sync with .word grid. */
  --pivot-x: 40%;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #16171a;
  --surface: #1f2024;
  --surface-2: #2a2b30;
  --border: #393a40;
  --text: #ececec;
  --text-muted: #9a9aa0;
  --accent: #7c9cff;
  --accent-text: #0e1220;
  --pivot: #ff6b6b;
  --reticle: #46474e;
  --error: #ff8a80;
  --focus: #7c9cff;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #16171a;
    --surface: #1f2024;
    --surface-2: #2a2b30;
    --border: #393a40;
    --text: #ececec;
    --text-muted: #9a9aa0;
    --accent: #7c9cff;
    --accent-text: #0e1220;
    --pivot: #ff6b6b;
    --reticle: #46474e;
    --error: #ff8a80;
    --focus: #7c9cff;
  }
}

/* ==========================================================================
   Base
   ========================================================================== */

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 400 1rem/1.5 var(--font-ui);
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 820px;
  margin: 0 auto;
  padding: 0 var(--gutter) var(--gutter);
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}

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

[hidden] {
  display: none !important;
}

.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.sprite {
  display: none;
}

kbd {
  font: 0.75rem/1 var(--font-mono);
  padding: 0.2em 0.4em;
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 4px;
  background: var(--surface);
}

/* ==========================================================================
   Top bar
   ========================================================================== */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  max-width: 820px;
  margin: 0 auto;
  padding: 12px var(--gutter);
}

.brand {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

/* ==========================================================================
   Buttons
   ========================================================================== */

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  font-weight: 500;
  cursor: pointer;
  user-select: none;
  transition: background-color 120ms, border-color 120ms, opacity 120ms;
}

.button:hover {
  background: var(--surface-2);
}

.button:has(input:focus-visible) {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.button svg {
  width: 18px;
  height: 18px;
  flex: none;
}

.button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.button.primary:hover {
  filter: brightness(1.08);
}

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

.button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.icon-button {
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  cursor: pointer;
  transition: background-color 120ms;
}

.icon-button:hover {
  background: var(--surface-2);
}

.icon-button svg {
  width: 24px;
  height: 24px;
}

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

.icon-button.play {
  width: 64px;
  height: 64px;
  background: var(--accent);
  color: var(--accent-text);
}

.icon-button.play:hover {
  background: var(--accent);
  filter: brightness(1.08);
}

.icon-button.play svg {
  width: 30px;
  height: 30px;
}

/* ==========================================================================
   Input view
   ========================================================================== */

.view {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.dropzone {
  position: relative;
  flex: 1;
  display: flex;
  min-height: 240px;
}

#text-input {
  flex: 1;
  width: 100%;
  resize: none;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface);
  font-size: 1rem;
  line-height: 1.6;
}

#text-input:focus-visible {
  outline-offset: -1px;
}

.drop-overlay {
  position: absolute;
  inset: 0;
  display: none;
  place-items: center;
  border: 2px dashed var(--accent);
  border-radius: var(--radius-lg);
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  color: var(--accent);
  font-weight: 600;
  pointer-events: none;
}

.dropzone.dragging .drop-overlay {
  display: grid;
}

.input-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 16px;
  min-height: 1.5em;
  color: var(--text-muted);
  font-size: 0.875rem;
}

.error {
  color: var(--error);
}

.input-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

#read-button {
  margin-left: auto;
  min-width: 120px;
}

.about {
  margin: 4px 0 0;
  color: var(--text-muted);
  font-size: 0.8125rem;
  line-height: 1.5;
}

@media (max-width: 480px) {
  .input-actions .button {
    flex: 1 1 auto;
  }

  #read-button {
    flex-basis: 100%;
    margin-left: 0;
  }
}

/* ==========================================================================
   Reader view
   ========================================================================== */

.stage {
  /* Set from app.js for the current layout: --lines per frame, and --size,
     how much smaller than a single word the text is. */
  --word-size: calc(clamp(1.75rem, 7.2vw, 3.25rem) * var(--size, 1));
  /* Half-height of the band between the rules: room for --lines lines. */
  --gap: max(clamp(2.6rem, 9vw, 4.2rem), calc(var(--lines, 1) * var(--word-size) * 0.725 + 0.6rem));
  position: relative;
  min-height: max(clamp(180px, 38dvh, 320px), calc(2 * var(--gap) + 4rem));
  border-radius: var(--radius-lg);
  background: var(--surface);
  border: 1px solid var(--border);
  overflow: hidden;
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* A full focus ring around the stage distracts while reading; tint the border instead. */
.stage:focus-visible {
  outline: none;
  border-color: var(--focus);
}

/* A band between two horizontal rules, with notches marking the focus
   position. The frame (one line or several) sits inside the band. */
.reticle {
  position: absolute;
  inset: calc(50% - var(--gap)) 0;
  border-block: 1px solid var(--reticle);
  pointer-events: none;
}

.reticle::before,
.reticle::after {
  content: "";
  position: absolute;
  left: var(--pivot-x);
  width: 2px;
  height: 12px;
  margin-left: -1px;
  background: var(--reticle);
}

.reticle::before {
  bottom: 100%;
}

.reticle::after {
  top: 100%;
}

/* The frame is anchored at --pivot-x and holds one or more lines; app.js
   shifts each line left by the distance to its focus letter's centre, so
   every focus letter lands exactly on the notch. */
.word {
  position: absolute;
  top: 50%;
  left: var(--pivot-x);
  transform: translateY(-50%);
  font-family: var(--font-word);
  /* --fit < 1 shrinks a frame that would not fit the stage (long groups, URLs). */
  font-size: calc(var(--word-size) * var(--fit, 1));
  font-weight: 500;
  line-height: 1.2;
  white-space: nowrap;
  font-variant-ligatures: none;
}

/* Room between lines so descenders and ascenders of adjacent lines never touch. */
.word.multiline {
  line-height: 1.45;
}

.word .line {
  display: block;
  width: max-content;
}

/* Scroll mode: one continuous line gliding right to left past the anchor.
   app.js sets the transform; the text fades out at the window's edges. */
.track-window {
  position: absolute;
  inset: 0;
  overflow: hidden;
  border-radius: inherit;
  mask-image: linear-gradient(to right, transparent, #000 10%, #000 90%, transparent);
  pointer-events: none;
}

.track {
  position: absolute;
  top: 50%;
  left: 0;
  font-family: var(--font-word);
  font-size: var(--word-size);
  font-weight: 500;
  line-height: 1.2;
  white-space: pre;
  font-variant-ligatures: none;
  will-change: transform;
}

.stage.scrolling .word,
.stage:not(.scrolling) .track-window {
  display: none;
}

.track-word.paragraph-start {
  margin-left: 1.5em;
}

.word .pivot {
  color: var(--pivot);
}

.stage-hint {
  position: absolute;
  inset: auto 0 12px;
  margin: 0;
  text-align: center;
  font-size: 0.8125rem;
  color: var(--text-muted);
  pointer-events: none;
}

.progress {
  height: 3px;
  border-radius: 2px;
  background: var(--surface-2);
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 100%;
  background: var(--accent);
  transform-origin: left;
  transform: scaleX(0);
}

.finished {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
}

.finished-title {
  margin: 0;
  font-weight: 600;
}

.finished-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

@media (max-width: 480px) {
  .finished-actions,
  .finished-actions .button {
    flex: 1 1 auto;
  }
}

.context {
  max-height: 30dvh;
  overflow-y: auto;
  padding: 14px 16px;
  border-radius: var(--radius);
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 1rem;
  line-height: 1.7;
  overscroll-behavior: contain;
}

.context p {
  margin: 0;
}

.context-word {
  border-radius: 3px;
  cursor: pointer;
}

.context-word:hover {
  background: var(--surface-2);
}

.context-word.current {
  background: var(--pivot);
  color: var(--surface);
  padding: 0 2px;
}

.controls {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
}

.transport {
  display: flex;
  align-items: center;
  gap: 12px;
}

.speed {
  display: flex;
  align-items: center;
  gap: 8px;
}

#wpm-slider {
  flex: 1;
  min-width: 0;
  accent-color: var(--accent);
  height: 44px;
}

#wpm-value {
  min-width: 5.5em;
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

.wave {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 16px;
  color: var(--text-muted);
  font-size: 0.875rem;
}

.wave-field {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.wave-field input {
  width: 4.5em;
  min-height: 36px;
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.wave-field input:disabled {
  opacity: 0.5;
}

.layout {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px 20px;
  color: var(--text-muted);
  font-size: 0.875rem;
}

.layout:has(.layout-note:not([hidden])) .stepper,
.layout:has(.layout-note:not([hidden])) .layout-preview {
  opacity: 0.4;
}

.layout-label {
  font-weight: 600;
  color: var(--text);
}

.stepper {
  display: inline-flex;
  align-items: center;
  gap: 2px;
}

.stepper > span {
  margin-right: 4px;
}

.stepper output {
  min-width: 1.5ch;
  text-align: center;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

/* A tiny picture of the layout: one dot per word slot. */
.layout-preview {
  display: inline-grid;
  grid-template-columns: repeat(var(--cols, 1), 6px);
  gap: 3px;
  margin-left: auto;
}

.layout-preview i {
  width: 6px;
  height: 6px;
  border-radius: 1px;
  background: var(--accent);
}

.options {
  display: flex;
  flex-wrap: wrap;
  gap: 0 24px;
  align-items: center;
  color: var(--text-muted);
  font-size: 0.875rem;
}

.switch {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  cursor: pointer;
}

.switch-note {
  font-variant-numeric: tabular-nums;
  opacity: 0.8;
}

.switch input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
}

.advanced {
  color: var(--text-muted);
  font-size: 0.875rem;
}

.advanced summary {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  cursor: pointer;
  user-select: none;
  list-style: none;
}

.advanced summary::-webkit-details-marker {
  display: none;
}

/* inline-flex drops the native marker, so draw a chevron that turns when open. */
.advanced summary::before {
  content: "";
  width: 6px;
  height: 6px;
  margin: 0 10px 0 4px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(-45deg);
  transition: transform 150ms;
}

.advanced[open] summary::before {
  transform: rotate(45deg);
}

.advanced-body {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding-left: 16px;
  border-left: 2px solid var(--border);
}

.advanced-note {
  margin: 4px 0 8px;
  font-size: 0.8125rem;
  opacity: 0.8;
}

.shortcuts {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.8125rem;
  text-align: center;
}

/* Keyboard hints only where there is a real keyboard and pointer. */
@media (hover: none), (pointer: coarse) {
  .shortcuts {
    display: none;
  }
}

/* While playing, recede everything but the word. */
#reader-view .controls,
#reader-view .speed,
#reader-view .wave,
#reader-view .layout,
#reader-view .options,
#reader-view .advanced,
#reader-view .shortcuts {
  transition: opacity 200ms;
}

#reader-view[data-status="playing"] :is(.controls, .speed, .wave, .layout, .options, .advanced, .shortcuts) {
  opacity: 0.3;
}

#reader-view[data-status="playing"] :is(.controls, .speed, .wave, .layout, .options, .advanced):is(:hover, :focus-within) {
  opacity: 1;
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
  }
}
