/* ============ CSS 变量 / 主题 ============ */
:root {
  --bg: #f0f4f8;
  --bg-card: #ffffff;
  --bg-board: #ffffff;
  --text: #1a1a2e;
  --text-secondary: #6c7293;
  --primary: #4a90d9;
  --primary-light: #e8f1fc;
  --accent: #f5a623;
  --error: #e74c3c;
  --given: #1a1a2e;
  --user: #4a90d9;
  --related-bg: #e8edf3;
  --same-num-bg: #d4e4f7;
  --selected-bg: #c8dcf0;
  --border: #d0d5e0;
  --border-bold: #4a5568;
  --shadow: rgba(0,0,0,0.08);
  --note-color: #8fa4bc;
  --note-bg: #f0f4f8;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

[data-theme="dark"] {
  --bg: #1a1a2e;
  --bg-card: #16213e;
  --bg-board: #1e2a4a;
  --text: #e0e0e0;
  --text-secondary: #8892b0;
  --primary: #64b5f6;
  --primary-light: #1a3a5c;
  --accent: #ffb74d;
  --error: #ef5350;
  --given: #e0e0e0;
  --user: #64b5f6;
  --related-bg: #2a3a5c;
  --same-num-bg: #3a5a7c;
  --selected-bg: #4a7ab0;
  --border: #2a3a5c;
  --border-bold: #8892b0;
  --shadow: rgba(0,0,0,0.3);
  --note-color: #7a8fa8;
  --note-bg: #1e2a4a;
}

/* ============ 基础重置 ============ */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC',
    'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

body {
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}

body.modal-open {
  overflow: hidden;
}

#app {
  max-width: 540px;
  margin: 0 auto;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding: 0 12px;
}

/* ============ 顶部栏 ============ */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 4px 8px;
  flex-shrink: 0;
}

.topbar-left .game-title {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
}

.topbar-center .timer {
  font-size: 24px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--primary);
  letter-spacing: 1px;
}

.topbar-right {
  display: flex;
  gap: 8px;
}

.icon-btn {
  background: var(--bg-card);
  border: none;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: var(--text);
  box-shadow: 0 1px 3px var(--shadow);
  transition: transform 0.15s;
}

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

/* ============ 控制条 ============ */
.control-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 0 12px;
  flex-shrink: 0;
}

.difficulty-selector {
  display: flex;
  gap: 4px;
  background: var(--bg-card);
  border-radius: 20px;
  padding: 3px;
  box-shadow: 0 1px 3px var(--shadow);
}

.diff-btn {
  border: none;
  background: transparent;
  padding: 6px 14px;
  border-radius: 18px;
  font-size: 13px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s;
  font-weight: 500;
}

.diff-btn.active {
  background: var(--primary);
  color: #fff;
}

.stats {
  display: flex;
  gap: 12px;
  font-size: 13px;
  color: var(--text-secondary);
}

.stat-item b {
  color: var(--text);
  font-weight: 600;
}

/* ============ 棋盘区 ============ */
.board-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  max-width: 480px;
  margin: 8px auto 4px;
  flex-shrink: 0;
}

.board {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 480px;
  aspect-ratio: 1;
  background: var(--bg-board);
  border: 3px solid var(--border-bold);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 4px 12px var(--shadow);
}

.board-row {
  display: flex;
  flex: 1;
}

.cell {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background-color 0.1s;
  min-height: 0;
  min-width: 0;
}

.cell:nth-child(9) {
  border-right: none;
}

.board-row:nth-child(9) .cell {
  border-bottom: none;
}

.border-right-bold {
  border-right: 2px solid var(--border-bold) !important;
}

.border-bottom-bold {
  border-bottom: 2px solid var(--border-bold) !important;
}

.cell-value {
  font-size: clamp(18px, 5vw, 28px);
  font-weight: 600;
  z-index: 2;
  line-height: 1;
}

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

.cell:not(.given) .cell-value {
  color: var(--user);
}

.cell.error {
  background: rgba(231, 76, 60, 0.15) !important;
}

.cell.error .cell-value {
  color: var(--error) !important;
}

.cell.related {
  background: var(--related-bg);
}

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

/* 选中数字标黄：含选中格本身 */
.cell.box-highlight {
  background: rgba(255, 200, 0, 0.35) !important;
}

.cell.box-highlight .cell-value {
  color: #b8860b;
}

/* 选中格同时有 box-highlight 时，黄色优先 */
.cell.selected.box-highlight {
  background: rgba(255, 200, 0, 0.45) !important;
}

.note-highlight {
  background: rgba(255, 200, 0, 0.45) !important;
  border-radius: 3px;
  color: #b8860b !important;
}

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

/* 笔记：每个候选数占 1/9 格子（3x3 网格） */
.cell-notes {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  z-index: 1;
  pointer-events: none;
}

.cell-notes .note-num {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(9px, 2.2vw, 14px);
  color: var(--note-color);
  font-weight: 600;
  line-height: 1;
}

.board.note-mode .cell:not(.given):hover {
  background: var(--primary-light);
}

/* ============ 底部数字键盘 ============ */
.bottom-numpad {
  display: flex;
  justify-content: center;
  align-items: stretch;
  gap: 8px;
  margin: 4px 0 8px;
  flex-shrink: 0;
  width: 100%;
}

.numpad-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  flex: 1;
  max-width: 200px;
}

.num-btn {
  border: none;
  border-radius: 8px;
  font-size: clamp(18px, 5vw, 28px);
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s, background-color 0.15s;
  min-height: 40px;
  aspect-ratio: 1;
  box-shadow: 0 1px 2px var(--shadow);
}

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

/* 左侧：填充数，有底色 */
.numpad-left .num-btn {
  background: var(--primary);
  color: #fff;
}

.numpad-left .num-btn:active {
  background: var(--primary-dark, #3a7bc8);
}

/* 填满后去掉底色（添加 .num-done 类） */
.numpad-left .num-btn.num-done {
  background: var(--bg-card);
  color: var(--text-secondary);
  opacity: 0.5;
}

/* 右侧：备选数，浅灰蓝色，无底色变化 */
.numpad-right .num-btn {
  background: var(--bg-card);
  color: var(--note-color);
  font-weight: 600;
}

.numpad-right .num-btn:active {
  background: var(--accent);
  color: #fff;
}

/* 中间清空按钮 */
.clear-btn {
  border: none;
  border-radius: 10px;
  width: auto;
  min-width: 80px;
  min-height: 56px;
  padding: 14px 20px;
  align-self: center;
  background: var(--bg-card);
  color: var(--error);
  font-size: clamp(16px, 3.8vw, 22px);
  font-weight: 700;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  box-shadow: 0 1px 3px var(--shadow);
  transition: transform 0.1s;
}

.clear-btn:active {
  transform: scale(0.9);
}

.clear-btn .clear-x {
  position: relative;
  z-index: 1;
}

/* ============ 操作栏 ============ */
.action-bar {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 4px;
  padding: 4px 0 16px;
  flex-shrink: 0;
}

.action-btn {
  border: none;
  background: var(--bg-card);
  border-radius: 10px;
  padding: 8px 2px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  color: var(--text-secondary);
  cursor: pointer;
  box-shadow: 0 1px 3px var(--shadow);
  transition: all 0.12s;
  min-height: 48px;
}

.action-btn span {
  font-size: 10px;
  font-weight: 500;
}

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

.action-btn.active {
  background: var(--primary);
  color: #fff;
}

.action-btn.active svg {
  fill: #fff;
}

/* 提示按钮：突出显示（绿色） */
.hint-btn {
  background: #27ae60;
  color: #fff;
}

.hint-btn svg {
  fill: #fff;
}

.hint-btn:active {
  background: #219a52;
}

/* 解密按钮：突出显示（橙色） */
.solve-btn {
  background: var(--accent);
  color: #fff;
}

.solve-btn svg {
  fill: #fff;
}

.solve-btn:active {
  background: #e09400;
}

.camera-btn {
  background: var(--primary);
  color: #fff;
}

.camera-btn svg {
  fill: #fff;
}

/* ============ 模态框 ============ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
  animation: fadeIn 0.2s;
}

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

.modal {
  background: var(--bg-card);
  border-radius: 16px;
  max-width: 420px;
  width: 100%;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
  animation: slideUp 0.25s;
  overflow: hidden;
}

/* 可拖动面板 */
.draggable-modal {
  position: relative;
  transition: opacity 0.15s;
}

.draggable-modal.dragging {
  position: fixed;
  margin: 0;
  animation: none;
  box-shadow: 0 8px 32px rgba(0,0,0,0.3);
}

.drag-handle {
  cursor: grab;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

.drag-handle:active {
  cursor: grabbing;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

.opacity-slider {
  width: 60px;
  height: 16px;
  cursor: pointer;
  accent-color: var(--primary);
  opacity: 0.8;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.modal-header h2 {
  font-size: 18px;
  font-weight: 600;
  color: var(--text);
}

.close-btn {
  background: none;
  border: none;
  font-size: 28px;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}

.modal-footer {
  display: flex;
  gap: 8px;
  padding: 16px 20px;
  border-top: 1px solid var(--border);
  justify-content: center;
  align-items: center;
  flex-shrink: 0;
}

/* ============ 按钮 ============ */
.btn-primary {
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 10px 24px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.12s;
}

.btn-primary:active {
  transform: scale(0.96);
}

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

.btn-secondary {
  background: transparent;
  color: var(--primary);
  border: 1.5px solid var(--primary);
  border-radius: 10px;
  padding: 10px 20px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.12s;
}

.btn-secondary:active {
  background: var(--primary-light);
}

/* ============ 设置面板 ============ */
.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}

.setting-row:last-child {
  border-bottom: none;
}

.setting-row label {
  font-size: 15px;
  color: var(--text);
}

.toggle-group {
  display: flex;
  gap: 4px;
  background: var(--bg);
  border-radius: 8px;
  padding: 3px;
}

.toggle-btn {
  border: none;
  background: transparent;
  padding: 6px 16px;
  border-radius: 6px;
  font-size: 13px;
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.2s;
}

.toggle-btn.active {
  background: var(--primary);
  color: #fff;
}

/* Switch */
.switch {
  position: relative;
  width: 44px;
  height: 24px;
}

.switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.slider {
  position: absolute;
  inset: 0;
  background: var(--border);
  border-radius: 12px;
  transition: 0.3s;
  cursor: pointer;
}

.slider:before {
  content: '';
  position: absolute;
  width: 18px;
  height: 18px;
  left: 3px;
  top: 3px;
  background: #fff;
  border-radius: 50%;
  transition: 0.3s;
}

.switch input:checked + .slider {
  background: var(--primary);
}

.switch input:checked + .slider:before {
  transform: translateX(20px);
}

/* ============ 相机面板 ============ */
.camera-preview-wrapper {
  width: 100%;
  aspect-ratio: 3/4;
  background: var(--bg);
  border-radius: 12px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  margin-bottom: 16px;
}

.camera-preview-wrapper video {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.camera-placeholder {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  color: var(--text-secondary);
  font-size: 14px;
  text-align: center;
}

.camera-placeholder .captured-img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

/* 四角拖动定位画布 */
.corner-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  cursor: crosshair;
  touch-action: none;
  z-index: 10;
}

.corner-hint {
  text-align: center;
  font-size: 13px;
  color: var(--note-color);
  margin: 4px 0;
}

.camera-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}

.file-label {
  cursor: pointer;
  display: inline-flex;
  align-items: center;
}

.recognize-status {
  text-align: center;
  font-size: 14px;
  color: var(--text-secondary);
  margin-top: 12px;
  min-height: 20px;
}

/* ============ 识别结果编辑 ============ */
.mini-board {
  max-width: 320px;
  margin: 16px auto;
  border-width: 2px;
}

.hint-text {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 12px;
}

.hint-technique {
  font-size: 15px;
  color: var(--primary);
  margin-bottom: 8px;
  font-weight: 600;
}

.hint-desc {
  font-size: 14px;
  color: var(--text);
  margin-bottom: 10px;
  line-height: 1.5;
}

.hint-analysis-scroll {
  max-height: 45vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-right: 4px;
  margin-bottom: 8px;
}

.hint-analysis-scroll::-webkit-scrollbar {
  width: 4px;
}

.hint-analysis-scroll::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 2px;
}

.hint-analysis {
  padding-left: 20px;
  margin: 8px 0;
}

.hint-analysis li {
  font-size: 13px;
  color: var(--text);
  line-height: 1.7;
  margin-bottom: 4px;
  white-space: pre-wrap;
}

.reveal-options {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.reveal-opt {
  text-align: center;
}

/* ============ 推理步骤 ============ */
.reasoning-content {
  max-height: 50vh;
  overflow-y: auto;
}

.reasoning-step {
  background: var(--bg);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 12px;
}

.step-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.step-num {
  font-size: 14px;
  font-weight: 600;
  color: var(--primary);
}

.step-tech {
  background: var(--primary-light);
  color: var(--primary);
  padding: 2px 10px;
  border-radius: 10px;
  font-size: 12px;
  font-weight: 500;
}

.step-desc {
  font-size: 14px;
  line-height: 1.6;
  color: var(--text);
  margin-bottom: 8px;
}

.step-action {
  font-size: 16px;
  font-weight: 600;
  color: var(--accent);
  padding: 8px 12px;
  background: var(--bg-card);
  border-radius: 8px;
  text-align: center;
}

.step-grid-hint {
  font-size: 13px;
  color: var(--text-secondary);
  text-align: center;
  margin-top: 8px;
  font-style: italic;
}

.step-indicator {
  font-size: 14px;
  color: var(--text-secondary);
  font-weight: 500;
  min-width: 60px;
  text-align: center;
}

/* ============ 完成面板 ============ */
.complete-modal {
  max-width: 340px;
}

.complete-body {
  text-align: center;
  padding: 32px 20px;
}

.complete-icon {
  font-size: 64px;
  margin-bottom: 16px;
  animation: bounce 0.6s;
}

.complete-body h2 {
  font-size: 24px;
  color: var(--text);
  margin-bottom: 16px;
}

.complete-stats {
  font-size: 15px;
  color: var(--text-secondary);
  line-height: 2;
}

.complete-stats b {
  color: var(--text);
}

/* ============ Toast ============ */
.toast {
  position: fixed;
  bottom: 20%;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.8);
  color: #fff;
  padding: 10px 24px;
  border-radius: 24px;
  font-size: 14px;
  z-index: 2000;
  opacity: 0;
  transition: opacity 0.3s, transform 0.3s;
  pointer-events: none;
  white-space: nowrap;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(-8px);
}

.toast.hidden {
  display: none;
}

/* ============ 加载遮罩 ============ */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(255, 255, 255, 0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 3000;
  gap: 16px;
}

[data-theme="dark"] .loading-overlay {
  background: rgba(26, 26, 46, 0.85);
}

.loading-overlay.hidden {
  display: none;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.loading-text {
  font-size: 14px;
  color: var(--text-secondary);
}

/* ============ 动画 ============ */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

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

@keyframes spin {
  to { transform: rotate(360deg); }
}

@keyframes bounce {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.2); }
}

/* ============ 响应式 ============ */
@media (max-width: 360px) {
  .action-bar {
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
  }
  .numpad {
    grid-template-columns: repeat(10, 1fr);
  }
  .num-btn {
    font-size: clamp(16px, 4.5vw, 22px);
    border-radius: 8px;
    min-height: 36px;
  }
  .game-title { font-size: 18px; }
  .timer { font-size: 20px; }
}

@media (min-width: 600px) {
  #app {
    padding: 0 20px;
  }
  .board {
    max-width: 500px;
  }
  .numpad {
    max-width: 500px;
    margin: 0 auto;
    width: 100%;
  }
  .action-bar {
    max-width: 500px;
    margin: 0 auto;
    width: 100%;
  }
}

@media (min-width: 768px) {
  .action-btn span {
    font-size: 12px;
  }
  .num-btn {
    font-size: 24px;
  }
}

/* 微信内置浏览器适配 */
@media (min-width: 0px) {
  /* 处理微信 iOS 底部安全区域 */
  .action-bar {
    padding-bottom: max(16px, var(--safe-bottom));
  }
}

/* 防止选中文本 */
.cell, .num-btn, .action-btn, .diff-btn {
  -webkit-user-select: none;
  user-select: none;
}

/* 隐藏 */
.hidden {
  display: none !important;
}
