/* ── Design tokens ─────────────────────────────────────────────────────────── */

:root {
  --bg: #f7f8fa;
  --surface: #ffffff;
  --surface-raised: #ffffff;
  --text: #12151a;
  --text-muted: #5b6472;
  --border: #d3d8e0;

  --tile-border-empty: #d8dce4;
  --tile-border-filled: #98a2b3;

  --correct: #24885d;
  --present: #a9791b;
  --absent: #6b7482;
  --on-state: #ffffff;

  --key-bg: #e2e6ed;
  --key-text: #12151a;

  --focus-ring: #2563eb;
  --shadow: 0 10px 30px rgba(15, 23, 42, 0.12);
  --shadow: 0 10px 30px rgb(15 23 42 / 12%);

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;

  /* System fonts only: every platform ships a good Hebrew face, and the app stays
     fully functional offline with no external requests. */
  --font: system-ui, -apple-system, 'Segoe UI', 'Noto Sans Hebrew', 'Arial Hebrew', Arial,
    sans-serif;

  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --surface: #171b21;
    --surface-raised: #1e232b;
    --text: #eef1f5;
    --text-muted: #9aa4b4;
    --border: #2b313a;

    --tile-border-empty: #2b313a;
    --tile-border-filled: #4b5563;

    --correct: #2f9e6e;
    --present: #c9932f;

    /* Darker than --key-bg on purpose: an eliminated key must recede, not
       stand out more than one that is still unknown. */
    --absent: #343a44;
    --on-state: #ffffff;

    --key-bg: #4b5563;
    --key-text: #eef1f5;

    --focus-ring: #7aa2ff;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
    --shadow: 0 10px 30px rgb(0 0 0 / 45%);

    color-scheme: dark;
  }
}

/* ── Base ──────────────────────────────────────────────────────────────────── */

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

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

button:focus,
[tabindex]:focus {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

button:focus:not(:focus-visible),
[tabindex]:focus:not(:focus-visible) {
  outline: none;
}

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

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

/* ── Layout ────────────────────────────────────────────────────────────────── */

.app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  min-height: 100dvh;
  max-width: 520px;
  margin: 0 auto;
  margin-inline: auto;
  padding: 0 0.5rem;
  padding-inline: clamp(0.5rem, 3vw, 1rem);
}

.app-centered {
  align-items: center;
  justify-content: center;
  text-align: center;
}

.app-centered > * + * {
  margin-top: 1rem;
}

.static-status h1,
.static-status p {
  margin: 0;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  padding: 0.6rem 0;
  padding-block: 0.6rem;
  border-bottom: 1px solid var(--border);
  border-block-end: 1px solid var(--border);
}

.header-title h1 {
  margin: 0;
  font-size: 1.2rem;
  font-size: clamp(1.05rem, 4.5vw, 1.4rem);
  font-weight: 800;
  letter-spacing: 0.01em;
  text-align: center;
}

.header-subtitle {
  margin: 0.1rem 0 0;
  font-size: 0.75rem;
  color: var(--text-muted);
  text-align: center;
}

.main {
  display: flex;
  flex: 1;
  flex-direction: column;
  align-items: center;
  padding: 0.5rem 0 0.75rem;
  padding-block: 0.5rem 0.75rem;
}

.main > * + * {
  margin-top: 0.5rem;
}

.status-message {
  margin: 0;
  color: var(--text-muted);
}

.status-error {
  color: var(--text);
  font-weight: 600;
}

/* ── Board ─────────────────────────────────────────────────────────────────── */

.board {
  /* Tiles shrink to fit the narrowest of: viewport width, container, and the
     height left over once the header, keyboard and gaps are accounted for. */
  --gap: 4px;
  --gap: clamp(4px, 1.2vw, 6px);
  --tile-size: 52px;
  --tile-size: min(
    62px,
    calc((min(100vw, 520px) - 2rem - 4 * var(--gap)) / 5),
    calc((100dvh - 20rem) / 6)
  );

  display: flex;
  flex-direction: column;
  grid-template-rows: repeat(6, var(--tile-size));
  gap: var(--gap);

  /* Absorbs the spare height, centring the board between the header and the
     keyboard while the keyboard stays anchored at the bottom of the screen. */
  margin-top: auto;
  margin-bottom: auto;
  margin-block: auto;
}

.board-row + .board-row {
  margin-top: var(--gap);
}

.board-row {
  /* Logical index 0 renders in the rightmost column, so a Hebrew word reads
     naturally without ever reversing the underlying string. */
  direction: rtl;
  display: flex;
  grid-template-columns: repeat(5, var(--tile-size));
  gap: var(--gap);
}

.board-row .tile + .tile {
  margin-right: var(--gap);
}

@supports (display: grid) {
  .board,
  .board-row {
    display: grid;
  }

  .board-row + .board-row,
  .board-row .tile + .tile {
    margin-top: 0;
    margin-right: 0;
  }
}

.tile {
  --tile-bg: var(--surface);
  --tile-fg: var(--text);
  --tile-border: var(--tile-border-empty);

  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid var(--tile-border);
  border-radius: var(--radius-sm);
  background: var(--tile-bg);
  color: var(--tile-fg);
  font-size: calc(var(--tile-size) * 0.56);
  font-weight: 700;
  line-height: 1;
  user-select: none;
}

.tile[data-filled] {
  --tile-border: var(--tile-border-filled);
}

.tile[data-state='correct'] {
  --tile-bg: var(--correct);
  --tile-fg: var(--on-state);
  --tile-border: var(--correct);
}

.tile[data-state='present'] {
  --tile-bg: var(--present);
  --tile-fg: var(--on-state);
  --tile-border: var(--present);
}

.tile[data-state='absent'] {
  --tile-bg: var(--absent);
  --tile-fg: var(--on-state);
  --tile-border: var(--absent);
}

/* ── Keyboard ──────────────────────────────────────────────────────────────── */

.keyboard {
  --key-gap: 3px;
  --key-gap: clamp(3px, 1.1vw, 6px);

  /* Dropping the five final letters leaves the top row with only six keys. Rows
     stretch to fill, but a cap stops those six from ballooning; the row simply
     centres instead, the way a short keyboard row normally does. */
  --key-max: 3rem;

  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 500px;
}

.keyboard-row + .keyboard-row {
  margin-top: 5px;
}

.keyboard-row {
  display: flex;
  justify-content: center;
}

.keyboard-row .key + .key {
  margin-left: var(--key-gap);
}

.key {
  flex: 1 1 0;
  min-width: 0;
  max-width: var(--key-max);
  height: 50px;
  height: clamp(46px, 7.5dvh, 58px);
  padding: 0;
  border: 0;
  border-radius: var(--radius-md);
  background: var(--key-bg);
  color: var(--key-text);
  font-size: 1rem;
  font-size: clamp(0.95rem, 4vw, 1.15rem);
  font-weight: 600;
  touch-action: manipulation;
  transition: background-color 120ms ease, transform 80ms ease;
}

.key:active:not(:disabled) {
  transform: translateY(1px);
}

.key:disabled {
  opacity: 0.6;
  cursor: default;
}

.key-wide {
  flex: 1.6 1 0;
  max-width: calc(var(--key-max) * 1.6);
  font-size: 0.75rem;
  font-size: clamp(0.7rem, 3vw, 0.85rem);
}

.key[data-state='correct'] {
  background: var(--correct);
  color: var(--on-state);
}

.key[data-state='present'] {
  background: var(--present);
  color: var(--on-state);
}

.key[data-state='absent'] {
  background: var(--absent);
  color: var(--on-state);
}

/* ── Toast ─────────────────────────────────────────────────────────────────── */

.toast-area {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 2.4rem;
  pointer-events: none;
}

.toast {
  padding: 0.5rem 0.9rem;
  border-radius: var(--radius-md);
  background: var(--text);
  color: var(--bg);
  font-size: 0.9rem;
  font-weight: 600;
  box-shadow: var(--shadow);
  animation: toast-in 160ms ease-out;
}

/* ── Result ────────────────────────────────────────────────────────────────── */

.result {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  padding: 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: var(--surface-raised);
}

.result > * + * {
  margin-top: 0.75rem;
}

.result-headline {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 700;
  text-align: center;
}

.result-answer {
  color: var(--correct);
  letter-spacing: 0.05em;
}

.result-score {
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.result-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
}

.result-actions .button + .button {
  margin-right: 0.5rem;
}

.countdown {
  margin: 0;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.countdown-value {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

.button {
  padding: 0.6rem 1.1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
}

.button-primary {
  border-color: transparent;
  background: var(--correct);
  color: var(--on-state);
}

.icon-button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 2.5rem;
  height: 2.5rem;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: transparent;
  font-size: 1rem;
  font-weight: 700;
  flex-shrink: 0;
}

.stats-icon {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  width: 1.1rem;
  height: 1.1rem;
}

.stats-icon span {
  width: 0.22rem;
  margin: 0 0.06rem;
  background: currentColor;
}

.stats-icon span:nth-child(1) {
  height: 45%;
}

.stats-icon span:nth-child(2) {
  height: 100%;
}

.stats-icon span:nth-child(3) {
  height: 70%;
}

/* ── Dialogs ───────────────────────────────────────────────────────────────── */

.dialog-backdrop {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: rgba(0, 0, 0, 0.45);
  background: rgb(0 0 0 / 45%);
}

.dialog {
  width: 100%;
  max-width: 420px;
  width: min(100%, 420px);
  max-height: 85vh;
  max-height: 85dvh;
  overflow-y: auto;
  padding: 1rem 1.25rem 1.5rem;
  border-radius: var(--radius-lg);
  background: var(--surface-raised);
  box-shadow: var(--shadow);
  animation: dialog-in 160ms ease-out;
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.5rem;
  margin-block-end: 0.5rem;
}

.dialog-title {
  margin: 0;
  font-size: 1.15rem;
}

.dialog-body p {
  margin: 0.6rem 0;
  font-size: 0.92rem;
  line-height: 1.6;
}

.example-row {
  --tile-size: 40px;
  --gap: 4px;
  margin: 0.75rem 0 0.25rem;
  margin-block: 0.75rem 0.25rem;
  justify-content: start;
}

.help-note,
.help-footer {
  color: var(--text-muted);
  font-size: 0.85rem !important;
}

.help-footer {
  padding-top: 0.75rem;
  padding-block-start: 0.75rem;
  border-top: 1px solid var(--border);
  border-block-start: 1px solid var(--border);
}

.setting {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.85rem 0;
  padding-block: 0.85rem;
  border-top: 1px solid var(--border);
  border-block-start: 1px solid var(--border);
}

.setting-text {
  display: flex;
  flex-direction: column;
}

.setting-title {
  font-weight: 600;
}

.setting-hint {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.setting input[type='checkbox'] {
  width: 1.4rem;
  height: 1.4rem;
  accent-color: var(--correct);
  flex-shrink: 0;
}

/* ── Statistics ────────────────────────────────────────────────────────────── */

.stats-summary {
  display: flex;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
  margin: 0.5rem 0 1.25rem;
}

.stat {
  flex: 1 1 25%;
}

@supports (display: grid) {
  .stats-summary {
    display: grid;
  }
}

.stat {
  text-align: center;
}

.stat dt {
  font-size: 0.7rem;
  color: var(--text-muted);
  line-height: 1.3;
}

.stat dd {
  margin: 0.15rem 0 0;
  font-size: 1.5rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.stats-heading {
  margin: 0 0 0.5rem;
  font-size: 0.9rem;
}

.stats-empty {
  color: var(--text-muted);
  font-size: 0.9rem;
}

.distribution {
  display: flex;
  flex-direction: column;
  margin: 0;
  padding: 0;
  list-style: none;
}

.distribution-row + .distribution-row {
  margin-top: 0.3rem;
}

.distribution-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
}

.distribution-label {
  width: 1rem;
  margin-left: 0.5rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.distribution-bar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  min-width: 1.5rem;
  padding: 0 0.4rem;
  padding-inline: 0.4rem;
  border-radius: var(--radius-sm);
  background: var(--absent);
  color: var(--on-state);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.distribution-bar[data-highlight] {
  background: var(--correct);
}

/* ── Animation ─────────────────────────────────────────────────────────────── */

@keyframes pop {
  0% {
    transform: scale(0.9);
  }

  60% {
    transform: scale(1.06);
  }

  100% {
    transform: scale(1);
  }
}

@keyframes shake {
  0%,
  100% {
    transform: translateX(0);
  }

  20% {
    transform: translateX(-6px);
  }

  40% {
    transform: translateX(6px);
  }

  60% {
    transform: translateX(-4px);
  }

  80% {
    transform: translateX(4px);
  }
}

/* The colour swaps at the midpoint, while the tile is edge-on. */
@keyframes reveal {
  0% {
    transform: rotateX(0deg);
    background: var(--surface);
    border-color: var(--tile-border-filled);
    color: var(--text);
  }

  49.9% {
    transform: rotateX(90deg);
    background: var(--surface);
    border-color: var(--tile-border-filled);
    color: var(--text);
  }

  50% {
    transform: rotateX(90deg);
  }

  100% {
    transform: rotateX(0deg);
  }
}

@keyframes celebrate {
  0%,
  100% {
    transform: translateY(0);
  }

  40% {
    transform: translateY(-14px);
  }

  70% {
    transform: translateY(-4px);
  }
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(-6px);
  }
}

@keyframes dialog-in {
  from {
    opacity: 0;
    transform: translateY(8px) scale(0.98);
  }
}

.tile[data-filled]:not([data-state]) {
  animation: pop 110ms ease-out;
}

.tile[data-revealing] {
  /* `backwards` keeps the pre-flip colours during the stagger delay, so a tile
     never shows its result before its turn. */
  animation: reveal 520ms ease-in-out backwards;
}

.board-row[data-rejected] {
  animation: shake 480ms ease-in-out;
}

/* Delay comes from an inline style per tile, so the stagger does not depend on
   the row's DOM order (the screen-reader summary is also a child). */
.tile[data-celebrating] {
  animation: celebrate 620ms ease-in-out backwards;
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}
