:root {
  --bg: #f7f7fb;
  --surface: #ffffff;
  --text: #1c1c28;
  --muted: #6b6b7b;
  --border: #e0e0ea;
  --accent: #4f46e5;
  --correct: #22c55e;
  --wrong: #ef4444;
  --new: #9ca3af;
  --learning: #3b82f6;
  --familiar: #f59e0b;
  --mastered: #22c55e;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14141c;
    --surface: #1f1f2c;
    --text: #eceef2;
    --muted: #9797a8;
    --border: #2c2c3c;
    --accent: #818cf8;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding: 1.5rem;
}

.app-header {
  text-align: center;
  margin-bottom: 1rem;
}

.app-header h1 {
  margin: 0 0 0.25rem;
  font-size: 1.6rem;
}

.subtitle {
  margin: 0;
  color: var(--muted);
}

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

.stat-chip {
  font-size: 0.8rem;
  padding: 0.3rem 0.7rem;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--muted);
}

.stat-new { border-color: var(--new); }
.stat-learning { border-color: var(--learning); color: var(--learning); }
.stat-familiar { border-color: var(--familiar); color: var(--familiar); }
.stat-mastered { border-color: var(--mastered); color: var(--mastered); }
.stat-total { font-weight: 600; color: var(--text); }

.level-progress {
  max-width: 320px;
  margin: -0.75rem auto 1.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.3rem;
}

.level-progress-label {
  font-size: 0.75rem;
  color: var(--muted);
}

.level-progress-track {
  display: block;
  width: 100%;
  height: 0.3rem;
  border-radius: 999px;
  background: var(--border);
  overflow: hidden;
}

.level-progress-fill {
  display: block;
  height: 100%;
  background: var(--accent);
  border-radius: 999px;
  transition: width 0.3s ease;
}

.board {
  max-width: 760px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.5rem;
}

.column-title {
  text-align: center;
  font-size: 1rem;
  color: var(--muted);
  margin: 0 0 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.card-list {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  min-height: 20rem;
}

.card {
  display: flex;
  flex-direction: column;
  justify-content: center; /* centers the card-main+context block when context is absent */
  gap: 0.2rem;
  font-family: inherit;
  padding: 0.85rem 1rem;
  /* Fixed height (min AND max, not just a minimum) so every card is the
     same size regardless of content -- a short word, a long phrase, and an
     English card with a context subheading all match. Sized to fit
     card-main's own fixed 3-line height (see below) plus room for context's
     own 2-line clamp (see .card-context) -- context isn't otherwise
     height-reserved, since it's only ever present on some English cards,
     never on the Spanish side (see main.js), and ~10% of context strings
     genuinely run to 2 lines even at desktop width, not just as an edge
     case. overflow: hidden is the safety net for the rare longer one.
     Re-computed for the mobile breakpoint below, since padding/font-sizes
     change there too. */
  min-height: 7.6rem;
  max-height: 7.6rem;
  overflow: hidden;
  border-radius: 0.6rem;
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  text-align: center;
  transition: transform 0.1s ease, border-color 0.15s ease, background 0.15s ease;
  /* Long-press-friendly: no text-selection callout or native context menu flicker on hold. */
  touch-action: manipulation;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

.card-main {
  font-size: 1rem;
  line-height: 1.3;
  /* Fixed at exactly 3 lines' worth of height (3 x line-height) -- both
     min- and max-height, not just a minimum -- regardless of whether this
     particular card's text needs it, so a short single-word card and a
     long-phrase card are always the same height instead of the list
     looking jagged, and so a card swapping to a longer/shorter word on
     match never changes height either. In em units so it scales
     automatically with the smaller font-size .long-text and the mobile
     breakpoint use. The rare phrase that would wrap past 3 lines gets
     clipped (overflow: hidden) rather than growing the card.  */
  min-height: 3.9em;
  max-height: 3.9em;
  overflow: hidden;
  display: flex;
  align-items: center; /* vertical centering within the fixed height */
  justify-content: center; /* horizontal centering (text-align, above, handles wrapped lines) */
}

.card.long-text .card-main {
  font-size: 0.85rem;
}

.card-context {
  font-size: 0.72rem;
  font-weight: 400;
  line-height: 1.25;
  color: var(--muted);
  /* .card's fixed total height (see above) leaves room for about one line
     of context -- fine for most, but a few genuinely run to 2-3 lines
     (e.g. the "derecho"/"derecha" disambiguation). Clamping to 2 lines
     with an ellipsis degrades those gracefully instead of the card's
     overflow: hidden clipping mid-sentence with no visual indicator. Not
     needed on .card-main: its own longest real entries are comfortably
     short, and it's already a flex box for centering, which -webkit-box
     (required for line-clamp) can't be combined with on the same element. */
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card:hover {
  border-color: var(--accent);
}

.card:active {
  transform: scale(0.98);
}

.card.selected {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, var(--surface));
}

/* A confirmed match: green and locked (see selectCard in game.js -- it
   can't be selected or deselected anymore), staying this way until a
   second pair is also confirmed and both clear together. Not
   pointer-events: none, though -- that would also block the long-press
   menu, which should still work on it (e.g. marking it known). */
.card.correct {
  border-color: var(--correct);
  background: color-mix(in srgb, var(--correct) 20%, var(--surface));
  cursor: default;
}

.card.wrong {
  border-color: var(--wrong);
  background: color-mix(in srgb, var(--wrong) 20%, var(--surface));
}

/* Correct-match resolution (see game.js resolveMatch/swapInReplacement):
   the matched card fades out in place -- still green, since it just won --
   then its replacement fades in at that same board position. Every other
   card's index is untouched by this, so nothing else has to shift to fill
   a gap. Keyframe animations (not transitions) are used deliberately: this
   app re-renders columns by rebuilding their DOM from scratch, and a
   transition only plays on a property change to an *already-mounted*
   element, whereas a keyframe animation plays as soon as the element (with
   this class) is inserted, mount or not. */
.card.card-removing {
  border-color: var(--correct);
  background: color-mix(in srgb, var(--correct) 20%, var(--surface));
  pointer-events: none;
  animation: card-fade-out 220ms ease forwards;
}

.card.card-entering {
  animation: card-fade-in 250ms ease;
}

@keyframes card-fade-out {
  from {
    opacity: 1;
    transform: scale(1);
  }
  to {
    opacity: 0;
    transform: scale(0.92);
  }
}

@keyframes card-fade-in {
  from {
    opacity: 0;
    transform: scale(0.92);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.controls {
  max-width: 760px;
  margin: 2rem auto 0;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
  color: var(--muted);
}

.controls select {
  margin-left: 0.4rem;
  padding: 0.2rem 0.4rem;
}

#reset-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 0.4rem 0.9rem;
  border-radius: 0.5rem;
  cursor: pointer;
}

#reset-btn:hover {
  border-color: var(--wrong);
  color: var(--wrong);
}

.hint {
  max-width: 760px;
  margin: 0.75rem auto 0;
  text-align: center;
  font-size: 0.8rem;
  color: var(--muted);
}

/* ------------------------------------------------------------------- */
/* Tabs + panels                                                       */
/* ------------------------------------------------------------------- */

.tab-bar {
  max-width: 1000px;
  margin: 0 auto 1.5rem;
  display: flex;
  gap: 0.5rem;
  justify-content: center;
  flex-wrap: wrap;
}

.tab-btn {
  font: inherit;
  padding: 0.5rem 1.1rem;
  border-radius: 999px;
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  cursor: pointer;
}

.tab-btn[aria-pressed='true'] {
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 600;
  background: color-mix(in srgb, var(--accent) 10%, var(--surface));
}

.panel {
  max-width: 1000px;
  margin: 0 auto;
}

/* ------------------------------------------------------------------- */
/* Word List tab                                                       */
/* ------------------------------------------------------------------- */

.word-list-controls {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
  align-items: center;
  margin-bottom: 1rem;
}

.word-list-controls input[type='search'],
.word-list-controls select {
  font: inherit;
  padding: 0.5rem 0.7rem;
  border-radius: 0.5rem;
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--text);
}

.word-list-controls input[type='search'] {
  flex: 1 1 200px;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  color: var(--muted);
  font-size: 0.9rem;
  white-space: nowrap;
}

.table-scroll {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 0.6rem;
}

.word-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}

.word-table th,
.word-table td {
  padding: 0.55rem 0.75rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.word-table thead th {
  cursor: pointer;
  color: var(--muted);
  background: var(--surface);
  user-select: none;
}

.word-table thead th.sorted-asc::after {
  content: ' \25B2';
}

.word-table thead th.sorted-desc::after {
  content: ' \25BC';
}

.word-table tbody tr:hover {
  background: color-mix(in srgb, var(--accent) 6%, transparent);
}

.table-action-btn {
  font: inherit;
  font-size: 0.8rem;
  padding: 0.35rem 0.7rem;
  border-radius: 0.4rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

.pagination {
  margin: 0.75rem 0 0;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1rem;
  color: var(--muted);
  font-size: 0.85rem;
}

.pagination button {
  font: inherit;
  padding: 0.35rem 0.8rem;
  border-radius: 0.4rem;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}

.pagination button:disabled {
  opacity: 0.4;
  cursor: default;
}

/* ------------------------------------------------------------------- */
/* Stats tab                                                            */
/* ------------------------------------------------------------------- */

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

.streak-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.75rem;
  padding: 1rem 1.5rem;
  text-align: center;
  min-width: 8rem;
}

.streak-number {
  font-size: 2rem;
  font-weight: 700;
  color: var(--accent);
}

.streak-label {
  font-size: 0.8rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.section-title {
  font-size: 0.95rem;
  color: var(--muted);
  text-align: center;
  margin: 0 0 0.5rem;
  font-weight: 500;
}

.chart-container {
  margin: 0 auto 1.5rem;
  max-width: 700px;
}

.chart-container svg {
  width: 100%;
  height: 140px;
  display: block;
}

.chart-bar {
  fill: var(--accent);
}

.chart-range-label {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 0.3rem;
}

.stats-grid {
  max-width: 700px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.75rem 1.5rem;
}

.stats-grid dt {
  color: var(--muted);
  font-size: 0.85rem;
}

.stats-grid dd {
  margin: 0.1rem 0 0;
  font-size: 1.15rem;
  font-weight: 600;
}

/* ------------------------------------------------------------------- */
/* Long-press card menu                                                */
/* ------------------------------------------------------------------- */

.card-menu {
  position: fixed;
  z-index: 100;
  min-width: 220px;
  max-width: 90vw;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 0.6rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
  padding: 0.4rem;
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

/* The [hidden] attribute selector's specificity beats a bare .card-menu
   selector (one class + one attribute vs. one class), so this correctly
   overrides the display: flex above -- without it, the browser's default
   [hidden] { display: none } UA-stylesheet rule loses to our own
   .card-menu { display: flex }, and setting .hidden = true in JS (see
   closeCardMenu in main.js) has *no visual effect at all*: the menu stays
   on screen, just empty, wherever it was last positioned. */
.card-menu[hidden] {
  display: none;
}

.card-menu-title {
  padding: 0.4rem 0.6rem 0.5rem;
  font-size: 0.8rem;
  color: var(--muted);
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.2rem;
}

.card-menu-item {
  font: inherit;
  text-align: left;
  padding: 0.6rem 0.6rem;
  border-radius: 0.4rem;
  border: none;
  background: none;
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

.card-menu-item:hover:not(:disabled) {
  background: color-mix(in srgb, var(--accent) 10%, transparent);
}

.card-menu-item.active {
  color: var(--mastered);
}

.card-menu-item:disabled {
  cursor: default;
  opacity: 0.7;
}

.card-menu-cancel {
  color: var(--muted);
  border-top: 1px solid var(--border);
  margin-top: 0.2rem;
  padding-top: 0.6rem;
}

/* ------------------------------------------------------------------- */
/* Small-screen tweaks                                                  */
/* ------------------------------------------------------------------- */

@media (max-width: 480px) {
  body {
    padding: 1rem 0.75rem;
  }

  /* Keep the board two columns (left/right pairing is the whole point of
     the game) even at narrow phone widths -- just tighten the gap and
     shrink card padding/text so two columns comfortably fit. */
  .board {
    gap: 0.6rem;
  }

  .card-list {
    gap: 0.4rem;
    min-height: 0;
  }

  .card {
    padding: 0.6rem 0.6rem;
    /* Re-derived for this breakpoint's smaller padding/font-sizes -- see
       the base .card rule's comment for how this is computed. */
    min-height: 6.35rem;
    max-height: 6.35rem;
  }

  .card-main {
    font-size: 0.85rem;
  }

  .card.long-text .card-main {
    font-size: 0.72rem;
  }

  .card-context {
    font-size: 0.65rem;
  }

  .stats-grid {
    grid-template-columns: 1fr;
  }

  .word-list-controls {
    flex-direction: column;
    align-items: stretch;
  }

  /* flex-basis follows the main axis: once the container above switches
     to a column, the search input's 200px *width* hint would otherwise
     be read as a 200px *height* instead. */
  .word-list-controls input[type='search'] {
    flex-basis: auto;
  }

  .streak-card {
    min-width: 40%;
    padding: 0.85rem 1rem;
  }
}
