/* Flow Studio — dark, touch-first, Flow-like. */
:root {
  --bg: #0b0b0d;
  --bg-soft: #121216;
  --card: #17171c;
  --card-2: #202028;
  --line: #2b2b35;
  --text: #f3f3f6;
  --muted: #9a9aa8;
  --accent: #8b7cf6;
  --accent-ink: #14121f;
  --green: #4ade80;
  --amber: #fbbf24;
  --red: #f87171;
  --radius: 18px;
  --tap: 44px;
  --inset-b: env(safe-area-inset-bottom, 0px);
  --inset-l: env(safe-area-inset-left, 0px);
  --inset-r: env(safe-area-inset-right, 0px);
  color-scheme: dark;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, system-ui, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
.sprite { position: absolute; width: 0; height: 0; overflow: hidden; }
.i { width: 18px; height: 18px; fill: currentColor; flex: none; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* Buttons ------------------------------------------------------------- */
.btn {
  font: inherit;
  color: var(--text);
  background: var(--card-2);
  border: 1px solid var(--line);
  border-radius: 12px;
  min-height: var(--tap);
  padding: 0 16px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  touch-action: manipulation;
  text-decoration: none;
}
.btn:disabled { opacity: .45; cursor: default; }
.btn:active:not(:disabled) { transform: scale(.98); }
.btn.primary { background: var(--accent); color: #fff; border-color: transparent; font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.small { min-height: var(--tap); padding: 0 14px; font-size: 14px; }
.btn.block { width: 100%; }
.btn.icon { width: var(--tap); padding: 0; }
.btn.chip { min-height: var(--tap); padding: 0 14px; font-size: 14px; border-radius: 999px; white-space: nowrap; }
button, select, input, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Login --------------------------------------------------------------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.login-card {
  width: min(420px, 100%);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 24px;
  padding: 28px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.login-brand { display: flex; align-items: center; gap: 10px; }
.login-brand .i { width: 26px; height: 26px; color: var(--accent); }
.login-brand h1 { font-size: 22px; margin: 0; letter-spacing: -.01em; }
.login-field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.login-field input {
  min-height: var(--tap);
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0 14px;
  color: var(--text);
  font-size: 16px; /* no Safari zoom */
}
.form-error { color: var(--red); font-size: 14px; margin: 0; }

/* Shell --------------------------------------------------------------- */
.shell {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding-left: var(--inset-l);
  padding-right: var(--inset-r);
}
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  padding-top: calc(10px + env(safe-area-inset-top, 0px));
  border-bottom: 1px solid var(--line);
  background: var(--bg);
  flex: none;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; letter-spacing: -.01em; white-space: nowrap; flex: none; }
.topbar .btn { flex: none; white-space: nowrap; }
.brand .i { color: var(--accent); }
.agent-pill {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  max-width: 58vw;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--card);
  border: 1px solid var(--line);
  font-size: 13px;
  color: var(--muted);
}
.agent-pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.agent-pill[data-state="online"] { color: #d9f7e4; border-color: #21543a; background: #10261b; }
.agent-pill[data-state="online"] .dot { background: var(--green); box-shadow: 0 0 8px var(--green); }
.agent-pill[data-state="warn"] { color: #fde9b8; border-color: #5b4415; background: #2a2010; }
.agent-pill[data-state="warn"] .dot { background: var(--amber); }
.agent-pill[data-state="offline"] .dot { background: #6b6b78; }
.agent-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conn {
  margin: 0;
  padding: 6px 14px;
  font-size: 13px;
  color: var(--amber);
  background: #241d0d;
  border-bottom: 1px solid #4a3b13;
}

/* Grid ---------------------------------------------------------------- */
.grid-wrap {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 14px;
}
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 240px), 1fr));
  gap: 14px;
  align-items: start;
}
.empty { color: var(--muted); text-align: center; margin: 40px 0; }
.load-more { display: flex; justify-content: center; margin: 16px 0 8px; }
.grid-end { height: 8px; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.card[data-status="running"] { border-color: #3b3660; }
.card[data-status="failed"] { border-color: #5a2b2b; }
.card-media {
  position: relative;
  background: #000;
  aspect-ratio: 16 / 9;
  display: grid;
  place-items: center;
  overflow: hidden;
}
.card-media video, .card-media img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  background: #000;
}
.media-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  padding: 16px;
}
.card-media img { cursor: zoom-in; }
.media-tools { position: absolute; top: 8px; right: 8px; display: flex; gap: 6px; }
.media-tools .btn.tool {
  width: var(--tap);
  height: var(--tap);
  min-height: var(--tap);
  padding: 0;
  border-radius: 13px;
  background: rgba(10, 10, 14, .62);
  border: 1px solid rgba(255, 255, 255, .18);
  color: #fff;
  backdrop-filter: blur(6px);
}
.media-count {
  position: absolute;
  top: 8px;
  left: 8px;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(10, 10, 14, .62);
  font-size: 12px;
  color: #fff;
}

.card-body { padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.card-top { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 12px; color: var(--muted); }
.who { margin-left: auto; white-space: nowrap; }
.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  background: var(--card-2);
  color: var(--muted);
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge[data-status="queued"] { color: #b9b3ff; background: #201d33; }
.badge[data-status="running"] { color: #cfc8ff; background: #251f3d; }
.badge[data-status="running"]::before { animation: pulse 1.1s ease-in-out infinite; }
.badge[data-status="done"] { color: #9ef0bb; background: #12291d; }
.badge[data-status="failed"] { color: #ffb4b4; background: #2c1414; }
.badge[data-status="cancelled"] { color: #cfcfd6; background: #24242c; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .25; } }

.progress, .job-error {
  margin: 0;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
}
.job-error { color: #ffb4b4; }
.spinner {
  width: 13px;
  height: 13px;
  border-radius: 50%;
  border: 2px solid #4b4470;
  border-top-color: #cfc8ff;
  animation: spin .8s linear infinite;
  flex: none;
}
@keyframes spin { to { transform: rotate(360deg); } }

.prompt {
  margin: 0;
  font-size: 14px;
  color: #e6e6ec;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 3;
  overflow: hidden;
  cursor: pointer;
  text-align: left;
  background: none;
  border: 0;
  padding: 0;
  min-height: var(--tap);
}
.prompt.is-open { -webkit-line-clamp: unset; overflow: visible; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip-tag {
  font-size: 11px;
  color: var(--muted);
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px 8px;
  white-space: nowrap;
}
.actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 2px; }
.actions .btn { min-height: var(--tap); padding: 0 12px; font-size: 13px; border-radius: 11px; }
.actions .btn.danger { color: #ffb4b4; }

/* Composer ------------------------------------------------------------ */
.composer {
  flex: none;
  border-top: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(11, 11, 13, .92), var(--bg) 40%);
  padding: 10px 12px calc(10px + var(--inset-b));
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.composer-top { display: flex; align-items: center; gap: 8px; }
.segmented { display: inline-flex; background: var(--card-2); border: 1px solid var(--line); border-radius: 999px; padding: 3px; }
.seg {
  border: 0;
  background: none;
  color: var(--muted);
  min-height: var(--tap);
  padding: 0 18px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 14px;
}
.seg.is-active { background: var(--accent); color: #fff; font-weight: 600; }
.attach-buttons { display: flex; gap: 6px; margin-left: auto; overflow-x: auto; scrollbar-width: none; }
.attach-buttons::-webkit-scrollbar { display: none; }
#prompt {
  width: 100%;
  resize: none;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 10px 12px;
  font-size: 16px;
  min-height: 58px;
  max-height: 160px;
  line-height: 1.4;
}
.options { display: flex; gap: 8px; align-items: flex-end; overflow-x: auto; padding-bottom: 2px; scrollbar-width: none; }
.options::-webkit-scrollbar { display: none; }
.field { display: flex; flex-direction: column; gap: 3px; font-size: 11px; color: var(--muted); flex: none; }
.field span { padding-left: 4px; white-space: nowrap; }
.field em { font-style: normal; opacity: .7; }
.field select, .field input {
  min-height: var(--tap);
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 11px;
  padding: 0 10px;
  font-size: 16px; /* 16px keeps iOS Safari from zooming the page on focus */
  color: var(--text);
  max-width: 200px;
}
/* WebKit ignores min-height on a native select and renders it 22px tall, so
   give it an explicit height and draw the chevron ourselves. */
.field select {
  height: var(--tap);
  -webkit-appearance: none;
  appearance: none;
  padding-right: 30px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5l5 5 5-5' fill='none' stroke='%239a9aa8' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 9px center;
  background-size: 12px 8px;
}
.field.narrow input { width: 84px; }
.field.is-muted { opacity: .5; }
.field select:disabled { color: var(--muted); }
.send { margin-left: auto; align-self: flex-end; min-height: var(--tap); padding: 0 18px; }
.attachments { display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; }
.attachments::-webkit-scrollbar { display: none; }
.thumb { position: relative; flex: none; width: 84px; }
.thumb img {
  width: 84px;
  height: 60px;
  object-fit: cover;
  border-radius: 10px;
  border: 1px solid var(--line);
  display: block;
  background: #000;
}
.thumb .thumb-label { font-size: 10px; color: var(--muted); display: block; text-align: center; margin-top: 2px; }
.thumb .thumb-remove {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 28px;
  height: 28px;
  min-height: 28px;
  border-radius: 50%;
  background: #2a2a33;
  border: 1px solid var(--line);
  color: #fff;
  display: grid;
  place-items: center;
  padding: 0;
  cursor: pointer;
}
/* keep the visible dot small but the touch area at least 44 px */
.thumb .thumb-remove::before { content: ""; position: absolute; inset: -8px; border-radius: 50%; }
.thumb .thumb-remove .i { width: 14px; height: 14px; }
.thumb.is-uploading { opacity: .55; }

/* Lightbox ------------------------------------------------------------ */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(5, 5, 7, .96);
  display: flex;
  flex-direction: column;
  padding: calc(env(safe-area-inset-top, 0px) + 8px) 12px calc(env(safe-area-inset-bottom, 0px) + 8px);
  gap: 10px;
}
.lightbox-bar { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.lightbox-count { font-size: 13px; color: var(--muted); }
.lightbox-stage { flex: 1; min-height: 0; display: grid; place-items: center; }
.lightbox-stage video, .lightbox-stage img { width: 100%; height: 100%; object-fit: contain; border-radius: 12px; background: #000; }
.lightbox-nav { display: flex; align-items: center; justify-content: center; gap: 16px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 24px);
  transform: translateX(-50%);
  z-index: 50;
  margin: 0;
  padding: 10px 16px;
  border-radius: 999px;
  background: #262631;
  border: 1px solid var(--line);
  font-size: 14px;
  max-width: 90vw;
}

/* Wide screens -------------------------------------------------------- */
@media (min-width: 900px) {
  .grid-wrap { padding: 18px 20px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }
  .composer { padding-left: 20px; padding-right: 20px; }
  .options { flex-wrap: wrap; overflow: visible; }
}
@media (min-width: 1280px) {
  /* width:100% keeps flex items from shrink-wrapping around their content */
  .composer, .topbar, .grid-wrap { width: 100%; max-width: 1500px; margin-left: auto; margin-right: auto; }
}

/* Small phones: wrap the option row so Generate stays reachable --------- */
@media (max-width: 620px) {
  .options { flex-wrap: wrap; overflow: visible; }
  .send { width: 100%; margin-left: 0; }
  .agent-pill { max-width: 42vw; }
}
