/* Components — grows as new pages/states are built. Header/nav/button/layout/sidebar/aside/stat-card exist so far. */

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-2) var(--space-3);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  /* Full-width header: never rounded, per docs/DESIGN_SPEC.md §14 */
  border-radius: 0;
}

.app-header__brand {
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--color-text);
  letter-spacing: -0.01em;
  text-decoration: none;
}

.nav {
  display: flex;
  gap: var(--space-3);
  list-style: none;
  margin: 0;
  padding: 0;
}

.nav__link {
  text-decoration: none;
  color: var(--color-text-secondary);
  font-weight: 600;
  font-size: 0.9rem;
}

.nav__link:hover {
  color: var(--color-primary-dark);
}

.nav__link--active {
  color: var(--color-primary);
  border-bottom: 2px solid var(--color-primary);
  padding-bottom: 2px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.95rem;
  border-radius: var(--radius-sm);
  padding: 0.7em 1.4em;
  border: 1px solid transparent;
  cursor: pointer;
  text-decoration: none;
  transition: background 120ms ease, transform 120ms ease;
}

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

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

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

.btn--secondary {
  background: var(--color-surface);
  color: var(--color-text);
  border-color: var(--color-border-strong);
}

.btn--secondary:hover {
  background: var(--color-surface-subtle);
}

.btn--cta {
  padding: 0.85em 2em;
  font-size: 1.05rem;
}

.layout--centered {
  max-width: 680px;
  margin: 0 auto;
  padding: var(--space-5) var(--space-3);
  text-align: center;
}

/* Returning-visitor layout: sidebar / main / aside (docs/DESIGN_EXAMPLES.md Example 2) */
.layout {
  display: grid;
  grid-template-columns: 180px 1fr 220px;
  gap: var(--space-4);
  max-width: 1100px;
  margin: 0 auto;
  padding: var(--space-4) var(--space-3);
}

.layout__main {
  text-align: center;
  padding: var(--space-4) 0;
}

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

.sidebar__link {
  text-decoration: none;
  color: var(--color-text-secondary);
  font-weight: 600;
  font-size: 0.9rem;
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-sm);
}

.sidebar__link:hover {
  background: var(--color-surface-subtle);
  color: var(--color-text);
}

.sidebar__link--active {
  background: var(--color-primary-light);
  color: var(--color-primary-dark);
}

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

.stat-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-2);
}

.stat-card__label {
  display: block;
  font-size: 0.78rem;
  color: var(--color-text-muted);
  margin-bottom: var(--space-1);
}

.stat-card__value {
  display: block;
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--color-text);
}

.stat-card__value--accent {
  color: var(--color-primary);
}

@media (max-width: 900px) {
  .layout {
    grid-template-columns: 1fr;
  }

  .sidebar,
  .aside {
    flex-direction: row;
    flex-wrap: wrap;
  }
}

/* True mobile — docs/DESIGN_EXAMPLES.md Example 9: sidebar/aside step fully aside during
   an active test, the one-line writing area and live feedback stay intact. */
@media (max-width: 640px) {
  .app-header {
    gap: var(--space-2);
  }

  .is-focus-mode .sidebar,
  .is-focus-mode .aside {
    display: none;
  }

  .typing-workspace {
    padding: var(--space-3) var(--space-2);
  }

  .writing-area {
    font-size: 1.05rem;
    padding: var(--space-2);
  }

  .wpm-live {
    font-size: 1.5rem;
  }
}

/* ---------- Focus mode (active test) — docs/DESIGN_EXAMPLES.md Example 3 ---------- */

.is-focus-mode .app-header {
  padding: var(--space-1) var(--space-3);
  box-shadow: var(--shadow-sm);
}

.is-focus-mode .sidebar,
.is-focus-mode .aside {
  opacity: 0.35;
  pointer-events: none;
  filter: grayscale(40%);
}

/* Header swaps between the compact timer (focus mode) and the full nav (normal/result state) */
.app-header__timer {
  display: none;
}

.is-focus-mode .app-header__timer {
  display: inline;
}

.is-focus-mode .app-header__nav {
  display: none;
}

/* The main column swaps between the live typing workspace and the normal "start again" CTA */
.layout__after-result {
  display: none;
  text-align: center;
  padding: var(--space-4) 0;
}

.is-result-mode .typing-workspace {
  display: none;
}

.is-result-mode .layout__after-result {
  display: block;
}

.timer {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  color: var(--color-text-secondary);
}

/* ---------- Typing workspace: graph + WPM + writing area + accuracy, one unit ---------- */

.typing-workspace {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: var(--space-4) var(--space-3);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-2);
}

.graph {
  width: 100%;
  max-width: 640px;
}

.graph svg {
  width: 100%;
  height: auto;
  display: block;
}

.graph__avg-label {
  font-size: 0.72rem;
  fill: var(--color-text-muted);
  font-family: var(--font-family-base);
}

.graph__avg-line {
  stroke: var(--color-border-strong);
  stroke-width: 1;
  stroke-dasharray: 4 4;
}

.graph__segment--ok { stroke: var(--color-success); }
.graph__segment--unstable { stroke: var(--color-warning); }
.graph__segment--error { stroke: var(--color-error); }

.wpm-live {
  font-size: 2rem;
  font-weight: 700;
  color: var(--color-text);
}

.wpm-live__unit {
  font-size: 1rem;
  font-weight: 600;
  color: var(--color-text-muted);
  margin-left: var(--space-1);
}

/* ---------- Writing area — one-line rolling interaction, docs/DESIGN_EXAMPLES.md Example 6 ---------- */

/* Writing area — grid layout guarantees the current-word box sits exactly centred
   (equal 1fr tracks on both sides, regardless of content width). Previous text fades
   away (not an abrupt cut) via a right-to-left text gradient — the oldest characters
   dissolve toward the left edge instead of looking cut off/overwritten. Upcoming text
   fades in the mirrored way toward the right edge, with a small gap so it doesn't
   crowd the current word. */
.writing-area {
  width: 100%;
  max-width: 640px;
  height: 88px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: stretch;
  overflow: hidden;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  font-family: var(--font-family-mono);
  font-size: 1.25rem;
  font-weight: 600;
}

.writing-area__prev {
  /* Grid items default to min-width:auto, which resolves to the nowrap text's full
     (unbounded) content width — that lets this 1fr track grow past its fair share
     and overlap the current word. min-width:0 forces it to actually honour the 1fr
     split, so overflow:hidden then clips at the real column edge. */
  min-width: 0;
  display: flex;
  align-items: center;
  /* Plain LTR, right-aligned: the text hugs the current word on the right, and
     whatever doesn't fit overflows (and gets clipped) on the LEFT — i.e. the
     newest characters stay visible, the oldest disappear first. (An earlier
     direction:rtl attempt showed the OPPOSITE — the string's beginning stayed
     visible and the most recent characters were the ones cut off — so plain
     LTR + flex-end is used instead of relying on bidi behaviour here.) */
  justify-content: flex-end;
  overflow: hidden;
  white-space: nowrap;
  padding: 0 5px 0 var(--space-2);
  background: linear-gradient(to right, var(--color-surface), var(--color-text-secondary));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.writing-area__current {
  align-self: center;
  height: 68px;
  display: flex;
  align-items: center;
  justify-content: center;
  white-space: nowrap;
  background: linear-gradient(90deg, var(--color-success-bg), var(--color-success-bg-strong));
  /* Tight blur radius on purpose — anything wider bleeds over the zero-gap prev/next text. */
  box-shadow: inset 0 0 0 1px var(--color-success-bg-strong), 0 1px 5px var(--color-success-glow);
  border-radius: var(--radius-sm);
  color: var(--color-text);
  font-weight: 700;
  padding: 0 var(--space-2);
  transition: background 150ms ease, box-shadow 150ms ease;
}

.writing-area__current--miss {
  background: linear-gradient(90deg, var(--color-error-bg), var(--color-error-bg-strong));
  box-shadow: inset 0 0 0 1px var(--color-error-bg-strong), 0 1px 5px var(--color-error-glow);
  /* Dark text (not the red itself) keeps contrast guaranteed regardless of how saturated
     --color-error-bg is — the red glow/background + underline already carry the error meaning. */
  color: var(--color-text);
  text-decoration: underline;
  text-decoration-thickness: 2px;
}

.writing-area__next {
  min-width: 0;
  display: flex;
  align-items: center;
  overflow: hidden;
  direction: ltr;
  white-space: nowrap;
  padding: 0 var(--space-2) 0 5px;
  background: linear-gradient(to right, var(--color-text-secondary), var(--color-surface));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.accuracy-live {
  font-size: 0.95rem;
  color: var(--color-text-secondary);
}

.accuracy-live strong {
  color: var(--color-success);
}

/* ---------- Result modal — docs/DESIGN_EXAMPLES.md Example 7 ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(36, 54, 60, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--space-3);
  z-index: 50;
}

.modal-overlay[hidden] {
  display: none;
}

.modal {
  background: var(--color-surface);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  padding: var(--space-4);
  max-width: 420px;
  width: 100%;
  text-align: center;
}

.modal__title {
  color: var(--color-text-secondary);
  font-size: 0.95rem;
  margin-bottom: var(--space-2);
}

.modal__value {
  font-size: 3rem;
  font-weight: 700;
  color: var(--color-text);
  line-height: 1;
}

.modal__unit {
  color: var(--color-text-muted);
  font-size: 1rem;
  margin-bottom: var(--space-3);
}

.modal__stats {
  display: flex;
  justify-content: center;
  gap: var(--space-4);
  margin: var(--space-3) 0;
}

.modal__stat-value {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--color-text);
}

.modal__stat-value--positive {
  color: var(--color-success);
}

.modal__stat-label {
  font-size: 0.78rem;
  color: var(--color-text-muted);
}
