:root {
  --bg: #e9ebee;
  --surface: #ffffff;
  --raised: #ffffff;
  --sunken: #eceef1;
  --ink: #1b1f24;
  --ink-2: #57606a;
  --ink-3: #68717b;
  --line: #dfe3e8;
  --line-2: #c4cad1;
  --accent: #2a66c8;
  --accent-soft: #e6eefb;
  --on-accent: #ffffff;
  --press: #1f52a6;
  --lift: 0 1px 2px rgba(27, 31, 36, 0.16);
  --sans: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --panel: 372px;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121315;
  --surface: #1d1f22;
  --raised: #3d434b;
  --sunken: #2a2d31;
  --ink: #e8eaed;
  --ink-2: #aab1b9;
  --ink-3: #8b939d;
  --line: #2f3338;
  --line-2: #474d55;
  --accent: #7aaef0;
  --accent-soft: #1d2f4a;
  --on-accent: #10161f;
  --press: #9cc2f5;
  --lift: 0 1px 2px rgba(0, 0, 0, 0.5);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, p { margin: 0; }
a { color: inherit; }
button, input, select { font: inherit; color: inherit; }
canvas, svg { display: block; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ---------- header ---------- */

.site-head {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 52px;
  padding: 0 16px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  font-weight: 600;
  font-size: 1.05rem;
  letter-spacing: -0.01em;
  white-space: nowrap;
}
.brand-mark { width: 24px; height: 24px; }

.head-side { display: flex; align-items: center; gap: 8px; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: none;
  color: var(--ink-2);
  cursor: pointer;
}
.icon-btn:hover:not(:disabled) { color: var(--ink); border-color: var(--line-2); }
.icon-btn:disabled { opacity: 0.4; cursor: default; }
.icon-sun, :root[data-theme="dark"] .icon-moon { display: none; }
:root[data-theme="dark"] .icon-sun { display: block; }

.back-link {
  margin-left: 8px;
  color: var(--ink-2);
  font-size: 0.85rem;
  text-decoration: none;
  white-space: nowrap;
}
.back-link:hover { color: var(--ink); text-decoration: underline; text-underline-offset: 3px; }

/* ---------- layout ---------- */

.app {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--panel);
}

.work { display: flex; flex-direction: column; min-width: 0; min-height: 0; }

.toolbar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.toolbar-status {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  color: var(--ink-2);
  font-size: 0.82rem;
  font-variant-numeric: tabular-nums;
  text-align: right;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stage {
  flex: 1;
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}
.frame { position: relative; }
#view { width: 100%; height: 100%; box-shadow: 0 0 0 1px var(--line-2), 0 6px 24px rgba(0, 0, 0, 0.14); }
/* Reaches 14px past the picture (EDGE in app.js) so points on its edges can be grabbed. */
.overlay {
  position: absolute;
  inset: -14px;
  width: calc(100% + 28px);
  height: calc(100% + 28px);
  touch-action: none;
  cursor: crosshair;
  transition: opacity 0.15s;
}
/* The lines step aside while the pointer is on any control that isn't to do with the curves. */
.app:has(.panel:hover) .overlay { opacity: 0; }
.app:has(#curves-group:hover) .overlay { opacity: 1; }

.stage-msg {
  max-width: 420px;
  padding: 24px;
  color: var(--ink-2);
  text-align: center;
  white-space: pre-line;
}

.panel {
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  border-left: 1px solid var(--line);
  background: var(--surface);
}

.tabs { position: sticky; top: 0; z-index: 2; padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--surface); }

.lede { padding: 14px 16px; color: var(--ink-2); font-size: 0.9rem; }
#anim-top { padding-top: 0; }
#tab-design > .lede + .group, #anim-groups { border-top: 0; }

/* ---------- groups ---------- */

.group { border-top: 1px solid var(--line); }
.group > summary, .group-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 11px 16px;
  border: 0;
  background: none;
  font-weight: 600;
  font-size: 0.92rem;
  text-align: left;
  list-style: none;
  cursor: pointer;
  user-select: none;
}
.group > summary::-webkit-details-marker { display: none; }
.group > summary::after, .group-title::after {
  content: "";
  width: 7px;
  height: 7px;
  margin-right: 3px;
  border-right: 2px solid var(--ink-3);
  border-bottom: 2px solid var(--ink-3);
  transform: rotate(-45deg);
}
.group[open] > summary::after, .group-title[aria-expanded="true"]::after { transform: rotate(45deg) translate(-2px, -2px); }
.group > summary:focus-visible, .group-title:focus-visible { outline-offset: -2px; }

.group-head { position: relative; }
.switch { position: absolute; top: 50%; right: 46px; width: 34px; height: 20px; margin-top: -10px; cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.switch span {
  display: block;
  height: 100%;
  border-radius: 10px;
  background: var(--line-2);
  transition: background 0.12s;
}
.switch span::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  transition: transform 0.12s;
}
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(14px); }
.switch input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.group-body { display: grid; gap: 14px; padding: 2px 16px 18px; }

.sub {
  margin-top: 6px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
  font-size: 0.85rem;
  font-weight: 600;
}

.note { color: var(--ink-2); font-size: 0.8rem; }
.note:empty { display: none; }

.row { display: flex; align-items: center; gap: 8px; }
.row > input[type="text"] { flex: 1; min-width: 0; }
.links { gap: 18px; flex-wrap: wrap; }
.pair { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 10px; }

/* ---------- rows: label on the left, control on the right ---------- */

.lines { display: grid; gap: 10px; }
.line { display: grid; grid-template-columns: 82px minmax(0, 1fr) 52px; align-items: center; gap: 10px; min-width: 0; }
.line > :nth-child(2):last-child, .line.wide > :nth-child(2) { grid-column: 2 / -1; }
.line-label { color: var(--ink-2); font-size: 0.82rem; }
.line .num { height: 30px; padding: 0 4px; font-size: 0.82rem; font-variant-numeric: tabular-nums; text-align: center; -moz-appearance: textfield; appearance: textfield; }
.line .num::-webkit-outer-spin-button, .line .num::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.line select, .line input[type="text"] { height: 32px; }
.line input[type="color"] { width: 32px; height: 32px; }

.stepper { display: grid; grid-template-columns: 26px minmax(0, 1fr) 26px; gap: 4px; }
.step {
  height: 32px;
  padding: 0 0 2px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: none;
  color: var(--ink-2);
  font-size: 1.05rem;
  line-height: 1;
  cursor: pointer;
}
.step:hover { color: var(--ink); border-color: var(--line-2); }

/* ---------- fields ---------- */

.field { display: grid; gap: 5px; min-width: 0; }
.field > label {
  color: var(--ink-2);
  font-size: 0.8rem;
  font-weight: 500;
}

input[type="text"], input[type="number"], select {
  width: 100%;
  height: 36px;
  padding: 0 10px;
  border: 1px solid var(--line-2);
  border-radius: 6px;
  background-color: var(--surface);
  font-size: 0.88rem;
}
input[type="text"]:hover, input[type="number"]:hover, select:hover { border-color: var(--ink-3); }
input:disabled, select:disabled { opacity: 0.5; }
input::placeholder { color: var(--ink-3); }

input[type="range"] { width: 100%; height: 20px; margin: 0; accent-color: var(--accent); cursor: pointer; }

input[type="range"].hue { -webkit-appearance: none; appearance: none; background: none; }
input[type="range"].hue::-webkit-slider-runnable-track { height: 10px; border-radius: 5px; background: var(--track); }
input[type="range"].hue::-moz-range-track { height: 10px; border-radius: 5px; background: var(--track); }
input[type="range"].hue::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  margin-top: -4px;
  border: 2px solid var(--ink);
  border-radius: 50%;
  background: var(--surface);
}
input[type="range"].hue::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border: 2px solid var(--ink);
  border-radius: 50%;
  background: var(--surface);
}

input[type="color"] {
  flex: none;
  width: 36px;
  height: 36px;
  padding: 2px;
  border: 1px solid var(--line-2);
  border-radius: 6px;
  background: var(--surface);
  cursor: pointer;
}
.colour-row { display: flex; gap: 6px; }
.colour-row input[type="text"] { font-variant-numeric: tabular-nums; }

.check { display: flex; align-items: center; gap: 9px; font-size: 0.88rem; cursor: pointer; }
.check input { flex: none; width: 16px; height: 16px; margin: 0; accent-color: var(--accent); }

.seg {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: minmax(0, 1fr);
  gap: 2px;
  padding: 3px;
  border-radius: 7px;
  background: var(--sunken);
}
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span {
  display: block;
  padding: 6px 8px;
  border-radius: 5px;
  color: var(--ink-2);
  font-size: 0.84rem;
  font-weight: 500;
  text-align: center;
  cursor: pointer;
}
.seg span:hover { color: var(--ink); }
.seg input:checked + span { background: var(--raised); color: var(--ink); box-shadow: var(--lift); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: 36px;
  padding: 0 14px;
  border: 1px solid var(--line-2);
  border-radius: 6px;
  background: var(--surface);
  font-size: 0.88rem;
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: var(--ink-3); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn-primary { border-color: var(--accent); background: var(--accent); color: var(--on-accent); }
.btn-primary:hover:not(:disabled) { border-color: var(--press); background: var(--press); }

.link-btn {
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent);
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
}
.link-btn:hover:not(:disabled) { text-decoration: underline; text-underline-offset: 3px; }
.link-btn:disabled { color: var(--ink-3); opacity: 0.6; cursor: default; }

/* ---------- presets ---------- */

.thumbs { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px 8px; }
.thumbs:empty { display: none; }
.thumb-wrap { position: relative; min-width: 0; }
.thumb {
  display: grid;
  gap: 4px;
  width: 100%;
  min-width: 0;
  padding: 0;
  border: 0;
  background: none;
  text-align: left;
  cursor: pointer;
}
.thumb canvas { width: 100%; height: auto; border-radius: 5px; box-shadow: 0 0 0 1px var(--line-2); }
.thumb:hover canvas { box-shadow: 0 0 0 2px var(--ink-3); }
.thumb span {
  overflow: hidden;
  color: var(--ink-2);
  font-size: 0.76rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.thumb-del {
  position: absolute;
  top: 4px;
  right: 4px;
  width: 22px;
  height: 22px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 15px;
  line-height: 20px;
  cursor: pointer;
}

/* ---------- colour bar ---------- */

.bar { position: relative; padding: 0 9px 24px; }
.bar-track {
  width: 100%;
  height: 34px;
  border-radius: 5px;
  box-shadow: 0 0 0 1px var(--line-2);
  cursor: copy;
}
.bar-stops { position: absolute; left: 9px; right: 9px; top: 0; bottom: 0; pointer-events: none; }
.stop {
  position: absolute;
  top: 28px;
  width: 18px;
  height: 18px;
  margin-left: -9px;
  padding: 0;
  border: 2px solid var(--surface);
  border-radius: 4px;
  background: var(--c);
  box-shadow: 0 0 0 1px var(--ink-3);
  pointer-events: auto;
  touch-action: none;
  cursor: ew-resize;
}
.stop[aria-pressed="true"] { z-index: 1; box-shadow: 0 0 0 2px var(--accent); }
.touch .bar { padding-bottom: 32px; }
.touch .stop { width: 26px; height: 26px; margin-left: -13px; }

.stop-edit { display: grid; grid-template-columns: minmax(0, 1.5fr) minmax(0, 1fr) auto; align-items: end; gap: 10px; }

/* ---------- curve ---------- */

.curve {
  width: 100%;
  height: 240px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  touch-action: none;
  cursor: crosshair;
}

/* ---------- phones and tablets: bigger things to tap ---------- */

.touch input[type="text"], .touch input[type="number"], .touch select, .touch .btn, .touch input[type="color"], .touch .step { height: 42px; }
.touch input[type="color"] { width: 42px; }
.touch .line { grid-template-columns: 76px minmax(0, 1fr) 56px; }
.touch .stepper { grid-template-columns: 38px minmax(0, 1fr) 38px; }
.touch .toolbar .btn { height: 38px; padding: 0 12px; }
.touch input[type="range"] { height: 30px; }
.touch .link-btn { padding: 8px 0; }
.touch .check { min-height: 30px; }
.touch .thumb-del { width: 28px; height: 28px; font-size: 18px; line-height: 26px; }

/* ---------- narrow screens: preview pinned on top, controls scroll underneath ---------- */

@media (max-width: 860px) {
  body { height: auto; min-height: 100vh; display: block; overflow: visible; }
  .app { display: block; }
  .work { position: sticky; top: 0; z-index: 5; border-bottom: 1px solid var(--line); background: var(--bg); }
  .stage { flex: none; height: 36vh; min-height: 180px; }
  .toolbar { flex-wrap: wrap; gap: 6px; padding: 6px 10px; }
  .toolbar .btn, .touch .toolbar .btn { padding: 0 9px; font-size: 0.82rem; }
  .toolbar-status { order: 9; flex-basis: 100%; font-size: 0.78rem; text-align: left; }
  .toolbar-status.idle { display: none; }
  #save-btn { margin-left: auto; }
  .panel { overflow: visible; border-left: 0; }
  .tabs { position: static; }
  .back-link { margin-left: 4px; }
}
