* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; overflow: hidden; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  background: #ebf2f9;
  color: #222;
  -webkit-user-select: none;
  user-select: none;
}

#app {
  display: grid;
  grid-template-rows: 56px 1fr;
  height: 100%;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0 0.75rem;
  background: rgba(255, 255, 255, 0.6);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid rgba(0, 0, 0, 0.08);
  z-index: 5;
}
.toolbar .spacer { flex: 1; }

.tool-btn {
  appearance: none;
  border: none;
  background: white;
  color: #888;
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12);
  transition: color 120ms ease, box-shadow 120ms ease, transform 80ms ease;
}
.tool-btn:hover { transform: translateY(-1px); }
.tool-btn[aria-pressed="true"] {
  color: #007aff;
  box-shadow: 0 2px 6px rgba(0, 122, 255, 0.25);
}
.tool-btn.danger { color: #e34a4a; }

/* Tower button has two icons: stacks (off) and bowl (on). Swap by aria-pressed. */
#btn-tower .icon-bowl { display: none; }
#btn-tower[aria-pressed="true"] .icon-stack { display: none; }
#btn-tower[aria-pressed="true"] .icon-bowl { display: inline-block; }

.stage {
  position: relative;
  overflow: hidden;
}

#balance-canvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

.equation-overlay {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: rgba(255, 255, 255, 0.75);
  border-radius: 6px;
  padding: 0.25rem 0.75rem;
  z-index: 3;
  pointer-events: auto;
}
.equation-overlay .eq-num,
.equation-overlay .eq-op {
  font-size: clamp(48px, 9vw, 100px);
  font-weight: 700;
  line-height: 1;
}
.eq-num {
  appearance: none;
  border: none;
  background: transparent;
  color: var(--plate-color, #E16B79);
  cursor: pointer;
  padding: 0 0.25rem;
  font: inherit;
}
/* Per-token colouring when useMinusColor is on:
   "minus" tokens use --minus-color, "total" tokens that have an attached
   minus value use a left→right gradient between plus and minus colors.
   When useMinusColor is off, --minus-color equals --plate-color so the
   gradient renders as a single solid color (no visual difference). */
.eq-num.token-minus {
  color: var(--minus-color, var(--plate-color, #E16B79));
}
.eq-num.token-total-mixed {
  background-image: linear-gradient(90deg, var(--plate-color, #E16B79) 0%, var(--minus-color, var(--plate-color, #E16B79)) 100%);
  -webkit-background-clip: text;
          background-clip: text;
  color: transparent;
}
.eq-op { color: #000; padding: 0 0.25rem; }

.verbal-overlay {
  position: absolute;
  top: calc(8px + clamp(48px, 9vw, 100px) + 8px);
  left: 50%;
  transform: translateX(-50%);
  z-index: 3;
}
.verbal-overlay button {
  appearance: none;
  border: none;
  background: rgba(255, 255, 255, 0.75);
  border-radius: 6px;
  padding: 0.25rem 0.75rem;
  font-size: clamp(20px, 3vw, 36px);
  color: rgba(0, 0, 0, 0.8);
  cursor: pointer;
}

.numberline-container {
  position: absolute;
  bottom: 8px;
  left: 8px;
  right: 8px;
  height: 170px;
  background: rgba(255, 255, 255, 0.5);
  border-radius: 8px;
  z-index: 2;
}

.settings-panel,
.keypad-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}
.settings-panel[hidden],
.keypad-modal[hidden] { display: none !important; }

.settings-panel .panel-content {
  width: min(380px, 92vw);
  max-height: 92vh;
  overflow-y: auto;
  background: white;
  border-radius: 12px;
  padding: 1rem 1.25rem;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.2);
}

@media (orientation: landscape) and (min-width: 720px) {
  .settings-panel { justify-content: flex-end; }
  .settings-panel .panel-content {
    width: 320px;
    height: 100vh;
    max-height: 100vh;
    border-radius: 0;
    box-shadow: -8px 0 24px rgba(0, 0, 0, 0.15);
  }
}

.keypad-modal .panel-content {
  width: min(380px, 92vw);
  background: white;
  border-radius: 12px;
  padding: 1rem;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.2);
}

.keypad-display-row {
  display: flex;
  gap: 0.5rem;
  align-items: stretch;
  margin-bottom: 0.75rem;
}
.keypad-display {
  flex: 1;
  background: #f5f5f7;
  border-radius: 8px;
  padding: 0.75rem 1rem;
  font-size: 28px;
  display: flex;
  align-items: center;
  min-height: 64px;
}
.keypad-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.5rem;
}
.keypad-btn {
  appearance: none;
  border: none;
  background: rgba(0, 0, 0, 0.06);
  border-radius: 10px;
  font-size: 24px;
  font-weight: 500;
  height: 64px;
  cursor: pointer;
}
.keypad-btn.primary { background: #007aff; color: white; }
.keypad-btn:hover { background: rgba(0, 0, 0, 0.1); }
.keypad-btn.primary:hover { background: #0066d6; }

.settings .panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.5rem;
}
.settings h2 { font-size: 18px; margin: 0; }
.settings h3 {
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  color: #666;
  margin: 1rem 0 0.5rem;
}
.settings section { padding: 0.25rem 0; border-top: 1px solid #eee; }
.settings section:first-of-type { border-top: none; }
.settings label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.4rem 0;
  font-size: 15px;
}
.settings label.checkbox { justify-content: flex-start; gap: 0.5rem; }
.settings select, .settings input[type="color"], .settings input[type="number"] { font-size: 15px; padding: 0.15rem; }
.settings input[type="number"] { width: 80px; text-align: right; }

/* ---- Toast ---- */
.toast {
  position: fixed;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%) translateY(8px);
  background: rgba(40, 40, 40, 0.88);
  color: #fff;
  padding: 0.55rem 0.95rem;
  border-radius: 8px;
  font-size: 14px;
  z-index: 50;
  pointer-events: none;
  opacity: 0;
  transition: opacity 220ms ease, transform 220ms ease;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.18);
}
.toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
.toast[hidden] { display: none !important; }
.close-btn {
  appearance: none;
  border: none;
  background: transparent;
  font-size: 18px;
  width: 32px;
  height: 32px;
  cursor: pointer;
}

/* Prevent iOS Safari double-tap zoom */
button { touch-action: manipulation; }

/* Hide scroll bars on the canvas stage on small viewports */
.stage { -webkit-overflow-scrolling: touch; }

/* Make sure equation overlay doesn't intercept canvas pointer events outside its bounds */
.equation-overlay { pointer-events: auto; }
.stage > canvas { pointer-events: auto; }

.equation-overlay.at-bottom {
  top: auto;
  bottom: 16px;
}
.verbal-overlay.at-bottom {
  top: auto;
  bottom: calc(16px + clamp(48px, 9vw, 100px) + 8px);
}

/* ---- Equation/Number shutters & handles ---- */
.eq-side { display: inline-flex; align-items: stretch; gap: 0.25rem; }
.num-piece { position: relative; display: inline-flex; align-items: stretch; }
.equation-overlay .num-op {
  font-size: clamp(48px, 9vw, 100px);
  font-weight: 700;
  line-height: 1;
  color: #000;
  padding: 0 0.1rem;
  user-select: none;
}
.radio-group-label { font-size: 13px; color: #666; margin: 0.5rem 0 0.25rem; }

.shutter-var {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(48px, 9vw, 100px);
  font-weight: 700;
  line-height: 1;
  color: #1a1a1a;
  z-index: 5;
  opacity: 0;
  pointer-events: none;
  transition: opacity 380ms cubic-bezier(0.2, 0.7, 0.3, 1);
}
.shutter-var.show { opacity: 1; }

.overlay-shutter {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 100%;
  pointer-events: none;
  background-image:
    linear-gradient(180deg, #5a6068 0, #5a6068 4px, transparent 4px),
    repeating-linear-gradient(180deg, #b6bdc4 0, #b6bdc4 6px, #8b9098 6px, #8b9098 7px);
  border-radius: 6px;
  transform-origin: bottom;
  transform: scaleY(var(--shutter, 0));
  transition: transform 400ms cubic-bezier(0.2, 0.7, 0.3, 1);
  z-index: 4;
}

.overlay-handle {
  position: absolute;
  bottom: -14px;
  left: 0;
  right: 0;
  height: 14px;
  border-radius: 6px 6px 8px 8px;
  border: none;
  background: rgba(0, 0, 0, 0.12);
  cursor: pointer;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 5;
  transition: background 120ms ease;
}
.overlay-handle:hover { background: rgba(0, 0, 0, 0.22); }
.overlay-handle::before {
  content: "";
  display: block;
  width: 0; height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-bottom: 7px solid rgba(60, 60, 60, 0.75);
}
.overlay-handle.closed::before {
  border-bottom: none;
  border-top: 7px solid rgba(60, 60, 60, 0.75);
}
/* Equation-wide handle sits below the per-number handles, flush with overlay */
#eq-handle { bottom: -14px; }
/* Pull per-number handles up by the overlay's bottom padding so they sit flush with the overlay edge */
.num-handle { bottom: calc(-14px + 0.25rem); }
.overlay-handle[hidden] { display: none; }
.overlay-shutter[hidden] { display: none; }

/* ---- Share modal ---- */
.share-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}
.share-modal[hidden] { display: none !important; }
.share-modal .panel-content {
  width: min(440px, 92vw);
  background: white;
  border-radius: 12px;
  padding: 1rem 1.25rem 1.25rem;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.2);
}
.share-modal .panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.75rem;
}
.share-modal h2 { font-size: 18px; margin: 0; }
.share-modal .url-row {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1rem;
}
.share-modal .url-input {
  flex: 1;
  font-size: 13px;
  font-family: ui-monospace, SFMono-Regular, monospace;
  padding: 0.5rem 0.75rem;
  border: 1px solid #ccc;
  border-radius: 6px;
  background: #f6f6f8;
  color: #333;
  user-select: all;
  overflow-x: auto;
}
.share-modal .copy-btn {
  appearance: none;
  border: none;
  background: #007aff;
  color: white;
  border-radius: 6px;
  padding: 0 0.85rem;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
}
.share-modal .copy-btn.copied { background: #34c759; }
.share-modal .qr-host {
  display: flex;
  justify-content: center;
  padding: 0.5rem 0;
}
.share-modal .qr-host svg {
  width: min(260px, 60vw);
  height: auto;
  display: block;
}
.share-modal .hint {
  font-size: 12px;
  color: #666;
  text-align: center;
  margin-top: 0.5rem;
}

/* ---- Info modal ---- */
.info-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}
.info-modal[hidden] { display: none !important; }
.info-modal .panel-content {
  width: min(560px, 92vw);
  max-height: 90vh;
  overflow-y: auto;
  background: white;
  border-radius: 12px;
  padding: 1rem 1.25rem 1.25rem;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.2);
  font-size: 15px;
  line-height: 1.5;
  color: #333;
}
.info-modal .panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.75rem;
}
.info-modal h2 { font-size: 18px; margin: 0; }
.info-modal .info-hero {
  display: flex;
  justify-content: center;
  margin: 0.25rem 0 0.5rem;
}
.info-modal .info-hero img {
  width: 96px;
  height: 96px;
  border-radius: 22px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.12);
}
.info-modal h3 {
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  color: #666;
  margin: 1.25rem 0 0.5rem;
}
.info-modal p { margin: 0.5rem 0; }
.info-modal ul, .info-modal ol { margin: 0.5rem 0; padding-left: 1.25rem; }
.info-modal li { margin: 0.25rem 0; }
.info-modal details {
  margin: 0.75rem 0;
  border: 1px solid #eee;
  border-radius: 8px;
  padding: 0.5rem 0.75rem;
}
.info-modal details > summary {
  cursor: pointer;
  font-weight: 600;
  color: #333;
  list-style: none;
}
.info-modal details > summary::-webkit-details-marker { display: none; }
.info-modal details > summary::before {
  content: "▸";
  display: inline-block;
  margin-right: 0.4rem;
  transition: transform 150ms ease;
  color: #888;
}
.info-modal details[open] > summary::before { transform: rotate(90deg); }
.info-modal a {
  color: #007aff;
  text-decoration: none;
}
.info-modal a:hover { text-decoration: underline; }
.info-modal .info-links {
  margin-top: 1rem;
  padding-top: 0.75rem;
  border-top: 1px solid #eee;
  font-size: 13px;
  color: #888;
  text-align: center;
}
