*,
*::before,
*::after {
  box-sizing: border-box;
}

:root {
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: #f8fafb;
  background-color: #111729;
  line-height: 1.5;
  font-weight: 400;
  --accent: #5865f2;
  --accent-strong: #4752c4;
  --surface: #1c2338;
  --surface-strong: #252f4f;
  --surface-soft: #2d3655;
  --border: rgba(255, 255, 255, 0.06);
  --shadow: 0 20px 60px rgba(0, 0, 0, 0.35);
}

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  padding: 40px 24px;
}

.app-shell {
  max-width: 1024px;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.app-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: 16px;
  position: relative;
}

.branding h1 {
  margin: 0;
  font-size: 2.4rem;
  font-weight: 700;
}

.branding p {
  margin: 4px 0 0;
  color: rgba(248, 250, 251, 0.72);
}

.scoreboard {
  display: flex;
  gap: 12px;
}

.score-card {
  background: var(--surface);
  border-radius: 16px;
  padding: 14px 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 110px;
  box-shadow: var(--shadow);
}

.score-card .label {
  font-size: 0.85rem;
  color: rgba(248, 250, 251, 0.6);
}

.score-card .value {
  font-size: 1.6rem;
  font-weight: 700;
  margin-top: 6px;
}

.app-main {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 24px;
}

.controls {
  display: grid;
  gap: 16px;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
}

.primary-controls,
.timer-controls,
.storage-controls {
  background: var(--surface);
  border-radius: 20px;
  padding: 16px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  box-shadow: var(--shadow);
}

.timer-controls {
  align-items: stretch;
}

.timer-display {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
}

.timer-entry {
  display: flex;
  justify-content: space-between;
}

.timer-entry .label {
  color: rgba(248, 250, 251, 0.65);
}

.timer-entry .value {
  font-weight: 600;
}

.btn {
  border: none;
  border-radius: 14px;
  background: var(--surface-soft);
  color: inherit;
  padding: 10px 18px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.18s ease, background 0.18s ease, box-shadow 0.18s ease;
  box-shadow: inset 0 0 0 1px var(--border);
}

.btn:hover {
  transform: translateY(-1px);
  background: var(--surface-strong);
}

.btn:active {
  transform: translateY(0);
  background: rgba(88, 101, 242, 0.2);
}

.btn.accent {
  background: var(--accent);
  box-shadow: 0 10px 30px rgba(88, 101, 242, 0.45);
}

.btn.accent:hover {
  background: var(--accent-strong);
}

.btn.ghost {
  background: transparent;
  box-shadow: inset 0 0 0 1px var(--border);
}

.btn.disabled {
  opacity: 0.4;
  pointer-events: none;
}

.custom-panel {
  display: none;
  flex-basis: 100%;
  background: rgba(88, 101, 242, 0.08);
  border-radius: 12px;
  padding: 12px;
  border: 1px solid rgba(88, 101, 242, 0.3);
}

.custom-panel.active {
  display: flex;
}

.custom-panel label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.9rem;
}

select {
  border: none;
  border-radius: 10px;
  padding: 8px 12px;
  background: rgba(0, 0, 0, 0.2);
  color: inherit;
}

.file-input {
  position: relative;
  overflow: hidden;
}

.file-input input[type="file"] {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.board-wrapper {
  display: flex;
  justify-content: center;
}

.board {
  width: min(480px, 90vw);
  aspect-ratio: 1;
  background: var(--surface);
  border-radius: 28px;
  padding: 18px;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  grid-gap: 14px;
  box-shadow: var(--shadow);
  position: relative;
  touch-action: none; /* 禁止默认手势滚动，配合 JS 防抖 */
}

.board.custom-mode {
  box-shadow: 0 0 0 2px rgba(88, 101, 242, 0.45), var(--shadow);
}

.board.custom-mode .cell {
  cursor: pointer;
}

.cell {
  background: rgba(255, 255, 255, 0.04);
  border-radius: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.8rem;
  font-weight: 700;
  color: #fff;
  position: relative;
  transition: transform 0.2s ease, background 0.2s ease;
}

.cell.new {
  animation: pop 0.2s ease;
}

@keyframes pop {
  0% {
    transform: scale(0.65);
  }
  100% {
    transform: scale(1);
  }
}

.cell[data-value="0"] {
  opacity: 0.15;
  color: transparent;
}

/* 数字颜色：每个数值独立配色，同时组间色系不同 */
.cell[data-value="2"] { background: #edf6ff; color: #0b2a63; }
.cell[data-value="4"] { background: #e4f1ff; color: #0b2a63; }
.cell[data-value="8"] { background: #daeaff; color: #0a295f; }
.cell[data-value="16"] { background: #cfe3ff; color: #09285c; }

.cell[data-value="32"] { background: #b4dcff; color: #082654; }
.cell[data-value="64"] { background: #9bd0ff; color: #07244e; }
.cell[data-value="128"] { background: #83c3ff; color: #fff; }
.cell[data-value="256"] { background: #6ab7ff; color: #fff; }

.cell[data-value="512"] { background: #2f7cf0; color: #fff; }
.cell[data-value="1024"] { background: #1b65de; color: #fff; }
.cell[data-value="2048"] { background: #0f4ec6; color: #fff; }

.cell[data-value="4096"] { background: #ffdca8; color: #3b2a00; }
.cell[data-value="8192"] { background: #ffc36d; color: #3b2300; }
.cell[data-value="16384"] { background: #ffa43a; color: #3b1d00; }
.cell[data-value="32768"] { background: #ff7a47; color: #3b1400; }

.cell[data-value="65536"] { background: #000000; color: #fff; }

/* 不同数量级缩放字号，保持可读性 */
.cell[data-value="128"] { font-size: 1.6rem; }
.cell[data-value="256"] { font-size: 1.6rem; }
.cell[data-value="512"] { font-size: 1.5rem; }
.cell[data-value="1024"] { font-size: 1.4rem; }
.cell[data-value="2048"] { font-size: 1.3rem; }
.cell[data-value="4096"] { font-size: 1.25rem; }
.cell[data-value="8192"] { font-size: 1.2rem; }
.cell[data-value="16384"] { font-size: 1.15rem; }
.cell[data-value="32768"] { font-size: 1.1rem; }
.cell[data-value="65536"] { font-size: 1.05rem; }

.timeline {
  background: var(--surface);
  border-radius: 20px;
  padding: 18px;
  box-shadow: var(--shadow);
}

.timeline h2 {
  margin: 0 0 12px;
  font-size: 1.2rem;
}

.timeline ul {
  list-style: none;
  padding: 0;
  margin: 0;
  display: grid;
  gap: 8px;
}

.timeline li {
  background: rgba(255, 255, 255, 0.04);
  border-radius: 14px;
  padding: 12px 16px;
  display: flex;
  justify-content: space-between;
  font-size: 0.95rem;
}

.app-footer {
  text-align: center;
  color: rgba(248, 250, 251, 0.6);
  font-size: 0.9rem;
  margin-top: 12px;
}

/* 移动端计时徽标（仅移动端且棋盘视图显示） */
.mobile-timer-badge {
  display: none;
  position: absolute;
  right: 8px;
  top: 8px;
  padding: 6px 10px;
  border-radius: 12px;
  background: var(--surface-strong);
  box-shadow: var(--shadow);
  font-weight: 700;
  z-index: 2;
}
body.mobile-mode.view-board .mobile-timer-badge.visible { display: inline-flex; }

/* 移动端底部工具栏与撤回条 */
.mobile-only {
  display: none;
}
body:not(.mobile-mode) .mobile-only { display: none !important; }

.mobile-undo-bar {
  margin-top: 10px;
  display: flex;
  justify-content: center;
  width: min(480px, 92vw);
  margin-left: auto;
  margin-right: auto;
  position: relative;
  z-index: 1;
}
.mobile-undo-bar .btn { width: 100%; }

/* 移动端自定义数值面板（仅在自定义模式 + 棋盘页显示） */
.mobile-custom-panel {
  display: none;
  width: min(480px, 92vw);
  background: rgba(88, 101, 242, 0.08);
  border: 1px solid rgba(88, 101, 242, 0.3);
  border-radius: 12px;
  padding: 10px 12px;
}
.mobile-custom-panel.active { display: flex; }
.mobile-custom-panel label { width: 100%; display: flex; flex-direction: column; gap: 6px; }
.mobile-custom-panel select { width: 100%; }

.mobile-toolbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  min-height: 64px;
  background: var(--surface);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  box-shadow: 0 -10px 30px rgba(0, 0, 0, 0.35);
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  z-index: 50;
}

.mobile-toolbar .toolbar-scroll {
  display: none; /* 默认隐藏，点击“操作”选项卡时显示 */
  gap: 10px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.mobile-toolbar .tabs {
  display: flex;
  justify-content: space-between;
  gap: 8px;
}
.mobile-toolbar .tab {
  flex: 1;
  border: none;
  border-radius: 12px;
  background: var(--surface-soft);
  color: inherit;
  padding: 10px 12px;
  font-weight: 700;
}
.mobile-toolbar .tab.active { background: var(--accent); }

dialog {
  border: none;
  background: var(--surface);
  color: inherit;
  border-radius: 20px;
  padding: 24px;
  max-width: 360px;
  box-shadow: var(--shadow);
}

dialog::backdrop {
  background: rgba(0, 0, 0, 0.55);
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

.switch {
  position: relative;
  display: inline-flex;
  width: 54px;
  height: 28px;
}

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

.slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background-color: rgba(255, 255, 255, 0.15);
  transition: 0.2s;
  border-radius: 999px;
}

.slider::before {
  position: absolute;
  content: "";
  height: 20px;
  width: 20px;
  left: 4px;
  bottom: 4px;
  background-color: #fff;
  transition: 0.2s;
  border-radius: 50%;
}

input:checked + .slider {
  background-color: var(--accent);
}

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

.switch-label {
  font-size: 0.9rem;
  color: rgba(248, 250, 251, 0.65);
}

/* 移动端模式（由 JS 在 body 上添加 .mobile-mode） */
body.mobile-mode, html.mobile-mode {
  height: 100%;
  overflow: hidden;
}
body.mobile-mode {
  padding: 12px 12px 96px; /* 为底部工具栏留出空间 */
}
/* 移动端仅按具体元素显示，避免所有 mobile-only 一起弹出 */
/* 统一在下方分别指定需要显示的元素 */
body.mobile-mode .app-main {
  flex: 1;
  display: grid;
  place-items: center;
}
body.mobile-mode .board { width: 92vw; }
body.mobile-mode .board-wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

/* 视图切换：棋盘 / 统计 / 操作 */
body.mobile-mode.view-board .controls,
body.mobile-mode.view-board .timer-controls,
body.mobile-mode.view-board .timeline { display: none; }

body.mobile-mode.view-stats .controls { display: none; }
body.mobile-mode.view-stats .timer-controls,
body.mobile-mode.view-stats .timeline {
  display: block;
  width: 100%;
  max-width: 720px;
}
body.mobile-mode.view-stats .board-wrapper { display: none; }

body.mobile-mode.view-actions .controls,
body.mobile-mode.view-actions .storage-controls { display: none; }
body.mobile-mode.view-actions .controls { display: none; }
body.mobile-mode.view-actions .timer-controls,
body.mobile-mode.view-actions .timeline { display: none; }
/* 保留棋盘可见，便于自定义与游戏 */
body.mobile-mode.view-actions .board-wrapper { display: grid; }

/* 操作视图时显示动作区 */
body.mobile-mode.view-actions .mobile-toolbar .toolbar-scroll { display: flex; }

/* 选择性开启移动端元素 */
body.mobile-mode .mobile-toolbar { display: flex; }
body.mobile-mode.view-board .mobile-undo-bar { display: flex; }
body.mobile-mode.view-board .mobile-custom-panel.active { display: flex; }

/* 底部动作按钮横向排布且不换行 */
.mobile-toolbar .toolbar-scroll .btn,
.mobile-toolbar .toolbar-scroll .file-input {
  white-space: nowrap;
  min-width: 88px;
  word-break: keep-all;
}
