:root {
  --bg: #fff6ec;
  --card: #ffffff;
  --ink: #2b1b3d;
  --muted: #7b6c8c;
  --line: #f1e1d0;
  --idle: #b9aec4;
  --accent: #ff5a36;
  --grad: linear-gradient(135deg, #ff8a00 0%, #ff3d77 100%);
  --radius: 20px;
  --shadow: 0 6px 24px rgba(255, 90, 54, 0.1);
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", "Segoe UI", system-ui, sans-serif;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1024;
    --card: #2a1b3b;
    --ink: #fff3e6;
    --muted: #b9a8cc;
    --line: #3d2a54;
    --idle: #6f5f85;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-size: 16px;
  line-height: 1.35;
  -webkit-tap-highlight-color: transparent;
}

button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ------------------------------------------------------------ figure */

.fig { display: block; width: 100%; height: auto; overflow: hidden; }
.fig path { fill: none; stroke: currentColor; stroke-width: 7.5; stroke-linecap: round; stroke-linejoin: round; }
.fig-head { fill: currentColor; }
.fig-far { opacity: 0.45; }
.fig-front .fig-far { opacity: 1; }
.fig-ground { stroke: currentColor; stroke-width: 1.5; opacity: 0.22; }
.fig-prop path { stroke-width: 4; opacity: 0.4; }
.fig-prop .fig-wall { stroke-width: 6; }

/* ------------------------------------------------------------- setup */

#setup {
  max-width: 1080px;
  margin: 0 auto;
  padding: 20px 16px 120px;
}

.hero { padding: 12px 4px 20px; }
.hero h1 {
  margin: 0;
  font-size: clamp(2.2rem, 7vw, 3.4rem);
  font-weight: 900;
  letter-spacing: -0.03em;
  line-height: 1;
  background: var(--grad);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.hero p { margin: 8px 0 0; color: var(--muted); font-size: 1.05rem; }

.panel {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  margin-bottom: 16px;
}
.panel h2, .library-head h2 { margin: 0; font-size: 1.15rem; font-weight: 800; }
.hint { color: var(--muted); font-size: 0.9rem; margin: 6px 0 0; }

.steppers { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.stepper { background: var(--bg); border-radius: 16px; padding: 12px; text-align: center; }
.stepper label { display: block; font-size: 0.8rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.stepper-row { display: flex; align-items: center; justify-content: center; gap: 6px; margin-top: 6px; }
.stepper input {
  width: 3.2ch;
  min-width: 0;
  flex: 0 1 auto;
  border: 0;
  background: none;
  text-align: center;
  font-size: 2rem;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  -moz-appearance: textfield;
  appearance: textfield;
  padding: 0;
}
.stepper input::-webkit-outer-spin-button, .stepper input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.stepper .unit { color: var(--muted); font-weight: 700; font-size: 0.85rem; }
.round-btn {
  flex: none;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 0;
  background: var(--card);
  box-shadow: 0 2px 8px rgba(43, 27, 61, 0.12);
  font-size: 1.4rem;
  font-weight: 800;
  line-height: 1;
  color: var(--accent);
}
.round-btn:active { transform: scale(0.92); }
.summary { margin: 14px 0 0; font-weight: 700; text-align: center; }
.summary span { color: var(--accent); }

.option-row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; }
.option-row + .option-row { margin-top: 14px; padding-top: 14px; border-top: 1px solid var(--line); }
.option-label { font-weight: 800; min-width: 5.5em; }

.segmented { display: inline-flex; background: var(--bg); border-radius: 999px; padding: 4px; flex-wrap: wrap; }
.segmented button {
  border: 0;
  background: none;
  padding: 8px 16px;
  border-radius: 999px;
  font-weight: 700;
  color: var(--muted);
}
.segmented button[aria-pressed="true"] { background: var(--grad); color: #fff; box-shadow: 0 3px 10px rgba(255, 61, 119, 0.35); }

.mini-stepper { display: inline-flex; align-items: center; gap: 8px; font-weight: 700; }
.mini-stepper input { width: 3ch; text-align: center; border: 0; background: var(--bg); border-radius: 10px; padding: 6px 4px; font-weight: 800; -moz-appearance: textfield; appearance: textfield; }
.mini-stepper input::-webkit-outer-spin-button, .mini-stepper input::-webkit-inner-spin-button { -webkit-appearance: none; }
.mini-stepper .round-btn { width: 32px; height: 32px; font-size: 1.1rem; background: var(--bg); box-shadow: none; }

.toggle { display: inline-flex; align-items: center; gap: 8px; font-weight: 700; cursor: pointer; user-select: none; }
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.toggle .knob { width: 44px; height: 26px; border-radius: 999px; background: var(--idle); position: relative; transition: background 0.15s; flex: none; }
.toggle .knob::after { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; transition: transform 0.15s; }
.toggle input:checked + .knob { background: var(--accent); }
.toggle input:checked + .knob::after { transform: translateX(18px); }
.toggle input:focus-visible + .knob { outline: 3px solid var(--accent); outline-offset: 2px; }

/* order list */
.order-list { list-style: none; margin: 12px 0 0; padding: 0; display: grid; gap: 6px; }
.order-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg);
  border-radius: 12px;
  padding: 6px 8px 6px 12px;
  border-left: 5px solid var(--g);
}
.order-list li.dragging { opacity: 0.4; }
.order-list .num { font-weight: 900; color: var(--muted); width: 1.6em; font-variant-numeric: tabular-nums; }
.order-list .name { flex: 1; font-weight: 700; cursor: grab; }
.order-list button { border: 0; background: var(--card); width: 34px; height: 34px; border-radius: 10px; font-weight: 800; }
.order-list button:disabled { opacity: 0.3; cursor: default; }

/* library */
.library-head { display: flex; align-items: center; gap: 10px; margin: 24px 4px 4px; }
.library-head h2 { flex: 1; font-size: 1.3rem; }
.link-btn { border: 0; background: none; color: var(--accent); font-weight: 800; padding: 6px 8px; border-radius: 8px; }

.group { margin-top: 18px; }
.group-head { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; padding: 0 4px 10px; }
.group-check { display: inline-flex; align-items: center; gap: 10px; font-size: 1.15rem; font-weight: 900; color: var(--g); cursor: pointer; }
.group-check input { width: 22px; height: 22px; accent-color: var(--g); cursor: pointer; }
.group-count { color: var(--muted); font-weight: 700; font-size: 0.9rem; }
.group-weight { margin-left: auto; display: flex; align-items: center; gap: 8px; color: var(--muted); font-weight: 700; font-size: 0.9rem; }
.group-weight input { width: 130px; accent-color: var(--g); }
.group-weight output { min-width: 3.2em; text-align: right; color: var(--ink); font-variant-numeric: tabular-nums; }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); gap: 10px; }

.card {
  position: relative;
  background: var(--card);
  border: 2px solid var(--line);
  border-radius: 16px;
  color: var(--idle);
  transition: transform 0.12s, border-color 0.12s, background 0.12s;
}
.card-main { display: block; width: 100%; border: 0; background: none; padding: 8px 8px 10px; border-radius: 14px; color: inherit; }
.card-name { display: block; color: var(--muted); font-weight: 700; font-size: 0.86rem; line-height: 1.2; min-height: 2.4em; }
.card:hover { transform: translateY(-2px); }
.card.on { border-color: var(--g); background: color-mix(in srgb, var(--g) 10%, var(--card)); color: var(--g); box-shadow: 0 4px 14px color-mix(in srgb, var(--g) 22%, transparent); }
.card.on .card-name { color: var(--ink); }
.card-check, .card-pos, .card-share {
  position: absolute;
  top: 6px;
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 900;
  line-height: 22px;
  text-align: center;
  pointer-events: none;
}
.card-check { right: 6px; background: var(--g); color: #fff; display: none; }
.card.on .card-check { display: block; }
.card-pos, .card-share { left: 6px; background: var(--ink); color: var(--bg); display: none; }
.mode-order .card.on .card-pos { display: block; }
.mode-weighted .card.on .card-share { display: block; }
.card-chair { position: absolute; left: 6px; bottom: 44px; font-size: 0.8rem; opacity: 0.8; pointer-events: none; }
.card-weight { display: none; align-items: center; gap: 6px; padding: 0 8px 8px; }
.card-weight input { flex: 1; min-width: 0; accent-color: var(--g); }
.card-weight output { font-size: 0.8rem; font-weight: 800; color: var(--ink); min-width: 2.2em; text-align: right; font-variant-numeric: tabular-nums; }
.mode-weighted .card.on .card-weight { display: flex; }
.group-weight { display: none; }
.mode-weighted .group-weight { display: flex; }

/* start bar */
.startbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-top: 1px solid var(--line);
  z-index: 5;
}
.startbar-inner { max-width: 1080px; margin: 0 auto; display: flex; align-items: center; gap: 14px; }
.startbar-info { flex: 1; font-weight: 700; color: var(--muted); font-size: 0.95rem; }
.startbar-info.warn { color: #e11d48; }
.start-btn {
  border: 0;
  background: var(--grad);
  color: #fff;
  font-size: 1.25rem;
  font-weight: 900;
  letter-spacing: 0.02em;
  padding: 14px 38px;
  border-radius: 999px;
  box-shadow: 0 8px 22px rgba(255, 61, 119, 0.4);
  transition: transform 0.12s;
}
.start-btn:hover { transform: translateY(-2px) scale(1.02); }
.start-btn:active { transform: scale(0.97); }
.start-btn:disabled { filter: grayscale(1); opacity: 0.5; cursor: not-allowed; transform: none; }

/* --------------------------------------------------------------- run */

#run {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto auto;
  padding: calc(14px + env(safe-area-inset-top)) 18px calc(14px + env(safe-area-inset-bottom));
  background: var(--run-bg);
  color: var(--run-fg);
  overflow: hidden;
  --run-bg: linear-gradient(160deg, #ffd84d, #ffa51f);
  --run-fg: #3a1f0b;
  --run-soft: rgba(58, 31, 11, 0.16);
}
#run[data-phase="work"] { --run-bg: linear-gradient(160deg, #ff9500 0%, #ff3d6e 65%, #e0218a 100%); --run-fg: #fff; --run-soft: rgba(255, 255, 255, 0.26); }
#run[data-phase="rest"] { --run-bg: linear-gradient(160deg, #10c9a8, #2b8cf0); --run-fg: #fff; --run-soft: rgba(255, 255, 255, 0.26); }
#run[data-phase="done"] { --run-bg: linear-gradient(160deg, #7c3aed, #ff3d77); --run-fg: #fff; --run-soft: rgba(255, 255, 255, 0.26); }

.run-top { display: flex; align-items: baseline; justify-content: space-between; font-weight: 800; font-size: 1.05rem; font-variant-numeric: tabular-nums; }

.run-progress { display: flex; gap: 3px; margin-top: 10px; }
.run-progress i { flex: 1; height: 8px; border-radius: 4px; background: var(--run-soft); overflow: hidden; }
.run-progress i b { display: block; height: 100%; width: 0; background: var(--run-fg); border-radius: 4px; }

.run-body { display: flex; flex-direction: column; align-items: center; justify-content: space-evenly; min-height: 0; text-align: center; }
.run-stage { display: flex; flex-direction: column; align-items: center; min-height: 0; }
.run-phase { font-size: clamp(1rem, 3.4vw, 1.4rem); font-weight: 900; letter-spacing: 0.22em; text-transform: uppercase; opacity: 0.85; }
.run-fig { width: min(76vw, 40vh); }
.run-name { margin: 0; font-size: clamp(1.7rem, 7vw, 3.2rem); font-weight: 900; letter-spacing: -0.02em; line-height: 1.05; }
.run-cue { margin: 6px 0 0; font-size: clamp(0.95rem, 3vw, 1.2rem); font-weight: 600; opacity: 0.9; min-height: 1.4em; }

.run-clock { position: relative; width: min(50vw, 24vh); aspect-ratio: 1; flex: none; }
.run-clock svg { width: 100%; height: 100%; transform: rotate(-90deg); display: block; }
.run-clock circle { fill: none; stroke-width: 9; }
.run-clock .track { stroke: var(--run-soft); }
.run-clock .arc { stroke: var(--run-fg); stroke-linecap: round; transition: stroke-dashoffset 0.1s linear; }
.run-time {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: min(21vw, 10vh);
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.03em;
}
.run-time.pulse { animation: pulse 0.35s ease-out; }
@keyframes pulse { 0% { transform: scale(1.25); } 100% { transform: scale(1); } }

.run-next { display: flex; align-items: center; justify-content: center; gap: 10px; min-height: 72px; font-weight: 800; font-size: 1.05rem; }
.run-next .label { opacity: 0.75; font-weight: 700; }
.run-next-fig { width: 76px; flex: none; }
.run-cheer { margin: 0; text-align: center; font-weight: 800; font-size: 1.05rem; min-height: 1.5em; opacity: 0.95; }

.run-controls { display: flex; justify-content: center; align-items: center; gap: 14px; margin-top: 10px; }
.ctl {
  border: 0;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--run-soft);
  color: var(--run-fg);
  display: grid;
  place-items: center;
  transition: transform 0.1s;
}
.ctl svg { width: 26px; height: 26px; fill: currentColor; }
.ctl:active { transform: scale(0.9); }
.ctl.main { width: 76px; height: 76px; background: var(--run-fg); color: #ff3d6e; }
#run[data-phase="rest"] .ctl.main { color: #1e9fd0; }
#run[data-phase="ready"] .ctl.main { color: #ffc53a; }
.ctl.main svg { width: 34px; height: 34px; }
#run.paused .run-fig, #run.paused .run-clock { opacity: 0.55; }
#run:not(.paused) .icon-play, #run.paused .icon-pause { display: none; }

@media (min-aspect-ratio: 1/1) {
  .run-body { flex-direction: row; gap: 4vw; }
  .run-fig { width: min(40vw, 52vh); }
  .run-clock { width: min(30vw, 46vh); }
  .run-time { font-size: min(12vw, 19vh); }
  .run-name { font-size: clamp(1.6rem, 4vw, 3.2rem); }
}

/* end dialog + done */
.overlay { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(20, 8, 30, 0.55); z-index: 3; padding: 20px; }
.dialog { background: var(--card); color: var(--ink); border-radius: 22px; padding: 22px; width: min(360px, 100%); text-align: center; }
.dialog h3 { margin: 0 0 14px; font-size: 1.3rem; }
.dialog-actions { display: flex; gap: 10px; }
.btn { flex: 1; border: 0; border-radius: 999px; padding: 12px 16px; font-weight: 800; background: var(--bg); }
.btn.primary { background: var(--grad); color: #fff; }

.done { position: absolute; inset: 0; z-index: 2; background: var(--run-bg); display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; padding: 24px; text-align: center; overflow: hidden; }
.done h2 { margin: 0; font-size: clamp(2.2rem, 9vw, 4rem); font-weight: 900; letter-spacing: -0.03em; line-height: 1; }
.done p { margin: 0; font-size: 1.15rem; font-weight: 700; }
.done-stats { display: flex; gap: 12px; flex-wrap: wrap; justify-content: center; }
.done-stats div { background: var(--run-soft); border-radius: 16px; padding: 12px 18px; min-width: 96px; }
.done-stats b { display: block; font-size: 1.8rem; font-weight: 900; font-variant-numeric: tabular-nums; }
.done-stats span { font-size: 0.8rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.06em; opacity: 0.85; }
.done .dialog-actions { width: min(380px, 100%); margin-top: 8px; }
.done .btn { background: var(--run-soft); color: #fff; }
.done .btn.primary { background: #fff; color: #c026d3; }

.confetti { position: absolute; top: -20px; width: 10px; height: 16px; border-radius: 3px; animation: fall linear forwards; pointer-events: none; }
@keyframes fall { to { transform: translateY(110vh) rotate(720deg); } }

@media (max-width: 520px) {
  .steppers { grid-template-columns: 1fr; gap: 8px; }
  .stepper { display: flex; align-items: center; justify-content: space-between; padding: 8px 12px; }
  .stepper-row { margin-top: 0; }
  .start-btn { padding: 14px 28px; }
  .group-weight { margin-left: 0; width: 100%; }
  .group-weight input { flex: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .run-time.pulse, .confetti { animation: none; }
  .confetti { display: none; }
}
