:root {
  --bg: #fff9df;
  --ink: #223043;
  --muted: #647084;
  --road: #5b6472;
  --road-line: #dfe5ec;
  --panel: #ffffff;
  --accent: #f8cf4a;
  --accent-dark: #b36924;
  --green: #2f9e65;
  --shadow: 0 16px 42px rgba(33, 38, 46, 0.16);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(180deg, rgba(248, 207, 74, 0.35), rgba(255, 255, 255, 0) 280px),
    var(--bg);
}

button,
select {
  font: inherit;
}

button {
  min-height: 52px;
  border: 0;
  border-radius: 8px;
  color: var(--ink);
  background: #edf2f8;
  font-weight: 800;
  box-shadow: inset 0 -3px rgba(34, 48, 67, 0.14);
  touch-action: manipulation;
}

button:active {
  transform: translateY(1px);
}

button:disabled {
  color: #98a2b3;
  background: #e4e8ef;
}

.app-shell {
  width: min(1120px, 100%);
  margin: 0 auto;
  padding: max(18px, env(safe-area-inset-top)) 18px max(20px, env(safe-area-inset-bottom));
}

.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 16px;
}

.status-cluster {
  display: inline-flex;
  align-items: center;
  gap: 10px;
}

.kicker {
  margin: 0 0 4px;
  color: var(--accent-dark);
  font-size: 0.88rem;
  font-weight: 900;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: clamp(1.7rem, 4vw, 3rem);
  line-height: 1.02;
}

.offline-status {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  min-height: 44px;
  padding: 10px 13px;
  border: 2px solid rgba(34, 48, 67, 0.12);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.82);
  color: var(--muted);
  font-size: 0.94rem;
  font-weight: 800;
  white-space: nowrap;
}

.offline-status.ready {
  color: #176345;
  border-color: rgba(47, 158, 101, 0.35);
}

.status-dot {
  width: 12px;
  height: 12px;
  border-radius: 999px;
  background: #d99a2b;
}

.offline-status.ready .status-dot {
  background: var(--green);
}

.version-badge {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
  padding: 10px 12px;
  border: 2px solid rgba(34, 48, 67, 0.12);
  border-radius: 999px;
  background: rgba(34, 48, 67, 0.08);
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 900;
  white-space: nowrap;
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 18px;
  align-items: start;
}

.board-wrap,
.controls {
  border: 2px solid rgba(34, 48, 67, 0.12);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.76);
  box-shadow: var(--shadow);
}

.board-wrap {
  padding: 14px;
}

.level-strip {
  display: grid;
  grid-template-columns: 58px 1fr 58px;
  gap: 10px;
  margin-bottom: 14px;
}

.icon-button {
  font-size: 2.25rem;
  line-height: 1;
}

.level-picker {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  min-width: 0;
  padding: 0 12px;
  border-radius: 8px;
  background: #edf2f8;
  font-weight: 900;
}

.level-picker span {
  color: var(--muted);
  font-size: 0.82rem;
  text-transform: uppercase;
}

select {
  width: 100%;
  min-height: 52px;
  border: 0;
  color: var(--ink);
  background: transparent;
  font-weight: 900;
}

.board {
  position: relative;
  width: min(78vw, 650px);
  max-width: 100%;
  aspect-ratio: 1;
  margin: 0 auto;
  overflow: visible;
  border: 6px solid #2d3645;
  border-right-color: transparent;
  border-radius: 8px;
  background:
    linear-gradient(90deg, rgba(255,255,255,0.12) 1px, transparent 1px),
    linear-gradient(180deg, rgba(255,255,255,0.12) 1px, transparent 1px),
    var(--road);
  background-size: calc(100% / 6) calc(100% / 6);
  touch-action: none;
  user-select: none;
}

.board::after {
  content: "";
  position: absolute;
  top: calc((100% / 6) * 2);
  right: -18px;
  width: 22px;
  height: calc(100% / 6);
  background: var(--road);
  border-top: 6px solid #2d3645;
  border-bottom: 6px solid #2d3645;
}

.exit-label {
  position: absolute;
  top: calc((100% / 6) * 2 + 8px);
  right: -70px;
  z-index: 0;
  color: var(--accent-dark);
  font-size: 0.8rem;
  font-weight: 900;
  transform: rotate(90deg);
}

.piece {
  position: absolute;
  display: grid;
  place-items: center;
  padding: 5px;
  border: 4px solid rgba(0, 0, 0, 0.16);
  border-radius: 8px;
  color: rgba(255, 255, 255, 0.94);
  font-size: clamp(1rem, 4vw, 1.55rem);
  font-weight: 1000;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
  box-shadow: inset 0 -8px rgba(0, 0, 0, 0.14), 0 5px 12px rgba(0, 0, 0, 0.22);
  cursor: grab;
  transition: box-shadow 140ms ease, outline-color 140ms ease;
}

.piece::before,
.piece::after {
  content: "";
  position: absolute;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.46);
}

.piece.h::before,
.piece.h::after {
  width: 16%;
  height: 18%;
  top: 14%;
}

.piece.h::before {
  left: 18%;
}

.piece.h::after {
  right: 18%;
}

.piece.v::before,
.piece.v::after {
  width: 18%;
  height: 16%;
  left: 14%;
}

.piece.v::before {
  top: 18%;
}

.piece.v::after {
  bottom: 18%;
}

.piece.selected {
  outline: 5px solid var(--accent);
  outline-offset: 2px;
  box-shadow: inset 0 -8px rgba(0, 0, 0, 0.14), 0 0 0 7px rgba(248, 207, 74, 0.32);
}

.piece.dragging {
  z-index: 5;
  cursor: grabbing;
  transition: none;
}

.piece.escaping {
  pointer-events: none;
  animation: escape-right 760ms ease-in forwards;
}

.fireworks {
  position: absolute;
  inset: 0;
  z-index: 8;
  pointer-events: none;
  overflow: hidden;
}

.firework-burst {
  position: absolute;
  left: var(--burst-x);
  top: var(--burst-y);
  width: 8px;
  height: 8px;
  animation: burst-pop 1050ms ease-out var(--delay) both;
}

.firework-burst span {
  position: absolute;
  left: 2px;
  top: 2px;
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: var(--accent);
  box-shadow: 0 0 12px rgba(248, 207, 74, 0.8);
  transform: rotate(var(--angle)) translateX(0);
  animation: spark-fly 1050ms ease-out var(--delay) both;
}

.firework-burst span:nth-child(3n) {
  background: #4cc9f0;
  box-shadow: 0 0 12px rgba(76, 201, 240, 0.8);
}

.firework-burst span:nth-child(3n + 1) {
  background: #f72585;
  box-shadow: 0 0 12px rgba(247, 37, 133, 0.75);
}

@keyframes escape-right {
  0% {
    opacity: 1;
    transform: translateX(0) scale(1);
  }

  65% {
    opacity: 1;
    transform: translateX(42%) scale(1.02);
  }

  100% {
    opacity: 0;
    transform: translateX(155%) scale(0.92);
  }
}

@keyframes burst-pop {
  0%,
  15% {
    opacity: 0;
    transform: scale(0.2);
  }

  25% {
    opacity: 1;
    transform: scale(1);
  }

  100% {
    opacity: 0;
    transform: scale(1.3);
  }
}

@keyframes spark-fly {
  0%,
  15% {
    opacity: 0;
    transform: rotate(var(--angle)) translateX(0) scale(0.3);
  }

  26% {
    opacity: 1;
  }

  100% {
    opacity: 0;
    transform: rotate(var(--angle)) translateX(var(--distance)) scale(0.85);
  }
}

.controls {
  display: grid;
  gap: 14px;
  padding: 14px;
}

.stat-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.stat-row > div {
  min-height: 76px;
  padding: 12px;
  border-radius: 8px;
  background: #f3f6fa;
}

.stat-label {
  display: block;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 900;
  text-transform: uppercase;
}

.stat-row strong {
  display: block;
  margin-top: 3px;
  font-size: 2rem;
}

.level-name {
  min-height: 28px;
  margin: 0;
  font-size: 1.1rem;
  font-weight: 900;
}

.win-message {
  margin: 0;
  padding: 12px;
  border-radius: 8px;
  color: #176345;
  background: #daf5e8;
  font-weight: 900;
}

.nudge-pad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 64px);
  gap: 8px;
}

.nudge-pad button {
  min-height: 64px;
  font-size: 1.35rem;
}

#moveUp {
  grid-column: 2;
}

#moveLeft {
  grid-column: 1;
  grid-row: 2;
}

#moveRight {
  grid-column: 3;
  grid-row: 2;
}

#moveDown {
  grid-column: 2;
  grid-row: 3;
}

.action-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.generator-panel {
  display: grid;
  gap: 10px;
  padding: 12px;
  border-radius: 8px;
  background: #f3f6fa;
}

.generator-panel label {
  display: grid;
  gap: 4px;
}

.generator-panel select {
  min-height: 48px;
  padding: 0 10px;
  border-radius: 8px;
  background: #ffffff;
}

.generator-status {
  min-height: 22px;
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
  font-weight: 800;
}

.primary-action {
  color: #3b2600;
  background: var(--accent);
}

.floating-action {
  position: fixed;
  right: max(16px, env(safe-area-inset-right));
  z-index: 12;
  display: grid;
  place-items: center;
  width: 58px;
  min-width: 58px;
  height: 58px;
  min-height: 58px;
  aspect-ratio: 1;
  padding: 0;
  border: 2px solid rgba(34, 48, 67, 0.14);
  border-radius: 999px;
  background: #ffffff;
  box-shadow: var(--shadow);
}

.settings-button {
  bottom: max(16px, env(safe-area-inset-bottom));
}

.reset-floating-button {
  bottom: calc(max(16px, env(safe-area-inset-bottom)) + 70px);
  display: none;
}

.floating-action svg {
  display: block;
  flex: 0 0 auto;
  width: 28px;
  height: 28px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 1.8;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(34, 48, 67, 0.48);
}

.modal-backdrop[hidden] {
  display: none;
}

.settings-modal,
.level-complete-modal,
.confirm-modal {
  display: grid;
  gap: 14px;
  width: min(320px, 100%);
  padding: 18px;
  border: 2px solid rgba(34, 48, 67, 0.14);
  border-radius: 8px;
  background: #ffffff;
  box-shadow: var(--shadow);
}

.settings-modal {
  width: min(360px, 100%);
}

.level-complete-modal p,
.confirm-modal p {
  margin: 0;
  font-size: 1.3rem;
  font-weight: 900;
}

.confirm-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.modal-header p {
  margin: 0;
  font-size: 1.3rem;
  font-weight: 900;
}

.close-button {
  display: grid;
  place-items: center;
  width: 42px;
  min-height: 42px;
  padding: 0;
  border-radius: 999px;
  font-size: 1.5rem;
  line-height: 1;
}

@media (any-pointer: coarse) {
  .nudge-pad {
    display: none;
  }
}

@media (max-width: 860px) {
  .top-bar,
  .game-layout {
    grid-template-columns: 1fr;
  }

  .top-bar {
    display: grid;
    align-items: start;
  }

  .status-cluster {
    justify-self: start;
    flex-wrap: wrap;
  }

  .offline-status {
    white-space: normal;
  }

  .board {
    width: min(92vw, 690px);
  }

  .controls {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 520px) {
  .app-shell {
    padding-inline: 10px;
  }

  .board-wrap,
  .controls {
    padding: 10px;
  }

  .exit-label {
    display: none;
  }
}

@media (pointer: coarse) and (max-width: 1180px), (pointer: coarse) and (max-height: 820px) {
  html,
  body {
    height: 100%;
    overflow: hidden;
  }

  body {
    background: var(--bg);
  }

  .app-shell {
    display: block;
    width: 100%;
    height: 100dvh;
    padding: max(8px, env(safe-area-inset-top)) 10px max(8px, env(safe-area-inset-bottom));
    overflow: hidden;
  }

  .top-bar,
  .controls {
    display: none;
  }

  .game-layout {
    display: block;
    height: 100%;
  }

  .board-wrap {
    display: grid;
    grid-template-rows: 52px minmax(0, 1fr);
    gap: 10px;
    height: 100%;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: transparent;
    box-shadow: none;
  }

  .level-strip {
    grid-template-columns: 52px minmax(0, 1fr) 52px;
    gap: 8px;
    margin: 0;
  }

  .level-picker {
    padding: 0 10px;
  }

  .level-picker span {
    display: none;
  }

  select {
    min-height: 52px;
  }

  .board {
    align-self: center;
    width: min(calc(100vw - 36px), calc(100dvh - 90px));
  }

  .reset-floating-button {
    display: grid;
  }
}
