*,
*::before,
*::after {
  box-sizing: border-box;
}

:root {
  --paper: #f6f0e2;
  --paper-deep: #efe6d0;
  --card: #fffdf6;
  --card-2: #f7f1e0;
  --ink: #2b2a33;
  --ink-soft: #6f6a80;
  --line: #e4dbc4;
  --line-strong: #d5c9a9;
  --accent: #ff4f7d;
  --accent-soft: #ffd9e4;
  --board-bg: #fffdf6;
  --dot: #35324a;
  --shadow-card: 0 2px 0 var(--line), 0 16px 30px -18px rgba(70, 60, 30, 0.5);
  --radius: 18px;
  --font-display: "Fredoka", "Trebuchet MS", sans-serif;
  --font-body: "Nunito", "Trebuchet MS", sans-serif;
}

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

body {
  font-family: var(--font-body);
  color: var(--ink);
  background:
    radial-gradient(900px 500px at 85% -10%, #ffeed3 0%, transparent 60%),
    radial-gradient(700px 500px at -10% 100%, #ffe3ec 0%, transparent 55%),
    var(--paper);
  background-attachment: fixed;
  -webkit-font-smoothing: antialiased;
}

/* Notebook dot-grid texture over the whole page */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background-image: radial-gradient(rgba(53, 50, 74, 0.07) 1.2px, transparent 1.2px);
  background-size: 26px 26px;
}

.app {
  position: relative;
  z-index: 1;
  max-width: 1060px;
  margin: 0 auto;
  padding: 28px 20px 60px;
}

/* ---------- Floating cast ---------- */

.cast {
  position: absolute;
  inset: -10px 0 auto 0;
  height: 0;
  pointer-events: none;
}

.cast-mate {
  position: absolute;
  top: 6px;
  font-size: 34px;
  filter: drop-shadow(0 4px 6px rgba(70, 60, 30, 0.18));
  animation: bob 3.2s ease-in-out infinite;
  animation-delay: calc(var(--i) * -0.8s);
  opacity: 0.9;
}

.cast-mate:nth-child(1) { left: 6%; }
.cast-mate:nth-child(2) { left: 16%; top: -14px; font-size: 26px; }
.cast-mate:nth-child(3) { right: 16%; top: -10px; font-size: 28px; }
.cast-mate:nth-child(4) { right: 6%; font-size: 36px; }

@keyframes bob {
  0%, 100% { transform: translateY(0) rotate(-4deg); }
  50% { transform: translateY(-10px) rotate(5deg); }
}

/* ---------- Header ---------- */

.app-header {
  text-align: center;
  margin-bottom: 26px;
  position: relative;
}

.sound-toggle {
  position: absolute;
  top: 4px;
  right: 0;
  width: 44px;
  height: 44px;
  font-size: 20px;
  border-radius: 50%;
  border: 2px solid var(--ink);
  background: var(--card);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.12s, background 0.15s;
  box-shadow: 0 3px 0 var(--line-strong);
}

.sound-toggle:hover {
  background: var(--card-2);
  transform: translateY(-2px) rotate(-6deg);
}

.sound-toggle.muted {
  opacity: 0.5;
}

.app-header h1 {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: clamp(34px, 6vw, 48px);
  letter-spacing: -0.01em;
  transform: rotate(-1.2deg);
}

.app-header .amp {
  color: var(--accent);
}

.tagline {
  color: var(--ink-soft);
  margin: 6px 0 0;
  font-size: 15px;
  font-weight: 600;
}

/* ---------- Game layout ---------- */

.game-layout {
  display: flex;
  gap: 22px;
  align-items: flex-start;
  justify-content: center;
  flex-wrap: wrap;
}

.game-timer {
  width: 100%;
  text-align: center;
  font-family: var(--font-display);
  font-size: 14px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.1em;
  color: var(--ink-soft);
  background: var(--card);
  border: 2px solid var(--ink);
  border-radius: 999px;
  padding: 6px 18px;
  margin: 0 auto;
  max-width: 180px;
  box-shadow: 0 3px 0 var(--line-strong);
}

.sidebar {
  width: 260px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.turn-card,
.score-panel {
  background: var(--card);
  border: 2px solid var(--ink);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow-card);
}

.turn-label,
.panel-title {
  display: block;
  font-family: var(--font-display);
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--ink-soft);
  margin: 0 0 10px;
}

.turn-card .turn-label {
  margin-bottom: 10px;
}

.turn-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 14px 6px 8px;
  border-radius: 999px;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 16px;
  border: 2px solid;
  background: rgba(255, 255, 255, 0.6);
  transition: background 0.2s, border-color 0.2s, color 0.2s, transform 0.2s;
}

.turn-chip:not(.turn-chip-over) {
  animation: chip-wiggle 2.4s ease-in-out infinite;
}

@keyframes chip-wiggle {
  0%, 100% { transform: rotate(-1deg); }
  50% { transform: rotate(1.4deg); }
}

.turn-chip-over {
  color: var(--ink-soft) !important;
  border-color: var(--line-strong) !important;
}

.turn-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  font-size: 19px;
  border-radius: 50%;
  background: color-mix(in srgb, var(--turn-color, #999) 18%, white);
}

.status {
  display: block;
  margin-top: 12px;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-soft);
  line-height: 1.45;
}

.status.flash {
  animation: flash 0.5s ease;
}

@keyframes flash {
  0% { opacity: 0; transform: translateY(4px); }
  100% { opacity: 1; transform: translateY(0); }
}

.panel-title {
  margin: 0 0 12px;
}

.score-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.score-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-radius: 12px;
  background: var(--card-2);
  border: 2px solid transparent;
  transition: border-color 0.2s, transform 0.2s, box-shadow 0.2s;
}

.score-item-turn {
  border-color: var(--turn-color);
  transform: translateX(4px);
  box-shadow: 0 4px 0 -1px color-mix(in srgb, var(--turn-color) 30%, transparent);
}

/* ---------- Avatars ---------- */

.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  font-size: 18px;
  border-radius: 50%;
  flex-shrink: 0;
  transition: transform 0.15s;
}

.score-item:hover .avatar,
.result-row:hover .avatar {
  transform: scale(1.15) rotate(-8deg);
}

.score-item-offline {
  opacity: 0.45;
}

.score-item-offline .avatar {
  filter: grayscale(1);
}

.score-name {
  flex: 1;
  font-size: 14px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.score-you {
  color: var(--ink-soft);
  font-weight: 600;
  font-size: 12px;
}

.score-left {
  color: var(--ink-soft);
  font-weight: 700;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.score-value {
  font-family: var(--font-display);
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  font-size: 20px;
  min-width: 28px;
  text-align: right;
}

.score-pop {
  animation: score-pop 0.55s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes score-pop {
  0% { transform: scale(1); }
  40% { transform: scale(1.6) rotate(-6deg); }
  100% { transform: scale(1); }
}

/* ---------- Buttons ---------- */

.btn-primary,
.btn-ghost {
  font: inherit;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 15px;
  border-radius: 12px;
  padding: 12px 18px;
  cursor: pointer;
  transition: transform 0.1s, box-shadow 0.15s, background 0.2s;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  border: 2px solid var(--ink);
  box-shadow: 0 4px 0 #c22b58;
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 0 #c22b58;
}

.btn-primary:active {
  transform: translateY(1px);
  box-shadow: 0 2px 0 #c22b58;
}

.btn-ghost {
  background: var(--card);
  color: var(--ink);
  border: 2px solid var(--ink);
  box-shadow: 0 3px 0 var(--line-strong);
}

.btn-ghost:hover {
  background: var(--card-2);
  transform: translateY(-1px);
}

/* ---------- Board ---------- */

.board-area {
  flex: 1;
  min-width: 320px;
  max-width: 680px;
  display: flex;
  justify-content: center;
}

.board {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 22px;
  border: 2.5px solid var(--ink);
  box-shadow: 0 4px 0 var(--line-strong), 0 24px 40px -24px rgba(70, 60, 30, 0.55);
  transform: rotate(-0.4deg);
}

.board-bg {
  fill: var(--board-bg);
}

.board-glow {
  pointer-events: none;
}

.board-frame {
  fill: none;
  stroke: var(--line-strong);
  stroke-width: 2;
  stroke-dasharray: 7 6;
  opacity: 0.7;
  pointer-events: none;
}

.dot {
  fill: var(--dot);
  pointer-events: none;
}

.edge-slot {
  cursor: pointer;
}

.edge-slot rect {
  fill: transparent;
}

.edge-slot .edge-preview {
  stroke: var(--player-color, var(--accent));
  opacity: 0;
  stroke-linecap: round;
  transition: opacity 0.12s;
}

.edge-slot:hover .edge-preview {
  opacity: 0.45;
}

.edge-drawn {
  stroke-linecap: round;
  pointer-events: none;
}

.box-claimed,
.box-claimed * {
  pointer-events: none;
}

.box-pop {
  transform-box: fill-box;
  transform-origin: center;
  animation: box-pop 0.42s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

@keyframes box-pop {
  0% { transform: scale(0.3); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

.box-claimed .box-emoji {
  font-size: 24px;
}

/* ---------- Overlays ---------- */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(43, 42, 51, 0.45);
  backdrop-filter: blur(5px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 20;
  animation: overlay-in 0.25s ease;
}

@keyframes overlay-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

[hidden] {
  display: none !important;
}

.setup-card,
.result-card,
.wait-card {
  background: var(--card);
  border: 2.5px solid var(--ink);
  border-radius: 24px;
  padding: 26px;
  width: min(420px, 100%);
  box-shadow: 0 5px 0 var(--line-strong), 0 30px 60px -30px rgba(70, 60, 30, 0.6);
  animation: card-in 0.35s cubic-bezier(0.34, 1.4, 0.64, 1);
}

@keyframes card-in {
  from { opacity: 0; transform: translateY(16px) scale(0.97) rotate(-0.5deg); }
  to { opacity: 1; transform: translateY(0) scale(1) rotate(0); }
}

.setup-card h2,
.result-card h2,
.wait-card h2 {
  margin: 0 0 18px;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 24px;
}

/* ---------- Mode tabs ---------- */

.mode-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  background: var(--card-2);
  border: 2px solid var(--ink);
  border-radius: 14px;
  padding: 5px;
  margin-bottom: 20px;
}

.mode-tab {
  font: inherit;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 13px;
  color: var(--ink-soft);
  background: transparent;
  border: none;
  border-radius: 9px;
  padding: 9px 8px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, transform 0.1s;
}

.mode-tab:hover {
  color: var(--ink);
  transform: translateY(-1px);
}

.mode-tab.active {
  background: var(--ink);
  color: var(--card);
}

/* ---------- Network status ---------- */

.net-status {
  margin-top: 18px;
  border-top: 2px dashed var(--line-strong);
  padding-top: 16px;
}

.net-status .net-label {
  margin: 0 0 8px;
  font-size: 12px;
  font-weight: 700;
  color: var(--ink-soft);
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

.net-status .you-name {
  margin: 8px 0 0;
  font-size: 14px;
  color: var(--ink-soft);
  font-weight: 600;
}

.net-status .you-name strong {
  color: var(--accent);
  font-weight: 800;
}

.room-code {
  font-family: var(--font-display);
  font-size: 44px;
  font-weight: 700;
  letter-spacing: 0.14em;
  color: var(--accent);
  background: var(--card-2);
  border: 2px solid var(--ink);
  border-radius: 14px;
  padding: 10px 12px;
  text-align: center;
  font-variant-numeric: tabular-nums;
  user-select: all;
  box-shadow: 0 3px 0 var(--line-strong);
}

.qr-wrap {
  margin: 14px auto 0;
  width: fit-content;
  padding: 10px;
  background: #fff;
  border: 2px solid var(--ink);
  border-radius: 14px;
  box-shadow: 0 4px 0 var(--line-strong);
}

.qr-wrap img {
  display: block;
  width: 168px;
  height: 168px;
}

.qr-wrap .qr-hint {
  margin: 6px 0 2px;
  font-size: 12px;
  color: #333;
  text-align: center;
  letter-spacing: 0.06em;
  font-weight: 700;
}

.net-status .waiting {
  margin: 12px 0 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-soft);
}

.net-status .waiting.error,
.net-status.error {
  color: #e03131;
}

.lobby-list {
  list-style: none;
  margin: 0 0 12px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.lobby-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 10px;
  border-radius: 10px;
  background: var(--card-2);
  font-size: 14px;
  font-weight: 700;
  border: 1.5px solid var(--line);
}

#joinStatus.error {
  margin-top: 18px;
  border-top: 2px dashed var(--line-strong);
  padding-top: 16px;
  font-size: 14px;
  color: #e03131;
}

.wait-status {
  margin: 0 0 16px;
  font-size: 13px;
  color: var(--ink-soft);
  font-weight: 600;
}

.wait-players {
  list-style: none;
  margin: 0 0 20px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.wait-player {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--card-2);
  font-size: 14px;
  font-weight: 700;
  border: 1.5px solid var(--line);
}

.wait-player .wait-you {
  font-style: normal;
  font-size: 12px;
  font-weight: 700;
  color: var(--accent);
  margin-left: auto;
}

.lobby-status {
  margin: 0 0 16px;
  font-size: 12px;
  color: var(--ink-soft);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  display: flex;
  align-items: center;
  gap: 8px;
}

.lobby-status::before {
  content: "";
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--ink-soft);
  flex-shrink: 0;
}

.lobby-status.connected::before {
  background: #2fbf71;
}

.lobby-status.hosting::before {
  background: var(--accent);
}

.lobby-status.reconnecting::before,
.lobby-status.connecting::before {
  background: #f59f00;
  animation: pulse 1.2s ease-in-out infinite;
}

.lobby-status.offline::before {
  background: #e03131;
}

@keyframes pulse {
  50% { opacity: 0.35; }
}

/* ---------- Room list ---------- */

.room-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 224px;
  overflow-y: auto;
}

.room-list-empty {
  margin: 0;
  font-size: 13px;
  color: var(--ink-soft);
  font-weight: 600;
  padding: 6px 2px;
}

.room-item {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--card-2);
  border: 2px solid var(--ink);
  border-radius: 12px;
  padding: 10px 12px;
}

.room-item .room-info {
  flex: 1;
  min-width: 0;
}

.room-item .room-name {
  font-weight: 800;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.room-item .room-meta {
  font-size: 12px;
  color: var(--ink-soft);
  margin-top: 2px;
  font-weight: 600;
}

.btn-join {
  font: inherit;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 13px;
  background: var(--ink);
  color: var(--card);
  border: none;
  border-radius: 9px;
  padding: 8px 14px;
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 0.1s, box-shadow 0.15s;
}

.btn-join:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 0 rgba(70, 60, 30, 0.3);
}

.or-divider {
  text-align: center;
  font-size: 12px;
  color: var(--ink-soft);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  margin: 2px 0 14px;
}

/* ---------- Fields ---------- */

.field-row {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
}

.field {
  flex: 1;
  min-width: 96px;
  margin-bottom: 16px;
}

.field label {
  display: block;
  font-family: var(--font-display);
  font-size: 13px;
  font-weight: 600;
  color: var(--ink-soft);
  margin-bottom: 8px;
}

.field input,
.field select,
.player-names input {
  width: 100%;
  font: inherit;
  font-weight: 600;
  color: var(--ink);
  background: var(--card);
  border: 2px solid var(--ink);
  border-radius: 11px;
  padding: 10px 12px;
  outline: none;
  caret-color: var(--accent);
  transition: box-shadow 0.15s, transform 0.1s;
}

.field input::placeholder,
.player-names input::placeholder {
  color: var(--ink-soft);
  opacity: 0.7;
  font-weight: 600;
}

.field input:hover,
.field select:hover,
.player-names input:hover {
  box-shadow: 0 3px 0 var(--line);
}

.field input:focus,
.field select:focus,
.player-names input:focus {
  box-shadow: 0 3px 0 var(--accent), 0 0 0 3px var(--accent-soft);
}

.field select {
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml;charset=utf-8,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%236f6a80' d='M6 8 0 0h12z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 34px;
}

.player-names {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.player-name-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.player-name-row input {
  flex: 1;
}

#hostName,
#joinName {
  text-transform: none;
}

#joinCode {
  text-align: center;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.28em;
  font-variant-numeric: tabular-nums;
  padding: 11px 12px;
}

.btn-copy {
  width: 100%;
  margin-top: 12px;
  font-size: 14px;
}

.btn-copy.copied {
  background: #2fbf71;
  color: #fff;
  box-shadow: 0 4px 0 #1d8a4d;
}

/* ---------- Toasts ---------- */

.toast-container {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  z-index: 50;
  pointer-events: none;
}

.toast {
  background: var(--card);
  border: 2px solid var(--ink);
  color: var(--ink);
  border-radius: 12px;
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 700;
  box-shadow: 0 4px 0 var(--line-strong);
  animation: toast-in 0.3s cubic-bezier(0.34, 1.4, 0.64, 1);
  max-width: min(420px, 90vw);
  text-align: center;
}

.toast.leave {
  border-color: #e03131;
  background: #ffe9e9;
}

.toast.info {
  border-color: var(--ink);
}

.toast.out {
  opacity: 0;
  transition: opacity 0.35s ease;
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(10px) scale(0.95);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* ---------- Setup & results ---------- */

.setup-card .btn-primary {
  width: 100%;
  margin-top: 6px;
}

.result-card {
  text-align: center;
}

.result-emblem {
  font-size: 56px;
  line-height: 1;
  margin-bottom: 6px;
  animation: emblem-dance 1.6s ease-in-out infinite;
}

@keyframes emblem-dance {
  0%, 100% { transform: rotate(-8deg) scale(1); }
  50% { transform: rotate(8deg) scale(1.12); }
}

.result-list {
  list-style: none;
  margin: 18px 0 20px;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  text-align: left;
}

.result-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 14px;
  background: var(--card-2);
  border: 2px solid var(--line);
  border-radius: 12px;
}

.result-row .result-score {
  margin-left: auto;
  font-family: var(--font-display);
  font-weight: 700;
  color: var(--ink-soft);
}

.result-actions {
  display: flex;
  gap: 12px;
}

.result-actions .btn-primary,
.result-actions .btn-ghost {
  flex: 1;
}

/* ---------- Confetti ---------- */

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

.confetti-piece {
  position: absolute;
  top: -20px;
  animation-name: confetti-fall;
  animation-timing-function: linear;
  animation-fill-mode: both;
}

.confetti-emoji {
  font-size: 20px;
}

@keyframes confetti-fall {
  0% {
    transform: translateY(-20px) translateX(0) rotate(0deg);
    opacity: 1;
  }
  80% { opacity: 1; }
  100% {
    transform: translateY(105vh) translateX(var(--drift, 0px)) rotate(540deg);
    opacity: 0;
  }
}

/* ---------- Responsive ---------- */

@media (max-width: 720px) {
  .game-layout {
    flex-direction: column;
    align-items: center;
  }

  .sidebar {
    width: 100%;
    max-width: 420px;
  }

  .cast {
    display: none;
  }

  .board {
    transform: none;
  }
}
