:root {
  color-scheme: light;
  --ink: #202124;
  --muted: #6a6f78;
  --paper: #f7f5ef;
  --panel: #fffdfa;
  --line: #ded8cc;
  --rail: #8f704b;
  --rail-dark: #654b31;
  --bead-red: #d95043;
  --bead-red-dark: #a9352d;
  --bead-blue: #2f80a8;
  --bead-blue-dark: #1d5c7d;
  --bead-green: #5b9367;
  --bead-green-dark: #3c6d48;
  --gold: #e8b84f;
  --shadow: 0 20px 55px rgba(58, 45, 28, 0.16);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  overflow-x: hidden;
  background:
    linear-gradient(135deg, rgba(232, 184, 79, 0.22), transparent 34%),
    linear-gradient(215deg, rgba(47, 128, 168, 0.16), transparent 42%),
    var(--paper);
  color: var(--ink);
}

button {
  border: 0;
  font: inherit;
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 18px;
  min-height: 100vh;
  padding: 18px;
}

.app-shell.quiz-mode-active {
  grid-template-columns: 1fr;
}

.app-shell.quiz-mode-active .mentor-panel {
  display: none;
}

.workspace,
.mentor-panel {
  background: rgba(255, 253, 250, 0.86);
  border: 1px solid rgba(222, 216, 204, 0.9);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.workspace {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  min-width: 0;
  overflow: hidden;
}

.topbar,
.mentor-header {
  padding: 22px 24px 14px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.eyebrow {
  margin: 0 0 5px;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: clamp(2rem, 4.6vw, 4.2rem);
  line-height: 0.96;
}

h2 {
  font-size: 1.55rem;
}

.toolbar {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.column-control {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 42px;
  border-radius: 8px;
  padding: 0 10px 0 12px;
  background: #f0ece3;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 900;
}

.column-control select {
  min-width: 48px;
  border: 0;
  border-radius: 6px;
  padding: 5px 7px;
  background: #fffdfa;
  color: var(--ink);
  font: inherit;
}

.tool-button,
.secondary-button,
.primary-button {
  min-height: 42px;
  border-radius: 8px;
  padding: 0 14px;
  background: #f0ece3;
  color: var(--ink);
  font-weight: 800;
  transition: transform 160ms ease, background 160ms ease, box-shadow 160ms ease;
}

.tool-button {
  min-width: 46px;
}

.tool-button:hover,
.secondary-button:hover,
.primary-button:hover {
  transform: translateY(-1px);
}

.tool-button.active,
.primary-button {
  background: var(--ink);
  color: #fffdfa;
}

.value-strip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin: 0 24px 16px;
  padding: 11px 14px;
  background: #f0ece3;
  border-radius: 8px;
  color: var(--muted);
  font-weight: 800;
}

.value-strip strong {
  color: var(--ink);
  font-size: 1.8rem;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.value-strip.quiz-active strong {
  font-size: 2.8rem;
  color: var(--bead-red);
  letter-spacing: 0;
}

.value-strip.quiz-active span {
  font-size: 1.4rem;
}

.quiz-tracker {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 0 24px 16px;
  padding: 12px 18px;
  background: #f0ece3;
  border-radius: 8px;
  font-size: 1.1rem;
}

.quiz-tracker-left, .quiz-tracker-right {
  display: flex;
  gap: 20px;
  align-items: center;
}

#submitQuizTop {
  background: var(--bead-red);
  color: #fff;
  font-size: 1.1rem;
  padding: 0 24px;
}

#submitQuizTop:hover {
  background: var(--bead-red-dark);
}

.quiz-feedback {
  margin-top: 16px;
  padding: 12px;
  border-radius: 8px;
  background: #f0ece3;
  font-weight: 800;
}

.quiz-feedback.correct {
  background: rgba(91, 147, 103, 0.18);
  color: #2f6139;
}

.quiz-feedback.try-again {
  background: rgba(217, 80, 67, 0.14);
  color: #873329;
}

.quiz-score {
  display: inline-flex;
  align-items: center;
  min-height: 34px;
  margin: 0 0 14px;
  padding: 0 10px;
  border-radius: 8px;
  background: #f0ece3;
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 900;
}

.quiz-timer,
.quiz-time {
  margin: 0 0 14px;
  color: var(--ink);
  font-size: 1.15rem;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}

.quiz-time {
  font-size: 1.45rem;
}

.quiz-tip {
  margin: 0 0 14px;
  padding: 12px;
  border-radius: 8px;
  background: rgba(232, 184, 79, 0.22);
  color: #634627;
  font-weight: 900;
}

.quiz-hint-small {
  color: var(--muted);
  font-size: 0.82rem;
}

.abacus-stage {
  display: grid;
  place-items: center;
  min-height: 0;
  padding: 12px 24px 26px;
}

.abacus-frame {
  position: relative;
  width: min(860px, 100%);
  aspect-ratio: 1.55;
  min-height: 420px;
  border: 18px solid var(--rail-dark);
  border-radius: 8px;
  background:
    linear-gradient(90deg, transparent 0 14%, rgba(255, 255, 255, 0.2) 14% 15%, transparent 15% 100%),
    #f8f0df;
  box-shadow: inset 0 0 0 6px rgba(255, 255, 255, 0.38), 0 22px 48px rgba(48, 33, 18, 0.22);
  touch-action: none;
  user-select: none;
}

.divider {
  position: absolute;
  left: 0;
  right: 0;
  top: 31%;
  height: 14px;
  background: linear-gradient(180deg, #9d784d, #6c4e31);
  box-shadow: 0 3px 10px rgba(55, 37, 19, 0.22);
}

.rod {
  position: absolute;
  top: 4%;
  bottom: 5%;
  width: 8px;
  transform: translateX(-50%);
  border-radius: 999px;
  background: linear-gradient(90deg, #6b4b2c, #b9905d, #634627);
}

.place-label {
  position: absolute;
  bottom: 2%;
  width: 88px;
  transform: translateX(-50%);
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 900;
  overflow: hidden;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.bead {
  position: absolute;
  width: clamp(50px, 8vw, 82px);
  height: clamp(32px, 5vw, 52px);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  background: var(--bead-red);
  border: 2px solid rgba(255, 255, 255, 0.45);
  box-shadow:
    inset 0 5px 8px rgba(255, 255, 255, 0.32),
    inset 0 -7px 10px rgba(0, 0, 0, 0.16),
    0 5px 12px rgba(43, 30, 17, 0.22);
  transition: top 190ms ease, filter 140ms ease, outline-color 140ms ease;
  cursor: grab;
}

.abacus-frame[data-columns="5"] .bead,
.abacus-frame[data-columns="6"] .bead {
  width: clamp(44px, 6.4vw, 68px);
  height: clamp(29px, 4.2vw, 44px);
}

.abacus-frame[data-columns="1"] .bead {
  width: clamp(62px, 10vw, 92px);
  height: clamp(42px, 6vw, 60px);
}

.bead::after {
  content: "";
  position: absolute;
  inset: 45% 8% auto;
  height: 4px;
  border-radius: 999px;
  background: rgba(87, 39, 27, 0.23);
}

.bead[data-place="1"] {
  background: var(--bead-blue);
}

.bead[data-place="2"] {
  background: var(--bead-green);
}

.bead[data-place="3"] {
  background: var(--gold);
}

.bead[data-place="4"] {
  background: var(--bead-red);
}

.bead[data-place="5"] {
  background: var(--bead-blue);
}

.bead.upper {
  filter: saturate(0.96) brightness(1.04);
}

.bead.dragging,
.bead.group-moving {
  z-index: 10;
  filter: brightness(1.08) saturate(1.05);
}

.bead.dragging {
  cursor: grabbing;
}

.bead.live-drag {
  transition: filter 140ms ease, outline-color 140ms ease;
}

.bead.selected {
  outline: 4px solid var(--gold);
  outline-offset: 3px;
}

.mentor-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  overflow: hidden;
}

.lesson-body {
  padding: 0 24px 18px;
  overflow: auto;
  color: #33363a;
  line-height: 1.5;
}

.lesson-body p {
  margin: 0 0 13px;
}

.lesson-body ul {
  margin: 0;
  padding-left: 20px;
}

.lesson-body li {
  margin: 0 0 9px;
}

.lesson-progress {
  display: flex;
  gap: 6px;
  padding: 0 24px 16px;
}

.dot {
  flex: 1 1 18px;
  min-width: 12px;
  max-width: 28px;
  height: 5px;
  border-radius: 999px;
  background: #d9d2c5;
}

.dot.active {
  background: var(--ink);
}

.lesson-actions {
  display: flex;
  gap: 10px;
  padding: 0 24px 24px;
}

.lesson-actions button {
  flex: 1;
}

.lesson-actions .quiz-submit {
  flex: 1.3;
}

.mentor-hidden {
  display: none;
}

@media (max-width: 1120px) {
  .app-shell {
    grid-template-columns: 1fr;
    min-height: auto;
  }

  .workspace {
    grid-template-rows: auto auto auto;
  }

  .abacus-stage {
    min-height: auto;
    padding-bottom: 24px;
  }

  .abacus-frame {
    width: min(760px, 100%);
    min-height: 0;
  }

  .bead {
    width: clamp(52px, 8vw, 72px);
    height: clamp(34px, 5vw, 48px);
  }

  .mentor-panel {
    min-height: 0;
    grid-template-rows: auto auto auto auto;
  }

  .lesson-body {
    overflow: visible;
  }
}

@media (max-width: 620px) {
  .app-shell {
    width: 100%;
    gap: 10px;
    padding: 8px;
  }

  .topbar {
    display: grid;
    grid-template-columns: 1fr;
    align-items: stretch;
    gap: 12px;
    padding: 16px 14px 10px;
  }

  h1 {
    font-size: 2rem;
  }

  h2 {
    font-size: 1.35rem;
  }

  .toolbar {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 7px;
    width: 100%;
  }

  .column-control {
    justify-content: space-between;
    min-width: 0;
    min-height: 40px;
    padding: 0 10px;
  }

  .column-control select {
    min-width: 42px;
  }

  .tool-button,
  .secondary-button,
  .primary-button {
    min-height: 40px;
    padding: 0 10px;
    font-size: 0.95rem;
  }

  .tool-button {
    min-width: 0;
  }

  .value-strip {
    margin: 0 14px 10px;
    padding: 10px 12px;
  }

  .value-strip strong {
    font-size: 1.55rem;
  }

  .abacus-stage {
    padding: 8px 10px 18px;
  }

  .abacus-frame {
    width: 100%;
    min-height: 0;
    aspect-ratio: 0.96;
    border-width: 12px;
  }

  .bead {
    width: clamp(44px, 15vw, 58px);
    height: clamp(28px, 10vw, 40px);
  }

  .abacus-frame[data-columns="5"] .bead,
  .abacus-frame[data-columns="6"] .bead {
    width: clamp(34px, 11vw, 48px);
    height: clamp(24px, 7.6vw, 34px);
  }

  .abacus-frame[data-columns="1"] .bead {
    width: clamp(58px, 22vw, 76px);
    height: clamp(38px, 14vw, 52px);
  }

  .rod {
    width: 6px;
  }

  .divider {
    height: 11px;
  }

  .place-label {
    width: 42px;
    font-size: 0;
  }

  .place-label::after {
    content: attr(data-short-label);
    font-size: 0.68rem;
  }

  .mentor-panel {
    min-height: 0;
  }

  .mentor-header {
    padding: 18px 20px 12px;
  }

  .lesson-body {
    padding: 0 20px 16px;
  }

  .lesson-progress {
    padding: 0 20px 14px;
  }

  .lesson-actions {
    padding: 0 20px 20px;
  }
}

.print-view {
  display: none;
}

@media print {
  body {
    background: white;
  }
  .app-shell {
    display: none !important;
  }
  .print-view {
    display: block !important;
    padding: 0;
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }
  .print-view * {
    -webkit-print-color-adjust: exact !important;
    print-color-adjust: exact !important;
  }
  .print-header {
    text-align: center;
    margin-bottom: 15px;
  }
  .print-header h2 {
    font-size: 20px;
    margin: 0 0 5px;
  }
  .print-header p {
    font-size: 14px;
    color: var(--muted);
    margin: 0;
  }
  .print-page {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px 30px;
  }
  .print-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    break-inside: avoid;
    page-break-inside: avoid;
  }
  .print-abacus {
    width: 100%;
    max-width: 320px;
    min-height: 0 !important;
    border-width: 6px !important;
    box-shadow: none !important;
  }
  .print-abacus .divider {
    background: #6c4e31 !important;
    box-shadow: none !important;
    height: 8px !important;
  }
  .print-blank {
    margin-top: 15px;
    width: 60%;
    border-bottom: 2px solid #000;
    height: 20px;
  }
  .print-abacus .bead {
    width: 10% !important;
    height: 8% !important;
    box-shadow: inset 0 2px 4px rgba(255, 255, 255, 0.5) !important;
    border: 1px solid rgba(0, 0, 0, 0.2) !important;
  }
  .print-abacus[data-columns="5"] .bead,
  .print-abacus[data-columns="6"] .bead {
    width: 8% !important;
    height: 7% !important;
  }
  .print-abacus[data-columns="1"] .bead {
    width: 12% !important;
    height: 10% !important;
  }
  .print-abacus .place-label {
    width: 32px !important;
    font-size: 0 !important;
  }
  .print-abacus .place-label::after {
    content: attr(data-short-label);
    font-size: 0.55rem;
  }
}
