body {
  min-height: 100vh;
  background:
    radial-gradient(circle at 12% 8%, rgba(37, 99, 235, .16), transparent 34%),
    radial-gradient(circle at 88% 18%, rgba(239, 68, 68, .14), transparent 30%),
    linear-gradient(135deg, #eef5ff 0%, #f8fafc 48%, #ecfeff 100%);
}

.equation-app {
  width: min(1180px, calc(100vw - 28px));
  margin: 0 auto;
  padding: 24px 0 40px;
}

.app-header,
.tool-panel,
.equation-board,
.action-bar {
  border: 1px solid rgba(148, 163, 184, .45);
  border-radius: 8px;
  background: rgba(255, 255, 255, .88);
  box-shadow: 0 18px 52px rgba(15, 23, 42, .12);
}

.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 18px;
  padding: 18px 20px;
}

.eyebrow {
  margin: 0 0 4px;
  color: #2563eb;
  font-size: .78rem;
  font-weight: 800;
  letter-spacing: .12em;
  text-transform: uppercase;
}

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

h1 {
  font-size: clamp(1.6rem, 3vw, 2.5rem);
}

.back-link {
  flex: 0 0 auto;
  padding: 10px 16px;
  border-radius: 8px;
  background: #2563eb;
  color: #fff;
  font-weight: 800;
  text-decoration: none;
}

.tool-panel {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  margin-top: 14px;
  padding: 14px 16px;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 18px;
  color: #334155;
  font-weight: 700;
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 7px;
}

.dot {
  width: 13px;
  height: 13px;
  border-radius: 50%;
}

.dot-x {
  background: #111827;
}

.dot-factor {
  background: #2563eb;
}

.dot-constant {
  background: #dc2626;
}

.message {
  min-height: 30px;
  padding: 7px 10px;
  border-radius: 8px;
  background: #eff6ff;
  color: #1e3a8a;
  font-weight: 800;
}

.message.warn {
  background: #fff7ed;
  color: #9a3412;
}

.message.good {
  background: #ecfdf5;
  color: #047857;
}

.equation-board {
  display: grid;
  gap: 6px;
  margin-top: 10px;
  padding: 10px;
}

.equation-line {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: stretch;
  gap: 8px;
  padding: 7px 9px;
  border: 1px solid rgba(148, 163, 184, .35);
  border-radius: 8px;
  background: #f8fafc;
}

.equation-line.latest {
  background: #ffffff;
  border-color: rgba(37, 99, 235, .35);
}

.equation-line.preview-line {
  border-color: rgba(14, 165, 233, .72);
  background: #f0f9ff;
}

.side {
  position: relative;
  min-height: 60px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  padding: 6px;
  border: 1px dashed transparent;
  border-radius: 8px;
}

.side.right {
  justify-content: flex-start;
}

.latest .side {
  background: rgba(241, 245, 249, .7);
}

.latest .side.drop-ready {
  border-color: #2563eb;
  background: #eff6ff;
}

.latest .side.factor-preview {
  padding-bottom: 20px;
  background: #dbeafe;
}

.latest .side.factor-preview::after {
  content: "";
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 17px;
  height: 4px;
  border-radius: 999px;
  background: #1d4ed8;
  box-shadow: 0 8px 18px rgba(29, 78, 216, .2);
}

.latest .side.factor-preview > .term-wrap {
  transform: translateY(-11px);
}

.equals {
  display: grid;
  place-items: center;
  min-width: 52px;
  color: #0f172a;
  font-size: 2rem;
  font-weight: 900;
}

.term-wrap {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  transition: transform .16s ease;
}

.sign {
  min-width: 18px;
  color: #334155;
  font-size: 1.25rem;
  font-weight: 900;
  text-align: center;
}

.sign.first {
  color: transparent;
}

.tile,
.divisor {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 38px;
  min-height: 36px;
  padding: 5px 8px;
  border: 2px solid transparent;
  border-radius: 8px;
  color: #fff;
  font-size: 1.08rem;
  font-weight: 900;
  line-height: 1;
  cursor: grab;
  user-select: none;
  touch-action: none;
}

.tile:active,
.divisor:active {
  cursor: grabbing;
}

.tile.x {
  background: #111827;
  border-color: #020617;
}

.tile.factor,
.divisor {
  background: #2563eb;
  border-color: #1d4ed8;
}

.tile.constant {
  background: #dc2626;
  border-color: #b91c1c;
}

.factor-x {
  display: inline-flex;
  align-items: center;
  gap: 3px;
}

.group-wrap {
  gap: 6px;
}

.group-factor {
  box-shadow: 0 0 0 3px rgba(37, 99, 235, .12);
}

.parenthesis {
  color: #334155;
  font-size: 1.72rem;
  font-weight: 900;
  line-height: 1;
}

.group-inner {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  padding: 2px 1px;
}

.preview-line .term-wrap {
  animation: preview-pop .18s ease-out;
}

.fraction-term {
  display: inline-grid;
  justify-items: center;
  gap: 2px;
}

.fraction-term .bar {
  width: 100%;
  min-width: 42px;
  height: 3px;
  border-radius: 999px;
  background: #1e293b;
}

.ghost-term {
  opacity: .38;
}

.drag-ghost {
  position: fixed;
  z-index: 1000;
  pointer-events: none;
  transform: translate(-50%, -50%);
  filter: drop-shadow(0 14px 20px rgba(15, 23, 42, .25));
}

.drag-ghost .factor-x {
  padding: 4px;
  border-radius: 8px;
  background: rgba(255, 255, 255, .9);
  box-shadow: 0 0 0 2px rgba(37, 99, 235, .18);
}

.hidden {
  display: none !important;
}

.action-bar {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 14px;
  padding: 14px;
}

.action-bar button {
  min-height: 44px;
  padding: 0 18px;
  border: 0;
  border-radius: 8px;
  background: #2563eb;
  color: #fff;
  font-size: 1rem;
  font-weight: 900;
  cursor: pointer;
}

.action-bar button:disabled {
  cursor: not-allowed;
  opacity: .45;
  transform: none;
}

#newBtn {
  background: #0f766e;
}

.success-panel {
  position: relative;
  overflow: hidden;
  margin-top: 8px;
  padding: 12px 16px;
  border: 1px solid rgba(16, 185, 129, .45);
  border-radius: 8px;
  background: linear-gradient(135deg, #ecfdf5, #ffffff 62%, #eff6ff);
  text-align: center;
  box-shadow: 0 18px 52px rgba(15, 23, 42, .12);
}

#confettiCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.success-content {
  position: relative;
  z-index: 1;
  display: grid;
  justify-items: center;
  gap: 4px;
}

.success-panel h2 {
  font-size: 1.45rem;
  line-height: 1.12;
}

.success-panel p:not(.eyebrow) {
  margin: 0;
  color: #334155;
  font-size: .98rem;
  font-weight: 700;
}

.success-panel button {
  min-height: 36px;
  padding: 0 14px;
  border: 0;
  border-radius: 8px;
  background: #0f766e;
  color: #fff;
  font-size: .95rem;
  font-weight: 900;
  cursor: pointer;
}

@keyframes preview-pop {
  from {
    transform: translateY(-2px);
    opacity: .72;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

@media (max-width: 760px) {
  .app-header,
  .tool-panel,
  .action-bar {
    align-items: stretch;
    flex-direction: column;
  }

  .equation-line {
    grid-template-columns: 1fr;
  }

  .equals {
    min-height: 34px;
  }

  .side,
  .side.right {
    justify-content: center;
  }
}
