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

:root {
  --bg: #0f0f1a;
  --surface: #1a1a2e;
  --surface-2: #252542;
  --border: #3a3a5c;
  --border-thick: #6366f1;
  --text: #f0f0f5;
  --text-muted: #9898b0;
  --given: #ffffff;
  --user: #a5b4fc;
  --player1: #60a5fa;
  --player2: #f472b6;
  --highlight: rgba(99, 102, 241, 0.18);
  --highlight-same: rgba(99, 102, 241, 0.28);
  --selected: rgba(99, 102, 241, 0.42);
  --error: #f87171;
  --error-bg: rgba(248, 113, 113, 0.15);
  --success: #34d399;
  --notes: #9898b0;
  --controls-space: 360px;
  --board-pad: 24px;
  --cell-size: min(
    calc((100vw - var(--board-pad) - 6px) / 9),
    calc((100svh - var(--controls-space) - var(--safe-top) - var(--safe-bottom)) / 9),
    48px
  );
  --btn-size: min(48px, calc((100vw - var(--board-pad) - 32px) / 9));
  --radius: 12px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --chat-height: 280px;
  --menu-gap: 22px;
  --menu-inner-gap: 10px;
}

html {
  height: 100%;
  overflow-y: auto;
  touch-action: manipulation;
}

body {
  margin: 0;
  min-height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow-y: auto;
  user-select: none;
  -webkit-user-select: none;
}

.app {
  width: 100%;
  min-height: 100svh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  padding: calc(12px + var(--safe-top)) 12px calc(16px + var(--safe-bottom));
  max-width: 520px;
  margin: 0 auto;
  position: relative;
}

.app:not(.in-game) {
  min-height: 0;
}

.app-top-bar {
  display: none;
  justify-content: flex-end;
  align-items: center;
  flex-shrink: 0;
  width: 100%;
  margin-bottom: 8px;
}

.app.has-user .app-top-bar {
  display: flex;
  padding-bottom: 10px;
  margin-bottom: 4px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.app.in-game .app-top-bar {
  display: none;
}

.user-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  width: fit-content;
  max-width: 100%;
  padding: 5px 8px 5px 5px;
  border-radius: 999px;
  border: 2px solid rgba(99, 102, 241, 0.35);
  background: rgba(26, 26, 46, 0.94);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.25);
}

.user-chip.hidden {
  display: none;
}

.user-chip-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
  border: 2px solid rgba(255, 255, 255, 0.12);
}

.user-chip-avatar.hidden,
.user-chip-fallback.hidden {
  display: none;
}

.user-chip-fallback {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0.02em;
  color: #fff;
  background: linear-gradient(135deg, #6366f1, #818cf8);
  border: 2px solid rgba(255, 255, 255, 0.12);
}

.user-chip-name {
  font-size: 0.88rem;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.user-chip-logout {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-muted);
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background 0.15s, color 0.15s;
}

.user-chip-logout svg {
  width: 16px;
  height: 16px;
  fill: currentColor;
}

.user-chip-logout:hover {
  background: rgba(248, 113, 113, 0.18);
  color: #f87171;
}

.app.in-game {
  min-height: 100svh;
  min-height: 100dvh;
  max-width: 520px;
}

.app.in-game.has-chat {
  max-width: 980px;
}

/* ── Main menu ── */

.main-menu {
  flex: 0 1 auto;
  display: flex;
  flex-direction: column;
  gap: var(--menu-gap);
  padding: 0 2px calc(20px + var(--safe-bottom));
  animation: menu-in 0.35s ease;
}

.app.has-user .menu-hero {
  padding-top: 0;
}

.main-menu.hidden {
  display: none;
}

@keyframes menu-in {
  from {
    opacity: 0;
    transform: translateY(12px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.menu-hero {
  text-align: center;
  padding: 8px 0 10px;
}

.menu-logo {
  width: 72px;
  height: 72px;
  margin: 0 auto 14px;
  display: block;
  border-radius: 18px;
  box-shadow:
    0 10px 28px rgba(99, 102, 241, 0.28),
    0 2px 8px rgba(0, 0, 0, 0.18);
}

.menu-title {
  margin: 0;
  font-size: 2.15rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  background: linear-gradient(135deg, #fff 0%, #c7d2fe 55%, #6ee7b7 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.menu-subtitle {
  margin: 6px 0 0;
  color: var(--text-muted);
  font-size: 0.92rem;
}

.menu-section {
  display: flex;
  flex-direction: column;
  gap: var(--menu-inner-gap);
}

.menu-section--modes {
  gap: 12px;
}

.menu-section-title {
  margin: 0;
  padding: 0 2px;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.menu-section.hidden {
  display: none;
}

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

.menu-mode-card {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  min-height: 96px;
  padding: 12px 8px;
  border: 2px solid var(--border);
  border-radius: 16px;
  background: rgba(26, 26, 46, 0.65);
  color: var(--text-muted);
  font: inherit;
  cursor: pointer;
  touch-action: manipulation;
  appearance: none;
  -webkit-appearance: none;
  transition: border-color 0.15s, background 0.15s, transform 0.1s, opacity 0.15s;
}

.menu-mode-card.is-locked {
  opacity: 0.78;
  cursor: not-allowed;
}

.menu-mode-card.is-locked:active {
  transform: none;
}

.menu-mode-card.is-locked.active {
  border-color: var(--border);
  background: rgba(26, 26, 46, 0.65);
  color: var(--text-muted);
  box-shadow: none;
}

.menu-mode-badge {
  position: absolute;
  top: 8px;
  right: 8px;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 0.62rem;
  font-weight: 700;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  line-height: 1.3;
  color: #fbbf24;
  background: rgba(251, 191, 36, 0.14);
  border: 1px solid rgba(251, 191, 36, 0.38);
  pointer-events: none;
}

.menu-mode-badge.hidden {
  display: none;
}

.menu-mode-card:active {
  transform: scale(0.97);
}

.menu-mode-card.active {
  border-color: var(--border-thick);
  background: rgba(99, 102, 241, 0.16);
  color: var(--text);
  box-shadow: 0 8px 24px rgba(99, 102, 241, 0.15);
}

.menu-mode-icon {
  font-size: 1.35rem;
  line-height: 1;
}

.menu-mode-name {
  font-size: 0.88rem;
  font-weight: 700;
}

.menu-mode-desc {
  font-size: 0.65rem;
  line-height: 1.25;
  text-align: center;
  opacity: 0.85;
}

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

.menu-diff-btn {
  min-height: 48px;
  padding: 10px 8px;
  border: 2px solid var(--border);
  border-radius: 14px;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: 0.88rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  appearance: none;
  -webkit-appearance: none;
  transition: all 0.15s;
}

.menu-diff-btn.active {
  border-color: var(--border-thick);
  background: rgba(99, 102, 241, 0.15);
  color: var(--text);
}

.menu-diff-btn.locked {
  opacity: 0.65;
  pointer-events: none;
}

.menu-rated-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px;
  border: 2px solid rgba(251, 191, 36, 0.35);
  border-radius: 16px;
  background: linear-gradient(135deg, rgba(251, 191, 36, 0.1), rgba(99, 102, 241, 0.08));
}

.menu-rated-panel.hidden {
  display: none;
}

.menu-rated-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.menu-rated-label {
  font-size: 0.82rem;
  color: var(--text-muted);
  font-weight: 600;
}

.menu-rated-value {
  font-size: 1.75rem;
  font-weight: 800;
  color: #fbbf24;
  letter-spacing: -0.02em;
}

.menu-rated-rank {
  display: inline-flex;
  align-self: flex-start;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(251, 191, 36, 0.15);
  border: 1px solid rgba(251, 191, 36, 0.35);
  color: #fcd34d;
  font-size: 0.78rem;
  font-weight: 700;
}

.menu-rated-hint {
  margin: 0;
  padding-top: 2px;
  font-size: 0.78rem;
  line-height: 1.5;
  color: var(--text-muted);
}

.modal-rating {
  margin: 0 0 4px;
  font-size: 1rem;
  font-weight: 700;
  color: #fbbf24;
}

.modal-rating.hidden {
  display: none;
}

.stat-value.rating-value {
  color: #fbbf24;
}

.tool-btn.hidden {
  display: none;
}

.menu-online-hint {
  margin: 0;
  font-size: 0.82rem;
  color: var(--text-muted);
  line-height: 1.45;
}

.menu-join-row {
  display: flex;
  gap: 8px;
}

.menu-join-input {
  flex: 1;
  min-width: 0;
  min-height: 48px;
  padding: 10px 14px;
  border: 2px solid var(--border);
  border-radius: 14px;
  background: rgba(15, 15, 26, 0.55);
  color: var(--text);
  font: inherit;
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.menu-join-input:focus {
  outline: none;
  border-color: var(--border-thick);
}

.menu-join-input.invalid {
  border-color: var(--error);
  animation: menu-shake 0.35s ease;
}

@keyframes menu-shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

.menu-secondary-btn {
  flex-shrink: 0;
  min-height: 48px;
  padding: 0 18px;
  border: 2px solid var(--border);
  border-radius: 14px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  appearance: none;
  -webkit-appearance: none;
}

.menu-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding-top: 14px;
  margin-top: 2px;
  border-top: 1px solid rgba(255, 255, 255, 0.07);
}

.menu-start-btn,
.menu-continue-btn {
  width: 100%;
  min-height: 52px;
  border: none;
  border-radius: 16px;
  font: inherit;
  font-size: 1.05rem;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  appearance: none;
  -webkit-appearance: none;
  transition: transform 0.1s, opacity 0.15s;
}

.menu-start-btn {
  background: linear-gradient(135deg, #6366f1, #818cf8);
  color: #fff;
  box-shadow: 0 10px 28px rgba(99, 102, 241, 0.35);
}

.menu-start-btn.is-locked,
.menu-start-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  box-shadow: none;
  filter: grayscale(0.25);
}

.menu-continue-btn {
  background: rgba(52, 211, 153, 0.15);
  border: 2px solid rgba(52, 211, 153, 0.45);
  color: var(--success);
}

.menu-start-btn:active,
.menu-continue-btn:active {
  transform: scale(0.98);
}

.menu-continue-btn.hidden {
  display: none;
}

.menu-auth {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px 16px;
  border: 2px solid var(--border);
  border-radius: 16px;
  background: rgba(26, 26, 46, 0.55);
}

.menu-auth.hidden {
  display: none;
}

.auth-label {
  margin: 0 0 2px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.auth-buttons {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.auth-btn {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  width: 100%;
  min-height: 48px;
  padding: 0 16px;
  border-radius: 12px;
  font: inherit;
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  appearance: none;
  -webkit-appearance: none;
  transition: transform 0.1s, box-shadow 0.15s, opacity 0.15s;
}

.auth-btn:active:not(:disabled) {
  transform: scale(0.98);
}

.auth-btn:disabled {
  opacity: 0.65;
  cursor: wait;
}

.auth-btn.is-loading {
  pointer-events: none;
}

.auth-btn.is-loading .auth-btn-text {
  opacity: 0.7;
}

.auth-btn-icon {
  flex-shrink: 0;
  display: block;
}

.auth-btn-icon-apple {
  width: 16px;
  height: 20px;
}

.auth-btn-text {
  line-height: 1.2;
}

.auth-btn-google {
  background: #fff;
  color: #3c4043;
  border: 1px solid #dadce0;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}

.auth-btn-google:hover:not(:disabled) {
  box-shadow: 0 2px 8px rgba(66, 133, 244, 0.25);
}

.auth-btn-apple {
  background: #000;
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.18);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
}

.auth-btn-apple:hover:not(:disabled) {
  background: #1a1a1a;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.45);
}

.auth-btn-apple.is-unavailable {
  opacity: 0.72;
}

.auth-soon-badge {
  position: absolute;
  top: 8px;
  right: 10px;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 0.62rem;
  font-weight: 700;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  line-height: 1.3;
  color: #fbbf24;
  background: rgba(251, 191, 36, 0.14);
  border: 1px solid rgba(251, 191, 36, 0.38);
  pointer-events: none;
}

.auth-soon-badge.hidden {
  display: none;
}

.auth-notice.hidden {
  display: none;
}

.auth-notice {
  margin: 0;
  font-size: 0.82rem;
  color: #fbbf24;
  text-align: center;
}

.menu-rated-login-hint {
  display: none;
}

.menu-leaderboard-btn {
  width: 100%;
  min-height: 48px;
  border: 2px solid rgba(251, 191, 36, 0.35);
  border-radius: 14px;
  background: rgba(251, 191, 36, 0.08);
  color: #fcd34d;
  font: inherit;
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
}

.menu-footer {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding-top: 4px;
}

.menu-footer-links {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.menu-footer-link {
  color: var(--text-muted);
  font-size: 0.78rem;
  text-decoration: none;
}

.menu-footer-link:hover {
  color: var(--user);
}

.menu-footer-sep {
  color: var(--border);
  font-size: 0.78rem;
}

.menu-footer-danger {
  border: none;
  background: none;
  color: #f87171;
  font: inherit;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  padding: 4px 8px;
}

.menu-footer-danger.hidden {
  display: none;
}

.menu-footer-danger:hover {
  text-decoration: underline;
}

.menu-leaderboard-view.hidden {
  display: none;
}

.menu-leaderboard-view {
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex: 1;
  min-height: 0;
}

.main-menu.is-leaderboard-view .menu-hero {
  display: none;
}

.main-menu.is-leaderboard-view .menu-leaderboard-view {
  flex: 1;
}

.main-menu.is-leaderboard-view .menu-leaderboard-header {
  margin-bottom: 4px;
}

.main-menu.is-leaderboard-view .menu-leaderboard-title {
  flex: 1;
  text-align: center;
  padding-right: 72px;
}

.main-menu.is-leaderboard-view .leaderboard-list {
  flex: 1;
  max-height: none;
}

.menu-leaderboard-header {
  display: flex;
  align-items: center;
  gap: 10px;
}

.menu-back-btn {
  flex-shrink: 0;
  min-height: 40px;
  padding: 0 12px;
  border: 2px solid var(--border);
  border-radius: 12px;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.menu-leaderboard-title {
  margin: 0;
  font-size: 1.2rem;
  font-weight: 800;
}

.leaderboard-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  max-height: min(60vh, 520px);
  padding-right: 2px;
}

.leaderboard-row {
  display: grid;
  grid-template-columns: auto auto 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: 2px solid var(--border);
  border-radius: 14px;
  background: rgba(26, 26, 46, 0.55);
}

.leaderboard-row-mine {
  border-color: rgba(251, 191, 36, 0.45);
  background: rgba(251, 191, 36, 0.08);
}

.leaderboard-rank {
  width: 2rem;
  text-align: center;
  font-weight: 800;
  color: #fbbf24;
  font-size: 1.05rem;
  line-height: 1;
}

.leaderboard-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
}

.leaderboard-avatar-fallback {
  display: grid;
  place-items: center;
  font-size: 0.72rem;
  font-weight: 800;
  color: #fff;
  letter-spacing: 0.02em;
}

.leaderboard-meta {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.leaderboard-name {
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.leaderboard-stats {
  font-size: 0.72rem;
  color: var(--text-muted);
}

.leaderboard-rating {
  font-size: 1rem;
  color: #fbbf24;
}

.leaderboard-empty {
  margin: 0;
  padding: 24px 12px;
  text-align: center;
  color: var(--text-muted);
}

#menu-home-view.hidden {
  display: none;
}

@media (min-width: 600px) {
  .app:not(.in-game) {
    padding-top: calc(16px + var(--safe-top));
  }

  .app.has-user .app-top-bar {
    margin-bottom: 10px;
  }

  .app.has-user .user-chip {
    max-width: min(280px, 100%);
  }

  .menu-hero {
    padding-top: 8px;
  }

  .app.has-user .menu-hero {
    padding-top: 0;
  }
}

/* Главное меню: вертикальный список на телефонах и планшетах */
@media (max-width: 1023px) {
  .main-menu {
    gap: 20px;
  }

  .menu-section--modes {
    gap: 14px;
  }

  .menu-mode-grid {
    grid-template-columns: 1fr;
    gap: 10px;
  }

  .app.has-user .app-top-bar {
    justify-content: stretch;
    margin-bottom: 10px;
  }

  .app.has-user .user-chip {
    width: 100%;
    max-width: 100%;
    border-radius: 14px;
    padding: 8px 10px 8px 8px;
  }

  .user-chip-name {
    flex: 1;
    font-size: 0.9rem;
  }

  .menu-mode-card {
    display: grid;
    grid-template-columns: auto 1fr;
    grid-template-rows: auto auto;
    column-gap: 14px;
    row-gap: 2px;
    align-items: center;
    min-height: 72px;
    padding: 14px 16px;
    text-align: left;
  }

  .menu-mode-icon {
    grid-row: 1 / 3;
    grid-column: 1;
    font-size: 1.5rem;
    width: 2rem;
    text-align: center;
  }

  .menu-mode-name {
    grid-column: 2;
    grid-row: 1;
    font-size: 1rem;
    text-align: left;
  }

  .menu-mode-desc {
    grid-column: 2;
    grid-row: 2;
    font-size: 0.78rem;
    text-align: left;
  }

  .menu-join-row {
    flex-direction: column;
  }

  .menu-secondary-btn {
    width: 100%;
    min-height: 52px;
  }
}

/* ── Game screen ── */

.game-screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.game-screen.hidden {
  display: none;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

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

.header-logo {
  width: 32px;
  height: 32px;
  flex-shrink: 0;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(99, 102, 241, 0.22);
}

.header-back-btn {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
  appearance: none;
  -webkit-appearance: none;
}

.header-back-btn svg {
  width: 22px;
  height: 22px;
  fill: currentColor;
}

.game-mode-badge {
  margin: -2px 0 8px;
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(99, 102, 241, 0.12);
  border: 1px solid rgba(99, 102, 241, 0.25);
  color: var(--text-muted);
  font-size: 0.78rem;
  font-weight: 600;
  text-align: center;
}

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.modal-actions .secondary-btn {
  min-height: 44px;
}

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

.mode-bar {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  margin-bottom: 8px;
}

.mode-btn {
  min-height: 36px;
  padding: 6px 8px;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s;
  appearance: none;
  -webkit-appearance: none;
}

.mode-btn.active {
  border-color: var(--border-thick);
  background: rgba(99, 102, 241, 0.15);
  color: var(--text);
}

.turn-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-bottom: 8px;
  padding: 8px 12px;
  border-radius: var(--radius);
  background: var(--surface-2);
  font-size: 0.9rem;
}

.turn-bar.hidden {
  display: none;
}

.turn-label {
  color: var(--text-muted);
}

.turn-player {
  font-weight: 700;
}

.turn-player.p1 {
  color: var(--player1);
}

.turn-player.p2 {
  color: var(--player2);
}

.turn-toast {
  position: fixed;
  left: 50%;
  top: 50%;
  z-index: 90;
  transform: translate(-50%, -50%) scale(0.85);
  padding: 18px 28px;
  border-radius: 16px;
  background: rgba(26, 26, 46, 0.95);
  border: 2px solid var(--border-thick);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  font-size: 1.25rem;
  font-weight: 700;
  text-align: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.25s ease, transform 0.25s ease;
}

.turn-toast.show {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}

.turn-toast.auth-toast {
  top: auto;
  bottom: calc(24px + var(--safe-bottom));
  transform: translate(-50%, 12px) scale(0.95);
  font-size: 0.95rem;
  font-weight: 600;
  max-width: min(92vw, 360px);
  border-color: rgba(251, 191, 36, 0.55);
  color: #fcd34d;
}

.turn-toast.auth-toast.show {
  transform: translate(-50%, 0) scale(1);
}

.turn-toast.hidden {
  display: block;
}

.turn-toast.p1 {
  color: var(--player1);
  border-color: var(--player1);
}

.turn-toast.p2 {
  color: var(--player2);
  border-color: var(--player2);
}

.chat-panel {
  margin-bottom: 10px;
  padding: 12px;
  border-radius: 16px;
  background: linear-gradient(180deg, rgba(37, 37, 66, 0.95) 0%, rgba(26, 26, 46, 0.98) 100%);
  border: 1px solid rgba(99, 102, 241, 0.25);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.22);
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  gap: 8px;
  height: var(--chat-height);
  max-height: var(--chat-height);
  min-height: var(--chat-height);
  overflow: hidden;
}

.chat-panel.hidden {
  display: none;
}

.chat-header,
.chat-form {
  flex: 0 0 auto;
}

.chat-body {
  flex: 1 1 0;
  min-height: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.chat-messages-wrap {
  flex: 1 1 0;
  min-height: 0;
  overflow: hidden;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding-bottom: 8px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.chat-header-main {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
}

.call-start-btn {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid rgba(52, 211, 153, 0.45);
  border-radius: 10px;
  background: rgba(52, 211, 153, 0.12);
  color: var(--success);
  cursor: pointer;
  touch-action: manipulation;
  appearance: none;
  -webkit-appearance: none;
  transition: background 0.15s, transform 0.1s, opacity 0.15s;
}

.call-start-btn:active:not(:disabled) {
  transform: scale(0.92);
}

.call-start-btn:disabled,
.call-start-btn.busy {
  opacity: 0.55;
  cursor: wait;
}

.call-start-btn svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
}

.call-start-btn:hover {
  background: rgba(52, 211, 153, 0.22);
}

.call-start-btn.hidden {
  display: none;
}

.call-float {
  position: fixed;
  z-index: 220;
  top: calc(12px + var(--safe-top));
  right: 12px;
  width: min(300px, calc(100vw - 24px));
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px;
  border-radius: 16px;
  background: linear-gradient(180deg, rgba(37, 37, 66, 0.98) 0%, rgba(20, 20, 36, 0.98) 100%);
  border: 1px solid rgba(52, 211, 153, 0.35);
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.45);
  box-sizing: border-box;
}

.call-float.hidden {
  display: none;
}

.call-float.dragging {
  opacity: 0.96;
  box-shadow: 0 20px 56px rgba(0, 0, 0, 0.55);
}

.call-float-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 2px 4px 6px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.call-float.dragging .call-float-header {
  cursor: grabbing;
}

.call-float-title {
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--success);
  letter-spacing: 0.02em;
}

.call-float-title::before {
  content: '📹 ';
}

.call-float-grip {
  font-size: 1rem;
  line-height: 1;
  color: var(--text-muted);
  opacity: 0.85;
}

.call-float .call-incoming,
.call-float .call-videos,
.call-float .call-toolbar {
  flex: 0 0 auto;
}

.call-incoming {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 12px 8px;
  text-align: center;
}

.call-incoming.hidden {
  display: none;
}

.call-incoming-text {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
}

.call-incoming-actions {
  display: flex;
  gap: 10px;
  width: 100%;
}

.call-incoming-btn {
  flex: 1;
  min-height: 44px;
  border-radius: 12px;
  border: none;
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  appearance: none;
  -webkit-appearance: none;
}

.call-incoming-btn.accept {
  background: var(--success);
  color: #0f0f1a;
}

.call-incoming-btn.decline {
  background: rgba(248, 113, 113, 0.2);
  color: var(--error);
  border: 1px solid rgba(248, 113, 113, 0.45);
}

.call-float.waiting .call-videos {
  opacity: 0.85;
}

.call-videos {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 10;
  min-height: 168px;
  border-radius: 12px;
  overflow: hidden;
  background: #0a0a14;
  border: 1px solid var(--border);
}

.call-videos audio {
  position: absolute;
  width: 0;
  height: 0;
  opacity: 0;
  pointer-events: none;
}

.call-videos.hidden {
  display: none;
}

.call-video {
  background: #0a0a14;
}

.call-video.remote {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center;
}

.call-video.local {
  position: absolute;
  right: 8px;
  bottom: 8px;
  width: 28%;
  min-width: 72px;
  max-width: 100px;
  aspect-ratio: 3 / 4;
  height: auto;
  max-height: 35%;
  object-fit: cover;
  object-position: center;
  border-radius: 8px;
  border: 2px solid rgba(255, 255, 255, 0.35);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  z-index: 2;
}

.call-video.local.hidden {
  display: none;
}

.call-toolbar {
  display: flex;
  justify-content: center;
  gap: 10px;
}

.call-tool {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}

.call-tool svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
}

.call-tool:hover {
  border-color: var(--border-thick);
  background: rgba(99, 102, 241, 0.12);
}

.call-tool.off {
  background: rgba(248, 113, 113, 0.15);
  border-color: rgba(248, 113, 113, 0.45);
  color: var(--error);
}

.call-tool.off .icon-on {
  display: none;
}

.call-tool.off .icon-off {
  display: block !important;
}

.call-tool.hangup {
  background: rgba(248, 113, 113, 0.2);
  border-color: rgba(248, 113, 113, 0.5);
  color: var(--error);
}

.call-tool:active {
  transform: scale(0.94);
}

.chat-title {
  font-size: 0.95rem;
  font-weight: 700;
  color: var(--text);
}

.chat-title::before {
  content: '💬 ';
}

.chat-subtitle {
  font-size: 0.72rem;
  color: var(--text-muted);
}

.chat-messages {
  height: 100%;
  max-height: 100%;
  overflow-y: scroll;
  overflow-x: hidden;
  display: block;
  padding: 2px 4px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.chat-messages::-webkit-scrollbar {
  width: 5px;
}

.chat-messages::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}

.chat-empty {
  margin: auto;
  padding: 12px 8px;
  text-align: center;
  font-size: 0.78rem;
  color: var(--text-muted);
  line-height: 1.45;
}

.chat-msg {
  display: flex;
  flex-direction: column;
  gap: 3px;
  max-width: 90%;
  margin-bottom: 10px;
  animation: chat-in 0.2s ease;
}

@keyframes chat-in {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.chat-msg.mine {
  align-self: flex-end;
  align-items: flex-end;
}

.chat-msg:not(.mine) {
  align-self: flex-start;
  align-items: flex-start;
}

.chat-bubble-name {
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.chat-msg.p1 .chat-bubble-name {
  color: var(--player1);
}

.chat-msg.p2 .chat-bubble-name {
  color: var(--player2);
}

.chat-bubble-text {
  padding: 9px 13px;
  border-radius: 16px;
  font-size: 0.92rem;
  line-height: 1.4;
  word-break: break-word;
}

.chat-msg.p1 .chat-bubble-text {
  background: rgba(96, 165, 250, 0.16);
  border: 1px solid rgba(96, 165, 250, 0.28);
  border-bottom-left-radius: 5px;
}

.chat-msg.p2 .chat-bubble-text {
  background: rgba(244, 114, 182, 0.16);
  border: 1px solid rgba(244, 114, 182, 0.28);
  border-bottom-left-radius: 5px;
}

.chat-msg.mine.p1 .chat-bubble-text {
  background: rgba(96, 165, 250, 0.26);
  border-bottom-left-radius: 16px;
  border-bottom-right-radius: 5px;
}

.chat-msg.mine.p2 .chat-bubble-text {
  background: rgba(244, 114, 182, 0.26);
  border-bottom-left-radius: 16px;
  border-bottom-right-radius: 5px;
}

.chat-msg.system {
  align-self: center;
  align-items: center;
  max-width: 100%;
}

.chat-msg.system .chat-bubble-text {
  padding: 6px 12px;
  font-size: 0.78rem;
  color: var(--text-muted);
  background: rgba(255, 255, 255, 0.05);
  border: 1px dashed rgba(255, 255, 255, 0.12);
  border-radius: 999px;
}

.emoji-picker {
  flex: 0 0 auto;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 2px;
  padding: 8px;
  background: rgba(15, 15, 26, 0.6);
  border: 1px solid var(--border);
  border-radius: 12px;
  max-height: 112px;
  overflow-y: auto;
}

.emoji-picker.hidden {
  display: none;
}

.emoji-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 36px;
  border: none;
  background: transparent;
  font-size: 1.35rem;
  line-height: 1;
  cursor: pointer;
  border-radius: 8px;
  transition: background 0.12s, transform 0.1s;
}

.emoji-btn:hover {
  background: rgba(255, 255, 255, 0.08);
}

.emoji-btn:active {
  transform: scale(0.92);
}

.chat-form {
  display: flex;
  align-items: center;
  gap: 8px;
  padding-top: 2px;
}

.chat-emoji-btn {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  font-size: 1.2rem;
  line-height: 1;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.chat-emoji-btn:hover {
  border-color: var(--border-thick);
  background: rgba(99, 102, 241, 0.12);
}

.chat-emoji-btn.active {
  border-color: var(--border-thick);
  background: rgba(99, 102, 241, 0.2);
}

.chat-input {
  flex: 1;
  min-width: 0;
  min-height: 40px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: rgba(15, 15, 26, 0.55);
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.chat-input:focus {
  outline: none;
  border-color: var(--border-thick);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
}

.chat-send {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 12px;
  background: linear-gradient(135deg, #6366f1, #818cf8);
  color: #fff;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(99, 102, 241, 0.35);
  transition: transform 0.1s, box-shadow 0.15s;
}

.chat-send svg {
  width: 18px;
  height: 18px;
  fill: currentColor;
}

.chat-send:active {
  transform: scale(0.94);
}

.chat-send:hover {
  box-shadow: 0 6px 18px rgba(99, 102, 241, 0.45);
}

.online-panel {
  margin-bottom: 10px;
  padding: 12px;
  border-radius: var(--radius);
  background: var(--surface-2);
  border: 1px solid var(--border);
}

.online-panel.hidden {
  display: none;
}

.online-status {
  margin: 0 0 10px;
  font-size: 0.8rem;
  color: var(--text-muted);
  line-height: 1.4;
  word-break: break-word;
}

.online-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.secondary-btn {
  flex: 1;
  min-width: 120px;
  min-height: 40px;
  padding: 8px 12px;
  border: 2px solid var(--border-thick);
  border-radius: var(--radius);
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  transition: background 0.15s, transform 0.1s;
}

.secondary-btn:active {
  transform: scale(0.98);
}

.secondary-btn.hidden {
  display: none;
}

.room-box {
  margin-bottom: 10px;
  padding: 12px;
  border-radius: var(--radius);
  background: rgba(99, 102, 241, 0.12);
  border: 1px solid var(--border-thick);
}

.room-box.hidden {
  display: none;
}

.room-code-wrap {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}

.room-code-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.room-code {
  font-size: 1.4rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  color: var(--player1);
}

.room-link {
  display: block;
  font-size: 0.75rem;
  color: var(--text-muted);
  word-break: break-all;
  line-height: 1.4;
  text-decoration: underline;
}

.title {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 800;
  letter-spacing: -0.03em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  background: linear-gradient(135deg, #fff 0%, #c7d2fe 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.stats {
  display: flex;
  gap: 20px;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 2px;
}

.stat-label {
  font-size: 0.7rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.stat-value {
  font-size: 1.1rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.main {
  flex: 1 0 auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
}

.game-layout {
  display: flex;
  flex-direction: column;
  gap: 12px;
  flex: 1;
  min-width: 0;
}

.board-wrap {
  display: flex;
  justify-content: center;
  touch-action: pan-y;
}

.board {
  display: grid;
  grid-template-columns: repeat(9, var(--cell-size));
  grid-template-rows: repeat(9, var(--cell-size));
  width: calc(var(--cell-size) * 9 + 6px);
  max-width: 100%;
  border: 3px solid var(--border-thick);
  border-radius: 6px;
  overflow: hidden;
  background: var(--surface);
  touch-action: pan-y;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  padding: 0;
  margin: 0;
  border: 1px solid var(--border);
  border-radius: 0;
  background: var(--surface);
  font: inherit;
  font-size: calc(var(--cell-size) * 0.48);
  font-weight: 600;
  line-height: 1;
  text-align: center;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  transition: background 0.1s;
  overflow: hidden;
}

.cell:nth-child(3n):not(:nth-child(9n)) {
  border-right: 2px solid var(--border-thick);
}

.cell:nth-child(n + 19):nth-child(-n + 27),
.cell:nth-child(n + 46):nth-child(-n + 54) {
  border-bottom: 2px solid var(--border-thick);
}

.cell.given {
  color: var(--given);
  font-weight: 700;
}

.cell.user {
  color: var(--user);
  font-weight: 600;
}

.cell.user .value {
  text-shadow: 0 0 20px rgba(165, 180, 252, 0.15);
}

.cell.user.player1 {
  color: var(--player1);
}

.cell.user.player2 {
  color: var(--player2);
}

.cell.locked {
  opacity: 0.55;
}

.cell.remote-select {
  position: relative;
}

.cell.remote-p1::after,
.cell.remote-p2::after {
  content: '';
  position: absolute;
  inset: 2px;
  border: 2px solid transparent;
  border-radius: 2px;
  pointer-events: none;
}

.cell.remote-p1::after {
  border-color: var(--player1);
  box-shadow: 0 0 8px rgba(96, 165, 250, 0.6);
}

.cell.remote-p2::after {
  border-color: var(--player2);
  box-shadow: 0 0 8px rgba(244, 114, 182, 0.6);
}

.cell.selected.remote-p1::after,
.cell.selected.remote-p2::after {
  inset: 0;
  border-width: 3px;
}

.difficulty.hidden {
  display: none;
}

.cell.selected {
  background: var(--selected);
}

.cell.highlight {
  background: var(--highlight);
}

.cell.highlight-same {
  background: var(--highlight-same);
}

.cell.mistake {
  color: var(--error);
  background: var(--error-bg);
}

.cell.mistake.highlight,
.cell.mistake.highlight-same,
.cell.mistake.selected {
  background: var(--error-bg);
}

.cell.mistake .value {
  font-weight: 800;
}

.cell.mistake-flash {
  animation: cell-mistake-shake 0.42s ease;
}

.cell.mistake-flash::after {
  content: '';
  position: absolute;
  inset: 1px;
  border: 2px solid var(--error);
  border-radius: 3px;
  pointer-events: none;
  animation: cell-mistake-ring 0.55s ease-out forwards;
}

@keyframes cell-mistake-shake {
  0%, 100% { transform: translateX(0); }
  15% { transform: translateX(-5px); }
  30% { transform: translateX(5px); }
  45% { transform: translateX(-4px); }
  60% { transform: translateX(4px); }
  75% { transform: translateX(-2px); }
}

@keyframes cell-mistake-ring {
  0% {
    opacity: 1;
    box-shadow: 0 0 0 0 rgba(248, 113, 113, 0.55);
  }
  100% {
    opacity: 0;
    box-shadow: 0 0 0 10px rgba(248, 113, 113, 0);
  }
}

/* legacy alias */
.cell.error {
  color: var(--error);
  background: var(--error-bg);
}

.cell .value {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  color: inherit;
  font-variant-numeric: tabular-nums;
}

.cell .notes {
  position: absolute;
  inset: 1px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  font-size: calc(var(--cell-size) * 0.18);
  font-weight: 500;
  color: var(--notes);
  pointer-events: none;
}

.cell .notes span {
  display: flex;
  align-items: center;
  justify-content: center;
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 12px;
  position: sticky;
  bottom: 0;
  z-index: 10;
  padding-top: 10px;
  padding-bottom: max(8px, var(--safe-bottom));
  background: var(--bg);
  box-shadow: 0 -12px 24px var(--bg);
  isolation: isolate;
}

.toolbar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.tool-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-height: var(--btn-size);
  padding: 8px 4px;
  border: none;
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 0.65rem;
  line-height: 1.2;
  text-align: center;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  transition: background 0.15s, transform 0.1s;
}

.tool-btn svg {
  width: 22px;
  height: 22px;
  fill: currentColor;
}

.tool-btn:active {
  transform: scale(0.95);
}

.tool-btn.active {
  background: var(--border-thick);
  color: #fff;
}

.tool-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.numpad {
  display: grid;
  grid-template-columns: repeat(9, minmax(0, 1fr));
  gap: 4px;
  width: 100%;
}

.num-btn {
  aspect-ratio: 1;
  width: 100%;
  min-height: 0;
  padding: 0;
  border: none;
  border-radius: var(--radius);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: clamp(0.85rem, 2.8vw, 1.4rem);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  transition: background 0.15s, transform 0.1s;
}

.num-btn:active {
  transform: scale(0.92);
}

.num-btn.highlight-num {
  background: var(--border-thick);
  color: #fff;
}

.num-btn.completed {
  opacity: 0.35;
}

.num-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  position: relative;
  z-index: 5;
}

.difficulty {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
  position: relative;
  z-index: 2;
}

.difficulty-hint {
  margin: -4px 0 0;
  font-size: 0.72rem;
  color: var(--text-muted);
  text-align: center;
}

.difficulty-hint.hidden {
  display: none;
}

.difficulty.guest-locked .diff-btn {
  cursor: default;
}

.diff-btn {
  min-height: 44px;
  min-width: 0;
  padding: 8px 4px;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: 0.8rem;
  font-weight: 500;
  line-height: 1.2;
  text-align: center;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  touch-action: manipulation;
  transition: all 0.15s;
}

.diff-btn.active {
  border-color: var(--border-thick);
  background: rgba(99, 102, 241, 0.15);
  color: var(--text);
}

.diff-btn.locked {
  opacity: 0.72;
  cursor: default;
  pointer-events: none;
}

.diff-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

@media (max-width: 767px) {
  .controls .toolbar {
    order: 1;
  }

  .controls .actions {
    order: 2;
  }

  .controls .numpad {
    order: 3;
  }

  .diff-btn {
    min-height: 48px;
  }
}

.new-game-btn {
  min-height: 48px;
  padding: 12px 24px;
  border: none;
  border-radius: var(--radius);
  background: var(--border-thick);
  color: #fff;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s, opacity 0.15s;
}

.new-game-btn:active {
  transform: scale(0.98);
}

.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.7);
  backdrop-filter: blur(4px);
  z-index: 100;
  padding: 20px;
}

.overlay.hidden {
  display: none;
}

.modal {
  background: var(--surface);
  border-radius: 16px;
  padding: 32px 28px;
  text-align: center;
  max-width: 320px;
  width: 100%;
  border: 1px solid var(--border);
}

.modal h2 {
  margin: 0 0 8px;
  font-size: 1.5rem;
}

.modal p {
  margin: 0 0 20px;
  color: var(--text-muted);
}

.modal-actions {
  margin-top: 4px;
}

@media (max-width: 767px) {
  :root {
    --chat-height: min(300px, 42svh);
  }

  .chat-panel {
    height: auto;
    min-height: var(--chat-height);
    max-height: min(340px, 46svh);
  }

  .chat-body {
    min-height: 150px;
  }

  .chat-messages-wrap {
    min-height: 110px;
  }

  .call-float {
    width: min(280px, calc(100vw - 20px));
    right: 10px;
    top: calc(10px + var(--safe-top));
  }

  .call-videos {
    min-height: 150px;
    aspect-ratio: 4 / 3;
  }
}

@media (min-width: 1024px) {
  .app:not(.in-game) {
    max-width: 480px;
  }

  .menu-mode-grid {
    gap: 12px;
  }

  .menu-mode-card {
    min-height: 110px;
  }
}

@media (min-width: 768px) {
  .app:not(.in-game) {
    max-width: 480px;
  }

  .app.in-game.has-chat {
    max-width: 980px;
  }

  .game-layout.has-chat {
    flex-direction: row;
    align-items: flex-start;
    gap: 16px;
  }

  .game-layout.has-chat .main {
    flex: 1;
    min-width: 0;
    justify-content: flex-start;
  }

  .game-layout.has-chat .board-wrap {
    justify-content: flex-start;
  }

  .game-layout.has-chat .chat-panel {
    width: min(280px, 30vw);
    flex-shrink: 0;
    height: min(520px, calc(100svh - var(--safe-top) - var(--safe-bottom) - 80px));
    max-height: min(520px, calc(100svh - var(--safe-top) - var(--safe-bottom) - 80px));
    min-height: min(520px, calc(100svh - var(--safe-top) - var(--safe-bottom) - 80px));
    margin-bottom: 0;
    overflow: hidden;
    position: sticky;
    top: calc(8px + var(--safe-top));
    align-self: flex-start;
  }

  .game-layout.has-chat .chat-form {
    flex-direction: row;
    align-items: center;
  }

  .game-layout.has-chat .chat-send {
    width: 40px;
    min-height: 40px;
  }
}

@media (min-width: 600px) and (orientation: portrait) {
  :root {
    --controls-space: 330px;
    --board-pad: 32px;
    --cell-size: min(
      calc((100vw - var(--board-pad) - 6px) / 9),
      calc((100svh - var(--controls-space) - var(--safe-top) - var(--safe-bottom)) / 9),
      46px
    );
  }

  .app {
    max-width: 560px;
    padding-top: calc(16px + var(--safe-top));
  }
}

@media (min-width: 600px) {
  .tool-btn {
    font-size: 0.75rem;
  }

  .tool-btn svg {
    width: 24px;
    height: 24px;
  }
}

@media (max-height: 820px) {
  :root {
    --controls-space: 300px;
  }

  .header {
    margin-bottom: 10px;
  }

  .title {
    font-size: 1.3rem;
  }

  .main,
  .controls {
    gap: 10px;
  }

  .tool-btn {
    min-height: var(--btn-size);
    padding: 6px 4px;
  }

  .num-btn {
    min-height: 0;
  }

  .diff-btn {
    min-height: 36px;
    font-size: 0.75rem;
  }

  .new-game-btn {
    min-height: 44px;
    padding: 10px 20px;
    font-size: 0.95rem;
  }
}

@media (orientation: landscape) and (min-width: 600px) {
  :root {
    --controls-space: 72px;
    --board-pad: 32px;
    --cell-size: min(
      calc((100svh - 72px - var(--safe-top) - var(--safe-bottom)) / 9),
      calc((100vw - 380px) / 9),
      52px
    );
  }

  .app {
    max-width: 980px;
    padding-left: 16px;
    padding-right: 16px;
  }

  .app.in-game.has-chat {
    max-width: 1100px;
  }

  .app.in-game.has-chat {
    --cell-size: min(
      calc((100svh - 72px - var(--safe-top) - var(--safe-bottom)) / 9),
      calc((100vw - 680px) / 9),
      52px
    );
  }

  .header {
    margin-bottom: 8px;
  }

  .game-layout.has-chat {
    flex-direction: row;
    align-items: flex-start;
    gap: 16px;
  }

  .main {
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: 20px;
  }

  .game-layout.has-chat .main {
    flex: 1;
    justify-content: flex-start;
    align-items: center;
  }

  .game-layout.has-chat .board-wrap {
    justify-content: flex-start;
  }

  .game-layout.has-chat .chat-panel {
    width: min(260px, 28vw);
    height: min(480px, calc(100svh - var(--safe-top) - var(--safe-bottom) - 56px));
    max-height: min(480px, calc(100svh - var(--safe-top) - var(--safe-bottom) - 56px));
    min-height: min(480px, calc(100svh - var(--safe-top) - var(--safe-bottom) - 56px));
    align-self: flex-start;
    position: sticky;
    top: calc(8px + var(--safe-top));
    margin-bottom: 0;
    overflow: hidden;
  }

  .board-wrap {
    flex-shrink: 0;
  }

  .controls {
    flex: 0 1 340px;
    width: min(340px, 38vw);
    min-width: 260px;
    margin-top: 0;
    position: static;
    padding-top: 0;
    padding-bottom: 0;
    background: none;
  }
}

@media (orientation: landscape) and (max-height: 500px) {
  :root {
    --cell-size: min(
      calc((100svh - 56px - var(--safe-top) - var(--safe-bottom)) / 9),
      calc((100vw - 320px) / 9),
      44px
    );
    --btn-size: 40px;
  }

  .tool-btn span {
    display: none;
  }

  .tool-btn {
    min-height: 40px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition: none !important;
  }

  .chat-msg {
    animation: none !important;
  }
}
