/* ===== 全局重置 ===== */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

:root {
  --bg-gradient-start: #1a1a2e;
  --bg-gradient-end: #16213e;
  --card-bg: rgba(255, 255, 255, 0.06);
  --card-border: rgba(255, 255, 255, 0.12);
  --text-primary: #f0f0f0;
  --text-secondary: #a0a0b0;
  --accent: #4fc3f7;
  --accent-hover: #29b6f6;
  --success: #66bb6a;
  --error: #ef5350;
  --warning: #ffa726;
  --cell-bg: rgba(255, 255, 255, 0.08);
  --cell-hover: rgba(79, 195, 247, 0.2);
  --cell-correct: rgba(102, 187, 106, 0.4);
  --cell-wrong: rgba(239, 83, 80, 0.5);
  --radius: 12px;
  --transition: 0.15s ease;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: linear-gradient(135deg, var(--bg-gradient-start), var(--bg-gradient-end));
  color: var(--text-primary);
  display: flex;
  justify-content: center;
  align-items: center;
}

/* ===== 应用容器 ===== */
.app {
  width: 100%;
  height: 100%;
  max-width: 600px;
  display: flex;
  flex-direction: column;
  padding: clamp(8px, 2vw, 20px);
}

/* ===== 顶部栏 ===== */
.top-bar {
  display: flex;
  flex-direction: column;
  gap: clamp(6px, 1.5vw, 10px);
  margin-bottom: clamp(6px, 1.5vw, 12px);
  flex-shrink: 0;
}

.top-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.top-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.title {
  font-size: clamp(18px, 4vw, 26px);
  font-weight: 700;
  letter-spacing: 2px;
  background: linear-gradient(90deg, var(--accent), #b388ff);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* 用户徽章 */
.user-badge {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 12px;
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 20px;
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition);
  font-size: clamp(11px, 2.2vw, 14px);
}

.user-badge:hover {
  background: rgba(79, 195, 247, 0.15);
  border-color: var(--accent);
}

.user-icon {
  font-size: clamp(12px, 2.5vw, 16px);
}

.user-name {
  font-weight: 600;
  max-width: 100px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stats {
  display: flex;
  justify-content: center;
  gap: clamp(8px, 3vw, 24px);
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.stat-label {
  font-size: clamp(10px, 2vw, 13px);
  color: var(--text-secondary);
}

.stat-value {
  font-size: clamp(14px, 3vw, 20px);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  min-width: 3ch;
  text-align: center;
}

/* 游戏中操作栏 */
.game-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
}

.game-actions.hidden {
  display: none;
}

/* ===== 游戏区域 ===== */
.game-area {
  flex: 1;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 0;
}

/* ===== 网格 ===== */
.grid-container {
  width: 100%;
  aspect-ratio: 1 / 1;
  max-width: min(90vw, 520px, calc(100vh - 240px));
  max-height: calc(100vh - 240px);
  display: flex;
  align-items: center;
  justify-content: center;
}

.grid {
  width: 100%;
  height: 100%;
  display: grid;
  gap: clamp(2px, 0.6vw, 6px);
  padding: clamp(3px, 0.8vw, 6px);
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
}

.cell {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--cell-bg);
  border: 1px solid var(--card-border);
  border-radius: 6px;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition), transform var(--transition),
    border-color var(--transition);
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

/* 不同网格大小的字体自适应 */
.grid[data-size="3"] .cell { font-size: clamp(28px, 8vw, 48px); }
.grid[data-size="4"] .cell { font-size: clamp(22px, 6vw, 38px); }
.grid[data-size="5"] .cell { font-size: clamp(16px, 5vw, 32px); }
.grid[data-size="6"] .cell { font-size: clamp(13px, 4vw, 26px); }
.grid[data-size="7"] .cell { font-size: clamp(11px, 3.5vw, 22px); }
.grid[data-size="8"] .cell { font-size: clamp(10px, 3vw, 19px); }
.grid[data-size="9"] .cell { font-size: clamp(9px, 2.6vw, 17px); }

.cell:hover:not(.disabled):not(.correct) {
  background: var(--cell-hover);
  border-color: var(--accent);
}

.cell:active:not(.disabled) {
  transform: scale(0.94);
}

.cell.correct {
  background: var(--cell-correct);
  border-color: var(--success);
  color: var(--success);
  pointer-events: none;
  animation: pop 0.25s ease;
}

.cell.wrong {
  background: var(--cell-wrong);
  border-color: var(--error);
  animation: shake 0.3s ease;
}

.cell.disabled {
  pointer-events: none;
  opacity: 0.5;
}

@keyframes pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.12); }
  100% { transform: scale(1); }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

/* ===== 提示栏 ===== */
.hint-bar {
  margin-top: clamp(6px, 1.5vw, 12px);
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: clamp(13px, 2.8vw, 17px);
  color: var(--text-secondary);
  flex-shrink: 0;
}

.hint-number {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 36px;
  height: 34px;
  padding: 0 10px;
  background: var(--accent);
  color: #000;
  border-radius: 8px;
  font-weight: 700;
  font-size: clamp(15px, 3.2vw, 20px);
}

/* ===== 遮罩层 ===== */
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.75);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  border-radius: var(--radius);
  z-index: 10;
  animation: fadeIn 0.2s ease;
}

.overlay.hidden {
  display: none;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.overlay-content {
  width: 90%;
  max-width: 380px;
  max-height: 90%;
  overflow-y: auto;
  padding: clamp(14px, 3.5vw, 24px);
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: var(--radius);
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: clamp(8px, 2vw, 14px);
}

.overlay-content h2 {
  font-size: clamp(17px, 3.8vw, 22px);
}

.desc {
  color: var(--text-secondary);
  font-size: clamp(12px, 2.5vw, 14px);
}

.hint-text {
  color: var(--text-secondary);
  font-size: clamp(10px, 2vw, 12px);
  margin-top: 4px;
}

/* ===== 用户管理 ===== */
.user-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: 200px;
  overflow-y: auto;
}

.user-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid var(--card-border);
  border-radius: 8px;
  cursor: pointer;
  transition: background var(--transition), border-color var(--transition);
}

.user-item:hover {
  background: rgba(79, 195, 247, 0.15);
  border-color: var(--accent);
}

.user-item.active {
  background: rgba(79, 195, 247, 0.2);
  border-color: var(--accent);
}

.user-item-name {
  font-weight: 600;
  font-size: clamp(13px, 2.6vw, 15px);
}

.user-item-stats {
  font-size: clamp(10px, 2vw, 12px);
  color: var(--text-secondary);
}

.user-item-delete {
  background: none;
  border: none;
  color: var(--error);
  cursor: pointer;
  font-size: 16px;
  padding: 2px 6px;
  border-radius: 4px;
  opacity: 0.6;
  transition: opacity var(--transition);
}

.user-item-delete:hover {
  opacity: 1;
  background: rgba(239, 83, 80, 0.15);
}

.user-create {
  display: flex;
  gap: 8px;
}

.user-create input {
  flex: 1;
  padding: 8px 12px;
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid var(--card-border);
  border-radius: 6px;
  color: var(--text-primary);
  font-size: 14px;
  font-family: inherit;
}

.user-create input:focus {
  outline: none;
  border-color: var(--accent);
}

/* ===== 设置项 ===== */
.settings {
  display: flex;
  flex-direction: column;
  gap: 8px;
  text-align: left;
  padding: 10px;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 8px;
}

.settings label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: clamp(12px, 2.5vw, 14px);
  color: var(--text-secondary);
  cursor: pointer;
}

.settings select {
  flex: 1;
  padding: 5px 8px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid var(--card-border);
  border-radius: 6px;
  color: var(--text-primary);
  font-size: 13px;
  cursor: pointer;
  font-family: inherit;
}

.settings select:focus {
  outline: none;
  border-color: var(--accent);
}

.checkbox-label input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  cursor: pointer;
}

/* ===== 按钮 ===== */
.btn {
  padding: clamp(9px, 2.2vw, 13px) clamp(14px, 3.5vw, 22px);
  border: none;
  border-radius: 8px;
  font-size: clamp(13px, 2.8vw, 15px);
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition), transform var(--transition),
    opacity var(--transition);
  font-family: inherit;
}

.btn:active {
  transform: scale(0.97);
}

.btn-small {
  padding: 6px 14px;
  font-size: clamp(11px, 2.2vw, 13px);
}

.btn-primary {
  background: var(--accent);
  color: #000;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.1);
  color: var(--text-primary);
  border: 1px solid var(--card-border);
}

.btn-secondary:hover {
  background: rgba(255, 255, 255, 0.18);
}

.btn-warning {
  background: rgba(255, 167, 38, 0.2);
  color: var(--warning);
  border: 1px solid rgba(255, 167, 38, 0.4);
}

.btn-warning:hover {
  background: rgba(255, 167, 38, 0.3);
}

.btn-danger {
  background: rgba(239, 83, 80, 0.2);
  color: var(--error);
  border: 1px solid rgba(239, 83, 80, 0.4);
}

.btn-danger:hover {
  background: rgba(239, 83, 80, 0.3);
}

/* ===== 倒计时 ===== */
.countdown-number {
  font-size: clamp(80px, 25vw, 180px);
  font-weight: 800;
  color: var(--accent);
  text-shadow: 0 0 40px rgba(79, 195, 247, 0.5);
  animation: countdownPulse 0.8s ease infinite;
}

@keyframes countdownPulse {
  0% { transform: scale(0.8); opacity: 0.5; }
  50% { transform: scale(1.1); opacity: 1; }
  100% { transform: scale(1); opacity: 0.8; }
}

/* ===== 结果页 ===== */
.result-stats {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 8px;
}

.result-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.result-label {
  color: var(--text-secondary);
  font-size: clamp(11px, 2.3vw, 13px);
}

.result-value {
  font-weight: 700;
  font-size: clamp(14px, 3vw, 18px);
  font-variant-numeric: tabular-nums;
}

.result-message {
  font-size: clamp(12px, 2.6vw, 14px);
  color: var(--warning);
  min-height: 18px;
}

/* ===== 历史记录 ===== */
.history-content {
  max-height: 85vh;
}

.history-user {
  font-size: clamp(11px, 2.2vw, 13px);
  color: var(--accent);
  font-weight: normal;
}

.history-filter {
  display: flex;
  justify-content: center;
  margin-bottom: 4px;
}

.history-filter label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: clamp(11px, 2.2vw, 13px);
  color: var(--text-secondary);
}

.history-filter select {
  padding: 4px 8px;
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid var(--card-border);
  border-radius: 5px;
  color: var(--text-primary);
  font-size: 12px;
  cursor: pointer;
  font-family: inherit;
}

.history-list {
  max-height: 30vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.history-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 7px 10px;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 6px;
  font-size: clamp(10px, 2vw, 12px);
  gap: 6px;
}

.history-item .h-date {
  color: var(--text-secondary);
  flex-shrink: 0;
}

.history-item .h-grid {
  color: var(--text-secondary);
  font-size: 0.9em;
  flex-shrink: 0;
}

.history-item .h-time {
  font-weight: 600;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
}

.history-item .h-errors {
  color: var(--error);
  font-variant-numeric: tabular-nums;
  flex-shrink: 0;
}

.empty-hint {
  color: var(--text-secondary);
  text-align: center;
  padding: 16px;
  font-size: 13px;
}

.db-info {
  padding: 8px 10px;
  background: rgba(0, 0, 0, 0.15);
  border-radius: 6px;
  font-size: clamp(10px, 2vw, 11px);
  color: var(--text-secondary);
}

.db-info code {
  color: var(--accent);
  background: rgba(79, 195, 247, 0.1);
  padding: 1px 5px;
  border-radius: 3px;
}

.history-actions {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}

/* ===== 响应式：横屏手机 ===== */
@media (max-height: 500px) and (orientation: landscape) {
  .app {
    max-width: 900px;
    flex-direction: row;
    align-items: center;
    gap: 12px;
  }
  .top-bar {
    width: 170px;
    flex-shrink: 0;
    margin-bottom: 0;
  }
  .game-area {
    flex: 1;
  }
  .grid-container {
    max-width: min(80vh, 500px);
    max-height: 82vh;
  }
}

/* ===== 小屏优化 ===== */
@media (max-width: 360px) {
  .stats {
    gap: 10px;
  }
  .grid {
    gap: 2px;
    padding: 2px;
  }
}

/* ===== 游戏中图标按钮 ===== */
.btn-icon {
  padding: 6px 10px;
  font-size: 16px;
  line-height: 1;
  background: rgba(255, 255, 255, 0.1);
  color: var(--text-primary);
  border: 1px solid var(--card-border);
}
.btn-icon:hover { background: rgba(255, 255, 255, 0.2); }
.btn-icon.active { background: rgba(79, 195, 247, 0.25); border-color: var(--accent); }
.btn-icon.off { opacity: 0.45; }

/* ===== 区块标题 ===== */
.section-title {
  font-size: clamp(12px, 2.4vw, 14px);
  font-weight: 600;
  color: var(--text-secondary);
  text-align: left;
  margin-bottom: 6px;
  border-left: 3px solid var(--accent);
  padding-left: 8px;
}

/* ===== 开始页布局 ===== */
.start-content { max-height: 92%; }
.start-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

/* ===== 首页最近记录 ===== */
.home-history { margin-top: 4px; text-align: left; }
.home-history-list {
  max-height: 160px; overflow-y: auto;
  display: flex; flex-direction: column; gap: 4px;
}
.home-history-item {
  display: flex; justify-content: space-between; align-items: center;
  padding: 6px 10px; background: rgba(0, 0, 0, 0.18);
  border-radius: 5px; font-size: clamp(10px, 2vw, 12px); gap: 6px;
}
.home-history-item .h-date { color: var(--text-secondary); flex-shrink: 0; }
.home-history-item .h-grid { color: var(--text-secondary); font-size: 0.9em; flex-shrink: 0; }
.home-history-item .h-time { font-weight: 600; color: var(--accent); font-variant-numeric: tabular-nums; flex-shrink: 0; }
.home-history-item .h-errors { color: var(--error); font-variant-numeric: tabular-nums; flex-shrink: 0; }

/* ===== 结果页历史列表 ===== */
.result-content { max-height: 92%; }
.result-history { margin-top: 4px; text-align: left; }
.result-history-list {
  max-height: 140px; overflow-y: auto;
  display: flex; flex-direction: column; gap: 4px;
}
.result-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

/* ===== 正确点击不变色模式 ===== */
.cell.correct-no-color {
  background: rgba(255, 255, 255, 0.03);
  border-color: rgba(255, 255, 255, 0.06);
  color: var(--text-secondary);
  opacity: 0.4;
  pointer-events: none;
}

/* ===== 统计页面 ===== */
.stats-content { max-height: 90%; }
.stats-overview {
  display: grid; grid-template-columns: 1fr 1fr;
  gap: 8px; margin-bottom: 10px;
}
.stat-card {
  background: rgba(0, 0, 0, 0.2);
  border: 1px solid var(--card-border);
  border-radius: 8px; padding: 10px; text-align: center;
}
.stat-card .stat-card-label { font-size: clamp(10px, 2vw, 12px); color: var(--text-secondary); margin-bottom: 4px; }
.stat-card .stat-card-value { font-size: clamp(16px, 3.5vw, 22px); font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-card .stat-card-value.accent { color: var(--accent); }
.stat-card .stat-card-value.success { color: var(--success); }
.stat-card .stat-card-value.warning { color: var(--warning); }

.stats-grid-table { width: 100%; border-collapse: collapse; font-size: clamp(10px, 2vw, 12px); }
.stats-grid-table th, .stats-grid-table td {
  padding: 6px 4px; text-align: center;
  border-bottom: 1px solid var(--card-border); font-variant-numeric: tabular-nums;
}
.stats-grid-table th { color: var(--text-secondary); font-weight: 600; font-size: 0.9em; }
.stats-grid-table td.best { color: var(--success); font-weight: 600; }
.stats-grid-table tr:hover td { background: rgba(255, 255, 255, 0.03); }

/* ===== 分页控件 ===== */
.pagination {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  margin: 6px 0;
  flex-shrink: 0;
}

.pagination.hidden {
  display: none;
}

.page-info {
  font-size: clamp(11px, 2.2vw, 13px);
  color: var(--text-secondary);
  min-width: 70px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.pagination button:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}
