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

/* =============================================================
   LIGHT THEME — typewriter on a dark walnut desk
   ============================================================= */
:root {
  /* Desk — dark walnut surface */
  --bg-desk: #2c2520;
  --bg-desk-grain: rgba(255,240,200,0.03);

  /* Paper — warm ivory with tactile feel */
  --paper: #f6f1e7;
  --paper-edge: #e6ddd0;
  --paper-line: #ede6db;

  /* Ink */
  --ink: #1a1612;
  --ink-secondary: #5a5248;
  --ink-faded: #a09584;

  /* Mechanical accents */
  --accent: #b82e14;
  --accent-deep: #8a2210;
  --correct: #2d7a4f;
  --wrong: #b82e14;
  --wrong-bg: rgba(184, 46, 20, 0.07);

  /* Key caps — ivory keys with brass rims */
  --key-top: #ede7dc;
  --key-rim: #a89c8c;
  --key-side: #8a7e6e;
  --key-letter: #2a2420;
  --key-active-top: #2a2420;
  --key-active-letter: #f6f1e7;
  --key-active-side: #1a1612;

  /* Gauge / timer — brass instrument */
  --gauge-face: #f0ece4;
  --gauge-rim: #8a7e6e;

  /* Components */
  --card-shadow:
    0 2px 4px rgba(0,0,0,0.2),
    0 8px 24px rgba(0,0,0,0.3),
    0 24px 48px rgba(0,0,0,0.15);
  --inset: inset 0 2px 6px rgba(40,30,15,0.12);
  --key-shadow: 0 4px 0 var(--key-side), 0 4px 8px rgba(0,0,0,0.2);
  --border-light: 1px solid var(--paper-edge);

  /* Overlay */
  --overlay-bg: rgba(20, 14, 8, 0.8);

  /* Header on desk */
  --header-color: #e8dcc8;

  /* Texture noise intensity */
  --noise-opacity: 0.35;
  --paper-noise-opacity: 0.12;

  /* Glass highlight */
  --glass-highlight: rgba(255,255,255,0.08);
  --glass-highlight-strong: rgba(255,255,255,0.15);
}

/* =============================================================
   DARK THEME — midnight workshop, leather & slate
   ============================================================= */
[data-theme="dark"] {
  --bg-desk: #111010;
  --bg-desk-grain: rgba(255,255,255,0.015);

  --paper: #1e1c1a;
  --paper-edge: #2e2a26;
  --paper-line: #242120;

  --ink: #e0d8cc;
  --ink-secondary: #a89c8c;
  --ink-faded: #5a5248;

  --accent: #d44020;
  --accent-deep: #a83018;
  --correct: #5cc88a;
  --wrong: #e85040;
  --wrong-bg: rgba(232, 80, 64, 0.1);

  --key-top: #2a2724;
  --key-rim: #1e1c1a;
  --key-side: #0a0908;
  --key-letter: #b8ad9c;
  --key-active-top: #d44020;
  --key-active-letter: #fff;
  --key-active-side: #8a2210;

  --gauge-face: #222120;
  --gauge-rim: #3a3532;

  --card-shadow:
    0 2px 4px rgba(0,0,0,0.4),
    0 8px 24px rgba(0,0,0,0.5),
    0 24px 48px rgba(0,0,0,0.3);
  --inset: inset 0 2px 6px rgba(0,0,0,0.35);
  --key-shadow: 0 4px 0 var(--key-side), 0 4px 8px rgba(0,0,0,0.4);
  --border-light: 1px solid #2e2a26;

  --overlay-bg: rgba(0, 0, 0, 0.85);

  --header-color: #c8bfb0;

  --noise-opacity: 0.2;
  --paper-noise-opacity: 0.06;

  --glass-highlight: rgba(255,255,255,0.03);
  --glass-highlight-strong: rgba(255,255,255,0.06);
}

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
}

body {
  font-family: 'Space Grotesk', -apple-system, sans-serif;
  color: var(--ink);
  min-height: 100vh;
  overflow-x: hidden;
  transition: color 0.3s ease;
  background-color: var(--bg-desk);
  /* Layered wood grain — organic variation from overlapping grain lines at
     slightly different angles, widths, and opacities */
  background-image:
    /* Warm overhead lamp spotlight */
    radial-gradient(ellipse 80% 50% at 50% 25%,
      rgba(180,140,80,0.10) 0%, transparent 100%),
    /* Vignette — darker edges */
    radial-gradient(ellipse at 50% 50%,
      transparent 40%, rgba(0,0,0,0.35) 100%),
    /* Primary grain — tight horizontal lines */
    repeating-linear-gradient(
      2deg,
      transparent 0px, transparent 2px,
      rgba(160,120,60,0.04) 2px, rgba(160,120,60,0.04) 3px,
      transparent 3px, transparent 7px,
      rgba(120,90,40,0.03) 7px, rgba(120,90,40,0.03) 8px,
      transparent 8px, transparent 14px
    ),
    /* Secondary grain — wider, offset angle for organic feel */
    repeating-linear-gradient(
      178deg,
      transparent 0px, transparent 11px,
      rgba(140,100,50,0.035) 11px, rgba(140,100,50,0.035) 13px,
      transparent 13px, transparent 28px,
      rgba(100,70,30,0.025) 28px, rgba(100,70,30,0.025) 29px,
      transparent 29px, transparent 40px
    ),
    /* Broad tonal variation — simulates plank color shifts */
    repeating-linear-gradient(
      1deg,
      transparent 0px,
      rgba(180,140,80,0.03) 80px,
      transparent 160px,
      rgba(80,50,20,0.03) 240px,
      transparent 320px
    );
}

/* Fine noise overlay — adds tactile grain to the whole surface */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  opacity: var(--noise-opacity);
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 512 512' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.75' numOctaves='5' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)'/%3E%3C/svg%3E");
  background-size: 512px 512px;
  pointer-events: none;
  z-index: 0;
  mix-blend-mode: overlay;
}

/* Secondary noise layer — coarser, adds knot-like tonal variation */
body::after {
  content: '';
  position: fixed;
  inset: 0;
  opacity: calc(var(--noise-opacity) * 0.5);
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='grain'%3E%3CfeTurbulence type='turbulence' baseFrequency='0.15' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23grain)'/%3E%3C/svg%3E");
  background-size: 256px 256px;
  pointer-events: none;
  z-index: 0;
  mix-blend-mode: soft-light;
}

/* ===== Layout — desk surface ===== */
.app-background {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 2.5rem 1.5rem 4rem;
  position: relative;
  z-index: 1;
}

/* ===== Header ===== */
.header {
  text-align: center;
  margin-bottom: 1.5rem;
}

.header h1 {
  font-family: 'Space Grotesk', sans-serif;
  font-weight: 700;
  font-size: 2.25rem;
  color: var(--header-color);
  letter-spacing: -0.02em;
  text-transform: uppercase;
  line-height: 1.1;
  text-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

/* ===== Theme Toggle — brass switch ===== */
.theme-toggle {
  position: fixed;
  top: 1rem;
  right: 1rem;
  width: 2.75rem;
  height: 2.75rem;
  border: 2px solid var(--gauge-rim);
  border-radius: 50%;
  background: var(--key-top);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
  color: var(--key-letter);
  box-shadow: 0 3px 0 var(--key-side), 0 3px 8px rgba(0,0,0,0.15),
              inset 0 1px 0 var(--glass-highlight-strong);
  transition: all 0.1s ease;
  z-index: 100;
}

.theme-toggle:hover {
  box-shadow: 0 2px 0 var(--key-side), 0 2px 5px rgba(0,0,0,0.15),
              inset 0 1px 0 var(--glass-highlight-strong);
  transform: translateY(1px);
}

.theme-toggle:active {
  box-shadow: inset 0 1px 3px rgba(0,0,0,0.2);
  transform: translateY(3px);
}

/* ===== Duration Selector — typewriter key row ===== */
.duration-selector {
  display: flex;
  gap: 0.4rem;
  justify-content: center;
  margin-bottom: 1.5rem;
}

.duration-btn {
  font-family: 'JetBrains Mono', monospace;
  font-weight: 600;
  font-size: 0.8rem;
  padding: 0.55rem 1.15rem;
  border: 2px solid var(--key-rim);
  border-radius: 3px;
  background: var(--key-top);
  color: var(--key-letter);
  cursor: pointer;
  transition: all 0.08s ease;
  box-shadow: var(--key-shadow),
              inset 0 1px 0 var(--glass-highlight-strong);
  position: relative;
  top: 0;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.duration-btn:hover {
  top: 2px;
  box-shadow: 0 2px 0 var(--key-side), 0 2px 4px rgba(0,0,0,0.1),
              inset 0 1px 0 var(--glass-highlight-strong);
}

.duration-btn:active {
  top: 4px;
  box-shadow: inset 0 1px 3px rgba(0,0,0,0.15);
}

.duration-btn.active {
  background: var(--key-active-top);
  color: var(--key-active-letter);
  border-color: var(--key-active-top);
  box-shadow: 0 4px 0 var(--key-active-side), 0 4px 8px rgba(0,0,0,0.2),
              inset 0 1px 0 rgba(255,255,255,0.05);
}

.duration-btn.active:hover {
  top: 2px;
  box-shadow: 0 2px 0 var(--key-active-side), 0 2px 4px rgba(0,0,0,0.2);
}

/* ===== Test Card — physical paper sheet ===== */
.test-card {
  background: var(--paper);
  border: 1px solid var(--paper-edge);
  border-radius: 0;
  box-shadow: var(--card-shadow);
  width: 100%;
  max-width: 56rem;
  position: relative;
  overflow: visible;
  transition: background-color 0.3s ease;
}

/* Paper texture overlay */
.test-card::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 0;
  opacity: var(--paper-noise-opacity);
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='grain'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='1.2' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23grain)'/%3E%3C/svg%3E");
  background-size: 200px 200px;
  pointer-events: none;
  z-index: 2;
  mix-blend-mode: multiply;
}

/* Paper top-edge highlight — light fold effect */
.test-card::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(
    to bottom,
    var(--glass-highlight-strong),
    transparent
  );
  border-radius: 0;
  pointer-events: none;
  z-index: 3;
}

/* ===== Metrics Bar — instrument panel ===== */
.metrics-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.25rem 1.75rem;
  border-bottom: var(--border-light);
  background: linear-gradient(to bottom, var(--paper), var(--paper-line));
  transition: background 0.3s ease;
  position: relative;
}

.metrics-group {
  display: flex;
  gap: 0.75rem;
}

/* Metric boxes — embossed counter readouts with glass effect */
.metric-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 5.5rem;
  height: 5rem;
  border: 1px solid var(--gauge-rim);
  border-radius: 3px;
  background:
    linear-gradient(
      170deg,
      var(--glass-highlight) 0%,
      transparent 40%
    ),
    var(--gauge-face);
  box-shadow:
    var(--inset),
    0 1px 0 var(--glass-highlight);
  transition: background-color 0.3s ease;
  position: relative;
}

/* Engraved top edge */
.metric-box::before {
  content: '';
  position: absolute;
  top: 0;
  left: 4px;
  right: 4px;
  height: 1px;
  background: var(--glass-highlight-strong);
  border-radius: 3px 3px 0 0;
}

/* Subtle bottom shadow for depth */
.metric-box::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 4px;
  right: 4px;
  height: 1px;
  background: rgba(0,0,0,0.06);
  border-radius: 0 0 3px 3px;
}

.metric-box .value {
  font-family: 'JetBrains Mono', monospace;
  font-weight: 700;
  font-size: 1.5rem;
  color: var(--ink);
  line-height: 1;
}

.metric-box .label {
  font-family: 'IBM Plex Mono', monospace;
  font-weight: 500;
  font-size: 0.75rem;
  color: var(--ink-secondary);
  margin-top: 0.3rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* ===== Timer — analog stopwatch with depth ===== */
.timer-container {
  display: flex;
  align-items: center;
  justify-content: center;
}

.timer-circle {
  width: 7rem;
  height: 7rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  position: relative;
  border: 3px solid var(--gauge-rim);
  border-radius: 50%;
  background:
    radial-gradient(
      circle at 40% 35%,
      var(--glass-highlight) 0%,
      transparent 60%
    ),
    var(--gauge-face);
  box-shadow:
    var(--inset),
    0 2px 0 var(--glass-highlight),
    0 4px 12px rgba(0,0,0,0.12),
    inset 0 0 20px rgba(0,0,0,0.04);
  transition: background-color 0.3s ease;
}

/* Crown pin at top */
.timer-circle::before {
  content: '';
  position: absolute;
  top: -10px;
  left: 50%;
  transform: translateX(-50%);
  width: 10px;
  height: 8px;
  background: linear-gradient(to bottom, var(--gauge-rim), var(--key-side));
  border-radius: 3px 3px 1px 1px;
  box-shadow: 0 -1px 2px rgba(0,0,0,0.15);
}

/* Inner ring — bezel detail */
.timer-circle::after {
  content: '';
  position: absolute;
  inset: 4px;
  border: 1px solid var(--gauge-rim);
  border-radius: 50%;
  opacity: 0.3;
  pointer-events: none;
}

.timer-circle svg {
  position: absolute;
  top: -3px;
  left: -3px;
}

.timer-circle .timer-value {
  font-family: 'JetBrains Mono', monospace;
  font-weight: 700;
  font-size: 1.7rem;
  color: var(--ink);
  line-height: 1;
  position: relative;
  z-index: 1;
}

.timer-circle .timer-label {
  font-family: 'IBM Plex Mono', monospace;
  font-weight: 500;
  font-size: 0.65rem;
  color: var(--ink-secondary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-top: 0.15rem;
  position: relative;
  z-index: 1;
}

.timer-progress {
  transition: stroke-dashoffset 1s linear;
}

/* ===== Typing Area — ruled notebook paper ===== */
.typing-area {
  padding: 2.75rem 2.25rem 2.25rem 4.5rem;
  position: relative;
  cursor: text;
  background: var(--paper);
  /* Ruled lines matching 3rem line-height — visible whether text is there or not */
  background-image: repeating-linear-gradient(
    to bottom,
    transparent 0px,
    transparent calc(3rem - 1px),
    rgba(120, 140, 170, 0.18) calc(3rem - 1px),
    rgba(120, 140, 170, 0.18) 3rem
  );
  background-size: 100% 3rem;
  background-position: 0 calc(2.75rem - 0.1rem);
  transition: background-color 0.3s ease;
}

[data-theme="dark"] .typing-area {
  background-image: repeating-linear-gradient(
    to bottom,
    transparent 0px,
    transparent calc(3rem - 1px),
    rgba(140, 160, 190, 0.07) calc(3rem - 1px),
    rgba(140, 160, 190, 0.07) 3rem
  );
}

/* Double red margin lines */
.typing-area::before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: 3.6rem;
  width: 3px;
  border-left: 1px solid rgba(184, 46, 20, 0.2);
  border-right: 1px solid rgba(184, 46, 20, 0.13);
  pointer-events: none;
  z-index: 1;
}

/* ===== Start indicator ===== */
.start-indicator {
  position: absolute;
  left: 50%;
  top: 0.5rem;
  transform: translateX(-50%);
  background: var(--key-active-top);
  color: var(--key-active-letter);
  font-family: 'JetBrains Mono', monospace;
  font-weight: 600;
  font-size: 0.75rem;
  padding: 0.4rem 1rem 0.35rem;
  border-radius: 1px;
  z-index: 10;
  pointer-events: none;
  white-space: nowrap;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  opacity: 1;
  transition: opacity 0.4s ease, transform 0.4s ease;
}

.start-indicator::after {
  content: "";
  border-style: solid;
  border-width: 5px 4px 0 4px;
  border-color: var(--key-active-top) transparent transparent transparent;
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
}

.start-indicator.hidden {
  opacity: 0;
  transform: translateX(-50%) translateY(-8px);
  pointer-events: none;
}

/* ===== Words Container — horizontal single-line scroll ===== */
.words-container {
  position: relative;
  overflow: hidden;
  height: 3rem;
  font-family: 'IBM Plex Mono', monospace;
  font-weight: 400;
  font-size: 1.7rem;
  line-height: 3rem;
  text-rendering: optimizeLegibility;
  padding-left: 1.5rem;
  /* Fade edges so words don't clip harshly */
  mask-image: linear-gradient(
    to right,
    transparent 0%,
    black 3%,
    black 92%,
    transparent 100%
  );
  -webkit-mask-image: linear-gradient(
    to right,
    transparent 0%,
    black 3%,
    black 92%,
    transparent 100%
  );
}

.words-inner {
  display: flex;
  flex-wrap: nowrap;
  gap: 0 0.6rem;
  will-change: transform;
  white-space: nowrap;
  transition: transform 0.5s ease-out;
}

/* ===== Word & Character styling ===== */
.word {
  display: inline-block;
  position: relative;
  padding: 0 1px;
  border-radius: 2px;
  flex-shrink: 0;
}

/* Untyped — full ink */
.word .char {
  color: var(--ink);
}

/* Typed correctly — ink fades like dried text */
.word.typed .char {
  color: var(--ink-faded);
}

/* Typed wrong — red strike */
.word.typed.wrong .char {
  color: var(--wrong);
  opacity: 0.55;
}

.word.typed.wrong {
  text-decoration: line-through;
  text-decoration-color: var(--wrong);
  text-decoration-thickness: 2px;
}

/* Current word — subtle warm highlight */
.word.current {
  background: rgba(160, 140, 100, 0.1);
  border-radius: 3px;
  padding: 0 3px;
  margin: 0 -2px;
}

[data-theme="dark"] .word.current {
  background: rgba(200, 180, 140, 0.08);
}

/* Current word characters — NO transitions, instant feedback */
.word.current .char.correct {
  color: var(--correct);
  font-weight: 500;
}

.word.current .char.wrong {
  color: var(--wrong);
  background: var(--wrong-bg);
  border-radius: 2px;
  font-weight: 500;
  text-decoration: line-through;
  text-decoration-thickness: 2px;
  opacity: 0.7;
}

.word.current .char.pending {
  color: var(--ink);
}

/* ===== Hidden Input ===== */
.hidden-input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
  width: 0;
  height: 0;
}

.hidden-input:focus {
  outline: none !important;
  box-shadow: none !important;
}

/* ===== Results Overlay ===== */
.results-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: var(--overlay-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  opacity: 0;
  visibility: hidden;
  transition: all 0.25s ease;
  backdrop-filter: blur(2px);
}

.results-overlay.visible {
  opacity: 1;
  visibility: visible;
}

/* Results card — physical report / receipt feel */
.results-card {
  background: var(--paper);
  border: 1px solid var(--paper-edge);
  box-shadow: var(--card-shadow);
  max-width: 28rem;
  width: 90%;
  overflow: hidden;
  border-radius: 0;
  transform: scale(0.95) translateY(20px);
  transition: transform 0.3s cubic-bezier(0.16, 1, 0.3, 1);
  position: relative;
}

/* Paper texture on results card */
.results-card::before {
  content: '';
  position: absolute;
  inset: 0;
  opacity: var(--paper-noise-opacity);
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='grain'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='1.2' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23grain)'/%3E%3C/svg%3E");
  background-size: 200px 200px;
  pointer-events: none;
  z-index: 1;
  mix-blend-mode: multiply;
}

.results-overlay.visible .results-card {
  transform: scale(1) translateY(0);
}

.results-header {
  padding: 2rem 2rem 1.5rem;
  text-align: center;
  position: relative;
  background-color: var(--key-active-top);
  background-image:
    linear-gradient(170deg, rgba(255,255,255,0.05) 0%, transparent 50%),
    repeating-linear-gradient(
      0deg,
      transparent 0px,
      transparent 2px,
      rgba(0,0,0,0.03) 2px,
      rgba(0,0,0,0.03) 3px
    );
  border-bottom: 2px solid rgba(0,0,0,0.15);
}

/* Embossed border between header and body */
.results-header::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: rgba(255,255,255,0.06);
}

.results-avatar {
  width: 3.5rem;
  height: 3.5rem;
  margin: 0 auto 0.75rem;
  color: var(--key-active-letter);
}

.results-title {
  font-family: 'Space Grotesk', sans-serif;
  font-weight: 700;
  font-size: 1.5rem;
  color: var(--key-active-letter);
  margin-bottom: 0.4rem;
  text-transform: uppercase;
  text-shadow: 0 1px 2px rgba(0,0,0,0.2);
}

.results-subtitle {
  font-family: 'IBM Plex Mono', monospace;
  font-weight: 400;
  font-size: 0.9rem;
  color: rgba(255,255,255,0.7);
  line-height: 1.7;
}

.results-subtitle .highlight {
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  padding: 0.1rem 0.45rem;
  border-radius: 2px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.15);
}

.results-subtitle .bold {
  font-weight: 600;
  color: #fff;
}

/* Results metrics — inset counter readouts like the main gauges */
.results-metrics {
  display: flex;
  gap: 0;
  border-bottom: var(--border-light);
  position: relative;
  z-index: 2;
}

.result-metric {
  text-align: center;
  flex: 1;
  padding: 1.25rem 0.75rem;
  border-right: var(--border-light);
  background:
    linear-gradient(to bottom, var(--glass-highlight), transparent 50%),
    var(--gauge-face);
  box-shadow: var(--inset);
}

.result-metric:last-child {
  border-right: none;
}

.result-metric .value {
  font-family: 'JetBrains Mono', monospace;
  font-weight: 700;
  font-size: 2rem;
  color: var(--ink);
  line-height: 1;
}

.result-metric .label {
  font-family: 'IBM Plex Mono', monospace;
  font-weight: 500;
  font-size: 0.8rem;
  color: var(--ink-secondary);
  margin-top: 0.3rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* Result try-again — big mechanical key */
.results-actions {
  padding: 1.25rem 1.5rem;
  position: relative;
  z-index: 2;
}

.btn-try-again {
  font-family: 'Space Grotesk', sans-serif;
  font-weight: 700;
  font-size: 0.9rem;
  padding: 0.8rem 2rem;
  border: 2px solid var(--accent);
  border-radius: 3px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  transition: all 0.08s ease;
  width: 100%;
  box-shadow: 0 4px 0 var(--accent-deep),
              inset 0 1px 0 rgba(255,255,255,0.15);
  position: relative;
  top: 0;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.btn-try-again:hover {
  top: 2px;
  box-shadow: 0 2px 0 var(--accent-deep);
}

.btn-try-again:active {
  top: 4px;
  box-shadow: inset 0 1px 3px rgba(0,0,0,0.2);
}

/* ===== Reset Button — key on desk ===== */
.reset-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  margin: 1.5rem auto 0;
  font-family: 'JetBrains Mono', monospace;
  font-weight: 600;
  font-size: 0.8rem;
  padding: 0.55rem 1.4rem;
  border: 2px solid var(--key-rim);
  border-radius: 3px;
  background: var(--key-top);
  color: var(--key-letter);
  cursor: pointer;
  transition: all 0.08s ease;
  box-shadow: var(--key-shadow),
              inset 0 1px 0 var(--glass-highlight-strong);
  position: relative;
  top: 0;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.reset-btn:hover {
  top: 2px;
  box-shadow: 0 2px 0 var(--key-side), 0 2px 4px rgba(0,0,0,0.1);
}

.reset-btn:active {
  top: 4px;
  box-shadow: inset 0 1px 3px rgba(0,0,0,0.15);
}

.reset-btn svg {
  width: 16px;
  height: 16px;
  transition: transform 0.3s ease;
}

.reset-btn:hover svg {
  transform: rotate(-180deg);
}

/* ===== Save Result Button ===== */
.btn-save-result {
  font-family: 'JetBrains Mono', monospace;
  font-weight: 600;
  font-size: 0.8rem;
  padding: 0.65rem 2rem;
  border: 2px solid var(--key-rim);
  border-radius: 3px;
  background: var(--key-top);
  color: var(--key-letter);
  cursor: pointer;
  transition: all 0.08s ease;
  width: 100%;
  box-shadow: 0 4px 0 var(--key-side),
              inset 0 1px 0 var(--glass-highlight-strong);
  position: relative;
  top: 0;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 0.5rem;
}

.btn-save-result:hover {
  top: 2px;
  box-shadow: 0 2px 0 var(--key-side);
}

.btn-save-result:active {
  top: 4px;
  box-shadow: inset 0 1px 3px rgba(0,0,0,0.15);
}

.btn-save-result.saved {
  background: var(--correct);
  color: #fff;
  border-color: var(--correct);
  box-shadow: none;
  top: 0;
  cursor: default;
  opacity: 0.85;
}

/* ===== Leaderboard Sidebar — Clipboard ===== */
.leaderboard-sidebar {
  position: fixed;
  top: 50%;
  right: 0;
  transform: translateY(-50%);
  z-index: 50;
  display: flex;
  align-items: flex-start;
}

/* Tab button — always visible on edge */
.leaderboard-tab {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-family: 'JetBrains Mono', monospace;
  font-weight: 600;
  font-size: 0.7rem;
  padding: 0.6rem 0.5rem;
  border: 2px solid var(--key-rim);
  border-right: none;
  border-radius: 3px 0 0 3px;
  background: var(--key-top);
  color: var(--key-letter);
  cursor: pointer;
  box-shadow: -2px 2px 8px rgba(0,0,0,0.15),
              inset 0 1px 0 var(--glass-highlight-strong);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  writing-mode: vertical-lr;
  text-orientation: mixed;
  transition: background 0.15s ease;
}

.leaderboard-tab svg { transform: rotate(90deg); }

.leaderboard-tab:hover { background: var(--gauge-face); }

.leaderboard-sidebar.open .leaderboard-tab {
  background: var(--key-active-top);
  color: var(--key-active-letter);
  border-color: var(--key-active-top);
}

/* Panel — clipboard body */
.leaderboard-panel {
  width: 0;
  max-height: 75vh;
  overflow: hidden;
  transition: width 0.25s ease;
  position: relative;
  /* Clipboard backing — dark cardboard */
  background: #5c4a3a;
  border-radius: 3px 0 0 3px;
  box-shadow:
    -2px 4px 12px rgba(0,0,0,0.25),
    inset 0 0 30px rgba(0,0,0,0.15);
  border: none;
}

[data-theme="dark"] .leaderboard-panel {
  background: #2a2420;
  box-shadow:
    -2px 4px 12px rgba(0,0,0,0.5),
    inset 0 0 30px rgba(0,0,0,0.3);
}

.leaderboard-sidebar.open .leaderboard-panel {
  width: 22rem;
  overflow-y: auto;
}

/* Metal clip at top */
.leaderboard-panel::before {
  content: '';
  position: absolute;
  top: 0.6rem;
  left: 50%;
  transform: translateX(-50%);
  width: 4rem;
  height: 1.4rem;
  background:
    linear-gradient(to bottom,
      #c8c0b4 0%,
      #a8a090 30%,
      #b8b0a4 50%,
      #989088 100%
    );
  border-radius: 3px 3px 6px 6px;
  box-shadow:
    0 2px 4px rgba(0,0,0,0.2),
    inset 0 1px 0 rgba(255,255,255,0.3),
    inset 0 -1px 2px rgba(0,0,0,0.1);
  z-index: 10;
}

[data-theme="dark"] .leaderboard-panel::before {
  background:
    linear-gradient(to bottom,
      #6a6258 0%,
      #4a4238 30%,
      #5a524a 50%,
      #3a3430 100%
    );
  box-shadow:
    0 2px 4px rgba(0,0,0,0.4),
    inset 0 1px 0 rgba(255,255,255,0.08),
    inset 0 -1px 2px rgba(0,0,0,0.2);
}

/* Clip lever — small raised bar */
.leaderboard-panel::after {
  content: '';
  position: absolute;
  top: 0.3rem;
  left: 50%;
  transform: translateX(-50%);
  width: 2.4rem;
  height: 0.5rem;
  background:
    linear-gradient(to bottom, #d0c8bc, #b0a898);
  border-radius: 2px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.15);
  z-index: 11;
}

[data-theme="dark"] .leaderboard-panel::after {
  background: linear-gradient(to bottom, #5a524a, #3a3430);
}

/* Paper sheet inside clipboard */
.leaderboard-inner {
  margin: 2.4rem 0.5rem 0.5rem;
  background: var(--paper);
  border-radius: 2px;
  position: relative;
  overflow: hidden;
  box-shadow:
    0 1px 3px rgba(0,0,0,0.1),
    inset 0 0 0 1px rgba(0,0,0,0.04);
}

/* Paper grain */
.leaderboard-inner::before {
  content: '';
  position: absolute;
  inset: 0;
  opacity: var(--paper-noise-opacity);
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='grain'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='1.2' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23grain)'/%3E%3C/svg%3E");
  background-size: 200px 200px;
  pointer-events: none;
  z-index: 1;
  mix-blend-mode: multiply;
}

.leaderboard-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 0.85rem;
  border-bottom: var(--border-light);
  position: relative;
  z-index: 2;
  white-space: nowrap;
}

.leaderboard-title {
  font-family: 'Space Grotesk', sans-serif;
  font-weight: 700;
  font-size: 0.85rem;
  color: var(--ink);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.leaderboard-clear {
  font-family: 'IBM Plex Mono', monospace;
  font-weight: 500;
  font-size: 0.6rem;
  padding: 0.2rem 0.5rem;
  border: 1px solid var(--gauge-rim);
  border-radius: 3px;
  background: var(--gauge-face);
  color: var(--ink-secondary);
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  transition: all 0.08s ease;
}

.leaderboard-clear:hover {
  color: var(--wrong);
  border-color: var(--wrong);
}

.leaderboard-empty {
  padding: 1.5rem 1rem;
  text-align: center;
  font-family: 'IBM Plex Mono', monospace;
  font-weight: 400;
  font-size: 0.8rem;
  color: var(--ink-faded);
  position: relative;
  z-index: 2;
  white-space: nowrap;
}

.leaderboard-table {
  width: 100%;
  border-collapse: collapse;
  position: relative;
  z-index: 2;
}

.leaderboard-table thead th {
  font-family: 'IBM Plex Mono', monospace;
  font-weight: 600;
  font-size: 0.6rem;
  color: var(--ink-secondary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 0.45rem 0.5rem;
  text-align: left;
  border-bottom: var(--border-light);
  background: linear-gradient(to bottom, var(--paper), var(--paper-line));
  white-space: nowrap;
}

.leaderboard-table tbody td {
  font-family: 'JetBrains Mono', monospace;
  font-weight: 500;
  font-size: 0.75rem;
  color: var(--ink);
  padding: 0.45rem 0.5rem;
  border-bottom: var(--border-light);
  white-space: nowrap;
}

.leaderboard-table tbody tr:last-child td {
  border-bottom: none;
}

.leaderboard-table tbody tr:hover {
  background: var(--glass-highlight);
}

.leaderboard-table tbody tr.best-row td:first-child {
  color: var(--accent);
  font-weight: 700;
}

.leaderboard-table .col-rank { width: 2rem; text-align: center; }
.leaderboard-table .col-wpm { font-weight: 700; }
.leaderboard-table .col-date {
  font-size: 0.65rem;
  color: var(--ink-secondary);
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
  .app-background { padding: 1.5rem 0.75rem 3rem; }
  .header h1 { font-size: 1.6rem; }

  .metrics-bar {
    flex-direction: column;
    gap: 1rem;
    padding: 1rem;
  }
  .metrics-group { order: 2; }
  .timer-container { order: 1; }

  .metric-box { width: 4.5rem; height: 4rem; }
  .metric-box .value { font-size: 1.2rem; }
  .metric-box .label { font-size: 0.65rem; }

  .timer-circle { width: 5.5rem; height: 5.5rem; }
  .timer-circle .timer-value { font-size: 1.35rem; }

  .typing-area { padding: 2rem 1rem 1.5rem 3.5rem; }
  .typing-area::before { left: 2.6rem; }
  .words-container {
    font-size: 1.2rem;
    line-height: 2.2rem;
    height: 2.2rem;
    padding-left: 1rem;
  }
  .duration-selector { flex-wrap: wrap; }

  .results-metrics { flex-direction: column; }
  .result-metric {
    border-right: none;
    border-bottom: var(--border-light);
    padding: 1rem;
  }
  .result-metric:last-child { border-bottom: none; }
  .result-metric .value { font-size: 1.5rem; }

  .theme-toggle { width: 2.25rem; height: 2.25rem; font-size: 1rem; }

  .leaderboard-sidebar.open .leaderboard-panel { width: 17rem; }
  .leaderboard-inner { margin: 2.2rem 0.35rem 0.35rem; }
  .leaderboard-table thead th,
  .leaderboard-table tbody td {
    padding: 0.35rem 0.35rem;
    font-size: 0.65rem;
  }
}

/* Accessibility: ensure focus-visible ring on interactive elements */
.duration-btn:focus-visible,
.reset-btn:focus-visible,
.btn-try-again:focus-visible,
.btn-save-result:focus-visible,
.theme-toggle:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

/* Reduced motion preference */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
