:root{
  --bg:#f4f2ec;
  --card-bg:#ffffff;
  --cell-fill:#f9dfa0;
  --cell-fill-2:#f5d078;
  --grid-line:#3a2c14;
  --cell-text:#241a08;
  --selected-ring:#3f5efb;
  --tile-bg:#bdeecb;
  --tile-bg-2:#8fdca9;
  --tile-text:#0e5f31;
  --tile-shadow:#5fb87f;
  --accent:#3f5efb;
  --success:#2fae66;
  --error:#e2543a;
  --topbar-bg:#ffffff;
  --text:#2b2b2b;
  --muted:#8a8578;
  --input-border:#e3ddd0;
  --shadow:0 6px 16px rgba(40,30,10,0.08);
  --radius:14px;
}

.theme-pastel{
  --bg:#f3eefc;
  --cell-fill:#ffd9ec;
  --cell-fill-2:#ffc2e0;
  --grid-line:#5a2140;
  --cell-text:#3d1730;
  --tile-bg:#cdeaff;
  --tile-bg-2:#a9d9ff;
  --tile-text:#1c4f75;
  --tile-shadow:#7fb8e0;
  --accent:#9b6bff;
  --selected-ring:#9b6bff;
}

.theme-dark{
  --bg:#161821;
  --card-bg:#1f2230;
  --cell-fill:#4a3a1e;
  --cell-fill-2:#5c481f;
  --grid-line:#000000;
  --cell-text:#ffe6ad;
  --tile-bg:#1f5a3d;
  --tile-bg-2:#227048;
  --tile-text:#c9ffdf;
  --tile-shadow:#0e3a26;
  --accent:#8f7bff;
  --selected-ring:#8f7bff;
  --topbar-bg:#1a1c28;
  --text:#eceef5;
  --muted:#9298ad;
  --input-border:#333649;
  --shadow:0 6px 18px rgba(0,0,0,0.4);
}

.theme-contrast{
  --bg:#000000;
  --card-bg:#000000;
  --cell-fill:#ffd400;
  --cell-fill-2:#ffd400;
  --grid-line:#000000;
  --cell-text:#000000;
  --tile-bg:#00ff66;
  --tile-bg-2:#00ff66;
  --tile-text:#000000;
  --tile-shadow:#00b347;
  --accent:#00e5ff;
  --selected-ring:#00e5ff;
  --topbar-bg:#000000;
  --text:#ffffff;
  --muted:#cccccc;
  --input-border:#ffffff;
  --shadow:none;
}

*{box-sizing:border-box;-webkit-tap-highlight-color:transparent;}
html,body{height:100%;}
body{
  margin:0;
  font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif;
  background:var(--bg);
  color:var(--text);
  overscroll-behavior:none;
}
.sr-only{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);}

#app{
  display:flex;
  flex-direction:column;
  min-height:100vh;
  max-width:560px;
  margin:0 auto;
  background:var(--bg);
}

.topbar{
  display:flex;
  align-items:center;
  justify-content:space-between;
  padding:14px 16px;
  background:var(--topbar-bg);
  box-shadow:var(--shadow);
  position:sticky;
  top:0;
  z-index:20;
}
.brand{font-weight:700;font-size:1.15rem;letter-spacing:.2px;}
.topbar-actions{display:flex;gap:8px;}

.icon-btn{
  width:40px;height:40px;
  display:flex;align-items:center;justify-content:center;
  background:transparent;
  border:none;
  border-radius:50%;
  color:var(--accent);
  cursor:pointer;
  transition:background .15s ease, transform .1s ease;
}
.icon-btn svg{width:22px;height:22px;}
.icon-btn:active{transform:scale(.9);background:rgba(0,0,0,0.06);}

.toolbar{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:10px;
  padding:10px 16px;
}
.difficulty-select select{
  appearance:none;
  border:1px solid var(--input-border);
  background:var(--card-bg);
  color:var(--text);
  font-weight:600;
  padding:8px 30px 8px 14px;
  border-radius:999px;
  font-size:.95rem;
  box-shadow:var(--shadow);
  background-image:url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'><path d='M6 9l6 6 6-6' fill='none' stroke='%238a8578' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/></svg>");
  background-repeat:no-repeat;
  background-position:right 10px center;
  background-size:16px;
}

.board-wrap{
  flex:1;
  display:flex;
  align-items:center;
  justify-content:center;
  padding:6px 12px 12px;
  overflow:hidden;
}

.grid{
  display:grid;
  gap:0;
  background:var(--card-bg);
  padding:14px;
  border-radius:18px;
  box-shadow:var(--shadow);
}

.cell{
  display:flex;
  align-items:center;
  justify-content:center;
  font-weight:800;
  font-size:var(--cell-font, 1.15rem);
  user-select:none;
  position:relative;
  width:100%;
  height:100%;
  box-sizing:border-box;
}

.cell.wide{font-size:calc(var(--cell-font, 1.15rem) * 0.72);}

.cell.empty{background:transparent;border:none;}

/* Every cell that's part of an equation — operator, given number, or
   blank-to-fill — shares one uniform crossword-tile look: a filled
   square with its own border, so adjacent tiles read as ruled grid
   lines (like a real crossword), not floating cards. */
.cell.op,
.cell.clue,
.cell.input{
  background:linear-gradient(180deg,var(--cell-fill),var(--cell-fill-2));
  color:var(--cell-text);
  border:1.5px solid var(--grid-line);
  border-radius:3px;
}

.cell.op{font-weight:700;}

.cell.input{
  cursor:pointer;
  transition:transform .12s ease, border-color .15s ease, box-shadow .15s ease;
}
.cell.input:active{transform:scale(.94);}
.cell.input.selected{
  border-color:var(--selected-ring);
  box-shadow:inset 0 0 0 2px var(--selected-ring);
  z-index:2;
}
.cell.input.correct{
  animation:pop .28s ease;
  border-color:var(--success);
  box-shadow:inset 0 0 0 2px var(--success);
  z-index:1;
}
.cell.input.error{
  border-color:var(--error);
  box-shadow:inset 0 0 0 2px var(--error);
  animation:shake .3s ease;
  z-index:1;
}
.cell.input.hinted{
  animation:hintPulse 1s ease;
}

@keyframes pop{0%{transform:scale(1);}40%{transform:scale(1.18);}100%{transform:scale(1);}}
@keyframes shake{0%,100%{transform:translateX(0);}25%{transform:translateX(-4px);}75%{transform:translateX(4px);}}
@keyframes hintPulse{0%{box-shadow:0 0 0 0 rgba(63,94,251,0.5);}70%{box-shadow:0 0 0 10px rgba(63,94,251,0);}100%{box-shadow:0 0 0 0 rgba(63,94,251,0);}}

.toast{
  position:fixed;
  left:50%;
  bottom:120px;
  transform:translateX(-50%) translateY(20px);
  background:var(--text);
  color:var(--bg);
  padding:10px 18px;
  border-radius:999px;
  font-size:.9rem;
  font-weight:600;
  opacity:0;
  pointer-events:none;
  transition:opacity .2s ease, transform .2s ease;
  z-index:50;
  max-width:80vw;
  text-align:center;
}
.toast.show{opacity:1;transform:translateX(-50%) translateY(0);}

.tray{
  padding:12px 16px calc(16px + env(safe-area-inset-bottom));
  background:var(--card-bg);
  box-shadow:0 -6px 16px rgba(40,30,10,0.06);
}

.tiles{
  display:grid;
  grid-template-columns:repeat(8,1fr);
  gap:8px;
  margin-bottom:12px;
}
@media (max-width:380px){
  .tiles{grid-template-columns:repeat(5,1fr);}
}

.tile{
  aspect-ratio:1/1;
  background:linear-gradient(180deg,var(--tile-bg),var(--tile-bg-2));
  color:var(--tile-text);
  border:none;
  border-radius:10px;
  font-weight:800;
  font-size:1.05rem;
  box-shadow:0 3px 0 var(--tile-shadow), var(--shadow);
  cursor:pointer;
  transition:transform .08s ease, box-shadow .08s ease, opacity .15s ease;
}
.tile.wide{font-size:.85rem;}
.tile:active{
  transform:translateY(2px);
  box-shadow:0 1px 0 var(--tile-shadow);
}
.tile.used{
  opacity:.3;
  box-shadow:none;
  cursor:default;
  filter:grayscale(.4);
}
.tile:disabled{pointer-events:none;}

.tray-actions{
  display:flex;
  justify-content:center;
  gap:20px;
}
.round-btn{
  width:56px;height:56px;
  border-radius:50%;
  border:none;
  background:var(--card-bg);
  border:1.5px solid var(--input-border);
  color:var(--accent);
  display:flex;align-items:center;justify-content:center;
  box-shadow:var(--shadow);
  cursor:pointer;
  position:relative;
}
.round-btn svg{width:24px;height:24px;}
.round-btn:active{transform:scale(.92);}
.hint-btn{
  width:64px;height:64px;
  background:var(--accent);
  color:#fff;
  border:none;
}
.badge{
  position:absolute;
  bottom:-6px;
  right:-6px;
  background:var(--text);
  color:var(--bg);
  font-size:.65rem;
  font-weight:800;
  padding:2px 6px;
  border-radius:999px;
}

.win-overlay{
  position:fixed;inset:0;
  background:rgba(0,0,0,.5);
  display:flex;align-items:center;justify-content:center;
  z-index:100;
  padding:20px;
}
.win-overlay.hidden{display:none;}
.win-card{
  background:var(--card-bg);
  color:var(--text);
  border-radius:20px;
  padding:28px 24px;
  text-align:center;
  max-width:320px;
  box-shadow:0 20px 40px rgba(0,0,0,0.3);
}
.win-emoji{font-size:3rem;margin-bottom:6px;}
.win-card h2{margin:0 0 8px;}
.win-card p{color:var(--muted);margin:0 0 20px;}
.win-actions{display:flex;gap:10px;justify-content:center;}
.primary-btn,.ghost-btn{
  padding:12px 20px;
  border-radius:999px;
  font-weight:700;
  border:none;
  cursor:pointer;
  font-size:.95rem;
}
.primary-btn{background:var(--accent);color:#fff;}
.ghost-btn{background:transparent;color:var(--muted);border:1.5px solid var(--input-border);}
