:root {
  --bg: #0f172a;
  --panel-bg: rgba(15, 23, 42, 0.85);
  --accent: #38bdf8;
  --accent-strong: #0ea5e9;
  --danger: #f87171;
  --success: #34d399;
  --text: #f8fafc;
  --muted: #94a3b8;
  --board-line: rgba(148, 163, 184, 0.35);
  --board-hover: rgba(56, 189, 248, 0.25);
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}

.cached-error-question {
  width: 1px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: #1d4ed8;
  color: var(--text);
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 32px 16px;
}

#app {
  width: min(1200px, 100%);
  min-height: 680px;
  background: linear-gradient(145deg, rgba(15, 23, 42, 0.92), rgba(15, 23, 42, 0.75));
  border-radius: 24px;
  box-shadow: 0 40px 80px rgba(15, 23, 42, 0.45);
  overflow: hidden;
  position: relative;
}

.screen {
  display: none;
  height: 100%;
  padding: 32px 48px;
}

.screen:not(.hidden) {
  display: flex;
  flex-direction: column;
}

.panel {
  margin: auto;
  width: min(420px, 100%);
  background: var(--panel-bg);
  border-radius: 20px;
  padding: 48px 40px;
  text-align: center;
  box-shadow: 0 24px 50px rgba(2, 6, 23, 0.35);
}

.panel h1 {
  font-size: 2.2rem;
  margin-bottom: 12px;
}

.subtitle {
  color: var(--muted);
  margin-bottom: 24px;
}

form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

input[type="text"] {
  padding: 14px 18px;
  border: 1px solid rgba(148, 163, 184, 0.3);
  border-radius: 12px;
  background: rgba(15, 23, 42, 0.6);
  color: var(--text);
  font-size: 1rem;
  transition: border 0.2s ease, box-shadow 0.2s ease;
}

input[type="text"]:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.25);
}

button {
  padding: 12px 20px;
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: #0f172a;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}

button:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 10px 20px rgba(56, 189, 248, 0.35);
}

button:disabled {
  cursor: not-allowed;
  background: rgba(148, 163, 184, 0.25);
  color: var(--muted);
}

button.secondary {
  background: rgba(148, 163, 184, 0.2);
  color: var(--text);
}

button.secondary:hover:not(:disabled) {
  background: rgba(148, 163, 184, 0.35);
  box-shadow: none;
}

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

.error {
  color: var(--danger);
  min-height: 20px;
}

.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
}

.toolbar h2 {
  margin: 0 0 4px;
}

.toolbar p {
  margin: 0;
  color: var(--muted);
}

.lobby-layout {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 24px;
  margin-top: 24px;
}

.lobby-controls {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.user-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}

.user-row {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px;
  border-radius: 16px;
  background: var(--panel-bg);
  border: 1px solid rgba(148, 163, 184, 0.2);
  box-shadow: 0 10px 30px rgba(2, 6, 23, 0.3);
}

.user-row .avatar {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: linear-gradient(135deg, rgba(56, 189, 248, 0.35), rgba(56, 189, 248, 0.05));
}

.user-row .details {
  flex: 1;
}

.user-row .username {
  margin: 0 0 6px;
  font-weight: 600;
  font-size: 1.05rem;
}

.user-row .meta {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.game-layout {
  margin-top: 24px;
  flex: 1;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: center;
  gap: 24px;
}

.board-grid {
  background: rgba(15, 23, 42, 0.6);
  border-radius: 20px;
  padding: 16px;
  display: grid;
  grid-template-columns: repeat(17, minmax(22px, 1fr));
  grid-template-rows: repeat(17, minmax(22px, 1fr));
  gap: 2px;
  box-shadow: inset 0 0 0 1px rgba(148, 163, 184, 0.1);
  flex: 1 1 520px;
  max-width: min(680px, 100%);
}

.board-cell {
  width: 100%;
  aspect-ratio: 1 / 1;
  border: 1px solid var(--board-line);
  background: rgba(15, 23, 42, 0.45);
  border-radius: 6px;
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s ease, transform 0.2s ease;
}

.board-cell:hover {
  background: var(--board-hover);
  transform: translateY(-1px);
}

.board-cell.mark-x {
  color: #22d3ee;
  background: rgba(34, 211, 238, 0.12);
  border-color: rgba(34, 211, 238, 0.4);
}

.board-cell.mark-o {
  color: #f87171;
  background: rgba(248, 113, 113, 0.12);
  border-color: rgba(248, 113, 113, 0.4);
}

.board-cell.win {
  animation: pulse 0.9s ease-in-out infinite alternate;
}

@keyframes pulse {
  from {
    box-shadow: 0 0 0 rgba(250, 204, 21, 0.2);
  }
  to {
    box-shadow: 0 0 12px rgba(250, 204, 21, 0.45);
  }
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 20px;
  flex: 0 0 320px;
  max-width: 100%;
}

.player-card {
  display: flex;
  gap: 16px;
  background: var(--panel-bg);
  padding: 16px;
  border-radius: 16px;
  align-items: center;
  border: 1px solid rgba(148, 163, 184, 0.25);
  transition: border 0.25s ease, box-shadow 0.25s ease;
}

.player-card.active {
  border-color: var(--accent);
  box-shadow: 0 12px 24px rgba(56, 189, 248, 0.25);
}

.player-card .mark {
  display: inline-flex;
  width: 46px;
  height: 46px;
  align-items: center;
  justify-content: center;
  border-radius: 14px;
  font-weight: 700;
  font-size: 1.2rem;
}

.player-card[data-mark="x"] .mark {
  background: rgba(34, 211, 238, 0.2);
  color: #22d3ee;
}

.player-card[data-mark="o"] .mark {
  background: rgba(248, 113, 113, 0.2);
  color: #f87171;
}

.player-card .name {
  margin: 0;
  font-weight: 600;
}

.player-card .status {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.timer-panel {
  background: var(--panel-bg);
  border-radius: 16px;
  padding: 20px;
  border: 1px solid rgba(148, 163, 184, 0.25);
  text-align: center;
}

.turn-indicator {
  font-size: 1.4rem;
  font-weight: 700;
  margin: 4px 0;
}

.countdown {
  font-size: 1.6rem;
  font-weight: 600;
  color: var(--accent);
  margin: 0;
}

.chat-panel {
  flex: 0 0 360px;
  height: 360px;
  max-height: 360px;
  background: var(--panel-bg);
  border-radius: 16px;
  border: 1px solid rgba(148, 163, 184, 0.2);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.chat-panel h3 {
  margin: 0;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
  padding-right: 4px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.chat-message {
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(15, 23, 42, 0.7);
  border: 1px solid rgba(148, 163, 184, 0.2);
  font-size: 0.95rem;
}

.chat-message.self {
  align-self: flex-end;
  background: rgba(56, 189, 248, 0.2);
  border-color: rgba(56, 189, 248, 0.4);
}

.chat-message .author {
  font-weight: 600;
  margin-bottom: 4px;
}

.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(15, 23, 42, 0.7);
  backdrop-filter: blur(6px);
  z-index: 20;
}

.modal.hidden {
  display: none;
}

.modal-content {
  background: var(--panel-bg);
  padding: 0px 32px 28px;
  border-radius: 20px;
  min-width: 300px;
  max-width: 380px;
  text-align: center;
  box-shadow: 0 24px 60px rgba(2, 6, 23, 0.5);
  border: 1px solid rgba(148, 163, 184, 0.25);
}

.modal-actions {
  display: flex;
  gap: 12px;
  margin-top: 24px;
}

#toast-container {
  position: fixed;
  right: 24px;
  bottom: 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  z-index: 1500;
}

.toast {
  padding: 14px 18px;
  border-radius: 14px;
  background: rgba(15, 23, 42, 0.85);
  border: 1px solid rgba(148, 163, 184, 0.25);
  box-shadow: 0 14px 32px rgba(2, 6, 23, 0.4);
  min-width: 220px;
  animation: slide-in 0.3s ease;
}

.toast.success {
  border-color: rgba(52, 211, 153, 0.6);
}

.toast.error {
  border-color: rgba(248, 113, 113, 0.6);
}

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

.floating-chat {
  position: fixed;
  right: 24px;
  bottom: 24px;
  width: min(520px, calc(100% - 48px));
  min-height: 520px;
  max-height: 520px;
  background: var(--panel-bg);
  border: 1px solid rgba(148, 163, 184, 0.3);
  border-radius: 16px;
  box-shadow: 0 18px 44px rgba(2, 6, 23, 0.45);
  display: flex;
  flex-direction: column;
  z-index: 1200;
  overflow: hidden;
  transition: transform 0.3s ease;
}

.floating-chat.collapsed {
  transform: translateX(calc(100% - 52px));
}

.floating-body {
  display: flex;
  flex-direction: column;
  flex: 1;
  gap: 12px;
  padding: 12px 16px 16px;
  overflow: hidden;
}
.floating-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  scrollbar-width: none;
  -ms-overflow-style: none;
  scroll-behavior: smooth;
}
.floating-messages::-webkit-scrollbar {
  display: none;
}

.floating-toggle {
  background: rgba(56, 189, 248, 0.2);
  border: 1px solid rgba(56, 189, 248, 0.4);
  color: var(--accent);
  border-radius: 999px;
  padding: 4px 10px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.3s ease, background 0.2s ease;
}

.floating-toggle:hover {
  background: rgba(56, 189, 248, 0.35);
}

.floating-chat.has-unread .floating-toggle {
  background: rgba(248, 113, 113, 0.35);
  border-color: rgba(248, 113, 113, 0.55);
  color: #f97316;
}

.floating-chat.collapsed .floating-title {
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  font-size: 0.85rem;
}

.eq-button {
  width: 250px;
}

.floating-title {
  padding-left: 10px;
}

.floating-chat.collapsed .floating-toggle {
  transform: rotate(180deg);
}

.floating-chat.collapsed .floating-body {
  display: none;
}

.floating-message {
  background: rgba(15, 23, 42, 0.7);
  border: 1px solid rgba(148, 163, 184, 0.3);
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 0.95rem;
  overflow-wrap: anywhere;
}

.floating-message.self {
  align-self: flex-end;
  background: rgba(56, 189, 248, 0.22);
  border-color: rgba(56, 189, 248, 0.4);
}

.floating-message .meta {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 4px;
}

.floating-form {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  gap: 8px;
  align-items: center;
  flex-shrink: 0;
  width: 100%;
}

.floating-form input[type="text"] {
  flex: 1;
  min-width: 0;
}

.floating-form button {
  flex: 0 0 auto;
  padding: 10px 14px;
  background: var(--accent-strong);
  color: #0f172a;
  white-space: nowrap;
}

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

.hidden {
  display: none !important;
}

@media (max-width: 1080px) {
  body {
    padding: 16px;
  }
  #app {
    border-radius: 16px;
  }
  .screen {
    padding: 24px;
  }
  .board-grid {
    flex: 1 1 100%;
    order: 2;
  }
  .sidebar {
    order: 1;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    gap: 16px;
  }
  .chat-panel {
    flex: 1 1 100%;
    height: 280px;
    max-height: 280px;
  }
  .floating-chat {
    right: 16px;
    bottom: 16px;
    width: min(280px, calc(100% - 32px));
    max-height: 55vh;
  }
}