:root {
  --bg: #faf7f5;
  --fg: #2a2524;
  --muted: #7a706c;
  --accent: #c2410c;
  --line: #e6dfdb;
  --on: #16a34a;
  --warn: #d97706;
  --error: #dc2626;
  /* Minimum touch target (WCAG 2.5.5 / platform guidelines). */
  --tap: 44px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1817;
    --fg: #ede6e3;
    --muted: #a39793;
    --accent: #fb923c;
    --line: #3a3331;
    --error: #f87171;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font: 16px/1.4 system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
}
/* dvh: the mobile URL bar does not cut the frame off; vh is the fallback. */
#app { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }
.card {
  margin: auto;
  padding: max(24px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  width: 100%;
  max-width: 400px;
  text-align: center;
}
.card button { width: 100%; }
button {
  font: inherit;
  min-height: var(--tap);
  padding: 10px 18px;
  border-radius: 10px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  touch-action: manipulation;
}
button:disabled { opacity: 0.5; cursor: default; }
button.small {
  min-width: var(--tap);
  padding: 4px 12px;
  font-size: 14px;
  background: transparent;
  color: var(--fg);
  border-color: var(--line);
}
.status { color: var(--muted); min-height: 1.4em; overflow-wrap: anywhere; }

/* Compact header; the frame takes the rest of the viewport. */
header {
  display: flex;
  align-items: center;
  gap: 8px 12px;
  padding: 4px max(12px, env(safe-area-inset-right)) 4px max(12px, env(safe-area-inset-left));
  padding-top: max(4px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  flex: none;
}
header strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 30vw; }
header .peers { display: flex; gap: 8px; flex: 1; min-width: 0; overflow: hidden; }
.peer { color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.peer::before { content: "\25CF  "; color: var(--line); }
.peer.on::before { color: var(--on); }
.conn { color: var(--muted); font-size: 13px; white-space: nowrap; }
.notice:empty { display: none; }
.notice { margin: 0; padding: 8px 12px; background: var(--line); overflow-wrap: anywhere; }
.stage { flex: 1; min-height: 0; display: flex; padding-bottom: env(safe-area-inset-bottom); }
.stage iframe { flex: 1; border: 0; width: 100%; height: 100%; background: #fff; }
.stage[hidden], .studio[hidden] { display: none; }

/* ---------- studio (owner) ---------- */
.studio { flex: 1; min-height: 0; overflow-y: auto; overflow-x: hidden; }
/* Grid tracks are minmax(0, 1fr), not 1fr: a 1fr track's minimum is the content's min-content
   width, so one long unbreakable line would widen the page past the viewport. */
.studio-inner {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas: "head" "gen" "preview" "versions" "media";
  gap: 12px;
  max-width: 1400px;
  margin: 0 auto;
  padding: 8px max(12px, env(safe-area-inset-right)) max(48px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
}
.studio-inner > * { min-width: 0; }
.area-head { grid-area: head; }
.area-gen { grid-area: gen; }
.area-preview { grid-area: preview; }
.area-versions { grid-area: versions; }
.area-media { grid-area: media; }
/* Desktop: the form column on the left, the preview beside it. */
@media (min-width: 960px) {
  .studio-inner {
    grid-template-columns: minmax(0, 2fr) minmax(0, 3fr);
    grid-template-areas: "head head" "gen preview" "versions preview" "media preview";
    align-items: start;
  }
  .studio .area-preview { position: sticky; top: 8px; }
}
.studio h1 { font-size: 22px; margin: 4px 0; flex: 1; }
.studio h2 { font-size: 17px; margin: 0 0 8px; }
.studio section { border: 1px solid var(--line); border-radius: 12px; padding: 12px; margin: 0; }
.studio section[hidden] { display: none; }
.studio textarea, .studio input[type=text] {
  width: 100%;
  font: inherit;
  padding: 10px;
  min-height: var(--tap);
  border-radius: 8px;
  border: 1px solid var(--line);
  background: transparent;
  color: inherit;
}
.studio input[type=text] { width: auto; flex: 1 1 160px; min-width: 0; }
.studio input[type=file] { max-width: 100%; min-height: var(--tap); }
.studio .row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin: 8px 0; }
.studio .row > * { min-width: 0; }
/* The whole label is the touch target, not just the box. */
.studio .check { display: flex; align-items: center; gap: 10px; min-height: var(--tap); margin: 4px 0; color: var(--muted); }
.studio .check input { width: 22px; height: 22px; flex: none; margin: 0; }
.studio .hint, .studio .meta { color: var(--muted); font-size: 14px; overflow-wrap: anywhere; }
.studio .status[data-kind=error], .studio .notice[data-kind] { color: var(--error); }
.studio .status[data-kind=ok] { color: var(--on); }
.studio .prompt-text { white-space: pre-wrap; overflow-wrap: anywhere; margin: 0 0 4px; }
.studio #warnings[data-kind=warn] { border-left: 4px solid var(--warn); padding: 4px 10px; margin: 8px 0; }
.studio #warnings ul { margin: 6px 0; padding-left: 18px; font: 13px/1.4 ui-monospace, monospace; overflow-wrap: anywhere; }
.studio .preview-stage {
  display: flex;
  height: 70vh;
  height: 70dvh;
  min-height: 320px;
  border: 1px dashed var(--accent);
  border-radius: 8px;
  overflow: hidden;
}
.studio .preview-stage:empty { display: none; }
.studio .preview-stage iframe { flex: 1; min-width: 0; border: 0; background: #fff; }
.studio #launch-keep, .studio #launch-reset { flex: 1 1 160px; }
.studio ul.versions, .studio ul.media { list-style: none; margin: 0; padding: 0; }
.studio ul.versions li {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 2px 8px;
  padding: 8px 0;
  border-top: 1px solid var(--line);
}
.studio ul.versions li > * { min-width: 0; }
.studio ul.versions li .title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.studio ul.versions li .meta { grid-column: 1; }
.studio ul.versions li button { grid-row: 1 / span 2; grid-column: 2; align-self: center; }
.studio ul.media li { padding: 6px 0; overflow-wrap: anywhere; }
.badge { background: var(--on); color: #fff; border-radius: 6px; padding: 0 6px; margin-right: 6px; font-size: 12px; }
