#game-app, .modal-backdrop {
  --bg-board: #fdf6ea;
  --board-border: #222222;
  --slot-empty-bg: rgba(0, 0, 0, 0.05);
  --text-main: #222222;
  --text-muted: #555555;
  --chute-red: #ad4242;
  --chute-blue: #3a7ede;
  --chute-green: #529c52;
  --chute-yellow: #f7e752;
  --accent-yellow: #f7e55f;
  --accent-aqua: #82dee6;
  --accent-grey: #e0e0e0;
}

#game-app, #game-app *, #game-app *::before, #game-app *::after,
.modal-backdrop, .modal-backdrop *, .modal-backdrop *::before, .modal-backdrop *::after {
  box-sizing: border-box;
}

#game-app {
  width: 100%;
  max-width: 600px;
  margin: 0 auto 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  font-family: "Comic Relief", system-ui, sans-serif;
  color: var(--text-main);
  padding: 0 10px 16px;
}

/* Header & Controls */
.game-header {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  width: 100%;
  padding: 8px 12px;
  margin-bottom: 8px;
  gap: 8px 12px;
}

.title-group {
  display: contents;
}

.title-group h1 {
  flex-basis: 100%;
  margin: 0 0 4px;
  font-size: 2em;
  text-align: center;
  font-weight: 700;
  color: #000;
}

.level-badge {
  font-size: 0.95rem;
  padding: 4px 14px;
  cursor: default;
  font-weight: bold;
  border-radius: 16px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

button.icon-btn, .icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid #000;
}

.hidden {
  display: none !important;
}

.game-instructions {
  font-size: 0.96rem;
  text-align: center;
  margin: 0 0 10px 0;
  color: var(--text-muted);
  line-height: 1.4;
  max-width: 480px;
}

/* Status Bar */
.status-bar {
  display: flex;
  justify-content: space-around;
  width: 100%;
  background: rgba(255, 255, 255, 0.7);
  border: 2px solid #000;
  border-radius: 12px;
  padding: 8px 12px;
  margin-bottom: 12px;
  font-size: 0.92rem;
  font-weight: 600;
}

/* Outer Playfield Container */
.playfield-container {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 16px;
}

/* Board Outer Wrapper with Chutes on all 4 sides */
.board-wrapper {
  position: relative;
  display: grid;
  grid-template-rows: 44px 1fr 44px;
  grid-template-columns: 44px 1fr 44px;
  width: min(88vw, 440px);
  height: min(88vw, 440px);
  background: #f7ede0;
  border: 3px solid #000;
  border-radius: 20px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
  overflow: hidden;
}

/* Chute Perimeter Zones */
.chute-zone {
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  font-size: 0.85rem;
  color: #fff;
  position: relative;
  transition: filter 0.2s;
}

.chute-top,
.chute-bottom,
.chute-left,
.chute-right {
  background: #e8dccb;
}

.chute-top { grid-row: 1; grid-column: 2; border-bottom: 2px solid #000; }
.chute-bottom { grid-row: 3; grid-column: 2; border-top: 2px solid #000; }
.chute-left { grid-row: 2; grid-column: 1; border-right: 2px solid #000; }
.chute-right { grid-row: 2; grid-column: 3; border-left: 2px solid #000; }

/* Coloured opening over the exit cells; the rest of each side is wall */
.chute-mouth {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: left 0.25s, top 0.25s, width 0.25s, height 0.25s;
}

.chute-top .chute-mouth,
.chute-bottom .chute-mouth {
  top: 0;
  bottom: 0;
  left: var(--mouth-start, 0%);
  width: var(--mouth-size, 100%);
  flex-direction: row;
}

.chute-left .chute-mouth,
.chute-right .chute-mouth {
  left: 0;
  right: 0;
  top: var(--mouth-start, 0%);
  height: var(--mouth-size, 100%);
  flex-direction: column;
}

.chute-top .chute-mouth { background: var(--chute-red); }
.chute-bottom .chute-mouth { background: var(--chute-blue); }
.chute-left .chute-mouth { background: var(--chute-green); }
.chute-right .chute-mouth { background: var(--chute-yellow); color: #222; }

/* Corner blocks of the frame */
.chute-corner {
  background: #e8dccb;
  border: 1px solid rgba(0,0,0,0.15);
}
.corner-tl { grid-row: 1; grid-column: 1; border-right: 2px solid #000; border-bottom: 2px solid #000; }
.corner-tr { grid-row: 1; grid-column: 3; border-left: 2px solid #000; border-bottom: 2px solid #000; }
.corner-bl { grid-row: 3; grid-column: 1; border-right: 2px solid #000; border-top: 2px solid #000; }
.corner-br { grid-row: 3; grid-column: 3; border-left: 2px solid #000; border-top: 2px solid #000; }

.chute-arrows {
  font-size: 0.9rem;
  line-height: 1.1;
  text-align: center;
  opacity: 0.85;
  letter-spacing: 2px;
}

.chute-badge {
  background: rgba(0, 0, 0, 0.25);
  color: #fff;
  border-radius: 12px;
  padding: 2px 7px;
  font-size: 0.8rem;
  min-width: 22px;
  text-align: center;
}

.chute-right .chute-badge {
  background: rgba(0, 0, 0, 0.15);
  color: #000;
}

/* The Central Stage / Grid */
.board-stage {
  grid-row: 2;
  grid-column: 2;
  position: relative;
  background: var(--bg-board);
  width: 100%;
  height: 100%;
  overflow: visible;
}

.board-grid {
  position: absolute;
  touch-action: none; /* Swipes move tiles instead of scrolling the page */
  inset: 0;
  width: 100%;
  height: 100%;
}

/* Grid Slots */
.grid-slot {
  position: absolute;
  box-sizing: border-box;
  border: 1px dashed rgba(0, 0, 0, 0.1);
  background: var(--slot-empty-bg);
  pointer-events: none;
}

/* The Tiles */
.chute-tile {
  position: absolute;
  margin: 4px;
  border: 2px solid;
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.4), 0 3px 0 rgba(0,0,0,0.2), 0 5px 8px rgba(0,0,0,0.12);
  transition: transform 0.15s ease, filter 0.15s ease;
  z-index: 10;
  will-change: transform, top, left, opacity;
}

.chute-tile.dragging {
  z-index: 30;
  filter: brightness(1.1);
}

.chute-tile:hover {
  filter: brightness(1.05);
}

.chute-tile:active {
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 0 rgba(0,0,0,0.2);
}

.tile-chevron {
  font-size: 1.5rem;
  font-weight: 900;
  line-height: 1;
  pointer-events: none;
  opacity: 0.9;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.25);
}

/* Directional Blocked Nudge animations */
@keyframes blockedNudgeUp {
  0% { transform: translateY(0); }
  35% { transform: translateY(-7px); }
  70% { transform: translateY(2px); }
  100% { transform: translateY(0); }
}
@keyframes blockedNudgeDown {
  0% { transform: translateY(0); }
  35% { transform: translateY(7px); }
  70% { transform: translateY(-2px); }
  100% { transform: translateY(0); }
}
@keyframes blockedNudgeLeft {
  0% { transform: translateX(0); }
  35% { transform: translateX(-7px); }
  70% { transform: translateX(2px); }
  100% { transform: translateX(0); }
}
@keyframes blockedNudgeRight {
  0% { transform: translateX(0); }
  35% { transform: translateX(7px); }
  70% { transform: translateX(-2px); }
  100% { transform: translateX(0); }
}

.blocked-nudge-up { animation: blockedNudgeUp 0.2s ease-out; }
.blocked-nudge-down { animation: blockedNudgeDown 0.2s ease-out; }
.blocked-nudge-left { animation: blockedNudgeLeft 0.2s ease-out; }
.blocked-nudge-right { animation: blockedNudgeRight 0.2s ease-out; }
.blocked-nudge { animation: blockedNudgeUp 0.2s ease-out; }

/* Hint Pulse animation */
@keyframes hintGlow {
  0%, 100% { transform: scale(1); filter: brightness(1); }
  50% { transform: scale(1.08); filter: brightness(1.3) drop-shadow(0 0 10px #f7e752); }
}

.hint-pulse {
  animation: hintGlow 0.6s ease-in-out 2;
  z-index: 25 !important;
}

/* Evacuating Tile Animation */
.chute-tile.evacuating {
  pointer-events: none;
  z-index: 60 !important;
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.6), 0 6px 16px rgba(0,0,0,0.35);
}

/* Toolbar Controls */
.toolbar {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px 8px 24px;
  margin-top: 4px;
  margin-bottom: 24px;
  flex-wrap: wrap;
}

.toolbar button {
  font-size: 0.95rem;
  padding: 8px 16px;
  border: 2px solid #000;
  border-radius: 16px;
  cursor: pointer;
  font-family: inherit;
  font-weight: bold;
}

/* Button Color Theme */
#game-app .button.grey, .modal-backdrop .button.grey { background: #e0e0e0; color: #000; }
#game-app .button.grey:hover, .modal-backdrop .button.grey:hover { background: #d0d0d0; }
#game-app .button.yellow, .modal-backdrop .button.yellow { background: #f7e55f; color: #000; }
#game-app .button.yellow:hover, .modal-backdrop .button.yellow:hover { background: #ebd849; }
#game-app .button.aqua, .modal-backdrop .button.aqua { background: #82dee6; color: #000; }
#game-app .button.aqua:hover, .modal-backdrop .button.aqua:hover { background: #6ecdd5; }
#game-app .button.green, .modal-backdrop .button.green { background: #7cd97c; color: #000; }
#game-app .button.green:hover, .modal-backdrop .button.green:hover { background: #68c768; }

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
}

.modal-card {
  background: #fffdf8;
  border: 3px solid #000;
  border-radius: 24px;
  padding: 24px;
  max-width: 380px;
  width: 100%;
  text-align: center;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.25);
}

.celebration-icon {
  font-size: 3rem;
  margin-bottom: 8px;
}

.modal-card h2 {
  margin: 0 0 8px 0;
  font-size: 1.6rem;
}

.modal-subtext {
  color: var(--text-muted);
  margin-bottom: 16px;
}

.modal-stats {
  background: #f7ede0;
  border: 2px solid #000;
  border-radius: 12px;
  padding: 12px;
  margin-bottom: 20px;
  font-size: 1.05rem;
  display: flex;
  justify-content: space-around;
}

.modal-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
}

.modal-actions button {
  padding: 10px 22px;
  font-size: 1.05rem;
  border: 2px solid #000;
  border-radius: 18px;
  cursor: pointer;
  font-weight: bold;
}

/* Settings Form */
.settings-card {
  text-align: left;
}

.settings-card h2 {
  text-align: center;
  margin-bottom: 16px;
}

.setting-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
  font-size: 1rem;
}

.setting-row select {
  font-size: 1rem;
  padding: 4px 10px;
  border-radius: 8px;
  border: 2px solid #000;
  font-family: inherit;
}

#save-status {
  font-size: 0.85rem;
  color: var(--text-muted);
  text-align: center;
  margin: 12px 0;
}

.center {
  text-align: center;
}

/* Responsive adjustments */
@media (max-width: 480px) {
  .board-wrapper {
    width: min(94vw, 360px);
    height: min(94vw, 360px);
    grid-template-rows: 38px 1fr 38px;
    grid-template-columns: 38px 1fr 38px;
  }
  .tile-chevron {
    font-size: 1.25rem;
  }
  .toolbar button {
    padding: 6px 12px;
    font-size: 0.88rem;
  }
}
