:root {
  --wood-dark: #5b3a29;
  --wood-mid: #7a4f36;
  --frame-shadow: rgba(0, 0, 0, 0.25);
  --bead-color: #6b4226;
  --bead-active: #3d2314;
  --bar-color: #2a1c12;
  --bg: #f4f1ec;
  --panel-bg: #ffffff;
  --border: #ddd6cb;
  --text: #2d2620;
  --accent: #6b4226;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px 10px 32px;
}

h1 {
  margin: 0 0 10px;
  font-size: 1.4rem;
}

h2 {
  margin: 0 0 8px;
  font-size: 1.05rem;
}

/* ---- Controls ---- */
.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  justify-content: center;
  background: var(--panel-bg);
  border: 1px solid var(--border);
  padding: 8px 12px;
  border-radius: 8px;
  margin-bottom: 10px;
}

.control-group {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: center;
}

.controls label {
  font-size: 0.85rem;
}

input[type="number"] {
  width: 110px;
  padding: 5px 8px;
  border-radius: 6px;
  border: 1px solid #c9c0b3;
  background: #fff;
  color: var(--text);
  font-size: 0.9rem;
}

#rod-count-input { width: 55px; }

button {
  padding: 6px 12px;
  border-radius: 6px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
  font-size: 0.85rem;
}

button:hover { opacity: 0.9; }
button:active { opacity: 0.75; }

button.secondary {
  background: #fff;
  color: var(--accent);
}

/* ---- Value display ---- */
.value-display {
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 16px;
  margin-bottom: 8px;
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.value-display .label {
  font-size: 0.8rem;
  color: #6b6258;
}

.value-display .value {
  font-size: 1.6rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.message {
  min-height: 1.1em;
  margin-bottom: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  color: #2e7d32;
}

.message.error { color: #c62828; }

/* ---- Abacus frame ---- */
.abacus-wrapper {
  overflow-x: auto;
  max-width: 100%;
}

.abacus {
  position: relative;
  background: transparent;
  border: 3px solid var(--wood-dark);
  border-radius: 6px;
  padding: 0 35px;
  display: inline-flex;
  gap: 6px;
  overflow: hidden;
}

.rod {
  position: relative;
  width: var(--rod-width, 44px);
  height: 190px;
  display: flex;
  justify-content: center;
}

.rod::before {
  content: "";
  position: absolute;
  top: 4px;
  bottom: 4px;
  width: 4px;
  border-radius: 2px;
  background: linear-gradient(90deg, #3a2517, #6b4429 50%, #3a2517);
}

.reckoning-bar {
  position: absolute;
  left: -8px;
  right: -8px;
  top: 70px;
  height: 10px;
  background: linear-gradient(180deg, var(--bar-color), #100a06);
  box-shadow: 0 2px 3px rgba(0,0,0,0.4);
  z-index: 3;
}

/* Small clickable dots along the beam marking every rod's position, so a
   learner can start counting/reading from any column. Every 3rd rod (a
   traditional "unit point") gets a slightly larger, gold dot. */
.bar-dot {
  position: absolute;
  left: 50%;
  top: 71px;
  width: 8px;
  height: 8px;
  transform: translateX(-50%);
  border-radius: 50%;
  background: #d8cdbb;
  border: 1px solid #3a2517;
  z-index: 4;
  cursor: pointer;
}

.bar-dot:hover { background: #fff2c9; }

.bar-dot.unit-point {
  width: 11px;
  height: 11px;
  top: 69.5px;
  background: #ffd93d;
  border-color: #7a5a12;
}

.bar-dot.unit-point:hover { background: #ffe98a; }

.rod.rod-highlighted::before {
  background: linear-gradient(90deg, #d68c2f, #ffb84d 50%, #d68c2f);
  box-shadow: 0 0 6px rgba(255, 184, 77, 0.8);
}

.bead {
  position: absolute;
  left: 50%;
  width: 34px;
  height: 24px;
  transform: translate(-50%, 0);
  cursor: pointer;
  z-index: 2;
  transition: top 0.15s ease-out;
}

.bead .diamond {
  width: 100%;
  height: 100%;
  background: radial-gradient(circle at 35% 30%, #a97c53, var(--bead-color) 60%, #3a2313 100%);
  clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
  box-shadow: 0 2px 3px rgba(0,0,0,0.5);
  transition: filter 0.15s ease-out;
}

.bead:hover .diamond { filter: brightness(1.12); }

.bead.active .diamond {
  background: radial-gradient(circle at 35% 30%, #c99a68, var(--bead-active) 65%, #1c0f07 100%);
}

/* Heaven bead sits above the bar with real travel room, drops flush onto it when active.
   Note: .bead positions are relative to .rod, while .reckoning-bar/.bar-dot are
   relative to .abacus's padding box (6px higher) - that offset is accounted for
   below so active beads land exactly on the bar, not into it. */
.bead.heaven { top: 16px; }
.bead.heaven.active { top: 46px; }

/* Earth beads sit below the bar with a clear stack; active ones rise flush to meet it */
.bead.earth-0 { top: 160px; }
.bead.earth-1 { top: 140px; }
.bead.earth-2 { top: 120px; }
.bead.earth-3 { top: 100px; }

.bead.earth-0.active { top: 140px; }
.bead.earth-1.active { top: 120px; }
.bead.earth-2.active { top: 100px; }
.bead.earth-3.active { top: 80px; }

/* ---- Practice panel ---- */
.practice-panel {
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 16px;
  margin-top: 16px;
  width: 100%;
  max-width: 480px;
  text-align: center;
}

.practice-ops button { min-width: 90px; }

.config-panel {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.config-panel.hidden { display: none; }

#digits-input, #rows-input { width: 70px; }

.problem-box {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.problem-box.hidden { display: none; }

.problem-text {
  font-size: 1.3rem;
  font-weight: 700;
  margin-bottom: 8px;
  font-variant-numeric: tabular-nums;
  white-space: pre-line;
  line-height: 1.4;
}

#answer-input { width: 130px; }

.score-line {
  margin-top: 6px;
  font-size: 0.85rem;
  color: #6b6258;
}

.guided-toggle {
  margin-top: 10px;
}

.guided-panel {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px dashed var(--border);
  text-align: left;
}

.guided-panel.hidden { display: none; }

.guided-step-text {
  font-size: 0.9rem;
  margin-bottom: 8px;
  min-height: 2.4em;
}

/* ---- Tutorial modal ---- */
.tutorial-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 16px;
}

.tutorial-overlay.hidden { display: none; }

.tutorial-box {
  background: #fff;
  border-radius: 10px;
  padding: 20px;
  max-width: 420px;
  width: 100%;
  box-shadow: 0 10px 30px rgba(0,0,0,0.3);
  position: relative;
  text-align: center;
}

.tutorial-close {
  position: absolute;
  top: 8px;
  right: 10px;
  background: none;
  border: none;
  font-size: 1.1rem;
  cursor: pointer;
  color: #6b6258;
  padding: 0;
}

.tutorial-step-content {
  min-height: 130px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

.tutorial-step-content h3 { margin: 0; }
.tutorial-step-content p { margin: 0; font-size: 0.95rem; }

.tutorial-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 14px;
}

#tutorial-progress { font-size: 0.8rem; color: #6b6258; }

@media (max-width: 700px) {
  .rod { width: 30px; height: 200px; }
  .bead { width: 28px; height: 20px; }
  .bead.heaven { top: 12px; }
  .bead.heaven.active { top: 50px; }
  .bead.earth-0 { top: 146px; }
  .bead.earth-1 { top: 128px; }
  .bead.earth-2 { top: 110px; }
  .bead.earth-3 { top: 92px; }
  .bead.earth-0.active { top: 134px; }
  .bead.earth-1.active { top: 116px; }
  .bead.earth-2.active { top: 98px; }
  .bead.earth-3.active { top: 80px; }
}
