:root {
  color-scheme: light;
  --bg: #f6f3ea;
  --paper: #fffdf8;
  --ink: #202327;
  --muted: #68707a;
  --line: #d8d0c3;
  --accent: #197278;
  --danger: #c44536;
  --gold: #f6bd60;
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: linear-gradient(180deg, #fffdf8 0, var(--bg) 44%, #edf5f3 100%);
  color: var(--ink);
}

button,
select {
  font: inherit;
}

a {
  color: var(--accent);
  font-weight: 850;
  text-decoration-thickness: 0.08em;
  text-underline-offset: 0.18em;
}

.app {
  width: min(100% - 24px, 980px);
  margin: 0 auto;
  padding: max(14px, env(safe-area-inset-top)) 0 max(22px, env(safe-area-inset-bottom));
}

.top-nav,
.site-footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 14px;
  color: var(--muted);
  font-size: 0.84rem;
}

.top-nav {
  padding: 10px 0 14px;
}

.top-nav strong {
  margin-right: auto;
  color: var(--ink);
}

.brand-link {
  color: inherit;
  text-decoration: none;
}

.hero-line {
  display: grid;
  gap: 5px;
  margin-bottom: 12px;
}

.intro-screen {
  width: min(100%, 430px);
  min-height: calc(100vh - 118px);
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 12px;
}

.intro-screen .hero-line {
  margin-bottom: 0;
}

.intro-screen h1 {
  font-size: clamp(2.3rem, 13vw, 4rem);
}

.intro-screen .lead {
  font-size: 1rem;
  line-height: 1.55;
  font-weight: 700;
}

.hidden {
  display: none !important;
}

h1 {
  margin: 0;
  font-size: clamp(2.1rem, 10vw, 4.5rem);
  line-height: 0.95;
  letter-spacing: 0;
}

.lead {
  margin: 0;
  color: var(--muted);
  font-size: clamp(0.98rem, 2.3vw, 1.15rem);
  font-weight: 760;
  line-height: 1.5;
}

.game-shell {
  display: grid;
  grid-template-columns: minmax(280px, 520px) minmax(230px, 300px);
  align-items: start;
  gap: 14px;
}

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

.intro-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  box-shadow: 0 4px 14px rgba(36, 38, 43, 0.06);
  padding: 14px;
}

.intro-grid > .intro-card:not(.intro-primary):not(.intro-ranking-panel) {
  order: 1;
}

.intro-primary {
  order: 2;
}

.intro-ranking-panel {
  order: 3;
}

.intro-card h2 {
  margin: 0 0 8px;
  font-size: 1.05rem;
}

.intro-primary {
  display: grid;
  gap: 10px;
}

.intro-note {
  margin: 0;
  color: var(--muted);
  font-weight: 700;
  line-height: 1.5;
}

.intro-stats {
  grid-template-columns: 1fr;
}

.button-large {
  width: 100%;
  min-height: 44px;
  font-size: 1.05rem;
}

.rules-list {
  display: grid;
  gap: 10px;
  margin: 0;
  padding: 0;
  list-style: none;
  color: var(--ink);
  font-size: 0.95rem;
  font-weight: 700;
  line-height: 1.45;
}

.rules-list li {
  display: grid;
  grid-template-columns: 88px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.rules-list p {
  margin: 0;
}

.rules-list b {
  display: block;
  margin-bottom: 3px;
  color: var(--ink);
  font-size: 0.95rem;
}

.rules-list span {
  color: var(--muted);
  font-size: 0.86rem;
}

.intro-action-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.intro-ranking-panel {
  display: grid;
  gap: 8px;
}

.mini-board {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 2px;
  width: 88px;
  aspect-ratio: 1 / 1;
  padding: 2px;
  border-radius: 8px;
  background: #5e6472;
}

.mini-cell {
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  border-radius: 4px;
  background: #fffaf0;
  color: var(--ink);
  font-size: 0.72rem;
  line-height: 1;
  font-style: normal;
  font-weight: 900;
}

.mini-piece {
  display: block;
  width: 78%;
  height: 78%;
  object-fit: contain;
  pointer-events: none;
  filter: drop-shadow(0 1px 1px rgba(32, 35, 39, 0.2));
}

.mini-cell.alt {
  background: #eadfca;
}

.mini-cell.player {
  background: #dff1ee;
  color: var(--accent);
  box-shadow: inset 0 0 0 2px var(--accent);
}

.mini-cell.target {
  background: #f6bd60;
  box-shadow: inset 0 0 0 2px #9c6644;
}

.mini-cell.preview {
  background: #eef6ff;
  box-shadow: inset 0 0 0 2px #4968a8;
}

.mini-cell.danger {
  background: #fff1ed;
  color: var(--danger);
  box-shadow: inset 0 0 0 2px var(--danger);
}

.mini-cell.fade {
  color: rgba(32, 35, 39, 0.34);
}

.intro-card .leaderboard-list li {
  background: var(--paper);
}

.board-panel,
.side-panel,
.rules-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 253, 248, 0.92);
  box-shadow: 0 12px 28px rgba(36, 38, 43, 0.08);
}

.board-panel {
  position: relative;
  overflow: hidden;
  padding: 10px;
}

.board-panel.board-flash {
  animation: boardFlash 240ms ease;
}

.board-panel.board-damage {
  animation: boardDamage 260ms ease;
}

.board-canvas {
  display: block;
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 8px;
  touch-action: manipulation;
  transition: filter 180ms ease, transform 180ms ease, opacity 180ms ease;
}

.board-panel.game-over .board-canvas {
  filter: blur(8px) saturate(0.9) brightness(0.95);
  transform: scale(0.985);
  opacity: 0.9;
}

.board-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 12px;
  background: rgba(246, 243, 234, 0.42);
  overflow: auto;
}

.overlay-card {
  width: min(100%, 360px);
  max-height: 100%;
  display: grid;
  gap: 10px;
  border: 1px solid rgba(216, 208, 195, 0.96);
  border-radius: 8px;
  background: rgba(255, 253, 248, 0.96);
  box-shadow: 0 14px 32px rgba(36, 38, 43, 0.16);
  padding: 14px;
  overflow: auto;
}

.overlay-card h2 {
  margin: 0;
  font-size: 1.35rem;
}

.overlay-eyebrow {
  margin: 0;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 850;
  text-transform: uppercase;
}

.overlay-summary {
  margin: 0;
  color: var(--muted);
  font-weight: 760;
  line-height: 1.45;
}

.overlay-actions {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.share-actions {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.share-actions .button {
  min-width: 0;
  padding-inline: 8px;
}

.side-panel {
  display: grid;
  gap: 12px;
  padding: 14px;
}

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

.stat-grid.compact {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.stat {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
  background: #fff;
}

.stat.primary {
  grid-column: 1 / -1;
  background: #edf7f5;
  border-color: rgba(25, 114, 120, 0.28);
}

.timer-stat {
  background: #fff8e7;
  border-color: rgba(202, 128, 36, 0.34);
}

.timer-stat strong {
  color: #9a4b20;
}

.timer-stat.timer-warning {
  background: #fff1ec;
  border-color: rgba(197, 56, 44, 0.48);
}

.timer-stat.timer-warning strong {
  color: #c4382c;
}

.stat span {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 850;
}

.stat strong {
  display: block;
  margin-top: 1px;
  font-size: 1.05rem;
  line-height: 1.1;
  overflow-wrap: anywhere;
}

.stat.primary strong {
  font-size: 1.55rem;
}

.stat.stat-pop {
  animation: statPop 240ms ease;
}

.stat.stat-soft {
  animation: statSoft 220ms ease;
}

.stat.stat-damage {
  animation: statDamage 320ms ease;
}

.stat.stat-heal {
  animation: statHeal 320ms ease;
}

.field {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 850;
}

.field select {
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 0 10px;
  font-weight: 850;
}

.field input {
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 0 10px;
  font: inherit;
  font-weight: 850;
}

.button-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.button {
  min-height: 44px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 900;
  cursor: pointer;
}

.button.secondary {
  background: #fff;
  color: var(--accent);
}

.button.warn {
  border-color: var(--danger);
  background: #fff;
  color: var(--danger);
}

.message {
  min-height: 44px;
  margin: 0;
  padding: 10px 11px;
  border-radius: 8px;
  background: #edf7f5;
  color: var(--accent);
  font-weight: 850;
  line-height: 1.35;
}

.message.error {
  background: #fff1ed;
  color: var(--danger);
}

.message.pulse-note {
  animation: messageNote 220ms ease;
}

.message.pulse-combo {
  animation: messageCombo 300ms ease;
}

.message.pulse-heal {
  animation: messageHeal 320ms ease;
}

.message.pulse-damage {
  animation: messageDamage 300ms ease;
}

.piece-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 0;
}

.piece-list span {
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  padding: 4px 8px;
  font-size: 0.74rem;
  font-weight: 850;
}

.leaderboard-block {
  display: grid;
  gap: 8px;
  padding-top: 2px;
  border-top: 1px solid rgba(216, 208, 195, 0.8);
}

.leaderboard-title {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: baseline;
  margin: 0;
}

.leaderboard-title strong {
  font-size: 0.96rem;
}

.leaderboard-title span {
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 850;
}

.leaderboard-list {
  display: grid;
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.overlay-card .leaderboard-list {
  max-height: min(240px, 34vh);
  overflow-y: auto;
  padding-right: 2px;
}

.leaderboard-list li {
  display: grid;
  grid-template-columns: 26px minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 7px 9px;
}

.leaderboard-list .rank,
.leaderboard-list .score-value {
  font-weight: 900;
}

.leaderboard-list .rank {
  color: var(--muted);
  font-size: 0.8rem;
}

.leaderboard-list .name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 850;
}

.leaderboard-list .score-value {
  font-size: 0.88rem;
}

.leaderboard-list .own {
  border-color: rgba(25, 114, 120, 0.35);
  background: #edf7f5;
}

.status-note {
  min-height: 1.2em;
  margin: 0;
  color: var(--accent);
  font-size: 0.76rem;
  font-weight: 850;
}

.status-note.error {
  color: var(--danger);
}

.leaderboard-hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.74rem;
  font-weight: 750;
  line-height: 1.45;
}

.rules-panel {
  margin-top: 14px;
  padding: 14px;
}

.rules-panel h2 {
  margin: 0 0 8px;
  font-size: 1.05rem;
}

.rules-panel ul {
  margin: 0;
  padding-left: 1.1rem;
  color: var(--muted);
  font-weight: 720;
  line-height: 1.55;
}

.site-footer {
  justify-content: center;
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

@keyframes boardFlash {
  0% { box-shadow: 0 12px 28px rgba(36, 38, 43, 0.08); }
  45% { box-shadow: 0 16px 34px rgba(246, 189, 96, 0.22); }
  100% { box-shadow: 0 12px 28px rgba(36, 38, 43, 0.08); }
}

@keyframes boardDamage {
  0% { transform: translateX(0); box-shadow: 0 12px 28px rgba(36, 38, 43, 0.08); }
  20% { transform: translateX(-2px); box-shadow: 0 14px 30px rgba(196, 69, 54, 0.12); }
  45% { transform: translateX(2px); }
  70% { transform: translateX(-1px); }
  100% { transform: translateX(0); box-shadow: 0 12px 28px rgba(36, 38, 43, 0.08); }
}

@keyframes statPop {
  0% { transform: scale(1); }
  45% { transform: scale(1.04); }
  100% { transform: scale(1); }
}

@keyframes statSoft {
  0% { transform: scale(1); }
  45% { transform: scale(1.025); }
  100% { transform: scale(1); }
}

@keyframes statDamage {
  0% { transform: scale(1); background: #fff; }
  35% { transform: scale(1.02); background: #fff1ed; }
  100% { transform: scale(1); background: #fff; }
}

@keyframes statHeal {
  0% { transform: scale(1); background: #fff; }
  35% { transform: scale(1.02); background: #edf7f5; }
  100% { transform: scale(1); background: #fff; }
}

@keyframes messageNote {
  0% { transform: translateY(0); }
  40% { transform: translateY(-1px); }
  100% { transform: translateY(0); }
}

@keyframes messageCombo {
  0% { transform: scale(1); }
  40% { transform: scale(1.02); }
  100% { transform: scale(1); }
}

@keyframes messageHeal {
  0% { box-shadow: 0 0 0 rgba(25, 114, 120, 0); }
  45% { box-shadow: 0 0 0 4px rgba(25, 114, 120, 0.1); }
  100% { box-shadow: 0 0 0 rgba(25, 114, 120, 0); }
}

@keyframes messageDamage {
  0% { transform: translateX(0); }
  25% { transform: translateX(-2px); }
  50% { transform: translateX(2px); }
  75% { transform: translateX(-1px); }
  100% { transform: translateX(0); }
}

@media (max-width: 760px) {
  .app {
    width: min(100% - 20px, 430px);
  }

  .top-nav strong {
    flex-basis: 100%;
  }

  .intro-grid,
  .game-shell {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .intro-screen {
    width: 100%;
    min-height: calc(100vh - 112px);
    gap: 12px;
  }

  .intro-card,
  .side-panel {
    gap: 8px;
    padding: 10px;
  }

  .intro-card {
    box-shadow: 0 4px 14px rgba(36, 38, 43, 0.06);
  }

  .board-panel {
    padding: 8px;
  }

  .side-panel {
    grid-template-columns: 1fr;
  }

  .side-panel .play-actions {
    order: 1;
  }

  .side-panel .message {
    order: 2;
  }

  .side-panel .stat-grid {
    order: 3;
  }

  .play-actions {
    grid-template-columns: 1fr 1fr 1fr;
    gap: 6px;
  }

  .message {
    min-height: 40px;
    padding: 8px 10px;
    font-size: 0.9rem;
  }

  .stat-grid.compact {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
  }

  .stat {
    padding: 7px;
  }

  .stat.primary {
    grid-column: span 3;
  }

  .stat.primary strong {
    font-size: 1.35rem;
  }

  .stat strong {
    font-size: 0.96rem;
  }

  .leaderboard-list li {
    padding: 7px 8px;
  }

  .board-overlay {
    padding: 10px;
  }

  .overlay-card {
    box-shadow: 0 10px 18px rgba(36, 38, 43, 0.12);
    padding: 12px;
  }

  .rules-panel {
    margin-top: 10px;
  }
}
