:root {
  --ink: #18324b;
  --muted: #5f7182;
  --paper: #f5f9fc;
  --card: #ffffff;
  --line: #d9e4ec;
  --blue: #1976d2;
  --blue-dark: #0f579d;
  --blue-soft: #e8f3fc;
  --red: #c83745;
  --gold: #f2b134;
  --green: #14865f;
  --green-soft: #e7f7f0;
  --orange-soft: #fff4df;
  --shadow: 0 18px 48px rgba(24, 50, 75, .10);
}

* { box-sizing: border-box; }

html { min-height: 100%; background: var(--paper); }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background:
    radial-gradient(circle at 8% 2%, rgba(25, 118, 210, .10), transparent 28rem),
    linear-gradient(180deg, #fafdff, var(--paper));
  font-family: ui-rounded, "Avenir Next", Avenir, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; }
button:focus-visible { outline: 3px solid rgba(25,118,210,.35); outline-offset: 3px; }

.topbar {
  min-height: 68px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: max(12px, env(safe-area-inset-top)) max(20px, env(safe-area-inset-right)) 12px max(20px, env(safe-area-inset-left));
  border-bottom: 1px solid rgba(24, 50, 75, .10);
  background: rgba(255,255,255,.88);
  backdrop-filter: blur(18px);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand { color: var(--ink); display: inline-flex; align-items: center; gap: .65rem; text-decoration: none; font-size: 1.05rem; font-weight: 800; }
.brand-mark { width: 36px; height: 36px; display: grid; place-items: center; border-radius: 11px; color: white; background: var(--ink); font-size: 1.35rem; }
.header-actions { display: flex; align-items: center; gap: .65rem; }
.score-chip { min-height: 38px; display: flex; align-items: center; gap: .35rem; padding: .45rem .8rem; border-radius: 999px; background: var(--orange-soft); color: #714b08; font-size: .9rem; }
.score-chip > span { color: var(--gold); }
.icon-button { width: 40px; height: 40px; border: 1px solid var(--line); border-radius: 12px; color: var(--ink); background: white; font-size: 1.25rem; cursor: pointer; }

main { width: min(1180px, calc(100% - 32px)); margin: 0 auto; padding: 28px 0 max(40px, env(safe-area-inset-bottom)); }

.mode-picker { display: flex; align-items: flex-end; justify-content: space-between; gap: 1.5rem; margin-bottom: 18px; }
.eyebrow { margin: 0 0 .25rem; color: var(--blue-dark); font-size: .78rem; line-height: 1.2; font-weight: 800; letter-spacing: .11em; text-transform: uppercase; }
h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 0; font-size: clamp(1.65rem, 3vw, 2.45rem); line-height: 1.05; letter-spacing: -.035em; }
h2 { letter-spacing: -.02em; }

.segmented { display: flex; flex-wrap: wrap; justify-content: flex-end; max-width: 680px; padding: 5px; gap: 3px; border: 1px solid var(--line); border-radius: 15px; background: white; box-shadow: 0 6px 20px rgba(24,50,75,.05); }
.segmented button { min-height: 40px; padding: .55rem .9rem; border: 0; border-radius: 10px; color: var(--muted); background: transparent; font-weight: 750; cursor: pointer; }
.segmented button[aria-selected="true"] { color: white; background: var(--ink); box-shadow: 0 4px 10px rgba(24,50,75,.2); }

.workspace { display: grid; grid-template-columns: minmax(300px, .82fr) minmax(460px, 1.28fr); gap: 18px; align-items: stretch; }
.challenge-card, .graph-card, .progress-panel { border: 1px solid rgba(24,50,75,.10); background: var(--card); box-shadow: var(--shadow); }
.challenge-card { min-height: 620px; border-radius: 24px; padding: clamp(20px, 3vw, 32px); display: flex; flex-direction: column; }
.challenge-topline { display: flex; justify-content: space-between; align-items: center; color: var(--muted); font-size: .85rem; font-weight: 750; }
.question-count { color: var(--blue-dark); }
.streak { display: inline-flex; align-items: center; gap: .35rem; }
.streak.hot::before { content: "●"; color: var(--gold); }
.instruction { margin: 25px 0 16px; font-size: clamp(1.3rem, 2.3vw, 1.85rem); font-weight: 800; line-height: 1.23; letter-spacing: -.025em; }
.equation { display: flex; align-items: center; flex-wrap: wrap; gap: .65rem; padding: 14px 16px; border-radius: 14px; background: #f3f7fa; font-family: Georgia, "Times New Roman", serif; font-size: 1.08rem; }
.equation-original { color: var(--red); }
.equation-answer { color: var(--blue-dark); }
.tip { display: flex; gap: .7rem; align-items: center; margin: 18px 0; padding: 12px 14px; border: 1px solid #cae0f3; border-radius: 13px; color: #35566f; background: var(--blue-soft); font-size: .92rem; line-height: 1.4; }
.tip-icon { font-size: 1.15rem; }
.control-area { border-top: 1px solid var(--line); padding-top: 16px; }
.control-label { margin-bottom: 10px; color: var(--muted); font-size: .78rem; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; }
.tool-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 9px; }
.tool-button { min-height: 46px; border: 1px solid #c8d7e3; border-radius: 12px; color: var(--ink); background: white; font-weight: 800; cursor: pointer; }
.tool-button:hover { border-color: #8eb8d9; background: #f8fbfd; }
.tool-button.selected { border-color: var(--blue); color: var(--blue-dark); background: var(--blue-soft); box-shadow: inset 0 0 0 1px var(--blue); }
.stepper-row { display: grid; grid-template-columns: 1fr 42px 38px 42px; align-items: center; gap: 8px; margin: 8px 0; }
.stepper-title { font-weight: 700; }
.step-button { width: 42px; height: 42px; border: 1px solid #c8d7e3; border-radius: 12px; color: var(--ink); background: white; font-size: 1.4rem; cursor: pointer; }
.step-button:active { transform: scale(.96); background: var(--blue-soft); }
.stepper-row output { text-align: center; font-variant-numeric: tabular-nums; font-size: 1.15rem; font-weight: 800; }
.answer-option-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 9px; }
.answer-option { min-height: 58px; display: flex; align-items: center; gap: 10px; padding: .65rem .75rem; border: 1px solid #c8d7e3; border-radius: 13px; color: var(--ink); background: white; text-align: left; cursor: pointer; }
.answer-option:hover { border-color: #8eb8d9; background: #f8fbfd; }
.answer-option.selected { border-color: var(--blue); color: var(--blue-dark); background: var(--blue-soft); box-shadow: inset 0 0 0 1px var(--blue); }
.option-letter { width: 28px; height: 28px; flex: 0 0 28px; display: grid; place-items: center; border-radius: 8px; color: var(--muted); background: #eef3f6; font-size: .78rem; font-weight: 900; }
.answer-option.selected .option-letter { color: white; background: var(--blue); }
.option-equation { font-family: Georgia, serif; font-size: 1rem; line-height: 1.2; }
.feedback { min-height: 52px; margin-top: 16px; padding: 0; border-radius: 13px; font-size: .92rem; font-weight: 700; line-height: 1.4; }
.feedback:not(:empty) { padding: 12px 14px; }
.feedback.correct { color: #0e6648; background: var(--green-soft); }
.feedback.try-again { color: #7b4d03; background: var(--orange-soft); }
.challenge-actions { display: flex; gap: 10px; margin-top: auto; padding-top: 18px; }
.primary-button, .secondary-button { min-height: 50px; border-radius: 14px; padding: .75rem 1.1rem; border: 0; font-weight: 800; cursor: pointer; }
.primary-button { flex: 1; color: white; background: var(--blue); box-shadow: 0 7px 16px rgba(25,118,210,.22); }
.primary-button:active { background: var(--blue-dark); transform: translateY(1px); }
.secondary-button { border: 1px solid var(--line); color: var(--muted); background: white; }

.graph-card { border-radius: 24px; padding: clamp(14px, 2.2vw, 24px); display: flex; flex-direction: column; overflow: hidden; }
.graph-legend { min-height: 34px; display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 18px; color: var(--muted); font-size: .83rem; }
.graph-legend span { display: inline-flex; align-items: center; gap: 6px; }
.line { display: inline-block; width: 23px; height: 4px; border-radius: 3px; }
.original-line { background: var(--red); }
.student-line { background: var(--blue); }
.target-line { border-top: 3px dashed var(--green); height: 1px; }
.canvas-wrap { position: relative; flex: 1; min-height: 500px; border-radius: 18px; overflow: hidden; background: #fbfdff; touch-action: none; }
#graphCanvas { display: block; width: 100%; height: 100%; cursor: grab; }
#graphCanvas.dragging { cursor: grabbing; }
#graphCanvas.modifier-armed { cursor: crosshair; }
.coordinate-pill { position: absolute; left: 12px; bottom: 12px; padding: 7px 10px; border: 1px solid rgba(24,50,75,.12); border-radius: 9px; color: var(--blue-dark); background: rgba(255,255,255,.9); font-size: .78rem; font-weight: 800; pointer-events: none; }
.gesture-guide { position: absolute; top: 12px; left: 50%; transform: translateX(-50%); z-index: 2; padding: 8px 11px; border: 1px solid #b9ccd9; border-radius: 10px; color: var(--ink); background: rgba(255,255,255,.94); box-shadow: 0 5px 16px rgba(24,50,75,.10); font-size: .78rem; white-space: nowrap; pointer-events: none; }
.gesture-guide kbd { padding: 2px 6px; border: 1px solid #aebdca; border-bottom-width: 2px; border-radius: 5px; background: #f5f7f9; font-family: inherit; font-weight: 800; }
.graph-caption { margin: 10px 0 0; min-height: 22px; text-align: center; color: var(--muted); font-size: .82rem; }

.progress-panel { margin-top: 18px; min-height: 88px; display: grid; grid-template-columns: auto minmax(140px, 1fr) auto; align-items: center; gap: 22px; border-radius: 20px; padding: 16px 20px; box-shadow: 0 10px 30px rgba(24,50,75,.06); }
.progress-panel h2 { margin: 0; font-size: 1.1rem; }
.session-stats { margin: .25rem 0 0; color: var(--muted); font-size: .8rem; }
.progress-panel.complete { border-color: rgba(20,134,95,.34); background: linear-gradient(90deg, var(--green-soft), white 42%); }
.progress-panel.complete .progress-track > div { background: var(--green); }
.progress-track { height: 9px; border-radius: 99px; overflow: hidden; background: #e8eef3; }
.progress-track > div { width: 0%; height: 100%; border-radius: inherit; background: linear-gradient(90deg, var(--blue), #4bb3fd); transition: width .35s ease; }
.text-button { border: 0; padding: .5rem; color: var(--blue-dark); background: transparent; font-weight: 800; cursor: pointer; }
.progress-actions { display: flex; justify-content: flex-end; align-items: center; gap: .5rem; flex-wrap: wrap; }
.reset-points-button { color: #8a3b42; }

.hidden { display: none !important; }

dialog { width: min(420px, calc(100% - 32px)); border: 0; border-radius: 22px; color: var(--ink); box-shadow: 0 30px 80px rgba(10,30,45,.3); }
dialog::backdrop { background: rgba(10,30,45,.55); backdrop-filter: blur(3px); }
dialog form { position: relative; padding: 20px; }
.dialog-close { position: absolute; top: 2px; right: 2px; width: 38px; height: 38px; border: 0; background: transparent; color: var(--muted); font-size: 1.6rem; cursor: pointer; }
.dialog-icon { width: 54px; height: 54px; margin-bottom: 16px; display: grid; place-items: center; border-radius: 16px; color: white; background: var(--blue); font-size: 1.35rem; font-weight: 800; }
dialog h2 { margin-bottom: 10px; font-size: 1.45rem; }
dialog ol { padding-left: 1.35rem; line-height: 1.65; }
dialog p { color: var(--muted); line-height: 1.5; }
.dialog-done { width: 100%; }

@media (max-width: 850px) {
  main { width: min(100% - 22px, 700px); padding-top: 20px; }
  .mode-picker { align-items: stretch; flex-direction: column; gap: 14px; }
  .segmented { flex-wrap: nowrap; justify-content: flex-start; max-width: 100%; overflow-x: auto; }
  .segmented button { flex: 1; white-space: nowrap; }
  .workspace { grid-template-columns: 1fr; }
  .graph-card { grid-row: 1; }
  .challenge-card { min-height: auto; }
  .canvas-wrap { min-height: min(60vh, 560px); }
  .challenge-actions { margin-top: 10px; }
  .progress-panel { grid-template-columns: 1fr auto; gap: 10px 16px; }
  .progress-track { grid-column: 1 / -1; grid-row: 2; }
}

@media (max-width: 520px) {
  .brand > span:last-child { display: none; }
  .score-chip { font-size: .8rem; }
  .mode-picker { margin-bottom: 12px; }
  .segmented button { padding-inline: .7rem; font-size: .88rem; }
  .graph-card { padding: 10px; border-radius: 19px; }
  .canvas-wrap { min-height: 440px; }
  .challenge-card { padding: 20px 17px; border-radius: 19px; }
  .instruction { margin-top: 18px; }
  .answer-option-grid { grid-template-columns: 1fr; }
  .gesture-guide { max-width: calc(100% - 20px); white-space: normal; text-align: center; }
  .progress-panel { grid-template-columns: 1fr; }
  .progress-track { grid-column: auto; grid-row: auto; }
  .text-button { text-align: left; padding-left: 0; }
  .progress-actions { justify-content: flex-start; }
}

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