#game-app, .modal-backdrop {
  --bg-board: #fdf6ea;
  --bg-tray: #fffaf0;
  --board-border: #000000;
  --slot-empty-bg: rgba(0, 0, 0, 0.045);
  --text-main: #222222;
  --text-muted: #555555;
  --accent-blue: #3a7ede;
  --accent-yellow: #f7e55f;
  --match-green: #589a56;
  --coral: #ec8d6f;
  --purple: #dcb0ed;
  --grey-border: #bec6ce;
  --blocked-bg: #ece8df;
  --blocked-border: #d4d0c7;
}

#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%;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  font-family: "Comic Relief", "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji", system-ui, sans-serif;
  color: var(--text-main);
}

/* Header Bar - Centred title with level badge and actions as nav row beneath */
.game-header {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  width: 100%;
  padding: 4px 12px;
  margin-bottom: 6px;
  gap: 8px 12px;
}

.title-group {
  display: contents;
}

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

.level-badge {
  font-size: 0.95rem;
  padding: 4px 14px;
  cursor: default;
}

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

.hidden {
  display: none !important;
}

.theme-toggle-btn {
  font-size: 1.25rem;
  padding: 6px 14px !important;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border: 2px solid #000;
}

button.icon-btn, .icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  min-width: 44px;
  min-height: 44px;
  padding: 0 !important;
  border: 2px solid #000;
  border-radius: 50% !important;
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 0.1s ease, background-color 0.1s;
}

button.icon-btn svg, .icon-btn svg {
  display: block;
  flex-shrink: 0;
  pointer-events: none;
}

.icon-btn:hover, .theme-toggle-btn:hover {
  transform: translateY(-1px);
}

.icon-btn:active, .theme-toggle-btn:active {
  transform: translateY(1px);
}

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

/* Status Bar */
.status-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  max-width: 580px;
  padding: 0 12px 6px;
  font-size: 0.95rem;
  color: var(--text-muted);
}

.status-bar strong {
  color: #000;
}

/* Playfield & Board Frame */
.playfield-container {
  position: relative;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 10px;
}

.board-frame {
  position: relative;
  padding: 14px;
  background: var(--bg-board);
  border: 2px solid var(--board-border);
  border-radius: 24px;
  display: flex;
  justify-content: center;
  align-items: center;
  width: min(580px, calc(100vw - 32px));
  max-width: 100%;
  min-height: 290px;
  overflow: visible;
}

.pile-stage {
  position: relative;
  width: 100%;
  height: 260px;
  margin: 0 auto;
}

/* Tiles: the box is the hit area; the SVG draws the face and slab. */
.pile-tile {
  position: absolute;
  font-size: var(--pile-font-size, 1.85rem);
  line-height: 1;
  user-select: none;
}

.pile-tile.free {
  cursor: pointer;
}

.pile-tile.blocked {
  cursor: not-allowed;
}

.tile-body {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
  pointer-events: none;
}

/* Only tiles resting on the board itself cast a shadow. */
.pile-tile.base .tile-body {
  filter: drop-shadow(1px 2px 2px rgba(66, 57, 39, 0.16));
}

.tile-body path, .tile-body rect {
  stroke: var(--grey-border, #bec6ce);
  stroke-width: 2px;
  stroke-linejoin: round;
  vector-effect: non-scaling-stroke;
  transition: stroke 0.15s ease;
}

.tile-slab { fill: url(#tile-side-free); }
.tile-edge { fill: none; opacity: 0.6; }
.tile-top { fill: #ffffff; }

/* Covered tiles keep their colours; only the face is a touch darker. */
.pile-tile.blocked .tile-top { fill: #e8ecef; }

.pile-tile.free:hover .tile-body path, .pile-tile.free:hover .tile-body rect {
  stroke: var(--accent-blue);
}
.pile-tile.free:hover .tile-body {
  filter: drop-shadow(0 0 5px rgba(58, 126, 222, 0.35));
}
.pile-tile.base.free:hover .tile-body {
  filter: drop-shadow(1px 2px 2px rgba(66, 57, 39, 0.16)) drop-shadow(0 0 5px rgba(58, 126, 222, 0.35));
}

/* The symbol sits centred on the square face, not the whole box. */
.tile-symbol {
  position: absolute;
  left: 0;
  top: 0;
  width: calc(100% / (1 + 0.16));
  height: calc(100% / (1 + 0.16));
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.pile-tile.shake {
  animation: tile-shake 0.28s ease;
}

@keyframes tile-shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-3px); }
  75% { transform: translateX(3px); }
}

/* Holding Tray (7 Slots: tight, same 50px size & 6px gap as above) */
.tray-container {
  width: 100%;
  display: flex;
  justify-content: center;
  margin-bottom: 10px;
  padding: 0 8px;
}

.tray-frame {
  width: fit-content;
  background: var(--bg-tray);
  border: 2px solid var(--board-border);
  border-radius: 18px;
  padding: 8px 10px;
}

.tile-tray {
  display: flex;
  gap: 6px;
}

.tray-slot {
  position: relative;
  width: 50px;
  height: 50px;
  aspect-ratio: 1 / 1;
  background: var(--slot-empty-bg);
  border: 2px dotted var(--grey-border);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.85rem;
  line-height: 1;
  transition: border-color 0.15s ease, background-color 0.15s ease;
}

.tray-slot.has-tile {
  background: none;
  border: none;
  font-size: 1.6rem;
}

.pile-tile:focus-visible {
  outline: 2px solid var(--accent-blue);
  outline-offset: 3px;
}

.tray-slot.matching .tile-body path, .tray-slot.matching .tile-body rect {
  stroke: var(--match-green);
}

.tray-slot.matching {
  animation: match-glow 0.32s ease infinite alternate;
}

@keyframes match-glow {
  from { transform: scale(1); }
  to { transform: scale(1.05); }
}

.tray-slot.poofing {
  animation: poof-vanish 0.35s forwards cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes poof-vanish {
  0% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.2) rotate(4deg); opacity: 0.8; }
  100% { transform: scale(0) rotate(-8deg); opacity: 0; }
}

/* Flying tile clone */
.flying-tile {
  position: fixed;
  z-index: 9999;
  width: 50px;
  height: 50px;
  font-size: 1.85rem;
  line-height: 1;
  pointer-events: none;
  transform-origin: 0 0;
}

/* Confetti / Puff Particles */
.puff-particle {
  position: fixed;
  z-index: 10000;
  pointer-events: none;
  font-size: 1.2rem;
  user-select: none;
  animation: float-pop 0.65s cubic-bezier(0.2, 0.8, 0.3, 1) forwards;
}

@keyframes float-pop {
  0% {
    opacity: 1;
    transform: translate(0, 0) scale(0.6);
  }
  50% {
    opacity: 0.95;
    transform: translate(var(--dx), var(--dy)) scale(1.2);
  }
  100% {
    opacity: 0;
    transform: translate(calc(var(--dx) * 1.3), calc(var(--dy) * 1.3 + 20px)) scale(0.4);
  }
}

/* Toolbar Controls */
.toolbar {
  display: flex;
  justify-content: center;
  gap: 10px;
  width: 100%;
  max-width: 580px;
  margin-top: 4px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

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

.button.purple {
  background: var(--purple);
  color: #000;
}

.button.purple:hover {
  background: #cd9ce2;
}

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

.modal-backdrop.hidden {
  display: none !important;
}

.modal-card {
  background: #ffffff;
  border: 2px solid var(--board-border);
  border-radius: 24px;
  padding: 24px;
  max-width: 440px;
  width: 100%;
  text-align: center;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
  animation: modal-pop 0.25s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes modal-pop {
  0% { transform: scale(0.92); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

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

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

.modal-subtext {
  color: var(--text-muted);
  font-size: 0.95rem;
  margin: 0 0 16px;
  line-height: 1.4;
}

.modal-stats {
  background: rgba(0, 0, 0, 0.03);
  border: 2px solid var(--grey-border);
  border-radius: 12px;
  padding: 10px;
  margin-bottom: 16px;
  font-size: 0.95rem;
  color: var(--text-main);
}

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

.modal-actions button {
  padding: 10px 18px;
  font-size: 1rem;
  border: 2px solid #000;
  cursor: pointer;
}

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

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

.setting-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
}

.setting-row label {
  font-weight: bold;
  font-size: 0.95rem;
}

.setting-row select {
  font-family: inherit;
  font-size: 0.95rem;
  padding: 8px 10px;
  border: 2px solid #000;
  border-radius: 8px;
  background: #ffffff;
  outline: none;
}

/* Mobile Responsiveness */
@media (max-width: 540px) {
  .title-group h1 {
    font-size: 1.4rem;
  }
  .board-frame {
    min-height: 330px;
    padding: 10px;
  }
  .pile-stage {
    height: 310px;
  }
  .pile-tile {
    font-size: var(--pile-font-size, 1.55rem);
  }
  .flying-tile {
    width: 44px;
    height: 44px;
    aspect-ratio: 1 / 1;
    font-size: 1.55rem;
  }
  .tray-slot {
    width: 44px;
    height: 44px;
    aspect-ratio: 1 / 1;
    font-size: 1.55rem;
  }
  .tile-tray {
    gap: 4px;
  }
  .tray-frame {
    padding: 8px 6px;
  }
}
