:root {
  --bg: #0f1115;
  --fg: #f3f4f6;
  --muted: #9ca3af;
  --accent: #60a5fa;
  --correct: #60a5fa;
  --wrong: #ef4444;
  --card-bg: rgba(26, 29, 35, 0.82);
}

[data-theme="light"] {
  --bg: #f3f4f6;
  --fg: #111827;
  --muted: #6b7280;
  --accent: #3b82f6;
  --correct: #3b82f6;
  --wrong: #dc2626;
  --card-bg: rgba(255, 255, 255, 0.92);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 1rem;
}

#bg-canvas {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  pointer-events: none;
}

#toolbar {
  position: fixed;
  top: 0.75rem;
  right: 0.75rem;
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

#toolbar button {
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(26, 29, 35, 0.7);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 0.5rem;
  padding: 0.45rem;
  color: var(--muted);
  cursor: pointer;
  min-width: unset;
  line-height: 0;
  transition: background 0.2s ease, border-color 0.2s ease, color 0.2s ease;
}

#toolbar button:hover {
  background: rgba(26, 29, 35, 0.9);
  border-color: rgba(255, 255, 255, 0.25);
  color: var(--fg);
}

#toolbar button.off {
  opacity: 0.5;
}

.card {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 50rem;
}

h1 {
  font-size: 2.75rem;
  margin: 0 0 1rem;
  text-align: center;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  position: relative;
  top: -3rem;
}

.stats {
  display: flex;
  gap: 1rem;
  justify-content: center;
  margin-bottom: 1.5rem;
}

.stat-item {
  text-align: center;
  flex: 1;
  max-width: 8rem;
}

.stat-label {
  color: var(--muted);
  font-size: 0.875rem;
  margin-bottom: 0.25rem;
}

.stat-value {
  font-size: 1.5rem;
  font-weight: 600;
  color: var(--accent);
  white-space: nowrap;
}

.type-helper {
  position: absolute;
  top: 0; left: 0;
  width: 1px; height: 1px;
  opacity: 0;
  border: none;
  outline: none;
  resize: none;
  pointer-events: none;
  font-size: 16px;   /* prevents iOS zoom on focus */
}

.text-display {
  position: relative;
  background: var(--card-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: 0.5rem;
  padding: 1.5rem;
  margin-bottom: 1rem;
  min-height: 8rem;
  line-height: 1.8;
  font-size: 1.125rem;
  text-align: justify;
  white-space: pre-wrap;
  word-break: break-word;
  cursor: text;
  outline: none;
  caret-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  box-shadow: inset 0 0 22px 8px var(--bg);
}

.text-display:focus {
  outline: none;
  caret-color: transparent;
  box-shadow: inset 0 0 22px 8px var(--bg);
}

.char { transition: background-color 0.1s ease; }
.char.correct { color: var(--correct); }
.char.wrong { color: var(--wrong); background: rgba(239, 68, 68, 0.15); }
.char.pending { color: var(--muted); }

.char.cursor {
  border-bottom: 2px solid var(--accent);
  animation: blink 1s infinite;
}

@keyframes blink {
  0%, 50% { border-bottom-color: var(--accent); }
  51%, 100% { border-bottom-color: transparent; }
}

.controls {
  display: flex;
  gap: 1rem;
  justify-content: center;
  flex-wrap: wrap;
}

/* ========================================================================
   Button System
   Usage: <button class="btn btn-{variant}">Label</button>
   Variants: btn-primary (blue), btn-secondary (gray), btn-review (purple),
             btn-danger (red)

   Frosted-glass style: semi-transparent backgrounds with backdrop-blur so
   the animated canvas bleeds through the edges, creating a soft fade.
   ======================================================================== */

button, .btn {
  position: relative;
  border-radius: 0.5rem;
  padding: 0.75rem 1.5rem;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  min-width: 7rem;
  text-align: center;
  font-family: inherit;
  /* Ghost default — text follows theme */
  background: transparent;
  border: 1px solid rgba(96, 165, 250, 0.35);
  color: var(--fg);
  transition: background 0.2s ease, border-color 0.2s ease, color 0.2s ease,
              transform 0.1s ease;
}

button:hover, .btn:hover {
  color: #ffffff;
  border-color: rgba(96, 165, 250, 1);
  background: rgb(96, 165, 250);   /* fully opaque */
}
button:active, .btn:active { transform: scale(0.98); }
button:disabled, .btn:disabled { opacity: 0.35; cursor: not-allowed; }

/* Color variants — border color distinguishes them; text = var(--fg) until hover */
.btn-primary           { border-color: rgba(96, 165, 250, 0.35); }
.btn-primary:hover     { border-color: #60a5fa; background: rgb(96, 165, 250); }

.btn-secondary         { border-color: rgba(156, 163, 175, 0.40); }
.btn-secondary:hover   { border-color: #9ca3af; background: rgb(156, 163, 175); }

.btn-review            { border-color: rgba(168, 85, 247, 0.70); }
.btn-review:hover      { border-color: #a855f7; background: rgb(168, 85, 247); }

.btn-danger            { border-color: rgba(239, 68, 68, 0.35); }
.btn-danger:hover      { border-color: #ef4444; background: rgb(239, 68, 68); }

/* Backward compat */
button.secondary       { border-color: rgba(156, 163, 175, 0.35); background: transparent; color: var(--fg); }
button.review-btn      { border-color: rgba(168, 85, 247, 0.70); background: transparent; color: var(--fg); }

.hidden { display: none; }

/* Records tabs */
.tab {
  background: transparent;
  color: var(--muted);
  border: none;
  border-bottom: 2px solid transparent;
  padding: 0.5rem 1rem;
  font-size: 0.875rem;
  cursor: pointer;
  min-width: unset;
  border-radius: 0;
  transition: color 0.2s ease, border-color 0.2s ease;
}
.tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}
.tab:hover {
  opacity: 1;
  color: var(--fg);
}

/* Records table rows */
.records-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 0.5rem;
}
.records-table th, .records-table td {
  padding: 0.4rem 0.5rem;
  text-align: center;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  font-size: 0.9375rem;
}
.records-table th {
  color: var(--muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.records-table .rank {
  color: var(--muted);
  font-size: 0.8125rem;
  width: 2rem;
}
.records-table .speed-col { color: var(--accent); font-weight: 600; }
.records-table .acc-col  { color: var(--correct); }
.records-table .date-col  { color: var(--muted); font-size: 0.8125rem; }

/* Feedback floating button */
#feedback-fab {
  position: fixed;
  bottom: 1rem;
  right: 1rem;
  z-index: 10;
  background: transparent;
  color: var(--muted);
  border: none;
  font-size: 0.8125rem;
  cursor: pointer;
  min-width: unset;
  padding: 0.25rem 0.5rem;
  transition: color 0.2s ease;
}
#feedback-fab:hover {
  color: var(--fg);
  opacity: 1;
  transform: none;
}

/* Completion banner */
.completion-banner {
  text-align: center;
  padding: 0.5rem 1rem;
  margin-bottom: 1rem;
  border-radius: 0.5rem;
  background: var(--card-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 0.5rem;
}
.banner-stat-label {
  color: var(--muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-right: 0.25rem;
}
.banner-stat {
  font-size: 1.125rem;
  font-weight: 600;
  color: var(--accent);
}
.banner-stat small {
  font-size: 0.6875rem;
  font-weight: 400;
  color: var(--muted);
}
.banner-divider {
  color: var(--muted);
  font-size: 0.875rem;
}
.banner-best {
  font-size: 1.25rem;
  margin-left: 0.25rem;
  animation: pop 0.4s ease;
}
@keyframes pop {
  0%   { transform: scale(0); }
  70%  { transform: scale(1.3); }
  100% { transform: scale(1); }
}
.banner-enter-active { transition: all 0.35s ease-out; }
.banner-leave-active { transition: all 0.15s ease-in; }
.banner-enter-from  { opacity: 0; transform: translateY(-8px); }
.banner-leave-to    { opacity: 0; transform: translateY(-8px); }

/* Review panel */
.review-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.review-panel {
  background: var(--card-bg);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: 0.75rem;
  padding: 2rem;
  max-width: 40rem;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
}

.review-panel h2 {
  margin: 0 0 1rem;
  font-size: 1.5rem;
}

.review-table {
  width: 100%;
  border-collapse: collapse;
}

.review-table th, .review-table td {
  padding: 0.5rem 0.75rem;
  text-align: left;
  border-bottom: 1px solid rgba(255,255,255,0.1);
}

.review-table th {
  color: var(--muted);
  font-size: 0.875rem;
}

.review-table .wrong-word { color: var(--wrong); font-weight: 600; }
.review-table .correct-word { color: var(--correct); }
.review-table .count-badge {
  background: var(--accent);
  color: #000;
  border-radius: 1rem;
  padding: 0.125rem 0.5rem;
  font-size: 0.75rem;
  font-weight: 600;
}

.review-empty {
  color: var(--muted);
  text-align: center;
  padding: 2rem;
}

/* Mobile responsive */
@media (max-width: 640px) {
  h1 {
    font-size: 1.5rem;
    top: -2rem;
    letter-spacing: 0.05em;
  }

  .stats {
    gap: 0.5rem;
    margin-bottom: 1rem;
  }

  .stat-label {
    font-size: 0.75rem;
  }

  .stat-value {
    font-size: 1.125rem;
  }

  .text-display {
    padding: 1rem;
    font-size: 1rem;
    min-height: 6rem;
  }

  .controls .btn {
    min-width: 5rem;
    padding: 0.6rem 1rem;
    font-size: 0.875rem;
  }

  .review-panel {
    padding: 1.25rem;
    width: 95%;
  }
}
