* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

:root {
  --bg-outer: #1a161d;
  --bg-inner: #2d2631;
  --accent: #c084fc;
  --accent-soft: #a855f7;
  --accent-deep: #6b2ea0;
  --pink: #f472b6;
  --muted: #9ca3af;
  --good: #4ade80;
  --warn: #f87171;
  --s-curve: cubic-bezier(0.83, 0, 0.17, 1);
  --pop: cubic-bezier(0.18, 0.89, 0.35, 1.15);
}

html, body { margin: 0; padding: 0; }
body {
  background-color: var(--bg-inner);
  color: white;
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overflow: hidden;
  height: 100vh; height: 100dvh;
  display: flex; justify-content: center;
  user-select: none; -webkit-user-select: none;
}

.app {
  width: 500px; max-width: 100%; height: 100%;
  display: flex; flex-direction: column; align-items: center;
  background-color: transparent;
  padding: 18px 20px calc(10px + env(safe-area-inset-bottom)) 20px;
  position: relative;
}

.title {
  font-size: 2.25rem; font-weight: 900; letter-spacing: 2px;
  margin: 4px 0 6px;
}
.title .accent { color: var(--accent); }

.top-icons { display: flex; gap: 18px; margin-bottom: 8px; color: var(--muted); }
.top-icons button {
  background: none; border: none; color: inherit; padding: 4px; cursor: pointer;
  transition: color 0.2s, transform 0.2s;
}
.top-icons button:hover { color: var(--accent); transform: translateY(-1px); }

.stage {
  position: relative; width: 100%; flex: 1;
  display: flex; align-items: center; justify-content: center;
  min-height: 0;
}
.stage svg {
  width: 100%; height: 100%;
  display: block;
  touch-action: none;
  -webkit-touch-callout: none;
}

.hit-pad { fill: transparent; cursor: crosshair; }
.stage, .stage svg, #board { overflow: visible; }
#shape-layer, #pole-layer, #centroid-point-layer, #balance-hover-layer,
#centroid-ideal-layer, #cut-layer, #cut-lines-layer, #cut-points-layer,
#inscribe-ideal-layer, #inscribe-lines-layer, #inscribe-points-layer,
#inscribe-hover-layer, #label-layer { overflow: visible; }

.shape-fill, .shape-outline, .piece, .label-leader, .label-dot, .label-text, .cut-line {
  pointer-events: none;
}

.shape-fill {
  fill: var(--accent-soft); fill-opacity: 0.78;
  fill-rule: evenodd;
  stroke: none;
}
.shape-outline {
  fill: none;
  stroke: var(--accent); stroke-width: 2.5; stroke-linejoin: round;
  filter: drop-shadow(0 0 12px rgba(192,132,252,0.28));
}
.shape-group {
  animation: shapeIn 0.55s var(--pop);
}

.cut-line {
  fill: none;
  stroke-width: 2.4; stroke-linecap: butt;
}
.cut-line.preview      { stroke: var(--pink); stroke-dasharray: 6 8; opacity: 0.7; }
.cut-line.preview.valid { stroke: var(--accent); opacity: 1; stroke-dasharray: none; stroke-width: 3; }

.cut-segment {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2.6;
  stroke-linecap: round;
  opacity: 0.95;
  filter: drop-shadow(0 0 5px rgba(192,132,252,0.45));
  pointer-events: none;
}
.cut-handle { pointer-events: none; }
.ch-halo {
  fill: var(--accent);
  fill-opacity: 0.14;
  stroke: var(--accent);
  stroke-width: 1.2;
  stroke-opacity: 0.55;
}
.ch-dot {
  fill: white;
  stroke: var(--accent);
  stroke-width: 2.2;
  filter: drop-shadow(0 0 6px rgba(192,132,252,0.55));
}
.ch-dot.locked {
  fill: var(--accent);
  stroke: white;
  stroke-width: 1.6;
}
.target-banner {
  color: var(--muted);
  font-family: ui-monospace, monospace;
  font-weight: 700;
  font-size: 0.95rem;
  letter-spacing: 0.5px;
}
.target-banner b { color: var(--accent); }
.cut-line.final {
  stroke: white; stroke-width: 2; opacity: 0;
  stroke-linecap: butt;
  filter: drop-shadow(0 0 8px rgba(255,255,255,0.65));
  animation: cutFlash 0.7s ease forwards;
}

@keyframes cutFlash {
  0%   { opacity: 0; stroke-width: 6; }
  30%  { opacity: 1; stroke-width: 4; }
  100% { opacity: 0.35; stroke-width: 1.5; }
}

.piece {
  transition: transform 0.95s var(--s-curve);
  will-change: transform;
}

.label-leader {
  stroke: var(--accent); stroke-width: 1.4;
  opacity: 0; transition: opacity 0.35s ease 0.55s;
}
.label-leader.show { opacity: 0.9; }

.label-dot {
  fill: var(--accent);
  opacity: 0; transition: opacity 0.3s ease 0.55s;
}
.label-dot.show { opacity: 1; }

.label-text {
  fill: white;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-weight: 800; font-size: 16px;
  text-anchor: middle; dominant-baseline: middle;
  opacity: 0; transition: opacity 0.4s ease 0.7s;
}
.label-text.show { opacity: 1; }

.score-line {
  margin-top: 12px; min-height: 80px; text-align: center;
  display: flex; flex-direction: column; align-items: center; gap: 6px;
}
.verdict {
  font-size: 1.4rem; font-weight: 900; letter-spacing: 1px;
  opacity: 0; transform: translateY(8px);
  transition: opacity 0.45s ease 0.85s, transform 0.5s var(--pop) 0.85s;
}
.verdict.show { opacity: 1; transform: translateY(0); }
.verdict.perfect  { color: var(--good); text-shadow: 0 0 16px rgba(74,222,128,0.55); }
.verdict.great    { color: var(--accent); text-shadow: 0 0 14px rgba(192,132,252,0.5); }
.verdict.good     { color: var(--pink); text-shadow: 0 0 12px rgba(244,114,182,0.45); }
.verdict.fair     { color: #fbbf24; }

.score-stats {
  font-family: ui-monospace, monospace; color: var(--muted);
  font-size: 0.95rem; font-weight: 600;
  opacity: 0; transition: opacity 0.4s ease 1s;
}
.score-stats.show, .score-stats.always { opacity: 1; }
.hint { color: var(--muted); font-size: 0.95rem; transition: color 0.25s; }

.actions {
  display: flex; gap: 12px; width: 100%; max-width: 460px;
  margin-top: 12px; margin-bottom: 6px; justify-content: center;
}
.btn {
  background-color: var(--accent-soft); border: none; color: white;
  border-radius: 20px; padding: 13px 0; width: 48%;
  font-weight: 700; font-size: 1rem; letter-spacing: 0.5px; text-align: center;
  box-shadow: 0 4px 0 var(--accent-deep);
  transition: all 0.2s; cursor: pointer; font-family: inherit;
}
.btn:hover { filter: brightness(1.08); }
.btn:active { transform: translateY(2px); box-shadow: 0 2px 0 var(--accent-deep); }
.btn.secondary { background-color: #4b5563; box-shadow: 0 4px 0 #374151; }
.btn.secondary:active { box-shadow: 0 2px 0 #374151; }
.btn.pulse { animation: btnPulse 1.4s ease-in-out infinite; }
@keyframes btnPulse {
  0%,100% { box-shadow: 0 4px 0 var(--accent-deep), 0 0 0 0 rgba(192,132,252,0.5); }
  50%     { box-shadow: 0 4px 0 var(--accent-deep), 0 0 0 10px rgba(192,132,252,0); }
}

.modal-back {
  position: fixed; inset: 0; background: transparent;
  display: none; align-items: center; justify-content: center; z-index: 100;
}
.modal-back.open { display: flex; }

body::before {
  content: '';
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.55);
  z-index: 99;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.22s ease;
}
body.modals-open::before { opacity: 1; }
.modal {
  background: var(--bg-inner); border: 1px solid #3a3340; border-radius: 14px;
  padding: 22px 22px 18px; width: min(380px, 86%);
  box-shadow: 0 20px 50px rgba(0,0,0,0.5);
  animation: fadeScaleIn 0.25s var(--pop);
}
.modal h2 { margin: 0 0 10px; font-size: 1.2rem; color: var(--accent); letter-spacing: 1px; }
.modal .stats-subtitle {
  margin: -4px 0 12px; font-size: 0.82rem; color: #9ca3af;
  letter-spacing: 0.6px; text-transform: uppercase;
}
.modal p, .modal li { color: #d1d5db; line-height: 1.5; font-size: 0.95rem; }
.modal ul { padding-left: 18px; margin: 6px 0 14px; }
.modal .close-row { display: flex; justify-content: flex-end; gap: 8px; }
.modal .btn { width: 110px; }

@keyframes fadeScaleIn { from { opacity: 0; transform: scale(0.92); } to { opacity: 1; transform: scale(1); } }

/* ---- inscribe mode ---- */
body[data-mode="cut"]      .help-inscribe,
body[data-mode="cut"]      .help-balance  { display: none; }
body[data-mode="inscribe"] .help-cut,
body[data-mode="inscribe"] .help-balance  { display: none; }
body[data-mode="balance"]  .help-cut,
body[data-mode="balance"]  .help-inscribe { display: none; }

body[data-mode="cut"] .help-cut > div { display: none; }
body[data-mode="cut"][data-cut-variation="half"]  .help-cut-half,
body[data-mode="cut"][data-cut-variation="ratio"] .help-cut-ratio,
body[data-mode="cut"][data-cut-variation="quad"]  .help-cut-quad,
body[data-mode="cut"][data-cut-variation="tri"]   .help-cut-tri,
body[data-mode="cut"][data-cut-variation="angle"] .help-cut-angle { display: block; }

body[data-mode="inscribe"] .help-inscribe > div { display: none; }
body[data-mode="inscribe"][data-inscribe-variation="square"]   .help-inscribe-square,
body[data-mode="inscribe"][data-inscribe-variation="triangle"] .help-inscribe-triangle { display: block; }

body[data-mode="balance"] .help-balance > div { display: none; }
body[data-mode="balance"][data-balance-variation="centroid"] .help-balance-centroid,
body[data-mode="balance"][data-balance-variation="pole"]     .help-balance-pole     { display: block; }

.math-note {
  font-size: 0.82rem;
  color: #9ca3af;
  border-top: 1px solid #3a3340;
  padding-top: 10px;
  margin-top: 10px;
  line-height: 1.45;
}
.math-note b { color: var(--accent); font-weight: 700; }
.math-note i { color: #d1d5db; font-style: italic; }

.inscribe-point { pointer-events: none; }
.sp-halo {
  fill: var(--accent);
  fill-opacity: 0.14;
  stroke: var(--accent);
  stroke-width: 1.2;
  stroke-opacity: 0.55;
}
.sp-dot {
  fill: white;
  stroke: var(--accent);
  stroke-width: 2.2;
  filter: drop-shadow(0 0 6px rgba(192,132,252,0.55));
}
.sp-hover {
  fill: var(--pink);
  fill-opacity: 0.9;
  stroke: white;
  stroke-width: 1.5;
  opacity: 0.85;
  pointer-events: none;
  filter: drop-shadow(0 0 6px rgba(244,114,182,0.55));
}

.inscribe-line {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2.4;
  stroke-linecap: round;
  opacity: 0.95;
  filter: drop-shadow(0 0 5px rgba(192,132,252,0.45));
  pointer-events: none;
}

.ideal-inscribe { opacity: 0; transition: opacity 0.5s ease; pointer-events: none; }
.ideal-inscribe.show { opacity: 1; }
.ideal-edge {
  fill: none;
  stroke: var(--good);
  stroke-width: 2;
  stroke-dasharray: 5 5;
  filter: drop-shadow(0 0 6px rgba(74,222,128,0.5));
}
.ideal-corner {
  fill: var(--good);
  filter: drop-shadow(0 0 4px rgba(74,222,128,0.7));
}

.mode-picker {
  display: flex; flex-direction: column; gap: 10px;
  margin: 6px 0 14px;
}
.mode-card, .var-card, .inscribe-var-card, .balance-var-card {
  text-align: left;
  background: #2a232f;
  border: 1.5px solid #3a3340;
  color: #e5e7eb;
  padding: 12px 14px;
  border-radius: 12px;
  cursor: pointer;
  transition: border-color 0.2s, transform 0.15s, background 0.2s;
  font-family: inherit;
}
.mode-card:hover, .var-card:hover, .inscribe-var-card:hover, .balance-var-card:hover { border-color: var(--accent); transform: translateY(-1px); }
.mode-card.active, .var-card.active, .inscribe-var-card.active, .balance-var-card.active {
  border-color: var(--accent);
  background: linear-gradient(180deg, #35233e 0%, #2a1f34 100%);
  box-shadow: 0 0 0 2px rgba(192,132,252,0.22);
}
.mode-title {
  font-weight: 800; letter-spacing: 1px;
  color: var(--accent); font-size: 0.95rem; margin-bottom: 3px;
}
.mode-desc { font-size: 0.88rem; color: #d1d5db; line-height: 1.35; }

/* ---- balance mode ---- */
.centroid-guess { pointer-events: none; }
.centroid-halo {
  fill: var(--pink);
  fill-opacity: 0.15;
  stroke: var(--pink);
  stroke-width: 1.2;
  stroke-opacity: 0.5;
  pointer-events: none;
}
.centroid-dot {
  fill: white;
  stroke: var(--pink);
  stroke-width: 2.2;
  pointer-events: none;
  filter: drop-shadow(0 0 6px rgba(244,114,182,0.6));
}
.centroid-hover {
  fill: var(--pink);
  fill-opacity: 0.75;
  stroke: white;
  stroke-width: 1.5;
  pointer-events: none;
  filter: drop-shadow(0 0 5px rgba(244,114,182,0.5));
}

.centroid-reveal { opacity: 0; transition: opacity 0.5s ease; pointer-events: none; }
.centroid-reveal.show { opacity: 1; }

.centroid-connector {
  fill: none;
  stroke: var(--good);
  stroke-width: 1.8;
  stroke-dasharray: 5 4;
  opacity: 0.75;
}
.centroid-arm {
  stroke: var(--good);
  stroke-width: 2.4;
  stroke-linecap: round;
  filter: drop-shadow(0 0 5px rgba(74,222,128,0.6));
}
.centroid-ring {
  fill: none;
  stroke: var(--good);
  stroke-width: 2;
  filter: drop-shadow(0 0 6px rgba(74,222,128,0.7));
}

.pole-group { pointer-events: none; }
.pole-shaft {
  fill: var(--accent);
  opacity: 0.92;
  filter: drop-shadow(0 0 6px rgba(192,132,252,0.55));
}
.pole-tip {
  fill: white;
  stroke: var(--accent);
  stroke-width: 2;
  filter: drop-shadow(0 0 5px rgba(192,132,252,0.65));
}
.pole-hover {
  stroke: var(--pink);
  stroke-width: 2;
  stroke-dasharray: 4 4;
  opacity: 0.7;
  pointer-events: none;
  filter: drop-shadow(0 0 4px rgba(244,114,182,0.5));
}

.modal-back.closing { display: flex; }
.modal-back.closing .modal { animation: fadeScaleOut 0.22s var(--s-curve) forwards; }
@keyframes fadeScaleOut { from { opacity: 1; transform: scale(1); } to { opacity: 0; transform: scale(0.94); } }
@keyframes shapeIn { from { opacity: 0; transform: scale(0.85); } to { opacity: 1; transform: scale(1); } }
