:root {
  color-scheme: light;
  --page: #f5f7fb;
  --surface: #ffffff;
  --surface-strong: #eef2f8;
  --ink: #17191f;
  --muted: #646b7a;
  --line: #d9dfeb;
  --maze: #11131a;
  --wall: #26304d;
  --wall-edge: #4d75d4;
  --accent: #c3002f;
  --gold: #ffd100;
  --green: #00945e;
  --focus: #1f5eff;
  --shadow: 0 18px 45px rgba(23, 25, 31, 0.14);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-width: 320px;
  background: var(--page);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  width: min(1520px, 100%);
  margin: 0 auto;
  padding: 18px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 72px;
  padding: 14px 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

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

.brand-mark {
  position: relative;
  width: 46px;
  height: 52px;
  flex: 0 0 auto;
  --brand-ghost-primary: var(--accent);
  --brand-ghost-secondary: var(--gold);
}

.brand-ghost-body {
  position: absolute;
  inset: 0;
  background: var(--accent);
  box-shadow: inset -10px 0 0 var(--gold);
  clip-path: polygon(0 28%, 8% 9%, 24% 0, 76% 0, 92% 9%, 100% 28%, 100% 100%, 84% 86%, 68% 100%, 50% 86%, 32% 100%, 16% 86%, 0 100%);
  filter: drop-shadow(0 4px 7px rgba(23, 25, 31, 0.18));
  animation: ghostHue 9s ease-in-out infinite;
}

.brand-ghost-body::before,
.brand-ghost-body::after {
  position: absolute;
  top: 14px;
  width: 7px;
  height: 7px;
  content: "";
  border-radius: 50%;
  background: #ffffff;
}

.brand-ghost-body::before {
  left: 13px;
}

.brand-ghost-body::after {
  right: 13px;
}

.brand-shield {
  position: absolute;
  left: 50%;
  bottom: 6px;
  display: none;
  width: 23px;
  height: 27px;
  object-fit: contain;
  transform: translateX(-50%);
  filter: drop-shadow(0 2px 3px rgba(23, 25, 31, 0.22));
}

.brand-mark.is-contrada-color .brand-ghost-body {
  animation: none;
  background: linear-gradient(180deg, #fffdf4 0%, #dfe3ea 100%);
  box-shadow: none;
}

.brand-mark.is-contrada-color .brand-ghost-body::before,
.brand-mark.is-contrada-color .brand-ghost-body::after {
  background: #2b2f38;
}

.brand-mark.is-contrada-color .brand-shield {
  display: block;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: 1.42rem;
  line-height: 1.05;
  overflow-wrap: anywhere;
}

h2 {
  font-size: 1.1rem;
  line-height: 1.2;
}

@keyframes ghostHue {
  0%,
  100% {
    background: #c3002f;
    box-shadow: inset -10px 0 0 #ff6f76;
  }

  33% {
    background: #ffd100;
    box-shadow: inset -10px 0 0 #fff0a8;
  }

  66% {
    background: #00945e;
    box-shadow: inset -10px 0 0 #8bd46e;
  }

  84% {
    background: #1f5eff;
    box-shadow: inset -10px 0 0 #7daed3;
  }
}

.status-pill {
  min-width: 96px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface-strong);
  color: var(--ink);
  font-size: 0.9rem;
  font-weight: 800;
  text-align: center;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.sound-toggle {
  min-width: 72px;
  min-height: 36px;
  padding: 7px 11px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #ffffff;
  color: var(--ink);
  font-size: 0.78rem;
  font-weight: 900;
}

.sound-toggle[aria-pressed="false"] {
  background: var(--surface-strong);
  color: var(--muted);
}

.sound-toggle:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(190px, 220px) minmax(780px, 1fr);
  gap: 18px;
  align-items: start;
  margin-top: 18px;
}

.setup-panel,
.stage-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.setup-panel {
  padding: 16px;
}

.setup-panel.is-locked {
  pointer-events: none;
}

.setup-panel.is-locked .setup-toggle,
.setup-panel.is-locked input,
.setup-panel.is-locked .contrada-card {
  cursor: default;
}

.setup-toggle {
  display: none;
}

.setup-body {
  display: block;
}

.panel-heading {
  margin-bottom: 16px;
}

.field-label {
  display: grid;
  gap: 8px;
  margin-bottom: 16px;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 800;
}

input {
  width: 100%;
  min-height: 44px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--ink);
  outline: none;
}

input:focus-visible,
button:focus-visible {
  outline: 3px solid rgba(31, 94, 255, 0.24);
  outline-offset: 2px;
  border-color: var(--focus);
}

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

.contrada-card {
  position: relative;
  display: grid;
  grid-template-rows: 74px auto;
  gap: 7px;
  min-height: 126px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  text-align: center;
}

.contrada-card:disabled {
  color: var(--ink);
}

.contrada-card.is-locked {
  cursor: default;
}

.contrada-card.is-eliminated {
  cursor: not-allowed;
  opacity: 0.66;
}

.contrada-card.is-qualified {
  border-color: rgba(0, 148, 94, 0.72);
  box-shadow: 0 0 0 2px rgba(0, 148, 94, 0.12);
}

.contrada-card.is-eliminated::after {
  position: absolute;
  inset: 6px;
  content: "";
  border: 2px solid rgba(195, 0, 47, 0.72);
  border-radius: 8px;
  pointer-events: none;
}

.contrada-eliminated {
  position: absolute;
  top: 50%;
  left: 50%;
  z-index: 2;
  color: #c3002f;
  font-size: 4.8rem;
  font-weight: 950;
  line-height: 1;
  text-shadow:
    0 2px 0 #ffffff,
    0 0 14px rgba(195, 0, 47, 0.35);
  transform: translate(-50%, -54%) rotate(-9deg);
  pointer-events: none;
}

.contrada-qualified {
  position: absolute;
  top: 6px;
  right: 6px;
  z-index: 2;
  display: grid;
  width: 28px;
  height: 28px;
  place-items: center;
  border: 2px solid #ffffff;
  border-radius: 999px;
  background: #00945e;
  color: #ffffff;
  font-size: 1.05rem;
  font-weight: 950;
  line-height: 1;
  box-shadow: 0 8px 18px rgba(0, 148, 94, 0.28);
  pointer-events: none;
}

.contrada-card[aria-checked="true"] {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(195, 0, 47, 0.12);
}

.contrada-card.is-qualified[aria-checked="true"] {
  border-color: rgba(0, 148, 94, 0.72);
  box-shadow: 0 0 0 2px rgba(0, 148, 94, 0.12);
}

.contrada-ghost {
  position: relative;
  display: block;
  width: 64px;
  height: 72px;
  align-self: end;
  justify-self: center;
  min-height: 0;
}

.contrada-ghost-body {
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, #fffdf5 0%, #dfe3eb 100%);
  clip-path: polygon(0 28%, 8% 9%, 24% 0, 76% 0, 92% 9%, 100% 28%, 100% 100%, 84% 86%, 68% 100%, 50% 86%, 32% 100%, 16% 86%, 0 100%);
  filter: drop-shadow(0 5px 8px rgba(23, 25, 31, 0.16));
}

.contrada-ghost-body::before,
.contrada-ghost-body::after {
  position: absolute;
  top: 17px;
  width: 7px;
  height: 7px;
  content: "";
  border-radius: 50%;
  background: #2b2f38;
}

.contrada-ghost-body::before {
  left: 18px;
}

.contrada-ghost-body::after {
  right: 18px;
}

.contrada-shield {
  position: absolute;
  left: 50%;
  bottom: 7px;
  width: 36px;
  height: 42px;
  object-fit: contain;
  transform: translateX(-50%);
  filter: drop-shadow(0 2px 3px rgba(23, 25, 31, 0.2));
}

.contrada-name {
  min-height: 32px;
  font-size: 0.74rem;
  font-weight: 800;
  line-height: 1.15;
  overflow-wrap: anywhere;
}

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  min-height: 44px;
  padding: 10px 14px;
  border: 1px solid transparent;
  border-radius: 8px;
  font-weight: 900;
}

.button-secondary {
  background: #ffffff;
  color: var(--ink);
  border-color: var(--line);
}

.stage-panel {
  overflow: hidden;
}

.scorebar {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 1px;
  background: var(--line);
  border-bottom: 1px solid var(--line);
}

.score-item {
  display: grid;
  gap: 3px;
  min-width: 0;
  padding: 12px;
  background: var(--surface);
}

.score-item span {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 800;
  text-transform: uppercase;
}

.score-item strong {
  min-height: 24px;
  font-size: 1.25rem;
  line-height: 1.1;
  overflow-wrap: anywhere;
}

.canvas-frame {
  position: relative;
  display: grid;
  place-items: center;
  background: var(--maze);
  padding: 14px;
}

canvas {
  display: block;
  width: min(100%, 672px);
  height: auto;
  aspect-ratio: 672 / 736;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  background: var(--maze);
  touch-action: none;
}

.game-overlay {
  position: absolute;
  inset: 14px;
  display: grid;
  place-items: center;
  padding: 20px;
  border-radius: 8px;
  background: rgba(17, 19, 26, 0.82);
  color: #ffffff;
  text-align: center;
  z-index: 10;
}

.game-overlay.is-hidden {
  display: none;
}

.game-overlay h2 {
  margin-bottom: 8px;
  font-size: 1.45rem;
}

.game-overlay p:not(.eyebrow) {
  margin: 0 0 18px;
  color: rgba(255, 255, 255, 0.78);
}

.game-overlay.is-countdown {
  background: rgba(17, 19, 26, 0.66);
}

.game-overlay.is-countdown h2 {
  min-width: min(70vw, 360px);
  margin-bottom: 10px;
  color: #ffffff;
  font-size: clamp(4.8rem, 18vw, 9rem);
  font-weight: 950;
  line-height: 0.95;
  text-shadow:
    0 12px 34px rgba(0, 0, 0, 0.5),
    0 0 28px rgba(255, 209, 0, 0.5);
  animation: countdownPop 680ms cubic-bezier(0.18, 0.86, 0.22, 1.18) both;
}

.game-overlay.is-countdown p:not(.eyebrow) {
  margin: 0;
  color: rgba(255, 255, 255, 0.9);
  font-size: 1rem;
  font-weight: 800;
}

.game-overlay.is-countdown .button {
  display: none;
}

.game-overlay.is-respawn {
  background: rgba(17, 19, 26, 0.58);
  pointer-events: none;
}

.game-overlay.is-respawn h2 {
  margin-bottom: 8px;
  color: #ffffff;
  font-size: clamp(3.8rem, 12vw, 6.5rem);
  font-weight: 950;
  line-height: 1;
  text-shadow:
    0 12px 28px rgba(0, 0, 0, 0.55),
    0 0 22px rgba(255, 209, 0, 0.42);
}

.game-overlay.is-respawn p:not(.eyebrow) {
  margin: 0;
  color: rgba(255, 255, 255, 0.9);
  font-size: 1rem;
  font-weight: 800;
}

.game-overlay.is-respawn .button {
  display: none;
}

.overlay-winner {
  position: relative;
  display: grid;
  width: min(44vw, 190px);
  aspect-ratio: 1;
  place-items: center;
  margin: 0 auto 18px;
}

.overlay-winner[hidden] {
  display: none;
}

.overlay-winner::before,
.overlay-winner::after {
  position: absolute;
  inset: 0;
  content: "";
  border-radius: 50%;
  pointer-events: none;
}

.overlay-winner::before {
  background: conic-gradient(
    from 0deg,
    rgba(255, 255, 255, 0),
    rgba(255, 222, 118, 0.95),
    rgba(255, 255, 255, 0),
    rgba(136, 220, 255, 0.72),
    rgba(255, 255, 255, 0)
  );
  filter: blur(8px);
  opacity: 0.8;
  animation: winnerSpark 2.4s linear infinite;
}

.overlay-winner::after {
  inset: 14px;
  background:
    radial-gradient(circle at 22% 22%, rgba(255, 255, 255, 0.95) 0 2px, transparent 3px),
    radial-gradient(circle at 76% 34%, rgba(255, 236, 153, 0.95) 0 2px, transparent 3px),
    radial-gradient(circle at 58% 82%, rgba(153, 224, 255, 0.9) 0 2px, transparent 3px);
  animation: winnerTwinkle 1.45s ease-in-out infinite alternate;
}

.overlay-shield {
  position: relative;
  z-index: 1;
  width: min(82%, 156px);
  max-height: 156px;
  object-fit: contain;
  filter: drop-shadow(0 18px 28px rgba(0, 0, 0, 0.42)) drop-shadow(0 0 18px rgba(255, 224, 116, 0.5));
  transform-origin: center;
  animation: winnerShieldPop 520ms cubic-bezier(0.2, 0.9, 0.22, 1.25) both;
}

.touch-controls {
  display: none;
}

@keyframes winnerSpark {
  from {
    transform: rotate(0deg) scale(0.98);
  }

  to {
    transform: rotate(360deg) scale(0.98);
  }
}

@keyframes winnerTwinkle {
  from {
    opacity: 0.45;
    transform: scale(0.92);
  }

  to {
    opacity: 1;
    transform: scale(1.06);
  }
}

@keyframes winnerShieldPop {
  from {
    opacity: 0;
    transform: scale(0.58);
  }

  to {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes countdownPop {
  0% {
    opacity: 0;
    transform: scale(0.62);
  }

  36% {
    opacity: 1;
    transform: scale(1.08);
  }

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

@media (max-width: 1160px) {
  .game-layout {
    grid-template-columns: 220px minmax(520px, 1fr);
  }
}

@media (max-width: 820px) {
  :root {
    --mobile-topbar-height: 40px;
    --mobile-setup-height: 32px;
    --mobile-scorebar-height: 28px;
    --touch-control-size: 44px;
  }

  .app-shell {
    min-height: 100svh;
    padding: 0;
  }

  .topbar,
  .game-layout {
    display: grid;
  }

  .topbar {
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    min-height: var(--mobile-topbar-height);
    padding: 4px 8px;
    border-width: 0 0 1px;
    border-radius: 0;
    box-shadow: none;
  }

  .brand {
    gap: 8px;
  }

  .brand-mark {
    width: 29px;
    height: 33px;
  }

  .brand-ghost-body {
    box-shadow: inset -6px 0 0 var(--gold);
    animation-name: ghostHueMobile;
  }

  .brand-ghost-body::before,
  .brand-ghost-body::after {
    top: 10px;
    width: 5px;
    height: 5px;
  }

  .brand-ghost-body::before {
    left: 9px;
  }

  .brand-ghost-body::after {
    right: 9px;
  }

  .brand-shield {
    bottom: 4px;
    width: 16px;
    height: 19px;
  }

  .brand-mark.is-contrada-color .brand-ghost-body {
    animation: none;
    box-shadow: none;
  }

  h1 {
    font-size: 0.96rem;
    line-height: 1.05;
  }

  .status-pill {
    min-width: 62px;
    padding: 4px 7px;
    font-size: 0.68rem;
  }

  .topbar-actions {
    gap: 5px;
  }

  .sound-toggle {
    min-width: 46px;
    min-height: 26px;
    padding: 3px 6px;
    font-size: 0.62rem;
  }

  .game-layout {
    grid-template-columns: 1fr;
    gap: 0;
    margin-top: 0;
  }

  .setup-panel,
  .stage-panel {
    border-right: 0;
    border-left: 0;
    border-radius: 0;
    box-shadow: none;
  }

  .setup-panel {
    position: relative;
    z-index: 20;
    padding: 0;
  }

  .setup-toggle {
    position: relative;
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: 8px;
    align-items: center;
    width: 100%;
    min-height: var(--mobile-setup-height);
    padding: 4px 10px;
    border: 0;
    border-bottom: 1px solid var(--line);
    border-radius: 0;
    background: var(--surface);
    color: var(--ink);
    text-align: left;
  }

  .setup-toggle::after {
    content: "";
    width: 9px;
    height: 9px;
    border-right: 2px solid currentColor;
    border-bottom: 2px solid currentColor;
    transform: rotate(45deg) translateY(-2px);
    transition: transform 150ms ease;
  }

  .setup-panel.is-expanded .setup-toggle::after {
    transform: rotate(225deg) translate(-2px, -1px);
  }

  .setup-toggle span {
    color: var(--muted);
    font-size: 0.58rem;
    font-weight: 900;
    text-transform: uppercase;
  }

  .setup-toggle strong {
    min-width: 0;
    overflow: hidden;
    font-size: 0.76rem;
    line-height: 1.1;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .setup-body {
    position: absolute;
    top: 100%;
    right: 0;
    left: 0;
    max-height: min(72svh, 560px);
    padding: 12px;
    overflow: auto;
    border-bottom: 1px solid var(--line);
    background: var(--surface);
    box-shadow: 0 18px 34px rgba(23, 25, 31, 0.18);
  }

  .setup-panel:not(.is-expanded) .setup-body {
    display: none;
  }

  .setup-body .panel-heading {
    margin-bottom: 10px;
  }

  .field-label {
    margin-bottom: 10px;
  }

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

  .contrada-card {
    grid-template-rows: 48px auto;
    min-height: 88px;
    padding: 6px;
  }

  .contrada-eliminated {
    font-size: 3.6rem;
  }

  .contrada-qualified {
    width: 22px;
    height: 22px;
    font-size: 0.82rem;
  }

  .contrada-ghost {
    width: 42px;
    height: 48px;
  }

  .contrada-ghost-body::before,
  .contrada-ghost-body::after {
    top: 12px;
    width: 5px;
    height: 5px;
  }

  .contrada-ghost-body::before {
    left: 12px;
  }

  .contrada-ghost-body::after {
    right: 12px;
  }

  .contrada-shield {
    bottom: 5px;
    width: 24px;
    height: 28px;
  }

  .contrada-name {
    min-height: 24px;
    font-size: 0.62rem;
  }

  .scorebar {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    min-height: var(--mobile-scorebar-height);
  }

  .score-item {
    display: flex;
    gap: 5px;
    align-items: center;
    justify-content: center;
    padding: 2px 4px;
  }

  .score-item span {
    font-size: 0.52rem;
  }

  .score-item strong {
    min-height: 0;
    font-size: 0.78rem;
    line-height: 1;
  }

  .canvas-frame {
    min-height: calc(100svh - var(--mobile-topbar-height) - var(--mobile-setup-height) - var(--mobile-scorebar-height));
    padding: 0;
  }

  canvas {
    width: 100%;
    max-height: none;
    border: 0;
    border-radius: 0;
  }

  .game-overlay {
    inset: 0;
    border-radius: 0;
  }

  .touch-controls {
    position: absolute;
    right: 50%;
    bottom: calc(8px + env(safe-area-inset-bottom));
    z-index: 8;
    display: grid;
    grid-template-columns: repeat(3, var(--touch-control-size));
    grid-template-rows: repeat(2, var(--touch-control-size));
    gap: 7px;
    transform: translateX(50%);
    pointer-events: none;
  }

  .touch-control {
    width: var(--touch-control-size);
    height: var(--touch-control-size);
    min-height: 0;
    padding: 0;
    border: 1px solid rgba(255, 255, 255, 0.3);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.84);
    color: #17191f;
    box-shadow: 0 8px 18px rgba(0, 0, 0, 0.28);
    font-size: 1.35rem;
    font-weight: 900;
    line-height: 1;
    pointer-events: auto;
    touch-action: manipulation;
    user-select: none;
  }

  .touch-control:active {
    background: #ffd100;
    transform: translateY(1px);
  }

  .touch-control-up {
    grid-column: 2;
    grid-row: 1;
  }

  .touch-control-left {
    grid-column: 1;
    grid-row: 2;
  }

  .touch-control-down {
    grid-column: 2;
    grid-row: 2;
  }

  .touch-control-right {
    grid-column: 3;
    grid-row: 2;
  }

}

@media (max-width: 520px) {
  .brand {
    gap: 7px;
  }

  .brand-mark {
    width: 27px;
    height: 31px;
  }

  .brand-ghost-body {
    box-shadow: inset -5px 0 0 var(--gold);
    animation-name: ghostHueMobile;
  }

  .brand-ghost-body::before,
  .brand-ghost-body::after {
    top: 9px;
    width: 5px;
    height: 5px;
  }

  .brand-ghost-body::before {
    left: 8px;
  }

  .brand-ghost-body::after {
    right: 8px;
  }

  .brand-shield {
    bottom: 4px;
    width: 15px;
    height: 18px;
  }

  h1 {
    font-size: 0.92rem;
    line-height: 1.1;
  }

  .status-pill {
    min-width: 56px;
    padding: 3px 6px;
    font-size: 0.64rem;
  }

  .sound-toggle {
    min-width: 42px;
    padding: 3px 5px;
    font-size: 0.6rem;
  }

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

@keyframes ghostHueMobile {
  0%,
  100% {
    background: #c3002f;
    box-shadow: inset -6px 0 0 #ff6f76;
  }

  33% {
    background: #ffd100;
    box-shadow: inset -6px 0 0 #fff0a8;
  }

  66% {
    background: #00945e;
    box-shadow: inset -6px 0 0 #8bd46e;
  }

  84% {
    background: #1f5eff;
    box-shadow: inset -6px 0 0 #7daed3;
  }
}

@media (max-width: 820px) and (orientation: landscape) {
  :root {
    --touch-control-size: 38px;
  }

  canvas {
    width: auto;
    max-width: 100%;
    height: calc(100svh - var(--mobile-topbar-height) - var(--mobile-setup-height) - var(--mobile-scorebar-height));
  }

  .touch-controls {
    right: calc(10px + env(safe-area-inset-right));
    bottom: calc(8px + env(safe-area-inset-bottom));
    transform: none;
  }
}
