/* ---------- Base ---------- */

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

html,
body {
  margin: 0;
  height: 100%;
  /* No rubber-band scrolling of the whole app on iOS. */
  overscroll-behavior: none;
}

body {
  background: var(--surface-2);
  color: var(--glyph-1);
  font-family: var(--font-family);
  font-size: var(--font-size);
  line-height: var(--line-height);
  font-weight: var(--weight-regular);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  font-feature-settings: "cv11", "ss01";
  overflow: hidden;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  margin: 0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

a {
  color: inherit;
}

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

.icon {
  display: block;
  flex: none;
}

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

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

.app {
  --nav-col: calc(var(--panel-width) + var(--panel-inset));
  --settings-col: 0px;
  display: grid;
  grid-template-columns: var(--nav-col) minmax(0, 1fr) var(--settings-col);
  height: 100vh;
  height: 100dvh;
  transition: grid-template-columns var(--duration-slow) var(--ease-out);
}

.app[data-settings-open="true"] {
  --settings-col: calc(var(--panel-width) + var(--panel-inset));
}

/* Panels: floating cards inset from the window edge. */
.panel {
  display: flex;
  flex-direction: column;
  width: var(--panel-width);
  margin: var(--panel-inset) 0;
  background: var(--surface-1);
  border: 1px solid var(--border-3);
  border-radius: var(--radius-xl);
  overflow: hidden;
}

.nav {
  grid-column: 1;
  margin-left: var(--panel-inset);
  padding: var(--space-3);
}

/* On desktop the nav's wrapper is invisible to layout; on mobile it becomes
   the sheet frame (see the media query at the bottom). */
.nav-slot {
  display: contents;
}

.settings-slot {
  grid-column: 3;
  overflow: hidden;
  min-width: 0;
}

/* Desktop only: the settings panel slides in from the right. Kept out of the
   mobile range so it can't override the bottom-sheet transform. */
@media (min-width: 1001px) {
  .settings {
    margin-right: var(--panel-inset);
    height: calc(100% - 2 * var(--panel-inset));
    transition: opacity var(--duration-base) var(--ease-out), transform var(--duration-slow) var(--ease-out);
  }

  .app:not([data-settings-open="true"]) .settings {
    opacity: 0;
    transform: translateX(16px);
    pointer-events: none;
    visibility: hidden;
    transition: opacity var(--duration-base) var(--ease-out), transform var(--duration-slow) var(--ease-out), visibility 0s var(--duration-slow);
  }
}

/* ---------- Nav (left) ---------- */

.nav__logo {
  display: flex;
  justify-content: center;
  padding: var(--space-4) 0 var(--space-5);
  color: var(--glyph-1);
}

.nav__logo svg {
  width: 64px;
  height: 40px;
}

.nav__body {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.nav__footer {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-3);
  margin-top: auto;
  padding-top: var(--space-4);
}

.credit {
  color: var(--glyph-2);
}

.credit a {
  display: inline-block;
  color: var(--glyph-2);
  text-decoration: underline;
  text-underline-offset: 3px;
  text-decoration-color: color-mix(in srgb, var(--glyph-2) 60%, transparent);
  transition: color var(--duration-fast);
}

.credit a:hover {
  color: var(--glyph-1);
}

/* ---------- Stage (center) ---------- */

.stage {
  grid-column: 2;
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.toolbar {
  position: absolute;
  inset: 0 0 auto 0;
  z-index: 5;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-4);
  pointer-events: none;
}

.toolbar > * {
  pointer-events: auto;
}

.toolbar__group {
  display: flex;
  gap: var(--space-2);
}

.toolbar__group--end {
  justify-content: flex-end;
}

.canvas {
  flex: 1;
  min-height: 0;
  display: grid;
  place-items: center;
  padding: calc(var(--control-md) + 2 * var(--space-4)) var(--space-4);
  padding-bottom: max(calc(var(--control-md) + 2 * var(--space-4)), var(--sheet-offset, 0px));
  overflow: hidden;
  transition: padding-bottom var(--duration-slow) var(--ease-out);
}

.bottombar {
  display: none;
}

/* ---------- Buttons ---------- */

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: var(--control-md);
  height: var(--control-md);
  border-radius: var(--radius-full);
  border: 1px solid var(--border-2);
  color: var(--glyph-1);
  transition: background-color var(--duration-fast), border-color var(--duration-fast), color var(--duration-fast);
}

.icon-btn[hidden],
.btn[hidden] {
  display: none;
}

.icon-btn:hover {
  background: var(--fill-3);
}

.icon-btn:active {
  background: var(--fill-2);
}

.icon-btn--danger,
.icon-btn--danger:hover,
.icon-btn--danger:active {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}

.icon-btn--danger:hover {
  filter: brightness(1.08);
}

.icon-btn--ghost {
  width: var(--control-sm);
  height: var(--control-sm);
  border-color: transparent;
  border-radius: var(--radius-sm);
  color: var(--glyph-2);
}

.icon-btn--ghost:hover {
  color: var(--glyph-1);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  height: var(--control-md);
  padding: 0 var(--space-4) 0 var(--space-3);
  border-radius: var(--radius-full);
  font-weight: var(--weight-medium);
  white-space: nowrap;
  transition: opacity var(--duration-fast), background-color var(--duration-fast);
}

.btn--primary {
  background: var(--fill-1);
  color: var(--glyph-1);
}

.btn--outline {
  border: 1px solid var(--border-2);
  color: var(--glyph-1);
}

.btn--outline:hover {
  background: var(--fill-3);
}

.btn[aria-disabled="true"] {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ---------- Tooltip ---------- */

.tooltip {
  position: fixed;
  z-index: 100;
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-sm);
  background: var(--fill-1);
  color: var(--glyph-1);
  font-weight: var(--weight-medium);
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transform: translateY(4px);
  transition: opacity var(--duration-fast), transform var(--duration-base) var(--ease-out);
}

.tooltip[data-visible="true"] {
  opacity: 1;
  transform: none;
}

/* ---------- Segmented control ---------- */

.segmented {
  position: relative;
  display: grid;
  grid-auto-columns: 1fr;
  grid-auto-flow: column;
  height: var(--control-md);
  padding: var(--space-1);
  border: 1px solid var(--border-3);
  border-radius: var(--radius-full);
}

.segmented--sm {
  height: var(--control-sm);
  padding: 3px;
  border-radius: var(--radius-sm);
}

.segmented__thumb {
  position: absolute;
  top: var(--space-1);
  bottom: var(--space-1);
  left: var(--space-1);
  border-radius: var(--radius-full);
  background: var(--fill-2);
  transition: transform var(--duration-slow) var(--ease-out), width var(--duration-slow) var(--ease-out);
}

.segmented--sm .segmented__thumb {
  top: 3px;
  bottom: 3px;
  left: 3px;
  border-radius: 5px;
}

.segmented__option {
  position: relative;
  z-index: 1;
  border-radius: var(--radius-full);
  color: var(--glyph-2);
  font-weight: var(--weight-medium);
  transition: color var(--duration-base);
}

.segmented__option[aria-checked="true"],
.segmented__option:hover {
  color: var(--glyph-1);
}

.segmented--strong .segmented__option {
  color: var(--glyph-1);
}

/* ---------- Section + radio list ---------- */

.section {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.section__label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: var(--control-sm);
  padding: 0 var(--space-2);
  color: var(--glyph-2);
}

button.section__label {
  width: 100%;
  text-align: left;
}

button.section__label:hover {
  color: var(--glyph-1);
}

.section__label .icon {
  transition: transform var(--duration-base) var(--ease-out);
}

.section__label[aria-expanded="false"] .icon {
  transform: rotate(-90deg);
}

.section__value {
  color: var(--glyph-1);
  font-weight: var(--weight-medium);
  font-variant-numeric: tabular-nums;
}

.radio-list {
  display: flex;
  flex-direction: column;
}

.radio {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  width: 100%;
  height: var(--control-sm);
  padding: 0 var(--space-2);
  border-radius: var(--radius-sm);
  color: var(--glyph-2);
  text-align: left;
  transition: background-color var(--duration-fast), color var(--duration-fast);
}

.radio:hover {
  color: var(--glyph-1);
  background: var(--fill-3);
}

.radio[aria-checked="true"] {
  color: var(--glyph-1);
  background: var(--fill-2);
  font-weight: var(--weight-medium);
}

.radio__mark {
  position: relative;
  flex: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 1.5px solid var(--border-2);
  transition: border-color var(--duration-fast), background-color var(--duration-fast);
}

.radio__mark::after {
  content: "";
  position: absolute;
  inset: 0;
  margin: auto;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--_glyph-on-fill-1);
  transform: scale(0);
  transition: transform var(--duration-base) var(--ease-out);
}

.radio[aria-checked="true"] .radio__mark {
  border-color: var(--fill-1);
  background: var(--fill-1);
}

.radio[aria-checked="true"] .radio__mark::after {
  transform: scale(1);
}

.radio__label {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.radio__meta {
  color: var(--glyph-2);
  font-weight: var(--weight-regular);
}

/* ---------- Dropdown (phase picker) ---------- */

.dropdown {
  position: relative;
}

.dropdown__trigger {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  height: var(--control-md);
  padding: 0 var(--space-3) 0 var(--space-3);
  border: 1px solid var(--border-2);
  border-radius: var(--radius-full);
  font-weight: var(--weight-medium);
  white-space: nowrap;
  transition: background-color var(--duration-fast);
}

.dropdown__trigger:hover,
.dropdown__trigger[aria-expanded="true"] {
  background: var(--fill-3);
}

.dropdown__trigger .icon {
  transition: transform var(--duration-base) var(--ease-out);
}

.dropdown__trigger[aria-expanded="true"] .icon {
  transform: rotate(180deg);
}

.dropdown__menu {
  position: absolute;
  top: calc(100% + var(--space-2));
  left: 50%;
  z-index: 20;
  min-width: 200px;
  padding: var(--space-1);
  background: var(--surface-1);
  border: 1px solid var(--border-3);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-pop);
  transform-origin: top center;
  transform: translateX(-50%);
  transition: opacity var(--duration-fast), transform var(--duration-base) var(--ease-out);
}

.dropdown__menu[hidden] {
  display: block;
  opacity: 0;
  pointer-events: none;
  visibility: hidden;
  transform: translateX(-50%) translateY(-4px) scale(0.98);
}

.dropdown__divider {
  height: 1px;
  margin: var(--space-1) var(--space-2);
  background: var(--border-3);
}

.radio__live {
  width: 6px;
  height: 6px;
  margin-left: auto;
  border-radius: 50%;
  background: var(--glyph-1);
  opacity: 0;
  transition: opacity var(--duration-base);
}

.radio__live[data-on="true"] {
  opacity: 1;
}

/* ---------- Slider ---------- */

.slider {
  position: relative;
  height: var(--control-sm);
  border: 1px solid var(--border-3);
  border-radius: var(--radius-sm);
  overflow: hidden;
  touch-action: none;
}

.slider__fill {
  position: absolute;
  inset: 3px auto 3px 3px;
  width: calc((100% - 6px) * var(--p, 0.5));
  min-width: 10px;
  border-radius: 5px;
  background: var(--fill-2);
}

.slider__thumb {
  position: absolute;
  top: 50%;
  right: 5px;
  width: 2px;
  height: 14px;
  border-radius: 1px;
  background: var(--glyph-1);
  transform: translateY(-50%);
}

.slider input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: ew-resize;
  -webkit-appearance: none;
  appearance: none;
}

.slider input::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 2px;
  height: 32px;
}

.slider input::-moz-range-thumb {
  width: 2px;
  height: 32px;
  border: 0;
}

.slider:focus-within {
  outline: 2px solid var(--glyph-1);
  outline-offset: 2px;
}

/* ---------- Swatches ---------- */

.swatches {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  gap: var(--space-1);
}

.swatch {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  height: var(--control-sm);
  padding: 0 var(--space-2);
  border-radius: var(--radius-sm);
  color: var(--glyph-2);
  transition: background-color var(--duration-fast), color var(--duration-fast);
}

.swatch:hover {
  background: var(--fill-3);
  color: var(--glyph-1);
}

.swatch[aria-checked="true"] {
  background: var(--fill-2);
  color: var(--glyph-1);
  font-weight: var(--weight-medium);
}

.swatch__chip {
  flex: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--glyph-1) 12%, transparent);
}

/* ---------- Settings panel ---------- */

.settings__head {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  height: 56px;
  padding: 0 var(--space-3) 0 var(--space-4);
  flex: none;
}

.settings__title {
  flex: 1;
  font-weight: var(--weight-medium);
}

.settings__body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  padding: var(--space-2) var(--space-3) var(--space-4);
  scrollbar-width: thin;
}

.param {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.param .section__label {
  min-height: 24px;
}

.settings__group-title {
  padding: var(--space-2) var(--space-2) 0;
  border-top: 1px solid var(--border-3);
  margin: 0 calc(-1 * var(--space-3));
  padding-left: calc(var(--space-3) + var(--space-2));
  padding-top: var(--space-4);
  color: var(--glyph-2);
}

.preview {
  display: grid;
  place-items: center;
  aspect-ratio: 1 / 1;
  border: 1px solid var(--border-3);
  border-radius: var(--radius-md);
  background: var(--surface-2);
  overflow: hidden;
  transition: height var(--duration-slow) var(--ease-out);
}

.preview[data-kind="voice"] {
  aspect-ratio: 4 / 3;
}

.preview[hidden] {
  display: none;
}

.preview__mount {
  display: grid;
  place-items: center;
  width: 100px;
  height: 100px;
}

.preview[data-kind="voice"] .preview__mount {
  width: 100%;
  height: auto;
  padding: var(--space-3);
}

/* ---------- Chat scene ---------- */

.chat {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 540px;
}

.chat__bubble {
  align-self: flex-end;
  max-width: min(312px, 85%);
  padding: 12px var(--space-3);
  border-radius: 20px;
  background: var(--fill-2);
}

.chat__reply {
  margin: var(--space-5) 0 0;
}

.chat__status {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-top: var(--space-4);
  min-height: 20px;
}

.indicator {
  flex: none;
  width: 20px;
  height: 20px;
  color: var(--glyph-1);
}

.indicator svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
}

.status-label {
  position: relative;
  flex: 1;
  height: var(--line-height);
}

.status-label__text {
  position: absolute;
  inset: 0 auto 0 0;
  white-space: nowrap;
  color: transparent;
  background-image: linear-gradient(
    90deg,
    var(--glyph-2) 0%,
    var(--glyph-2) 35%,
    var(--glyph-1) 50%,
    var(--glyph-2) 65%,
    var(--glyph-2) 100%
  );
  background-size: 300% 100%;
  background-position: 100% 0;
  -webkit-background-clip: text;
  background-clip: text;
  animation: shimmer var(--shimmer-duration, 2.2s) linear infinite;
  transition: opacity var(--duration-slow) var(--ease-out), transform var(--duration-slow) var(--ease-out), filter var(--duration-slow);
}

.status-label__text[data-state="out"] {
  opacity: 0;
  transform: translateY(-6px);
  filter: blur(2px);
}

.status-label__text[data-state="in"] {
  opacity: 0;
  transform: translateY(6px);
  filter: blur(2px);
}

@keyframes shimmer {
  from { background-position: 100% 0; }
  to { background-position: 0% 0; }
}

.app[data-paused="true"] .status-label__text {
  animation-play-state: paused;
}

/* ---------- Voice scene ---------- */

.voice {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
  width: 100%;
  max-width: 560px;
}

.voice__stage {
  width: 100%;
  aspect-ratio: 2 / 1;
}

.voice__stage svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
}

.voice__caption {
  height: var(--line-height);
  color: var(--glyph-2);
  transition: opacity var(--duration-base);
}

/* ---------- Sheets (mobile) ---------- */

.sheet-close,
.scrim {
  display: none;
}

@media (max-width: 1000px) {
  .app,
  .app[data-settings-open="true"] {
    grid-template-columns: minmax(0, 1fr);
  }

  .stage {
    grid-column: 1;
  }

  .toolbar {
    padding: var(--space-3);
  }

  .toolbar [data-desktop-only] {
    display: none;
  }

  .canvas {
    padding: calc(var(--control-md) + 2 * var(--space-3)) var(--space-3);
    padding-bottom: max(calc(var(--control-md) + 2 * var(--space-3)), var(--sheet-offset, 0px));
    transition: padding-bottom var(--duration-sheet) var(--ease-sheet);
  }

  .bottombar {
    position: absolute;
    inset: auto 0 0 0;
    z-index: 5;
    display: flex;
    justify-content: space-between;
    gap: var(--space-2);
    padding: var(--space-3);
    padding-bottom: max(var(--space-3), env(safe-area-inset-bottom));
  }

  .bottombar .btn {
    padding: 0 var(--space-3);
  }

  /*
   * Bottom sheets: floating cards 8px above the bottom of the viewport (in iOS
   * Safari, that's 8px above the browser controls), rounded on every corner,
   * and scrolling inside the card when the content is taller than the screen.
   *
   * Each sheet sits in a fixed frame that stops 8px short of the bottom and
   * clips its contents. The sheet slides up from that line, so it never
   * touches the screen edge. That matters on iOS, where Safari colours the
   * area under its controls from whatever fixed element touches the edge. The
   * frame ignores the pointer, so only the overlay is there to be picked up.
   */
  .sheet-frame {
    position: fixed;
    inset: 0 0 calc(var(--panel-inset) + env(safe-area-inset-bottom)) 0;
    z-index: 40;
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    padding: 64px var(--panel-inset) 0;
    overflow: hidden;
    pointer-events: none;
  }

  .panel {
    position: relative;
    width: 100%;
    max-width: 560px;
    height: auto;
    max-height: 100%;
    margin: 0 auto;
    box-shadow: var(--shadow-pop);
    pointer-events: auto;
    transform: translate3d(0, calc(100% + 32px), 0);
    visibility: hidden;
    will-change: transform;
    transition: transform var(--duration-sheet) var(--ease-sheet), visibility 0s var(--duration-sheet);
  }

  .app[data-sheet="nav"] .nav,
  .app[data-sheet="settings"] .settings {
    transform: translate3d(0, 0, 0);
    visibility: visible;
    transition: transform var(--duration-sheet) var(--ease-sheet);
  }

  .nav {
    padding-top: var(--space-2);
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .settings__body {
    overscroll-behavior: contain;
  }

  .nav__logo {
    padding-top: 0;
  }

  .sheet-close {
    display: flex;
    justify-content: flex-end;
  }

  .settings .sheet-close {
    display: grid;
  }

  .settings [data-desktop-only] {
    display: none;
  }

  /*
   * iOS Safari tints its status bar (and the strip under its controls) from
   * the background colour of fixed elements at the screen edges, and it
   * ignores opacity. So the overlay fades its background colour, never its
   * opacity: the tint then follows the same fade as the page, rather than
   * jumping to full strength the moment the sheet opens.
   */
  .scrim {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 30;
    background-color: rgb(0 0 0 / 0);
    visibility: hidden;
    pointer-events: none;
    transition:
      background-color var(--duration-sheet) var(--ease-sheet),
      visibility 0s var(--duration-sheet);
  }

  .app[data-sheet] .scrim {
    background-color: rgb(0 0 0 / 0.2);
    visibility: visible;
    pointer-events: auto;
    transition: background-color var(--duration-sheet) var(--ease-sheet);
  }

  .preview {
    aspect-ratio: 16 / 9;
  }
}

.preview__mount svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
  color: var(--glyph-1);
}
