:root {
  --bg: #f4f7f1;
  --ink: #202522;
  --muted: #68736d;
  --panel: #ffffff;
  --line: #dbe3dc;
  --accent: #1f8a70;
  --accent-strong: #14634f;
  --warn: #e95d4f;
  --sun: #f2b84b;
  --tile: #ffffff;
  --tile-edge: #cfd9d1;
  --tile-shadow: 0 10px 24px rgba(34, 50, 41, 0.13);
  --radius: 8px;
  --gap: clamp(5px, 0.7vw, 10px);
  --board-cols: 12;
  --match-duration: 150ms;
  --shuffle-duration: 500ms;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
  font-family: Arial, "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
  color: var(--ink);
  background:
    linear-gradient(135deg, rgba(31, 138, 112, 0.1), transparent 34%),
    linear-gradient(315deg, rgba(233, 93, 79, 0.08), transparent 30%),
    var(--bg);
}

button {
  font: inherit;
}

.game-shell {
  height: 100vh;
  display: flex;
  flex-direction: column;
  padding: clamp(10px, 1.4vh, 18px) clamp(14px, 2vw, 28px);
  gap: 10px;
}

.hud {
  width: min(1180px, 100%);
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.brand-mark {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  border-radius: var(--radius);
  color: #ffffff;
  background: var(--accent);
  box-shadow: 0 8px 18px rgba(31, 138, 112, 0.26);
  font-size: 1.35rem;
  font-weight: 800;
}

.brand h1 {
  margin: 0;
  font-size: clamp(1.1rem, 2vw, 1.6rem);
  line-height: 1.1;
}

.brand p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.92rem;
}

.hud-panel {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.hud-item {
  min-width: 96px;
  min-height: 54px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.84);
  display: grid;
  gap: 2px;
}

.hud-label {
  color: var(--muted);
  font-size: 0.76rem;
  text-transform: uppercase;
  letter-spacing: 0;
}

.hud-item strong {
  font-size: 1.24rem;
  line-height: 1;
}

.control-button {
  min-height: 44px;
  padding: 0 16px;
  border: 0;
  border-radius: var(--radius);
  color: #ffffff;
  background: var(--ink);
  cursor: pointer;
  transition: transform 140ms ease, background 140ms ease, box-shadow 140ms ease;
  box-shadow: 0 9px 18px rgba(32, 37, 34, 0.16);
}

.control-button:hover {
  transform: translateY(-1px);
  background: var(--accent-strong);
}

.control-button:active {
  transform: translateY(0);
}

.secondary-button {
  color: var(--ink);
  background: #ffffff;
  border: 1px solid var(--line);
  box-shadow: 0 6px 14px rgba(32, 37, 34, 0.1);
}

.secondary-button:hover {
  color: #ffffff;
  border-color: var(--accent-strong);
}

.board-zone {
  flex: 1;
  min-height: 0;
  display: grid;
  place-items: center;
  padding: 0;
}

.board-stage {
  position: relative;
  width: min(1320px, calc(100vw - 28px), calc((100vh - 126px) * 1.8));
  overflow: visible;
}

.board {
  position: relative;
  z-index: 2;
  display: grid;
  grid-template-columns: repeat(var(--board-cols), minmax(0, 1fr));
  gap: var(--gap);
  width: 100%;
  padding: clamp(8px, 1vw, 14px);
  border: 1px solid rgba(31, 138, 112, 0.18);
  border-radius: var(--radius);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.72), rgba(255, 255, 255, 0.48)),
    rgba(244, 247, 241, 0.8);
  box-shadow: 0 18px 48px rgba(34, 50, 41, 0.14);
}

.tile,
.tile-empty {
  aspect-ratio: 0.8;
  min-width: 0;
}

.tile {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  border: 2px solid var(--tile-edge);
  border-radius: var(--radius);
  background:
    linear-gradient(160deg, rgba(255, 255, 255, 0.94), rgba(244, 248, 246, 0.9)),
    var(--tile);
  box-shadow: var(--tile-shadow);
  cursor: pointer;
  user-select: none;
  transform: translateZ(0);
  transition:
    border-color var(--match-duration) ease,
    box-shadow var(--match-duration) ease,
    filter var(--match-duration) ease,
    opacity var(--match-duration) ease,
    transform var(--match-duration) ease;
}

.tile::before {
  content: "";
  position: absolute;
  inset: 7px;
  border: 1px solid rgba(207, 217, 209, 0.8);
  border-radius: 6px;
  pointer-events: none;
}

.tile:hover {
  border-color: var(--accent);
  filter: brightness(1.04);
}

.tile.is-selected {
  z-index: 4;
  border-color: var(--warn);
  box-shadow: 0 0 0 3px rgba(233, 93, 79, 0.22), 0 14px 26px rgba(34, 50, 41, 0.2);
  transform: scale(1.035);
}

.tile.is-matched {
  pointer-events: none;
  opacity: 0;
  transform: scale(0.78);
}

.tile.is-invalid {
  animation: invalidWiggle 190ms ease;
}

.tile:disabled {
  cursor: default;
}

.tile-icon {
  position: relative;
  z-index: 1;
  display: grid;
  place-items: center;
  width: 78%;
  height: 68%;
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.6);
  font-size: 2rem;
  line-height: 1;
}

.tile-empty {
  border-radius: var(--radius);
  background: rgba(31, 138, 112, 0.04);
}

.board.is-shuffling .tile {
  animation: shuffleTile var(--shuffle-duration) ease both;
}

.path-layer {
  position: absolute;
  inset: 0;
  z-index: 5;
  width: 100%;
  height: 100%;
  overflow: visible;
  pointer-events: none;
}

.connection-line {
  fill: none;
  stroke: var(--sun);
  stroke-width: 5;
  stroke-linecap: round;
  stroke-linejoin: round;
  filter: drop-shadow(0 3px 4px rgba(32, 37, 34, 0.22));
  animation: drawLine 150ms ease both;
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(32, 37, 34, 0.46);
  backdrop-filter: blur(3px);
}

.overlay[hidden] {
  display: none;
}

.overlay.is-paused {
  cursor: pointer;
}

.overlay-panel {
  width: min(360px, 100%);
  padding: 28px;
  border-radius: var(--radius);
  background: var(--panel);
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.22);
  cursor: default;
}

.overlay-kicker {
  margin: 0 0 8px;
  color: var(--accent);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0;
}

.overlay-panel h2 {
  margin: 0;
  font-size: 2rem;
}

.overlay-panel p {
  margin: 14px 0 22px;
  color: var(--muted);
}

.restart-button {
  min-width: 132px;
  background: var(--accent);
}

@keyframes invalidWiggle {
  0%,
  100% {
    transform: translateX(0);
  }
  35% {
    transform: translateX(-4px);
  }
  70% {
    transform: translateX(4px);
  }
}

@keyframes drawLine {
  from {
    opacity: 0;
    stroke-dasharray: 18 14;
  }
  to {
    opacity: 1;
    stroke-dasharray: 0 0;
  }
}

@keyframes shuffleTile {
  0% {
    opacity: 1;
    transform: scale(1) rotate(0);
    filter: brightness(1);
  }
  35% {
    opacity: 0.55;
    transform: scale(0.88) rotate(-2deg);
    filter: brightness(1.08);
  }
  70% {
    opacity: 0.7;
    transform: scale(0.9) rotate(2deg);
  }
  100% {
    opacity: 1;
    transform: scale(1) rotate(0);
    filter: brightness(1);
  }
}

@media (max-width: 900px) {
  html,
  body {
    overflow-x: hidden;
    overflow-y: hidden;
  }

  .game-shell {
    min-height: 100dvh;
    min-height: 100svh;
    height: 100dvh;
    height: 100svh;
    width: 100vw;
    max-width: 100vw;
    overflow-x: hidden;
    overflow-y: hidden;
    align-items: center;
    padding: max(14px, env(safe-area-inset-top)) 12px max(28px, calc(env(safe-area-inset-bottom) + 28px));
    gap: 8px;
  }

  .hud {
    align-items: stretch;
    flex-direction: column;
    gap: 7px;
    width: min(calc(100vw - 24px), 286px);
  }

  .hud-panel {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
    justify-content: initial;
    align-self: stretch;
    width: 100%;
    max-width: 100%;
  }

  .hud-item {
    min-width: 0;
    min-height: 40px;
    padding: 5px 7px;
    overflow: hidden;
  }

  .hud-item strong {
    font-size: 0.95rem;
  }

  .control-button {
    min-height: 38px;
    padding: 0 8px;
  }

  .secondary-button {
    grid-column: 1 / span 1;
  }

  #pauseButton {
    grid-column: 2 / span 2;
  }

  .brand p {
    display: none;
  }

  .brand-mark {
    width: 36px;
    height: 36px;
    font-size: 1.1rem;
  }

  .brand h1 {
    font-size: 1.08rem;
  }

  .board-zone {
    align-items: start;
    width: 100%;
    justify-items: center;
    overflow: hidden;
    padding: 0;
  }

  .board-stage {
    width: min(calc(100vw - 24px), 286px);
    max-width: 100%;
  }

  .board {
    gap: 3px;
    padding: 6px;
  }

  .tile,
  .tile-empty {
    aspect-ratio: 1;
  }

  .tile::before {
    inset: 4px;
  }

  .tile-icon {
    width: 74%;
    height: 66%;
    font-size: 1rem;
  }
}

@media (max-width: 420px) {
  .game-shell {
    padding: max(12px, env(safe-area-inset-top)) 10px max(24px, calc(env(safe-area-inset-bottom) + 24px));
  }

  .hud {
    width: min(calc(100vw - 20px), 286px);
  }

  .hud-panel {
    width: 100%;
  }

  .hud-label {
    font-size: 0.66rem;
  }

  .hud-item strong {
    font-size: 0.92rem;
  }

  .board-stage {
    width: min(calc(100vw - 20px), 286px);
  }

  .tile-icon {
    font-size: 0.98rem;
  }
}

@media (max-width: 900px) and (max-height: 680px) {
  .game-shell {
    padding: max(10px, env(safe-area-inset-top)) 10px max(18px, calc(env(safe-area-inset-bottom) + 18px));
    gap: 6px;
  }

  .brand {
    gap: 8px;
  }

  .brand-mark {
    width: 32px;
    height: 32px;
    font-size: 1rem;
  }

  .brand h1 {
    font-size: 1rem;
  }

  .hud {
    gap: 6px;
  }

  .hud-panel {
    width: 100%;
    gap: 5px;
  }

  .hud {
    width: min(calc(100vw - 20px), 246px);
  }

  .hud-item {
    min-height: 36px;
    padding: 4px 6px;
  }

  .hud-label {
    font-size: 0.62rem;
  }

  .hud-item strong {
    font-size: 0.86rem;
  }

  .control-button {
    min-height: 34px;
    font-size: 0.9rem;
  }

  .board-stage {
    width: min(calc(100vw - 20px), 246px);
  }

  .board {
    gap: 2px;
    padding: 5px;
  }

  .tile-icon {
    font-size: 0.84rem;
  }
}
