:root {
  --bg: #0e0f13;
  --panel: #16181f;
  --panel-2: #1d2029;
  --line: #2a2e3a;
  --text: #e8e9ee;
  --muted: #8b90a0;
  --accent: #7c5cff;
  --accent-2: #9b83ff;
  --danger: #ff5c7a;
  --ok: #3ecf8e;
  --radius: 12px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
h1, h2 { margin: 0; font-weight: 650; }
h1 { font-size: 17px; }
h2 { font-size: 16px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); min-height: 1em; margin: 8px 0 0; white-space: pre-wrap; }
.error-inline { color: var(--danger); }
.block { display: block; margin: -6px 0 14px; }
.row { display: flex; align-items: center; gap: 8px; }
.gap-s { gap: 6px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

input, select, textarea {
  width: 100%;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 9px;
  padding: 9px 11px;
  font: inherit;
  outline: none;
  transition: border-color .15s;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
textarea { resize: vertical; min-height: 44px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 9px;
  padding: 8px 14px; font: inherit; cursor: pointer; text-decoration: none;
  transition: background .15s, border-color .15s, opacity .15s;
  white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover { background: var(--accent-2); }
.btn.primary:disabled { opacity: .6; cursor: progress; }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 12px; }
.btn.icon { padding: 8px 11px; flex: none; }
.btn.big { width: 100%; padding: 13px; font-size: 15px; margin-top: 6px; }

/* ---------- login ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(360px, 100%); background: var(--panel); border: 1px solid var(--line);
  border-radius: 16px; padding: 28px; display: grid; gap: 12px;
}

.user-buttons { display: grid; gap: 10px; margin-top: 6px; }
.user-buttons .btn { width: 100%; padding: 14px; font-size: 16px; font-weight: 600; }
.user-buttons .btn:hover { background: var(--accent); border-color: var(--accent); color: #fff; }
.linklike {
  background: none; border: 0; padding: 0; color: var(--accent-2); cursor: pointer; font: inherit; font-size: 12px;
}
.linklike:hover { text-decoration: underline; }

/* ---------- layout ---------- */
.app { display: grid; grid-template-columns: 420px 1fr; min-height: 100%; }
.panel {
  background: var(--panel); border-right: 1px solid var(--line);
  padding: 18px; overflow-y: auto; height: 100vh; position: sticky; top: 0;
}
.results { padding: 18px 22px; min-width: 0; }

.brand { display: flex; gap: 12px; align-items: center; margin-bottom: 16px; }
.brand-text { flex: 1; min-width: 0; }
.brand-user {
  display: grid; justify-items: end; gap: 1px; text-align: right; flex: none;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 5px 10px;
}
.brand-user b { font-size: 13px; }
.seg.compact { padding: 2px; }
.seg.compact button { padding: 4px 10px; font-size: 12px; flex: none; }
.logo {
  width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center;
  background: linear-gradient(135deg, var(--accent), #ff7ac6); font-weight: 800; font-size: 20px;
}

.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field > span, .field-head > span { font-weight: 550; font-size: 13px; }
.field-head { display: flex; justify-content: space-between; align-items: center; }

.section {
  border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; margin-bottom: 14px;
  background: rgba(255,255,255,.015);
}
.section > summary { cursor: pointer; font-weight: 600; list-style: none; display: flex; align-items: center; gap: 8px; }
.section > summary::before { content: "▸"; color: var(--muted); transition: transform .15s; }
.section[open] > summary::before { transform: rotate(90deg); }
.section[open] > summary { margin-bottom: 12px; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 99px; background: var(--panel-2); color: var(--muted); font-weight: 500; }
.badge.ok { color: var(--ok); }
.badge.warn { color: var(--danger); }

/* segmented */
.seg { display: flex; background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 3px; gap: 3px; }
.seg button {
  flex: 1; background: transparent; border: 0; color: var(--muted); padding: 7px 6px;
  border-radius: 7px; cursor: pointer; font: inherit; font-weight: 550;
}
.seg button.active { background: var(--accent); color: #fff; }
.ratios { display: flex; flex-wrap: wrap; gap: 6px; }
.ratios button {
  background: var(--panel-2); border: 1px solid var(--line); color: var(--muted);
  border-radius: 8px; padding: 5px 9px; cursor: pointer; font: inherit; font-size: 12px;
}
.ratios button.active { border-color: var(--accent); color: var(--text); }
#customSize input { width: 110px; }

/* dropzone */
.dropzone {
  border: 1.5px dashed var(--line); border-radius: var(--radius); padding: 18px; text-align: center;
  cursor: pointer; transition: border-color .15s, background .15s; color: var(--muted);
}
.dropzone:hover, .dropzone.over { border-color: var(--accent); background: rgba(124,92,255,.07); color: var(--text); }
.dropzone.full { opacity: .5; pointer-events: none; }
.refs { display: grid; grid-template-columns: repeat(auto-fill, minmax(72px, 1fr)); gap: 8px; }
.ref {
  position: relative; aspect-ratio: 1; border-radius: 10px; overflow: hidden;
  border: 1px solid var(--line); background: var(--panel-2); cursor: grab;
}
.ref.dragging { opacity: .4; }
.ref img { width: 100%; height: 100%; object-fit: cover; display: block; }
.ref .num {
  position: absolute; left: 4px; top: 4px; background: rgba(0,0,0,.7); color: #fff;
  font-size: 11px; font-weight: 700; border-radius: 6px; padding: 1px 6px;
}
.ref .del {
  position: absolute; right: 4px; top: 4px; width: 22px; height: 22px; border-radius: 50%;
  border: 0; background: rgba(0,0,0,.7); color: #fff; cursor: pointer; line-height: 22px; padding: 0;
}
.ref .meta {
  position: absolute; left: 0; right: 0; bottom: 0; font-size: 10px; padding: 2px 4px;
  background: linear-gradient(transparent, rgba(0,0,0,.8)); color: #ddd; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.ref.loading::after {
  content: ""; position: absolute; inset: 0; background: rgba(0,0,0,.5) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 50 50'%3E%3Ccircle cx='25' cy='25' r='18' fill='none' stroke='white' stroke-width='4' stroke-dasharray='80 40'%3E%3CanimateTransform attributeName='transform' type='rotate' from='0 25 25' to='360 25 25' dur='1s' repeatCount='indefinite'/%3E%3C/circle%3E%3C/svg%3E") center/28px no-repeat;
}

/* toggles */
.toggles { display: grid; gap: 10px; margin-bottom: 14px; }
.toggle { display: flex; align-items: center; gap: 10px; cursor: pointer; margin-bottom: 8px; }
.toggle input { display: none; }
.switch {
  width: 36px; height: 20px; border-radius: 99px; background: var(--line); position: relative; flex: none; transition: background .15s;
}
.switch::after {
  content: ""; position: absolute; left: 2px; top: 2px; width: 16px; height: 16px; border-radius: 50%;
  background: #fff; transition: transform .15s;
}
.toggle input:checked + .switch { background: var(--accent); }
.toggle input:checked + .switch::after { transform: translateX(16px); }

.preview summary { cursor: pointer; color: var(--muted); font-size: 12px; }
.preview pre {
  background: #0a0b0e; border: 1px solid var(--line); border-radius: 8px; padding: 10px;
  font-size: 11px; max-height: 260px; overflow: auto; white-space: pre-wrap; word-break: break-all;
}

/* sticky generate bar */
.gen-bar {
  position: sticky; bottom: 0; z-index: 5;
  margin: 8px -18px 0; padding: 12px 18px 14px;
  background: var(--panel); border-top: 1px solid var(--line);
  box-shadow: 0 -10px 24px rgba(0,0,0,.35);
}
.gen-bar .btn.big { margin-top: 0; }
.gen-bar .error { margin: 0 0 8px; }
.gen-bar .error:empty { display: none; }
.gen-meta { display: flex; justify-content: space-between; gap: 10px; margin-top: 7px; font-size: 12px; color: var(--muted); }
.gen-meta b { color: var(--text); font-weight: 600; }

.spent {
  font-size: 12px; background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 99px; padding: 4px 11px; cursor: help;
}
.spent b { color: var(--ok); }

/* results */
.results-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }
.jobs { display: grid; gap: 16px; }
.empty { padding: 60px 0; text-align: center; }
.job { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 14px; }
.job-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; margin-bottom: 10px; }
.job-prompt { font-size: 13px; white-space: pre-wrap; word-break: break-word; max-height: 4.4em; overflow: hidden; cursor: pointer; }
.job-prompt.expanded { max-height: none; }
.job-meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.chip { font-size: 11px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 99px; padding: 2px 8px; color: var(--muted); }
.chip.running { color: var(--accent-2); border-color: var(--accent); }
.chip.done { color: var(--ok); }
.chip.error, .chip.partial { color: var(--danger); }
.chip.cost { color: var(--ok); }
.chip.user { color: var(--text); border-color: var(--accent); }
.job-actions { display: flex; gap: 6px; flex: none; }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px; }
.tile {
  position: relative; border-radius: 10px; overflow: hidden; background: var(--panel-2);
  border: 1px solid var(--line); aspect-ratio: 1; cursor: zoom-in;
}
.tile img { width: 100%; height: 100%; object-fit: contain; display: block; background: #0a0b0e; }
.tile.placeholder { display: grid; place-items: center; cursor: default; }
.tile.placeholder::after {
  content: ""; width: 34px; height: 34px; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--accent); animation: spin 1s linear infinite;
}
.tile .tile-actions {
  position: absolute; right: 6px; bottom: 6px; display: flex; gap: 4px; opacity: 0; transition: opacity .15s;
}
.tile:hover .tile-actions { opacity: 1; }
.tile .tile-actions a, .tile .tile-actions button {
  background: rgba(0,0,0,.75); color: #fff; border: 0; border-radius: 7px; padding: 4px 8px; font-size: 11px; cursor: pointer; text-decoration: none;
}
.job-errors { margin-top: 10px; color: var(--danger); font-size: 12px; white-space: pre-wrap; word-break: break-word; }
@keyframes spin { to { transform: rotate(360deg); } }

/* lightbox */
.lightbox {
  position: fixed; inset: 0; background: rgba(5,6,8,.94); z-index: 50;
  display: grid; place-items: center; padding: 50px 60px 80px;
}
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 8px; }
.lb-close, .lb-nav {
  position: absolute; background: rgba(255,255,255,.08); color: #fff; border: 0; cursor: pointer;
  border-radius: 50%; width: 42px; height: 42px; font-size: 20px;
}
.lb-close { top: 14px; right: 14px; }
.lb-nav { top: 50%; transform: translateY(-50%); font-size: 30px; }
.lb-nav.prev { left: 12px; }
.lb-nav.next { right: 12px; }
.lb-bar { position: absolute; bottom: 18px; left: 0; right: 0; display: flex; justify-content: center; align-items: center; gap: 10px; flex-wrap: wrap; padding: 0 16px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 60;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 16px;
  box-shadow: 0 8px 30px rgba(0,0,0,.4);
}

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .panel { height: auto; position: static; border-right: 0; border-bottom: 1px solid var(--line); padding: 16px; }
  .results { padding: 16px 16px 130px; }
  .results-head { flex-wrap: wrap; gap: 8px; }
  .results-head .row { flex-wrap: wrap; }
  .gen-bar {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; margin: 0;
    padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  }
  .gallery { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
  .lightbox { padding: 50px 8px 90px; }
  .lb-nav { display: none; }
}
