/* ==========================================================================
   Kaleidoscope — UI shell
   Full-bleed fractal, floating dark-glass UI.
   ========================================================================== */

:root {
  --bg: #07080e;
  --glass: rgba(13, 17, 28, 0.72);
  --glass-strong: rgba(13, 17, 28, 0.88);
  --border: rgba(255, 255, 255, 0.08);
  --border-hi: rgba(255, 255, 255, 0.18);
  --text: #f1f5f9;
  --text-dim: #94a3b8;
  --text-faint: #64748b;
  /* One violet accent (the original Kaleidoscope scheme). */
  --violet: #a855f7;
  --violet-hi: #d8b4fe;
  --violet-deep: #7c3aed;
  --glow: rgba(168, 85, 247, 0.4);
  --success: #10b981;
  --accent-grad: linear-gradient(135deg, var(--violet), var(--violet-deep));
  --field: rgba(255, 255, 255, 0.055);
  --field-hover: rgba(255, 255, 255, 0.09);
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 18px 50px -12px rgba(0, 0, 0, 0.65), 0 2px 8px rgba(0, 0, 0, 0.35);
  --font: "Outfit", "Hiragino Sans", "Noto Sans JP", system-ui, -apple-system, sans-serif;
  --font-num: "Space Grotesk", "Outfit", "Hiragino Sans", "Noto Sans JP", ui-monospace, sans-serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --panel-w: 330px;
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  text-size-adjust: 100%;
}

body { position: relative; }

[hidden] { display: none !important; }
.hidden { display: none !important; }

button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }

:focus { outline: none; }
:focus-visible {
  outline: 2px solid var(--violet-hi);
  outline-offset: 2px;
  border-radius: 6px;
}

/* --------------------------------------------------------------------------
   Glass
   -------------------------------------------------------------------------- */
.glass {
  background: var(--glass);
  -webkit-backdrop-filter: blur(22px) saturate(1.5);
  backdrop-filter: blur(22px) saturate(1.5);
  border: 1px solid var(--border);
  box-shadow: var(--shadow), inset 0 1px 0 rgba(255, 255, 255, 0.05);
}

/* --------------------------------------------------------------------------
   Stage
   -------------------------------------------------------------------------- */
#stage {
  position: fixed;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  cursor: grab;
  touch-action: none;
  /* placeholder until the first frame is drawn */
  background:
    radial-gradient(60% 55% at 50% 45%, rgba(168, 85, 247, 0.2), transparent 70%),
    radial-gradient(40% 40% at 70% 65%, rgba(124, 58, 237, 0.12), transparent 70%),
    #05060b;
}
#stage.dragging { cursor: grabbing; }
#stage canvas {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

/* --------------------------------------------------------------------------
   Julia picture-in-picture
   -------------------------------------------------------------------------- */
#pip {
  position: absolute;
  z-index: 5;
  display: none;
  pointer-events: none;
  background: transparent;
  border-radius: 14px;
  border: 1.5px solid rgba(255, 255, 255, 0.55);
  box-shadow:
    0 0 0 1px rgba(0, 0, 0, 0.5),
    0 16px 40px -8px rgba(0, 0, 0, 0.7),
    0 0 36px -6px rgba(139, 124, 255, 0.55);
  overflow: hidden;
}
#pip.visible { display: block; animation: pip-in 0.22s ease-out; }
.pip-label {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 16px 8px 6px;
  text-align: center;
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.04em;
  color: #fff;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
  background: linear-gradient(to top, rgba(5, 5, 12, 0.78), transparent);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
@keyframes pip-in {
  from { opacity: 0; transform: scale(0.94); }
  to { opacity: 1; transform: scale(1); }
}

/* --------------------------------------------------------------------------
   Brand + HUD
   -------------------------------------------------------------------------- */
.topleft {
  position: fixed;
  z-index: 10;
  top: calc(18px + var(--safe-t));
  left: calc(20px + var(--safe-l));
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-width: calc(100vw - 100px);
  pointer-events: none;
}

.brand h1 {
  margin: 0;
  font-size: 26px;
  font-weight: 800;
  letter-spacing: 0.34em;
  line-height: 1.1;
  background: linear-gradient(135deg, #ffffff 30%, var(--violet-hi) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  color: transparent;
  filter: drop-shadow(0 2px 14px rgba(0, 0, 0, 0.55)) drop-shadow(0 4px 20px rgba(168, 85, 247, 0.18));
  user-select: none;
}
.brand .subtitle {
  margin: 5px 0 0;
  font-size: 10.5px;
  font-weight: 400;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--text-dim);
  text-shadow: 0 1px 8px rgba(0, 0, 0, 0.9);
}

#hud {
  width: 300px;
  max-width: 320px;
  padding: 10px 14px 12px;
  border-radius: 14px;
  font-family: var(--font-num);
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  display: grid;
  gap: 4px;
}
.hud-row {
  display: grid;
  grid-template-columns: 72px 1fr;
  gap: 10px;
  align-items: baseline;
  min-width: 0;
}
.hud-label {
  font-family: var(--font);
  font-size: 10.5px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-faint);
}
.hud-val {
  min-width: 0;
  text-align: right;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.hud-val-wrap {
  white-space: normal;
  word-break: break-all;
  overflow: visible;
  font-size: 11px;
  line-height: 1.3;
  color: var(--text-dim);
}

.track {
  height: 3px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.09);
  overflow: hidden;
}
.track-bar {
  height: 100%;
  width: 0;
  border-radius: 3px;
  background: var(--accent-grad);
  box-shadow: 0 0 10px var(--glow);
  transition: width 0.12s linear;
}
.hud-track { margin-top: 6px; }

/* --------------------------------------------------------------------------
   Buttons
   -------------------------------------------------------------------------- */
.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 50%;
  color: var(--text);
  transition: transform 0.18s, background 0.18s, border-color 0.18s;
}
.icon-btn:hover { border-color: var(--border-hi); transform: scale(1.06); }
.icon-btn:active { transform: scale(0.96); }
.icon-btn.small {
  width: 30px;
  height: 30px;
  font-size: 15px;
  font-weight: 600;
  background: var(--field);
  border: 1px solid var(--border);
}
.icon-btn.small:hover { background: var(--field-hover); transform: none; }

#btn-panel {
  position: fixed;
  z-index: 30;
  top: calc(16px + var(--safe-t));
  right: calc(16px + var(--safe-r));
}

.text-btn {
  height: 30px;
  padding: 0 12px;
  border-radius: 15px;
  border: 1px solid var(--border);
  background: var(--field);
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0.02em;
  color: var(--text-dim);
  transition: background 0.18s, color 0.18s;
}
.text-btn:hover { background: var(--field-hover); color: var(--text); text-decoration: none; }
a.text-btn { display: inline-flex; align-items: center; text-decoration: none; }

.learn-link {
  display: inline-block;
  margin-top: 10px;
  font-size: 12.5px;
  font-weight: 500;
  color: var(--violet-hi);
  text-decoration: none;
}
.learn-link:hover { text-decoration: underline; text-underline-offset: 3px; }

/* Links to the content pages (Learn, About, Privacy, Terms) */
.panel-links {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
  padding: 12px 4px 4px;
  margin-top: 4px;
  border-top: 1px solid var(--border);
  font-size: 12px;
}
.panel-links a { color: var(--text-dim); text-decoration: none; }
.panel-links a:hover { color: var(--violet-hi); text-decoration: underline; text-underline-offset: 3px; }
.panel-links a:first-child { color: var(--text); font-weight: 500; }

/* Shown only without JavaScript */
.noscript {
  position: fixed;
  inset: 0;
  z-index: 100;
  padding: 24px;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.6;
}
.noscript a { color: var(--violet-hi); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  padding: 0 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--field);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  transition: background 0.18s, border-color 0.18s, transform 0.12s, box-shadow 0.18s;
}
.btn:hover { background: var(--field-hover); border-color: var(--border-hi); }
.btn:active { transform: translateY(1px); }
.btn.small { min-height: 30px; padding: 0 12px; font-size: 12px; }
.btn.big { min-height: 46px; font-size: 15px; font-weight: 600; letter-spacing: 0.04em; }
.btn-block { width: 100%; }
.btn-primary {
  border-color: transparent;
  background: linear-gradient(135deg, var(--violet), var(--violet-deep));
  color: #fff;
  font-weight: 600;
  box-shadow: 0 4px 15px rgba(124, 58, 237, 0.3);
}
.btn-primary:hover {
  background: linear-gradient(135deg, var(--violet-hi), var(--violet));
  border-color: transparent;
  box-shadow: 0 6px 20px rgba(124, 58, 237, 0.5);
}
.btn-danger { color: #ffb4c2; border-color: rgba(255, 110, 140, 0.35); background: rgba(255, 80, 120, 0.12); }
.btn-danger:hover { background: rgba(255, 80, 120, 0.22); border-color: rgba(255, 110, 140, 0.6); }

/* --------------------------------------------------------------------------
   Panel
   -------------------------------------------------------------------------- */
#panel {
  position: fixed;
  z-index: 20;
  top: calc(70px + var(--safe-t));
  right: calc(16px + var(--safe-r));
  width: var(--panel-w);
  max-height: calc(100vh - 86px - var(--safe-t) - var(--safe-b));
  max-height: calc(100dvh - 86px - var(--safe-t) - var(--safe-b));
  display: flex;
  flex-direction: column;
  border-radius: 20px;
  overflow: hidden;
  transform-origin: top right;
  transition: transform 0.38s cubic-bezier(0.22, 1, 0.36, 1), opacity 0.25s ease, visibility 0s linear 0s;
}
body.panel-collapsed #panel {
  transform: translateX(calc(100% + 40px));
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: transform 0.38s cubic-bezier(0.22, 1, 0.36, 1), opacity 0.25s ease, visibility 0s linear 0.38s;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px 10px 18px;
  border-bottom: 1px solid var(--border);
  flex: none;
}
.panel-title {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--text-dim);
}
.panel-head-actions { display: flex; align-items: center; gap: 8px; }

.panel-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 4px 0 6px;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.2) transparent;
}
.panel-body::-webkit-scrollbar { width: 8px; }
.panel-body::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.16);
  border-radius: 8px;
  border: 2px solid transparent;
  background-clip: content-box;
}

.panel-foot {
  flex: none;
  padding: 9px 18px 11px;
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--text-faint);
  line-height: 1.45;
}

/* Sections */
.section { border-bottom: 1px solid var(--border); }
.section:last-child { border-bottom: 0; }
.section > summary {
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 13px 18px;
  cursor: pointer;
  user-select: none;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--text);
  transition: color 0.15s;
}
.section > summary::-webkit-details-marker { display: none; }
.section > summary::after {
  content: "";
  width: 7px;
  height: 7px;
  border-right: 1.5px solid var(--text-dim);
  border-bottom: 1.5px solid var(--text-dim);
  transform: rotate(45deg) translate(-2px, -2px);
  transition: transform 0.22s ease;
}
.section[open] > summary::after { transform: rotate(225deg) translate(-2px, -2px); }
.section > summary:hover { color: var(--violet-hi); }
.section > summary:focus-visible { outline-offset: -3px; border-radius: 10px; }
.section-body {
  padding: 0 18px 16px;
  display: grid;
  gap: 13px;
}

/* Fields */
.field { display: grid; gap: 6px; min-width: 0; }
.field.compact { margin-top: -6px; }
.field-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.field label, .field .label, .switch-row label {
  font-size: 12.5px;
  font-weight: 400;
  color: var(--text-dim);
}
.badge {
  font-family: var(--font-num);
  font-variant-numeric: tabular-nums;
  font-size: 11.5px;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--field);
  border: 1px solid var(--border);
  color: var(--text);
  min-width: 38px;
  text-align: center;
}
.note {
  margin: 0;
  font-size: 11.5px;
  line-height: 1.5;
  color: var(--text-faint);
}
.note.tip {
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  background: rgba(139, 124, 255, 0.09);
  border: 1px solid rgba(139, 124, 255, 0.22);
  color: var(--text-dim);
}
.pair { display: grid; grid-template-columns: 1fr 78px; gap: 10px; align-items: center; }
.sub-controls { display: grid; gap: 13px; }
.sub-controls.hidden { display: none !important; }

.row-2 { display: grid; grid-template-columns: 1fr auto; gap: 8px; }
.row-2.even { grid-template-columns: 1fr 1fr; }
.row-2 .btn { min-width: 0; }
.row-export { grid-template-columns: 84px 1fr; }
.row-tour select { min-width: 0; text-overflow: ellipsis; }

/* --------------------------------------------------------------------------
   Form controls
   -------------------------------------------------------------------------- */
input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 20px;
  margin: 0;
  background: transparent;
  cursor: pointer;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: 4px;
  background: linear-gradient(90deg, rgba(168, 85, 247, 0.55), rgba(216, 180, 254, 0.45));
  background-color: rgba(255, 255, 255, 0.1);
}
input[type="range"]::-moz-range-track {
  height: 4px;
  border-radius: 4px;
  background: linear-gradient(90deg, rgba(168, 85, 247, 0.55), rgba(216, 180, 254, 0.45));
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  margin-top: -6px;
  border-radius: 50%;
  background: #fff;
  border: 3px solid var(--violet);
  box-shadow: 0 0 0 0 rgba(139, 124, 255, 0.4), 0 2px 6px rgba(0, 0, 0, 0.5);
  transition: box-shadow 0.18s, transform 0.18s;
}
input[type="range"]::-moz-range-thumb {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #fff;
  border: 3px solid var(--violet);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5);
}
input[type="range"]:hover::-webkit-slider-thumb { box-shadow: 0 0 0 6px rgba(139, 124, 255, 0.22), 0 2px 6px rgba(0, 0, 0, 0.5); }
input[type="range"]:active::-webkit-slider-thumb { transform: scale(1.12); }
input[type="range"]:focus-visible { outline: none; }
input[type="range"]:focus-visible::-webkit-slider-thumb { box-shadow: 0 0 0 4px rgba(216, 180, 254, 0.45); border-color: var(--violet-hi); }
input[type="range"]:focus-visible::-moz-range-thumb { box-shadow: 0 0 0 4px rgba(216, 180, 254, 0.45); border-color: var(--violet-hi); }
input[type="range"]:disabled { opacity: 0.4; cursor: not-allowed; }

input[type="number"],
select {
  width: 100%;
  min-height: 34px;
  padding: 0 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background-color: var(--field);
  color: var(--text);
  font-size: 13px;
  transition: border-color 0.18s, background-color 0.18s;
}
input[type="number"] {
  font-family: var(--font-num);
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  padding: 0 8px;
  -moz-appearance: textfield;
  appearance: textfield;
}
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
input[type="number"]:hover, select:hover { border-color: var(--border-hi); background-color: var(--field-hover); }
input[type="number"]:focus-visible, select:focus-visible { outline: 2px solid var(--violet-hi); outline-offset: 1px; border-radius: var(--radius-sm); }
select {
  -webkit-appearance: none;
  appearance: none;
  padding-right: 30px;
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M2.5 4.5 6 8l3.5-3.5' fill='none' stroke='%23a3a2b8' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
}
select option { background: #10131f; color: var(--text); }

/* Switch */
.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.switch-row label { cursor: pointer; flex: 1; }
input.switch {
  -webkit-appearance: none;
  appearance: none;
  flex: none;
  position: relative;
  width: 38px;
  height: 22px;
  margin: 0;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.13);
  border: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.2s, border-color 0.2s;
}
input.switch::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5);
  transition: transform 0.22s cubic-bezier(0.22, 1, 0.36, 1);
}
input.switch:checked {
  background: var(--accent-grad);
  border-color: transparent;
}
input.switch:checked::after { transform: translateX(16px); }
input.switch:focus-visible { outline: 2px solid var(--violet-hi); outline-offset: 2px; border-radius: 999px; }
input.switch:disabled { opacity: 0.4; cursor: not-allowed; }

/* Segmented control */
.seg-group {
  display: flex;
  padding: 3px;
  gap: 2px;
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.28);
  border: 1px solid var(--border);
}
.seg {
  flex: 1;
  min-height: 30px;
  padding: 0 8px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--text-dim);
  font-size: 12.5px;
  font-weight: 500;
  transition: background 0.2s, color 0.2s, box-shadow 0.2s;
}
.seg:hover { color: var(--text); background: rgba(255, 255, 255, 0.06); }
.seg.active {
  color: #fff;
  background: linear-gradient(135deg, rgba(168, 85, 247, 0.9), rgba(124, 58, 237, 0.85));
  box-shadow: 0 2px 10px -2px rgba(168, 85, 247, 0.6);
}
.seg:focus-visible { outline-offset: 0; border-radius: 9px; }
.seg:disabled { opacity: 0.4; cursor: not-allowed; }

/* Fractal cards */
.fractal-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.fractal-card {
  position: relative;
  aspect-ratio: 1 / 1;
  padding: 0;
  overflow: hidden;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: #0a0d16;
  color: #fff;
  transition: transform 0.18s, border-color 0.18s, box-shadow 0.18s;
}
.fractal-card img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: transform 0.4s ease;
}
.fractal-card span {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 14px 2px 5px;
  font-size: 10px;
  font-weight: 500;
  line-height: 1.15;
  text-align: center;
  letter-spacing: 0;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.9);
  background: linear-gradient(to top, rgba(4, 4, 10, 0.88), transparent);
}
.fractal-card:hover { border-color: var(--border-hi); transform: translateY(-1px); }
.fractal-card:hover img { transform: scale(1.07); }
.fractal-card.active {
  border-color: transparent;
  box-shadow: 0 0 0 2px var(--violet), 0 0 0 5px rgba(168, 85, 247, 0.2), 0 6px 16px -4px rgba(168, 85, 247, 0.5);
}
.fractal-card:focus-visible { outline: 2px solid var(--violet-hi); outline-offset: 2px; border-radius: 12px; }

/* Palette swatches */
.palette-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.palette-swatch {
  height: 28px;
  padding: 0;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.3);
  transition: transform 0.18s, box-shadow 0.18s;
}
.palette-swatch:hover { transform: scale(1.06); }
.palette-swatch.active {
  border-color: transparent;
  box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px #fff;
}
.palette-swatch:focus-visible { outline: 2px solid var(--violet-hi); outline-offset: 3px; border-radius: 999px; }

/* --------------------------------------------------------------------------
   Hint, toast
   -------------------------------------------------------------------------- */
#hint {
  position: fixed;
  z-index: 9;
  left: 50%;
  bottom: calc(22px + var(--safe-b));
  transform: translateX(-50%);
  padding: 8px 18px;
  border-radius: 999px;
  font-size: 12.5px;
  letter-spacing: 0.04em;
  color: var(--text-dim);
  white-space: nowrap;
  pointer-events: none;
  transition: opacity 0.8s ease, transform 0.8s ease;
}
#hint.faded { opacity: 0; transform: translate(-50%, 8px); }

#toast {
  position: fixed;
  z-index: 60;
  left: 50%;
  bottom: calc(64px + var(--safe-b));
  transform: translate(-50%, 14px);
  max-width: min(92vw, 520px);
  padding: 10px 20px;
  border-radius: 999px;
  background: rgba(13, 17, 28, 0.92);
  -webkit-backdrop-filter: blur(18px);
  backdrop-filter: blur(18px);
  border: 1px solid var(--border-hi);
  box-shadow: var(--shadow);
  font-size: 13px;
  font-weight: 500;
  text-align: center;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.28s ease, transform 0.28s cubic-bezier(0.22, 1, 0.36, 1), visibility 0s linear 0.28s;
}
#toast.show {
  opacity: 1;
  visibility: visible;
  transform: translate(-50%, 0);
  transition-delay: 0s;
}

/* --------------------------------------------------------------------------
   Benchmark overlay
   -------------------------------------------------------------------------- */
#bench-overlay {
  display: none;
  position: fixed;
  z-index: 40;
  left: 50%;
  bottom: calc(28px + var(--safe-b));
  transform: translateX(-50%);
  width: min(420px, calc(100vw - 32px));
  padding: 14px 18px 16px;
  border-radius: 18px;
}
body.benchmarking #bench-overlay { display: block; animation: rise 0.3s ease-out; }
.bench-top { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.bench-scene { font-size: 15px; font-weight: 600; letter-spacing: 0.02em; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bench-stats { display: flex; gap: 28px; margin-bottom: 12px; }
.bench-stat { display: grid; gap: 1px; }
.stat-label { font-size: 10.5px; letter-spacing: 0.14em; text-transform: uppercase; color: var(--text-faint); }
.stat-val { font-family: var(--font-num); font-variant-numeric: tabular-nums; font-size: 22px; font-weight: 500; }
@keyframes rise {
  from { opacity: 0; transform: translate(-50%, 12px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

/* --------------------------------------------------------------------------
   Dialogs
   -------------------------------------------------------------------------- */
dialog {
  padding: 0;
  border: 1px solid var(--border-hi);
  border-radius: 22px;
  color: var(--text);
  background: rgba(14, 14, 28, 0.82);
  -webkit-backdrop-filter: blur(28px) saturate(1.5);
  backdrop-filter: blur(28px) saturate(1.5);
  box-shadow: 0 30px 80px -10px rgba(0, 0, 0, 0.8), inset 0 1px 0 rgba(255, 255, 255, 0.06);
  width: min(520px, calc(100vw - 28px));
  max-height: min(88vh, 760px);
  max-height: min(88dvh, 760px);
  overflow: hidden;
}
dialog[open] { display: flex; flex-direction: column; animation: dlg-in 0.28s cubic-bezier(0.22, 1, 0.36, 1); }
dialog::backdrop {
  background: rgba(3, 3, 9, 0.55);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
}
@keyframes dlg-in {
  from { opacity: 0; transform: translateY(14px) scale(0.97); }
  to { opacity: 1; transform: none; }
}
.dlg {
  padding: 24px 26px 20px;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.2) transparent;
}
.dlg-title {
  margin: 0 0 14px;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: 0.12em;
  background: linear-gradient(135deg, #ffffff 20%, var(--violet-hi) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  color: transparent;
}
.dlg-desc { margin: 0 0 18px; color: var(--text-dim); font-size: 13.5px; line-height: 1.6; }
.dlg-sub {
  margin: 22px 0 8px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--text-faint);
}
.dlg-foot { display: flex; justify-content: flex-end; margin-top: 20px; }

/* Radio cards */
.radio-cards { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 16px; }
.radio-card { position: relative; display: block; cursor: pointer; }
.radio-card input {
  position: absolute;
  opacity: 0;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  cursor: pointer;
}
.rc-body {
  display: grid;
  gap: 3px;
  height: 100%;
  padding: 12px 14px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--field);
  transition: border-color 0.2s, background 0.2s, box-shadow 0.2s;
}
.rc-title { font-size: 15px; font-weight: 600; }
.rc-desc { font-size: 11.5px; color: var(--text-dim); font-family: var(--font-num); font-variant-numeric: tabular-nums; }
.radio-card:hover .rc-body { border-color: var(--border-hi); background: var(--field-hover); }
.radio-card input:checked + .rc-body {
  border-color: var(--violet);
  background: linear-gradient(135deg, rgba(168, 85, 247, 0.22), rgba(124, 58, 237, 0.12));
  box-shadow: 0 0 0 1px var(--violet), 0 8px 22px -10px rgba(168, 85, 247, 0.7);
}
.radio-card input:focus-visible + .rc-body { outline: 2px solid var(--violet-hi); outline-offset: 2px; }

/* History */
.history { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; max-height: 150px; overflow-y: auto; }
.history li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 7px 12px;
  border-radius: 10px;
  background: var(--field);
  font-size: 12.5px;
  color: var(--text-dim);
}
.history li strong, .history li b { font-family: var(--font-num); font-variant-numeric: tabular-nums; color: var(--text); font-weight: 600; }

/* Results */
.score-block { display: grid; justify-items: center; gap: 0; padding: 4px 0 14px; }
.score-label { font-size: 11px; font-weight: 600; letter-spacing: 0.3em; text-transform: uppercase; color: var(--text-faint); }
.score-num {
  font-family: var(--font-num);
  font-variant-numeric: tabular-nums;
  font-size: clamp(56px, 16vw, 92px);
  font-weight: 700;
  line-height: 1.05;
  letter-spacing: -0.02em;
  background: linear-gradient(135deg, #ffffff 20%, var(--violet-hi) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  color: transparent;
  filter: drop-shadow(0 4px 24px rgba(168, 85, 247, 0.35));
}
.bench-table { width: 100%; border-collapse: collapse; margin-bottom: 10px; font-size: 13px; }
.bench-table th {
  padding: 6px 8px;
  text-align: left;
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--text-faint);
  border-bottom: 1px solid var(--border-hi);
}
.bench-table td { padding: 8px 8px; border-bottom: 1px solid var(--border); color: var(--text-dim); }
.bench-table td:first-child { color: var(--text); }
.bench-table .num, .bench-table td:nth-child(n+2) { text-align: right; font-family: var(--font-num); font-variant-numeric: tabular-nums; }
.bench-table tbody tr:last-child td { border-bottom: 0; }
#bench-results .note { margin-bottom: 14px; text-align: center; }

/* Help */
.keys { margin: 0; display: grid; gap: 2px; }
.key-row {
  display: grid;
  grid-template-columns: 148px 1fr;
  gap: 14px;
  align-items: center;
  padding: 7px 0;
  border-bottom: 1px solid var(--border);
}
.key-row:last-child { border-bottom: 0; }
.keys dt { display: flex; flex-wrap: wrap; gap: 4px; }
.keys dd { margin: 0; font-size: 13px; color: var(--text-dim); }
kbd {
  display: inline-block;
  min-width: 24px;
  padding: 2px 8px;
  border-radius: 7px;
  border: 1px solid var(--border-hi);
  border-bottom-width: 2px;
  background: rgba(255, 255, 255, 0.07);
  font-family: var(--font-num);
  font-size: 11.5px;
  line-height: 1.5;
  text-align: center;
  color: var(--text);
}

/* --------------------------------------------------------------------------
   Body state classes
   -------------------------------------------------------------------------- */
body.ui-hidden > :not(#stage):not(#toast):not(script) { display: none !important; }

body.benchmarking #toast { bottom: calc(190px + var(--safe-b)); }
body.benchmarking #panel,
body.benchmarking #hint,
body.benchmarking #pip,
body.benchmarking #btn-panel,
body.benchmarking #hud { display: none !important; }

/* --------------------------------------------------------------------------
   Mobile: bottom sheet
   -------------------------------------------------------------------------- */
@media (max-width: 759px) {
  .topleft {
    top: calc(12px + var(--safe-t));
    left: calc(14px + var(--safe-l));
    gap: 8px;
    max-width: calc(100vw - 76px);
  }
  .brand h1 { font-size: 17px; letter-spacing: 0.26em; }
  .brand .subtitle { display: none; }

  #hud {
    width: auto;
    max-width: min(240px, calc(100vw - 76px));
    padding: 7px 10px 9px;
    font-size: 11px;
    gap: 2px;
  }
  .hud-row { grid-template-columns: 54px 1fr; gap: 6px; }
  .hud-label { font-size: 9.5px; }
  .hud-row-center, .hud-row-mode { display: none; }

  #btn-panel { top: calc(10px + var(--safe-t)); right: calc(12px + var(--safe-r)); width: 42px; height: 42px; }

  #panel {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: auto;
    max-height: 55vh;
    max-height: 55dvh;
    border-radius: 22px 22px 0 0;
    border-bottom: 0;
    padding-bottom: var(--safe-b);
    transform-origin: bottom center;
  }
  #panel::before {
    content: "";
    flex: none;
    align-self: center;
    width: 38px;
    height: 4px;
    margin: 8px 0 0;
    border-radius: 4px;
    background: rgba(255, 255, 255, 0.22);
  }
  .panel-head { padding-top: 6px; }
  body.panel-collapsed #panel { transform: translateY(105%); }

  .panel-foot { display: none; }
  .fractal-card { aspect-ratio: 4 / 3; }

  #hint { bottom: calc(14px + var(--safe-b)); font-size: 11.5px; padding: 7px 14px; max-width: calc(100vw - 24px); overflow: hidden; text-overflow: ellipsis; }
  body:not(.panel-collapsed) #hint { opacity: 0; }
  #toast { bottom: calc(58px + var(--safe-b)); }
  body:not(.panel-collapsed) #toast { bottom: calc(55vh + 14px); bottom: calc(55dvh + 14px); }

  #bench-overlay { bottom: calc(16px + var(--safe-b)); }

  dialog { border-radius: 20px; max-height: 90vh; max-height: 90dvh; }
  .dlg { padding: 20px 18px 16px; }
  .radio-cards { grid-template-columns: 1fr; }
  .key-row { grid-template-columns: 112px 1fr; gap: 10px; }
}

/* --------------------------------------------------------------------------
   Short desktop windows
   -------------------------------------------------------------------------- */
@media (min-width: 760px) and (max-height: 640px) {
  .brand .subtitle { display: none; }
  #hud .hud-row-mode { display: none; }
}

/* --------------------------------------------------------------------------
   Reduced motion
   -------------------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    transition-delay: 0s !important;
    scroll-behavior: auto !important;
  }
}

/* Fallback when backdrop-filter is unsupported */
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  .glass { background: var(--glass-strong); }
  dialog { background: rgba(14, 14, 28, 0.96); }
}
