:root {
  color-scheme: light;
  --ink: #20352d;
  --cream: #f5edda;
  --cream-deep: #e8ddc3;
  --leaf: #275b46;
  --leaf-light: #8fb665;
  --berry: #e55244;
  --yellow: #f0bd53;
  --level-accent: #e55244;
  font-family: Inter, ui-rounded, "Avenir Next", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  width: 100%;
  min-width: 320px;
  height: 100%;
  margin: 0;
  overflow: hidden;
  touch-action: none;
}

body {
  color: var(--ink);
  background: var(--cream);
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}

button {
  font: inherit;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

.game-shell {
  position: relative;
  width: 100%;
  height: 100dvh;
}

.topbar {
  position: absolute;
  z-index: 5;
  top: 12px;
  right: clamp(12px, 2vw, 28px);
  left: clamp(12px, 2vw, 28px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 58px;
  padding: 8px 16px;
  border: 1px solid rgba(32, 53, 45, 0.14);
  border-radius: 18px;
  background: rgba(250, 246, 235, 0.78);
  box-shadow: 0 8px 28px rgba(57, 63, 44, 0.08);
  backdrop-filter: blur(12px);
  pointer-events: none;
}

.brand {
  display: flex;
  gap: 12px;
  align-items: center;
  font-size: clamp(15px, 2vw, 20px);
  font-weight: 800;
  letter-spacing: -0.03em;
}

.brand-mark {
  display: grid;
  width: 38px;
  height: 38px;
  place-items: center;
  color: var(--cream);
  font-size: 15px;
  border-radius: 50%;
  background: rgba(39, 91, 70, 0.84);
  backdrop-filter: blur(8px);
  transform: rotate(-8deg);
}

.stats {
  display: flex;
  gap: clamp(18px, 4vw, 52px);
}

.hidden {
  display: none !important;
}

.stat {
  display: grid;
  grid-template-columns: auto auto;
  gap: 0 10px;
  align-items: baseline;
}

.stat-label {
  color: rgba(32, 53, 45, 0.58);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.stat strong {
  min-width: 1.5ch;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 28px;
  font-style: italic;
  line-height: 1;
}

.playfield {
  position: relative;
  width: 100%;
  height: 100dvh;
  min-height: 0;
  overflow: hidden;
  background:
    radial-gradient(circle at 18% 16%, rgba(255, 255, 255, 0.7), transparent 22%),
    linear-gradient(rgba(39, 91, 70, 0.04) 1px, transparent 1px),
    linear-gradient(90deg, rgba(39, 91, 70, 0.04) 1px, transparent 1px),
    var(--field-bg, var(--cream));
  background-size: auto, 42px 42px, 42px 42px, auto;
  transition: background-color 700ms ease;
}

.playfield.theme-berry {
  --level-accent: #a44778;
  --field-bg: #f2e3dd;
}

.playfield.theme-stone {
  --level-accent: #61756a;
  --field-bg: #e7e6d7;
}

.playfield.theme-night {
  --level-accent: #765a9b;
  --field-bg: #e6e1ec;
}

.playfield.theme-festival {
  --level-accent: #d98c2f;
  --field-bg: #f4e3bd;
}

.playfield.theme-mint {
  --level-accent: #4b9b78;
  --field-bg: #dfeee2;
}

.playfield.theme-blueberry {
  --level-accent: #5865a6;
  --field-bg: #e1e2ef;
}

.playfield.theme-autumn {
  --level-accent: #bb6b32;
  --field-bg: #f1dfc5;
}

.playfield.theme-storm {
  --level-accent: #536b78;
  --field-bg: #dce3e5;
}

.playfield.theme-royal {
  --level-accent: #9565a8;
  --field-bg: #eadff0;
}

#game {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.menu-card,
.den-card {
  max-height: calc(100dvh - 28px);
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
}

.overlay {
  position: absolute;
  z-index: 3;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(245, 237, 218, 0.2);
  transition: opacity 280ms ease, visibility 280ms ease;
}

.overlay.hidden {
  visibility: hidden;
  opacity: 0;
  pointer-events: none;
}

.mission-hud {
  position: absolute;
  z-index: 2;
  top: 86px;
  left: 50%;
  display: grid;
  grid-template-columns: auto minmax(110px, 220px) auto;
  gap: 12px;
  align-items: center;
  width: min(680px, calc(100% - 32px));
  padding: 9px 13px;
  border: 1px solid rgba(32, 53, 45, 0.12);
  border-radius: 14px;
  background: rgba(250, 246, 235, 0.8);
  box-shadow: 0 7px 22px rgba(46, 65, 49, 0.08);
  transform: translateX(-50%);
  backdrop-filter: blur(10px);
  pointer-events: none;
}

.mission-copy {
  display: grid;
  min-width: 150px;
}

.mission-copy strong {
  font-size: 13px;
}

.mission-copy span,
.mission-count {
  color: rgba(32, 53, 45, 0.58);
  font-size: 11px;
  font-weight: 700;
}

.mission-progress {
  height: 8px;
  overflow: hidden;
  border-radius: 99px;
  background: rgba(32, 53, 45, 0.12);
}

.mission-progress span {
  display: block;
  width: 0;
  height: 100%;
  border-radius: inherit;
  background: var(--level-accent);
  transition: width 220ms ease;
}

.mission-count {
  min-width: 56px;
  text-align: right;
}

.powerup-hud {
  position: absolute;
  z-index: 2;
  top: 144px;
  left: 50%;
  padding: 8px 14px;
  color: #fffaf0;
  font-size: 12px;
  font-weight: 900;
  border-radius: 99px;
  background: var(--leaf);
  box-shadow: 0 6px 18px rgba(32, 53, 45, 0.16);
  transform: translateX(-50%);
  pointer-events: none;
  max-width: calc(100% - 32px);
  text-align: center;
  white-space: normal;
}

.bonus-toast {
  position: absolute;
  z-index: 3;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 8px 14px;
  color: #fffaf0;
  font-size: 13px;
  font-weight: 950;
  text-align: center;
  border-radius: inherit;
  background: rgba(32, 53, 45, 0.82);
  backdrop-filter: blur(10px);
  animation: hud-notice 1.8s both;
  pointer-events: none;
}

.level-banner {
  position: absolute;
  z-index: 2;
  inset: 0;
  display: grid;
  grid-template-columns: auto auto 1fr;
  gap: 8px;
  align-items: center;
  padding: 8px 14px;
  border-radius: inherit;
  background: rgba(250, 246, 235, 0.9);
  backdrop-filter: blur(10px);
  animation: hud-notice 2.2s both;
  pointer-events: none;
}

.level-banner span {
  color: var(--level-accent);
  font-size: 11px;
  font-weight: 900;
  letter-spacing: 0.15em;
  text-transform: uppercase;
}

.level-banner strong {
  font-family: Georgia, "Times New Roman", serif;
  font-size: 15px;
  font-style: italic;
  font-weight: 700;
}

.level-banner small {
  color: rgba(32, 53, 45, 0.62);
  font-size: 11px;
  text-align: right;
}

@keyframes hud-notice {
  0% { opacity: 0; transform: translateY(-5px); }
  12%, 76% { opacity: 1; transform: translateY(0); }
  100% { opacity: 0; transform: translateY(-3px); }
}

.card {
  position: relative;
  width: min(460px, 100%);
  padding: clamp(28px, 5vw, 52px);
  text-align: center;
  border: 1px solid rgba(32, 53, 45, 0.16);
  border-radius: 30px 8px 30px 8px;
  background: rgba(250, 246, 235, 0.94);
  box-shadow: 0 30px 80px rgba(57, 63, 44, 0.14);
  backdrop-filter: blur(18px);
}

.card.compact {
  width: min(400px, 100%);
}

.card-worm {
  display: flex;
  justify-content: center;
  align-items: flex-end;
  height: 31px;
  margin-bottom: 18px;
}

.card-worm span {
  display: block;
  width: 28px;
  height: 28px;
  margin-left: -6px;
  border-radius: 50%;
  background: var(--leaf-light);
}

.card-worm span:nth-child(2) {
  transform: translateY(-7px);
  background: #80aa59;
}

.card-worm span:nth-child(3) {
  transform: translateY(-2px);
  background: #6c984e;
}

.card-worm span:nth-child(4) {
  position: relative;
  transform: translateY(-10px);
  background: var(--leaf);
}

.card-worm span:nth-child(4)::after {
  position: absolute;
  top: 7px;
  right: 6px;
  width: 4px;
  height: 4px;
  content: "";
  border-radius: 50%;
  background: var(--cream);
  box-shadow: -9px 0 var(--cream);
}

.eyebrow {
  margin: 0 0 10px;
  color: var(--berry);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.15em;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-weight: 500;
  font-style: italic;
  line-height: 0.86;
  letter-spacing: -0.06em;
}

h1 {
  font-size: clamp(54px, 9vw, 88px);
}

h2 {
  font-size: clamp(48px, 8vw, 72px);
}

.intro {
  max-width: 340px;
  margin: 25px auto 24px;
  color: rgba(32, 53, 45, 0.72);
  font-size: 15px;
  line-height: 1.55;
}

.primary-button {
  min-width: 190px;
  padding: 15px 24px;
  color: #fffaf0;
  font-weight: 800;
  border: 0;
  border-radius: 999px;
  background: var(--leaf);
  box-shadow: 0 8px 0 #173e2d;
  cursor: pointer;
  transition: transform 120ms ease, box-shadow 120ms ease, background 120ms ease;
}

.mode-buttons {
  display: grid;
  gap: 13px;
  justify-content: center;
}

.den-link {
  margin: 17px auto 0;
  padding: 7px 10px;
  color: #7a5527;
  font-size: 13px;
  font-weight: 900;
  border: 0;
  border-bottom: 2px dotted rgba(122, 85, 39, 0.35);
  background: transparent;
  cursor: pointer;
}

.den-card {
  width: min(720px, 100%);
  padding: clamp(24px, 4vw, 40px);
  text-align: left;
}

.den-card h2 {
  margin-bottom: 18px;
  text-align: center;
}

.den-card .eyebrow,
.den-card > .primary-button {
  display: block;
  margin-right: auto;
  margin-left: auto;
  text-align: center;
}

.den-wallet {
  width: fit-content;
  margin: 0 auto 8px;
  padding: 8px 15px;
  color: #75441d;
  font-weight: 900;
  border-radius: 99px;
  background: rgba(240, 189, 83, 0.26);
}

.den-note {
  min-height: 20px;
  margin: 0 0 16px;
  color: rgba(32, 53, 45, 0.64);
  font-size: 12px;
  text-align: center;
}

.den-section {
  margin: 16px 0 22px;
}

.den-section h3 {
  margin: 0 0 10px;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 22px;
  font-style: italic;
}

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

.upgrade-card {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 10px;
  align-items: center;
  padding: 11px 12px;
  text-align: left;
  border: 1px solid rgba(32, 53, 45, 0.14);
  border-radius: 13px;
  background: rgba(255, 255, 255, 0.45);
}

.upgrade-card strong,
.upgrade-card small {
  grid-column: 1;
}

.upgrade-card small {
  color: rgba(32, 53, 45, 0.58);
  line-height: 1.3;
}

.upgrade-card button {
  grid-column: 2;
  grid-row: 1 / span 2;
  padding: 8px 10px;
  color: #fffaf0;
  font-size: 12px;
  font-weight: 900;
  border: 0;
  border-radius: 10px;
  background: var(--leaf);
  cursor: pointer;
}

.upgrade-card button:disabled {
  color: rgba(32, 53, 45, 0.5);
  background: rgba(32, 53, 45, 0.11);
  cursor: default;
}

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

.skin-button {
  display: grid;
  gap: 6px;
  place-items: center;
  min-height: 74px;
  padding: 8px 5px;
  color: var(--ink);
  font-size: 11px;
  font-weight: 900;
  border: 2px solid transparent;
  border-radius: 13px;
  background: rgba(255, 255, 255, 0.5);
  cursor: pointer;
}

.skin-button.selected {
  border-color: var(--leaf);
  background: rgba(143, 182, 101, 0.18);
}

.skin-swatch {
  width: 38px;
  height: 22px;
  border: 3px solid rgba(255, 255, 255, 0.65);
  border-radius: 99px;
  box-shadow: 0 2px 7px rgba(32, 53, 45, 0.18);
}

.secondary-button {
  min-width: 190px;
  padding: 13px 22px;
  color: var(--leaf);
  font-weight: 800;
  border: 2px solid rgba(39, 91, 70, 0.38);
  border-radius: 999px;
  background: transparent;
  cursor: pointer;
}

.secondary-button:hover {
  border-color: var(--leaf);
  background: rgba(39, 91, 70, 0.07);
}

.bonus-button {
  min-width: 240px;
  padding: 13px 22px;
  color: #75441d;
  font-weight: 900;
  border: 2px solid rgba(217, 140, 47, 0.55);
  border-radius: 999px;
  background: rgba(240, 189, 83, 0.22);
  cursor: pointer;
  transition: transform 120ms ease, background 120ms ease;
}

.bonus-button:hover {
  background: rgba(240, 189, 83, 0.38);
  transform: translateY(-1px);
}

.network-card h2 {
  margin-bottom: 22px;
}

.network-status {
  min-height: 44px;
  margin: 0 auto 18px;
  color: rgba(32, 53, 45, 0.72);
  line-height: 1.4;
}

.player-list {
  display: grid;
  gap: 8px;
  margin-bottom: 22px;
  text-align: left;
}

.player-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 11px 14px;
  border: 1px solid rgba(32, 53, 45, 0.14);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.42);
}

.player-row strong::before {
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-right: 8px;
  content: "";
  border-radius: 50%;
  background: var(--leaf-light);
}

.player-row.berry strong::before {
  background: var(--berry);
}

.ready-state {
  color: rgba(32, 53, 45, 0.52);
  font-size: 12px;
  font-weight: 800;
}

.ready-state.is-ready {
  color: var(--leaf);
}

.text-button {
  display: block;
  margin: 18px auto 0;
  padding: 5px;
  color: rgba(32, 53, 45, 0.62);
  font-size: 13px;
  font-weight: 700;
  border: 0;
  background: transparent;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.primary-button:disabled {
  background: #8d9d91;
  box-shadow: 0 5px 0 #66746a;
  cursor: default;
  transform: none;
}

.primary-button:hover {
  background: #347357;
  transform: translateY(-2px);
  box-shadow: 0 10px 0 #173e2d;
}

.primary-button:active {
  transform: translateY(6px);
  box-shadow: 0 2px 0 #173e2d;
}

.primary-button:focus-visible,
.secondary-button:focus-visible,
.bonus-button:focus-visible,
.den-link:focus-visible,
.skin-button:focus-visible,
.upgrade-card button:focus-visible,
.text-button:focus-visible,
.sound-button:focus-visible,
.pause-button:focus-visible {
  outline: 3px solid var(--yellow);
  outline-offset: 4px;
}

.controls-hint {
  margin: 23px 0 0;
  color: rgba(32, 53, 45, 0.53);
  font-size: 12px;
  line-height: 1.7;
}

.controls-hint span {
  font-weight: 900;
}

.final-score {
  margin: 25px 0;
  color: rgba(32, 53, 45, 0.72);
}

.sound-button,
.pause-button {
  position: absolute;
  z-index: 4;
  right: 22px;
  bottom: 20px;
  display: grid;
  width: 46px;
  height: 46px;
  place-items: center;
  color: var(--ink);
  font-size: 21px;
  font-weight: 900;
  border: 1px solid rgba(32, 53, 45, 0.2);
  border-radius: 50%;
  background: rgba(250, 246, 235, 0.82);
  cursor: pointer;
}

.pause-button {
  right: 78px;
  letter-spacing: -0.12em;
}

.sound-button.muted {
  color: rgba(32, 53, 45, 0.35);
  text-decoration: line-through;
}

.mobile-tip {
  position: absolute;
  z-index: 2;
  bottom: 24px;
  left: 50%;
  display: none;
  padding: 9px 14px;
  color: rgba(32, 53, 45, 0.64);
  font-size: 12px;
  font-weight: 800;
  border: 1px solid rgba(32, 53, 45, 0.12);
  border-radius: 99px;
  background: rgba(250, 246, 235, 0.72);
  transform: translateX(-50%);
  pointer-events: none;
}

.turn-controls {
  position: absolute;
  z-index: 2;
  right: 50%;
  bottom: 58px;
  left: 50%;
  display: none;
  justify-content: space-between;
  width: min(330px, calc(100% - 42px));
  transform: translateX(-50%);
  pointer-events: none;
}

.turn-button {
  display: grid;
  width: 72px;
  height: 72px;
  place-items: center;
  color: var(--ink);
  font-size: 37px;
  border: 1px solid rgba(32, 53, 45, 0.2);
  border-radius: 50%;
  background: rgba(250, 246, 235, 0.78);
  box-shadow: 0 6px 18px rgba(46, 65, 49, 0.12);
  backdrop-filter: blur(8px);
  pointer-events: auto;
  touch-action: none;
}

.turn-button.active {
  color: #fffaf0;
  background: var(--leaf);
  transform: scale(0.94);
}

@media (pointer: coarse), (max-width: 700px) {
  .topbar {
    top: 8px;
    right: 8px;
    left: 8px;
    min-height: 50px;
    padding: 6px 10px;
    border-radius: 15px;
  }

  .brand span:last-child,
  .stat-label {
    display: none;
  }

  .brand-mark {
    width: 34px;
    height: 34px;
  }

  .stat strong {
    font-size: 23px;
  }

  .stat::before {
    color: rgba(32, 53, 45, 0.52);
    font-size: 15px;
    content: "●";
  }

  .stat:nth-child(2)::before {
    content: "★";
  }

  .stat:nth-child(3)::before {
    content: "¤";
  }

  .stat:nth-child(4)::before {
    content: "◆";
  }

  .stat:nth-child(5)::before {
    content: "≈";
  }

  .stats {
    gap: 9px;
  }

  .mission-hud {
    top: 67px;
    grid-template-columns: 1fr auto;
    gap: 6px 10px;
    padding: 8px 11px;
  }

  .mission-copy {
    min-width: 0;
  }

  .mission-copy span {
    display: none;
  }

  .mission-progress {
    grid-column: 1 / -1;
    grid-row: 2;
  }

  .mission-count {
    grid-column: 2;
    grid-row: 1;
  }

  .powerup-hud {
    top: 120px;
  }

  .level-banner {
    grid-template-columns: auto 1fr;
    gap: 6px;
    padding: 7px 10px;
  }

  .level-banner small {
    display: none;
  }

  .bonus-toast {
    padding: 7px 10px;
    font-size: 12px;
  }

  .overlay {
    padding: 14px;
  }

  .card {
    padding: 28px 24px;
  }

  .den-card {
    padding: 22px 17px;
  }

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

  .skin-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .intro {
    margin-top: 19px;
  }

  .controls-hint {
    margin-top: 18px;
  }

  .mobile-tip {
    display: block;
    bottom: calc(126px + env(safe-area-inset-bottom));
  }

  .playfield.is-playing .turn-controls {
    display: flex;
  }

  .turn-controls {
    bottom: calc(16px + env(safe-area-inset-bottom));
    width: calc(100% - 24px);
  }

  .turn-button {
    width: 92px;
    height: 92px;
    font-size: 47px;
    border-width: 2px;
    background: rgba(250, 246, 235, 0.94);
    box-shadow: 0 8px 26px rgba(46, 65, 49, 0.2);
  }

  .sound-button,
  .pause-button {
    bottom: calc(126px + env(safe-area-inset-bottom));
  }
}

@media (max-height: 660px) {
  .card-worm,
  .controls-hint {
    display: none;
  }

  .card {
    padding-top: 23px;
    padding-bottom: 23px;
  }

  h1 {
    font-size: 52px;
  }

  .intro {
    margin: 16px auto;
  }

  .mission-hud {
    opacity: 0.82;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
  }
}
