/* ───────────────────────── Theme & layout ───────────────────────── */
:root {
  --bg:      #0f0f0f;
  --panel:   #1c1c1e;
  --line:    #2c2c2e;
  --text:    #e5e5e7;
  --muted:   #8e8e93;
  --accent:  #6ee7b7;
  --accent-d:#065f46;
  --danger:  #ef4444;
  --warn:    #f59e0b;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, "Segoe UI", "Arial Unicode MS", sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

#header {
  width: 100%; max-width: 520px;
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px;
  padding: 8px 14px 5px;
  flex-shrink: 0;
}
h1 { font-size: 16px; letter-spacing: 0.12em; color: var(--accent); white-space: nowrap; }
#stats { display: flex; gap: 14px; font-size: 12px; color: var(--muted); align-items: center; }
#stats b { font-weight: 700; color: var(--text); }
#level-badge {
  font-size: 11px; color: var(--accent);
  background: #0d2d22; border: 1px solid var(--accent);
  border-radius: 10px; padding: 2px 8px;
}
#mute-btn {
  background: none; border: 1px solid var(--line); color: var(--muted);
  border-radius: 8px; width: 30px; height: 26px; cursor: pointer;
  font-size: 14px; flex-shrink: 0; -webkit-tap-highlight-color: transparent;
}
#mute-btn:active { background: var(--panel); }

/* progress meter (words under the red line / 10) */
#pile-meter-wrap { width: 100%; max-width: 520px; height: 6px; background: var(--panel); flex-shrink: 0; }
#pile-meter { height: 100%; width: 0%; background: var(--accent); transition: width .3s, background .3s; }

#under-row {
  width: 100%; max-width: 520px;
  display: flex; justify-content: space-between;
  padding: 2px 14px 3px; font-size: 11px; color: var(--muted); flex-shrink: 0;
}
#under-row .cnt { font-weight: 700; color: var(--text); }
#under-row.warn { background: rgba(239,68,68,.08); }
#under-row.warn .cnt { color: var(--danger); }

/* ───────────────────────── Game area ───────────────────────── */
#game-area {
  position: relative;
  width: 100%; max-width: 520px;
  flex: 1; min-height: 0;
  overflow: hidden;
  border-left: 1px solid var(--line);
  border-right: 1px solid var(--line);
  touch-action: none;
}

/* red line + label */
#red-line {
  position: absolute; left: 0; right: 0; height: 3px;
  background: rgba(239,68,68,.75); pointer-events: none; z-index: 5;
  transition: top 1.2s linear;
}
#red-line-label {
  position: absolute; left: 0; right: 0; text-align: center;
  font-size: 10px; color: rgba(239,68,68,.9); pointer-events: none; z-index: 6;
  transition: top 1.2s linear; white-space: nowrap; padding-bottom: 3px; line-height: 1.35;
}
#red-line.flashing { animation: line-flash .5s ease 3; }
#red-line.pushed   { animation: line-push .4s ease; }
@keyframes line-flash { 0%,100%{ background:rgba(239,68,68,.75);} 50%{ background:rgba(239,68,68,1); box-shadow:0 0 12px var(--danger);} }
@keyframes line-push  { 0%,100%{ background:rgba(239,68,68,.75);} 50%{ background:rgba(110,231,183,.9); box-shadow:0 0 12px var(--accent);} }

.rise-toast {
  position: absolute; left: 50%; transform: translateX(-50%);
  background: rgba(239,68,68,.9); color: #fff; font-size: 12px; font-weight: 700;
  border-radius: 20px; padding: 5px 14px; z-index: 10; pointer-events: none;
  animation: toast-up 1.8s ease forwards; white-space: nowrap;
}
@keyframes toast-up { 0%{opacity:1; top:60%;} 80%{opacity:1; top:40%;} 100%{opacity:0; top:30%;} }

/* floating score popup on a clear */
.pop {
  position: absolute; z-index: 9; pointer-events: none; font-weight: 800; font-size: 14px;
  color: var(--accent); text-shadow: 0 0 8px rgba(110,231,183,.5);
  animation: pop-up .9s ease forwards;
}
@keyframes pop-up { 0%{opacity:1; transform:translateY(0) scale(1);} 100%{opacity:0; transform:translateY(-34px) scale(1.3);} }

/* combo indicator */
#combo {
  position: absolute; top: 8px; right: 10px; z-index: 8;
  font-size: 13px; font-weight: 800; color: var(--warn);
  opacity: 0; transition: opacity .2s; pointer-events: none;
  text-shadow: 0 0 10px rgba(245,158,11,.6);
}
#combo.show { opacity: 1; }

/* ───────────────────────── Tiles ───────────────────────── */
.tile {
  position: absolute; background: var(--panel); border: 1px solid #3c3c3e;
  border-radius: 8px; padding: 5px 11px 6px; white-space: nowrap;
  line-height: 1.3; z-index: 2; will-change: top, left;
  transition: border-color .1s, background .1s, box-shadow .1s;
}
.tile .w-en { display: block; font-size: 13px; font-weight: 700; color: var(--text); letter-spacing: .04em; }
.tile .w-ar { display: block; font-size: 11px; font-weight: 500; color: #a1a1aa; direction: rtl; text-align: right; }
.tile .w-de { display: block; font-size: 11px; font-weight: 400; color: #71717a; letter-spacing: .02em; }
.tile.active { border-color: var(--accent); background: #0d2d22; box-shadow: 0 0 10px rgba(110,231,183,.25); }
.tile.active .w-en { color: #a7f3d0; }
.tile.danger { border-color: rgba(239,68,68,.7); }
.tile.danger .w-en { color: #fca5a5; }
.tile.danger .w-ar, .tile.danger .w-de { color: #f87171; }
/* locked-on: glows orange and shivers while the fireball homes in */
.tile.doomed {
  background: #3a1d05; border-color: #fbbf24; z-index: 4;
  box-shadow: 0 0 16px rgba(251,191,36,.6); animation: doom-shiver .09s linear infinite;
}
.tile.doomed .w-en { color: #fde047; }
.tile.doomed .w-ar, .tile.doomed .w-de { color: #fca5a5; }
@keyframes doom-shiver {
  0%,100% { transform: translate(0,0); } 25% { transform: translate(-1.5px,1px); }
  75% { transform: translate(1.5px,-1px); }
}
/* impact: blow up and vanish */
.tile.exploding { animation: tile-explode .26s ease-out forwards; pointer-events: none; }
@keyframes tile-explode {
  0%   { transform: scale(1);   opacity: 1; filter: brightness(1); }
  30%  { transform: scale(1.35); opacity: 1; filter: brightness(2.4) saturate(1.6); }
  100% { transform: scale(1.9);  opacity: 0; filter: brightness(3); }
}

/* ── Fireball + explosion (live on <body>, viewport-fixed) ── */
.fireball {
  position: fixed; width: 22px; height: 22px; margin: -11px 0 0 -11px;
  border-radius: 50%; z-index: 300; pointer-events: none;
  background: radial-gradient(circle at 35% 35%, #fff 0%, #fde047 30%, #f97316 65%, #b91c1c 100%);
  box-shadow: 0 0 16px 5px rgba(249,115,22,.85), 0 0 34px 12px rgba(249,115,22,.4);
  transition: transform cubic-bezier(.45,0,.75,.6);   /* duration set inline */
  animation: fb-flicker .1s ease-in-out infinite alternate;
}
@keyframes fb-flicker { from { box-shadow: 0 0 16px 5px rgba(249,115,22,.85), 0 0 34px 12px rgba(249,115,22,.4); }
                        to   { box-shadow: 0 0 22px 7px rgba(253,224,71,.95), 0 0 44px 16px rgba(249,115,22,.5); } }

.boom {
  position: fixed; width: 22px; height: 22px; margin: -11px 0 0 -11px;
  border-radius: 50%; z-index: 301; pointer-events: none;
  background: radial-gradient(circle, #fff 0%, #fde047 28%, #f97316 52%, rgba(185,28,28,0) 72%);
  animation: boom-ring .46s ease-out forwards;
}
@keyframes boom-ring { 0% { transform: scale(.4); opacity: 1; } 100% { transform: scale(9); opacity: 0; } }

.spark {
  position: fixed; width: 6px; height: 6px; margin: -3px 0 0 -3px; border-radius: 50%;
  background: #fde047; box-shadow: 0 0 8px 1px #f97316; z-index: 301; pointer-events: none;
  animation: spark-fly .5s ease-out forwards;
}
@keyframes spark-fly {
  0%   { transform: translate(0,0) scale(1); opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) scale(.3); opacity: 0; }
}
.tile.piled { background: #16161a; border-color: #2a2a2e; opacity: .72; z-index: 1;
  transition: top .25s cubic-bezier(.34,1.3,.64,1); }   /* drops down on collapse */
.tile.piled .w-en { color: #8a8a93; }
.tile.piled .w-ar, .tile.piled .w-de { color: #5a5a61; }
/* a buried word you're spelling lights up so you can see your dig target */
.tile.piled.active { opacity: 1; background: #0d2d22; border-color: var(--accent); z-index: 3; }

/* ───────────────────────── Input row ───────────────────────── */
#input-row {
  width: 100%; max-width: 520px; padding: 7px 10px;
  display: flex; gap: 7px; align-items: center;
  background: #111; border-top: 1px solid var(--line); flex-shrink: 0;
}
/* visible "what you typed" display — single source of truth is the JS buffer.
   Big, bold, glowing so it's unmistakable on a phone above the soft keyboard. */
#typed-box {
  flex: 1; min-height: 46px; background: var(--panel); border: 2px solid #3c3c3e;
  border-radius: 10px; padding: 8px 14px;
  font-size: 22px; font-weight: 800; letter-spacing: .06em;
  color: var(--text); caret-color: var(--accent);
  display: flex; align-items: center; overflow: hidden; cursor: text;
  transition: border-color .12s, box-shadow .12s, background .12s;
}
#typed-box.focused { border-color: var(--accent); }
#typed-box .placeholder { font-size: 13px; font-weight: 500; letter-spacing: 0; color: var(--muted); }

/* typed letters (Latin per-char, Arabic as one shaped word) share the glow */
#typed-box .ch, #typed-box .word {
  display: inline-block;
  background: linear-gradient(180deg,#fff,#a7f3d0);
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent;
  text-shadow: 0 0 12px rgba(110,231,183,.35);
}
#typed-box .ch:last-of-type { animation: ch-pop .18s ease; }
@keyframes ch-pop { 0% { transform: translateY(-4px) scale(1.5); } 100% { transform: none; } }

#typed-box .caret { width: 3px; height: 24px; background: var(--accent); margin-left: 3px;
  border-radius: 2px; box-shadow: 0 0 8px var(--accent); animation: blink 1s step-end infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* whole-box keystroke pop */
#typed-box.pulse { animation: typed-pop .14s ease; }
@keyframes typed-pop { 0% { transform: scale(1); } 45% { transform: scale(1.035); } 100% { transform: scale(1); } }

/* live feedback: green = spelling a real on-screen word, red = matches nothing */
#typed-box.on-track  { border-color: var(--accent); box-shadow: 0 0 18px rgba(110,231,183,.4); background: #0c241c; }
#typed-box.off-track { border-color: rgba(239,68,68,.7); box-shadow: 0 0 14px rgba(239,68,68,.25); }
#typed-box.off-track .ch, #typed-box.off-track .word {
  background: linear-gradient(180deg,#fff,#fca5a5); -webkit-background-clip: text; background-clip: text;
  text-shadow: 0 0 12px rgba(239,68,68,.35);
}

/* success / clear flash — bright colorful burst across the whole box */
#typed-box.burst { animation: typed-burst .42s ease; }
@keyframes typed-burst {
  0%   { background: var(--accent); box-shadow: 0 0 30px var(--accent); transform: scale(1.06); border-color:#fff; }
  100% { background: var(--panel); box-shadow: none; transform: scale(1); }
}
/* hidden input only used to summon the soft keyboard for touch users */
#soft-input {
  position: absolute; opacity: 0; pointer-events: none; width: 1px; height: 1px;
  left: -9999px; top: 0;
}
#submit-btn {
  background: var(--accent-d); border: 1px solid var(--accent); border-radius: 8px;
  color: var(--accent); font-size: 13px; font-weight: 700; padding: 9px 14px;
  cursor: pointer; -webkit-tap-highlight-color: transparent; flex-shrink: 0;
}
#submit-btn:active { background: #047857; }

/* ───────────────────────── Overlays ───────────────────────── */
.overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.88);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 13px; z-index: 100; padding: 24px; text-align: center;
}
.overlay h2 { font-size: 24px; color: var(--accent); letter-spacing: .08em; }
.overlay .sub { font-size: 13px; color: var(--muted); max-width: 320px; }
.overlay .reason { font-size: 13px; color: #fca5a5; max-width: 320px; }
.overlay .big-score { font-size: 50px; font-weight: 800; color: var(--text); line-height: 1; }
.lang-hint {
  font-size: 12px; color: #52525b; background: #18181b; border: 1px solid #27272a;
  border-radius: 8px; padding: 8px 14px; max-width: 280px; line-height: 1.7;
}
.rule-box {
  font-size: 12px; background: #1a0808; border: 1px solid rgba(239,68,68,.35);
  border-radius: 8px; padding: 10px 16px; max-width: 320px; line-height: 1.85; color: #fca5a5;
}
.rule-box strong { color: #f87171; }
.play-btn {
  margin-top: 4px; background: var(--accent-d); border: 1px solid var(--accent);
  border-radius: 10px; color: var(--accent); font-size: 15px; font-weight: 700;
  padding: 11px 34px; cursor: pointer; -webkit-tap-highlight-color: transparent;
}
.play-btn:active { background: #047857; }
#pause-overlay { background: rgba(0,0,0,.75); z-index: 200; }
#pause-overlay h2 { color: var(--text); font-size: 28px; letter-spacing: .12em; }

/* ───────────────────── Mobile-only minimal chrome ─────────────────────
   In-game exit (✕) + pause (−) floating in the play field's top corner.
   Hidden on desktop (only shown when JS adds body.mobile). */
#mobile-chrome { display: none; }
body.mobile #mobile-chrome {
  position: absolute; top: 6px; right: 6px; z-index: 12;
  display: flex; gap: 6px;
}
#mobile-chrome button {
  width: 34px; height: 34px; border-radius: 8px;
  background: rgba(0,0,0,.55); border: 1px solid var(--line);
  color: var(--text); font-size: 18px; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; -webkit-tap-highlight-color: transparent; backdrop-filter: blur(4px);
}
#mobile-chrome button:active { background: rgba(0,0,0,.8); }

/* ─────────────────────────── Mobile layout ───────────────────────────
   Phones have no hardware keyboard and the soft keyboard eats the bottom of
   the screen. We strip the chrome down to: play field + typed line + the X/−
   buttons + the OS keyboard. The hub's play page resizes THIS IFRAME to the
   space above the keyboard (an iframe can't see the keyboard itself), so a
   plain full-height flex column already fits: play field (flex:1) + typed line
   sit entirely in the visible area, and the play field shrinks rather than
   hiding behind the keyboard. */
body.mobile { height: 100%; }
/* Hide everything that isn't the play field, the typed line or the overlays. */
body.mobile #header,
body.mobile #pile-meter-wrap,
body.mobile #under-row,
body.mobile #submit-btn { display: none; }

body.mobile #game-area { max-width: none; border-left: none; border-right: none; }

/* Start / game-over overlays can be taller than a short phone — let them scroll. */
body.mobile .overlay { overflow-y: auto; padding: 20px 18px; gap: 10px; }

/* The typed line must stay visible right above the keyboard. */
body.mobile #input-row { max-width: none; padding: 6px 8px; position: relative; }
body.mobile #typed-box { min-height: 40px; font-size: 20px; padding: 6px 12px; }

/* THE KEY to mobile typing: a REAL, visible, tappable <input> laid directly
   over the typed-box. Tapping it brings up the OS keyboard natively — no
   programmatic focus() / user-gesture tricks needed (those are what failed).
   Its own text/caret are transparent so the styled #typed-box shows through as
   the "what you typed" display; the input's value still feeds the same buffer.
   font-size ≥16px stops iOS from zoom-jumping on focus. */
body.mobile #soft-input {
  position: absolute;
  left: 8px; right: 8px; top: 6px; bottom: 6px;   /* cover the typed-box exactly */
  width: auto; height: auto;
  margin: 0; padding: 6px 12px;
  opacity: 1; z-index: 3; pointer-events: auto;
  background: transparent; border: none; outline: none;
  color: transparent; -webkit-text-fill-color: transparent;
  caret-color: transparent;        /* #typed-box draws its own blinking caret */
  font-size: 16px;
}
