:root {
  color-scheme: light;
  --bg: #f6f1e8;
  --surface: #ffffff;
  --surface-soft: #fff8e8;
  --text: #17202a;
  --muted: #667085;
  --border: #d8dee8;
  --accent: #0f9f8f;
  --accent-dark: #08776d;
  --green: #19a974;
  --red: #d84f45;
  --yellow: #fff0a8;
  --blue: #3267d6;
  --shadow: 0 18px 50px rgba(23, 32, 42, 0.12);
  --radius: 8px;
  --focus: 0 0 0 4px rgba(15, 159, 143, 0.24);
}

body.dark {
  color-scheme: dark;
  --bg: #111827;
  --surface: #1f2937;
  --surface-soft: #152436;
  --text: #f8fafc;
  --muted: #b7c1cf;
  --border: #364456;
  --accent: #38d9c3;
  --accent-dark: #8ff5e7;
  --green: #54df9a;
  --red: #ff7b72;
  --yellow: #62561f;
  --blue: #81a8ff;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
}

body {
  min-height: 100vh;
  margin: 0;
  background:
    radial-gradient(circle at top left, rgba(15, 159, 143, 0.16), transparent 30rem),
    linear-gradient(135deg, var(--bg), color-mix(in srgb, var(--bg) 84%, #65b6ff));
  color: var(--text);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
  line-height: 1.5;
}

button,
input {
  font: inherit;
}

button {
  touch-action: manipulation;
}

.app {
  width: min(100%, 1180px);
  margin: 0 auto;
  padding: 18px 14px 32px;
}

.topbar,
.party-header,
.player-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

.topbar {
  margin-bottom: 18px;
}

.brand h1,
.party-header h2,
.panel-heading h2 {
  margin: 0;
  line-height: 1;
  letter-spacing: 0;
}

.brand h1 {
  font-size: clamp(2.1rem, 12vw, 4.8rem);
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--accent-dark);
  font-size: 0.78rem;
  font-weight: 900;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.icon-button {
  display: grid;
  place-items: center;
  width: 46px;
  height: 46px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  box-shadow: 0 8px 18px rgba(23, 32, 42, 0.08);
}

.connection-panel,
.party-shell,
.host-panel,
.player-panel,
.winners {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  box-shadow: var(--shadow);
}

.connection-panel,
.host-panel,
.player-panel,
.winners {
  padding: 14px;
}

.party-shell {
  display: grid;
  gap: 14px;
  padding: 14px;
}

.panel-heading {
  display: grid;
  gap: 5px;
  margin-bottom: 14px;
}

.panel-heading p,
.player-actions p {
  margin: 0;
  color: var(--muted);
}

.join-grid,
.controls-grid,
.dashboard,
.meta-grid {
  display: grid;
  gap: 12px;
}

.form-card {
  display: grid;
  gap: 12px;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.form-card h3,
.panel-section h3,
.pattern-panel h3,
.custom-editor h4,
.winners h3 {
  margin: 0;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 800;
}

.username-input-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
}

.username-input-row .refresh-button {
  width: 48px;
  height: 48px;
  border-radius: var(--radius);
}

input {
  width: 100%;
  min-height: 48px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-soft);
  color: var(--text);
  padding: 0 12px;
}

input:focus,
button:focus-visible {
  outline: none;
  box-shadow: var(--focus);
}

.action {
  min-height: 48px;
  border: 0;
  border-radius: var(--radius);
  color: #ffffff;
  cursor: pointer;
  font-weight: 850;
  transition:
    transform 150ms ease,
    filter 150ms ease,
    opacity 150ms ease;
}

.action:hover {
  transform: translateY(-1px);
  filter: brightness(1.03);
}

.action:disabled {
  cursor: not-allowed;
  opacity: 0.52;
  transform: none;
}

.primary {
  background: var(--accent-dark);
}

.secondary {
  background: var(--blue);
}

.danger {
  background: var(--red);
}

.call {
  background: #8c5cf6;
}

.party-header {
  align-items: stretch;
  border-bottom: 1px solid var(--border);
  padding-bottom: 12px;
}

.party-code {
  display: grid;
  place-items: center;
  min-width: 116px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-soft);
}

.party-code span,
.meta-grid span,
.called-card span {
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 900;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.party-code strong {
  font-size: 1.45rem;
  letter-spacing: 0.08em;
}

.status-panel,
.side-panel {
  display: grid;
  gap: 12px;
}

.called-card,
.meta-grid > div,
.panel-section,
.pattern-panel {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.called-card {
  display: grid;
  place-items: center;
  min-height: 132px;
  padding: 16px;
  text-align: center;
}

.called-card strong {
  font-size: clamp(2.4rem, 18vw, 6rem);
  line-height: 1;
}

.called-card.hit strong {
  color: var(--green);
}

.called-card.miss strong {
  color: var(--red);
}

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

.meta-grid > div {
  display: grid;
  gap: 4px;
  min-width: 0;
  padding: 12px;
}

.meta-grid strong {
  min-width: 0;
  overflow-wrap: anywhere;
}

.panel-section,
.pattern-panel {
  display: grid;
  gap: 12px;
  padding: 12px;
}

.player-list,
.winners ul {
  display: grid;
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.player-list li,
.winners li {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 10px;
  border-radius: var(--radius);
  background: var(--surface-soft);
}

.history {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  max-height: 180px;
  overflow: auto;
}

.history span {
  min-width: 52px;
  padding: 7px 8px;
  border-radius: var(--radius);
  background: var(--surface-soft);
  text-align: center;
  font-weight: 850;
}

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

.pattern-option {
  display: grid;
  gap: 6px;
  min-height: 64px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-soft);
  color: var(--text);
  cursor: pointer;
  font-weight: 800;
  text-align: left;
}

.pattern-option strong,
.pattern-option span {
  display: block;
}

.pattern-option span {
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 600;
}

.pattern-preview {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 3px;
}

.preview-cell {
  aspect-ratio: 1 / 1;
  border-radius: 3px;
  background: #edf2ff;
}

.preview-cell.active {
  background: var(--yellow);
}

.pattern-option.active {
  border-color: var(--accent-dark);
  background: var(--accent);
  color: #ffffff;
}

.waiting-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 6px;
  border-radius: 999px;
  background: rgba(255, 191, 71, 0.18);
  color: #8a5f00;
  font-size: 0.78rem;
  font-weight: 900;
}

.waiting-badge::before {
  content: "WAITING";
}

.waiting-summary {
  margin: 0;
  color: var(--muted);
  font-weight: 700;
}

.host-badge,
.player-badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 6px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 850;
}

.host-badge {
  background: rgba(15, 159, 143, 0.18);
  color: var(--accent-dark);
}

.player-badge {
  background: rgba(107, 114, 128, 0.14);
  color: var(--text);
}

.custom-grid,
.board {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
}

.custom-grid {
  gap: 6px;
}

.custom-cell {
  aspect-ratio: 1 / 1;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-soft);
  color: var(--text);
  cursor: pointer;
  font-weight: 900;
}

.custom-cell.selected {
  background: var(--yellow);
  color: var(--text);
}

.player-panel {
  display: grid;
  gap: 12px;
}

.player-actions {
  display: grid;
  gap: 8px;
}

.player-actions p {
  color: var(--muted);
  font-size: 0.95rem;
}


.board {
  width: 100%;
  max-width: min(92vw, 680px);
  margin: 0 auto;
  gap: clamp(5px, 1.5vw, 10px);
  padding: clamp(8px, 2.3vw, 16px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.header-cell,
.square {
  aspect-ratio: 1 / 1;
  border-radius: var(--radius);
}

.header-cell {
  display: grid;
  place-items: center;
  background: var(--text);
  color: var(--surface);
  font-size: clamp(1.1rem, 6.8vw, 2.35rem);
  font-weight: 950;
}

body.dark .header-cell {
  background: #ecfdfb;
  color: #11202b;
}

.square {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  border: 1px solid var(--border);
  background: var(--surface-soft);
  color: var(--text);
  cursor: pointer;
  font-size: clamp(1.02rem, 8.2vw, 2.55rem);
  font-weight: 900;
  line-height: 1;
  user-select: none;
}

.square.target {
  background: var(--yellow);
}

.square.target[data-column="0"],
.square.target[data-column="2"],
.square.target[data-column="4"] {
  background: #fff3b0;
}

.square.target[data-column="1"],
.square.target[data-column="3"] {
  background: #ffe082;
}

.square.target[data-row="0"],
.square.target[data-row="2"],
.square.target[data-row="4"] {
  background: #fff3b0;
}

.square.target[data-row="1"],
.square.target[data-row="3"] {
  background: #ffe082;
}

.square.called {
  border-color: var(--green);
  background: color-mix(in srgb, var(--green) 24%, var(--surface));
  color: var(--text);
}

.square.marked {
  background: var(--accent);
  border-color: var(--accent-dark);
  color: #ffffff;
}

body.dark .square.marked {
  color: #09211e;
}

.square.marked::after {
  content: "";
  position: absolute;
  inset: 14%;
  border: clamp(3px, 1vw, 6px) solid currentColor;
  border-radius: 50%;
  opacity: 0.22;
}

.square.winner-cell {
  background: #8b5cf6;
  color: #fff;
  box-shadow: inset 0 0 0 3px rgba(139, 92, 246, 0.65), 0 0 0 2px rgba(255, 255, 255, 0.35);
}

.square.winner-cell::after {
  content: "★";
  position: absolute;
  top: 4px;
  right: 4px;
  font-size: 0.72rem;
}

.square.free {
  font-size: clamp(0.76rem, 5.4vw, 1.45rem);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 18px;
  z-index: 20;
  width: min(calc(100vw - 28px), 520px);
  transform: translateX(-50%);
  padding: 12px 14px;
  border-radius: var(--radius);
  background: var(--text);
  color: var(--surface);
  box-shadow: var(--shadow);
  text-align: center;
  font-weight: 800;
}

.celebration {
  pointer-events: none;
  position: fixed;
  inset: 0;
  z-index: 15;
  overflow: hidden;
}

.confetti {
  position: absolute;
  top: -18px;
  width: 10px;
  height: 16px;
  border-radius: 2px;
  opacity: 0;
  animation: confettiDrop 1200ms ease-out forwards;
}

.hidden {
  display: none !important;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (min-width: 680px) {
  .app {
    padding: 28px 22px 44px;
  }

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

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

@media (min-width: 960px) {
  .dashboard {
    grid-template-columns: minmax(0, 1fr) 320px;
    align-items: start;
  }

  .side-panel {
    position: sticky;
    top: 18px;
  }

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

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

@keyframes confettiDrop {
  0% {
    opacity: 0;
    transform: translate3d(0, -20px, 0) rotate(0deg);
  }
  15% {
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: translate3d(var(--drift), 105vh, 0) rotate(720deg);
  }
}
