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

:root {
  --bg:         #0d0d0d;
  --surface:    #1a1a1a;
  --border:     #2e2e2e;
  --text:       #e0e0e0;
  --text-muted: #666;
  --green:      #44b926;
  --red:        #d23a18;
  --key-bg:     #252525;
  --tile-hidden:#1f1f1f;
  --tile-border:#383838;
  --tile-reveal:#0f1f0b;
  --tile-glow:  rgba(68,185,38,0.28);
  --accent:     #44b926;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'Courier New', 'Consolas', monospace;
  user-select: none;
  overflow-x: hidden;
}

/* ── Layout ──────────────────────────────────────────────────────── */
#app {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
  padding: 16px 12px 24px;
}

/* ── Tension: glow on the FIVE display only, not the whole screen ──── */
/* The #tension-overlay element is kept in the DOM but never used.     */
/* Tension is expressed as a box-shadow on #five-display via JS.       */
#tension-overlay { display: none; }

#five-display {
  transition: box-shadow 0.8s ease;
}

/* ── Lose strobe & win flash overlays ────────────────────────────── */
#lose-flash, #win-flash {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 60;
  opacity: 0;
}
#lose-flash { background: rgba(210, 58, 24, 0.82); transition: opacity 0.07s ease; }
#win-flash  { background: rgba(68, 185, 38, 0.38); transition: opacity 0.12s ease; }

/* ── Header ──────────────────────────────────────────────────────── */
header {
  width: 100%;
  max-width: 600px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 4px 12px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 14px;
  flex-wrap: wrap;
  gap: 8px;
}

.logo {
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: 0.15em;
  color: var(--accent);
  text-shadow: 0 0 12px rgba(68,185,38,0.5);
}

.header-btns { display: flex; gap: 6px; flex-wrap: wrap; }

.icon-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 5px 9px;
  cursor: pointer;
  font-size: 0.72rem;
  letter-spacing: 0.05em;
  font-family: inherit;
  transition: background 0.15s, border-color 0.15s;
}
.icon-btn:hover { background: #242424; border-color: #444; }
.icon-btn.active-toggle {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(68,185,38,0.08);
}

/* ── FIVE frame ──────────────────────────────────────────────────── */
#five-display {
  width: 100%;
  max-width: 500px;
  aspect-ratio: 15/7;
  position: relative;
  margin-bottom: 0;
}

#five-display img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  border-radius: 8px;
  transition: filter 0.4s ease;
}

#five-display img.high-tension {
  animation: tension-pulse 1.8s ease-in-out infinite;
}

@keyframes tension-pulse {
  0%, 100% { filter: brightness(1) saturate(1); }
  50%       { filter: brightness(1.12) saturate(1.2); }
}

/* ── Win: tile border blinks green × 3 ───────────────────────────── */
@keyframes tile-win-blink {
  0%, 100% {
    border-color: var(--accent);
    box-shadow: 0 0 10px var(--tile-glow);
  }
  50% {
    border-color: #c8ffa0;
    box-shadow: 0 0 30px rgba(68,185,38,0.9), 0 0 55px rgba(68,185,38,0.35);
  }
}

.tile.win-blink {
  animation: tile-win-blink 0.44s ease-in-out 3;
}

/* ── Fail: doom tiles pulse heavy red × 2 after reveal ───────────── */
@keyframes tile-doom-pulse {
  0%, 100% {
    border-color: var(--red);
    box-shadow: 0 0 8px rgba(210,58,24,0.22);
  }
  50% {
    border-color: #ff9090;
    box-shadow: 0 0 26px rgba(210,58,24,0.80), 0 0 48px rgba(210,58,24,0.28);
  }
}

.tile.doom-pulse {
  animation: tile-doom-pulse 0.85s ease-in-out 2;
}

#mistake-counter {
  position: absolute;
  bottom: 5px;
  right: 9px;
  font-size: 0.65rem;
  color: var(--text-muted);
  letter-spacing: 0.1em;
}

/* ── Timer bar ───────────────────────────────────────────────────── */
#timer-wrap {
  width: 100%;
  max-width: 500px;
  height: 5px;
  background: #1a1a1a;
  border-radius: 3px;
  overflow: hidden;
  margin-bottom: 12px;
  display: none;
}

#timer-bar {
  height: 100%;
  width: 100%;
  background: hsl(118,88%,42%);
  border-radius: 3px;
  transition: width 0.1s linear, background 0.3s ease;
}

#timer-bar.urgent {
  animation: timer-flash 0.25s ease-in-out infinite alternate;
}

@keyframes timer-flash {
  from { opacity: 1; }
  to   { opacity: 0.3; }
}

/* ── Difficulty & mode selectors ─────────────────────────────────── */
#difficulty-bar, #mode-bar {
  display: flex;
  gap: 6px;
  margin-bottom: 10px;
}

.diff-btn, .mode-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-muted);
  padding: 4px 12px;
  border-radius: 20px;
  cursor: pointer;
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  font-family: inherit;
  transition: all 0.15s;
}

.diff-btn.locked, .diff-btn:disabled,
.mode-btn.locked, .mode-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
  pointer-events: none;
}

.diff-btn.active, .mode-btn.active {
  border-color: var(--accent);
  color: var(--accent);
  background: rgba(68,185,38,0.08);
}

/* ── Hint ────────────────────────────────────────────────────────── */
#hint-display {
  font-size: 0.7rem;
  color: var(--text-muted);
  letter-spacing: 0.08em;
  margin-bottom: 8px;
  text-align: center;
  min-height: 1em;
}

/* ── Word tiles ──────────────────────────────────────────────────── */
#word-display {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: center;
  margin-bottom: 20px;
  max-width: 560px;
}

.tile {
  width: 44px;
  height: 52px;
  border: 2px solid var(--tile-border);
  border-radius: 6px;
  background: var(--tile-hidden);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  font-weight: 700;
  position: relative;
  transition: background 0.25s, border-color 0.25s;
}

/* Scratch-card hatching on hidden tiles */
.tile.hidden { color: transparent; }
.tile.hidden::after {
  content: '';
  position: absolute;
  inset: 3px;
  border-radius: 3px;
  background: repeating-linear-gradient(
    45deg,
    #252525, #252525 2px,
    #1f1f1f 2px, #1f1f1f 6px
  );
}

.tile.revealed {
  background: var(--tile-reveal);
  border-color: var(--accent);
  color: var(--accent);
}

/* Card flip */
.tile.flip {
  animation: card-flip 0.32s ease-out;
}

@keyframes card-flip {
  0%   { transform: rotateY(0deg) scale(1); }
  40%  { transform: rotateY(88deg) scale(0.92); opacity: 0.4; }
  60%  { transform: rotateY(-12deg) scale(1.04); opacity: 1; }
  100% { transform: rotateY(0deg) scale(1); }
}

/* Glow burst on reveal */
.tile.burst {
  animation: tile-burst 0.55s ease-out forwards;
}

@keyframes tile-burst {
  0%   { box-shadow: 0 0  0px 0   rgba(68,185,38,0); }
  25%  { box-shadow: 0 0 22px 7px rgba(68,185,38,0.72); }
  100% { box-shadow: 0 0 10px 2px rgba(68,185,38,0.22); }
}

/* ── Doom tile — unrevealed letters exposed on loss ─────────────── */
.tile.revealed-doom {
  background: #1f0505;
  border-color: var(--red);
  color: var(--red);
}

.tile.flip-doom {
  animation: card-flip-doom 0.28s ease-out;
}

@keyframes card-flip-doom {
  0%   { transform: rotateY(0deg) scale(1); }
  45%  { transform: rotateY(88deg) scale(0.9); opacity: 0.35; }
  65%  { transform: rotateY(-8deg) scale(1.03); opacity: 1; }
  100% { transform: rotateY(0deg) scale(1); }
}

.tile.burst-doom {
  animation: tile-burst-doom 0.45s ease-out forwards;
}

@keyframes tile-burst-doom {
  0%   { box-shadow: 0 0  0px 0   rgba(210,58,24,0); }
  25%  { box-shadow: 0 0 20px 6px rgba(210,58,24,0.65); }
  100% { box-shadow: 0 0  8px 2px rgba(210,58,24,0.20); }
}

/* ── Keyboard ────────────────────────────────────────────────────── */
#keyboard {
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: center;
  width: 100%;
  max-width: 560px;
}

.key-row { display: flex; gap: 5px; justify-content: center; }

.key {
  position: relative;
  width: 38px;
  height: 48px;
  background: var(--key-bg);
  border: 1px solid #333;
  border-radius: 6px;
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.key:active { transform: scale(0.91); }

.key sup {
  position: absolute;
  top: 3px;
  right: 4px;
  font-size: 0.55rem;
  font-weight: 400;
  color: var(--text-muted);
  line-height: 1;
}

.key.used    { border-color: #3a3a3a; }
.key.hit     { background: #1a3010; border-color: #336622; color: var(--green); }
.key.miss    { background: #2a1010; border-color: #662222; color: var(--red); }
.key.pressed { border-color: var(--green); color: var(--green); background: rgba(68,185,38,0.10); }

/* Wrong-guess shake */
.key.shaking {
  animation: key-shake 0.28s ease-out;
}

@keyframes key-shake {
  0%,100% { transform: translateX(0); }
  18%     { transform: translateX(-5px); }
  36%     { transform: translateX(5px); }
  54%     { transform: translateX(-4px); }
  72%     { transform: translateX(3px); }
  88%     { transform: translateX(-2px); }
}

/* ── Status ──────────────────────────────────────────────────────── */
#status {
  margin: 12px 0 0;
  min-height: 22px;
  font-size: 0.78rem;
  letter-spacing: 0.1em;
  color: var(--text-muted);
  text-align: center;
}

/* ── Challenge banner ────────────────────────────────────────────── */
#challenge-banner {
  display: none;
  background: rgba(68,185,38,0.08);
  border: 1px solid rgba(68,185,38,0.28);
  border-radius: 8px;
  padding: 8px 16px;
  margin-bottom: 12px;
  font-size: 0.73rem;
  letter-spacing: 0.05em;
  color: var(--accent);
  text-align: center;
  max-width: 500px;
  width: 100%;
}

/* ── Result overlay ──────────────────────────────────────────────── */
#result-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.88);
  z-index: 100;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 20px;
}
#result-overlay.show { display: flex; }

.result-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 32px 36px;
  text-align: center;
  max-width: 320px;
  width: 90%;
}

.result-title {
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: 0.15em;
  margin-bottom: 8px;
}
.result-title.win  { color: var(--green); text-shadow: 0 0 22px rgba(68,185,38,0.6); }
.result-title.lose { color: var(--red);   text-shadow: 0 0 22px rgba(210,58,24,0.6); }

.result-word  { font-size: 1rem; letter-spacing: 0.2em; color: var(--text-muted); margin-bottom: 16px; }
.reveal-letter.hit  { color: var(--green); }
.reveal-letter.miss { color: var(--red); }
.result-stats { font-size: 0.74rem; color: var(--text-muted); line-height: 1.9; margin-bottom: 22px; }
.result-actions { display: flex; flex-direction: column; gap: 8px; }

.btn {
  padding: 10px 20px;
  border-radius: 8px;
  font-family: inherit;
  font-size: 0.82rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  cursor: pointer;
  border: none;
  transition: opacity 0.15s, transform 0.08s;
}
.btn:active { transform: scale(0.97); }
.btn-primary   { background: var(--accent); color: #000; }
.btn-secondary { background: var(--surface); color: var(--text); border: 1px solid var(--border); }

/* ── Stats modal ─────────────────────────────────────────────────── */
#stats-modal, #challenge-modal {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.88);
  z-index: 100;
  align-items: center;
  justify-content: center;
}
#stats-modal.show, #challenge-modal.show { display: flex; }

.modal-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 26px 30px;
  max-width: 320px;
  width: 90%;
}
.modal-title {
  font-size: 0.85rem;
  letter-spacing: 0.2em;
  color: var(--text-muted);
  margin-bottom: 18px;
  text-align: center;
}

.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  margin-bottom: 22px;
}
.stat-cell { text-align: center; }
.stat-num   { font-size: 2rem; font-weight: 700; color: var(--accent); line-height: 1; }
.stat-label { font-size: 0.65rem; color: var(--text-muted); letter-spacing: 0.1em; margin-top: 4px; }

/* ── Challenge modal inputs ──────────────────────────────────────── */
.challenge-input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-family: inherit;
  font-size: 1rem;
  letter-spacing: 0.15em;
  padding: 10px 12px;
  text-transform: uppercase;
  margin-bottom: 10px;
  outline: none;
}
.challenge-input:focus { border-color: var(--accent); }

.challenge-hint { font-size: 0.68rem; color: var(--text-muted); margin-bottom: 16px; line-height: 1.5; }

.share-url-box {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 0.62rem;
  color: var(--text-muted);
  word-break: break-all;
  margin-bottom: 10px;
  display: none;
}

/* ── Responsive ──────────────────────────────────────────────────── */
@media (max-width: 400px) {
  .key  { width: 32px; height: 42px; font-size: 0.78rem; }
  .tile { width: 36px; height: 44px; font-size: 1.1rem; }
  .result-box { padding: 22px 18px; }
}
@media (max-width: 340px) {
  .key { width: 28px; height: 38px; font-size: 0.7rem; }
  .key-row { gap: 3px; }
}
