:root {
  --page: #F3F0E8;
  --panel: #FBFAF5;
  --ink: #2A2621;
  --ink-2: #4A443D;
  --muted: #8C847A;
  --line: #E2DCD0;
  --dark: #39342E;
  --btn: #FFFFFF;
  --btn-hover: #F4F1EA;
  --chip: #EFEBE2;
  --ok: #2B8A3E;
  --err: #C92A2A;
  --guide: #6741D9;
  --guide-ink: #4C2DB8;
  --radius: 20px;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 15px/1.6 -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.app {
  max-width: 1280px;
  margin: 0 auto;
  padding: 22px 20px 12px;
}

/* ---------- 顶栏 ---------- */
.topbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}
.brand h1 {
  margin: 0;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: 0.02em;
}
.brand h1 .dot { color: var(--muted); font-weight: 400; }
.brand p { margin: 4px 0 0; color: var(--muted); font-size: 14px; }

.top-right { display: flex; align-items: center; justify-content: flex-end; flex-wrap: wrap; gap: 10px 14px; }
.order { display: inline-flex; align-items: center; gap: 8px; }
.order-label { font-size: 13px; color: var(--muted); }
.seg {
  display: inline-flex;
  gap: 2px;
  padding: 3px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
}
.seg button {
  min-width: 34px;
  height: 28px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--ink-2);
  font: 600 14px var(--mono);
  cursor: pointer;
  transition: background-color 0.15s, color 0.15s;
}
.seg button:hover { background: var(--btn-hover); }
.seg button[aria-pressed="true"] { background: var(--dark); color: #fff; }
.seg.lang button { min-width: 38px; font: 600 13px -apple-system, BlinkMacSystemFont, "PingFang SC", "Segoe UI", sans-serif; }

.meters { display: flex; gap: 8px; flex-shrink: 0; }
.meter {
  display: inline-flex;
  align-items: baseline;
  gap: 5px;
  padding: 5px 11px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--panel);
  font-variant-numeric: tabular-nums;
}
.meter b { font-size: 15px; min-width: 2.2em; text-align: right; }
.meter small { color: var(--muted); font-size: 12px; }

/* ---------- 画布 ---------- */
.stage {
  display: grid;
  grid-template-columns: minmax(0, 0.7fr) minmax(0, 1fr);
  gap: 16px;
  height: clamp(440px, 70vh, 740px);
}
.no3d .stage { grid-template-columns: minmax(0, 1fr); }
.no3d .cube-panel { display: none; }

.panel {
  position: relative;
  min-height: 0;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: 0 1px 0 rgba(255, 255, 255, 0.7) inset, 0 10px 30px -18px rgba(60, 52, 45, 0.35);
}
.panel canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.cube-panel canvas { cursor: grab; }
.cube-panel canvas.grabbing { cursor: grabbing; }

.caption {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 10px;
  padding: 0 14px;
  text-align: center;
  font-size: 12.5px;
  color: var(--muted);
  pointer-events: none;
}
.caption b { color: var(--ink); }

kbd {
  display: inline-block;
  min-width: 1.6em;
  padding: 0 5px;
  font: 600 12px/1.6 var(--mono);
  text-align: center;
  color: var(--ink);
  background: var(--btn);
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 6px;
}

/* ---------- 控制区 ---------- */
.controls {
  margin-top: 16px;
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 20px;
}
.btn-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.btn-row .sep { width: 1px; height: 22px; background: var(--line); margin: 0 2px; }

.btn {
  height: 34px;
  padding: 0 15px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--btn);
  color: var(--ink);
  font: inherit;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.15s, border-color 0.15s, transform 0.05s;
}
.btn:hover { background: var(--btn-hover); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: default; transform: none; background: var(--btn); }
.btn.primary { background: var(--dark); border-color: var(--dark); color: #fff; }
.btn.primary:hover { background: #24201C; }
.btn.ghost { background: transparent; }
.btn.accent { background: #EFEAFD; border-color: #D5C8FA; color: #4C2DB8; font-weight: 600; }
.btn.accent:hover { background: #E5DCFC; }
.btn.accent[aria-pressed="true"] { background: var(--guide); border-color: var(--guide); color: #fff; }

.speed { display: inline-flex; align-items: center; gap: 8px; font-size: 14px; }
.speed input { width: 130px; accent-color: var(--dark); }
.speed output { min-width: 56px; color: var(--muted); font-variant-numeric: tabular-nums; font-size: 13px; }

.toggles { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 14px; }
.toggles label, .inline-toggle {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  user-select: none;
}
input[type="checkbox"] { accent-color: var(--dark); width: 15px; height: 15px; margin: 0; }

.status { margin-left: auto; font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.status .muted { font-weight: 400; }
#stateText { display: inline-block; }
#stateText.flash { animation: flash 1.4s ease-out; }
@keyframes flash {
  0% { color: var(--ok); transform: scale(1); }
  18% { color: var(--ok); transform: scale(1.18); }
  60% { color: var(--ok); transform: scale(1); }
  100% { color: var(--ink); }
}

/* 转动按钮 */
.movepad { display: flex; flex-wrap: wrap; gap: 12px 22px; }
.pad-group { display: flex; flex-direction: column; gap: 6px; }
.pad-title { font-size: 12px; color: var(--muted); letter-spacing: 0.12em; }
.pad-grid {
  display: grid;
  grid-template-columns: repeat(var(--cols, 6), 44px);
  gap: 6px;
}
.pad-btn {
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--btn);
  color: var(--ink);
  font: 600 14px var(--mono);
  cursor: pointer;
  transition: background-color 0.12s, color 0.12s, border-color 0.12s;
}
.pad-btn:hover { background: var(--dark); border-color: var(--dark); color: #fff; }
.pad-btn:active { transform: translateY(1px); }

/* 公式实验室 */
.lab { display: flex; flex-direction: column; gap: 8px; }
.lab-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
#algo {
  flex: 1 1 300px;
  min-width: 0;
  height: 36px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--btn);
  color: var(--ink);
  font: 14px var(--mono);
}
#preset {
  height: 36px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--btn);
  color: var(--ink);
  font: inherit;
  font-size: 14px;
}
.inline-toggle { font-size: 14px; margin-left: 4px; }
.lab-info { font-size: 13.5px; color: var(--ink-2); min-height: 22px; }
.lab-info b { color: var(--ink); font-variant-numeric: tabular-nums; }
.err { color: var(--err); }
.muted { color: var(--muted); }

.history {
  display: flex;
  gap: 4px;
  overflow-x: auto;
  white-space: nowrap;
  padding: 2px 0 4px;
  font: 12.5px var(--mono);
  scrollbar-width: thin;
}
.history .muted { font-family: inherit; font-size: 13px; }
.chip { flex: none; padding: 1px 7px; border-radius: 6px; background: var(--chip); color: var(--ink-2); }

:focus-visible { outline: 2px solid #8C7A5B; outline-offset: 2px; }

/* ---------- 说明 ---------- */
.explain { margin-top: 28px; padding: 0 4px; }
.explain h2 { font-size: 18px; margin: 0 0 12px; }
.explain h3 { font-size: 15px; margin: 0 0 4px; }
.explain p { margin: 0; color: var(--ink-2); font-size: 14px; }
.explain strong { color: var(--ink); }
.cols { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 22px; }
.keys { margin-top: 18px; }
.keys p { line-height: 2; }

.foot {
  margin: 30px 4px 10px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 6px 16px;
  font-size: 13px;
  color: var(--muted);
}
.foot a { color: var(--ink-2); }

/* ---------- 提示卡（浮在图的左上角） ---------- */
.hint-card {
  position: absolute;
  top: 12px;
  left: 12px;
  width: min(300px, calc(100% - 24px));
  padding: 12px 14px;
  background: rgba(255, 253, 248, 0.97);
  border: 1px solid #D9CFF7;
  border-radius: 16px;
  box-shadow: 0 14px 40px -18px rgba(42, 38, 33, 0.45);
  font-size: 13.5px;
  z-index: 5;
}
.hint-card[hidden] { display: none; }
/* 宽屏：放在 3D 面板下部，3D 魔方相应上移缩小 */
.cube-panel .hint-card { top: auto; left: 12px; right: 12px; bottom: 34px; width: auto; min-height: 172px; }
.hc-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.hc-title { font-weight: 700; font-size: 14px; color: var(--guide-ink); }
.hc-close {
  flex: none;
  width: 26px;
  height: 26px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: #fff;
  color: var(--muted);
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
}
.hc-close:hover { color: var(--ink); background: var(--btn-hover); }
.hc-move-row { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
.hc-move-row[hidden] { display: none; }
.hc-move {
  flex: none;
  min-width: 54px;
  padding: 2px 12px;
  border-radius: 10px;
  background: var(--guide);
  color: #fff;
  font: 700 24px/1.3 var(--mono);
  text-align: center;
  box-shadow: 0 0 0 4px rgba(103, 65, 217, 0.16);
  animation: now-pulse 1.15s ease-in-out infinite;
}
.hc-what { font-size: 14.5px; font-weight: 600; color: var(--ink); line-height: 1.4; }
.hc-key { margin-top: 6px; color: var(--muted); font-size: 12.5px; line-height: 1.5; }
.hc-key[hidden] { display: none; }
.hc-ctx { margin-top: 8px; padding-top: 8px; border-top: 1px dashed var(--line); color: var(--ink-2); font-size: 12.5px; line-height: 1.55; }
.hc-prog { margin-top: 2px; color: var(--muted); font-size: 12px; }
.hc-prog:empty { display: none; }
.hc-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 10px; }
.hc-actions .btn[hidden] { display: none; }
.btn.small { height: 30px; padding: 0 12px; border-radius: 9px; font-size: 13px; }
.hc-feedback { font-size: 12.5px; color: var(--muted); }
.hc-feedback.ok { color: var(--ok); }
.hc-feedback.warn { color: #B35C00; }

/* ---------- 新手教学面板（固定在底部） ---------- */
.coach {
  position: fixed;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  width: min(1120px, calc(100vw - 24px));
  max-height: min(48vh, 360px);
  overflow: auto;
  padding: 12px 16px 14px;
  background: #FFFDF8;
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: 0 18px 50px -18px rgba(42, 38, 33, 0.45), 0 1px 0 rgba(255, 255, 255, 0.8) inset;
  z-index: 40;
}
.coach[hidden] { display: none; }
.coach-head { display: flex; align-items: center; gap: 10px; }
.coach-chapters { display: flex; gap: 6px; flex: 1; overflow-x: auto; scrollbar-width: none; }
.coach-chapters::-webkit-scrollbar { display: none; }
.pill {
  flex: none;
  height: 26px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--muted);
  font: inherit;
  font-size: 12.5px;
  cursor: pointer;
}
.pill.now { background: var(--guide); border-color: var(--guide); color: #fff; }
.pill.done { color: var(--ok); border-color: #B7E1C1; background: #F1FAF3; }
.pill.done::before { content: '✓ '; }
.coach-close {
  flex: none;
  width: 30px;
  height: 30px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: #fff;
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}
.coach-close:hover { color: var(--ink); background: var(--btn-hover); }
.coach-body {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px 24px;
  margin-top: 10px;
  align-items: end;
}
.coach-main h3 { margin: 0 0 4px; font-size: 16px; }
.coach-main p { margin: 0; color: var(--ink-2); font-size: 14px; line-height: 1.7; }
.coach-moves { display: flex; flex-wrap: wrap; gap: 8px 16px; margin-top: 8px; }
.coach-moves:empty { display: none; }
.part { display: flex; flex-direction: column; gap: 3px; }
.part-label { font-size: 11.5px; color: var(--muted); }
.part-chips { display: flex; flex-wrap: wrap; gap: 4px; }
.coach .chip { font: 600 13px var(--mono); padding: 2px 8px; border-radius: 7px; background: var(--chip); color: var(--ink-2); }
.coach .chip.done { opacity: 0.4; text-decoration: line-through; }
.coach .chip.now {
  background: var(--guide);
  color: #fff;
  box-shadow: 0 0 0 3px rgba(103, 65, 217, 0.2);
  animation: now-pulse 1.15s ease-in-out infinite;
}
@keyframes now-pulse { 50% { box-shadow: 0 0 0 6px rgba(103, 65, 217, 0.08); } }
.coach-hint { margin-top: 8px; font-size: 13.5px; color: var(--ink); }
.coach-hint:empty { display: none; }
.coach-hint b { font-family: var(--mono); color: var(--guide-ink); }
.coach-actions { display: flex; flex-direction: column; align-items: stretch; gap: 8px; min-width: 156px; }
.coach-actions .btn[hidden] { display: none; }
.coach-feedback { min-height: 18px; font-size: 13px; text-align: center; color: var(--muted); }
.coach-feedback.ok { color: var(--ok); }
.coach-feedback.warn { color: #B35C00; }

.teaching { padding-bottom: calc(var(--coach-h, 0px) + 28px); }
.teaching .stage { height: clamp(360px, calc(100vh - var(--coach-h, 0px) - 150px), 720px); }

@media (prefers-reduced-motion: reduce) {
  .coach .chip.now, .hc-move { animation: none; }
}

/* ---------- 窄屏 ---------- */
@media (max-width: 860px) {
  .app { padding: 16px 16px 10px; }
  .topbar { flex-direction: column; align-items: flex-start; gap: 10px; }
  .brand h1 { font-size: 22px; }
  .stage { grid-template-columns: minmax(0, 1fr); height: auto; }
  .graph-panel { order: -1; height: min(100vw, 640px); }
  .cube-panel { height: min(72vw, 420px); }
  .status { margin-left: 0; }
  .cols { grid-template-columns: minmax(0, 1fr); gap: 14px; }
  .movepad { gap: 12px 14px; }
  .pad-grid { grid-template-columns: repeat(var(--cols, 6), minmax(36px, 44px)); }
  .top-right { justify-content: flex-start; }
  .coach { bottom: 8px; padding: 10px 12px 12px; max-height: 46vh; border-radius: 16px; }
  .coach-body { grid-template-columns: minmax(0, 1fr); gap: 8px; margin-top: 8px; }
  .coach-main h3 { font-size: 15px; }
  .coach-main p { font-size: 13.5px; line-height: 1.6; }
  .coach-actions { flex-direction: row; flex-wrap: wrap; }
  .coach-actions .btn { flex: 1 1 40%; }
  .coach-feedback { flex-basis: 100%; }
  .teaching .stage { height: auto; }
  /* 窄屏上提示卡贴在屏幕底部，不挡住图 */
  .hint-card, .cube-panel .hint-card { position: fixed; top: auto; left: 8px; right: 8px; bottom: 8px; width: auto; min-height: 0; z-index: 45; }
  /* 教学时让图正好填满面板上方的空间，边看边跟着转 */
  .teaching .graph-panel { height: clamp(280px, calc(100vh - var(--coach-h, 0px) - 24px), 640px); }
}
@media (max-width: 420px) {
  .caption { font-size: 11.5px; bottom: 6px; }
  .speed input { width: 100px; }
}
