* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  background: #0f0f1a;
  color: #e0e0e0;
  min-height: 100vh;
  min-height: 100dvh;
  overflow: hidden;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.hidden { display: none !important; }

.screen {
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Main Menu */
.menu-card {
  text-align: center;
  padding: 3rem 4rem;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
  border-radius: 20px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  border: 1px solid rgba(255,255,255,0.08);
}

.menu-logo {
  width: 96px;
  height: 96px;
  border-radius: 22%;
  margin-bottom: 1rem;
  object-fit: cover;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
}

.title {
  font-size: 3.5rem;
  background: linear-gradient(135deg, #E74C3C, #F39C12);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: 0.3em;
}

.subtitle {
  font-size: 1.2rem;
  color: #888;
  letter-spacing: 0.5em;
  margin-bottom: 1rem;
}

.desc {
  color: #aaa;
  margin-bottom: 2rem;
  font-size: 0.95rem;
}

.menu-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  justify-content: center;
}

/* Rules Modal */
.rules-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(4px);
}

.rules-modal.hidden {
  display: none !important;
}

body.rules-open {
  overflow: hidden;
}

.rules-panel {
  width: min(640px, 100%);
  max-height: min(90vh, 900px);
  display: flex;
  flex-direction: column;
  background: linear-gradient(160deg, #1a1a2e 0%, #16213e 100%);
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.6);
  overflow: hidden;
}

.rules-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  flex-shrink: 0;
}

.rules-header h2 {
  margin: 0;
  font-size: 1.4rem;
  color: #e0e0e0;
}

.rules-close {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.08);
  color: #aaa;
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  transition: background 0.2s;
}

.rules-close:hover {
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
}

.rules-body {
  flex: 1;
  overflow-y: auto;
  padding: 1rem 1.5rem 1.5rem;
  -webkit-overflow-scrolling: touch;
}

.rules-section {
  margin-bottom: 1.5rem;
}

.rules-section h3 {
  font-size: 1.05rem;
  color: #F39C12;
  margin-bottom: 0.6rem;
}

.rules-section p,
.rules-section li {
  color: #bbb;
  font-size: 0.92rem;
  line-height: 1.65;
}

.rules-note {
  margin-bottom: 1rem;
}

.rules-flow {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.flow-step {
  display: flex;
  gap: 0.75rem;
  align-items: flex-start;
  padding: 0.75rem;
  background: rgba(255, 255, 255, 0.04);
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.06);
}

.flow-num {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: linear-gradient(135deg, #E74C3C, #C0392B);
  color: white;
  font-size: 0.85rem;
  font-weight: bold;
}

.flow-step strong {
  color: #e0e0e0;
  display: block;
  margin-bottom: 0.25rem;
}

.flow-step p {
  margin: 0;
}

.flow-arrow {
  text-align: center;
  color: #555;
  font-size: 1.2rem;
  line-height: 1;
}

.win-examples {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 0.75rem;
}

.win-card {
  text-align: center;
  padding: 0.75rem;
  background: rgba(255, 255, 255, 0.04);
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.win-card-invalid {
  border-color: rgba(231, 76, 60, 0.35);
}

.win-card canvas {
  width: 100%;
  height: auto;
  display: block;
  border-radius: 8px;
  background: #12121f;
  margin-bottom: 0.5rem;
}

.win-card h4 {
  font-size: 0.9rem;
  color: #e0e0e0;
  margin: 0 0 0.25rem;
}

.win-card p {
  margin: 0;
  font-size: 0.8rem;
  color: #888;
}

.rules-list {
  margin: 0;
  padding-left: 1.25rem;
}

.rules-list li {
  margin-bottom: 0.4rem;
}

.rules-footer {
  flex-shrink: 0;
  padding: 1rem 1.5rem;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  text-align: center;
}

.text-input {
  width: 100%;
  max-width: 320px;
  padding: 0.75rem 1rem;
  margin: 1rem 0;
  border-radius: 8px;
  border: 1px solid rgba(255,255,255,0.15);
  background: #1a1a2e;
  color: #e0e0e0;
  font-size: 1rem;
  outline: none;
}

.text-input:focus {
  border-color: #3498DB;
  box-shadow: 0 0 0 2px rgba(52, 152, 219, 0.25);
}

.nickname-card h2 {
  margin-bottom: 0.5rem;
  color: #e0e0e0;
}

.join-password-panel {
  margin: 1rem 0;
  padding: 1rem;
  background: #1a1a2e;
  border-radius: 8px;
  border: 1px solid rgba(255,255,255,0.08);
}

.join-password-panel p {
  margin-bottom: 0.5rem;
  color: #aaa;
}

.btn.ready-active {
  background: linear-gradient(135deg, #27AE60, #2ECC71);
}

.toast {
  position: fixed;
  bottom: 2rem;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(30, 30, 50, 0.95);
  color: #fff;
  padding: 0.75rem 1.5rem;
  border-radius: 8px;
  border: 1px solid rgba(255,255,255,0.15);
  z-index: 200;
  max-width: 90vw;
  text-align: center;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}

.reconnect-banner {
  position: fixed;
  top: env(safe-area-inset-top, 0);
  left: 0;
  right: 0;
  z-index: 300;
  background: rgba(243, 156, 18, 0.95);
  color: #1a1a2e;
  text-align: center;
  padding: 0.5rem 1rem;
  font-size: 0.9rem;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}

.reconnect-banner.success {
  background: rgba(46, 204, 113, 0.95);
}

/* Buttons */
.btn {
  padding: 0.75rem 1.5rem;
  border: none;
  border-radius: 8px;
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.2s;
  background: #2a2a4a;
  color: #e0e0e0;
}

.btn:hover:not(:disabled) { transform: translateY(-2px); box-shadow: 0 4px 12px rgba(0,0,0,0.3); }
.btn:disabled { opacity: 0.6; cursor: not-allowed; transform: none; }
.btn.primary { background: linear-gradient(135deg, #E74C3C, #C0392B); color: white; }
.btn.secondary { background: linear-gradient(135deg, #3498DB, #2980B9); color: white; }
.btn.danger { background: linear-gradient(135deg, #E74C3C, #922B21); color: white; }
.btn.back { margin-top: 1rem; }

/* Lobby */
.lobby-panel, .waiting-panel {
  background: #16213e;
  padding: 2rem;
  border-radius: 16px;
  width: 90%;
  max-width: 600px;
  border: 1px solid rgba(255,255,255,0.08);
}

.lobby-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.25rem;
}

.lobby-header h2 {
  margin: 0;
}

.lobby-player-id-bar {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.25rem;
  flex-shrink: 0;
}

.lobby-player-id-label {
  font-size: 0.75rem;
  color: #888;
}

.lobby-player-id-text {
  font-weight: 600;
  color: #F1C40F;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.lobby-player-id-edit {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.lobby-id-input {
  width: 120px;
  padding: 0.35rem 0.6rem;
  margin: 0;
  font-size: 0.9rem;
}

.btn-link {
  background: none;
  border: none;
  color: #5DADE2;
  padding: 0.15rem 0.4rem;
  font-size: 0.85rem;
  min-width: auto;
  box-shadow: none;
}

.btn-link:hover {
  color: #85C1E9;
  transform: none;
}

.btn-small {
  padding: 0.35rem 0.75rem;
  font-size: 0.85rem;
  min-width: auto;
}

.lobby-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin: 1rem 0;
  align-items: center;
}

.lobby-actions input[type="text"] {
  padding: 0.5rem 1rem;
  border-radius: 6px;
  border: 1px solid #333;
  background: #1a1a2e;
  color: #e0e0e0;
}

.join-by-id {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  align-items: center;
  margin-bottom: 1rem;
  padding: 1rem;
  background: #1a1a2e;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.06);
}

.join-by-id .text-input {
  flex: 1;
  min-width: 120px;
  margin: 0;
}

.join-by-id #join-room-id-input {
  max-width: 140px;
  font-family: monospace;
  letter-spacing: 0.05em;
  text-transform: lowercase;
}

.room-id-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin: 1rem 0 0.5rem;
  padding: 0.85rem 1rem;
  background: #1a1a2e;
  border-radius: 8px;
  border: 1px solid rgba(52, 152, 219, 0.25);
}

.room-id-label {
  color: #aaa;
  font-size: 0.9rem;
  flex-shrink: 0;
}

.room-id-code {
  flex: 1;
  font-family: monospace;
  font-size: 1.35rem;
  letter-spacing: 0.12em;
  color: #3498DB;
  user-select: all;
}

.room-id-hint {
  color: #777;
  font-size: 0.85rem;
  margin-bottom: 1rem;
}

.btn-copy {
  padding: 0.45rem 0.9rem;
  font-size: 0.9rem;
  flex-shrink: 0;
  background: #2a3a5a;
}

.btn-copy:hover:not(:disabled) {
  background: #3498DB;
}

.room-list {
  list-style: none;
  max-height: 300px;
  overflow-y: auto;
  margin: 1rem 0;
}

.room-list li {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 1rem;
  background: #1a1a2e;
  border-radius: 8px;
  margin-bottom: 0.5rem;
}

.room-list li.empty {
  justify-content: center;
  color: #666;
}

.room-id { font-family: monospace; color: #3498DB; flex: 1; }
.room-players { color: #aaa; }
.room-status { color: #F39C12; font-size: 0.85rem; }

.players-ready {
  margin: 1.5rem 0;
}

.player-slot {
  display: flex;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  background: #1a1a2e;
  border-radius: 8px;
  margin-bottom: 0.5rem;
}

/* Game Layout */
html.game-active,
body.game-active {
  overflow: hidden;
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  min-height: -webkit-fill-available;
  padding: 0;
  touch-action: manipulation;
  overscroll-behavior: none;
}

#game-screen {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  min-height: -webkit-fill-available;
  padding: 0;
  z-index: 50;
}

#game-screen:not(.hidden) {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: stretch;
}

#game-screen.hidden {
  display: none !important;
}

#game-screen:fullscreen,
#game-screen:-webkit-full-screen {
  background: #0f0f1a;
}

.game-layout {
  display: flex;
  flex: 1 1 auto;
  width: 100%;
  height: 100%;
  min-height: 0;
  min-width: 0;
  overflow: hidden;
}

.board-area {
  flex: 1 1 0;
  position: relative;
  min-height: 0;
  min-width: 0;
  background: #0f0f1a;
  overflow: hidden;
}

#board {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  cursor: pointer;
}

.info-panel {
  flex: 0 0 280px;
  max-width: 32%;
  background: #16213e;
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  border-left: 1px solid rgba(255,255,255,0.06);
  overflow-y: auto;
}

.panel-section h3 {
  font-size: 0.85rem;
  color: #888;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  margin-bottom: 0.5rem;
}

.turn {
  font-size: 1.3rem;
  font-weight: bold;
  padding: 0.75rem 1rem;
  border-radius: 8px;
  background: #1a1a2e;
}

.turn.red { color: #E74C3C; border-left: 4px solid #E74C3C; }
.turn.black { color: #5DADE2; border-left: 4px solid #2C3E50; }
.turn.draw { color: #F39C12; border-left: 4px solid #F39C12; }

.my-color {
  font-size: 1.5rem;
  font-weight: bold;
  padding: 0.75rem 1rem;
  border-radius: 8px;
  background: #1a1a2e;
  text-align: center;
}

.my-color.red { color: #E74C3C; border: 2px solid #E74C3C; }
.my-color.black { color: #5DADE2; border: 2px solid #2C3E50; }

#move-count {
  font-size: 2rem;
  font-weight: bold;
  color: #3498DB;
}

.log-section { flex: 1; min-height: 0; }

.game-log {
  max-height: 250px;
  overflow-y: auto;
  font-size: 0.85rem;
}

.log-entry {
  padding: 0.4rem 0;
  border-bottom: 1px solid rgba(255,255,255,0.05);
  color: #bbb;
}

.log-entry.chat-msg {
  color: #7ec8e3;
  font-style: italic;
}

.quick-chat-section {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
}

.quick-chat-section h3 {
  margin-bottom: 0.5rem;
  flex-shrink: 0;
}

.quick-chat-scroll {
  max-height: 200px;
  overflow-y: auto;
  padding-right: 0.25rem;
}

.quick-chat-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.35rem;
}

.quick-chat-btn {
  padding: 0.4rem 0.55rem;
  font-size: 0.78rem;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.06);
  color: #dce4ef;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
  text-align: left;
  line-height: 1.3;
  word-break: break-all;
}

.quick-chat-btn:hover:not(:disabled) {
  background: rgba(52, 152, 219, 0.25);
  border-color: rgba(52, 152, 219, 0.5);
}

.quick-chat-btn:active:not(:disabled) {
  transform: scale(0.97);
}

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

.chat-bubble {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  max-width: min(92%, 320px);
  padding: 0.55rem 0.85rem;
  border-radius: 16px;
  font-size: 0.85rem;
  line-height: 1.35;
  text-align: center;
  z-index: 15;
  pointer-events: none;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
  animation: chatBubbleIn 0.25s ease-out;
}

.chat-bubble.opponent {
  top: calc(env(safe-area-inset-top, 12px) + 52px);
  background: rgba(44, 62, 80, 0.95);
  color: #ecf0f1;
  border: 1px solid rgba(255, 255, 255, 0.15);
}

.chat-bubble.self {
  bottom: calc(env(safe-area-inset-bottom, 12px) + 64px);
  background: rgba(41, 128, 185, 0.95);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.2);
}

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

@keyframes chatBubbleIn {
  from {
    opacity: 0;
    transform: translateX(-50%) translateY(8px) scale(0.92);
  }
  to {
    opacity: 1;
    transform: translateX(-50%) translateY(0) scale(1);
  }
}

.mobile-quick-chat {
  position: absolute;
  bottom: calc(env(safe-area-inset-bottom, 12px) + 56px);
  left: env(safe-area-inset-left, 8px);
  right: env(safe-area-inset-right, 8px);
  z-index: 25;
  padding: 0.65rem;
  border-radius: 12px;
  background: rgba(22, 33, 62, 0.96);
  backdrop-filter: blur(10px);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
  max-height: 45vh;
  overflow-y: auto;
}

.mobile-quick-chat.hidden {
  display: none;
}

.mobile-quick-chat .quick-chat-grid {
  grid-template-columns: 1fr;
}

.actions {
  display: flex;
  gap: 0.5rem;
}

.rules-hint {
  margin-top: auto;
  padding: 1rem;
  background: #1a1a2e;
  border-radius: 8px;
  font-size: 0.8rem;
  color: #888;
}

.rules-hint h4 {
  color: #aaa;
  margin-bottom: 0.5rem;
}

.rules-hint ul {
  padding-left: 1.2rem;
}

.rules-hint li {
  margin-bottom: 0.3rem;
}

/* Game end overlay */
.game-end-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 20;
}

.game-end-overlay.show-button {
  pointer-events: auto;
  background: rgba(0, 0, 0, 0.45);
}

.game-end-content {
  text-align: center;
  padding: 1.5rem 2rem;
  opacity: 0;
  transform: scale(0.85);
  transition: opacity 0.5s ease, transform 0.5s ease;
}

.game-end-overlay.show-button .game-end-content {
  opacity: 1;
  transform: scale(1);
}

.game-end-content h2 {
  font-size: clamp(1.8rem, 6vw, 2.5rem);
  margin-bottom: 0.5rem;
  text-shadow: 0 2px 12px rgba(0, 0, 0, 0.8);
}

.game-end-content h2.win { color: #2ECC71; }
.game-end-content h2.lose { color: #E74C3C; }
.game-end-content h2.draw { color: #F39C12; }

.game-end-subtitle {
  color: #ccc;
  margin-bottom: 1.25rem;
  font-size: 0.95rem;
}

.game-end-btn {
  min-width: 160px;
}

.mobile-top-bar {
  display: none;
}

.mobile-bottom-bar {
  display: none;
}

@media (max-width: 768px) {
  body {
    padding: 0;
  }

  #game-screen {
    align-items: stretch;
    justify-content: stretch;
  }

  .game-layout {
    flex-direction: column;
  }

  .board-area {
    flex: 1 1 auto;
    width: 100%;
    height: 100%;
  }

  .info-panel {
    display: none;
  }

  .mobile-top-bar {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    position: absolute;
    top: env(safe-area-inset-top, 8px);
    left: env(safe-area-inset-left, 8px);
    right: env(safe-area-inset-right, 8px);
    z-index: 10;
    pointer-events: none;
  }

  .mobile-top-bar .turn,
  .mobile-top-bar .my-color {
    pointer-events: auto;
    font-size: 0.85rem;
    padding: 0.4rem 0.75rem;
    border-radius: 20px;
    background: rgba(22, 33, 62, 0.92);
    backdrop-filter: blur(8px);
    box-shadow: 0 2px 8px rgba(0,0,0,0.3);
  }

  .mobile-top-bar .my-color {
    font-size: 0.8rem;
    padding: 0.35rem 0.65rem;
  }

  .mobile-bottom-bar {
    display: flex;
    gap: 0.5rem;
    position: absolute;
    bottom: env(safe-area-inset-bottom, 12px);
    left: env(safe-area-inset-left, 12px);
    right: env(safe-area-inset-right, 12px);
    z-index: 10;
    justify-content: center;
  }

  .mobile-bottom-bar .btn {
    flex: 1;
    max-width: 140px;
    padding: 0.6rem 0.5rem;
    font-size: 0.85rem;
    background: rgba(42, 42, 74, 0.92);
    backdrop-filter: blur(8px);
    box-shadow: 0 2px 8px rgba(0,0,0,0.3);
  }

  .mobile-bottom-bar .btn.danger {
    background: linear-gradient(135deg, rgba(231, 76, 60, 0.95), rgba(146, 57, 33, 0.95));
  }

  .menu-card {
    padding: 2rem 1.5rem;
    margin: 1rem;
    max-width: calc(100vw - 2rem);
  }

  .title {
    font-size: 2.5rem;
  }
}
