/* ─── TOKENS ──────────────────────────────────────────────────────────────── */
:root {
  --bg:           #1a1a2e;
  --surface:      #16213e;
  --border:       #444466;
  --text:         #e0e0f0;
  --text-muted:   #8888aa;

  --green:        #538d4e;
  --yellow:       #b59f3b;
  --grey:         #3a3a4a;
  --no-color:     #2a2a42;

  --number-bg:    #0f3460;
  --number-color: #a0c4ff;
  --slot-bg:      #252540;
  --result-color: #ffd080;

  /* Desktop sizes (fixed) */
  --eq-tile:   52px;
  --eq-font:   1.5rem;
  --eq-gap:    6px;

  --hist-tile: 68px;
  --hist-font: 2rem;
  --hist-gap:  12px;

  --key-size:  64px;
  --key-font:  1.5rem;
  --key-gap:   10px;

  --radius: 8px;
}

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

/* ─── DESKTOP LAYOUT ─────────────────────────────────────────────────────── */
body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, sans-serif;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow-x: hidden;
}

#app {
  width: 100%;
  max-width: 540px;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100dvh;
}

/* ─── HEADER ─────────────────────────────────────────────────────────────── */
header {
  width: 100%;
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

header h1 {
  font-size: 1.9rem;
  font-weight: 800;
  letter-spacing: 0.14em;
}

.header-right { width: 36px; }

.icon-btn {
  background: none;
  border: 2px solid var(--border);
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  font-size: 1.1rem;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--surface); }

/* ─── FREE PLAY BANNER ───────────────────────────────────────────────────── */
#freeplay-banner {
  background: #1e1040;
  border-bottom: 1px solid #6644aa;
  width: 100%;
  padding: 7px 16px;
  font-size: 0.82rem;
  color: #bb99ff;
  justify-content: center;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  flex-shrink: 0;
}

/* ─── MAIN ───────────────────────────────────────────────────────────────── */
main {
  flex: 1;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 12px 12px;
  gap: 24px;
}

/* ─── EQUATION DISPLAY ───────────────────────────────────────────────────── */
#equation-display {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--eq-gap);
  padding: 12px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  flex-shrink: 0;
}

/* ─── BASE TILE ──────────────────────────────────────────────────────────── */
.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  border-radius: var(--radius);
  user-select: none;
  flex-shrink: 0;
}

.tile--number {
  width: var(--eq-tile);
  height: var(--eq-tile);
  font-size: var(--eq-font);
  background: var(--number-bg);
  color: var(--number-color);
  border: 2px solid #1a5080;
}

.tile--equals {
  width: 28px;
  height: var(--eq-tile);
  font-size: 1.3rem;
  background: transparent;
  color: var(--text-muted);
  border: none;
}

.tile--result {
  min-width: var(--eq-tile);
  height: var(--eq-tile);
  padding: 0 10px;
  font-size: 1.4rem;
  background: var(--number-bg);
  color: var(--result-color);
  border: 2px solid #1a5080;
}

.tile--slot {
  width: var(--eq-tile);
  height: var(--eq-tile);
  font-size: var(--eq-font);
  background: var(--slot-bg);
  color: var(--text);
  border: 2px dashed var(--border);
  transition: border-color 0.15s, box-shadow 0.15s;
}

.tile--slot-filled {
  border-style: solid;
  border-color: #6666aa;
}

.tile--slot-solved {
  background: var(--green);
  border: 2px solid var(--green);
  color: #fff;
}

.tile--slot-active {
  border-style: solid;
  border-color: #9988ee;
  box-shadow: 0 0 0 3px #9988ee33;
  animation: pulse 1.4s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 3px #9988ee33; }
  50%       { box-shadow: 0 0 0 6px #9988ee15; }
}

/* ─── GUESS HISTORY ──────────────────────────────────────────────────────── */
#guess-history {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--hist-gap);
  width: 100%;
}

.guess-row {
  display: flex;
  gap: var(--hist-gap);
  align-items: center;
  justify-content: center;
}

.tile--op-result {
  width: var(--hist-tile);
  height: var(--hist-tile);
  font-size: var(--hist-font);
  border-radius: var(--radius);
  border: 2px solid transparent;
}

.tile--pending {
  background: var(--slot-bg);
  border-color: var(--border);
  color: var(--text);
}

/* Feedback colors */
.tile.green,
.tile--op-result.green  { background: var(--green);  border-color: var(--green);  color: #fff; }
.tile.yellow,
.tile--op-result.yellow { background: var(--yellow); border-color: var(--yellow); color: #fff; }
.tile.grey,
.tile--op-result.grey   { background: var(--grey);   border-color: var(--grey);   color: var(--text-muted); }

.tile--op-result.no-color {
  background: var(--no-color);
  border-color: #444466;
  color: var(--text-muted);
}

/* ─── ANIMATIONS ──────────────────────────────────────────────────────────── */
@keyframes flip {
  0%   { transform: scaleY(1); }
  50%  { transform: scaleY(0); }
  100% { transform: scaleY(1); }
}
.tile--op-result.flip { animation: flip 0.5s ease forwards; }

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20%       { transform: translateX(-7px); }
  40%       { transform: translateX(7px); }
  60%       { transform: translateX(-4px); }
  80%       { transform: translateX(4px); }
}
#equation-display.shake { animation: shake 0.4s ease; }

/* ─── KEYBOARD ────────────────────────────────────────────────────────────── */
#keyboard {
  width: 100%;
  display: flex;
  justify-content: center;
  gap: var(--key-gap);
  padding: 10px 16px 28px;
  flex-wrap: wrap;
  flex-shrink: 0;
}

.key {
  width: var(--key-size);
  height: var(--key-size);
  background: var(--key-bg, #2a2a4a);
  color: var(--text);
  border: none;
  border-radius: var(--radius);
  font-size: var(--key-font);
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  transition: background 0.15s, transform 0.1s;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}
.key:hover  { background: #3a3a60; }
.key:active { transform: scale(0.93); }

.key--action {
  width: auto;
  padding: 0 18px;
  font-size: 1rem;
  background: #2a3a5a;
}
.key--enter { background: #1a4a6a; }
.key--enter:hover { background: #1e5a80; }

.key.green  { background: var(--green);  color: #fff; }
.key.yellow { background: var(--yellow); color: #fff; }
.key.grey   { background: #252530;       color: var(--text-muted); }

/* ─── TOASTS ──────────────────────────────────────────────────────────────── */
#toast-container {
  position: fixed;
  top: 68px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  z-index: 200;
  pointer-events: none;
}

.toast {
  background: var(--text);
  color: var(--bg);
  padding: 10px 22px;
  border-radius: 24px;
  font-weight: 700;
  font-size: 0.95rem;
  opacity: 0;
  transform: translateY(-8px);
  transition: opacity 0.2s, transform 0.2s;
  white-space: nowrap;
}
.toast--visible { opacity: 1; transform: translateY(0); }

/* ─── MODALS ──────────────────────────────────────────────────────────────── */
dialog {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 0;
  max-width: 400px;
  width: 90vw;
  max-height: 90dvh;
  overflow-y: auto;
  box-shadow: 0 8px 40px #00000099;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin: 0;
}
dialog::backdrop {
  background: #00000088;
  backdrop-filter: blur(3px);
}

.modal-content {
  padding: 28px 24px 24px;
  position: relative;
}

.modal-close {
  position: absolute;
  top: 12px;
  right: 14px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  touch-action: manipulation;
}
.modal-close:hover { color: var(--text); background: #ffffff11; }

dialog h2  { font-size: 1.4rem; margin-bottom: 12px; }
dialog p   { font-size: 0.93rem; color: var(--text-muted); line-height: 1.6; margin-bottom: 10px; }
dialog ul  { list-style: none; margin: 8px 0 14px; }
dialog ul li {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.9rem;
  color: var(--text-muted);
  margin-bottom: 7px;
}

.swatch {
  display: inline-block;
  width: 22px;
  height: 22px;
  border-radius: 4px;
  flex-shrink: 0;
}
.swatch.green  { background: var(--green); }
.swatch.yellow { background: var(--yellow); }
.swatch.grey   { background: #4a4a5a; }

.example-equation {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-wrap: wrap;
  margin: 14px 0;
}
.example-equation .tile,
.example-equation .tile--op-result { width: 38px; height: 38px; font-size: 1rem; }
.example-equation .tile--result    { min-width: 38px; height: 38px; font-size: 0.95rem; padding: 0 6px; }
.example-equation .tile--equals    { width: 22px; height: 38px; font-size: 1rem; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin: 16px 0;
}
.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: var(--bg);
  border-radius: 8px;
  padding: 10px 4px;
}
.stat-value { font-size: 1.8rem; font-weight: 800; color: var(--text); }
.stat-label { font-size: 0.68rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; margin-top: 2px; }

.modal-actions { display: flex; gap: 10px; margin-top: 10px; }
.btn {
  flex: 1;
  padding: 12px;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  transition: opacity 0.15s;
}
.btn:hover      { opacity: 0.85; }
.btn--primary   { background: var(--green); color: #fff; }
.btn--secondary { background: #2a3a6a; color: var(--text); }

/* ═══════════════════════════════════════════════════════════════════════════
   MOBILE ONLY — everything below only applies on phones
   ═══════════════════════════════════════════════════════════════════════════ */
@media (max-width: 600px) {

  /* Fluid tile sizes that fit 11 equation elements across the screen */
  :root {
    --eq-tile:   clamp(24px, calc((100vw - 88px) / 11), 52px);
    --eq-font:   clamp(0.75rem, 3.6vw, 1.5rem);
    --eq-gap:    clamp(2px, 0.6vw, 6px);

    --hist-tile: clamp(54px, 15vw, 68px);
    --hist-font: clamp(1.4rem, 4.5vw, 2rem);
    --hist-gap:  clamp(6px, 2vw, 12px);

    --key-size:  clamp(50px, 13vw, 64px);
    --key-font:  clamp(1.1rem, 3.8vw, 1.5rem);
    --key-gap:   clamp(5px, 1.5vw, 10px);
  }

  /* Full-viewport layout: keyboard pinned at bottom */
  html, body { height: 100%; overflow: hidden; }

  #app {
    height: 100dvh;
    min-height: unset;
    max-width: 100%;
  }

  header h1 { font-size: 1.5rem; }

  /* Scrollable game area */
  main {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 14px 8px 10px;
    gap: 16px;
    scrollbar-width: none;
  }
  main::-webkit-scrollbar { display: none; }

  /* Tighter equation container */
  #equation-display {
    padding: 10px 10px;
    gap: var(--eq-gap);
    width: 100%;
  }

  .tile--equals {
    width: clamp(16px, 3.5vw, 28px);
    font-size: clamp(0.85rem, 2.8vw, 1.3rem);
  }

  .tile--result {
    font-size: clamp(0.8rem, 3.2vw, 1.4rem);
    padding: 0 6px;
  }

  /* Keyboard pinned at bottom with border */
  #keyboard {
    border-top: 1px solid var(--border);
    padding: 8px 10px 16px;
    gap: var(--key-gap);
    background: var(--bg);
  }

  .key--action {
    padding: 0 clamp(10px, 2.5vw, 18px);
    font-size: clamp(0.8rem, 2.2vw, 1rem);
  }

  /* iPhone home bar safe area */
  @supports (padding: max(0px)) {
    #keyboard { padding-bottom: max(16px, env(safe-area-inset-bottom)); }
  }
}
