html {
  height: 100%;
}

body {
  min-height: 100vh;
  height: auto !important;
  overflow: auto !important;
  background: var(--paper);
  color: var(--ink);
}

body > header {
  padding: 18px clamp(14px, 3vw, 40px) 12px !important;
  border-bottom: 1px solid var(--line);
  background: var(--card);
  box-shadow: 0 8px 20px rgba(30, 27, 23, 0.04);
}

body > header h1 {
  margin: 0;
  font-size: clamp(18px, 2.4vw, 26px) !important;
  line-height: 1.35;
  letter-spacing: 0;
}

.nav-bar {
  box-shadow: 0 8px 20px rgba(30, 27, 23, 0.06);
}

.nav-btn,
.card,
.stage,
.stage-container,
.control-panel,
.guide-text,
.formula-box,
.btn {
  border-radius: 8px !important;
}

.nav-btn:focus-visible,
.card-link:focus-visible .card,
.btn:focus-visible {
  outline: 3px solid rgba(45, 97, 139, 0.25);
  outline-offset: 2px;
}

main:has(.stage-container) {
  width: min(1440px, 100%);
  margin: 0 auto;
  display: grid !important;
  grid-template-columns: minmax(0, 2.5fr) minmax(290px, 0.9fr);
  gap: 20px !important;
  padding: 20px clamp(14px, 3vw, 40px) 28px !important;
  height: calc(100vh - 128px) !important;
  min-height: 560px;
}

.stage-container {
  min-width: 0;
  min-height: 0;
  border: 1px solid var(--line) !important;
  background: #fff;
  box-shadow: 0 10px 25px rgba(30, 27, 23, 0.05) !important;
  overflow: hidden;
}

.stage-container svg {
  width: min(96%, 1120px) !important;
  height: 96% !important;
  max-height: none !important;
}

.control-panel {
  min-width: 0;
  min-height: 0;
  padding: 18px !important;
  border: 1px solid var(--line) !important;
  background: var(--card);
  box-shadow: 0 10px 25px rgba(30, 27, 23, 0.05) !important;
  overflow: hidden;
}

.info-top {
  min-height: 0;
  overflow: auto;
  padding-right: 2px;
}

.info-top h2 {
  font-size: clamp(16px, 1.8vw, 19px) !important;
  line-height: 1.45;
  margin-bottom: 12px !important;
}

.guide-text {
  min-height: 0 !important;
  max-height: min(36vh, 310px);
  overflow: auto;
  margin-bottom: 14px !important;
  padding: 12px 14px !important;
  border: 1px solid #e5dccf !important;
  border-left: 4px solid var(--blue) !important;
  background: #fff !important;
  color: var(--ink);
  overflow-wrap: anywhere;
  word-break: normal;
}

.formula-box {
  padding: 12px 14px !important;
  border: 1px solid rgba(185, 78, 59, 0.18) !important;
  border-left: 4px solid var(--shade) !important;
  background: #fff8f5 !important;
}

.formula-box h3 {
  line-height: 1.4;
}

.formula-box p,
#formulaText {
  line-height: 1.55 !important;
  overflow-wrap: anywhere;
  word-break: normal;
  font-variant-numeric: tabular-nums;
}

.formula-line {
  display: inline-block;
  opacity: 0;
  transform: translateY(4px);
  animation: formulaLineIn 0.36s ease forwards;
}

.button-group {
  padding-top: 14px;
}

.btn {
  min-height: 42px;
}

.svg-label,
.letter-tag,
.label {
  paint-order: stroke;
  stroke: #fff;
  stroke-width: 3px;
  stroke-linejoin: round;
  vector-effect: non-scaling-stroke;
}

/* Dynamic explanation belongs in the right panel; keep the geometry stage clean. */
.math-tag {
  display: none;
}

.stage-container::after {
  content: "";
  position: absolute;
  inset: 10px;
  pointer-events: none;
  border: 2px solid rgba(45, 97, 139, 0);
  border-radius: 8px;
}

.stage-container.snap-ok::after {
  animation: stageSnapOk 0.72s ease-out;
}

.drag-piece.piece-snapped {
  animation: pieceSnapPulse 0.5s ease-out;
}

.card {
  transition: transform 0.2s ease, box-shadow 0.2s ease, border-color 0.2s ease;
}

.card:hover {
  transform: translateY(-3px);
}

@keyframes formulaLineIn {
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes stageSnapOk {
  0% {
    border-color: rgba(45, 97, 139, 0);
    box-shadow: inset 0 0 0 0 rgba(45, 97, 139, 0);
  }
  35% {
    border-color: rgba(45, 97, 139, 0.7);
    box-shadow: inset 0 0 0 6px rgba(45, 97, 139, 0.08);
  }
  100% {
    border-color: rgba(45, 97, 139, 0);
    box-shadow: inset 0 0 0 16px rgba(45, 97, 139, 0);
  }
}

@keyframes pieceSnapPulse {
  0% {
    filter: drop-shadow(0 0 0 rgba(45, 97, 139, 0));
  }
  45% {
    filter: drop-shadow(0 0 8px rgba(45, 97, 139, 0.45));
  }
  100% {
    filter: drop-shadow(0 0 0 rgba(45, 97, 139, 0));
  }
}

@media (max-width: 900px) {
  body > header {
    padding-top: 14px !important;
  }

  main:has(.stage-container) {
    display: block !important;
    height: auto !important;
    min-height: 0;
  }

  .stage-container {
    height: min(56vh, 430px) !important;
    min-height: 300px;
    margin-bottom: 14px;
  }

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

  .guide-text {
    max-height: none;
  }
}

@media (max-width: 700px) {
  .nav-bar {
    justify-content: center !important;
  }

  .nav-title {
    order: -1;
    flex: 0 0 100% !important;
    min-width: 0 !important;
  }

  .nav-btn {
    padding: 6px 10px !important;
  }

  body > header h1 {
    font-size: 17px !important;
  }

  .stage-container {
    height: 46vh !important;
    min-height: 260px;
  }
}
