:root {
  --ink: #f7fbff;
  --muted: rgba(247,251,255,.66);
  --line: rgba(255,255,255,.16);
  --panel: rgba(9,12,20,.82);
  --accent: #55d6c2;
  --danger: #ff6d7a;
  --slide-scale: 82;
  --slide-x: -6;
  --slide-y: -2;
  --subtitle-font: Arial, "Microsoft JhengHei", sans-serif;
  --subtitle-size: 34px;
  --subtitle-line-height: 1.38;
  --subtitle-width: 58%;
  --subtitle-y: 68%;
  --subtitle-text-color: #fff;
  --subtitle-background: rgba(5,7,13,.68);
  --play-x: 50%;
  --play-y: 84%;
  --ask-x: 50%;
  --ask-y: 93%;
}

* { box-sizing: border-box; }
html, body { width: 100%; height: 100%; }
body {
  margin: 0;
  overflow: hidden;
  font-family: Arial, "Microsoft JhengHei", sans-serif;
  color: var(--ink);
  background: #070a11;
}
button, input, select { font: inherit; }
button { cursor: pointer; }
svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
svg path:first-child:last-child,
svg path:first-child:nth-last-child(2) { fill: currentColor; stroke: none; }

.app {
  position: relative;
  width: 100%;
  height: 100%;
  min-height: 100dvh;
  overflow: hidden;
  isolation: isolate;
}

.stage-background {
  position: absolute;
  inset: 0;
  z-index: -4;
  background:
    linear-gradient(115deg, rgba(255,255,255,.06) 0 1px, transparent 1px 120px),
    linear-gradient(25deg, rgba(85,214,194,.09), transparent 32%),
    radial-gradient(ellipse at 16% 82%, rgba(255,202,98,.16), transparent 28%),
    radial-gradient(ellipse at 84% 18%, rgba(130,167,255,.2), transparent 34%),
    linear-gradient(145deg, #070a11 0%, #111827 52%, #080a12 100%);
  background-size: 120px 120px, cover, cover, cover, cover;
  background-position: center;
}
.app[data-background="api"] .stage-background {
  background-size: cover;
  background-position: center;
}

.status-pill {
  position: fixed;
  top: max(18px, env(safe-area-inset-top));
  left: max(18px, env(safe-area-inset-left));
  z-index: 14;
  display: flex;
  align-items: center;
  gap: 9px;
  min-height: 38px;
  max-width: min(54vw, 620px);
  padding: 0 13px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: rgba(247,251,255,.78);
  background: rgba(7,10,17,.55);
  backdrop-filter: blur(18px);
  font-size: 12px;
  font-weight: 800;
}
.status-dot {
  width: 8px;
  height: 8px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: #8a93a3;
}
.status-dot.active {
  background: #54f0ad;
  box-shadow: 0 0 0 5px rgba(84,240,173,.13);
}
.status-dot.error { background: var(--danger); }

.slide-layer {
  position: absolute;
  left: calc(50% + (var(--slide-x) * 1vw));
  top: calc(50% + (var(--slide-y) * 1vh));
  z-index: 1;
  width: min(calc(var(--slide-scale) * 1vw), calc(var(--slide-scale) * 1.7778vh));
  aspect-ratio: 16 / 9;
  transform: translate(-50%, -50%);
  border: 1px solid rgba(255,255,255,.22);
  border-radius: 8px;
  overflow: hidden;
  background: rgba(230,235,242,.94);
  box-shadow: 0 36px 90px rgba(0,0,0,.46);
}
.slide-layer iframe {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
  background: #e9edf4;
}
.slide-empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 10px;
  text-align: center;
  color: #20242c;
  background:
    linear-gradient(90deg, rgba(32,36,44,.08) 1px, transparent 1px),
    linear-gradient(0deg, rgba(32,36,44,.08) 1px, transparent 1px),
    #eef2f7;
  background-size: 42px 42px;
}
.slide-empty strong { font-size: clamp(24px, 3vw, 46px); }
.slide-empty span { color: #657080; font-weight: 800; }
.slide-layer.has-slides .slide-empty { display: none; }

.avatar-layer {
  position: absolute;
  left: 70%;
  top: 50%;
  z-index: 4;
  width: min(28vw, 390px);
  height: min(72vh, 720px);
  min-width: 170px;
  min-height: 290px;
  transform: translate(-50%, -50%);
  touch-action: none;
  cursor: grab;
}
.avatar-layer.dragging { cursor: grabbing; }
.avatar-layer canvas {
  width: 100%;
  height: 100%;
  display: block;
  filter: drop-shadow(0 20px 22px rgba(0,0,0,.32));
}
.resize-handle {
  position: absolute;
  right: 10px;
  bottom: 10px;
  width: 22px;
  height: 22px;
  border-right: 3px solid rgba(255,255,255,.62);
  border-bottom: 3px solid rgba(255,255,255,.62);
  border-radius: 0 0 6px 0;
  opacity: .5;
}
.avatar-layer:hover .resize-handle { opacity: 1; }

.subtitle {
  position: fixed;
  left: 50%;
  top: var(--subtitle-y);
  z-index: 9;
  width: min(var(--subtitle-width), 1200px);
  transform: translate(-50%, -50%);
  text-align: center;
  color: var(--subtitle-text-color);
  font-family: var(--subtitle-font);
  pointer-events: none;
  filter: drop-shadow(0 8px 18px rgba(0,0,0,.48));
}
.subtitle p {
  display: inline;
  margin: 0;
  padding: .22em .5em .27em;
  border-radius: .38em;
  background: var(--subtitle-background);
  box-decoration-break: clone;
  -webkit-box-decoration-break: clone;
  font-size: clamp(18px, var(--subtitle-size), 72px);
  font-weight: 900;
  line-height: var(--subtitle-line-height);
  text-wrap: balance;
}
.subtitle.updating { animation: subtitle-in .22s ease both; }
@keyframes subtitle-in {
  from { opacity: 0; transform: translate(-50%, calc(-50% + 10px)); }
  to { opacity: 1; transform: translate(-50%, -50%); }
}

.control-cluster {
  position: fixed;
  z-index: 12;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 9px;
  border: 1px solid rgba(255,255,255,.18);
  border-radius: 999px;
  background: rgba(6,9,15,.68);
  box-shadow: 0 18px 54px rgba(0,0,0,.32);
  backdrop-filter: blur(18px);
}
.play-controls {
  left: var(--play-x);
  top: var(--play-y);
  transform: translate(-50%, -50%);
}
.ask-controls {
  left: var(--ask-x);
  top: var(--ask-y);
  width: min(680px, calc(100vw - 28px));
  transform: translate(-50%, -50%);
}
.icon-button {
  width: 48px;
  height: 48px;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  border: 1px solid rgba(255,255,255,.16);
  border-radius: 50%;
  color: var(--ink);
  background: rgba(255,255,255,.1);
}
.icon-button:hover { background: rgba(255,255,255,.18); }
.icon-button.primary {
  color: #06100e;
  background: var(--accent);
  border-color: transparent;
}
.icon-button.danger {
  color: #fff;
  background: rgba(255,109,122,.22);
}
.icon-button:disabled { opacity: .48; cursor: not-allowed; }
.ask-controls input {
  width: 100%;
  min-width: 0;
  height: 48px;
  border: 0;
  border-radius: 999px;
  padding: 0 17px;
  color: var(--ink);
  background: rgba(255,255,255,.1);
  outline: none;
}
.ask-controls input::placeholder { color: rgba(247,251,255,.54); }

.menu-button {
  position: fixed;
  top: max(18px, env(safe-area-inset-top));
  right: max(18px, env(safe-area-inset-right));
  z-index: 20;
  width: 54px;
  height: 54px;
  display: grid;
  align-content: center;
  gap: 6px;
  padding: 0 14px;
  border: 1px solid rgba(255,255,255,.24);
  border-radius: 8px;
  background: rgba(247,251,255,.88);
  box-shadow: 0 18px 54px rgba(0,0,0,.24);
}
.menu-button span {
  display: block;
  height: 2px;
  border-radius: 99px;
  background: #151a22;
}

.scrim {
  position: fixed;
  inset: 0;
  z-index: 29;
  background: rgba(0,0,0,.42);
  backdrop-filter: blur(3px);
}
.settings-panel {
  position: fixed;
  top: 0;
  right: 0;
  z-index: 30;
  width: min(470px, 100vw);
  height: 100%;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 26px 26px calc(28px + env(safe-area-inset-bottom));
  color: #111827;
  background: #fbfcff;
  box-shadow: -22px 0 70px rgba(0,0,0,.24);
  transform: translateX(102%);
  transition: transform .24s ease;
}
.settings-panel.open { transform: translateX(0); }
.panel-header {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 24px;
}
.panel-header p {
  margin: 0 0 8px;
  color: #788292;
  font-size: 10px;
  font-weight: 900;
  letter-spacing: .18em;
}
.panel-header h1 {
  margin: 0;
  font-size: 28px;
}
.close-button {
  width: 40px;
  height: 40px;
  border: 1px solid #d8dde7;
  border-radius: 50%;
  color: #343b46;
  background: #fff;
  font-size: 26px;
  line-height: 1;
}
#settingsForm,
#settingsForm section {
  display: grid;
  gap: 16px;
}
#settingsForm section {
  padding: 22px 0;
  border-top: 1px solid #e3e8f0;
}
#settingsForm h2 {
  margin: 0;
  color: #0d121a;
  font-size: 14px;
  letter-spacing: .04em;
}
label {
  display: grid;
  gap: 8px;
  color: #4b5565;
  font-size: 13px;
  font-weight: 800;
}
small {
  color: #7b8493;
  font-size: 11px;
  line-height: 1.6;
}
input[type="text"],
input[type="url"],
input:not([type]),
select {
  width: 100%;
  min-height: 44px;
  border: 1px solid #d8dde7;
  border-radius: 8px;
  padding: 0 12px;
  color: #111827;
  background: #fff;
  outline: none;
}
input:focus,
select:focus {
  border-color: #18ad9a;
  box-shadow: 0 0 0 3px rgba(24,173,154,.12);
}
input[type="range"] {
  width: 100%;
  accent-color: #18ad9a;
}
input[type="color"] {
  width: 100%;
  height: 44px;
  border: 1px solid #d8dde7;
  border-radius: 8px;
  padding: 4px;
  background: #fff;
}
output { color: #0f8f7f; font-weight: 900; }
.grid-2 {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}
.button-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}
.secondary-button,
.primary-button,
.reset-button {
  min-height: 44px;
  border: 0;
  border-radius: 999px;
  padding: 0 16px;
  font-weight: 900;
}
.secondary-button {
  color: #111827;
  background: #e9eef5;
}
.primary-button {
  color: #06100e;
  background: var(--accent);
}
.reset-button {
  color: #fff;
  background: #303846;
}
.segmented {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin: 0;
  padding: 0;
  border: 0;
}
.segmented legend {
  grid-column: 1 / -1;
  margin-bottom: 8px;
  color: #4b5565;
  font-size: 13px;
  font-weight: 900;
}
.segmented label {
  display: block;
}
.segmented input {
  position: absolute;
  opacity: 0;
}
.segmented span {
  min-height: 42px;
  display: grid;
  place-items: center;
  border: 1px solid #d8dde7;
  border-radius: 8px;
  color: #4b5565;
  background: #fff;
}
.segmented input:checked + span {
  color: #06100e;
  border-color: transparent;
  background: var(--accent);
}
.diagnostics dl {
  display: grid;
  gap: 8px;
  margin: 0;
}
.diagnostics div {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  padding: 9px 0;
  border-bottom: 1px solid #edf1f6;
}
.diagnostics dt {
  color: #7b8493;
  font-size: 12px;
  font-weight: 900;
}
.diagnostics dd {
  margin: 0;
  color: #111827;
  text-align: right;
  font-size: 12px;
  font-weight: 900;
}
.panel-footer {
  position: sticky;
  bottom: -26px;
  display: flex;
  gap: 10px;
  padding: 16px 0 0;
  background: linear-gradient(180deg, rgba(251,252,255,0), #fbfcff 24%);
}

@media (max-width: 760px) {
  .status-pill {
    top: auto;
    bottom: calc(env(safe-area-inset-bottom) + 10px);
    left: 10px;
    max-width: calc(100vw - 20px);
  }
  .slide-layer {
    width: min(calc(var(--slide-scale) * 1vw), 94vw);
  }
  .avatar-layer {
    left: 74%;
    width: min(44vw, 260px);
    height: 54vh;
  }
  .play-controls { top: min(var(--play-y), 78%); }
  .ask-controls { top: min(var(--ask-y), 89%); }
  .icon-button {
    width: 43px;
    height: 43px;
  }
  .control-cluster { gap: 7px; padding: 7px; }
  .ask-controls input { height: 43px; }
  .grid-2, .segmented { grid-template-columns: 1fr; }
  .panel-footer { display: grid; }
}
