:root {
  --bg: #F3EBDC;
  --panel: #EADFCB;
  --text: #3A3128;
  --muted: #8C7C66;
  --accent: #E5484D;
  --btn: #FBF6EA;
  --btn-border: rgba(60, 45, 25, 0.14);
  --shadow: 0 2px 6px rgba(60, 45, 25, 0.14);
  --card: #FBF6EA;
}
:root[data-theme="dark"] {
  --bg: #191C25;
  --panel: #1F232E;
  --text: #ECE6DA;
  --muted: #8C93A6;
  --accent: #FF6B6B;
  --btn: #2A2F3D;
  --btn-border: rgba(255, 255, 255, 0.1);
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
  --card: #262B38;
}

* { box-sizing: border-box; }
html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
}
#app {
  position: fixed;
  inset: 0;
  display: grid;
  touch-action: none;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-rows: minmax(0, 1fr);
  overflow: hidden;
  background: var(--bg);
}
#board {
  display: block;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  background: var(--bg);
  touch-action: none;
  cursor: pointer;
  outline: none;
}
#panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  width: 84px;
  padding: calc(12px + env(safe-area-inset-top)) calc(10px + env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom)) 10px;
  background: var(--panel);
  transition: background 0.25s;
}
.spacer { flex: 0 0 6px; }
.stat { text-align: center; line-height: 1.1; }
.stat .label {
  display: block;
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
}
.stat b { font-size: 24px; font-weight: 700; }
.stat .meta { display: block; font-size: 10px; color: var(--muted); margin-top: 2px; white-space: nowrap; }

#hearts { display: flex; gap: 3px; }
.heart { width: 20px; height: 20px; fill: var(--accent); transition: transform 0.2s, opacity 0.3s, filter 0.3s; }
.heart.lost { opacity: 0.22; filter: grayscale(1); transform: scale(0.85); }
#hearts.shake { animation: shake 0.4s; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  50% { transform: translateX(4px); }
  75% { transform: translateX(-3px); }
}

button, #overlay, #panel {
  touch-action: manipulation;
}
#overlay { z-index: 20; }
#toast, #level-flash { z-index: 15; }
#debug {
  position: fixed;
  left: 6px;
  top: 6px;
  z-index: 30;
  margin: 0;
  padding: 6px 8px;
  font: 11px/1.3 ui-monospace, Menlo, monospace;
  background: rgba(0, 0, 0, 0.65);
  color: #fff;
  border-radius: 8px;
  pointer-events: none;
  white-space: pre;
}
button.icon {
  width: 54px;
  height: 54px;
  border-radius: 17px;
  border: 1px solid var(--btn-border);
  background: var(--btn);
  color: var(--text);
  box-shadow: var(--shadow);
  display: grid;
  place-items: center;
  cursor: pointer;
  padding: 0;
  transition: transform 0.1s, background 0.25s;
}
button.icon svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}
button.icon:active { transform: scale(0.93); }
button.icon:disabled { opacity: 0.35; cursor: default; }
button.icon:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
#btn-theme svg { display: none; }
#btn-theme[data-pref="light"] .sun,
#btn-theme[data-pref="dark"] .moon,
#btn-theme[data-pref="auto"] .auto { display: block; }

#toast {
  position: fixed;
  left: 50%;
  top: calc(14px + env(safe-area-inset-top));
  transform: translateX(-50%) translateY(-6px);
  background: var(--card);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 14px;
  box-shadow: var(--shadow);
  font-weight: 600;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.25s, transform 0.25s;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

#overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(20, 15, 10, 0.35);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
}
.card {
  background: var(--card);
  padding: 26px 30px 22px;
  border-radius: 24px;
  text-align: center;
  min-width: 260px;
  max-width: min(420px, 90vw);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
}
.card h2 { margin: 0 0 8px; font-size: 22px; }
.card p { margin: 0 0 18px; color: var(--muted); line-height: 1.45; font-size: 15px; }
.card .row { display: flex; gap: 10px; justify-content: center; }
.card button {
  border: 0;
  border-radius: 14px;
  padding: 12px 22px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
}
.card .primary { background: var(--accent); color: #fff; }
.card input[type="number"] {
  width: 120px;
  font-size: 22px;
  font-weight: 700;
  text-align: center;
  padding: 8px 10px;
  border-radius: 12px;
  border: 1px solid var(--btn-border);
  background: var(--btn);
  color: var(--text);
  margin-bottom: 12px;
}
.card .chips { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
.card .chip {
  padding: 8px 12px;
  font-size: 14px;
  border-radius: 10px;
  background: var(--btn);
  color: var(--text);
  border: 1px solid var(--btn-border);
}
.stat { cursor: pointer; }
.card .secondary { background: var(--btn); color: var(--text); border: 1px solid var(--btn-border); }

@media (orientation: portrait) {
  #app { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto; }
  #panel {
    flex-direction: row;
    width: auto;
    justify-content: center;
    gap: 8px;
    padding: 10px calc(10px + env(safe-area-inset-right)) calc(10px + env(safe-area-inset-bottom)) calc(10px + env(safe-area-inset-left));
  }
  .spacer { flex: 0 0 4px; }
  .stat { min-width: 64px; }
  button.icon { width: 48px; height: 48px; border-radius: 15px; }
}
@media (max-width: 420px) and (orientation: portrait) {
  #btn-sound, #btn-zoom-in, #btn-zoom-out { display: none; }
}

#level-flash {
  position: fixed;
  left: 50%;
  top: 42%;
  transform: translate(-50%, -50%) scale(0.8);
  font-size: clamp(28px, 5vw, 48px);
  font-weight: 800;
  letter-spacing: 0.02em;
  color: var(--text);
  opacity: 0;
  pointer-events: none;
  text-shadow: 0 2px 12px rgba(0, 0, 0, 0.18);
}
#level-flash.show { animation: levelFlash 1.1s ease-out forwards; }
@keyframes levelFlash {
  0% { opacity: 0; transform: translate(-50%, -50%) scale(0.8); }
  18% { opacity: 1; transform: translate(-50%, -50%) scale(1.04); }
  35% { transform: translate(-50%, -50%) scale(1); }
  75% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -60%) scale(1); }
}

[hidden] { display: none !important; }
