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

[hidden] { display: none !important; }

body {
  font-family: system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
  background: #f0f0f0;
  color: #1a1a1a;
  height: 100dvh;
  overflow: hidden;
}

/* ── Level Select ─────────────────────────────────────────────────────────── */

#level-select {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  background: #1a1a1a;
}

#ls-header {
  padding: 28px 20px 20px;
  text-align: center;
  background: #111;
  flex-shrink: 0;
}

#ls-header h1 {
  font-size: 2.2rem;
  font-weight: 900;
  letter-spacing: 0.12em;
  color: #ff5500;
}

#ls-header p {
  font-size: 0.8rem;
  color: #666;
  margin-top: 4px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

#level-grid {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 12px;
  align-content: start;
}

.level-card {
  background: #252525;
  border: 2px solid #333;
  border-radius: 10px;
  padding: 16px 14px;
  cursor: pointer;
  text-align: left;
  color: #e0e0e0;
  transition: border-color 0.15s, transform 0.1s, background 0.15s;
  touch-action: manipulation;
}

.level-card:hover {
  border-color: #555;
  background: #2e2e2e;
  transform: translateY(-2px);
}

.level-card:active {
  transform: translateY(0);
}

.level-card.beaten {
  border-color: #008833;
}

.level-card.beaten:hover {
  border-color: #00cc55;
}

.level-card.perfect {
  border-color: #2277cc;
}

.level-card.perfect:hover {
  border-color: #44aaff;
}

.card-name {
  font-weight: 700;
  font-size: 0.95rem;
  margin-bottom: 2px;
}

.card-subtitle {
  font-size: 0.75rem;
  color: #777;
  margin-bottom: 10px;
}

.card-target {
  font-size: 0.75rem;
  color: #888;
  margin-bottom: 3px;
}

.card-best {
  font-size: 0.8rem;
  color: #bbb;
}

.card-badge {
  margin-top: 10px;
  font-size: 0.75rem;
  color: #00cc55;
  font-weight: 700;
}

.card-badge-perfect {
  color: #44aaff;
}

/* ── Game Screen ──────────────────────────────────────────────────────────── */

#game {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

/* Header uses CSS grid: left | center | right — status always truly centered */
#header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  background: #222;
  color: #e0e0e0;
  flex-shrink: 0;
  min-height: 56px;
}

#level-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

#level-name {
  font-weight: 700;
  font-size: 0.9rem;
}

#target-score {
  font-size: 0.72rem;
  color: #aaa;
}

#game-status {
  text-align: center;
  font-size: 0.88rem;
  white-space: nowrap;
}

#game-status.status-win {
  color: #44dd88;
  font-weight: 700;
}

#game-status.status-perfect {
  color: #44aaff;
  font-weight: 700;
}

#game-status.status-fail {
  color: #ff7755;
}

#controls {
  display: flex;
  justify-content: flex-end;
  gap: 6px;
}

.btn {
  padding: 0 12px;
  height: 38px;
  min-width: 64px;
  border: none;
  border-radius: 6px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  white-space: nowrap;
}

.btn:not(.btn-secondary) {
  background: #ff5500;
  color: #fff;
}

.btn.btn-secondary {
  background: #3a3a3a;
  color: #ccc;
}

.btn:active {
  opacity: 0.8;
}

/* ── Grid ── */

#grid-container {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
}

#grid-svg {
  width: min(100%, 560px);
  height: auto;
  max-height: 100%;
  display: block;
  border-radius: 2px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.18);
}

/* ── Footer ── */

#score-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 16px;
  background: #222;
  color: #e0e0e0;
  font-size: 0.85rem;
  min-height: 40px;
  flex-shrink: 0;
}

#score-display {
  font-weight: 600;
}

.best-score-btn {
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  font-size: 0.85rem;
  color: #aaa;
  cursor: default;
}

.best-score-btn.clickable {
  cursor: pointer;
  text-decoration: underline dotted;
  text-underline-offset: 3px;
}

.best-score-btn.clickable:hover {
  color: #ddd;
}

/* ── Help Dialog ── */

#help-dialog {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  margin: 0;
  border: none;
  border-radius: 12px;
  background: #2a2a2a;
  color: #e0e0e0;
  padding: 0;
  max-width: 360px;
  width: calc(100% - 40px);
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.6);
}

#help-dialog::backdrop {
  background: rgba(0, 0, 0, 0.65);
}

.dialog-inner {
  padding: 28px 24px 24px;
}

.dialog-inner h2 {
  font-size: 1.1rem;
  font-weight: 700;
  margin-bottom: 16px;
  color: #fff;
}

.dialog-inner ul {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 24px;
  font-size: 0.85rem;
  line-height: 1.5;
  color: #ccc;
}

.dialog-inner ul li::before {
  content: '→ ';
  color: #ff5500;
}

.help-perfect {
  color: #44aaff;
  font-weight: 700;
}

#help-close {
  width: 100%;
}

/* ── Animations ── */

@keyframes tile-pop {
  0%   { transform: scale(0.25); opacity: 0.4; }
  55%  { transform: scale(1.08); opacity: 1; }
  100% { transform: scale(1);    opacity: 1; }
}

.tile-pop {
  transform-box: fill-box;
  transform-origin: center;
  animation: tile-pop 0.18s ease-out forwards;
}

@keyframes ring-pulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.3; }
}

.ignite-ring {
  animation: ring-pulse 1.1s ease-in-out infinite;
}

/* ── Mobile ── */

@media (max-width: 480px) {
  #header {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    padding: 8px 10px 4px;
    gap: 4px;
    min-height: unset;
  }

  #level-info {
    flex: 1;
    order: 1;
  }

  #controls {
    order: 2;
    flex-shrink: 0;
    gap: 4px;
  }

  #game-status {
    order: 3;
    width: 100%;
    text-align: center;
    font-size: 0.75rem;
    padding-bottom: 4px;
    min-height: 18px;
  }

  #level-name { font-size: 0.82rem; }
  #target-score { font-size: 0.68rem; }

  .btn {
    height: 34px;
    min-width: 52px;
    padding: 0 8px;
    font-size: 0.76rem;
  }

  #grid-container { padding: 8px; }

  #score-bar {
    padding: 6px 10px;
    font-size: 0.8rem;
  }
}
