:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #1d2129;
  --muted: #6b7280;
  --border: #e3e5e8;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --hover: #eef1f5;
  --current: #fde68a;
  --current-text: #1d2129;
  --done: #9ca3af;
  --error-bg: #fef2f2;
  --error-text: #b91c1c;
  --gap: 16px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111317;
    --card: #1b1e24;
    --text: #e6e8eb;
    --muted: #9aa0a9;
    --border: #2c3038;
    --accent: #3b82f6;
    --hover: #252932;
    --current: #facc15;
    --done: #5f6670;
    --error-bg: #3a1618;
    --error-text: #fca5a5;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
  overflow: hidden;
}
h1 { margin: 0; font-size: 20px; white-space: nowrap; }
h2 { margin: 0; font-size: 15px; }

button {
  font: inherit;
  color: var(--text);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
  touch-action: manipulation;
}
button:hover:not(:disabled) { background: var(--hover); }
button:disabled { opacity: .4; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.primary:hover:not(:disabled) { filter: brightness(1.1); background: var(--accent); }
input[type=text] {
  width: 100%;
  font: 14px ui-monospace, Menlo, monospace;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
}

.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 9px; padding: 2px; background: var(--card); }
.segmented button { border: 0; border-radius: 7px; padding: 4px 12px; background: transparent; }
.segmented button.selected { background: var(--accent); color: var(--accent-text); }
.segmented button.selected:hover { background: var(--accent); }
.segmented.small button { padding: 2px 8px; font-size: 13px; }

/* ---------- 整体布局：页面不滚动，各区域自己滚动 ---------- */
.app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
.topbar {
  display: flex; align-items: center; flex-wrap: wrap;
  gap: 8px 14px;
  padding: 12px 20px;
}
.method-picker { display: flex; align-items: center; gap: 8px; }
.method { color: var(--muted); font-size: 13px; white-space: nowrap; }
.hint { margin: 0 0 0 auto; color: var(--muted); font-size: 12px; }

.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  gap: var(--gap);
  padding: 0 20px 20px;
  grid-template-columns: minmax(360px, 1.15fr) minmax(300px, 1fr) 340px;
  grid-template-rows: minmax(0, 1fr);
  grid-template-areas: "stage steps controls";
}
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px 16px;
}
.card > h2 { margin-bottom: 10px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 10px; }

/* ---------- 魔方 ---------- */
.stage { grid-area: stage; display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.scene {
  flex: 1;
  min-height: 160px;
  position: relative;
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.scene .empty { padding: 24px; text-align: center; }
.scene:active { cursor: grabbing; }
.scene canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

.caption { text-align: center; min-height: 44px; margin-top: 6px; }
.caption .move-now { font: 600 26px ui-monospace, Menlo, monospace; margin-left: 8px; }
.caption .sub { color: var(--muted); font-size: 13px; }
.actions { display: flex; gap: 8px; justify-content: center; margin: 8px 0; }
.actions button { min-width: 96px; }
.player { display: flex; gap: 8px; justify-content: center; align-items: center; flex-wrap: wrap; }
.player button { min-width: 44px; }
.speed {
  font: inherit;
  color: var(--text);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  margin-left: 6px;
  cursor: pointer;
}
.progress { height: 4px; background: var(--border); border-radius: 2px; margin-top: 12px; overflow: hidden; flex: none; }
#progress-bar { height: 100%; width: 0; background: var(--accent); transition: width .15s; }

/* ---------- 求解步骤 ---------- */
.steps { grid-area: steps; display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.summary { color: var(--muted); font-size: 13px; text-align: right; }
.stages-scroll { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; margin-right: -8px; padding-right: 8px; }
.stages { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 12px; }
.stage-name { font-weight: 600; margin-bottom: 4px; }
.stage-name span { color: var(--muted); font-weight: 400; margin-left: 4px; }
.stage-item.active .stage-name { color: var(--accent); }
.tokens { display: flex; flex-wrap: wrap; gap: 2px 3px; }
.token {
  font: 13px ui-monospace, Menlo, monospace;
  padding: 1px 5px;
  border-radius: 4px;
  cursor: pointer;
}
.token:hover { background: var(--hover); }
.token.done { color: var(--done); }
.token.current { background: var(--current); color: var(--current-text); }
.empty { color: var(--muted); font-size: 13px; margin: 0; }
.error {
  background: var(--error-bg); color: var(--error-text);
  border-radius: 8px; padding: 8px 10px; margin-bottom: 10px;
}

/* ---------- 操作面板 ---------- */
.controls { grid-area: controls; display: flex; flex-direction: column; gap: var(--gap); min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
.controls-group { display: flex; flex-direction: column; gap: var(--gap); }
.row { display: flex; gap: 8px; }
.row + .row { margin-top: 8px; }
.buttons button { flex: 1; }

.move-grid { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 6px; }
.move-grid button { padding: 6px 0; font-family: ui-monospace, Menlo, monospace; white-space: nowrap; }


.net {
  --cell: 20px;
  display: grid;
  grid-template-columns: repeat(4, max-content);
  grid-template-rows: repeat(3, max-content);
  gap: 3px;
  justify-content: center;
}
.net-face { position: relative; display: grid; grid-template-columns: repeat(var(--n), var(--cell)); gap: 2px; }
.net .cell { width: var(--cell); height: var(--cell); border-radius: 3px; border: 1px solid rgba(0, 0, 0, .25); }
.face-label {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  font-size: 10px; font-weight: 700; color: rgba(0, 0, 0, .55);
  pointer-events: none;
}

.tabbar { display: none; }

/* ---------- 中等宽度：魔方在上、步骤在下，操作面板在右 ---------- */
@media (max-width: 1180px) {
  .layout {
    grid-template-columns: minmax(0, 1fr) 320px;
    grid-template-rows: minmax(0, 1.3fr) minmax(0, 1fr);
    grid-template-areas: "stage controls" "steps controls";
  }
  .hint { display: none; }
}

/* ---------- 手机：魔方固定在上方，下面用标签切换 ---------- */
@media (max-width: 760px) {
  :root { --gap: 10px; }
  h1 { font-size: 17px; }
  .topbar { padding: 10px 16px 8px; }
  .topbar { row-gap: 6px; }
  .segmented button { padding: 4px 10px; }
  .layout { display: flex; flex-direction: column; padding: 0 12px; gap: var(--gap); }
  .card { padding: 12px; border-radius: 12px; }

  .stage { flex: none; height: 48dvh; min-height: 300px; padding: 8px 10px 10px; }
  .scene { min-height: 120px; }
  .caption { min-height: 38px; margin-top: 0; }
  .caption .move-now { font-size: 22px; }
  .caption .sub { font-size: 12px; }
  .actions { margin: 4px 0 6px; }
  .player { gap: 6px; flex-wrap: nowrap; }
  .player button { min-width: 0; flex: 1; padding: 6px 0; }
  .speed { flex: none; margin-left: 0; padding: 6px 4px; }
  .progress { margin-top: 8px; }

  .steps, .controls { flex: 1; min-height: 0; }
  body:not([data-tab="steps"]) .steps { display: none; }
  body[data-tab="steps"] .controls { display: none; }
  body[data-tab="controls"] .net-card { display: none; }
  body[data-tab="net"] .controls-group { display: none; }

  .tabbar {
    display: flex;
    gap: 6px;
    padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
    background: var(--card);
    border-top: 1px solid var(--border);
    margin-top: var(--gap);
  }
  .tabbar button { flex: 1; border: 0; background: transparent; color: var(--muted); padding: 8px 0; }
  .tabbar button.selected { color: var(--accent); background: var(--hover); font-weight: 600; }
  .move-grid button { padding: 9px 0; }
}
