/* =============================================================================
 * pm-studio / styles.css
 * -----------------------------------------------------------------------------
 * Single-file theme. Tokens first, then layout, then components.
 * Light + dark both supported; defaults to the system preference, with a toggle.
 * ========================================================================== */

/* --------------------------------------------------------------- tokens -- */
:root {
  /* Light theme */
  --bg: #f3f2ef;
  --bg-grad: radial-gradient(1200px 600px at 15% -10%, #ffffff, transparent 60%);
  --surface: #fbfbf9;
  --surface-2: #f0efeb;
  --border: #e0ded7;
  --border-strong: #cfccc3;
  --text: #1b1b18;
  --muted: #6f6e68;
  --faint: #98968f;

  --accent: #d98a2b;
  --accent-strong: #c2761b;
  --on-accent: #201607;
  --accent-soft: rgba(217, 138, 43, 0.14);

  --ok: #2f9e63;
  --ok-soft: rgba(47, 158, 99, 0.14);
  --warn: #c98a1d;
  --danger: #cf4b42;
  --danger-soft: rgba(207, 75, 66, 0.13);

  --shadow: 0 1px 2px rgba(30, 26, 18, 0.06), 0 12px 30px -18px rgba(30, 26, 18, 0.35);

  /* One radius scale: panels 16, controls 11, pills full. */
  --r-panel: 16px;
  --r-control: 11px;

  --font-display: "Space Grotesk", system-ui, sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, "SFMono-Regular", monospace;

  --topbar-h: 60px;
}

[data-theme="dark"] {
  --bg: #0d0e11;
  --bg-grad: radial-gradient(1200px 620px at 12% -12%, #1a1c22, transparent 58%);
  --surface: #15171c;
  --surface-2: #1b1e24;
  --border: #282c34;
  --border-strong: #3a3f49;
  --text: #eceae5;
  --muted: #9a9ea7;
  --faint: #6d727b;

  --accent: #e0a04a;
  --accent-strong: #ecb063;
  --on-accent: #17120a;
  --accent-soft: rgba(224, 160, 74, 0.15);

  --ok: #4bbd7d;
  --ok-soft: rgba(75, 189, 125, 0.15);
  --warn: #e0b24a;
  --danger: #e5645f;
  --danger-soft: rgba(229, 100, 95, 0.14);

  --shadow: 0 1px 0 rgba(255, 255, 255, 0.03), 0 18px 40px -22px rgba(0, 0, 0, 0.8);
}

/* Respect the OS preference unless the user has chosen explicitly. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d0e11;
    --bg-grad: radial-gradient(1200px 620px at 12% -12%, #1a1c22, transparent 58%);
    --surface: #15171c;
    --surface-2: #1b1e24;
    --border: #282c34;
    --border-strong: #3a3f49;
    --text: #eceae5;
    --muted: #9a9ea7;
    --faint: #6d727b;
    --accent: #e0a04a;
    --accent-strong: #ecb063;
    --on-accent: #17120a;
    --accent-soft: rgba(224, 160, 74, 0.15);
    --ok: #4bbd7d;
    --ok-soft: rgba(75, 189, 125, 0.15);
    --warn: #e0b24a;
    --danger: #e5645f;
    --danger-soft: rgba(229, 100, 95, 0.14);
    --shadow: 0 1px 0 rgba(255, 255, 255, 0.03), 0 18px 40px -22px rgba(0, 0, 0, 0.8);
  }
}

[data-theme="light"] { color-scheme: light; }
[data-theme="dark"] { color-scheme: dark; }

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

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg-grad), var(--bg);
  color: var(--text);
  font-family: var(--font-display);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

button, input, select, textarea { font: inherit; color: inherit; }

/* Author `display` rules beat the UA [hidden] rule, so make it explicit. */
[hidden] { display: none !important; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.skip-link {
  position: absolute; left: 12px; top: -48px;
  background: var(--accent); color: var(--on-accent);
  padding: 8px 12px; border-radius: var(--r-control);
  text-decoration: none; font-weight: 600; z-index: 50;
  transition: top 0.15s ease;
}
.skip-link:focus { top: 10px; }

svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

code { font-family: var(--font-mono); font-size: 0.86em; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }

/* --------------------------------------------------------------- topbar -- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  min-height: var(--topbar-h);
  padding: 10px clamp(14px, 3vw, 26px);
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 12px; }
.brand-mark {
  display: grid; place-items: center;
  width: 34px; height: 34px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--on-accent);
  font-weight: 700; font-size: 14px; letter-spacing: 0.02em;
}
.brand-text { display: flex; flex-direction: column; line-height: 1.15; }
.brand-name { font-weight: 600; font-size: 15px; letter-spacing: -0.01em; }
.brand-sub { font-size: 11.5px; color: var(--muted); font-family: var(--font-mono); }

.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: 8px; }

.icon-btn {
  display: grid; place-items: center;
  width: 36px; height: 36px;
  border-radius: var(--r-control);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}
.icon-btn:hover { color: var(--text); border-color: var(--border-strong); }
.icon-btn .i-sun { display: none; }
[data-theme="light"] .icon-btn .i-sun { display: block; }
[data-theme="light"] .icon-btn .i-moon { display: none; }
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) .icon-btn .i-sun { display: block; }
  :root:not([data-theme="dark"]) .icon-btn .i-moon { display: none; }
}

.key-chip {
  display: inline-flex; align-items: center; gap: 9px;
  height: 36px; padding: 0 12px;
  border-radius: var(--r-control);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  font-size: 13px; font-weight: 500;
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease, background 0.15s ease;
}
.key-chip:hover { color: var(--text); border-color: var(--border-strong); }
.key-chip svg { width: 16px; height: 16px; }
.key-chip.is-set { color: var(--text); }
.key-chip.is-set svg { color: var(--ok); }
.key-chip .chev {
  width: 7px; height: 7px; margin-left: 2px;
  border-right: 1.6px solid currentColor;
  border-bottom: 1.6px solid currentColor;
  transform: rotate(45deg) translateY(-2px);
  transition: transform 0.18s ease;
}
.key-chip[aria-expanded="true"] .chev { transform: rotate(-135deg) translateY(-1px); }

/* ------------------------------------------------------------ key panel -- */
.key-panel {
  flex-basis: 100%;
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr);
  gap: 16px 20px;
  margin-top: 4px;
  padding: 18px clamp(14px, 2vw, 20px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-panel);
  box-shadow: var(--shadow);
  animation: drop 0.2s ease;
}
@keyframes drop { from { opacity: 0; transform: translateY(-6px); } to { opacity: 1; transform: none; } }

.key-actions { grid-column: 1 / -1; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.feedback { font-size: 12.5px; color: var(--ok); font-family: var(--font-mono); }
.feedback.warn { color: var(--danger); }

/* --------------------------------------------------------------- fields -- */
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > label {
  font-size: 12px; font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--muted);
  text-transform: uppercase;
}
.field .opt { color: var(--faint); font-weight: 500; text-transform: none; letter-spacing: 0; }

input[type="text"], input[type="password"], input[type="url"], input[type="number"], select, textarea {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--r-control);
  color: var(--text);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
textarea { resize: vertical; min-height: 52px; line-height: 1.5; }
input::placeholder, textarea::placeholder { color: var(--faint); }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
select { appearance: none; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: calc(100% - 18px) 50%, calc(100% - 13px) 50%; background-size: 5px 5px, 5px 5px; background-repeat: no-repeat; padding-right: 32px; text-overflow: ellipsis; }

.hint { margin: 0; font-size: 12px; color: var(--faint); line-height: 1.45; }
.hint code { background: var(--surface-2); }

.input-row { display: flex; gap: 8px; align-items: stretch; }
.input-row input, .input-row select { flex: 1 1 auto; }
.input-row .icon-btn { flex: 0 0 auto; }

.grid-3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }

/* -------------------------------------------------------------- buttons -- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 40px; padding: 0 16px;
  border-radius: var(--r-control);
  border: 1px solid transparent;
  font-weight: 600; font-size: 14px; white-space: nowrap;
  cursor: pointer;
  transition: transform 0.1s ease, background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}
.btn:active { transform: translateY(1px); }
.btn svg { width: 16px; height: 16px; }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; }

.btn-primary { background: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent-strong); }

.btn-ghost { background: var(--surface); border-color: var(--border); color: var(--text); }
.btn-ghost:hover { border-color: var(--border-strong); background: var(--surface-2); }

.btn-compact { height: 40px; padding: 0 12px; font-size: 13px; }

.link-btn {
  background: none; border: none; padding: 0;
  color: var(--muted); font-size: 12.5px; font-weight: 600;
  cursor: pointer; text-decoration: underline; text-underline-offset: 3px;
}
.link-btn:hover { color: var(--accent-strong); }

/* --------------------------------------------------------------- stage --- */
.stage {
  flex: 1 1 auto;
  width: min(1200px, 100%);
  margin: 0 auto;
  padding: clamp(18px, 3vw, 32px) clamp(14px, 3vw, 26px) 24px;
  display: flex;
}
.output { flex: 1 1 auto; display: flex; flex-direction: column; }

/* Empty + status cards */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-panel);
  box-shadow: var(--shadow);
}

.empty {
  display: grid;
  grid-template-columns: minmax(0, 1.15fr) minmax(0, 0.85fr);
  gap: clamp(20px, 4vw, 48px);
  align-items: center;
  padding: clamp(24px, 4vw, 44px);
  margin: auto;
  width: 100%;
}
.empty-copy h1 {
  margin: 0 0 10px;
  font-size: clamp(26px, 3.4vw, 40px);
  line-height: 1.04;
  letter-spacing: -0.025em;
  font-weight: 600;
  max-width: 15ch;
}
.empty-copy p { margin: 0 0 18px; color: var(--muted); max-width: 46ch; }
.steps { list-style: none; margin: 0; padding: 0; display: grid; gap: 12px; }
.steps li { display: flex; gap: 12px; align-items: baseline; }
.step-n {
  font-family: var(--font-mono); font-size: 12px; font-weight: 600;
  color: var(--accent-strong);
  min-width: 26px;
}
.step-t { color: var(--muted); }
.step-t b { color: var(--text); font-weight: 600; }

.empty-visual {
  position: relative;
  aspect-ratio: 4 / 3;
  border-radius: var(--r-panel);
  border: 1px solid var(--border);
  background:
    linear-gradient(var(--border) 1px, transparent 1px) 0 0 / 100% 38px,
    linear-gradient(90deg, var(--border) 1px, transparent 1px) 0 0 / 38px 100%,
    var(--surface-2);
  overflow: hidden;
}
.empty-visual::after {
  content: "";
  position: absolute; inset: 0;
  background: radial-gradient(70% 70% at 50% 45%, var(--accent-soft), transparent 70%);
}
.empty-visual .reel {
  position: absolute; inset: 50% auto auto 50%;
  transform: translate(-50%, -50%);
  width: 62px; height: 62px; border-radius: 50%;
  border: 1.5px solid var(--border-strong);
  display: grid; place-items: center;
  color: var(--accent-strong);
  background: color-mix(in srgb, var(--surface) 70%, transparent);
}
.empty-visual .reel svg { width: 24px; height: 24px; stroke-width: 1.5; }

/* status card */
.status { padding: clamp(20px, 3vw, 30px); }
.status-head { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.status-title { font-size: 18px; font-weight: 600; letter-spacing: -0.01em; margin: 0; }
.status-sub { color: var(--muted); font-size: 13px; margin: 2px 0 0; font-family: var(--font-mono); }

.pill {
  display: inline-flex; align-items: center; gap: 7px;
  height: 26px; padding: 0 11px; border-radius: 999px;
  font-size: 12px; font-weight: 600; font-family: var(--font-mono);
  border: 1px solid var(--border);
  background: var(--surface-2); color: var(--muted);
}
.pill .dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.pill[data-state="running"], .pill[data-state="submitted"] { color: var(--accent-strong); background: var(--accent-soft); border-color: transparent; }
.pill[data-state="queued"] { color: var(--muted); }
.pill[data-state="succeeded"] { color: var(--ok); background: var(--ok-soft); border-color: transparent; }
.pill[data-state="failed"], .pill[data-state="expired"], .pill[data-state="canceled"] { color: var(--danger); background: var(--danger-soft); border-color: transparent; }
.pill[data-state="running"] .dot, .pill[data-state="submitted"] .dot { animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.3; } }

.progress {
  margin: 20px 0 6px;
  height: 8px; border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
  border: 1px solid var(--border);
}
.progress-bar {
  height: 100%; width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--accent-strong), var(--accent));
  transition: width 0.5s cubic-bezier(0.16, 1, 0.3, 1);
}
.progress.is-indeterminate .progress-bar {
  width: 35%;
  animation: slide 1.5s ease-in-out infinite;
}
@keyframes slide { 0% { margin-left: -35%; } 100% { margin-left: 100%; } }

.status-meta { display: flex; gap: 22px; flex-wrap: wrap; margin-top: 18px; }
.meta-item { display: flex; flex-direction: column; gap: 3px; }
.meta-k { font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--faint); }
.meta-v { font-family: var(--font-mono); font-size: 13px; color: var(--text); word-break: break-all; }

.prompt-echo {
  margin-top: 18px; padding: 12px 14px;
  border-left: 2px solid var(--accent);
  background: var(--surface-2);
  border-radius: 0 var(--r-control) var(--r-control) 0;
  color: var(--muted); font-size: 13.5px;
  max-height: 5.2em; overflow: auto;
}

/* result */
.result { padding: clamp(16px, 2.4vw, 22px); display: grid; gap: 16px; }
.result-media {
  position: relative;
  background: #000;
  border-radius: var(--r-panel);
  overflow: hidden;
  border: 1px solid var(--border);
  aspect-ratio: 16 / 9;
  display: grid; place-items: center;
}
.result-media video { width: 100%; height: 100%; object-fit: contain; display: block; }
.result-media .media-msg { color: var(--muted); font-family: var(--font-mono); font-size: 13px; padding: 20px; text-align: center; }

.result-bar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.result-bar .spacer { flex: 1 1 auto; }
.result-facts { display: flex; gap: 20px; flex-wrap: wrap; }
.result-facts .meta-v { color: var(--text); }

/* error */
.error-card { padding: clamp(20px, 3vw, 30px); border-left: 3px solid var(--danger); }
.error-card .status-title { color: var(--danger); }
.error-body { margin: 14px 0 0; color: var(--muted); font-size: 14px; }
.error-body code { color: var(--text); }
.error-actions { margin-top: 20px; display: flex; gap: 10px; flex-wrap: wrap; }

/* spinner (skeleton-ish, avoids generic circle-only look by pairing with text) */
.inline-spinner {
  width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent-strong);
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ------------------------------------------------------------ composer --- */
.composer {
  position: sticky;
  bottom: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(16px);
  border-top: 1px solid var(--border);
  padding: 12px clamp(14px, 3vw, 26px) max(12px, env(safe-area-inset-bottom));
}
.composer-inner {
  width: min(1200px, 100%);
  margin: 0 auto;
  display: grid;
  gap: 10px;
}
.composer-top { position: relative; }
#prompt { padding-bottom: 30px; }
.prompt-meta {
  position: absolute; right: 10px; bottom: 8px; left: 12px;
  display: flex; align-items: center; justify-content: space-between;
  pointer-events: none;
}
.prompt-meta .link-btn { pointer-events: auto; }
.char-count { font-family: var(--font-mono); font-size: 11px; color: var(--faint); }

.advanced {
  display: grid;
  gap: 14px;
  padding: 16px;
  max-height: 60vh;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--r-panel);
  animation: drop 0.18s ease;
}

.switch { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch-track {
  position: relative; width: 40px; height: 22px; flex: 0 0 auto;
  border-radius: 999px; background: var(--surface-2);
  border: 1px solid var(--border); transition: background 0.15s ease, border-color 0.15s ease;
}
.switch-track::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 16px; height: 16px; border-radius: 50%;
  background: var(--muted); transition: transform 0.18s cubic-bezier(0.16, 1, 0.3, 1), background 0.15s ease;
}
.switch input:checked + .switch-track { background: var(--accent-soft); border-color: var(--accent); }
.switch input:checked + .switch-track::after { transform: translateX(18px); background: var(--accent-strong); }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--accent); outline-offset: 2px; }
.switch-label { font-size: 13px; color: var(--muted); }

.composer-actions { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.composer-hint { font-size: 12.5px; color: var(--faint); font-family: var(--font-mono); }
.composer-hint.warn { color: var(--danger); }
.composer-buttons { margin-left: auto; display: flex; gap: 10px; }

/* ----------------------------------------------------------- responsive -- */
@media (max-width: 860px) {
  .key-panel { grid-template-columns: 1fr; }
  .empty { grid-template-columns: 1fr; }
  .empty-visual { order: -1; aspect-ratio: 16 / 9; }
  .grid-3 { grid-template-columns: 1fr; }
}

@media (max-width: 520px) {
  .brand-sub { display: none; }
  #key-status { max-width: 90px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .composer-hint { display: none; }
  .composer-buttons { margin-left: 0; width: 100%; }
  .composer-buttons .btn { flex: 1 1 auto; }
}

/* -------------------------------------------------------- reduced motion -- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001ms !important; animation-iteration-count: 1 !important; transition-duration: 0.001ms !important; }
}
#batch { width: min(880px, 100%); margin-inline: auto; }
#batch .field, #batch .panel { margin-block: 16px; }
#batch textarea { width: 100%; resize: vertical; }
#batch .panel { padding: 18px; }
#batch video { display: block; width: min(100%, 460px); max-height: 55vh; margin-block: 12px; }
#batch a { margin-inline-end: 14px; color: var(--text); }
#batch [role="status"] { overflow-wrap: anywhere; }
[hidden] { display: none !important; }
