/* ====== 全局变量 & 重置 ====== */
:root {
  --bg: #0f0f1a;
  --bg-card: #1a1a2e;
  --bg-card-hover: #22223a;
  --text: #e0e0e0;
  --text-dim: #8888aa;
  --primary: #4fc3f7;
  --primary-dark: #0288d1;
  --accent: #ff6b9d;
  --accent-dark: #c2185b;
  --success: #66bb6a;
  --warning: #ffa726;
  --danger: #ef5350;
  --border: #2a2a4a;
  --radius: 10px;
  --shadow: 0 4px 20px rgba(0,0,0,0.4);
}

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

body {
  font-family: 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
}

/* ====== 屏幕切换 ====== */
.screen {
  display: none;
  width: 100%;
  min-height: 100vh;
}
.screen.active {
  display: flex;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ====== 通用组件 ====== */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}
.card h3, .card h4 {
  margin-bottom: 12px;
  font-size: 14px;
  color: var(--primary);
  letter-spacing: 1px;
}

.badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 12px;
  font-size: 12px;
  background: var(--border);
  color: var(--text-dim);
}

.hidden { display: none !important; }

.btn {
  padding: 8px 18px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg-card);
  color: var(--text);
  cursor: pointer;
  font-size: 14px;
  transition: all 0.2s;
  white-space: nowrap;
}
.btn:hover { background: var(--bg-card-hover); border-color: var(--primary); }
.btn:active { transform: scale(0.97); }
.btn-sm { padding: 4px 12px; font-size: 12px; }
.btn-lg { padding: 12px 28px; font-size: 16px; font-weight: 600; }
.btn-primary { background: var(--primary-dark); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary); }
.btn-accent { background: var(--accent-dark); border-color: var(--accent); color: #fff; }
.btn-accent:hover { background: var(--accent); }
.btn-secondary { background: transparent; border-color: var(--text-dim); }
.btn-danger { background: #5c1a1a; border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }

.btn-group {
  display: flex;
  gap: 6px;
}
.btn-group .btn.selected {
  background: var(--primary-dark);
  border-color: var(--primary);
  color: #fff;
}

input[type="text"] {
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  flex: 1;
  outline: none;
}
input[type="text"]:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(79,195,247,0.2);
}

.form-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.option-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}
.option-row label:first-child {
  font-size: 13px;
  color: var(--text-dim);
}

.checkboxes {
  display: flex;
  gap: 12px;
  font-size: 13px;
}
.checkboxes label {
  display: flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
}

.button-row {
  display: flex;
  gap: 12px;
  margin-top: 16px;
}
.button-row .btn { flex: 1; }

.divider {
  text-align: center;
  margin: 16px 0;
  color: var(--text-dim);
  font-size: 12px;
  position: relative;
}
.divider::before, .divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 40%;
  height: 1px;
  background: var(--border);
}
.divider::before { left: 0; }
.divider::after { right: 0; }

/* ====== 大厅 ====== */
.lobby-container {
  max-width: 600px;
  margin: 0 auto;
  padding: 30px 20px;
  width: 100%;
}

.logo-section {
  text-align: center;
  margin-bottom: 30px;
}
.game-title {
  font-size: 48px;
  font-weight: 800;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  text-shadow: none;
  letter-spacing: 4px;
}
.subtitle {
  color: var(--text-dim);
  font-size: 14px;
  letter-spacing: 8px;
  margin-top: 6px;
}

.mode-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
.mode-card {
  text-align: center;
  padding: 14px 8px;
  border: 2px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.2s;
}
.mode-card:hover {
  border-color: var(--text-dim);
  background: var(--bg-card-hover);
}
.mode-card.selected {
  border-color: var(--primary);
  background: rgba(79,195,247,0.1);
}
.mode-icon { font-size: 28px; margin-bottom: 6px; }
.mode-name { font-size: 14px; font-weight: 600; margin-bottom: 4px; }
.mode-desc { font-size: 11px; color: var(--text-dim); line-height: 1.4; }

.toggle-header {
  cursor: pointer;
  user-select: none;
}
.toggle-header .arrow {
  float: right;
  transition: transform 0.2s;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
th, td {
  padding: 6px 10px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}
th { color: var(--text-dim); font-weight: 500; }

/* ====== 等候室 ====== */
.room-container {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  width: 100%;
}
.room-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}
.room-header h2 {
  font-size: 18px;
  color: var(--primary);
}
.room-info {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
}

.player-slots {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 10px;
  margin-bottom: 16px;
}
.player-slot {
  padding: 14px;
  border: 2px solid var(--border);
  border-radius: 10px;
  text-align: center;
  min-height: 80px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
}
.player-slot.occupied {
  border-color: var(--success);
  background: rgba(102,187,106,0.05);
}
.player-slot.ready {
  border-color: var(--primary);
  background: rgba(79,195,247,0.1);
}
.player-slot .slot-name { font-weight: 600; margin-bottom: 4px; }
.player-slot .slot-status { font-size: 12px; color: var(--text-dim); }
.player-slot .slot-color {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  margin-bottom: 6px;
}

.room-actions {
  display: flex;
  gap: 10px;
  margin-bottom: 16px;
  flex-wrap: wrap;
  align-items: center;
}
.add-ai-row {
  display: flex;
  gap: 6px;
  align-items: center;
}
.select-sm {
  padding: 5px 8px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
}
.select-sm:focus {
  outline: none;
  border-color: var(--primary);
}

.chat-box {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.chat-messages {
  height: 150px;
  overflow-y: auto;
  padding: 10px;
  font-size: 13px;
}
.chat-messages .chat-msg {
  margin-bottom: 4px;
  line-height: 1.4;
}
.chat-messages .chat-msg .chat-author {
  font-weight: 600;
  margin-right: 6px;
}
.chat-messages .system-msg {
  color: var(--text-dim);
  font-style: italic;
}
.chat-input-row {
  display: flex;
  border-top: 1px solid var(--border);
}
.chat-input-row input {
  border: none;
  border-radius: 0;
  flex: 1;
}
.chat-input-row .btn {
  border: none;
  border-radius: 0;
  border-left: 1px solid var(--border);
}

/* ====== 游戏界面 ====== */
.game-layout {
  display: flex;
  width: 100%;
  height: 100vh;
  overflow: hidden;
}

.game-sidebar {
  width: 240px;
  min-width: 200px;
  padding: 12px;
  overflow-y: auto;
  background: var(--bg-card);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.right-sidebar {
  border-right: none;
  border-left: 1px solid var(--border);
}

.game-center {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  background: radial-gradient(ellipse at center, #1a1a30, var(--bg));
  overflow: hidden;
}

canvas {
  cursor: pointer;
  max-width: 100%;
  max-height: 100%;
}

.turn-info {
  text-align: center;
  padding: 10px;
  background: var(--bg);
  border-radius: var(--radius);
}
.turn-number {
  font-size: 12px;
  color: var(--text-dim);
}
.turn-number span {
  font-weight: 700;
  color: var(--primary);
  font-size: 18px;
}
.current-player {
  font-size: 16px;
  font-weight: 700;
  margin-top: 4px;
}

.players-panel {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.player-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  border-radius: 6px;
  font-size: 13px;
  border: 1px solid transparent;
  transition: all 0.2s;
}
.player-row.active-turn {
  border-color: var(--primary);
  background: rgba(79,195,247,0.08);
}
.player-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.player-row .player-name { flex: 1; }
.player-row .player-score {
  font-size: 12px;
  color: var(--text-dim);
}
.player-row .player-pieces {
  font-size: 11px;
  color: var(--text-dim);
}

.event-log {
  flex: 1;
  min-height: 100px;
}
.event-log h4 {
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 6px;
}
.event-list {
  font-size: 12px;
  max-height: 200px;
  overflow-y: auto;
}
.event-list .event-item {
  padding: 4px 0;
  border-bottom: 1px solid rgba(42,42,74,0.5);
  line-height: 1.4;
}
.event-list .event-item .event-turn {
  color: var(--text-dim);
  font-size: 10px;
}

.piece-legend, .terrain-legend {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
}
.terrain-legend {
  gap: 6px;
}
.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
}
.terrain-legend-item {
  align-items: center;
  gap: 8px;
  padding: 3px 4px;
  border-radius: 6px;
  transition: background 0.15s;
}
.terrain-legend-item:hover {
  background: rgba(255,255,255,0.04);
}
.terrain-preview {
  flex-shrink: 0;
  border-radius: 4px;
}
.terrain-legend-text {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}
.terrain-legend-name {
  font-weight: 600;
  font-size: 12px;
  color: var(--text);
}
.terrain-legend-desc {
  font-size: 10px;
  color: var(--text-dim);
  line-height: 1.3;
}
.legend-swatch {
  width: 18px;
  height: 18px;
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  flex-shrink: 0;
}

.piece-info {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 20px;
  text-align: center;
  z-index: 10;
  min-width: 200px;
}
.piece-info .piece-name { font-weight: 700; font-size: 16px; }
.piece-info .piece-desc { font-size: 12px; color: var(--text-dim); margin-top: 4px; }
.piece-info .piece-hp { margin-top: 4px; }
.piece-info .piece-ability-status {
  margin-top: 4px;
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 6px;
  display: inline-block;
}
.piece-info .piece-ability-status.ready {
  color: #a569bd;
  background: rgba(165,105,189,0.12);
  animation: abilityPulse 2s ease infinite;
}
.piece-info .piece-ability-status.cd {
  color: var(--text-dim);
  background: rgba(255,255,255,0.05);
}
@keyframes abilityPulse {
  0%, 100% { opacity: 0.7; }
  50% { opacity: 1; }
}

.ability-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 2px;
}
.ability-title {
  font-weight: 700;
  font-size: 13px;
}
.ability-cd-tag {
  font-size: 10px;
  padding: 1px 6px;
  border-radius: 8px;
  background: rgba(255,255,255,0.08);
  color: var(--text-dim);
}
.ability-hint {
  font-size: 11px;
  color: var(--text-dim);
  margin: 2px 0 8px;
  line-height: 1.3;
}
.ability-options {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.ability-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 8px;
  border: 1.5px solid rgba(255,255,255,0.08);
  background: rgba(255,255,255,0.03);
  cursor: pointer;
  transition: all 0.2s;
  color: var(--text);
  text-align: left;
}
.ability-btn:hover {
  background: rgba(255,255,255,0.06);
  border-color: rgba(255,255,255,0.15);
}
.ability-btn.active {
  border-color: var(--accent);
  background: rgba(255,107,157,0.12);
  box-shadow: 0 0 12px rgba(255,107,157,0.2);
}
.ability-btn-icon {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  border: 1.5px solid;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  flex-shrink: 0;
}
.ability-btn-text {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}
.ability-btn-name {
  font-weight: 600;
  font-size: 12px;
}
.ability-btn-desc {
  font-size: 10px;
  color: var(--text-dim);
  line-height: 1.2;
}
.ability-cancel {
  margin-top: 6px;
  width: 100%;
  font-size: 11px;
  opacity: 0.7;
}
.ability-cancel:hover {
  opacity: 1;
}

.game-actions {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* ====== 模态框 ====== */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(4px);
}
.modal.hidden { display: none; }
.modal-content {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 30px;
  text-align: center;
  min-width: 320px;
  max-width: 500px;
  box-shadow: var(--shadow);
  animation: modalIn 0.3s ease;
}
@keyframes modalIn {
  from { opacity: 0; transform: scale(0.9); }
  to { opacity: 1; transform: scale(1); }
}
.modal-content h2 {
  font-size: 24px;
  margin-bottom: 16px;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.modal-actions {
  margin-top: 20px;
}
.ranking-table {
  width: 100%;
  margin: 12px 0;
}
.ranking-table td {
  padding: 8px;
}
.ranking-table .rank-1 { color: #ffd700; font-weight: 700; }
.ranking-table .rank-2 { color: #c0c0c0; font-weight: 600; }
.ranking-table .rank-3 { color: #cd7f32; }

/* ====== 浮动返回按钮 ====== */
.btn-float-back {
  position: fixed;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 90;
  padding: 12px 28px;
  border: none;
  border-radius: 30px;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  color: #fff;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 20px rgba(99,102,241,0.4);
  animation: floatBtnIn 0.4s ease;
}
.btn-float-back:hover {
  filter: brightness(1.15);
  transform: translateX(-50%) scale(1.05);
}
.btn-float-back.hidden { display: none; }
@keyframes floatBtnIn {
  from { opacity: 0; transform: translateX(-50%) translateY(20px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ====== 事件弹窗 ====== */
.event-popup {
  position: fixed;
  top: 18%;
  left: 50%;
  transform: translateX(-50%);
  z-index: 90;
  pointer-events: none;
}
.event-popup-content {
  background: var(--bg-card);
  border: 2px solid var(--warning);
  border-radius: 16px;
  padding: 24px 44px 20px;
  text-align: center;
  min-width: 240px;
  max-width: 360px;
  box-shadow: 0 0 50px rgba(255,167,38,0.3);
  animation: eventPop 0.5s cubic-bezier(.34,1.56,.64,1), eventFade 0.6s ease 3.3s forwards;
  backdrop-filter: blur(8px);
}
/* 事件类型颜色主题 */
.event-type-collapse { border-color: #e74c3c; box-shadow: 0 0 50px rgba(231,76,60,0.35); }
.event-type-blessing { border-color: #f1c40f; box-shadow: 0 0 50px rgba(241,196,15,0.35); }
.event-type-portal  { border-color: #a569bd; box-shadow: 0 0 50px rgba(165,105,189,0.35); }
.event-type-freeze  { border-color: #5dade2; box-shadow: 0 0 50px rgba(93,173,226,0.35); }
.event-type-heal    { border-color: #2ecc71; box-shadow: 0 0 50px rgba(46,204,113,0.35); }
.event-type-quake   { border-color: #e67e22; box-shadow: 0 0 50px rgba(230,126,34,0.35); }
.event-type-ability { border-color: #a569bd; box-shadow: 0 0 50px rgba(165,105,189,0.35); }

@keyframes eventPop {
  0%   { opacity: 0; transform: scale(0.3) translateY(20px); }
  100% { opacity: 1; transform: scale(1) translateY(0); }
}
@keyframes eventFade {
  to { opacity: 0; transform: translateY(-30px) scale(0.9); }
}
.event-icon {
  font-size: 44px;
  margin-bottom: 6px;
  animation: eventIconPulse 0.6s ease;
}
@keyframes eventIconPulse {
  0%   { transform: scale(0.5); }
  50%  { transform: scale(1.3); }
  100% { transform: scale(1); }
}
.event-text {
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 4px;
  letter-spacing: 1px;
}
.event-desc {
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1.4;
  margin-bottom: 2px;
}
.event-detail {
  font-size: 12px;
  color: var(--warning);
  font-weight: 600;
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px solid rgba(255,255,255,0.08);
}

/* ====== Toast ====== */
.toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.toast {
  padding: 10px 20px;
  border-radius: 8px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  font-size: 13px;
  animation: toastIn 0.3s ease, toastOut 0.3s ease 2.7s forwards;
  max-width: 300px;
}
.toast.error { border-color: var(--danger); }
.toast.success { border-color: var(--success); }
.toast.info { border-color: var(--primary); }
@keyframes toastIn {
  from { opacity: 0; transform: translateX(40px); }
  to { opacity: 1; transform: translateX(0); }
}
@keyframes toastOut {
  to { opacity: 0; transform: translateX(40px); }
}

/* ====== 响应式 ====== */
@media (max-width: 900px) {
  .game-layout {
    flex-direction: column;
    height: auto;
    min-height: 100vh;
  }
  .game-sidebar {
    width: 100%;
    flex-direction: row;
    flex-wrap: wrap;
    border-right: none;
    border-bottom: 1px solid var(--border);
    overflow-x: auto;
    min-width: 0;
    max-height: 180px;
  }
  .right-sidebar {
    border-left: none;
    border-top: 1px solid var(--border);
    border-bottom: none;
  }
  .game-center {
    min-height: 50vh;
  }
  .event-log { display: none; }
  .players-panel { flex-direction: row; gap: 6px; }
}

@media (max-width: 600px) {
  .mode-grid {
    grid-template-columns: 1fr;
  }
  .button-row {
    flex-direction: column;
  }
  .game-title { font-size: 32px; }
}

/* ====== 滚动条 ====== */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-dim); }
