:root {
  --bg: #f6f4f0;
  --surface: #ffffff;
  --ink: #1f2024;
  --muted: #71747c;
  --line: #e5e1da;
  --accent: #f26b4f;
  --accent-deep: #d9533a;
  --desk: #e9e6e0;
  --radius: 18px;
  --font: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  color-scheme: light;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
body[data-view="play"] { overflow: hidden; overscroll-behavior: none; }
button { font: inherit; color: inherit; cursor: pointer; }
a { color: inherit; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.noscript { padding: 24px; text-align: center; }
.icon { display: inline-flex; line-height: 0; }

/* ── buttons ──────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 12px 20px; border: 0; border-radius: 14px;
  background: var(--accent); color: #fff; font-weight: 650; font-size: 16px;
  box-shadow: 0 4px 14px rgba(242, 107, 79, 0.28);
  transition: transform .12s ease, background .15s ease;
}
.btn:active { transform: scale(.97); }
.btn:hover { background: var(--accent-deep); }
.btn:disabled { opacity: .6; }
.btn.ghost { background: var(--surface); color: var(--ink); box-shadow: inset 0 0 0 1.5px var(--line); }
.btn.ghost:hover { background: #fbfaf8; }
.btn.wide { width: 100%; margin-top: 10px; }
.icon-btn {
  display: inline-grid; place-items: center; width: 44px; height: 44px; flex: none;
  border: 0; border-radius: 12px; background: transparent;
}
.icon-btn:hover { background: rgba(0, 0, 0, 0.05); }
.icon-btn:active { background: rgba(0, 0, 0, 0.09); }
:focus-visible { outline: 3px solid rgba(242, 107, 79, 0.55); outline-offset: 2px; }

/* ── library ──────────────────────────────────────────────────────────── */
.lib {
  max-width: 1120px; margin: 0 auto;
  padding: calc(var(--safe-t) + 18px) calc(var(--safe-r) + 18px) calc(var(--safe-b) + 28px) calc(var(--safe-l) + 18px);
}
.lib-top { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand > div { min-width: 0; }
.brand h1 { margin: 0; font-size: 24px; letter-spacing: -0.01em; line-height: 1.1; white-space: nowrap; }
.brand .tag { margin: 2px 0 0; color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
@media (max-width: 420px) {
  .brand { gap: 10px; }
  .brand h1 { font-size: 21px; }
  .brand .tag { font-size: 13px; }
  .logo svg { width: 38px; height: 38px; }
  .create-btn { padding: 8px 13px; }
}
.logo { display: inline-flex; filter: drop-shadow(0 2px 6px rgba(0,0,0,.12)); }
.create-btn { min-height: 44px; padding: 10px 16px; }
.chips {
  position: sticky; top: 0; z-index: 5;
  display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none;
  margin: 0 calc(-18px - var(--safe-l)) 14px calc(-18px - var(--safe-r));
  padding: calc(var(--safe-t) * 0 + 8px) calc(18px + var(--safe-r)) 10px calc(18px + var(--safe-l));
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px);
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; border: 0; border-radius: 999px; padding: 9px 15px;
  background: var(--surface); box-shadow: inset 0 0 0 1.5px var(--line);
  font-weight: 600; font-size: 15px; color: #45474d;
}
.chip.on { background: var(--ink); color: #fff; box-shadow: none; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(158px, 1fr)); gap: 14px; }
@media (min-width: 720px) { .grid { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 18px; } }
.card {
  display: block; min-width: 0; text-decoration: none; background: var(--surface); border-radius: var(--radius);
  padding: 8px 8px 10px; box-shadow: 0 1px 2px rgba(0,0,0,.05), 0 6px 18px rgba(60, 50, 30, 0.07);
  transition: transform .15s ease, box-shadow .15s ease;
}
.card:hover { transform: translateY(-2px); box-shadow: 0 2px 4px rgba(0,0,0,.06), 0 12px 26px rgba(60, 50, 30, 0.12); }
.card:active { transform: scale(.98); }
.thumb {
  position: relative; aspect-ratio: 1; border-radius: 12px; overflow: hidden; background: #fbfaf8;
  display: grid; place-items: center;
}
.thumb:not(.ready):not(.create-thumb)::after {
  content: ""; width: 26px; height: 26px; border-radius: 50%;
  border: 3px solid #e7e3dc; border-top-color: #c9c3b8; animation: spin 0.9s linear infinite;
}
.thumb canvas { width: 100%; height: 100%; display: block; }
.thumb.broken::after { animation: none; border: 0; content: "!"; color: var(--muted); }
.card-meta { display: flex; align-items: center; justify-content: space-between; gap: 6px; margin: 9px 4px 0; }
.card-title { font-weight: 650; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-sub {
  margin: 1px 4px 0; font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  /* Contribute no intrinsic width: without this a long nowrap line widened its grid card. */
  width: 0; min-width: calc(100% - 8px);
}
.badge {
  flex: none; font-size: 12px; font-weight: 700; padding: 3px 8px; border-radius: 999px;
  background: #fff1ed; color: var(--accent-deep);
}
.badge.done { background: #e8f6ee; color: #1f8a4c; padding: 3px 5px; }
.card.create .create-thumb {
  gap: 8px; align-content: center; justify-items: center; text-align: center; color: var(--accent-deep);
  background: repeating-linear-gradient(135deg, #fff6f3 0 12px, #fff 12px 24px);
  box-shadow: inset 0 0 0 2px #f8c7bb; font-weight: 650; font-size: 14px; padding: 12px;
}
.hint-card { grid-column: 1 / -1; color: var(--muted); padding: 12px 4px; }
.lib-foot { margin-top: 28px; text-align: center; color: var(--muted); font-size: 13px; }

/* ── play ─────────────────────────────────────────────────────────────── */
.play {
  position: fixed; inset: 0; display: grid; grid-template-rows: auto 1fr auto auto;
  background: var(--desk); user-select: none; -webkit-user-select: none;
}
.play-top {
  display: flex; align-items: center; gap: 4px;
  padding: calc(var(--safe-t) + 6px) calc(var(--safe-r) + 8px) 6px calc(var(--safe-l) + 6px);
  background: rgba(255, 255, 255, 0.92); border-bottom: 1px solid var(--line);
  -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px); z-index: 2;
}
.play-head { flex: 1; min-width: 0; padding: 0 4px; }
.play-title { font-weight: 700; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.play-pct { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.play-stage { position: relative; overflow: hidden; min-height: 0; }
.play-canvas { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; display: block; cursor: grab; }
.play-canvas:active { cursor: grabbing; }
.bar { height: 4px; background: #dcd8d1; overflow: hidden; }
.bar-fill { height: 100%; background: var(--accent); transform-origin: 0 50%; transform: scaleX(0); transition: transform .35s ease; }
.bar.big { height: 8px; border-radius: 999px; width: min(320px, 80%); margin: 14px auto 0; }
.palette {
  display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; overscroll-behavior-x: contain;
  padding: 10px calc(var(--safe-r) + 14px) calc(var(--safe-b) + 12px) calc(var(--safe-l) + 14px);
  background: var(--surface); scroll-padding: 0 40px;
}
.palette::-webkit-scrollbar { display: none; }
/* Centre the strip when it fits; auto margins (unlike justify-content) never clip an overflow. */
.palette > :first-child { margin-left: auto; }
.palette > :last-child { margin-right: auto; }
.swatch {
  position: relative; flex: none; width: 60px; height: 60px; padding: 0; border: 0; border-radius: 50%;
  background: transparent; transition: transform .15s ease, opacity .2s ease;
}
.sw-ring {
  position: absolute; inset: 0; border-radius: 50%;
  background: conic-gradient(var(--accent) calc(var(--p, 0) * 360deg), #ebe7e1 0);
  -webkit-mask: radial-gradient(farthest-side, transparent calc(100% - 4px), #000 calc(100% - 3.5px));
  mask: radial-gradient(farthest-side, transparent calc(100% - 4px), #000 calc(100% - 3.5px));
}
.sw-dot {
  position: absolute; inset: 6px; border-radius: 50%; background: var(--c); color: var(--num);
  display: grid; place-items: center; box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.1);
  transition: transform .15s ease, box-shadow .15s ease;
}
.sw-num { font-weight: 750; font-size: 17px; color: var(--num); font-variant-numeric: tabular-nums; }
.sw-dot .icon { display: none; color: var(--num); }
.swatch.sel .sw-dot { transform: scale(1.06); box-shadow: inset 0 0 0 1px rgba(0,0,0,.1), 0 0 0 2.5px #fff, 0 0 0 5px var(--ink); }
.swatch.sel .sw-ring { opacity: 0; }
.swatch.done { opacity: 0.5; }
.swatch.done .sw-num { display: none; }
.swatch.done .sw-dot .icon { display: inline-flex; }
.swatch.pop { animation: pop .45s ease; }
@keyframes pop { 30% { transform: scale(1.25); } 60% { transform: scale(0.94); } }
.swatch:active .sw-dot { transform: scale(0.94); }

.spinner {
  position: absolute; left: 50%; top: 50%; width: 36px; height: 36px; margin: -18px 0 0 -18px;
  border-radius: 50%; border: 4px solid rgba(0,0,0,.08); border-top-color: var(--accent); animation: spin .9s linear infinite;
}
.spinner.inline { position: static; margin: 40px auto 0; display: block; }
@keyframes spin { to { transform: rotate(360deg); } }
.empty { position: relative; max-width: 420px; margin: 60px auto; padding: 0 24px; text-align: center; }

.done-card {
  position: fixed; left: 50%; bottom: calc(var(--safe-b) + 18px); z-index: 20;
  width: min(440px, calc(100% - 28px)); transform: translate(-50%, 140%);
  background: var(--surface); border-radius: 24px; padding: 18px 18px 16px; text-align: center;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.22); transition: transform .45s cubic-bezier(.2, .9, .25, 1.15);
}
.done-card.show { transform: translate(-50%, 0); }
.done-title { font-size: 24px; font-weight: 800; letter-spacing: -0.01em; }
.done-close { position: absolute; top: 6px; right: 6px; color: var(--muted); }
.done-sub { color: var(--muted); font-size: 14px; margin-top: 2px; }
.done-actions { display: flex; gap: 8px; justify-content: center; margin-top: 14px; flex-wrap: wrap; }
.done-actions .btn { flex: 1 1 110px; padding: 10px 12px; }
.coach {
  position: absolute; left: 50%; bottom: 14px; z-index: 3; width: min(360px, calc(100% - 28px));
  transform: translate(-50%, 12px); opacity: 0; transition: opacity .25s ease, transform .25s ease;
  background: rgba(28, 28, 32, 0.9); color: #fff; border-radius: 16px; padding: 12px 16px;
  font-weight: 600; font-size: 15px; text-align: center; pointer-events: none;
}
.coach.show { opacity: 1; transform: translate(-50%, 0); }
.confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 19; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--safe-b) + 104px); z-index: 30;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: rgba(28, 28, 32, 0.92); color: #fff; padding: 10px 16px; border-radius: 999px;
  font-weight: 600; font-size: 14.5px; transition: opacity .2s ease, transform .2s ease; white-space: nowrap;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

.sheet-back {
  position: fixed; inset: 0; z-index: 40; display: flex; align-items: flex-end; justify-content: center;
  background: rgba(20, 20, 24, 0); transition: background .2s ease;
}
.sheet-back.show { background: rgba(20, 20, 24, 0.32); }
.sheet {
  width: min(480px, 100%); background: var(--surface); border-radius: 22px 22px 0 0;
  padding: 10px 10px calc(var(--safe-b) + 10px); transform: translateY(100%); transition: transform .25s ease;
}
@media (min-width: 560px) { .sheet { border-radius: 22px; margin-bottom: 24px; } }
.sheet-back.show .sheet { transform: none; }
.sheet-title { font-weight: 700; padding: 10px 12px 2px; }
.sheet-msg { color: var(--muted); padding: 2px 12px 8px; font-size: 15px; }
.sheet-item {
  display: flex; align-items: center; gap: 12px; width: 100%; min-height: 52px; padding: 0 14px;
  border: 0; background: transparent; border-radius: 14px; text-align: left; font-size: 16.5px; font-weight: 550;
}
.sheet-item:hover { background: #f5f3ef; }
.sheet-item.danger { color: #d93b3b; }
.sheet-item.cancel { justify-content: center; font-weight: 700; margin-top: 4px; background: #f3f1ed; }

/* ── create ───────────────────────────────────────────────────────────── */
.create { max-width: 640px; margin: 0 auto; padding: calc(var(--safe-t) + 10px) calc(var(--safe-r) + 16px) calc(var(--safe-b) + 32px) calc(var(--safe-l) + 16px); }
.create-top { display: flex; align-items: center; gap: 6px; margin: 0 0 12px -8px; }
.create-top h1 { margin: 0; font-size: 22px; }
.drop {
  display: grid; justify-items: center; gap: 6px; padding: 48px 20px; border-radius: 22px; cursor: pointer;
  background: var(--surface); border: 2.5px dashed #e8b9ad; color: var(--accent-deep); text-align: center;
  transition: background .15s ease, border-color .15s ease;
}
.drop strong { font-size: 20px; color: var(--ink); }
.drop.over, .drop:hover { background: #fff6f3; border-color: var(--accent); }
.link-btn {
  display: flex; align-items: center; justify-content: center; gap: 8px; margin: 14px auto 0; padding: 10px 14px;
  width: fit-content; border-radius: 12px; color: var(--accent-deep); font-weight: 650; cursor: pointer;
}
.link-btn:hover { background: #fff1ed; }
.tips { color: var(--muted); padding-left: 20px; margin: 18px 4px; line-height: 1.55; }
.crop-wrap { display: grid; justify-items: center; }
.crop-frame {
  position: relative; border-radius: 16px; overflow: hidden; background: #2a2a2e; touch-action: none; cursor: move;
  box-shadow: 0 8px 26px rgba(0,0,0,.16);
}
.crop-canvas { width: 100%; height: 100%; display: block; }
.row { display: flex; gap: 10px; align-items: center; margin-top: 14px; }
.row.center { justify-content: center; }
.zoom-row { color: var(--muted); }
.zoom { flex: 1; }
.seg { display: flex; flex: 1; background: #ebe7e1; border-radius: 13px; padding: 3px; gap: 2px; }
.seg button {
  flex: 1; border: 0; background: transparent; border-radius: 10px; padding: 9px 6px; font-weight: 650; font-size: 14.5px; color: #55585f;
}
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.field { margin-top: 18px; }
.label { font-weight: 700; margin-bottom: 8px; display: flex; justify-content: space-between; }
.label output { color: var(--muted); font-variant-numeric: tabular-nums; }
.field input[type="range"] { width: 100%; accent-color: var(--accent); }
.zoom { accent-color: var(--accent); }
.text {
  width: 100%; font: inherit; padding: 12px 14px; border-radius: 12px; border: 1.5px solid var(--line); background: var(--surface);
}
.text:focus { outline: none; border-color: var(--accent); }
.converting { text-align: center; padding: 30px 0; }
.conv-label { margin-top: 14px; font-weight: 600; }
.preview-box { display: grid; place-items: center; min-height: 200px; }
.preview { border-radius: 14px; box-shadow: 0 8px 26px rgba(0,0,0,.14); background: #fff; }
.stats { text-align: center; color: var(--muted); margin: 10px 0 4px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
