* { box-sizing: border-box; }

:root {
  --stage-scale: 1;
  --bg: #f7f6f2;
  --panel: #ffffff;
  --text: #1a1408;
  --cell-bg: #fbe9b8;
  --cell-text: #1a1408;
  --cell-border: #e8d193;
  --muted-text: #8a8578;
  --accent: #4a6cf7;
  --correct: #3f9142;
  --incorrect: #d64545;
  --tray-bg: #e7f8f0;
  --tray-tile-bg: #f2fdf8;
  --tray-tile-border: #8fd9b6;
  --tray-tile-text: #1f7a52;
  --heart: #ef5b5b;
  --cell-size: 52px;
}

body.dark {
  --bg: #14151a;
  --panel: #1e1f26;
  --text: #e9e9ef;
  --cell-bg: #caa54c;
  --cell-text: #1a1408;
  --cell-border: #a98735;
  --muted-text: #9a9ba6;
  --accent: #7d9bff;
  --tray-bg: #14261e;
  --tray-tile-bg: #182e24;
  --tray-tile-border: #3f8a68;
  --tray-tile-text: #0f4a30;
}

html, body {
  height: 100%;
  width: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}

#viewport {
  position: fixed;
  inset: 0;
  overflow: hidden;
  background: #2b3a67;
}

#rotate-overlay {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: #14151a;
  color: #e9e9ef;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 32px;
}

.rotate-card { max-width: 320px; }

.rotate-icon {
  font-size: 56px;
  margin-bottom: 18px;
  display: inline-block;
  animation: rotate-hint 1.6s ease-in-out infinite;
}

.rotate-text {
  font-size: 16px;
  font-weight: 700;
  line-height: 1.5;
  margin: 0;
}

@keyframes rotate-hint {
  0%, 100% { transform: rotate(0deg); }
  50% { transform: rotate(90deg); }
}

@media (max-width: 900px) and (pointer: coarse) and (orientation: portrait) {
  #rotate-overlay { display: flex; }
  #viewport #stage { display: none; }
}

#stage {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 1280px;
  height: 720px;
  transform-origin: center center;
  overflow: hidden;
  background: url('../assets/bg-game.png') no-repeat center center / cover;
  color: var(--text);
  padding: 14px 18px 18px;
  display: flex;
  flex-direction: column;
  transition: background 0.2s, color 0.2s;
}

#game-screen {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  height: 100%;
}

header { flex: 0 0 auto; width: 100%; }

.top-bar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
}

.top-bar .score-stage {
  justify-self: center;
}

.top-bar .top-bar-group:last-child {
  justify-self: end;
}

.top-bar-group { display: flex; align-items: center; gap: 10px; }

h1 {
  margin: 0;
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.2px;
  color: var(--text);
}
.icon-btn {
  background: transparent;
  border: none;
  font-size: 22px;
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  cursor: pointer;
  color: var(--accent);
  transition: transform 0.1s, opacity 0.15s;
  position: relative;
}

.icon-btn:hover { opacity: 0.85; }
.icon-btn:active { transform: scale(0.9); }

#back-btn,
#new-game-btn,
#settings-btn,
#undo-btn,
#shuffle-btn,
#hint-btn,
#sounds-btn,
#song-btn {
  font-size: 0;
  width: 56px;
  height: 56px;
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
}

#back-btn { background-image: url('../assets/btn-back.png'); }
#new-game-btn { background-image: url('../assets/btn-refresh.png'); }
#settings-btn { background-image: url('../assets/btn-settings.png'); }
#undo-btn { background-image: url('../assets/btn-undo.png'); }
#shuffle-btn { background-image: url('../assets/btn-shuffle.png'); }
#hint-btn { background-image: url('../assets/btn-hint.png'); }

#sounds-btn { background-image: url('../assets/sounds-on.png'); }
#sounds-btn.off { background-image: url('../assets/sounds-off.png'); }
#song-btn { background-image: url('../assets/song-on.png'); }
#song-btn.off { background-image: url('../assets/song-off.png'); }

#hint-count {
  font-size: max(13px, calc(11px / var(--stage-scale)));
  font-weight: 700;
  position: absolute;
  bottom: -2px;
  right: -2px;
  background: #fff;
  color: #333;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 4px rgba(0,0,0,0.3);
}

.status-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 8px;
}

.badge {
  font-size: 14px;
  font-weight: 600;
  color: var(--muted-text);
}

.hearts { display: flex; gap: 4px; font-size: 18px; }
.hearts .heart { color: var(--heart); }

.controls {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 10px;
}

.controls label {
  font-size: max(13px, calc(11px / var(--stage-scale)));
  color: #ffffff;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  text-shadow:
    -1px -1px 0 #2b3a67,
    1px -1px 0 #2b3a67,
    -1px 1px 0 #2b3a67,
    1px 1px 0 #2b3a67,
    0 2px 3px rgba(0,0,0,0.25);
}

button, select {
  padding: 6px 12px;
  font-size: 13px;
  font-weight: 600;
  border-radius: 8px;
  border: 1px solid var(--cell-border);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
}

#check-btn {
  background: url('../assets/btn-check.png') no-repeat center / contain;
  border: none;
  width: 44px;
  height: 44px;
  padding: 0;
  font-size: 0;
}

#difficulty {
  appearance: none;
  -webkit-appearance: none;
  background: linear-gradient(180deg, #ffb066 0%, #f7871f 60%, #ea6f0a 100%);
  border: 3px solid #6ec3f0;
  border-radius: 12px;
  color: #ffffff;
  font-weight: 700;
  font-size: 13px;
  text-shadow: 0 1px 2px rgba(0,0,0,0.35);
  text-align: center;
  text-align-last: center;
  padding: 8px 30px 8px 30px;
  box-shadow:
    inset 0 2px 3px rgba(255,255,255,0.55),
    inset 0 -3px 4px rgba(0,0,0,0.18),
    0 3px 0 rgba(0,0,0,0.15);
  background-image:
    linear-gradient(180deg, #ffb066 0%, #f7871f 60%, #ea6f0a 100%),
    url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20'><path fill='white' d='M5 8l5 5 5-5z'/></svg>");
  background-repeat: no-repeat, no-repeat;
  background-position: 0 0, right 10px center;
  background-size: auto, 14px 14px;
  cursor: pointer;
}

#difficulty:hover {
  filter: brightness(1.05);
}

#difficulty option {
  background: #ffffff;
  color: #1a1408;
  text-shadow: none;
  font-weight: 600;
  text-align: center;
  text-align-last: center;
  direction: ltr;
}

main {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

#grid-wrapper {
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  width: 100%;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 12px;
  margin: 10px 0;
  scrollbar-width: thin;
  scrollbar-color: #f7871f #cdeaf9;
}

#grid-wrapper::-webkit-scrollbar {
  width: 10px;
}

#grid-wrapper::-webkit-scrollbar-track {
  background: #cdeaf9;
  border-radius: 8px;
}

#grid-wrapper::-webkit-scrollbar-thumb {
  background: linear-gradient(180deg, #ffb066 0%, #f7871f 60%, #ea6f0a 100%);
  border-radius: 8px;
  border: 2px solid #6ec3f0;
}

#grid-wrapper::-webkit-scrollbar-thumb:hover {
  filter: brightness(1.08);
}

#grid {
  display: grid;
  gap: 3px;
  background: transparent;
  margin: 10px;
}

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--cell-size) * 0.42);
  font-weight: 800;
  user-select: none;
  font-variant-numeric: tabular-nums;
  color: var(--cell-text);
  background: var(--cell-bg);
}

.cell.len-2 { font-size: max(calc(var(--cell-size) * 0.34), calc(11px / var(--stage-scale))); }
.cell.len-3 { font-size: max(calc(var(--cell-size) * 0.27), calc(12px / var(--stage-scale))); }

.cell.empty-cell { background: transparent; }

.cell.given,
.cell.operator,
.cell.equals,
.cell.blank {
  background: url('../assets/tile-cell.png') no-repeat center / 100% 100%;
}

.cell.blank {
  cursor: pointer;
  transition: filter 0.12s, transform 0.1s;
}

.cell.blank:hover { filter: brightness(0.96); }
.cell.blank.filled { color: var(--cell-text); }

.cell.blank.correct { background: #bfe8c2; }
.cell.blank.incorrect { background: #f3bcbc; }
body.dark .cell.blank.correct { background: #2c5230; }
body.dark .cell.blank.incorrect { background: #5a2b2b; }

body.dark #stage,
body.dark .title-screen {
  background: url('../assets/dark-back.png') no-repeat center center / cover;
}

.cell.blank.drag-hover {
  filter: brightness(1.1);
  transform: scale(1.05);
}

#message {
  flex: 0 0 auto;
  margin: 4px 0;
  font-size: max(13px, calc(12px / var(--stage-scale)));
  font-weight: 700;
  min-height: 16px;
  text-shadow:
    -1px -1px 0 rgba(255,255,255,0.6),
    1px -1px 0 rgba(255,255,255,0.6),
    -1px 1px 0 rgba(255,255,255,0.6),
    1px 1px 0 rgba(255,255,255,0.6);
}

.tray-controls {
  flex: 0 0 auto;
  display: flex;
  gap: 8px;
  margin-bottom: 6px;
}

#tray {
  flex: 0 0 auto;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  max-width: 100%;
  max-height: 13dvh;
  overflow-y: auto;
  padding: 10px;
  background: var(--tray-bg);
  border-radius: 14px;
  min-height: 36px;
}

.tile {
  width: 52px;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: url('../assets/tile-tray.png') no-repeat center / 100% 100%;
  border: none;
  font-size: 20px;
  font-weight: 800;
  cursor: grab;
  color: var(--tray-tile-text);
  touch-action: none;
  font-variant-numeric: tabular-nums;
}

.tile.len-2 { font-size: max(16px, calc(11px / var(--stage-scale))); }
.tile.len-3 { font-size: max(13px, calc(12px / var(--stage-scale))); }

.tile.selected {
  background: url('../assets/tile-selected.png') no-repeat center / 100% 100%;
  color: white;
}

.tile.drag-ghost {
  cursor: grabbing;
  opacity: 0.9;
  box-shadow: 0 4px 12px rgba(0,0,0,0.25);
}

#stats {
  flex: 0 0 auto;
  margin-top: 4px;
  color: #ffffff;
  font-size: max(13px, calc(12px / var(--stage-scale)));
  font-weight: 800;
  text-shadow:
    -1px -1px 0 #2b3a67,
    1px -1px 0 #2b3a67,
    -1px 1px 0 #2b3a67,
    1px 1px 0 #2b3a67,
    0 2px 3px rgba(0,0,0,0.25);
}
/* ---- Stage / Score ---- */
.score-stage { display: flex; gap: 22px; }

.stat-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.1;
}

.stat-label {
  font-size: max(12px, calc(11px / var(--stage-scale)));
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: #ffffff;
  text-shadow:
    -1px -1px 0 #2b3a67,
    1px -1px 0 #2b3a67,
    -1px 1px 0 #2b3a67,
    1px 1px 0 #2b3a67,
    0 2px 3px rgba(0,0,0,0.25);
}

.stat-value {
  font-size: 18px;
  font-weight: 800;
  color: #ffffff;
  text-shadow:
    -1px -1px 0 #2b3a67,
    1px -1px 0 #2b3a67,
    -1px 1px 0 #2b3a67,
    1px 1px 0 #2b3a67,
    0 2px 3px rgba(0,0,0,0.25);
}

/* ---- Difficulty badge / Hearts ---- */
.status-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 8px;
}

.badge {
  font-size: max(14px, calc(11px / var(--stage-scale)));
  font-weight: 800;
  color: #ffffff;
  text-shadow:
    -1px -1px 0 #2b3a67,
    1px -1px 0 #2b3a67,
    -1px 1px 0 #2b3a67,
    1px 1px 0 #2b3a67,
    0 2px 3px rgba(0,0,0,0.25);
}
.hearts { display: flex; gap: 5px; line-height: 1; }
.hearts .heart,
.hearts .heart-empty {
  display: inline-block;
  width: 30px;
  height: 30px;
  font-size: 0;
  background-repeat: no-repeat;
  background-position: center;
  background-size: contain;
}
.hearts .heart { background-image: url('../assets/heart-full.png'); }
.hearts .heart-empty { background-image: url('../assets/heart-empty.png'); opacity: 1; }
/* ---- Large font mode ---- */
body.large-font .cell { font-size: calc(var(--cell-size) * 0.52); }
body.large-font .cell.len-2 { font-size: calc(var(--cell-size) * 0.40); }
body.large-font .cell.len-3 { font-size: calc(var(--cell-size) * 0.31); }
body.large-font .tile { font-size: 24px; }
body.large-font .tile.len-2 { font-size: 19px; }
body.large-font .tile.len-3 { font-size: 15px; }

/* ---- Settings modal ---- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 500;
}
.hidden { display: none !important; }
.modal-overlay.hidden { display: none; }

.modal-panel {
  background: var(--panel);
  color: var(--text);
  border-radius: 18px;
  padding: 24px 22px;
  width: min(320px, 88vw);
  max-height: 85dvh;
  overflow-y: auto;
  box-shadow: 0 12px 40px rgba(0,0,0,0.3);
}
.modal-panel h2 { font-size: 16px; margin: 18px 0 12px; }
.modal-panel h2:first-child { margin-top: 0; }

.option-row { display: flex; gap: 16px; align-items: center; }

.theme-circle {
  width: 48px; height: 48px; border-radius: 50%;
  border: 2px solid var(--cell-border); cursor: pointer; position: relative; padding: 0;
}
.circle-light { background: #ffffff; }
.circle-dark { background: #1b1c22; }

.font-circle, .division-circle {
  width: 48px; height: 48px; border-radius: 50%;
  border: 2px solid var(--cell-border);
  background: var(--panel); color: var(--text);
  font-weight: 700; cursor: pointer; position: relative;
}
.font-circle[data-font="large"] { font-size: 20px; }
.font-circle[data-font="normal"] { font-size: 14px; }
.division-circle { font-size: 16px; }

.theme-circle.active, .font-circle.active, .division-circle.active {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent);
}
.theme-circle.active::after, .font-circle.active::after, .division-circle.active::after {
  content: '✓';
  position: absolute; bottom: -4px; right: -4px;
  background: var(--accent); color: white;
  width: 18px; height: 18px; border-radius: 50%;
  font-size: 11px; display: flex; align-items: center; justify-content: center;
}

.close-btn {
  margin-top: 22px; width: 100%;
  background: var(--accent); color: white; border: none;
  font-weight: 700; padding: 10px; border-radius: 10px;
}

.loading-screen {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--stage-bg, #2b3a67);
  z-index: 100;
}

.loading-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
}

.loading-bar-track {
  width: 220px;
  height: 10px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.15);
  overflow: hidden;
}

.loading-bar-fill {
  width: 0%;
  height: 100%;
  border-radius: 6px;
  background: #ffd166;
  transition: width 0.15s ease-out;
}

.loading-text {
  font-size: 14px;
  font-weight: 600;
  opacity: 0.8;
  margin: 0;
}

.title-screen {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: url('../assets/bg-game.png') no-repeat center center / cover;
  z-index: 100;
}

.title-card {
  text-align: center;
  padding: 2rem;
}

.title-logo {
  font-size: clamp(2rem, 8vw, 3rem);
  margin: 0 0 0.5rem;
  color: #ffffff;
  font-weight: 800;
  text-shadow:
    -1.5px -1.5px 0 #2b3a67,
    1.5px -1.5px 0 #2b3a67,
    -1.5px 1.5px 0 #2b3a67,
    1.5px 1.5px 0 #2b3a67,
    0 3px 4px rgba(0,0,0,0.25);
}

.title-tagline {
  margin: 0 0 2rem;
  opacity: 1;
  color: #ffffff;
  font-weight: 700;
  text-shadow:
    -1px -1px 0 #2b3a67,
    1px -1px 0 #2b3a67,
    -1px 1px 0 #2b3a67,
    1px 1px 0 #2b3a67,
    0 2px 3px rgba(0,0,0,0.25);
}

.play-btn {
  width: 96px;
  height: 96px;
  border: none;
  background: url('../assets/btn-play.png') no-repeat center / contain;
  cursor: pointer;
  font-size: 0;
  box-shadow: none;
  transition: transform 0.15s ease;
}

.play-btn:hover {
  transform: translateY(-2px);
}

.play-btn:active {
  transform: translateY(0);
}

/* ---- Sound & Animation additions ---- */

@keyframes tile-pop-in {
  0% { transform: scale(0.4); opacity: 0; }
  70% { transform: scale(1.12); opacity: 1; }
  100% { transform: scale(1); opacity: 1; }
}

@keyframes cell-place-pop {
  0% { transform: scale(0.7); }
  60% { transform: scale(1.15); }
  100% { transform: scale(1); }
}

@keyframes cell-correct-pulse {
  0% { transform: scale(1); }
  40% { transform: scale(1.12); }
  100% { transform: scale(1); }
}

@keyframes cell-shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

@keyframes heart-lose-shake {
  0%, 100% { transform: scale(1) rotate(0deg); }
  25% { transform: scale(1.3) rotate(-15deg); }
  50% { transform: scale(0.8) rotate(10deg); }
  75% { transform: scale(1.1) rotate(-5deg); }
}

@keyframes message-bounce-in {
  0% { transform: translateY(-8px) scale(0.9); opacity: 0; }
  60% { transform: translateY(2px) scale(1.05); opacity: 1; }
  100% { transform: translateY(0) scale(1); opacity: 1; }
}

@keyframes grid-win-glow {
  0%, 100% { filter: drop-shadow(0 0 0 rgba(255, 215, 0, 0)); }
  50% { filter: drop-shadow(0 0 14px rgba(255, 215, 0, 0.85)); }
}

.tile-enter {
  animation: tile-pop-in 0.28s cubic-bezier(0.34, 1.56, 0.64, 1) backwards;
}

.cell.blank.just-placed {
  animation: cell-place-pop 0.22s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.cell.blank.correct {
  animation: cell-correct-pulse 0.3s ease;
}

.cell.blank.incorrect {
  animation: cell-shake 0.35s ease;
}

.hearts.heart-just-lost {
  animation: heart-lose-shake 0.4s ease;
}

#message.message-pop {
  animation: message-bounce-in 0.3s ease;
}

#grid.win-glow {
  animation: grid-win-glow 0.9s ease 2;
}

.icon-btn, #check-btn, .play-btn, .close-btn {
  transition: transform 0.08s ease, filter 0.12s ease;
}

.icon-btn:active, #check-btn:active, .play-btn:active, .close-btn:active {
  transform: scale(0.9);
}