:root {
  --primary: #4f46e5;
  --primary-weak: #eef2ff;
  --danger: #e11d48;
  --danger-weak: #ffe4e6;
  --ok: #16a34a;
  --radius: 14px;
  --radius-sm: 10px;
  --maxw: 520px;
}

/* 浅色主题 */
.theme-light {
  --bg: #f4f5fb;
  --surface: #ffffff;
  --surface-2: #f1f3f9;
  --text: #1e2233;
  --muted: #6b7280;
  --border: #e2e5ee;
  --cell: #ffffff;
  --given: #c7cbe0;
  --given-text: #2b2f44;
  --selected: #c7d2fe;
  --peer: #eef1fb;
  --samenum: #dbe2ff;
  --conflict: #fecdd3;
  --wrong: #fecdd3;
  --wrong-text: #be123c;
}

/* 深色主题 */
.theme-dark {
  --bg: #0f1117;
  --surface: #181b24;
  --surface-2: #1f2330;
  --text: #e8eaf2;
  --muted: #9aa3b2;
  --border: #2a2f3d;
  --cell: #1a1e29;
  --given: #2c3142;
  --given-text: #aeb6c9;
  --selected: #3a3470;
  --peer: #1d2230;
  --samenum: #2a2f4d;
  --conflict: #5b2030;
  --wrong: #5b2030;
  --wrong-text: #ff8fa3;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Microsoft YaHei", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background 0.25s, color 0.25s;
}

#app {
  max-width: var(--maxw);
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom);
}

/* 顶栏 */
.appbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  position: sticky;
  top: 0;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  z-index: 10;
}
.appbar-title {
  font-size: 18px;
  font-weight: 700;
  margin: 0;
  letter-spacing: 4px;
}
.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform 0.1s, background 0.2s;
}
.icon-btn:active {
  transform: scale(0.94);
}

#main {
  flex: 1;
  padding: 8px 14px 18px;
}

.screen {
  animation: fade 0.25s ease;
}
.hidden {
  display: none !important;
}
@keyframes fade {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

/* 首页 */
.menu {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding-top: 18px;
}
.menu-hero {
  text-align: center;
  padding: 26px 0 18px;
}
.menu-logo {
  font-size: 46px;
  font-weight: 800;
  letter-spacing: 8px;
  background: linear-gradient(135deg, #6366f1, #a855f7);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.menu-sub {
  color: var(--muted);
  margin: 8px 0 0;
  font-size: 13px;
  letter-spacing: 1px;
}
.menu-row {
  display: flex;
  gap: 12px;
}
.menu-row .btn {
  flex: 1;
}
.menu-foot {
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  margin-top: 6px;
  min-height: 16px;
}

/* 按钮 */
.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 13px 16px;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s, background 0.2s, opacity 0.2s;
}
.btn:active {
  transform: scale(0.98);
}
.btn-block {
  width: 100%;
}
.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}
.btn-ghost {
  background: var(--surface-2);
}
.btn-danger-ghost {
  background: transparent;
  border-color: var(--danger);
  color: var(--danger);
}
.btn-sm {
  padding: 8px 12px;
  font-size: 13px;
}

/* 游戏顶栏 */
.game-top {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 12px;
}
.badge {
  background: var(--primary-weak);
  color: var(--primary);
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 700;
}
.game-stats {
  display: flex;
  gap: 14px;
  flex: 1;
  justify-content: center;
}
.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 18px;
  font-weight: 700;
  line-height: 1.1;
}
.stat-label {
  font-size: 11px;
  font-weight: 500;
  color: var(--muted);
  margin-bottom: 2px;
}

/* 棋盘 */
.board-wrap {
  position: relative;
  background: var(--surface);
  border: 2px solid var(--primary);
  border-radius: var(--radius);
  padding: 6px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
}
.board {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 0;
  aspect-ratio: 1 / 1;
  width: 100%;
}
.cell {
  position: relative;
  border: 1px solid var(--border);
  background: var(--cell);
  color: var(--text);
  font-size: clamp(18px, 6vw, 30px);
  font-weight: 600;
  display: grid;
  place-items: center;
  cursor: pointer;
  padding: 0;
  font-family: inherit;
  transition: background 0.12s;
  user-select: none;
}
.cell.bl {
  border-left: 2px solid var(--primary);
}
.cell.bt {
  border-top: 2px solid var(--primary);
}
.cell.br {
  border-right: 2px solid var(--primary);
}
.cell.bb {
  border-bottom: 2px solid var(--primary);
}
.cell.given {
  background: var(--given);
  color: var(--given-text);
  cursor: default;
}
.cell.peer {
  background: var(--peer);
}
.cell.samenum {
  background: var(--samenum);
}
.cell.selected {
  background: var(--selected);
  box-shadow: inset 0 0 0 2px var(--primary);
}
.cell.conflict,
.cell.wrong {
  background: var(--conflict);
  color: var(--wrong-text);
}
.cell .notes {
  position: absolute;
  inset: 3px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  font-size: clamp(11px, 3.2vw, 15px);
  font-weight: 700;
  color: var(--muted);
  pointer-events: none;
}
.cell .notes span {
  display: grid;
  place-items: center;
  line-height: 1;
  width: 100%;
  height: 100%;
  pointer-events: auto;
  cursor: pointer;
  border-radius: 3px;
  transition: background 0.12s;
}
.cell .notes span:hover {
  background: color-mix(in srgb, var(--muted) 20%, transparent);
}

/* 暂停：非阻塞横幅（位于棋盘上方，不遮挡棋盘，棋盘保留可见） */
.pause-overlay {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
  background: color-mix(in srgb, var(--primary) 12%, var(--surface));
  border: 1px solid var(--border);
  border-left: 3px solid var(--primary);
  border-radius: var(--radius);
  padding: 10px 14px;
  margin-bottom: 10px;
}
.pause-title {
  font-size: 15px;
  font-weight: 700;
  white-space: nowrap;
}
.pause-overlay .btn {
  padding: 6px 14px;
}
/* 暂停态：棋盘轻微变暗，提示已暂停（仍可点击，但落子被状态拦截） */
.board.paused {
  opacity: 0.5;
  filter: grayscale(0.3);
  transition: opacity 0.2s;
}

/* 数字盘 */
.pad {
  margin-top: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.pad-numbers {
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 6px;
}
.num {
  position: relative;
  aspect-ratio: 1 / 1;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--primary);
  border-radius: var(--radius-sm);
  font-size: clamp(18px, 5.5vw, 26px);
  font-weight: 700;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform 0.1s, background 0.2s;
}
.num:active {
  transform: scale(0.94);
}
.num.done {
  color: var(--muted);
  opacity: 0.45;
  pointer-events: none;
}
.num .remain {
  position: absolute;
  right: 3px;
  bottom: 1px;
  font-size: 9px;
  font-weight: 600;
  color: var(--muted);
}
.pad-tools {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}
.tool {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 10px 0;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  transition: background 0.2s, transform 0.1s;
}
.tool:active {
  transform: scale(0.96);
}
.tool.active {
  background: var(--primary-weak);
  border-color: var(--primary);
  color: var(--primary);
}
.tool-ico {
  font-size: 18px;
}

.game-actions {
  display: flex;
  gap: 10px;
  margin-top: 12px;
}
.game-actions .btn {
  flex: 1;
}

/* 通用头部 */
.screen-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 4px 0 14px;
}
.screen-head h2 {
  margin: 0;
  font-size: 20px;
}

/* 列表 */
.list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.row {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  cursor: default;
  transition: transform 0.1s, background 0.2s;
}
.row:active {
  transform: scale(0.99);
}
.row-main {
  flex: 1;
  min-width: 0;
}
.row-title {
  font-weight: 700;
  font-size: 15px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.row-sub {
  color: var(--muted);
  font-size: 12px;
  margin-top: 3px;
}
.row-right {
  text-align: right;
  font-weight: 700;
  font-size: 16px;
  white-space: nowrap;
}
.row-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-shrink: 0;
  margin-left: auto;
}
.tag {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 700;
}
.tag.win {
  background: color-mix(in srgb, var(--ok) 18%, transparent);
  color: var(--ok);
}
.tag.lose {
  background: var(--danger-weak);
  color: var(--danger);
}
.tag.d-easy {
  background: #dcfce7;
  color: #15803d;
}
.tag.d-medium {
  background: #dbeafe;
  color: #1d4ed8;
}
.tag.d-hard {
  background: #fef3c7;
  color: #b45309;
}
.tag.d-expert {
  background: #fae8ff;
  color: #a21caf;
}
.empty {
  text-align: center;
  color: var(--muted);
  padding: 40px 0;
  font-size: 14px;
}

/* 复盘 */
.replay-info {
  margin-bottom: 10px;
  font-size: 13px;
  color: var(--muted);
}
.replay-bar {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 14px;
}
.replay-step {
  text-align: center;
  color: var(--muted);
  font-size: 13px;
  margin-top: 8px;
}

/* 排行榜 */
.lb-section {
  margin-bottom: 18px;
}
.lb-section h3 {
  font-size: 15px;
  margin: 0 0 8px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.lb-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}
.lb-table th,
.lb-table td {
  padding: 9px 12px;
  text-align: left;
  font-size: 13px;
  border-bottom: 1px solid var(--border);
}
.lb-table th {
  color: var(--muted);
  font-weight: 600;
  background: var(--surface-2);
}
.lb-table tr:last-child td {
  border-bottom: none;
}
.lb-rank {
  font-weight: 800;
  color: var(--primary);
  width: 36px;
}
.lb-rank.top {
  color: #f59e0b;
}

/* 设置 */
.settings {
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.setting-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.setting-label {
  font-weight: 600;
  font-size: 14px;
}
.setting-hint {
  color: var(--muted);
  font-size: 12px;
  margin: 4px 0 0;
}
.seg {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.seg-btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 10px 14px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, border-color 0.2s;
}
.seg-btn.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

/* 弹窗 */
#modal-root {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.45);
  padding: 20px;
  z-index: 50;
}
#modal-root.show {
  display: flex;
}
.modal {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 22px;
  width: 100%;
  max-width: 340px;
  animation: pop 0.2s ease;
}
@keyframes pop {
  from {
    transform: scale(0.92);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}
.modal h3 {
  margin: 0 0 6px;
  font-size: 19px;
}
.modal p {
  color: var(--muted);
  margin: 0 0 16px;
  font-size: 14px;
}
.modal-actions {
  display: flex;
  gap: 10px;
}
.modal-actions .btn {
  flex: 1;
}
.modal-stats {
  display: flex;
  justify-content: space-around;
  margin: 6px 0 16px;
  text-align: center;
}
.modal-stats .ms-val {
  font-size: 22px;
  font-weight: 800;
}
.modal-stats .ms-label {
  font-size: 11px;
  color: var(--muted);
}
.seg.vertical {
  flex-direction: column;
}
.seg.vertical .seg-btn {
  border-radius: var(--radius-sm);
  text-align: left;
}

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 60;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
}
.toast.hidden {
  display: none;
}

@media (max-width: 380px) {
  .game-stats {
    gap: 8px;
  }
  .stat {
    font-size: 16px;
  }
}
