* {
  box-sizing: border-box; -webkit-tap-highlight-color: transparent;
}
button {
  appearance: none; -webkit-appearance: none; -moz-appearance: none;
  font-family: inherit;
}

:root {
  --pad-safe-top: env(safe-area-inset-top, 0px);
  --pad-safe-bottom: env(safe-area-inset-bottom, 0px);
}

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0; background: #101B26; font-family: -apple-system, "Segoe UI", Arial, sans-serif;
  color: #FFFFFF; display: flex; flex-direction: column; align-items: center;
  padding: calc(20px + var(--pad-safe-top)) 12px calc(40px + var(--pad-safe-bottom));
  min-height: 100vh; width: 100%; overflow-x: hidden;
}

h1 {
  font-size: clamp(19px, 5.5vw, 24px);
  font-weight: 800; margin: 4px 0 2px; letter-spacing: 1px; text-align: center;
}
p.sub {
  color: #9FB3C8; margin: 0 0 6px; font-size: clamp(12px, 3.4vw, 13px);
  text-align: center; max-width: 380px; padding: 0 6px;
}
#levelLabel {
  font-size: clamp(12px, 3.2vw, 13px); font-weight: 700; color: #6FA8FF;
  letter-spacing: 0.5px; margin-bottom: 10px; text-transform: uppercase; text-align: center;
}

#wrap {
  position: relative; border: 3px solid #F2A65A; border-radius: 14px; overflow: hidden;
  background: #101B26; touch-action: none; width: 100%; max-width: 520px;
  transition: border-color 0.15s ease;
}
#wrap.flash { border-color: #E5484D; animation: shake 0.4s ease; }
@keyframes shake {
  0%,100% { transform: translateX(0); } 20% { transform: translateX(-8px); }
  40% { transform: translateX(7px); } 60% { transform: translateX(-5px); } 80% { transform: translateX(4px); }
}

canvas { display: block; width: 100%; height: auto; }

#toast {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%);
  background: rgba(229,72,77,0.95); color: #fff; font-weight: 700;
  font-size: clamp(11px, 3.2vw, 13px);
  padding: 7px 14px; border-radius: 8px; opacity: 0; pointer-events: none; transition: opacity 0.2s ease;
  max-width: 92%; text-align: center;
}
#toast.show { opacity: 1; }
#toast.good { background: rgba(46,168,113,0.95); }

#hud {
  display: flex; gap: clamp(8px, 2.6vw, 14px); margin-top: 12px;
  font-size: clamp(11.5px, 3.2vw, 13px); color: #9FB3C8; flex-wrap: wrap; justify-content: center;
  padding: 0 6px;
}
#hud b { color: #F2A65A; }
#hud .opt b { color: #6FA8FF; }

#swipeHint { display: none; font-size: 12px; color: #6C8299; margin-top: 8px; text-align: center; }
@media (pointer: coarse) { #controls { display: none; } #swipeHint { display: block; } }

#controls {
  display: grid;
  grid-template-columns: repeat(3, clamp(42px, 12vw, 48px));
  grid-template-rows: repeat(3, clamp(42px, 12vw, 48px));
  gap: 6px; margin-top: 16px;
}
#controls button {
  background: #1F2F3D; border: 1px solid #35485A; color: #F2A65A;
  font-size: clamp(17px, 4.5vw, 19px); border-radius: 8px; cursor: pointer;
  touch-action: manipulation; transition: transform 0.08s ease, background 0.12s ease;
}
#controls button:active { background: #F2A65A; color: #16232E; transform: scale(0.92); }
#up { grid-column: 2; grid-row: 1; } #left { grid-column: 1; grid-row: 2; }
#down { grid-column: 2; grid-row: 2; } #right { grid-column: 3; grid-row: 2; }

.btnrow {
  display: flex; gap: 10px; margin-top: 16px; flex-wrap: wrap; justify-content: center; padding: 0 6px;
}
.ghostBtn {
  background: transparent; border: 1px solid #F2A65A; color: #F2A65A;
  padding: 8px 16px; border-radius: 8px; font-size: clamp(12px, 3.4vw, 13px); cursor: pointer;
  touch-action: manipulation; transition: transform 0.08s ease, background 0.12s ease;
}
.ghostBtn:hover { background: rgba(242,166,90,0.12); }
.ghostBtn:active { transform: scale(0.95); }
.ghostBtn.active { background: #F2A65A; color: #16232E; }

#board {
  margin-top: 18px; width: 100%; max-width: 380px; background: #1F2F3D; border-radius: 12px;
  padding: 14px 16px; display: none;
}
#board.show { display: block; }
#board h3 { margin: 0 0 10px; font-size: clamp(12.5px, 3.4vw, 14px); color: #F2A65A; letter-spacing: 0.5px; }
#board table { width: 100%; border-collapse: collapse; font-size: clamp(11.5px, 3.2vw, 12.5px); }
#board td { padding: 5px 4px; color: #D6E1EA; }
#board td.rank { color: #F2A65A; font-weight: 700; width: 22px; }
#board td.city { color: #6FA8FF; font-weight: 600; }
#board td.muted { color: #6C8299; font-size: 11px; text-align: right; }
#board tr.empty td { color: #6C8299; text-align: center; padding: 10px 0; }
#personalBest { margin-top: 6px; font-size: 12px; color: #6C8299; }

#siteFooter {
  margin-top: 28px; font-size: 11.5px; color: #4E6376; text-align: center; letter-spacing: 0.3px;
  padding: 0 12px; width: 100%;
}

canvas#c {
  transition: filter 0.4s ease;
}
canvas#c.dim {
  filter: brightness(0.22) saturate(0.6);
}

#startOverlay {
  position: absolute; inset: 0; background: rgba(10,16,22,0.62); display: flex;
  align-items: center; justify-content: center; flex-direction: column; text-align: center;
  padding: 16px; z-index: 5; opacity: 1; transition: opacity 0.4s ease;
  overflow-y: auto; -webkit-overflow-scrolling: touch;
}
#startOverlay.hide { opacity: 0; pointer-events: none; }
#startOverlay h2 {
  font-size: clamp(19px, 5.6vw, 26px); margin: 4px 0 6px; color: #F2A65A;
  text-shadow: 0 2px 10px rgba(0,0,0,0.8);
}
#startOverlay p {
  color: #E7EEF5; margin: 0 0 12px; font-size: clamp(12px, 3.4vw, 14px); max-width: 320px;
  text-shadow: 0 1px 6px rgba(0,0,0,0.8);
}
#startOverlay button#startLevelBtn {
  background: #F2A65A; border: none; color: #16232E; font-weight: 700;
  padding: 11px 26px; border-radius: 8px; font-size: clamp(13px, 3.8vw, 15px);
  cursor: pointer; touch-action: manipulation; transition: transform 0.08s ease;
  box-shadow: 0 4px 18px rgba(0,0,0,0.45); margin-top: 4px; flex-shrink: 0;
}
#startOverlay button#startLevelBtn:active { transform: scale(0.96); }

#instructionsBlock {
  background: rgba(12,20,28,0.92); border: 1px solid rgba(242,166,90,0.4);
  border-radius: 10px; padding: 10px 14px; margin: 0 0 12px; max-width: 340px;
  backdrop-filter: blur(4px); flex-shrink: 0;
}
#instructionsBlock ul { margin: 0; padding-left: 16px; text-align: left; }
#instructionsBlock li {
  color: #E7EEF5; font-size: clamp(11px, 3vw, 12.5px); margin: 4px 0; line-height: 1.35;
}

#colorPicker { margin: 0 0 14px; flex-shrink: 0; }
.pickerLabel {
  color: #E7EEF5; font-size: clamp(11px, 3vw, 12px); font-weight: 700;
  letter-spacing: 0.5px; text-transform: uppercase; margin: 0 0 8px;
  text-shadow: 0 1px 6px rgba(0,0,0,0.8);
}
.swatches { display: flex; gap: 12px; justify-content: center; }
.swatchBtn {
  width: 30px; height: 30px; border-radius: 50%; padding: 0;
  background: transparent; border: 2px solid rgba(255,255,255,0.45);
  cursor: pointer; touch-action: manipulation;
  box-shadow: 0 2px 6px rgba(0,0,0,0.4);
  transition: transform 0.12s ease, border-color 0.12s ease, box-shadow 0.12s ease;
}
.swatchBtn[data-color="blue"]   { background: #3B82F6; }
.swatchBtn[data-color="green"]  { background: #2FBF71; }
.swatchBtn[data-color="red"]    { background: #E5484D; }
.swatchBtn[data-color="yellow"] { background: #F2C94C; }
.swatchBtn:hover { transform: scale(1.08); }
.swatchBtn.active {
  border-color: #FFFFFF;
  box-shadow: 0 0 0 3px rgba(255,255,255,0.3), 0 2px 6px rgba(0,0,0,0.4);
  transform: scale(1.12);
}

#win {
  position: absolute; inset: 0; background: rgba(16,27,38,0.92); display: none;
  align-items: center; justify-content: center; flex-direction: column; text-align: center; padding: 20px;
  z-index: 6;
}
#win.show { display: flex; }
#win h2 { font-size: clamp(21px, 6vw, 26px); margin: 0 0 6px; color: #F2A65A; }
#win p { color: #9FB3C8; margin: 0 0 6px; font-size: clamp(12.5px, 3.6vw, 14px); }
#win .rankNote { color: #FFD9A6; font-size: clamp(11.5px, 3.4vw, 13px); margin-bottom: 14px; }
#win div { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; }
#win button {
  background: #F2A65A; border: none; color: #16232E; font-weight: 700;
  padding: 10px 20px; border-radius: 8px; font-size: clamp(12.5px, 3.6vw, 14px);
  cursor: pointer; margin: 4px; touch-action: manipulation; transition: transform 0.08s ease;
}
#win button:active { transform: scale(0.96); }
#win button.secondary { background: transparent; border: 1px solid #F2A65A; color: #F2A65A; }

.confetti { position: absolute; width: 8px; height: 8px; top: -10px; border-radius: 2px; animation: fall linear forwards; }
@keyframes fall { to { transform: translateY(420px) rotate(540deg); opacity: 0; } }

/* Extra-small screens: tighten spacing further */
@media (max-width: 360px) {
  body { padding-left: 8px; padding-right: 8px; }
  #controls { grid-template-columns: repeat(3, 40px); grid-template-rows: repeat(3, 40px); }
  .btnrow { gap: 8px; }
  .ghostBtn { padding: 7px 12px; }
}

/* Landscape phones: keep the maze from overflowing viewport height */
@media (max-height: 480px) and (orientation: landscape) {
  #wrap { max-width: min(60vh, 520px); }
  body { padding-top: 10px; padding-bottom: 16px; }
  h1 { margin: 0; }
  p.sub { display: none; }
}