:root {
  color-scheme: dark;
  --void: #080908;
  --coal: #111311;
  --panel: rgba(18, 20, 18, 0.84);
  --panel-strong: rgba(27, 29, 25, 0.94);
  --line: rgba(244, 238, 215, 0.16);
  --text: #f5efd9;
  --muted: #a9ad9d;
  --x: #ff5a5f;
  --o: #21d0b2;
  --gold: #f0bc42;
  --leaf: #9dd35d;
  --violet: #9a7cff;
  --shadow: rgba(0, 0, 0, 0.34);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  background:
    radial-gradient(circle at 16% 18%, rgba(33, 208, 178, 0.16), transparent 24rem),
    radial-gradient(circle at 84% 8%, rgba(255, 90, 95, 0.15), transparent 22rem),
    linear-gradient(135deg, #080908 0%, #14130f 52%, #0a1110 100%);
  color: var(--text);
  letter-spacing: 0;
  overflow-x: hidden;
}

button,
input {
  font: inherit;
}

button {
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

#fieldCanvas {
  position: fixed;
  inset: 0;
  z-index: -1;
  width: 100%;
  height: 100%;
}

.app-shell {
  width: min(1220px, calc(100% - 32px));
  min-height: 100vh;
  margin: 0 auto;
  padding: 22px 0 28px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 72px;
}

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

.brand-sigil {
  display: grid;
  place-items: center;
  width: 54px;
  height: 54px;
  border: 1px solid rgba(240, 188, 66, 0.52);
  border-radius: 8px;
  background:
    linear-gradient(145deg, rgba(255, 90, 95, 0.28), rgba(33, 208, 178, 0.22)),
    rgba(0, 0, 0, 0.28);
  color: #fff8df;
  font-size: 1.2rem;
  font-weight: 900;
  box-shadow: 0 16px 32px var(--shadow), inset 0 0 28px rgba(240, 188, 66, 0.14);
}

.brand h1 {
  margin: 0;
  font-size: 1.35rem;
  line-height: 1.1;
}

.brand p {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 0.92rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 56vw;
}

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

.icon-button,
.power-button,
.segmented button,
.room-actions button,
.join-form button {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(245, 239, 217, 0.08);
  color: var(--text);
  transition: transform 160ms ease, border-color 160ms ease, background 160ms ease, box-shadow 160ms ease;
}

.icon-button {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  font-size: 1.25rem;
}

.icon-button:hover,
.power-button:hover,
.segmented button:hover,
.room-actions button:hover,
.join-form button:hover {
  transform: translateY(-1px);
  border-color: rgba(240, 188, 66, 0.7);
  background: rgba(245, 239, 217, 0.13);
}

.game-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 18px;
  align-items: stretch;
  margin-top: 18px;
}

.arena-panel,
.side-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 24px 80px var(--shadow);
  backdrop-filter: blur(18px);
}

.arena-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 16px;
  min-height: 720px;
  padding: 18px;
}

.score-strip {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 10px;
  align-items: stretch;
}

.score-block,
.turn-core {
  min-height: 72px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(8, 9, 8, 0.38);
}

.score-block {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 18px;
}

.score-block span {
  font-size: 1.4rem;
  font-weight: 900;
}

.score-block strong {
  font-size: 1.8rem;
}

.score-x span {
  color: var(--x);
}

.score-o span {
  color: var(--o);
}

.turn-core {
  display: grid;
  place-items: center;
  min-width: 178px;
  padding: 10px 18px;
  text-align: center;
}

.turn-core span {
  color: var(--muted);
  font-size: 0.84rem;
  text-transform: uppercase;
}

.turn-core strong {
  display: block;
  margin-top: 4px;
  font-size: 1.25rem;
}

.board-frame {
  display: grid;
  place-items: center;
  min-height: 0;
  padding: 10px;
}

.board {
  --size: 3;
  display: grid;
  grid-template-columns: repeat(var(--size), minmax(0, 1fr));
  grid-template-rows: repeat(var(--size), minmax(0, 1fr));
  grid-auto-rows: minmax(0, 1fr);
  gap: 10px;
  width: min(640px, 100%);
  aspect-ratio: 1;
  padding: 10px;
  border: 1px solid rgba(245, 239, 217, 0.12);
  border-radius: 8px;
  background:
    linear-gradient(90deg, rgba(245, 239, 217, 0.05) 1px, transparent 1px),
    linear-gradient(0deg, rgba(245, 239, 217, 0.05) 1px, transparent 1px),
    rgba(0, 0, 0, 0.24);
  background-size: 28px 28px;
  box-shadow: inset 0 0 80px rgba(33, 208, 178, 0.08);
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  padding: 0;
  border: 1px solid rgba(245, 239, 217, 0.14);
  border-radius: 8px;
  background:
    radial-gradient(circle at 50% 20%, rgba(245, 239, 217, 0.1), transparent 52%),
    rgba(18, 20, 18, 0.68);
  color: var(--text);
  overflow: hidden;
  isolation: isolate;
  appearance: none;
  line-height: 1;
  contain: layout paint;
}

.cell:disabled {
  cursor: default;
  opacity: 1;
}

.cell.empty:disabled {
  cursor: not-allowed;
}

.cell::before {
  content: "";
  position: absolute;
  inset: 9px;
  border: 1px solid rgba(245, 239, 217, 0.08);
  border-radius: 7px;
}

.cell::after {
  content: "";
  position: absolute;
  inset: auto 12% 10% 12%;
  height: 4px;
  border-radius: 999px;
  background: transparent;
  opacity: 0;
}

.cell.empty:hover {
  transform: translateY(-2px);
  border-color: rgba(240, 188, 66, 0.7);
  background:
    radial-gradient(circle at 50% 20%, rgba(240, 188, 66, 0.14), transparent 52%),
    rgba(28, 27, 20, 0.76);
}

.cell.spark {
  border-color: rgba(240, 188, 66, 0.66);
  box-shadow: inset 0 0 22px rgba(240, 188, 66, 0.12), 0 0 24px rgba(240, 188, 66, 0.1);
}

.cell.spark::after {
  background: linear-gradient(90deg, transparent, var(--gold), transparent);
  opacity: 1;
}

.cell.winner {
  border-color: rgba(157, 211, 93, 0.84);
  box-shadow: 0 0 0 1px rgba(157, 211, 93, 0.34), 0 0 34px rgba(157, 211, 93, 0.2);
}

.cell.targetable {
  border-color: rgba(255, 90, 95, 0.84);
  box-shadow: inset 0 0 24px rgba(255, 90, 95, 0.16);
}

.cell.oldest {
  opacity: 0.72;
}

.mark {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  z-index: 1;
  font-size: 5.25rem;
  line-height: 1;
  font-weight: 900;
  pointer-events: none;
  text-shadow: 0 12px 30px rgba(0, 0, 0, 0.44);
}

.mark.fresh {
  animation: markIn 320ms cubic-bezier(0.2, 0.9, 0.18, 1) both;
}

.mark-x.fresh {
  animation-name: crossIn;
}

.board.size-4 .mark {
  font-size: 3.8rem;
}

.mark-x {
  color: var(--x);
}

.mark-o {
  color: var(--o);
}

.power-row {
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: 12px;
  align-items: stretch;
}

.power-button {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  min-height: 58px;
  padding: 0 14px;
  text-align: left;
}

.power-button.active {
  border-color: var(--gold);
  box-shadow: inset 0 0 28px rgba(240, 188, 66, 0.16);
}

.power-button strong {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 999px;
  background: rgba(240, 188, 66, 0.18);
  color: var(--gold);
}

.power-icon {
  color: var(--gold);
  font-size: 1.25rem;
}

.energy-bank {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  background: rgba(8, 9, 8, 0.38);
}

.energy-bank div {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 10px;
}

.energy-bank span {
  font-weight: 900;
}

meter {
  width: 100%;
  height: 14px;
}

meter::-webkit-meter-bar {
  border: 0;
  border-radius: 999px;
  background: rgba(245, 239, 217, 0.1);
}

meter::-webkit-meter-optimum-value {
  border-radius: 999px;
  background: linear-gradient(90deg, var(--leaf), var(--gold));
}

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

.panel-section {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(8, 9, 8, 0.34);
  padding: 13px;
}

.section-label {
  margin-bottom: 10px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 800;
  text-transform: uppercase;
}

.segmented {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
}

.segmented button,
.room-actions button,
.join-form button {
  min-height: 42px;
  padding: 0 12px;
}

.segmented button.active {
  border-color: var(--o);
  background: rgba(33, 208, 178, 0.16);
}

.room-code {
  display: grid;
  place-items: center;
  min-height: 62px;
  border: 1px dashed rgba(240, 188, 66, 0.45);
  border-radius: 8px;
  background: rgba(240, 188, 66, 0.08);
  color: var(--gold);
  font-size: 1.7rem;
  font-weight: 900;
  letter-spacing: 0.12rem;
}

.room-actions,
.join-form {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 10px;
}

.join-form {
  grid-template-columns: minmax(0, 1fr) auto;
}

.join-form input {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(245, 239, 217, 0.08);
  color: var(--text);
  padding: 0 12px;
  text-transform: uppercase;
}

.status-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 0;
}

.status-grid div {
  min-width: 0;
  border: 1px solid rgba(245, 239, 217, 0.1);
  border-radius: 8px;
  padding: 10px;
  background: rgba(245, 239, 217, 0.05);
}

.status-grid dt {
  color: var(--muted);
  font-size: 0.78rem;
}

.status-grid dd {
  margin: 4px 0 0;
  font-size: 1.05rem;
  font-weight: 850;
}

.event-feed {
  display: grid;
  gap: 8px;
  height: 236px;
  min-height: 0;
  margin: 0;
  padding: 0;
  overflow: auto;
  list-style: none;
}

.event-feed li {
  border-left: 3px solid rgba(240, 188, 66, 0.56);
  border-radius: 0 8px 8px 0;
  background: rgba(245, 239, 217, 0.05);
  padding: 9px 10px;
  color: #ded8c4;
  font-size: 0.88rem;
}

.toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  max-width: min(360px, calc(100% - 40px));
  border: 1px solid rgba(240, 188, 66, 0.5);
  border-radius: 8px;
  background: var(--panel-strong);
  color: var(--text);
  padding: 12px 14px;
  box-shadow: 0 18px 40px var(--shadow);
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: opacity 160ms ease, transform 160ms ease;
}

.toast.visible {
  opacity: 1;
  transform: translateY(0);
}

@keyframes markIn {
  from {
    opacity: 0;
    transform: scale(0.72) rotate(-8deg);
  }
  to {
    opacity: 1;
    transform: scale(1) rotate(0deg);
  }
}

@keyframes crossIn {
  0% {
    opacity: 0;
    transform: scale(0.55) rotate(-16deg);
    filter: blur(7px);
  }
  62% {
    opacity: 1;
    transform: scale(1.08) rotate(3deg);
    filter: blur(0);
  }
  100% {
    opacity: 1;
    transform: scale(1) rotate(0deg);
    filter: blur(0);
  }
}

@media (max-width: 980px) {
  .game-layout {
    grid-template-columns: 1fr;
  }

  .arena-panel {
    min-height: auto;
  }

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

  .event-section {
    grid-column: 1 / -1;
  }
}

@media (max-width: 640px) {
  .app-shell {
    width: min(100% - 18px, 1220px);
    padding-top: 12px;
  }

  .topbar,
  .brand {
    align-items: flex-start;
  }

  .brand-sigil {
    width: 46px;
    height: 46px;
    font-size: 1rem;
  }

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

  .brand p {
    max-width: 52vw;
    white-space: normal;
  }

  .arena-panel {
    padding: 10px;
    gap: 10px;
  }

  .score-strip {
    grid-template-columns: 1fr 1fr;
  }

  .turn-core {
    grid-column: 1 / -1;
    order: -1;
    min-height: 58px;
  }

  .score-block {
    min-height: 56px;
    padding: 0 12px;
  }

  .board {
    gap: 7px;
    padding: 7px;
  }

  .mark {
    font-size: 3.6rem;
  }

  .board.size-4 .mark {
    font-size: 2.4rem;
  }

  .power-row,
  .energy-bank,
  .side-panel {
    grid-template-columns: 1fr;
  }

  .room-actions {
    grid-template-columns: 1fr 1fr;
  }
}
