:root {
  color-scheme: dark;
  font-family: "Microsoft YaHei", "PingFang SC", system-ui, sans-serif;
  letter-spacing: 0;
  --bg: #15181f;
  --surface: #1b1f27;
  --surface-strong: #252a34;
  --ink: #f7f4eb;
  --muted: #aeb5c0;
  --line: rgba(255, 255, 255, 0.13);
  --yellow: #f2c94c;
  --coral: #f06b68;
  --green: #68c780;
  --blue: #5b8def;
  --orange: #f29f4b;
  --cyan: #7fd6cf;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--bg);
}

body,
button {
  font: inherit;
  letter-spacing: 0;
}

button {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible,
.count-panel:focus-within {
  outline: 2px solid rgba(242, 201, 76, 0.72);
  outline-offset: 2px;
}

#stage,
.v3d-container {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

.v3d-container > canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.ui-hidden .lesson-overlay {
  visibility: hidden;
  opacity: 0;
}

.lesson-overlay {
  position: absolute;
  z-index: 10;
  inset: 0;
  min-width: 320px;
  display: grid;
  grid-template-rows: 64px minmax(0, 1fr);
  color: var(--ink);
  background: var(--bg);
  opacity: 1;
  transition: opacity 180ms ease;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 8px 62px 8px 16px;
  border-bottom: 1px solid var(--line);
  background: #101319;
}

.overview {
  min-width: 112px;
  display: grid;
  gap: 2px;
}

.overview strong {
  font-size: 20px;
  line-height: 1.1;
}

.overview span {
  color: var(--muted);
  font-size: 13px;
}

.toolbar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 7px;
}

.control-button,
.icon-button {
  height: 44px;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--ink);
  background: var(--surface-strong);
  font-weight: 800;
}

.control-button {
  min-width: 94px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding: 0 12px;
  white-space: nowrap;
}

.control-button.primary,
.control-button[aria-pressed="true"] {
  color: #1b1f27;
  border-color: var(--yellow);
  background: var(--yellow);
}

.control-button:hover,
.icon-button:hover {
  border-color: rgba(242, 201, 76, 0.58);
}

.control-button:disabled,
.icon-button:disabled {
  cursor: default;
  opacity: 0.42;
}

.icon-button {
  width: 44px;
  min-width: 44px;
  padding: 0;
  font-size: 22px;
  line-height: 1;
}

.icon-button[aria-pressed="false"] {
  color: var(--muted);
}

.boards-grid {
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: repeat(2, minmax(0, 1fr));
  gap: 10px;
  padding: 6px 14px 14px;
}

.count-panel {
  position: relative;
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: 38px minmax(0, 1fr) 48px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 6px;
  background: var(--surface);
  transition: border-color 160ms ease, background 160ms ease;
}

.count-panel.is-active {
  border-color: rgba(242, 201, 76, 0.72);
  background: #1d2129;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 0 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.07);
}

.panel-head h2 {
  margin: 0;
  font-size: 17px;
  line-height: 1;
  font-weight: 900;
}

.panel-progress {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
}

.count-panel.is-active .panel-progress {
  color: var(--yellow);
}

.mini-board {
  width: 100%;
  height: 100%;
  display: block;
  min-height: 0;
  cursor: pointer;
  touch-action: manipulation;
}

.shape-line {
  fill: none;
  stroke: var(--yellow);
  stroke-width: 1;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.shape-line.secondary {
  stroke: rgba(247, 244, 235, 0.38);
}

.shape-node {
  fill: var(--surface);
  stroke: var(--yellow);
  stroke-width: 1;
}

.count-label circle {
  fill: var(--surface-strong);
  stroke: currentColor;
  stroke-width: 1;
}

.count-label text {
  fill: var(--ink);
  font-size: 17px;
  font-weight: 900;
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
}

.count-label.is-current {
  filter: drop-shadow(0 0 7px currentColor);
}

.band-fill {
  stroke: rgba(255, 255, 255, 0.26);
  stroke-width: 1;
}

.touch-highlight-line {
  fill: none;
  stroke: var(--coral);
  stroke-width: 1;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.touch-highlight-fill {
  fill: rgba(240, 107, 104, 0.2);
  stroke: var(--coral);
  stroke-width: 1;
  stroke-linejoin: round;
}

.touch-endpoint {
  fill: var(--surface);
  stroke: var(--coral);
  stroke-width: 1;
}

.panel-formula {
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
  overflow: hidden;
  padding: 5px 8px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(11, 14, 19, 0.45);
}

.formula-term {
  width: 31px;
  height: 31px;
  min-width: 31px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 5px;
  color: rgba(247, 244, 235, 0.22);
  background: rgba(255, 255, 255, 0.025);
  font-size: 17px;
  line-height: 1;
  font-weight: 900;
}

.formula-term.is-arrived {
  color: var(--ink);
  border-color: currentColor;
  background: var(--surface-strong);
}

.formula-plus,
.formula-equals {
  flex: 0 0 auto;
  color: rgba(247, 244, 235, 0.28);
  font-size: 17px;
  font-weight: 900;
}

.formula-plus.is-arrived {
  color: var(--muted);
}

.formula-result {
  min-width: 36px;
  flex: 0 0 auto;
  color: rgba(247, 244, 235, 0.25);
  font-size: 24px;
  line-height: 1;
  font-weight: 900;
  text-align: center;
}

.formula-result.is-complete {
  color: var(--yellow);
  animation: result-pop 520ms cubic-bezier(0.2, 0.9, 0.25, 1.3);
}

.formula-unit {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
}

.touch-count-strip {
  min-width: 0;
  max-width: 82%;
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  gap: 3px;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  scroll-behavior: auto;
}

.touch-count-strip::-webkit-scrollbar {
  display: none;
}

.touch-count-zero {
  min-width: 30px;
  color: var(--muted);
  font-weight: 900;
  text-align: center;
}

.flying-number {
  position: absolute;
  z-index: 30;
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border: 1px solid currentColor;
  border-radius: 50%;
  color: var(--yellow);
  background: var(--surface-strong);
  font-size: 18px;
  font-weight: 900;
  line-height: 1;
  box-shadow: 0 0 14px currentColor;
  pointer-events: none;
}

.panel-check {
  position: absolute;
  z-index: 8;
  left: 50%;
  top: 48%;
  width: 54px;
  height: 54px;
  display: grid;
  place-items: center;
  border: 1px solid var(--yellow);
  border-radius: 50%;
  color: var(--yellow);
  background: rgba(21, 24, 31, 0.9);
  font-size: 28px;
  font-weight: 900;
  transform: translate(-50%, -50%);
  animation: check-pulse 1050ms ease forwards;
  pointer-events: none;
}

.count-panel.is-complete-flash {
  animation: panel-confirm 900ms ease;
}

.effects-layer {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
}

@keyframes result-pop {
  0% { transform: scale(0.6); }
  55% { transform: scale(1.2); }
  100% { transform: scale(1); }
}

@keyframes check-pulse {
  0% { transform: translate(-50%, -50%) scale(0.72); opacity: 0; }
  25% { transform: translate(-50%, -50%) scale(1.05); opacity: 1; }
  70% { transform: translate(-50%, -50%) scale(1); opacity: 1; }
  100% { transform: translate(-50%, -56%) scale(0.95); opacity: 0; }
}

@keyframes panel-confirm {
  0%, 100% { border-color: rgba(242, 201, 76, 0.72); }
  45% { border-color: var(--green); }
}

@media (max-width: 760px), (max-height: 620px) {
  .lesson-overlay {
    grid-template-rows: 56px minmax(0, 1fr);
  }

  .topbar {
    padding-top: 6px;
    padding-bottom: 6px;
  }

  .overview strong {
    font-size: 17px;
  }

  .control-button {
    min-width: 78px;
    height: 42px;
    min-height: 42px;
    padding: 0 8px;
    font-size: 14px;
  }

  .icon-button {
    width: 42px;
    min-width: 42px;
    height: 42px;
    min-height: 42px;
  }

  .boards-grid {
    gap: 7px;
    padding: 4px 8px 8px;
  }

  .count-panel {
    grid-template-rows: 32px minmax(0, 1fr) 40px;
  }

  .panel-head {
    padding: 0 8px;
  }

  .panel-head h2 {
    font-size: 15px;
  }

  .panel-progress {
    font-size: 11px;
  }

  .formula-term {
    width: 25px;
    height: 27px;
    min-width: 25px;
    font-size: 14px;
  }

  .formula-plus,
  .formula-equals {
    font-size: 14px;
  }

  .formula-result {
    min-width: 30px;
    font-size: 20px;
  }
}

@media (max-width: 640px) {
  .lesson-overlay {
    grid-template-rows: 56px minmax(0, 1fr);
  }

  .topbar {
    justify-content: center;
    padding: 6px 48px 6px 8px;
  }

  .overview {
    display: none;
  }

  .toolbar {
    width: 100%;
    display: grid;
    grid-template-columns: 62px 50px 64px 50px 44px 44px;
    justify-content: center;
    gap: 4px;
  }

  .control-button {
    min-width: 0;
    height: 44px;
    min-height: 44px;
    gap: 3px;
    padding: 0 4px;
    font-size: 13px;
  }

  .icon-button {
    width: 44px;
    min-width: 44px;
    height: 44px;
    min-height: 44px;
  }

  .boards-grid {
    gap: 5px;
    padding: 3px 5px 5px;
  }

  .count-panel {
    grid-template-rows: 30px minmax(0, 1fr) 34px;
    border-radius: 4px;
  }

  .panel-head {
    padding: 0 6px;
  }

  .panel-head h2 {
    font-size: 14px;
  }

  .panel-progress {
    font-size: 10px;
  }

  .panel-formula {
    gap: 0;
    padding: 3px 2px;
  }

  .formula-term {
    width: 14px;
    height: 22px;
    min-width: 14px;
    border-color: transparent;
    background: transparent;
    font-size: 11px;
  }

  .formula-term.is-arrived {
    border-color: currentColor;
    background: rgba(255, 255, 255, 0.04);
  }

  .formula-plus,
  .formula-equals {
    font-size: 10px;
  }

  .formula-result {
    min-width: 20px;
    font-size: 15px;
  }

  .formula-unit {
    font-size: 10px;
  }

  .touch-count-strip {
    max-width: 78%;
    gap: 1px;
  }

  .count-label text {
    font-size: 14px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 1ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 1ms !important;
    scroll-behavior: auto !important;
  }
}
