:root {
  color-scheme: dark;
  --board-size: min(92vw, 62dvh, 640px);
  --gap: clamp(3px, .9vmin, 7px);
  --cell: calc((var(--board-size) - var(--gap) * 7) / 8);
  --ink: #172027;
  --paper: #fff7e8;
  --aqua: #5ed6d1;
  --coral: #ff6f61;
  --gold: #f7bf3b;
  --green: #68bf6b;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100dvh;
  font-family: Arial, Helvetica, sans-serif;
  background:
    linear-gradient(180deg, rgba(255,255,255,.2), rgba(255,255,255,0)),
    radial-gradient(circle at 18% 8%, #f8dc67 0 8rem, transparent 8.2rem),
    linear-gradient(145deg, #74c7df 0%, #f5d07b 48%, #ed806c 100%);
  color: var(--ink);
  overscroll-behavior: contain;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  overflow-x: hidden;
}

.shell {
  width: min(1120px, calc(100vw - 16px));
  margin: 0 auto;
  min-height: 100dvh;
  padding: max(8px, env(safe-area-inset-top)) 0 calc(82px + env(safe-area-inset-bottom));
}

.topbar, .panel, .board-wrap {
  background: rgba(255, 247, 232, .86);
  border: 3px solid rgba(23,32,39,.16);
  box-shadow: 0 16px 40px rgba(67, 48, 39, .18);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  border-radius: 8px;
  padding: 10px 14px;
}

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

.brand img {
  width: clamp(32px, 8vw, 54px);
  height: clamp(46px, 11vw, 86px);
  object-fit: cover;
  object-position: top;
}

h1 {
  margin: 0;
  font-size: clamp(1.2rem, 4vw, 2.6rem);
  line-height: 1;
}

p { line-height: 1.45; }
.brand p { margin: 6px 0 0; }

.stats {
  display: grid;
  grid-template-columns: repeat(6, minmax(62px, 1fr));
  gap: clamp(4px, 1vw, 8px);
}

.stats div, .target {
  background: #fffdf7;
  border: 2px solid rgba(23,32,39,.12);
  border-radius: 8px;
  padding: clamp(5px, 1.4vw, 8px) clamp(6px, 1.7vw, 12px);
  text-align: center;
}

.stats span, .target span {
  display: block;
  font-size: .72rem;
  text-transform: uppercase;
  letter-spacing: .08em;
  opacity: .75;
}

.stats strong, .target strong {
  display: block;
  font-size: clamp(1rem, 4vw, 1.45rem);
}

.game-area {
  display: grid;
  grid-template-columns: max-content minmax(230px, 1fr);
  gap: 18px;
  align-items: start;
  margin-top: 18px;
}

.board-wrap {
  position: relative;
  border-radius: 8px;
  padding: 12px;
}

.board {
  position: relative;
  width: calc(var(--cell) * 8 + var(--gap) * 7);
  height: calc(var(--cell) * 8 + var(--gap) * 7);
  touch-action: none;
  overflow: visible;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.cell-bg, .tile {
  position: absolute;
  width: var(--cell);
  height: var(--cell);
  border-radius: 8px;
}

.cell-bg {
  background: linear-gradient(145deg, rgba(255,255,255,.7), rgba(94,214,209,.24));
  border: 1px solid rgba(23,32,39,.11);
}

.tile {
  display: grid;
  place-items: center;
  border: 0;
  padding: 4px;
  background: radial-gradient(circle at 45% 35%, rgba(255,255,255,.82), color-mix(in srgb, var(--piece-accent, #f7bf3b) 34%, transparent));
  border: 2px solid color-mix(in srgb, var(--piece-accent, #f7bf3b) 72%, #172027);
  cursor: pointer;
  transition: transform 180ms ease, opacity 160ms ease, filter 160ms ease;
}

.tile img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  user-drag: none;
  -webkit-user-drag: none;
  filter: drop-shadow(0 8px 8px rgba(23,32,39,.2));
}

.tile:hover, .tile.selected { filter: brightness(1.12) saturate(1.1); }
.tile.selected { outline: 4px solid var(--gold); outline-offset: -4px; }
.tile.hint {
  outline: 4px solid #188b86;
  outline-offset: -4px;
  animation: hintPulse 700ms ease-in-out infinite alternate;
}
.tile.hint img { animation: hintImagePulse 700ms ease-in-out infinite alternate; }
.tile.removing { opacity: 0; transform: scale(.35) rotate(18deg); }
.particle {
  position: absolute;
  z-index: 5;
  width: 10px;
  height: 10px;
  margin: -5px 0 0 -5px;
  border-radius: 50%;
  pointer-events: none;
  animation: particlePop 620ms ease-out forwards;
}
.particle.burst {
  background: #f7bf3b;
  box-shadow: 0 0 12px #fff7e8;
}
.particle.hint {
  background: #5ed6d1;
  box-shadow: 0 0 12px #ffffff;
}
.tile.special-line-h::after, .tile.special-line-v::after, .tile.special-bomb::after, .tile.special-prism::after {
  position: absolute;
  right: 5px;
  bottom: 5px;
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #172027;
  color: white;
  font-weight: 800;
  font-size: 13px;
}
.tile.special-line-h::after { content: "H"; }
.tile.special-line-v::after { content: "V"; }
.tile.special-bomb::after { content: "*"; }
.tile.special-prism::after { content: "P"; }

.panel {
  border-radius: 8px;
  padding: 16px;
}

.goals h2, .kingdom h2, .collections h2 {
  margin: 14px 0 8px;
  font-size: 1rem;
}

.battle-panel {
  display: grid;
  gap: 5px;
  margin-top: 12px;
  padding: 10px;
  border-radius: 8px;
  background: #172027;
  color: #fff7e8;
}

.battle-panel meter {
  width: 100%;
}

.battle-panel span, .battle-panel small {
  opacity: .86;
}

.goal {
  display: grid;
  gap: 5px;
  margin: 8px 0;
  padding: 8px;
  border-radius: 8px;
  background: rgba(255,255,255,.58);
  border: 1px solid rgba(23,32,39,.12);
}

.goal div {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
}

.goal strong, .summary-row strong {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.goal-icon {
  width: 24px;
  height: 24px;
  object-fit: contain;
}

.goal span {
  white-space: nowrap;
  font-weight: 700;
}

.goal.complete {
  background: rgba(104,191,107,.2);
  border-color: rgba(45,125,68,.45);
}

.goal meter {
  width: 100%;
  height: 12px;
}

.kingdom, .collections {
  max-height: 260px;
  overflow: auto;
}

.kingdom-area {
  display: grid;
  gap: 6px;
  margin: 8px 0;
  padding: 8px;
  border-radius: 8px;
  background: rgba(255,255,255,.58);
  border: 1px solid rgba(23,32,39,.12);
}

.kingdom-area.locked {
  opacity: .62;
}

.kingdom-area small {
  font-weight: 700;
}

.area-title {
  display: flex;
  gap: 8px;
  align-items: center;
}

.area-title img, .collection-row img, .reward-icons img {
  width: 28px;
  height: 28px;
  object-fit: contain;
}

.collection-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 5px 8px;
  margin: 8px 0;
  padding: 8px;
  border-radius: 8px;
  background: rgba(255,255,255,.58);
  border: 1px solid rgba(23,32,39,.12);
}

.collection-row div {
  grid-column: 1 / -1;
  display: flex;
  gap: 4px;
}

.task {
  min-height: 34px;
  padding: 7px 9px;
  text-align: left;
  background: #188b86;
}

.actions {
  display: grid;
  gap: 10px;
  margin: 14px 0;
}

button {
  min-height: 44px;
  border: 0;
  border-radius: 8px;
  padding: 10px 14px;
  background: var(--coral);
  color: #fff;
  font-weight: 800;
  cursor: pointer;
  box-shadow: inset 0 -3px 0 rgba(0,0,0,.16);
}

.mobile-actions {
  position: fixed;
  z-index: 12;
  left: 0;
  right: 0;
  bottom: 0;
  display: none;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  padding: 8px max(8px, env(safe-area-inset-left)) calc(8px + env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-right));
  background: rgba(255,247,232,.94);
  border-top: 2px solid rgba(23,32,39,.14);
  backdrop-filter: blur(10px);
}

.mobile-actions button {
  min-height: 52px;
  padding: 7px 5px;
  font-size: .82rem;
}

.mobile-actions button span {
  display: block;
  font-size: .72rem;
  opacity: .9;
}

.mobile-actions button.selected {
  outline: 4px solid var(--gold);
}

button:nth-child(2) { background: #188b86; }
button:nth-child(3) { background: #4d6fbf; }
button:nth-child(4) { background: #6f5aa8; }
button:nth-child(5) { background: #8d5797; }
button:nth-child(6) { background: #5c7f54; }
button:disabled { opacity: .65; cursor: wait; }

.banner {
  position: absolute;
  inset: auto 22px 22px;
  min-height: 30px;
  text-align: center;
  font-weight: 800;
  color: #fff;
  text-shadow: 0 2px 8px rgba(0,0,0,.45);
  pointer-events: none;
}

.overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(23,32,39,.5);
  padding: 20px;
}

.hidden { display: none; }

.modal {
  width: min(620px, 100%);
  border-radius: 8px;
  background: var(--paper);
  padding: 24px;
  text-align: center;
  border: 3px solid rgba(23,32,39,.18);
}

.level-select {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 10px;
  max-height: min(52vh, 420px);
  overflow: auto;
  margin: 14px 0;
  text-align: left;
}

.overlay-details {
  display: grid;
  gap: 8px;
  margin: 12px 0;
}

.summary-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 10px;
  border-radius: 8px;
  background: rgba(255,255,255,.65);
  border: 1px solid rgba(23,32,39,.12);
}

.summary-row.complete {
  border-color: rgba(45,125,68,.5);
}

.summary-row.missed {
  border-color: rgba(255,111,97,.55);
}

.reward-summary {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px;
  border-radius: 8px;
  background: rgba(247,191,59,.18);
  border: 1px solid rgba(128,88,20,.25);
}

.reward-icons {
  display: flex;
  gap: 5px;
}

.asset-section {
  text-align: left;
  margin: 14px 0;
}

.asset-section h3 {
  margin: 0 0 8px;
}

.asset-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(86px, 1fr));
  gap: 8px;
  max-height: 260px;
  overflow: auto;
}

.asset-grid figure {
  margin: 0;
  min-height: 92px;
  display: grid;
  place-items: center;
  gap: 4px;
  padding: 6px;
  border-radius: 8px;
  background: rgba(255,255,255,.65);
  border: 1px solid rgba(23,32,39,.1);
}

.asset-grid img {
  width: 44px;
  height: 44px;
  object-fit: contain;
}

.asset-grid figcaption {
  max-width: 100%;
  font-size: .68rem;
  overflow: hidden;
  text-overflow: ellipsis;
}

.asset-bonus {
  position: absolute;
  z-index: 8;
  left: 50%;
  top: 50%;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 999px;
  background: rgba(23,32,39,.88);
  color: #fff7e8;
  transform: translate(-50%, -50%);
  animation: bonusFloat 1100ms ease-out forwards;
  pointer-events: none;
}

.asset-bonus img {
  width: 34px;
  height: 34px;
  object-fit: contain;
}

.level-card {
  min-height: 92px;
  display: grid;
  gap: 3px;
  align-content: center;
  background: #4d6fbf;
}

.level-card:nth-child(5n),
.level-card:nth-child(10),
.level-card:nth-child(15),
.level-card:nth-child(20),
.level-card:nth-child(25),
.level-card:nth-child(30) {
  background: #8d5797;
}

.level-card.complete {
  background: #188b86;
}

.level-card:disabled {
  background: #8a8f98;
  color: rgba(255,255,255,.75);
  cursor: not-allowed;
}

.level-card span, .level-card small {
  display: block;
  font-weight: 700;
}

@keyframes hintPulse {
  from { filter: brightness(1.08) saturate(1.12); }
  to { filter: brightness(1.32) saturate(1.35); }
}

@keyframes hintImagePulse {
  from { transform: scale(1); }
  to { transform: scale(1.08); }
}

@keyframes particlePop {
  0% {
    opacity: 0;
    transform: translate(0, 0) scale(.4);
  }
  18% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: translate(var(--x), var(--y)) scale(1.3);
  }
}

@keyframes bonusFloat {
  0% { opacity: 0; transform: translate(-50%, -35%) scale(.8); }
  18% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -95%) scale(1.08); }
}

@media (max-width: 900px), (hover: none) and (pointer: coarse) {
  :root {
    --board-size: min(94vw, 46dvh, 520px);
    --gap: clamp(3px, .9vw, 5px);
  }

  .shell {
    width: min(100vw, 680px);
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding-top: max(6px, env(safe-area-inset-top));
    padding-bottom: calc(72px + env(safe-area-inset-bottom));
    padding-left: max(6px, env(safe-area-inset-left));
    padding-right: max(6px, env(safe-area-inset-right));
  }

  .topbar {
    display: grid;
    gap: 6px;
    padding: 7px;
    border-width: 2px;
  }

  .brand {
    align-items: center;
    justify-content: center;
    text-align: center;
  }

  .brand img, .brand p {
    display: none;
  }

  h1 {
    font-size: clamp(1.05rem, 5.2vw, 1.45rem);
  }

  .stats {
    grid-template-columns: repeat(4, 1fr);
  }

  .stats div {
    min-width: 0;
    padding: 5px 4px;
  }

  .stats div:nth-child(2),
  .stats div:nth-child(6) {
    display: none;
  }

  .stats span {
    font-size: .58rem;
    letter-spacing: .04em;
  }

  .stats strong {
    font-size: clamp(.9rem, 4.6vw, 1.12rem);
  }

  .game-area {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
    margin-top: 0;
    flex: 1 1 auto;
    min-height: 0;
  }

  .board-wrap {
    order: 2;
    align-self: center;
    padding: 6px;
    border-width: 2px;
  }

  .panel {
    order: 1;
    width: 100%;
    padding: 7px;
    display: grid;
    gap: 6px;
    border-width: 2px;
  }

  .target, .kingdom, .collections, .panel > p, .panel > ul, .actions {
    display: none;
  }

  .goals h2 {
    margin: 0 0 6px;
  }

  #goals {
    display: flex;
    gap: 6px;
    overflow-x: auto;
    padding-bottom: 2px;
    scroll-snap-type: x proximity;
    -webkit-overflow-scrolling: touch;
  }

  #goals:empty {
    display: none;
  }

  #goals .goal {
    flex: 0 0 min(48vw, 178px);
    scroll-snap-align: start;
  }

  #goals:has(.goal:only-child) .goal {
    flex-basis: 100%;
  }

  .goal {
    margin: 0;
    padding: 6px;
    gap: 4px;
  }

  .goal div {
    display: grid;
    grid-template-columns: 1fr;
    gap: 6px;
    font-size: .8rem;
    line-height: 1.15;
  }

  .goal span {
    font-size: .82rem;
  }

  .goal-icon {
    width: 22px;
    height: 22px;
  }

  .battle-panel {
    margin-top: 0;
    padding: 7px;
    font-size: .84rem;
  }

  .mobile-actions {
    display: grid;
  }

  .modal {
    width: min(96vw, 620px);
    max-height: calc(100dvh - 24px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    overflow: auto;
    padding: 16px;
  }

  .level-select {
    grid-template-columns: repeat(auto-fit, minmax(104px, 1fr));
    max-height: 58dvh;
  }

  .asset-grid {
    grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  }
}

@media (max-width: 380px) {
  :root { --board-size: min(96vw, 43dvh); }
  .stats span { font-size: .62rem; }
  .stats strong { font-size: .95rem; }
  .brand img { display: none; }
  .mobile-actions { gap: 5px; }
  .mobile-actions button { font-size: .76rem; }
}

@media (max-width: 340px) {
  :root { --board-size: min(96vw, 40dvh); }
  .shell { padding-left: 4px; padding-right: 4px; }
  .topbar, .panel, .board-wrap { border-radius: 6px; }
  .cell-bg, .tile { border-radius: 6px; }
  .tile { padding: 2px; border-width: 1px; }
  .mobile-actions button { min-height: 48px; padding: 5px 3px; }
}

@media (max-height: 520px) and (orientation: landscape) {
  :root { --board-size: min(82dvh, 48vw, 520px); }

  .shell {
    width: min(100vw - 12px, 1180px);
    display: block;
    padding-top: max(6px, env(safe-area-inset-top));
    padding-bottom: max(6px, env(safe-area-inset-bottom));
  }

  .topbar {
    display: none;
  }

  .game-area {
    grid-template-columns: max-content minmax(0, 1fr);
    display: grid;
    gap: 8px;
    margin-top: 0;
    align-items: center;
  }

  .board-wrap {
    grid-column: 1;
    order: initial;
  }

  .panel {
    grid-column: 2;
    max-height: calc(100dvh - 18px);
    overflow: auto;
    padding: 8px;
  }

  .target, .goals, .battle-panel, .actions {
    display: block;
  }

  .kingdom, .collections, .panel > p, .panel > ul {
    display: none;
  }

  .actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(82px, 1fr));
    gap: 6px;
    margin: 8px 0 0;
  }

  .actions button {
    min-height: 40px;
    padding: 6px 8px;
    font-size: .78rem;
  }

  .mobile-actions {
    display: none;
  }
}

@media (min-width: 761px) and (max-height: 760px) {
  :root { --board-size: min(58vw, 72dvh, 620px); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 1ms !important;
    transition-duration: 1ms !important;
    scroll-behavior: auto !important;
  }
  .particle, .asset-bonus {
    display: none;
  }
}
