:root {
  color-scheme: light dark;
  --bg: #f4f4f6;
  --panel: #ffffff;
  --ink: #1a1a1e;
  --muted: #6b7280;
  --accent: #4f46e5;
  --border: #e2e2e7;
}

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

[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
  overscroll-behavior: none;
}

body {
  margin: 0;
  font-family:
    -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  touch-action: manipulation;
}

/* ---------- Header (compact, mobile-first) ---------- */

header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: env(safe-area-inset-top, 0) 12px 8px;
  padding-top: max(8px, env(safe-area-inset-top, 0));
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

h1 {
  font-size: 15px;
  margin: 0;
  font-weight: 600;
  white-space: nowrap;
}

.share-icon-btn {
  flex: 0 0 auto;
  width: 30px;
  height: 30px;
  padding: 0;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.share-icon-btn svg {
  width: 16px;
  height: 16px;
}

#share-link {
  flex: 1;
  min-width: 0;
  font-size: 12px;
  color: var(--muted);
  border: 1px solid var(--border);
  padding: 6px 10px;
  border-radius: 6px;
  background: var(--bg);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
}

#status {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  flex-shrink: 0;
}

#status.connected::before {
  content: '●';
  color: #22c55e;
  margin-right: 4px;
}

#status.disconnected::before {
  content: '●';
  color: #ef4444;
  margin-right: 4px;
}

/* ---------- Game status bar ---------- */

.status-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  color: var(--muted);
  overflow-x: auto;
  white-space: nowrap;
  scrollbar-width: none;
}

.status-bar::-webkit-scrollbar {
  display: none;
}

#phase-text {
  font-weight: 600;
  color: var(--ink);
  flex-shrink: 0;
}

.hint {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  letter-spacing: 2px;
  font-size: 14px;
  color: var(--accent);
  flex-shrink: 0;
}

.timer {
  position: relative;
  isolation: isolate;
  flex-shrink: 0;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--bg);
  border: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
  overflow: hidden;
}

.timer::before {
  content: '';
  position: absolute;
  inset: 0;
  width: calc(var(--pct, 1) * 100%);
  background: color-mix(in srgb, var(--accent) 25%, transparent);
  z-index: -1;
  transition: width 0.2s linear;
}

/* ---------- Inline guess bar (guesser view during a drawing round) ---------- */

.inline-guess-form {
  display: flex;
  gap: 8px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.inline-guess-form input {
  flex: 1;
  min-width: 0;
  height: 38px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0 12px;
  font-size: 14px;
  background: var(--bg);
  color: var(--ink);
}

.inline-guess-form button {
  flex: 0 0 auto;
  height: 38px;
  padding: 0 18px;
  font-weight: 600;
  background: var(--accent);
  color: #fff;
  border: none;
}

.status-actions {
  margin-left: auto;
  flex-shrink: 0;
  display: flex;
  gap: 6px;
}

.chip-btn {
  flex-shrink: 0;
  height: 28px;
  padding: 0 10px;
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.dropdown {
  flex-shrink: 0;
}

/* position:fixed with top/right set from JS (see toggleInsertMenu) rather
   than anchored via a `position: relative` ancestor — .status-bar needs
   overflow-x: auto for horizontal scrolling, and per the CSS overflow spec
   that forces overflow-y to compute as auto too, which would silently clip
   an absolutely-positioned dropdown rendered below it. */
.dropdown-menu {
  position: fixed;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 6px;
  min-width: 150px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  z-index: 25;
}

.dropdown-menu button {
  width: 100%;
  justify-content: flex-start;
  height: 36px;
  padding: 0 10px;
  border-radius: 6px;
  border: none;
  background: transparent;
  font-size: 13px;
  text-align: left;
}

.dropdown-menu button:hover {
  background: var(--bg);
  border: none;
}

#player-count-badge {
  background: var(--accent);
  color: #fff;
  border-radius: 999px;
  padding: 1px 7px;
  font-size: 11px;
  font-weight: 700;
}

/* ---------- Canvas ---------- */

main {
  flex: 1;
  position: relative;
  overflow: hidden;
  min-height: 0;
}

canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
  background: #f2f2f4;
}

canvas.not-drawer {
  cursor: not-allowed;
}

canvas.brush-cursor-active {
  cursor: none;
}

.brush-cursor {
  position: absolute;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  border: 2px solid #4f46e5;
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.85);
  box-sizing: border-box;
  pointer-events: none;
  transform: translate(-50%, -50%);
  line-height: 1;
  z-index: 5;
}

.brush-cursor.sticker-mode {
  border: none;
  box-shadow: none;
}

/* ---------- Bottom toolbar (thumb-reachable on mobile) ---------- */

.toolbar {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px 10px calc(10px + env(safe-area-inset-bottom, 0));
  background: var(--panel);
  border-top: 1px solid var(--border);
}

.swatches {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 2px;
  scrollbar-width: none;
  min-width: 0;
}

.swatches::-webkit-scrollbar {
  display: none;
}

.swatch {
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 2px solid var(--border);
  padding: 0;
  cursor: pointer;
}

.swatch.active {
  border-color: var(--ink);
  box-shadow:
    0 0 0 2px var(--panel),
    0 0 0 4px var(--accent);
}

.controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

.custom-color {
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  overflow: hidden;
  border: 2px solid var(--border);
  display: block;
  position: relative;
  background: conic-gradient(red, yellow, lime, cyan, blue, magenta, red);
}

.custom-color input[type='color'] {
  position: absolute;
  inset: -4px;
  width: calc(100% + 8px);
  height: calc(100% + 8px);
  border: none;
  padding: 0;
  cursor: pointer;
  opacity: 0;
}

.bg-picker {
  flex: 0 0 auto;
  width: 44px;
  height: 34px;
  border-radius: 8px;
  overflow: hidden;
  border: 2px dashed var(--border);
  display: block;
  position: relative;
  background: #f2f2f4;
}

.bg-picker input[type='color'] {
  position: absolute;
  inset: -4px;
  width: calc(100% + 8px);
  height: calc(100% + 8px);
  border: none;
  padding: 0;
  cursor: pointer;
  opacity: 0;
}

.size-control {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.size-control input[type='range'] {
  flex: 1;
  min-width: 0;
  height: 34px;
}

#width-preview {
  flex: 0 0 auto;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--ink);
}

button {
  flex: 0 0 auto;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--ink);
  padding: 0 16px;
  height: 34px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}

button:hover,
button:active {
  border-color: var(--accent);
  color: var(--accent);
}

#animate-toggle.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* ---------- Word choice overlay ---------- */

.choice-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.5);
  padding: 20px;
  z-index: 20;
}

.choice-card {
  background: var(--panel);
  border-radius: 14px;
  padding: 24px;
  text-align: center;
  max-width: 320px;
  width: 100%;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.25);
}

.choice-card h2 {
  margin: 0 0 16px;
  font-size: 16px;
}

.choice-buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.choice-buttons button {
  height: 44px;
  width: 100%;
  font-size: 15px;
  font-weight: 600;
  background: var(--accent);
  color: #fff;
  border: none;
}

.choice-buttons button:hover {
  filter: brightness(1.1);
  color: #fff;
}

/* ---------- Coloring page picker ---------- */

.templates-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.5);
  padding: 20px;
  z-index: 20;
}

.templates-card {
  background: var(--panel);
  border-radius: 14px;
  padding: 20px;
  max-width: 440px;
  width: 100%;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.25);
}

.templates-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.templates-header h2 {
  font-size: 16px;
  margin: 0;
}

.templates-header button {
  width: 30px;
  height: 30px;
  padding: 0;
  border-radius: 50%;
}

.templates-hint {
  margin: 0 0 12px;
  font-size: 12px;
  color: var(--muted);
}

/* ---------- Find players ---------- */

.public-toggle-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
  font-size: 13px;
  color: var(--ink);
  cursor: pointer;
}

.public-toggle-row input {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  cursor: pointer;
}

.quick-match-btn {
  width: 100%;
  height: 40px;
  margin-bottom: 14px;
  font-weight: 600;
  background: var(--accent);
  color: #fff;
  border: none;
}

.public-rooms-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 220px;
  overflow-y: auto;
}

.public-room-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
}

.public-room-info {
  font-size: 12px;
  color: var(--muted);
  text-align: left;
}

.public-room-info strong {
  display: block;
  font-size: 13px;
  color: var(--ink);
}

.public-room-row button {
  flex: 0 0 auto;
  height: 30px;
  padding: 0 14px;
  font-size: 12px;
  background: var(--accent);
  color: #fff;
  border: none;
}

.public-rooms-empty {
  font-size: 12px;
  color: var(--muted);
  text-align: center;
  padding: 12px 0;
}

/* ---------- Quick match searching animation ---------- */

.quick-match-searching {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  padding: 16px 0 4px;
}

.radar {
  position: relative;
  width: 120px;
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.radar-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 2px solid var(--accent);
  opacity: 0;
  animation: radar-pulse 2.4s ease-out infinite;
}

.radar-ring:nth-child(2) {
  animation-delay: 0.8s;
}

.radar-ring:nth-child(3) {
  animation-delay: 1.6s;
}

@keyframes radar-pulse {
  0% {
    transform: scale(0.3);
    opacity: 0.7;
  }
  100% {
    transform: scale(1);
    opacity: 0;
  }
}

.radar-dot {
  position: relative;
  z-index: 1;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
  animation: radar-dot-bounce 1.6s ease-in-out infinite;
}

@keyframes radar-dot-bounce {
  0%,
  100% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.08);
  }
}

.searching-text {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  color: var(--ink);
}

.searching-dots span {
  animation: searching-dot-blink 1.4s infinite;
  opacity: 0;
}

.searching-dots span:nth-child(1) {
  animation-delay: 0s;
}

.searching-dots span:nth-child(2) {
  animation-delay: 0.2s;
}

.searching-dots span:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes searching-dot-blink {
  0%,
  20% {
    opacity: 0;
  }
  50% {
    opacity: 1;
  }
  100% {
    opacity: 0;
  }
}

.quick-match-cancel {
  width: auto;
  padding: 0 20px;
  height: 36px;
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
}

.quick-match-not-found {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  padding: 24px 0 4px;
  text-align: center;
}

.quick-match-not-found-actions {
  display: flex;
  gap: 8px;
  width: 100%;
}

.quick-match-not-found-actions button {
  flex: 1;
  margin-bottom: 0;
}

@media (prefers-reduced-motion: reduce) {
  .radar-ring,
  .radar-dot,
  .searching-dots span {
    animation: none;
  }

  .radar-ring {
    opacity: 0.5;
  }

  .searching-dots span {
    opacity: 1;
  }
}

.templates-tabs {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
  overflow-x: auto;
  scrollbar-width: none;
}

.templates-tabs::-webkit-scrollbar {
  display: none;
}

.templates-tabs button {
  flex: 0 0 auto;
  height: 34px;
  padding: 0 14px;
  font-size: 13px;
}

.templates-tabs button.active {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.templates-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  overflow-y: auto;
}

.template-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  cursor: pointer;
}

.template-card:hover {
  border-color: var(--accent);
}

.template-thumb-svg {
  width: 100%;
  aspect-ratio: 1;
  color: var(--ink);
}

.template-thumb-svg polyline {
  fill: none;
  stroke: currentColor;
  stroke-width: 2.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.template-card span {
  font-size: 12px;
  color: var(--muted);
}

/* ---------- Sticker picker ---------- */

.stickers-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  overflow-y: auto;
}

.sticker-card {
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1;
  font-size: 28px;
  line-height: 1;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  cursor: pointer;
}

.sticker-card:hover {
  border-color: var(--accent);
}

/* ---------- Picture icon picker ---------- */

.picture-icon-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(68px, 1fr));
  gap: 8px;
  overflow-y: auto;
}

.picture-icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  cursor: pointer;
}

.picture-icon-btn:hover {
  border-color: var(--accent);
}

.picture-icon-btn .template-thumb-svg {
  width: 100%;
  color: var(--ink);
}

/* ---------- Picture zoom/rotate controls ---------- */

.picture-controls {
  position: absolute;
  top: 0;
  left: 0;
  transform: translate(-50%, -50%);
  display: flex;
  gap: 6px;
  padding: 6px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 999px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
  pointer-events: auto;
  z-index: 6;
}

.picture-controls button {
  width: 34px;
  height: 34px;
  padding: 0;
  border-radius: 50%;
  font-size: 16px;
  line-height: 1;
}

#picture-cancel {
  color: #ef4444;
  border-color: #ef4444;
}

/* ---------- Round-end toast ---------- */

.round-toast {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--panel);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
  z-index: 15;
}

.round-toast.error {
  background: #ef4444;
  color: #fff;
}

/* ---------- Players / chat panel ---------- */

.panel-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 30;
}

.panel {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  max-height: 78vh;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-radius: 16px 16px 0 0;
  padding: 12px 14px calc(12px + env(safe-area-inset-bottom, 0));
  box-shadow: 0 -8px 24px rgba(0, 0, 0, 0.2);
  z-index: 31;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.panel-header h2 {
  font-size: 15px;
  margin: 0;
}

.panel-header button {
  width: 30px;
  height: 30px;
  padding: 0;
  border-radius: 50%;
}

.game-controls {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}

.game-controls button {
  flex: 1;
  height: 38px;
  font-weight: 600;
  background: var(--accent);
  color: #fff;
  border: none;
}

#end-game {
  background: #ef4444;
}

.player-list {
  list-style: none;
  margin: 0 0 10px;
  padding: 0;
  max-height: 120px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
}

.player-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  font-size: 13px;
  border-bottom: 1px solid var(--border);
}

.player-row:last-child {
  border-bottom: none;
}

.player-row.offline {
  opacity: 0.45;
}

.player-row.me .player-name {
  font-weight: 700;
}

.player-score {
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

.chat-log {
  flex: 1;
  min-height: 100px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 13px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 10px;
}

.chat-row.correct {
  color: #16a34a;
  font-weight: 600;
}

.guess-form {
  display: flex;
  gap: 8px;
}

.guess-form input {
  flex: 1;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0 12px;
  font-size: 14px;
  background: var(--bg);
  color: var(--ink);
}

.guess-form button {
  height: 40px;
  background: var(--accent);
  color: #fff;
  border: none;
}

/* ---------- Name modal ---------- */

.name-modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.5);
  padding: 20px;
  z-index: 40;
}

.name-card {
  background: var(--panel);
  border-radius: 14px;
  padding: 28px 24px;
  max-width: 320px;
  width: 100%;
  text-align: center;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.25);
}

.name-card h2 {
  margin: 0 0 6px;
  font-size: 18px;
}

.name-card p {
  margin: 0 0 16px;
  font-size: 13px;
  color: var(--muted);
}

.name-card input {
  width: 100%;
  height: 42px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0 12px;
  font-size: 15px;
  margin-bottom: 12px;
  background: var(--bg);
  color: var(--ink);
  text-align: center;
}

.name-card button {
  width: 100%;
  height: 42px;
  font-weight: 600;
  background: var(--accent);
  color: #fff;
  border: none;
}

.name-card .templates-header {
  text-align: left;
}

#share-modal-close {
  width: 30px;
  height: 30px;
  padding: 0;
  border-radius: 50%;
  font-weight: 500;
}

.share-link-row {
  display: flex;
  gap: 8px;
}

.share-link-row input {
  flex: 1;
  width: auto;
  height: 42px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0 12px;
  font-size: 13px;
  background: var(--bg);
  color: var(--ink);
  text-align: left;
}

.share-link-row button {
  flex: 0 0 auto;
  width: auto;
  height: 42px;
  padding: 0 18px;
  font-weight: 600;
  background: var(--accent);
  color: #fff;
  border: none;
}

/* ---------- Larger screens ---------- */

@media (min-width: 640px) {
  header {
    padding: 10px 16px;
  }

  h1 {
    font-size: 16px;
  }

  #share-link {
    flex: 0 1 320px;
  }

  .toolbar {
    flex-direction: row;
    align-items: center;
    padding: 10px 16px;
  }

  .swatches {
    flex: 1;
  }

  .controls {
    flex: 0 0 auto;
  }

  .size-control input[type='range'] {
    width: 120px;
    flex: 0 0 auto;
  }

  .panel-overlay {
    background: transparent;
    pointer-events: none;
  }

  .panel {
    left: auto;
    top: 0;
    width: 320px;
    max-height: none;
    border-radius: 0;
    border-left: 1px solid var(--border);
    padding: 16px;
  }

  .player-list {
    max-height: 160px;
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111114;
    --panel: #1a1a1f;
    --ink: #f0f0f2;
    --muted: #9ca3af;
    --border: #2c2c33;
  }

  canvas {
    background: #1e1e24;
  }
}
