:root {
  --bg: #f7f3ec;
  --card: #fffaf2;
  --ink: #22201c;
  --muted: #6d665e;
  --line: #c7bba9;
  --selected: #1e5f50;
  --blocked: #9a4b3d;
  --station-stroke: #211d18;
  --node: #fffdf8;
  --shadow: 0 16px 42px rgba(54, 42, 25, 0.13);
  --root-color: #1e5f50;
  --root-color-soft: rgba(30, 95, 80, 0.18);
}

* { box-sizing: border-box; }
html, body { width: 100%; height: 100%; overflow: hidden; }
body {
  margin: 0;
  color: var(--ink);
  background: radial-gradient(circle at top left, rgba(255,255,255,0.9), transparent 35rem), var(--bg);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  overscroll-behavior: none;
}

.app {
  width: 100vw;
  height: 100dvh;
  padding: clamp(8px, 1vw, 16px);
  display: grid;
  grid-template-columns: clamp(285px, 24vw, 350px) minmax(0, 1fr);
  gap: clamp(8px, 1vw, 16px);
}

.panel,
.gameCard {
  min-width: 0;
  min-height: 0;
  background: var(--card);
  border: 1px solid rgba(120, 96, 55, 0.18);
  border-radius: clamp(14px, 1.5vw, 22px);
  box-shadow: var(--shadow);
}

.panel {
  padding: clamp(14px, 1.5vw, 20px);
  display: flex;
  flex-direction: column;
  gap: clamp(10px, 1.4vh, 18px);
  overflow: hidden;
}

.brandBlock { flex: 0 0 auto; }
.eyebrow {
  margin: 0 0 5px;
  font-size: clamp(11px, 1.3vh, 13px);
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--muted);
  font-weight: 800;
}

h1 {
  margin: 0;
  font-size: clamp(28px, 4vh, 38px);
  line-height: .95;
  letter-spacing: -0.04em;
}

h2, h3 { margin: 0 0 7px; }
.subhead {
  margin: 7px 0 0;
  color: var(--muted);
  line-height: 1.35;
  font-size: clamp(13px, 1.65vh, 16px);
}

.rules,
.note,
.playHint {
  background: rgba(255, 255, 255, 0.5);
  border: 1px solid rgba(120, 96, 55, 0.14);
  border-radius: 15px;
  padding: clamp(10px, 1.3vh, 14px);
}

.rules { flex: 0 1 auto; }
.rules h2 { font-size: clamp(15px, 2vh, 20px); }
.rules ol {
  padding-left: 18px;
  margin: 0;
  line-height: 1.35;
  color: var(--muted);
  font-size: clamp(13px, 1.62vh, 15px);
}

.note,
.playHint {
  font-size: clamp(12px, 1.5vh, 14px);
  color: var(--muted);
  line-height: 1.35;
}

.controls {
  flex: 0 0 auto;
  display: grid;
  gap: clamp(6px, .9vh, 9px);
}

label {
  font-size: 13px;
  color: var(--muted);
  font-weight: 800;
}
select, button { font: inherit; }
select {
  width: 100%;
  font-size: clamp(13px, 1.62vh, 15px);
  padding: clamp(8px, 1.1vh, 11px);
  border: 1px solid var(--line);
  border-radius: 11px;
  background: white;
  color: var(--ink);
  min-height: 34px;
}

.buttonRow { display: flex; gap: 8px; }
button {
  cursor: pointer;
  font-size: clamp(12px, 1.5vh, 14px);
  border: 0;
  border-radius: 11px;
  padding: clamp(8px, 1.1vh, 11px) 12px;
  font-weight: 800;
  color: white;
  background: var(--selected);
  min-height: 34px;
}
.buttonRow button { flex: 1; }
button.secondary,
#solutionBtn,
#modalNextBtn { color: var(--ink); background: #eadfce; }
.primaryAction { width: 100%; font-size: clamp(13px, 1.7vh, 16px); }
button:hover { filter: brightness(0.96); }

.legend {
  display: grid;
  gap: 6px;
  font-size: clamp(13px, 1.55vh, 15px);
  color: var(--muted);
}
.legendLine {
  display: inline-block;
  width: 30px;
  height: 0;
  margin-right: 7px;
  vertical-align: middle;
  border-top: 4px solid var(--line);
}
.legendLine.selected { border-color: var(--selected); }
.legendLine.blocked { border-color: var(--blocked); border-top-style: dashed; }

.gameCard {
  padding: clamp(10px, 1.2vw, 18px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.gameHeader {
  flex: 0 0 auto;
  min-height: 42px;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: flex-start;
  margin-bottom: 8px;
}
.gameHeader h2 { margin: 0; font-size: clamp(16px, 2.6vh, 24px); }
#puzzleMeta { margin: 3px 0 0; color: var(--muted); font-size: clamp(13px, 1.62vh, 15px); }

.statusBadge {
  flex: 0 0 auto;
  padding: 7px 10px;
  border-radius: 999px;
  background: #eadfce;
  color: var(--ink);
  font-size: clamp(11px, 1.45vh, 13px);
  font-weight: 900;
}
.statusBadge.win { color: white; background: var(--selected); }
.statusBadge.error { color: white; background: var(--blocked); }

.boardStage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  display: grid;
  place-items: stretch;
}

#board {
  width: 100%;
  height: 100%;
  min-height: 0;
  display: block;
  background:
    linear-gradient(rgba(120,96,55,0.055) 1px, transparent 1px),
    linear-gradient(90deg, rgba(120,96,55,0.055) 1px, transparent 1px);
  background-size: 36px 36px;
  border: 1px solid rgba(120, 96, 55, 0.15);
  border-radius: 16px;
  overflow: hidden;
  touch-action: none;
}

.playHint { flex: 0 0 auto; margin-top: 8px; padding: 8px 11px; }

.edgeVisible { stroke: var(--line); stroke-width: 5; stroke-linecap: round; pointer-events: none; }
.edgeVisible.selected { stroke: var(--root-color); stroke-width: 9; }
.edgeVisible.selected.neutralOwned { stroke: var(--selected); }
.edgeVisible.selected.conflictOwned { stroke: var(--blocked); }
.edgeVisible.blocked { stroke: var(--blocked); stroke-width: 6; stroke-dasharray: 9 12; }
.edgeX { stroke: var(--blocked); stroke-width: 5; stroke-linecap: round; pointer-events: none; }

.nodeCircle { fill: var(--node); stroke: #79694e; stroke-width: 3; }
.nodeCircle.owned { fill: var(--root-color-soft); stroke: var(--root-color); }
.nodeCircle.conflictOwned { fill: rgba(154, 75, 61, 0.14); stroke: var(--blocked); }
.stationCircle { fill: var(--root-color); stroke: var(--station-stroke); stroke-width: 4; }
.stationCircle.rootComplete {
  stroke: #111;
  stroke-width: 6;
}
.stationCircle.rootOver {
  stroke: var(--blocked);
  stroke-width: 6;
}
.nodeCircle.nodeSelected,
.stationCircle.nodeSelected {
  stroke: #111;
  stroke-width: 5;
  filter: drop-shadow(0 0 7px rgba(30, 95, 80, 0.55));
}
.nodeCircle.tutorialTarget,
.stationCircle.tutorialTarget {
  stroke: #111;
  stroke-width: 5;
  filter: drop-shadow(0 0 9px rgba(154, 90, 31, 0.72));
}
.nodeLabel {
  font-size: 18px;
  font-weight: 900;
  text-anchor: middle;
  dominant-baseline: central;
  fill: var(--ink);
  user-select: none;
  pointer-events: none;
}
.rootLabel {
  fill: #fff;
  stroke: rgba(0,0,0,.55);
  stroke-width: 3px;
  paint-order: stroke fill;
  font-size: 19px;
  letter-spacing: -.03em;
}
.nodeHit { fill: transparent; cursor: grab; }

#board.dragging { cursor: grabbing; user-select: none; }
#board.dragging .nodeHit { cursor: grabbing; }
.dragPreview {
  stroke: rgba(30, 95, 80, 0.48);
  stroke-width: 7;
  stroke-linecap: round;
  stroke-dasharray: 12 10;
  pointer-events: none;
}
.dragPreview.erasePreview {
  stroke: rgba(154, 75, 61, 0.62);
}


/* Submission feedback */
.edgeVisible.submissionError {
  stroke: #c23f32 !important;
  stroke-width: 11 !important;
  filter: drop-shadow(0 0 5px rgba(194,63,50,.55));
  animation: errorPulse 1.05s ease-in-out infinite;
}
.nodeCircle.submissionError,
.stationCircle.submissionError {
  stroke: #c23f32 !important;
  stroke-width: 7 !important;
  filter: drop-shadow(0 0 9px rgba(194,63,50,.7));
  animation: errorPulse 1.05s ease-in-out infinite;
}
@keyframes errorPulse {
  0%,100% { opacity: 1; }
  50% { opacity: .62; }
}

.modalOverlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(30,27,23,.42);
  backdrop-filter: blur(2px);
}
.modalOverlay.hidden { display: none; }
.modalCard {
  width: min(650px, calc(100vw - 28px));
  max-height: min(82dvh, 700px);
  overflow: auto;
  background: var(--card);
  border-radius: 24px;
  border: 1px solid rgba(120,96,55,.22);
  box-shadow: 0 32px 90px rgba(0,0,0,.32);
  padding: 0 24px 24px;
}
.resultBanner {
  margin: 0 -24px 20px;
  padding: 24px;
  min-height: 120px;
  display: flex;
  gap: 18px;
  align-items: center;
  color: white;
  background: var(--selected);
  border-radius: 24px 24px 14px 14px;
}
.resultBanner.fail { background: var(--blocked); }
.resultBannerText {
  font-size: clamp(18px, 3vw, 30px);
  line-height: 1;
  font-weight: 1000;
  letter-spacing: .04em;
}
.resultBanner h2 {
  margin: 7px 0 0;
  font-size: clamp(28px, 4.5vw, 46px);
  line-height: 1;
  color: white;
}
.modalIcon {
  width: 72px;
  height: 72px;
  flex: 0 0 72px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  background: rgba(255,255,255,.2);
  border: 4px solid rgba(255,255,255,.8);
  color: white;
  font-size: 44px;
  font-weight: 1000;
}
#modalMessage { margin: 0 0 12px; color: var(--muted); font-size: 17px; line-height: 1.42; }
#modalList { margin: 0; padding-left: 22px; color: var(--ink); font-size: 16px; line-height: 1.5; }
#modalList li + li { margin-top: 6px; }
.modalActions { display: flex; gap: 10px; margin-top: 22px; }
.modalActions button { flex: 1; font-size: 15px; min-height: 44px; }
.feedbackQuestions { margin-top: 20px; padding-top: 18px; border-top: 1px solid rgba(120,96,55,.18); }
.feedbackQuestions.hidden { display: none; }
.feedbackQuestions p, .feedbackQuestions ol { color: var(--muted); line-height: 1.45; }

/* Tutorial coachmark */
.tutorialCoach {
  position: absolute;
  z-index: 30;
  top: 14px;
  right: 14px;
  width: min(470px, 49%);
  background: rgba(255, 250, 242, .985);
  border: 2px solid rgba(30,95,80,.42);
  border-radius: 22px;
  box-shadow: 0 22px 58px rgba(34,32,28,.25);
  padding: 18px;
  backdrop-filter: blur(10px);
  pointer-events: auto;
}
.tutorialCoach.hidden { display: none; }
.tutorialTopline { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 10px; }
.tutorialStepLabel {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .13em;
  color: var(--selected);
  font-weight: 900;
}
.tutorialBadge {
  font-size: 11px;
  font-weight: 900;
  color: var(--muted);
  background: #efe6d9;
  border-radius: 999px;
  padding: 6px 9px;
}
.tutorialBody { display: grid; grid-template-columns: 1fr; gap: 12px; align-items: center; }
.tutorialCopy h3 { margin: 0 0 7px; font-size: 22px; letter-spacing: -.02em; }
.tutorialCopy p { margin: 0; font-size: 17px; line-height: 1.42; color: var(--muted); }
.tutorialDemo {
  height: 170px;
  border-radius: 16px;
  background: #f4eee4;
  border: 1px solid rgba(120,96,55,.18);
  overflow: hidden;
}
.tutorialDemo svg { width: 100%; height: 100%; display: block; }
.tutorialActions { display: flex; justify-content: flex-end; margin-top: 12px; }
.tutorialNextBtn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 108px;
  padding: 10px 18px;
  border-radius: 12px;
  font-size: 15px;
}
.demoNode { fill: #fffdf8; stroke: #6d665e; stroke-width: 3; }
.demoRoot { fill: #1e5f50; stroke: #211d18; stroke-width: 3; }
.demoLine { stroke: #c7bba9; stroke-width: 5; stroke-linecap: round; }
.demoLineActive { stroke: #1e5f50; stroke-width: 7; stroke-linecap: round; stroke-dasharray: 120; stroke-dashoffset: 120; animation: drawDemoLine 1.4s ease-in-out infinite; }
.demoFinger { fill: #22201c; opacity: .88; animation: moveDemoFinger 1.4s ease-in-out infinite; }
.demoTapFinger { fill: #22201c; animation: tapDemo 1.1s ease-in-out infinite; }
.demoSubmit { fill: #1e5f50; rx: 8; }
.demoSubmitText { fill: white; font-size: 12px; font-weight: 900; text-anchor: middle; dominant-baseline: central; }
.demoGoalRootText { fill: white; font-size: 22px; font-weight: 900; text-anchor: middle; dominant-baseline: central; }
.demoGoalLine { stroke: #c7bba9; stroke-width: 6; stroke-linecap: round; opacity: .28; animation: goalPulse 1.8s ease-in-out infinite; }
.demoGoalLine2 { animation-delay: .18s; }
.demoGoalLine3 { animation-delay: .36s; }
.demoGoalDot1, .demoGoalDot2, .demoGoalDot3 { animation: goalDotPulse 1.8s ease-in-out infinite; transform-origin: center; transform-box: fill-box; }
.demoGoalDot2 { animation-delay: .18s; }
.demoGoalDot3 { animation-delay: .36s; }
@keyframes goalPulse {
  0%, 100% { opacity: .2; stroke: #c7bba9; }
  45%, 65% { opacity: .95; stroke: #1e5f50; }
}
@keyframes goalDotPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.14); }
}
@keyframes drawDemoLine {
  0%, 15% { stroke-dashoffset: 120; }
  70%, 100% { stroke-dashoffset: 0; }
}
@keyframes moveDemoFinger {
  0%, 15% { transform: translate(0,0); }
  70%, 100% { transform: translate(62px,0); }
}

/* Mobile: tutorial deliberately becomes a large onboarding card. */
@media (max-width: 760px) {
  .tutorialCoach {
    top: 7px;
    right: 7px;
    left: 7px;
    width: auto;
    padding: 14px;
    border-radius: 17px;
  }
  .tutorialTopline { margin-bottom: 7px; }
  .tutorialBody { grid-template-columns: minmax(0,1fr) 145px; gap: 11px; }
  .tutorialCopy h3 { font-size: 18px; margin-bottom: 5px; }
  .tutorialCopy p { font-size: 14px; line-height: 1.35; }
  .tutorialDemo { height: 112px; }
  .tutorialNextBtn { font-size: 14px; padding: 9px 16px; }
}

@media (max-width: 560px) {
  .tutorialBody { grid-template-columns: 1fr; }
  .tutorialDemo { height: 118px; }
}

/* Very short phones: keep the coach readable instead of shrinking to tiny text. */
@media (max-width: 760px) and (max-height: 650px) {
  .tutorialCoach { padding: 10px; }
  .tutorialCopy h3 { font-size: 16px; }
  .tutorialCopy p { font-size: 13px; line-height: 1.3; }
  .tutorialDemo { height: 92px; }
  .tutorialTopline { margin-bottom: 5px; }
  .tutorialActions { margin-top: 7px; }
}
.demoLineActive.demoVertical {
  stroke-dasharray: 80;
  stroke-dashoffset: 80;
  animation: drawDemoVertical 1.4s ease-in-out infinite;
}
.demoFinger.demoFingerVertical {
  animation: moveDemoFingerVertical 1.4s ease-in-out infinite;
}
@keyframes drawDemoVertical {
  0%, 15% { stroke-dashoffset: 80; }
  70%, 100% { stroke-dashoffset: 0; }
}
@keyframes moveDemoFingerVertical {
  0%, 15% { transform: translate(0,0); }
  70%, 100% { transform: translate(0,27px); }
}

.demoEraseLine {
  stroke: #1e5f50;
  stroke-width: 7;
  stroke-linecap: round;
  animation: eraseDemoLine 1.5s ease-in-out infinite;
}
.demoEraseFinger {
  fill: #22201c;
  opacity: .88;
  animation: eraseDemoFinger 1.5s ease-in-out infinite;
}
@keyframes eraseDemoLine {
  0%, 25% { opacity: 1; stroke-dasharray: 0; }
  70%, 100% { opacity: .08; stroke-dasharray: 8 10; }
}
@keyframes eraseDemoFinger {
  0%, 20% { transform: translate(0,0); }
  70%, 100% { transform: translate(-62px,0); }
}

@media (max-width: 600px) {
  .modalCard { padding: 0 16px 18px; border-radius: 20px; }
  .resultBanner { margin: 0 -16px 16px; padding: 18px; min-height: 100px; border-radius: 20px 20px 12px 12px; }
  .modalIcon { width: 58px; height: 58px; flex-basis: 58px; font-size: 34px; }
  #modalMessage { font-size: 15px; }
  #modalList { font-size: 14px; }
}


/* v0.12 tutorial: double-tap a root to clear its full colored branch. */
.demoClearBranch {
  stroke: #1e5f50;
  stroke-width: 7;
  stroke-linecap: round;
  animation: clearWholeBranch 1.8s ease-in-out infinite;
}
.demoClearDot {
  animation: clearWholeDot 1.8s ease-in-out infinite;
}
.demoDoubleTapFinger {
  fill: #22201c;
  opacity: 0;
}
.demoDoubleTap1 {
  animation: doubleTapOne 1.8s ease-in-out infinite;
}
.demoDoubleTap2 {
  animation: doubleTapTwo 1.8s ease-in-out infinite;
}
.demoClearRootText {
  font-size: 13px;
}
@keyframes doubleTapOne {
  0%, 10% { opacity: 0; transform: scale(1); }
  16%, 24% { opacity: .92; transform: scale(.82); }
  30%, 100% { opacity: 0; transform: scale(1); }
}
@keyframes doubleTapTwo {
  0%, 31% { opacity: 0; transform: scale(1); }
  37%, 45% { opacity: .92; transform: scale(.82); }
  51%, 100% { opacity: 0; transform: scale(1); }
}
@keyframes clearWholeBranch {
  0%, 49% { opacity: 1; }
  58%, 78% { opacity: .05; }
  90%, 100% { opacity: 1; }
}
@keyframes clearWholeDot {
  0%, 49% { fill: rgba(30,95,80,.18); stroke: #1e5f50; }
  58%, 78% { fill: #fffdf8; stroke: #6d665e; }
  90%, 100% { fill: rgba(30,95,80,.18); stroke: #1e5f50; }
}


/* v0.13 tutorial concepts */
.demoBigRootText {
  font-size: 22px;
}
.demoNumberCaption,
.demoNumberValue {
  text-anchor: middle;
  fill: #6d665e;
  font-weight: 800;
}
.demoNumberCaption {
  font-size: 15px;
}
.demoNumberValue {
  font-size: 23px;
  fill: #22201c;
}
.demoOneStrokeRoute {
  fill: none;
  stroke: #d09145;
  stroke-width: 5;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 8 8;
  opacity: .75;
}
.demoOneStrokeFinger {
  fill: #22201c;
  opacity: .92;
  filter: drop-shadow(0 2px 2px rgba(0,0,0,.2));
}

/* Completion action hierarchy: forward is green, return is neutral. */
#modalCloseBtn.secondary {
  color: var(--ink);
  background: #eadfce;
}
#modalNextBtn {
  color: white;
  background: var(--selected);
}


/* Public 1.0 survey handoff */
.surveyBtn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-top: 8px;
  padding: 12px 16px;
  border-radius: 12px;
  background: var(--selected);
  color: white;
  text-decoration: none;
  font-weight: 900;
}
.surveyBtn:hover {
  filter: brightness(.96);
}
