/* ============================================================
   KNIGHT'S PATH — Wordle-Inspired Knight Maze Game
   ============================================================ */

/* --- Reset & Base --- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  /* Core palette (Wordle-inspired dark theme) */
  --bg:            #121213;
  --bg-secondary:  #1a1a1b;
  --border:        #3a3a3c;
  --text:          #d7dadc;
  --text-dim:      #818384;
  --text-bright:   #ffffff;

  /* Cell colors */
  --cell-empty-light: #3a3a3c;
  --cell-empty-dark:  #3a3a3c;
  --cell-wall:        #0d0520;
  --cell-wall-accent: #1e0a3e;
  --cell-void:        #0d0520;
  --cell-void-accent: #1e0a3e;
  --cell-start:       #538d4e;
  --cell-start-dim:   #3a6b37;
  --cell-finish:      #b59f3b;
  --cell-finish-dim:  #8a7a2e;
  --cell-gem:         #e5a100;
  --cell-gem-accent:  #c48800;
  --cell-pawn:        #8b0000;
  --cell-pawn-accent: #a02020;
  --cell-portal:      #0ea5e9;
  --cell-portal-b:    #f97316;
  --cell-crumble:     #78716c;
  --cell-crumble-accent: #a8a29e;
  --cell-threat:      rgba(220, 38, 38, 0.25);

  /* Game colors */
  --knight-color:     #ffffff;
  --knight-glow:      rgba(255, 255, 255, 0.4);
  --valid-move:       rgba(83, 141, 78, 0.55);
  --valid-move-hover: rgba(83, 141, 78, 0.8);
  --visited:          rgba(255, 255, 255, 0.08);
  --hint-color:       rgba(181, 159, 59, 0.6);

  /* Sizing */
  --cell-size:    clamp(36px, 10vw, 60px);
  --cell-gap:     3px;
  --cell-radius:  4px;
  --grid-max:     500px;

  /* Fonts */
  --font: 'Segoe UI', system-ui, -apple-system, sans-serif;
}

html {
  height: 100%;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  overflow: hidden;
}

#app {
  width: 100%;
  max-width: 540px;
  padding: 0 16px 32px;
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100vh;
  height: 100dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* --- Header --- */
#header {
  width: 100%;
  border-bottom: 1px solid var(--border);
  padding: 12px 0;
  margin-bottom: 8px;
}

.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

#header h1 {
  font-size: 1.6rem;
  font-weight: 800;
  letter-spacing: 0.15em;
  color: var(--text-bright);
  text-align: center;
}

.icon-btn {
  width: 36px;
  height: 36px;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: transparent;
  color: var(--text);
  font-size: 1.1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, color 0.2s;
}

.icon-btn:hover {
  background: var(--border);
  color: var(--text-bright);
}

/* --- Level Info --- */
#level-info {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 8px 0 12px;
}

#level-name {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
}

.badge {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  padding: 2px 8px;
  border-radius: 3px;
  text-transform: uppercase;
}

.badge.easy      { background: #538d4e; color: #fff; }
.badge.medium    { background: #b59f3b; color: #1a1a1b; }
.badge.hard      { background: #c9444d; color: #fff; }
.badge.expert    { background: #7b2d8b; color: #fff; }
.badge.master    { background: #b91c1c; color: #fff; }

/* --- Grid --- */
#grid-wrapper {
  width: 100%;
  display: flex;
  justify-content: center;
  margin: 4px 0;
}

#grid-container {
  width: 100%;
  max-width: var(--grid-max);
}

#grid {
  display: grid;
  gap: var(--cell-gap);
  width: 100%;
}

/* --- Cells --- */
.cell {
  aspect-ratio: 1;
  border-radius: var(--cell-radius);
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  cursor: default;
  font-size: 0.7rem;
  font-weight: 600;
  color: transparent;
  transition: transform 0.1s, box-shadow 0.2s;
  overflow: hidden;
}

/* Cell types */
.cell.empty       { background: var(--cell-empty-light); }
.cell.empty.dark  { background: var(--cell-empty-dark); }
.cell.void-cell {
  background: var(--cell-void);
  background: radial-gradient(circle at 50% 50%, var(--cell-void-accent) 0%, var(--cell-void) 80%);
  box-shadow: inset 0 0 10px rgba(138, 43, 226, 0.25);
}
.cell.start {
  background: var(--cell-start);
  box-shadow: inset 0 0 0 2px var(--cell-start-dim);
}
.cell.finish {
  background: var(--cell-finish);
  box-shadow: inset 0 0 0 2px var(--cell-finish-dim);
}
.cell.gem-cell {
  background: var(--cell-empty-light);
  overflow: visible;
}

/* Pawn cell */
.cell.pawn-cell {
  background: var(--cell-pawn);
  box-shadow: inset 0 0 6px rgba(255, 40, 40, 0.3);
}
.cell.pawn-cell::after {
  content: '♟';
  font-size: 1.3em;
  color: rgba(255, 200, 200, 0.85);
  text-shadow: 0 0 6px rgba(255, 60, 60, 0.4);
}

/* Portal A (blue) */
.cell.portal-a {
  background: var(--cell-empty-light);
  border: 2px solid #38bdf8;
  box-shadow: inset 0 0 12px rgba(56, 189, 248, 0.3), 0 0 8px rgba(56, 189, 248, 0.2);
  animation: portal-blink-a 2s ease-in-out infinite;
}
.cell.portal-a::after {
  content: '◈ A';
  font-size: 1em;
  font-weight: 900;
  color: #38bdf8;
  text-shadow: 0 0 8px rgba(56, 189, 248, 0.8);
}

/* Portal B (orange) */
.cell.portal-b {
  background: var(--cell-empty-light);
  border: 2px solid #fb923c;
  box-shadow: inset 0 0 12px rgba(251, 146, 60, 0.3), 0 0 8px rgba(251, 146, 60, 0.2);
  animation: portal-blink-b 2s ease-in-out infinite;
}
.cell.portal-b::after {
  content: '◈ B';
  font-size: 1em;
  font-weight: 900;
  color: #fb923c;
  text-shadow: 0 0 8px rgba(251, 146, 60, 0.8);
}

@keyframes portal-blink-a {
  0%, 100% { box-shadow: inset 0 0 12px rgba(56, 189, 248, 0.3), 0 0 8px rgba(56, 189, 248, 0.2); border-color: #38bdf8; }
  50%      { box-shadow: inset 0 0 20px rgba(56, 189, 248, 0.6), 0 0 16px rgba(56, 189, 248, 0.5); border-color: #7dd3fc; }
}
@keyframes portal-blink-b {
  0%, 100% { box-shadow: inset 0 0 12px rgba(251, 146, 60, 0.3), 0 0 8px rgba(251, 146, 60, 0.2); border-color: #fb923c; }
  50%      { box-shadow: inset 0 0 20px rgba(251, 146, 60, 0.6), 0 0 16px rgba(251, 146, 60, 0.5); border-color: #fdba74; }
}

/* Crumble cell */
.cell.crumble-cell {
  background: var(--cell-crumble);
  border: 1px dashed var(--cell-crumble-accent);
  overflow: visible;
}
.crumble-icon {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  font-size: 18px;
  line-height: 1;
  z-index: 2;
  pointer-events: none;
  opacity: 0.7;
}

/* Pawn threat overlay */
.cell.threatened {
  position: relative;
}
.cell.threatened::before {
  content: '' !important;
  position: absolute !important;
  inset: 0;
  width: auto !important;
  height: auto !important;
  border-radius: var(--cell-radius) !important;
  background: var(--cell-threat) !important;
  background-image: repeating-linear-gradient(
    45deg,
    transparent,
    transparent 3px,
    rgba(220, 38, 38, 0.15) 3px,
    rgba(220, 38, 38, 0.15) 6px
  ) !important;
  border: 1px solid rgba(220, 38, 38, 0.35) !important;
  z-index: 1;
  pointer-events: none;
  animation: none !important;
}

/* Finish icon — King to capture! */
.cell.finish::after {
  content: '♚';
  font-size: 1.4em;
  color: rgba(255, 255, 255, 0.85);
  text-shadow: 0 0 8px rgba(181, 159, 59, 0.5);
}

/* Gem icon — rendered via JS <span>, not ::after */
.cell.gem-cell::after { content: none; }
.gem-icon {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  font-size: 22px;
  line-height: 1;
  z-index: 2;
  pointer-events: none;
  color: #60a5fa;
  text-shadow: 0 0 6px rgba(96, 165, 250, 0.8), 0 0 12px rgba(96, 165, 250, 0.4);
  animation: gem-bob 2s ease-in-out infinite;
}
@keyframes gem-bob {
  0%, 100% { transform: translate(-50%, -50%) scale(1); }
  50%      { transform: translate(-50%, -55%) scale(1.1); text-shadow: 0 0 10px rgba(96, 165, 250, 1), 0 0 18px rgba(96, 165, 250, 0.6); }
}

/* Gem sparkle on pickup */
.gem-sparkle {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 10;
  border-radius: var(--cell-radius);
  animation: gem-sparkle-burst 0.5s ease-out forwards;
}
@keyframes gem-sparkle-burst {
  0%   { box-shadow: inset 0 0 8px 4px rgba(229, 200, 50, 0.9), 0 0 12px rgba(229, 200, 50, 0.7); opacity: 1; }
  100% { box-shadow: inset 0 0 20px 10px rgba(229, 200, 50, 0), 0 0 30px rgba(229, 200, 50, 0); opacity: 0; }
}

/* Wall hedge overlay */
.cell.wall::after {
  content: '🌿';
  position: absolute;
  font-size: 0.75em;
  opacity: 0.45;
  filter: saturate(1.5);
}

/* Void portal overlay */
.cell.void-cell::before {
  content: '✦';
  position: absolute;
  font-size: 0.9em;
  color: rgba(138, 43, 226, 0.35);
  animation: void-spin 4s linear infinite;
}
@keyframes void-spin {
  from { transform: rotate(0deg); }
  to   { transform: rotate(360deg); }
}

/* Thorn spike icon */
.cell.thorn::after {
  content: '⚠';
  font-size: 0.8em;
  color: rgba(255, 80, 80, 0.55);
}

/* --- Knight --- */
.cell .knight-icon {
  font-size: clamp(1.4rem, 5vw, 2.2rem);
  color: var(--knight-color);
  text-shadow: 0 0 12px var(--knight-glow), 0 0 24px rgba(255,255,255,0.15);
  z-index: 5;
  position: relative;
  animation: knight-pop 0.25s ease-out;
  line-height: 1;
}

@keyframes knight-pop {
  0%   { transform: scale(0.2); opacity: 0; }
  60%  { transform: scale(1.15); }
  100% { transform: scale(1); opacity: 1; }
}

/* Current cell highlight */
.cell.current {
  box-shadow: 0 0 0 2px var(--knight-glow), 0 0 16px rgba(255,255,255,0.12);
}

/* --- Valid Move Indicators --- */
.cell.valid-move {
  cursor: pointer;
}

.cell.valid-move::before {
  content: '';
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: var(--valid-move);
  z-index: 3;
  animation: pulse-dot 1.5s ease-in-out infinite;
}

.cell.valid-move:hover {
  transform: scale(1.05);
}

.cell.valid-move:hover::before {
  background: var(--valid-move-hover);
  transform: scale(1.3);
}

/* Occupied valid move (wall/void/thorn shown with ring instead of dot) */
/* Not needed since valid moves are only on landable cells */

@keyframes pulse-dot {
  0%, 100% { opacity: 0.6; transform: scale(1); }
  50%      { opacity: 1;   transform: scale(1.15); }
}

/* --- Hint highlight --- */
.cell.hint-cell::before {
  content: '';
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: var(--hint-color);
  z-index: 3;
  animation: pulse-hint 0.8s ease-in-out infinite;
}

@keyframes pulse-hint {
  0%, 100% { opacity: 0.5; transform: scale(1); }
  50%      { opacity: 1;   transform: scale(1.4); }
}

/* --- Pawn Capture Animation --- */
.capture-fly-piece {
  position: fixed;
  font-size: 1.8em;
  z-index: 9999;
  pointer-events: none;
  transform: translate(-50%, -50%);
  transition: left 0.3s ease-in, top 0.3s ease-in;
  filter: drop-shadow(0 0 8px rgba(255, 255, 255, 0.6));
}

.capture-flash {
  animation: capture-flash-anim 0.5s ease-out !important;
}

@keyframes capture-flash-anim {
  0%   { background: rgba(220, 38, 38, 0.8) !important; transform: scale(1.15); }
  50%  { background: rgba(220, 38, 38, 0.4) !important; }
  100% { background: rgba(220, 38, 38, 0.1) !important; transform: scale(1); }
}

.capture-burst {
  position: fixed;
  font-size: 2.5em;
  z-index: 9999;
  pointer-events: none;
  transform: translate(-50%, -50%) scale(0.5);
  animation: capture-burst-anim 0.8s ease-out forwards;
}

@keyframes capture-burst-anim {
  0%   { transform: translate(-50%, -50%) scale(0.5); opacity: 1; }
  40%  { transform: translate(-50%, -50%) scale(1.8); opacity: 1; }
  100% { transform: translate(-50%, -50%) scale(2.2); opacity: 0; }
}

/* Capture Modal */
.capture-modal {
  text-align: center;
  max-width: 360px;
}

.capture-icon {
  font-size: 3.5em;
  margin-bottom: 4px;
  animation: capture-icon-shake 0.6s ease-out;
}

@keyframes capture-icon-shake {
  0%, 100% { transform: translateX(0); }
  15% { transform: translateX(-8px) rotate(-5deg); }
  30% { transform: translateX(8px) rotate(5deg); }
  45% { transform: translateX(-5px) rotate(-3deg); }
  60% { transform: translateX(5px) rotate(3deg); }
  75% { transform: translateX(-2px); }
}

.capture-title {
  color: #dc2626;
  font-size: 1.5em;
  margin-bottom: 4px;
}

.capture-desc {
  color: var(--text-dim);
  margin-bottom: 16px;
  font-size: 0.95em;
}

.capture-explain {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  padding: 12px 16px;
  margin-bottom: 16px;
  font-size: 0.85em;
  line-height: 1.5;
  color: var(--text);
}

.capture-explain p {
  margin-bottom: 10px;
}

.capture-diagram {
  display: grid;
  grid-template-columns: repeat(3, 36px);
  grid-template-rows: repeat(3, 36px);
  gap: 3px;
  justify-content: center;
  margin-top: 8px;
}

.cd-cell {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  font-size: 0.9em;
  font-weight: 700;
}

.cd-safe {
  background: var(--cell-empty-light);
}

.cd-pawn {
  background: var(--cell-pawn);
  font-size: 1.2em;
}

.cd-threat {
  background: rgba(220, 38, 38, 0.25);
  color: #dc2626;
  border: 1px dashed rgba(220, 38, 38, 0.5);
}



/* --- Status Bar --- */
#status-bar {
  display: flex;
  justify-content: center;
  gap: 24px;
  margin: 14px 0 10px;
  padding: 10px 20px;
  background: var(--bg-secondary);
  border-radius: 8px;
  border: 1px solid var(--border);
  width: 100%;
  max-width: var(--grid-max);
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.stat-label {
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  color: var(--text-dim);
  text-transform: uppercase;
}

.stat-value {
  font-size: 1.2rem;
  font-weight: 800;
  color: var(--text-bright);
}

.stat-value.stars {
  font-size: 1.1rem;
  letter-spacing: 2px;
}

/* --- Controls --- */
#controls {
  display: flex;
  gap: 8px;
  margin: 8px 0;
}

.ctrl-btn {
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-secondary);
  color: var(--text);
  font-family: var(--font);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 5px;
  transition: background 0.2s, color 0.2s, opacity 0.2s;
}

.ctrl-btn:hover:not(:disabled) {
  background: var(--border);
  color: var(--text-bright);
}

.ctrl-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.ctrl-icon {
  font-size: 1rem;
}

/* --- Legend --- */
#legend {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: center;
  margin-top: 8px;
  padding: 8px 0;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.75rem;
  color: var(--text-dim);
}

.legend-swatch {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  display: inline-block;
}

.start-swatch  { background: var(--cell-start); }
.finish-swatch { background: var(--cell-finish); }
.void-swatch   { background: var(--cell-void); border: 1px solid var(--border); }
.gem-swatch    { background: #60a5fa; }
.pawn-swatch   { background: var(--cell-pawn); }
.portal-swatch { background: #38bdf8; }
.crumble-swatch { background: var(--cell-crumble); }
.threat-swatch { background: var(--cell-empty-light); border: 1px solid rgba(220,38,38,0.5); background-image: repeating-linear-gradient(45deg, transparent, transparent 2px, rgba(220,38,38,0.2) 2px, rgba(220,38,38,0.2) 4px); }

/* ============================================================
   MODALS
   ============================================================ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  -webkit-backdrop-filter: blur(4px);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
  animation: fade-in 0.2s ease;
}

@keyframes fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.modal {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px 24px;
  max-width: 420px;
  width: 100%;
  text-align: center;
  animation: modal-pop 0.3s ease-out;
  max-height: 90vh;
  overflow-y: auto;
}

.modal.wide {
  max-width: 480px;
}

@keyframes modal-pop {
  0%   { transform: scale(0.85); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

.modal h2 {
  font-size: 1.3rem;
  font-weight: 800;
  letter-spacing: 0.1em;
  color: var(--text-bright);
  margin-bottom: 16px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.modal-header h2 {
  margin-bottom: 0;
}

.close-btn {
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 1.2rem;
  cursor: pointer;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, color 0.2s;
}

.close-btn:hover {
  background: var(--border);
  color: var(--text-bright);
}

/* --- Level Complete Modal --- */
.big-stars {
  font-size: 2.5rem;
  margin: 12px 0;
  letter-spacing: 8px;
  animation: stars-bounce 0.5s ease-out 0.2s both;
}

@keyframes stars-bounce {
  0%   { transform: scale(0.3); opacity: 0; }
  50%  { transform: scale(1.15); }
  100% { transform: scale(1); opacity: 1; }
}

#complete-stats {
  margin: 12px 0 20px;
  font-size: 0.95rem;
  color: var(--text);
  line-height: 1.8;
}

.modal-buttons {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}

.modal-btn {
  padding: 10px 20px;
  border: none;
  border-radius: 6px;
  font-family: var(--font);
  font-size: 0.9rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}

.modal-btn:active {
  transform: scale(0.96);
}

.modal-btn.primary {
  background: var(--cell-start);
  color: #fff;
}

.modal-btn.primary:hover {
  background: #4a7f46;
}

.modal-btn.secondary {
  background: var(--border);
  color: var(--text);
}

.modal-btn.secondary:hover {
  background: #4a4a4c;
}

.modal-btn.full-width {
  width: 100%;
  margin-top: 16px;
}

.toast {
  margin-top: 10px;
  font-size: 0.8rem;
  color: var(--cell-start);
  font-weight: 600;
  animation: fade-in 0.3s ease;
}

/* --- Level Select --- */
#level-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin-top: 4px;
}

.level-card {
  background: var(--bg);
  border: 2px solid var(--border);
  border-radius: 8px;
  padding: 14px 8px;
  cursor: pointer;
  transition: border-color 0.2s, transform 0.1s, background 0.2s;
  text-align: center;
}

.level-card:hover {
  border-color: var(--text-dim);
  background: var(--bg-secondary);
  transform: translateY(-2px);
}

.level-card.active {
  border-color: var(--cell-start);
}

.level-card.gem-master {
  border-color: #a855f7;
  box-shadow: 0 0 8px rgba(168, 85, 247, 0.4);
}

.level-card .lc-num {
  font-size: 1.4rem;
  font-weight: 800;
  color: var(--text-bright);
}

.level-card .lc-stars {
  font-size: 0.75rem;
  margin: 4px 0 2px;
  letter-spacing: 1px;
}

.level-card .lc-diff {
  font-size: 0.6rem;
  color: var(--text-dim);
  text-transform: uppercase;
  font-weight: 700;
  letter-spacing: 0.05em;
}

/* --- Help Modal --- */
.help-content {
  text-align: left;
  font-size: 0.9rem;
  line-height: 1.6;
  color: var(--text);
}

.help-content p {
  margin: 8px 0;
}

.help-content h3 {
  font-size: 0.85rem;
  color: var(--text-dim);
  letter-spacing: 0.1em;
  margin: 12px 0 6px;
}

.help-content hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 14px 0;
}

.hl.green { color: var(--cell-start); font-weight: 700; }
.hl.gold  { color: var(--cell-finish); font-weight: 700; }

.help-diagram {
  display: flex;
  justify-content: center;
  margin: 12px 0;
}

.mini-grid {
  display: grid;
  grid-template-columns: repeat(5, 36px);
  grid-template-rows: repeat(5, 36px);
  gap: 2px;
}

.mg-cell {
  background: var(--cell-empty-dark);
  border-radius: 3px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
}

.mg-cell.knight {
  background: rgba(255,255,255,0.1);
  color: #fff;
  text-shadow: 0 0 8px rgba(255,255,255,0.4);
}

.mg-cell.dot::after {
  content: '';
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--valid-move);
}

.help-legend {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 4px 0;
  font-size: 0.85rem;
}

.help-legend > div {
  display: flex;
  align-items: center;
  gap: 8px;
}

.help-tip {
  background: rgba(181, 159, 59, 0.1);
  border: 1px solid rgba(181, 159, 59, 0.3);
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 0.85rem;
  margin: 10px 0 !important;
}

/* ============================================================
   CONFETTI / CELEBRATION
   ============================================================ */
.confetti-container {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 99;
  overflow: hidden;
}

.confetti {
  position: absolute;
  width: 8px;
  height: 8px;
  top: -10px;
  animation: confetti-fall linear forwards;
}

@keyframes confetti-fall {
  to {
    top: 110vh;
    transform: rotate(720deg);
  }
}

/* ============================================================
   RESPONSIVE
   ============================================================ */
@media (max-width: 400px) {
  :root {
    --cell-gap: 2px;
    --cell-radius: 3px;
  }

  #header h1 {
    font-size: 1.2rem;
  }

  #status-bar {
    gap: 14px;
    padding: 8px 12px;
  }

  .stat-value {
    font-size: 1rem;
  }

  .ctrl-btn {
    padding: 6px 10px;
    font-size: 0.78rem;
  }

  #level-grid {
    grid-template-columns: repeat(3, 1fr);
  }

  .mini-grid {
    grid-template-columns: repeat(5, 28px);
    grid-template-rows: repeat(5, 28px);
  }
}

@media (min-width: 600px) {
  :root {
    --cell-size: 60px;
  }
}

/* ============================================================
   ITCH.IO / IFRAME MOBILE — Compact Layout
   ============================================================ */

/* Short viewport (landscape mobile or small itch.io frame) */
@media (max-height: 600px) {
  :root {
    --cell-size: clamp(28px, 8vw, 48px);
    --cell-gap: 2px;
  }
  #header { padding: 6px 0; }
  #header h1 { font-size: 1.1rem; }
  #level-info { padding: 2px 0; font-size: 0.75rem; }
  #status-bar { padding: 6px 10px; gap: 12px; margin: 4px 0; }
  .stat-value { font-size: 0.9rem; }
  .stat-label { font-size: 0.55rem; }
  #controls { gap: 6px; padding: 4px 0; }
  .ctrl-btn { padding: 5px 10px; font-size: 0.75rem; }
  #legend { padding: 4px 0; gap: 6px; font-size: 0.65rem; }
  #app { padding: 0 8px 12px; }
}

@media (max-height: 450px) {
  :root {
    --cell-size: clamp(24px, 7vw, 38px);
    --cell-gap: 1px;
  }
  #header h1 { font-size: 0.95rem; letter-spacing: 0.08em; }
  #level-info { font-size: 0.65rem; }
  .icon-btn { width: 28px; height: 28px; font-size: 0.8rem; }
  #legend { display: none; }
  #status-bar { padding: 4px 8px; }
  #controls { padding: 2px 0; }
  .ctrl-btn { padding: 4px 8px; font-size: 0.7rem; }
}

/* ============================================================
   PIECE BADGE, TABS & DAILY CHALLENGE
   ============================================================ */
#piece-badge {
  font-size: 0.8rem;
  font-weight: 700;
  margin-left: 6px;
}

/* --- Piece filter tabs --- */
.piece-tabs {
  display: flex;
  gap: 6px;
  margin: 10px 0;
  justify-content: center;
  flex-wrap: wrap;
}
.piece-tab {
  padding: 5px 12px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: transparent;
  color: var(--text-dim);
  font-family: var(--font);
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, color 0.2s, border-color 0.2s;
}
.piece-tab:hover {
  background: var(--border);
  color: var(--text-bright);
}
.piece-tab.active {
  background: var(--cell-start);
  color: #fff;
  border-color: var(--cell-start);
}

/* --- Level card piece icon + name --- */
.level-card .lc-piece {
  font-size: 1.3rem;
  margin-bottom: 2px;
}
.level-card .lc-name {
  font-size: 0.6rem;
  color: var(--text-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}
.level-card .lc-par {
  font-size: 0.55rem;
  color: var(--text-dim);
  opacity: 0.7;
  white-space: nowrap;
}

/* --- Daily Challenge card --- */
.daily-challenge {
  background: linear-gradient(135deg, rgba(181,159,59,0.15) 0%, rgba(83,141,78,0.1) 100%);
  border: 1px solid rgba(181,159,59,0.4);
  border-radius: 10px;
  padding: 14px 16px;
  margin-bottom: 12px;
  cursor: pointer;
  transition: border-color 0.2s, transform 0.15s;
}
.daily-challenge:hover {
  border-color: var(--cell-finish);
  transform: translateY(-2px);
}
.daily-challenge.done {
  opacity: 0.7;
}
.daily-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}
.daily-badge {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--cell-finish);
}
.daily-date {
  font-size: 0.7rem;
  color: var(--text-dim);
}
.daily-body {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.9rem;
}
.daily-piece {
  font-size: 1.4rem;
}
.daily-name {
  font-weight: 600;
  color: var(--text);
}
.daily-action {
  margin-top: 8px;
  text-align: center;
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--cell-start);
}
.daily-challenge.done .daily-action {
  color: var(--text-dim);
}

/* --- Level grid within select content --- */
#level-select-content .level-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}
@media (max-width: 400px) {
  #level-select-content .level-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

/* ============================================================
   LOADING SCREEN
   ============================================================ */
#loader {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.4s, visibility 0.4s;
}
#loader.loaded {
  opacity: 0;
  visibility: hidden;
}
.loader-content {
  text-align: center;
}
.loader-icon {
  font-size: 4rem;
  animation: loader-bounce 1s ease-in-out infinite;
}
.loader-title {
  margin-top: 12px;
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: 0.15em;
  color: var(--text-bright);
}
.loader-bar {
  margin-top: 20px;
  width: 160px;
  height: 4px;
  background: var(--border);
  border-radius: 2px;
  overflow: hidden;
}
.loader-fill {
  height: 100%;
  width: 30%;
  background: var(--cell-start);
  border-radius: 2px;
  animation: loader-progress 1.2s ease-in-out infinite;
}
@keyframes loader-bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-10px); }
}
@keyframes loader-progress {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(540%); }
}

/* ============================================================
   SETTINGS
   ============================================================ */
.settings-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
}
.settings-row {
  display: block;
  width: 100%;
  padding: 12px 16px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.95rem;
  font-family: var(--font);
  cursor: pointer;
  text-align: left;
  transition: background 0.15s;
}
.settings-row:hover {
  background: var(--border);
}

/* ============================================================
   TOUCH POLISH
   ============================================================ */
#grid {
  touch-action: manipulation;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}
#app {
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
}
