:root {
  --bg: #fbf7f0;
  --ink: #1f2a44;
  --muted: #8a93a6;
  --plus: #12a37a;
  --minus: #ef5a5a;
  --accent: #4f6bed;
  --card: #ffffff;
  --st: env(safe-area-inset-top, 0px);
  --sb: env(safe-area-inset-bottom, 0px);
  --sl: env(safe-area-inset-left, 0px);
  --sr: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: "Fredoka", ui-rounded, -apple-system, system-ui, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  touch-action: none;
}

#scene { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; cursor: crosshair; }
#labels { position: fixed; inset: 0; pointer-events: none; overflow: hidden; }

/* ---- numbers under the line ---- */
.n {
  position: absolute; left: 0; top: 0;
  font-size: 14px; font-weight: 500; color: var(--muted);
  white-space: nowrap; line-height: 1;
  transition: opacity 0.25s, color 0.25s;
  will-change: transform;
}
.n.five { font-size: 16px; font-weight: 600; color: #b98a00; }
.n.ten { font-size: 22px; font-weight: 700; color: var(--ink); }
.n.on { color: var(--accent); font-weight: 700; }

/* ---- things floating above the line ---- */
.anchor { position: absolute; left: 0; top: 0; width: 0; height: 0; will-change: transform; }
.bubble {
  position: absolute; left: 0; bottom: 0;
  transform: translateX(-50%); transform-origin: 50% 100%;
  white-space: nowrap; line-height: 1;
  font-size: 17px; font-weight: 700;
  padding: 6px 10px 5px; border-radius: 999px;
  background: var(--card); color: var(--accent);
  box-shadow: 0 2px 8px rgba(31, 42, 68, 0.12);
}
.anchor.cur { z-index: 3; }
.anchor.cur .bubble {
  font-size: 26px; padding: 8px 15px 7px;
  background: var(--accent); color: #fff;
  box-shadow: 0 6px 18px rgba(79, 107, 237, 0.35);
}
.anchor.ghost { z-index: 4; }
.anchor.ghost .bubble {
  font-size: 22px; padding: 6px 13px 5px;
  background: rgba(255, 255, 255, 0.92); color: var(--accent);
  border: 2px dashed rgba(79, 107, 237, 0.55);
  box-shadow: none;
}
.bubble.pop { animation: pop 0.5s cubic-bezier(0.2, 1.6, 0.4, 1); }
.bubble.shake { animation: shake 0.4s; }

.pill {
  position: absolute; left: 0; bottom: 0;
  transform: translateX(-50%) scale(0.2); transform-origin: 50% 100%;
  opacity: 0;
  white-space: nowrap; line-height: 1;
  font-size: 20px; font-weight: 700; color: #fff;
  padding: 6px 12px 5px; border-radius: 999px;
  transition: transform 0.45s cubic-bezier(0.2, 1.6, 0.4, 1), opacity 0.25s;
}
.pill.show { transform: translateX(-50%) scale(1); opacity: 1; }
.pill.plus { background: var(--plus); }
.pill.minus { background: var(--minus); }
.anchor.pillA { z-index: 2; }
.anchor.prevA { z-index: 4; pointer-events: none; }
.pill.preview {
  transform: translateX(-50%) scale(1); opacity: 1; transition: none;
  font-size: 17px; padding: 5px 10px 4px;
  background: rgba(255, 255, 255, 0.85); border: 2px dashed currentColor;
}
.pill.preview.plus { color: var(--plus); background: rgba(255, 255, 255, 0.85); }
.pill.preview.minus { color: var(--minus); background: rgba(255, 255, 255, 0.85); }

/* ---- the sum at the top ---- */
.top {
  position: fixed; left: 0; right: 0; top: calc(var(--st) + 18px);
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  pointer-events: none;
}
.eq {
  display: flex; flex-wrap: wrap; justify-content: center; align-items: center;
  gap: 0.3em; max-width: 92vw;
  font-size: clamp(34px, 6vw, 64px); font-weight: 700; line-height: 1.15;
  padding: 10px 28px; border-radius: 26px;
  background: var(--card); box-shadow: 0 8px 28px rgba(31, 42, 68, 0.1);
  transition: font-size 0.2s;
}
.eq.empty { font-size: clamp(20px, 3vw, 28px); font-weight: 600; color: var(--muted); padding: 14px 28px; }
.eq .t { display: inline-block; white-space: nowrap; }
.eq .start { color: var(--accent); }
.eq .plus { color: var(--plus); }
.eq .minus { color: var(--minus); }
.eq .eqs { color: var(--muted); }
.eq .res { color: #fff; background: var(--accent); border-radius: 18px; padding: 0 0.28em; }
.eq .t.new { animation: termIn 0.5s cubic-bezier(0.2, 1.5, 0.4, 1); }
.tip {
  display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 6px;
  max-width: 92vw; font-size: 19px; font-weight: 500; color: var(--muted); min-height: 1.2em;
}
.tip .pt { font-size: 24px; font-weight: 700; }
.tip .pt.plus { color: var(--plus); }
.tip .pt.minus { color: var(--minus); }
.tip .pp { color: var(--muted); font-weight: 600; }
.tip .part {
  font-size: 20px; font-weight: 700; line-height: 1; padding: 6px 10px 5px; border-radius: 12px;
  background: #fff; color: #b7bfcd; box-shadow: 0 2px 8px rgba(31, 42, 68, 0.08);
  transition: background 0.2s, color 0.2s, transform 0.25s cubic-bezier(0.2, 1.6, 0.4, 1);
}
.tip .part.one { font-size: 16px; padding: 5px 8px 4px; }
.tip .part.done { background: var(--ink); color: #fff; transform: scale(1.08); }

/* ---- buttons ---- */
.bar {
  position: fixed; left: 0; right: 0; bottom: calc(var(--sb) + 16px);
  display: flex; justify-content: space-between; gap: 10px;
  padding: 0 calc(var(--sr) + 16px) 0 calc(var(--sl) + 16px);
  pointer-events: none;
}
.group { display: flex; gap: 10px; pointer-events: auto; }
.btn {
  font: inherit; font-size: 20px; font-weight: 600; color: var(--ink);
  height: 56px; min-width: 56px; padding: 0 20px;
  border: none; border-radius: 18px; background: var(--card);
  box-shadow: 0 4px 14px rgba(31, 42, 68, 0.1);
  cursor: pointer; transition: transform 0.12s, opacity 0.2s;
}
.btn.icon { width: 56px; padding: 0; font-size: 26px; }
a.btn { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }
.btn:active { transform: scale(0.93); }
.btn:disabled { opacity: 0.35; cursor: default; }

@keyframes pop { 0% { transform: translateX(-50%) scale(0.2); } 100% { transform: translateX(-50%) scale(1); } }
@keyframes shake {
  0%, 100% { transform: translateX(-50%); }
  25% { transform: translateX(calc(-50% - 7px)); }
  75% { transform: translateX(calc(-50% + 7px)); }
}
@keyframes termIn { 0% { opacity: 0; transform: translateY(14px) scale(0.6); } 100% { opacity: 1; transform: none; } }

@media (max-width: 600px) {
  .btn { height: 48px; min-width: 48px; font-size: 17px; padding: 0 14px; }
  .btn.icon { width: 48px; font-size: 22px; }
  .tip { font-size: 16px; }
}
@media (prefers-reduced-motion: reduce) {
  .bubble.pop, .bubble.shake, .eq .t.new { animation: none; }
  .pill { transition: opacity 0.2s; }
}

/* ---- settings sheet ---- */
.sheet {
  position: fixed; inset: 0; z-index: 10;
  display: flex; align-items: center; justify-content: center;
  background: rgba(31, 42, 68, 0.28);
  opacity: 0; pointer-events: none; transition: opacity 0.2s;
}
.sheet.open { opacity: 1; pointer-events: auto; }
.card {
  width: min(560px, 92vw); padding: 26px 28px 24px; border-radius: 28px;
  background: var(--card); box-shadow: 0 24px 60px rgba(31, 42, 68, 0.25);
  display: flex; flex-direction: column; gap: 22px;
  transform: translateY(16px) scale(0.98); transition: transform 0.25s cubic-bezier(0.2, 1.3, 0.4, 1);
}
.sheet.open .card { transform: none; }
.card h2 { margin: 0; font-size: 28px; }
.row { display: flex; flex-direction: column; gap: 10px; }
.row .lbl { font-size: 17px; font-weight: 600; color: var(--muted); }
.range { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.word { font-size: 20px; font-weight: 600; color: var(--muted); }
.stepper { display: inline-flex; align-items: center; gap: 6px; background: #f1f3f7; border-radius: 18px; padding: 5px; }
.stepper button {
  font: inherit; font-size: 26px; font-weight: 700; width: 48px; height: 48px;
  border: none; border-radius: 14px; background: #fff; color: var(--ink); cursor: pointer;
  box-shadow: 0 2px 6px rgba(31, 42, 68, 0.08);
}
.stepper button:active { transform: scale(0.92); }
.stepper .val { min-width: 64px; text-align: center; font-size: 28px; font-weight: 700; }
.seg { display: flex; gap: 6px; background: #f1f3f7; border-radius: 18px; padding: 5px; }
.seg button {
  flex: 1; font: inherit; font-size: 19px; font-weight: 600; height: 52px;
  border: none; border-radius: 14px; background: transparent; color: var(--ink); cursor: pointer;
}
.seg button.active { background: var(--accent); color: #fff; box-shadow: 0 4px 12px rgba(79, 107, 237, 0.3); }
.btn.done { align-self: flex-end; background: var(--accent); color: #fff; padding: 0 30px; }
