/* ---------- palette "Лайм" (Linear via styles.refero.design): near-black instrument, one acid-lime accent ---------- */
:root, [data-theme="dark"] {
  --bg: #08090a; --card: #0f1011; --well: #161718; --raised: #1d1e21; --border: #23252a; --border-strong: #383b3f;
  --text: #f7f8f8; --muted: #8a8f98; --faint: #62666d;
  --primary: #e4f222; --primary-text: #08090a; --primary-border: #e4f222; --accent-text: #e4f222;
  --ok: #4cc36a; --warn: #f2c94c; --danger: #eb5757; --info: #8b8ff6;
  --r-btn: 6px; --r-input: 6px; --r-card: 12px; --r-badge: 4px;
  --shadow-card: inset 0 0 0 1px var(--border);
  --shadow-pop: 0 12px 32px rgba(0, 0, 0, .55);
  --sel-bg: color-mix(in srgb, var(--primary) 16%, var(--card)); --sel-text: var(--accent-text);
}
[data-theme="light"] {
  --bg: #f4f5f6; --card: #ffffff; --well: #f4f5f6; --raised: #eceef0; --border: #e4e5e7; --border-strong: #cfd3da;
  --text: #0f1011; --muted: #62666d; --faint: #8a8f98;
  --primary: #e4f222; --primary-text: #08090a; --primary-border: #c3d000; --accent-text: #5c6300;
  --ok: #1f8a3b; --warn: #a87a00; --danger: #d93f3f; --info: #4f52d6;
  --shadow-card: 0 1px 2px rgba(15, 16, 17, .05), inset 0 0 0 1px var(--border);
  --shadow-pop: 0 12px 32px rgba(15, 16, 17, .16);
  --sel-bg: color-mix(in srgb, var(--primary) 55%, var(--card)); --sel-text: #2e3200;
}
/* ---------- base ---------- */
:root { color-scheme: light dark; }
[data-theme="dark"] { color-scheme: dark; }
[data-theme="light"] { color-scheme: light; }
* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
/* iOS inflates long text blocks on its own (text autosizing); the prompt highlight backdrop would then drift from the textarea. */
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  font: 14px/1.45 Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--text); -webkit-font-smoothing: antialiased;
}
button, select, textarea, input { font: inherit; color: inherit; }
[hidden] { display: none !important; }
::selection { background: color-mix(in srgb, var(--primary) 35%, transparent); }
:focus-visible { outline: 2px solid color-mix(in srgb, var(--primary) 70%, var(--text)); outline-offset: 2px; }
svg.i { width: 16px; height: 16px; flex: none; display: block; }

/* ---------- buttons ---------- */
button {
  cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  border: 1px solid var(--border-strong); background: var(--card); color: var(--text);
  border-radius: var(--r-btn); padding: 8px 14px; font-weight: 500; line-height: 1.2;
  transition: background .15s, border-color .15s, color .15s, transform .05s, filter .15s;
}
button:hover:not(:disabled) { background: var(--raised); }
button:active:not(:disabled) { transform: translateY(1px); }
button:disabled { opacity: .45; cursor: not-allowed; }
button.primary { background: var(--primary); color: var(--primary-text); border-color: var(--primary-border); font-weight: 600; }
button.primary:hover:not(:disabled) { background: var(--primary); filter: brightness(1.07) saturate(1.05); }
button.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
button.danger:hover:not(:disabled) { background: var(--danger); filter: brightness(1.08); }
button.ghost, button.link { background: transparent; border-color: transparent; color: var(--muted); }
button.ghost:hover:not(:disabled), button.link:hover:not(:disabled) { background: var(--raised); color: var(--text); }
button.link { padding: 4px 8px; font-size: 12.5px; }
.btn-small { padding: 6px 10px; font-size: 12.5px; border-radius: var(--r-btn); }
.btn-small.icon, button.icon { padding: 6px; aspect-ratio: 1; }

/* ---------- form ---------- */
select, textarea {
  width: 100%; background: var(--well); border: 1px solid var(--border); border-radius: var(--r-input);
  padding: 10px 12px; transition: border-color .15s, box-shadow .15s; outline: none;
}
select:focus, textarea:focus { border-color: color-mix(in srgb, var(--primary) 70%, var(--border-strong)); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 22%, transparent); }
textarea { resize: vertical; min-height: 150px; line-height: 1.5; }
textarea::placeholder { color: var(--faint); }
/* iOS zooms the page on focus when a field's text is smaller than 16px. */
@media (pointer: coarse) { textarea { font-size: 16px; } }
label.field, div.field { display: block; margin-top: 18px; }
.label, label.field > span {
  display: flex; align-items: baseline; justify-content: space-between; gap: 8px;
  font-size: 12.5px; font-weight: 500; color: var(--muted); margin-bottom: 7px;
}
.label b, .label .value { color: var(--text); font-weight: 600; font-variant-numeric: tabular-nums; }
.error { color: var(--danger); white-space: pre-wrap; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* Segmented control: few options side by side, the chosen one lit with the accent. */
.seg { display: flex; flex-wrap: wrap; gap: 3px; padding: 3px; background: var(--well); border: 1px solid var(--border); border-radius: calc(var(--r-btn) + 3px); }
.seg button {
  flex: 1 1 0; min-width: 0; border: 0; background: transparent; color: var(--muted); padding: 7px 10px;
  border-radius: var(--r-btn); font-size: 13px; font-weight: 500; white-space: nowrap;
}
.seg button:hover:not(:disabled) { background: var(--raised); color: var(--text); }
.seg button[aria-checked="true"], .seg button[aria-checked="true"]:hover {
  background: var(--sel-bg);
  color: var(--sel-text);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--primary) 55%, transparent);
}
.seg.stack button { flex-direction: column; gap: 5px; padding: 8px 4px 6px; font-size: 12px; }
.seg.disabled { opacity: .55; }
.seg button:disabled { opacity: 1; }
/* Aspect glyph: a rectangle in the frame's proportion. */
.ratio { display: block; border: 1.6px solid currentColor; border-radius: 3px; opacity: .9; }
.ratio.auto { border-style: dashed; }

/* Range: custom track filled up to the thumb via --fill (set by paintRange). */
input[type=range] { -webkit-appearance: none; appearance: none; width: 100%; height: 22px; background: transparent; margin: 0; cursor: pointer; }
input[type=range]::-webkit-slider-runnable-track {
  height: 6px; border-radius: 999px;
  background: linear-gradient(90deg, var(--primary) 0 var(--fill, 0%), var(--raised) var(--fill, 0%) 100%);
  box-shadow: inset 0 0 0 1px var(--border);
}
input[type=range]::-moz-range-track { height: 6px; border-radius: 999px; background: var(--raised); }
input[type=range]::-moz-range-progress { height: 6px; border-radius: 999px; background: var(--primary); }
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 18px; height: 18px; margin-top: -6px; border-radius: 50%;
  background: #fff; border: 2px solid var(--primary-border); box-shadow: 0 1px 4px rgba(0, 0, 0, .3); transition: transform .1s;
}
input[type=range]::-moz-range-thumb { width: 14px; height: 14px; border-radius: 50%; background: #fff; border: 2px solid var(--primary-border); }
input[type=range]:active::-webkit-slider-thumb { transform: scale(1.15); }
.range-scale { display: flex; justify-content: space-between; font-size: 11px; color: var(--faint); margin-top: 2px; }

/* ---------- layout ---------- */
/* Three zones the height of the window: form | selected result | history rail, each scrolls on its own. */
.layout { display: grid; grid-template-columns: minmax(320px, 400px) minmax(0, 1fr) 128px; gap: 16px; padding: 16px; height: 100vh; }
.layout > .panel { min-height: 0; }
@media (max-width: 900px) { .layout { grid-template-columns: minmax(0, 1fr); height: auto; padding: 12px; gap: 12px; } }
.panel { background: var(--card); border-radius: var(--r-card); box-shadow: var(--shadow-card); padding: 20px; }
.input { overflow: auto; scrollbar-width: thin; }
.title { display: flex; align-items: center; gap: 12px; margin-bottom: 4px; }
.title .logo {
  width: 36px; height: 36px; border-radius: calc(var(--r-btn) + 2px); display: grid; place-items: center; flex: none;
  background: var(--primary); color: var(--primary-text);
}
.title .logo svg { width: 18px; height: 18px; }
h1 { font-size: 17px; font-weight: 600; margin: 0; letter-spacing: -.01em; }
h1 + .muted, .title .muted { font-size: 12.5px; }
h2 { font-size: 16px; font-weight: 600; margin: 0; letter-spacing: -.01em; }
.counter { font-size: 11.5px; color: var(--faint); font-variant-numeric: tabular-nums; text-align: right; margin-top: 4px; }
.counter.over { color: var(--danger); font-weight: 600; }
textarea.over { border-color: var(--danger); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* Idea chips: one click fills the prompt and settings. */
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chips.one-line { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
.chips button { flex: none; }
.chips button { border-radius: 999px; padding: 6px 12px; font-size: 12.5px; background: var(--well); border-color: var(--border); color: var(--muted); }
.chips button:hover:not(:disabled) { color: var(--text); border-color: var(--border-strong); }

/* References / files */
.refs-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 7px; }
.refs-head .label { margin: 0; }
.drop {
  border: 1.5px dashed var(--border-strong); border-radius: var(--r-input); padding: 10px; background: var(--well);
  display: flex; flex-wrap: wrap; gap: 8px; align-items: flex-start; transition: border-color .15s, background .15s;
}
.drop.dragover { border-color: var(--primary-border); background: color-mix(in srgb, var(--primary) 8%, var(--well)); }
.thumbs { display: contents; }
.thumb, .add { width: 64px; height: 64px; border-radius: calc(var(--r-input) - 2px); position: relative; overflow: hidden; background: var(--raised); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb .x {
  position: absolute; top: 3px; right: 3px; width: 20px; height: 20px; padding: 0; border-radius: 50%; font-size: 13px; line-height: 1;
  background: rgba(0, 0, 0, .6); color: #fff; border: 0; opacity: 0; transition: opacity .15s;
}
.thumb:hover .x, .thumb .x:focus-visible { opacity: 1; }
@media (hover: none) { .thumb .x { opacity: 1; } }
.thumb .bar { position: absolute; left: 0; bottom: 0; height: 3px; background: var(--primary); }
.thumb .state { position: absolute; inset: auto 0 6px 0; text-align: center; font-size: 10px; color: #fff; text-shadow: 0 1px 2px #000; }
.thumb.uploading img { opacity: .5; }
.thumb.error { box-shadow: inset 0 0 0 2px var(--danger); }
.add {
  display: grid; place-items: center; color: var(--muted); cursor: pointer; background: var(--card);
  border: 1px solid var(--border-strong); transition: color .15s, border-color .15s;
}
.add:hover { color: var(--accent-text); border-color: var(--primary-border); }
.add svg { width: 22px; height: 22px; }
.add.disabled { opacity: .4; pointer-events: none; }
.hint { flex-basis: 100%; font-size: 11.5px; color: var(--faint); line-height: 1.4; }

/* Price + actions */
.summary {
  margin-top: 20px; padding: 12px 14px; border-radius: var(--r-input); background: var(--well); border: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center; gap: 10px;
}
.summary .label { margin: 0; }
.price { text-align: right; font-variant-numeric: tabular-nums; }
.price .formula { display: block; font-size: 11.5px; color: var(--faint); }
.price .total { font-size: 16px; font-weight: 650; }
.price .rub { color: var(--muted); font-weight: 500; margin-left: 4px; }
.actions { display: flex; gap: 8px; margin-top: 12px; }
.actions .primary { flex: 1; padding: 12px 16px; font-size: 14.5px; }
#formError:not(:empty) { margin-top: 10px; font-size: 12.5px; }

/* ---------- stage: the selected result large ---------- */
.stage { display: flex; flex-direction: column; padding: 16px; }
.stage-inner { flex: 1; min-height: 0; display: flex; flex-direction: column; gap: 14px; }
.stage-media {
  position: relative; flex: 1; min-height: 240px; border-radius: calc(var(--r-card) - 4px); background: var(--well); overflow: hidden;
  display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 13px; text-align: center;
}
.stage-media > img, .stage-media > video { width: 100%; height: 100%; object-fit: contain; display: block; }
.stage-inner.enter .stage-media > * { animation: stage-in .45s cubic-bezier(.16, 1, .3, 1); }
@keyframes stage-in { from { opacity: 0; transform: scale(.965); } }
@media (prefers-reduced-motion: reduce) { .stage-inner.enter .stage-media > * { animation: none; } }
.note { display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 16px; max-height: 100%; overflow: auto; }
.note svg { width: 22px; height: 22px; }
.note.error { color: var(--danger); }
.note .error-code { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 12px; }
.note .error-code b { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; color: var(--text); user-select: all; }
.stage-info { flex: none; display: flex; flex-direction: column; gap: 8px; }
.stage-line { display: flex; align-items: center; gap: 10px; min-width: 0; }
/* One line whatever the parameters: the rest is cut with an ellipsis (full text in the tooltip). */
.stage-line .meta { font-size: 12px; color: var(--faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.stage .prompt { font-size: 13.5px; line-height: 1.5; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.foot { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.foot .push { margin-left: auto; }
@media (max-width: 900px) { .stage-media { flex: none; height: 60vh; } }

/* ---------- rail: history thumbnails, click puts one on the stage ---------- */
.rail { display: flex; flex-direction: column; gap: 10px; padding: 12px 10px; }
.rail-head { display: flex; align-items: center; justify-content: space-between; min-height: 28px; padding-left: 2px; }
.rail-head .label { margin: 0; }
.rail-scroll { flex: 1; min-height: 0; overflow-y: auto; scrollbar-width: none; display: flex; flex-direction: column; gap: 8px; }
.rail-list { display: flex; flex-direction: column; gap: 8px; }
.tile {
  position: relative; flex: none; width: 100%; aspect-ratio: 1; padding: 0; border: 0; border-radius: calc(var(--r-card) - 4px);
  overflow: hidden; background: var(--raised); color: var(--faint);
}
.tile:hover:not(:disabled) { background: var(--raised); }
.tile > img, .tile > video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; pointer-events: none; opacity: .72; transition: opacity .2s, transform .4s cubic-bezier(.2, .8, .2, 1); }
.tile:hover > img, .tile:hover > video { opacity: 1; transform: scale(1.06); }
.tile.selected > img, .tile.selected > video { opacity: 1; }
/* Ring drawn above the picture, so it is visible on any image. */
.tile::after { content: ""; position: absolute; inset: 0; border-radius: inherit; box-shadow: inset 0 0 0 1px var(--border); pointer-events: none; transition: box-shadow .15s; }
.tile.selected::after { box-shadow: inset 0 0 0 2px var(--primary); }
.tile[data-status="failed"] { color: var(--danger); }
.tile .dot { position: absolute; top: 6px; left: 6px; z-index: 1; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); box-shadow: 0 0 0 2px rgba(0, 0, 0, .45); }
.tile .dot.running, .tile .dot.saving { background: var(--primary); animation: pulse 1.2s ease-in-out infinite; }
.tile .dot.completed { display: none; }
.tile .dot.failed { background: var(--danger); }
.tile .dot.cancelled { background: var(--faint); }
.tile.pending, .stage-media.pending { background: linear-gradient(100deg, var(--raised) 30%, color-mix(in srgb, var(--raised) 55%, var(--card)) 50%, var(--raised) 70%) 0 0 / 300% 100%; animation: shimmer 1.8s linear infinite; }
#more { width: 100%; flex: none; }
@media (max-width: 900px) {
  .rail-scroll, .rail-list { flex-direction: row; }
  .rail-scroll { overflow: auto hidden; }
  .tile { width: 84px; }
  #more { width: auto; }
}

@keyframes shimmer { from { background-position: 100% 0; } to { background-position: -50% 0; } }
.spinner { width: 22px; height: 22px; border-radius: 50%; border: 2px solid color-mix(in srgb, var(--muted) 30%, transparent); border-top-color: var(--accent-text); animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.progress { position: absolute; left: 0; right: 0; bottom: 0; height: 3px; background: color-mix(in srgb, var(--muted) 18%, transparent); z-index: 1; }
.progress > div { height: 100%; background: var(--primary); transition: width .5s linear; }
.progress.indeterminate { overflow: hidden; }
.progress.indeterminate > div { width: 30% !important; animation: slide 1.4s ease-in-out infinite; transition: none; }
@keyframes slide { from { transform: translateX(-100%); } to { transform: translateX(340%); } }

/* Status badge: pill with a dot, colour by status. */
.badge {
  --c: var(--muted);
  display: inline-flex; align-items: center; gap: 6px; padding: 3px 9px; border-radius: var(--r-badge);
  font-size: 11.5px; font-weight: 600; line-height: 1.3; white-space: nowrap; font-variant-numeric: tabular-nums;
  color: var(--c); background: color-mix(in srgb, var(--c) 14%, color-mix(in srgb, var(--card) 85%, transparent));
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--c) 32%, transparent);
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; flex: none; }
.badge.running, .badge.saving { --c: var(--accent-text); }
.badge.running::before, .badge.saving::before { animation: pulse 1.2s ease-in-out infinite; }
.badge.completed { --c: var(--ok); }
.badge.failed { --c: var(--danger); }
.badge.cancelled { --c: var(--faint); }
@keyframes pulse { 50% { opacity: .25; } }

.empty {
  flex: 1; padding: 48px 20px; justify-content: center; border: 1.5px dashed var(--border-strong); border-radius: var(--r-card);
  display: flex; flex-direction: column; align-items: center; gap: 10px; text-align: center; color: var(--muted);
}
.empty svg { width: 32px; height: 32px; color: var(--faint); }
.fatal { margin: 15vh auto; max-width: 440px; padding: 24px; text-align: center; background: var(--card); border-radius: var(--r-card); box-shadow: var(--shadow-card); }

/* ---------- dialogs ---------- */
.confirm { border: 0; border-radius: var(--r-card); background: var(--card); color: var(--text); box-shadow: var(--shadow-pop), var(--shadow-card); max-width: 420px; padding: 22px 22px 18px; }
.confirm::backdrop { background: rgba(0, 0, 0, .5); backdrop-filter: blur(2px); }
.confirm p { margin: 0 0 8px; }
.confirm .actions { justify-content: flex-end; margin-top: 16px; }
.confirm .actions .primary, .confirm .actions .danger { flex: none; }
