:root {
  --bg0: #0e0f21;
  --bg1: #13152c;
  --bg2: #191b31;
  --bg3: #22253f;
  --bg4: #2b2e4d;
  --line: rgba(255, 255, 255, .08);
  --line2: rgba(255, 255, 255, .14);
  --text: #ffffff;
  --text2: rgba(255, 255, 255, .72);
  --text3: rgba(255, 255, 255, .45);
  --accent: #f5f3c2;
  --accent-ink: #13152c;
  --warn: #f0a37a;
  --danger: #ff7a7a;
  --ok: #8be0b0;
  --radius: 6px;
  --font: "Source Sans Pro", "Source Sans 3", -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  --serif: "Eczar", Georgia, "Times New Roman", serif;
  --mono: ui-monospace, "SFMono-Regular", Menlo, monospace;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; background: var(--bg1); color: var(--text); font: 14px/1.45 var(--font); -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
.icon-library { display: none; }
.hidden { display: none !important; }
.muted { color: var(--text3); }
.gi { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

.pg { display: grid; grid-template-columns: 400px minmax(0, 1fr) 132px; height: 100vh; }

/* ---------- left side ---------- */
.pg-side { display: flex; flex-direction: column; background: var(--bg2); border-right: 1px solid var(--line); min-height: 0; }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.pg-brand { display: flex; align-items: center; gap: 10px; color: var(--accent); text-decoration: none; }
.pg-mark { display: grid; place-items: center; width: 28px; height: 28px; border-radius: 6px; background: var(--accent); color: var(--accent-ink); font: 700 16px var(--serif); }
.pg-brand-text { font: 600 19px var(--serif); letter-spacing: .01em; }
.side-link { color: var(--text2); padding: 6px; border-radius: 6px; display: grid; }
.side-link:hover { color: var(--accent); background: var(--bg3); }
.side-scroll { flex: 1; overflow-y: auto; padding: 14px 14px 20px; display: grid; gap: 12px; align-content: start; }
.side-scroll::-webkit-scrollbar, .history-list::-webkit-scrollbar { width: 6px; }
.side-scroll::-webkit-scrollbar-thumb, .history-list::-webkit-scrollbar-thumb { background: var(--bg4); border-radius: 3px; }

.model-select, .select-wrap { position: relative; }
.model-select select, .select-wrap select { appearance: none; width: 100%; background: var(--bg0); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 34px 10px 12px; cursor: pointer; }
.model-select select { font: 600 15px var(--serif); color: var(--accent); padding: 12px 34px 12px 14px; }
.chev { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); pointer-events: none; color: var(--text3); }
select option, select optgroup { background: var(--bg2); color: var(--text); }

.card { background: var(--bg3); border: 1px solid var(--line); border-radius: 8px; padding: 12px; display: grid; gap: 10px; }
.card-title { margin: 0; font: 600 15px var(--serif); color: var(--accent); }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.small-row { font-size: 13px; }
.hint { margin: 0; font-size: 12px; color: var(--text3); }

.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); }
.tab { background: none; border: 0; padding: 6px 10px 8px; color: var(--text3); font-weight: 600; cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }
textarea { width: 100%; resize: vertical; background: var(--bg0); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; min-height: 90px; line-height: 1.5; }
textarea:focus, input:focus, select:focus { outline: 1px solid rgba(245, 243, 194, .5); outline-offset: 0; }
input[type=text], input[type=number], input[type=password] { background: var(--bg0); border: 1px solid var(--line); border-radius: var(--radius); padding: 9px 10px; width: 100%; }
.field-label { font-size: 13px; color: var(--text2); margin-top: 2px; }

.toggle-line { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; font-size: 13px; color: var(--text2); user-select: none; }
.toggle-line input { position: absolute; opacity: 0; pointer-events: none; }
.tgl { width: 30px; height: 16px; border-radius: 8px; background: var(--bg0); border: 1px solid var(--line2); position: relative; transition: background .15s; flex: none; }
.tgl::after { content: ""; position: absolute; top: 2px; left: 2px; width: 10px; height: 10px; border-radius: 50%; background: var(--text3); transition: transform .15s, background .15s; }
.toggle-line input:checked + .tgl { background: rgba(245, 243, 194, .2); border-color: var(--accent); }
.toggle-line input:checked + .tgl::after { transform: translateX(14px); background: var(--accent); }
.toggle-line input:focus-visible + .tgl { outline: 1px solid var(--accent); }

.slider-row { display: grid; grid-template-columns: 1fr 120px 42px; align-items: center; gap: 10px; font-size: 13px; color: var(--text2); }
.slider-row output { text-align: right; font-variant-numeric: tabular-nums; color: var(--text); }
input[type=range] { -webkit-appearance: none; appearance: none; width: 100%; height: 4px; border-radius: 2px; background: var(--bg0); cursor: pointer; }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 14px; height: 14px; border-radius: 50%; background: var(--accent); border: 0; }
input[type=range]::-moz-range-thumb { width: 14px; height: 14px; border-radius: 50%; background: var(--accent); border: 0; }

.dims { display: grid; grid-template-columns: 1fr auto 1fr; gap: 8px; align-items: center; }
.seed-row { display: grid; grid-template-columns: 1fr auto; gap: 8px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; background: var(--accent); color: var(--accent-ink); border: 0; border-radius: var(--radius); padding: 9px 14px; font-weight: 700; cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:hover { filter: brightness(1.05); }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn.small { padding: 6px 10px; font-size: 13px; }
.btn.ghost { background: transparent; color: var(--text2); border: 1px solid var(--line2); font-weight: 600; }
.btn.ghost:hover { color: var(--accent); border-color: rgba(245, 243, 194, .4); }
.icon-btn { display: grid; place-items: center; background: var(--bg0); border: 1px solid var(--line); border-radius: var(--radius); color: var(--text2); padding: 8px; cursor: pointer; }
.icon-btn:hover { color: var(--accent); }

.image-tools { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tool-btn { display: grid; justify-items: center; gap: 6px; padding: 12px 6px; background: var(--bg3); border: 1px dashed var(--line2); border-radius: 8px; color: var(--text2); font-size: 12px; font-weight: 600; cursor: pointer; }
.tool-btn:hover, .tool-btn.active { color: var(--accent); border-color: rgba(245, 243, 194, .45); border-style: solid; }
.tool-btn .gi { width: 20px; height: 20px; }
.drop { border: 1px dashed var(--line2); border-radius: var(--radius); padding: 16px; text-align: center; font-size: 12px; color: var(--text3); cursor: pointer; background: var(--bg0); }
.drop:hover, .drop.over { border-color: var(--accent); color: var(--accent); }
.ref-list { display: grid; gap: 8px; }
.ref-item { display: grid; grid-template-columns: 64px 1fr auto; gap: 10px; align-items: center; background: var(--bg2); border: 1px solid var(--line); border-radius: var(--radius); padding: 8px; }
.ref-item img { width: 64px; height: 64px; object-fit: cover; border-radius: 4px; background: var(--bg0); }
.ref-item .slider-row { grid-template-columns: 1fr 90px 38px; font-size: 12px; }
.ref-controls { display: grid; gap: 4px; min-width: 0; }

.character { background: var(--bg2); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; display: grid; gap: 8px; }
.character-head { display: flex; align-items: center; gap: 8px; }
.character-head strong { font: 600 14px var(--serif); color: var(--accent); flex: 1; }
.character textarea { min-height: 60px; }
.position-grid { display: grid; grid-template-columns: repeat(5, 18px); gap: 3px; }
.position-grid button { width: 18px; height: 18px; border-radius: 3px; border: 1px solid var(--line2); background: var(--bg0); cursor: pointer; padding: 0; }
.position-grid button.active { background: var(--accent); border-color: var(--accent); }
.character .tabs { border-bottom: 0; }
.character .tab { padding: 2px 8px 4px; font-size: 12px; }

details.advanced summary { list-style: none; cursor: pointer; display: flex; align-items: center; justify-content: space-between; font-size: 13px; color: var(--text2); position: relative; padding-right: 26px; }
details.advanced summary::-webkit-details-marker { display: none; }
details.advanced[open] summary .chev { transform: translateY(-50%) rotate(180deg); }
details.advanced > *:not(summary) { margin-top: 10px; }
details.advanced { display: grid; gap: 10px; }

.side-foot { padding: 12px 14px 14px; border-top: 1px solid var(--line); background: var(--bg2); }
.generate { width: 100%; padding: 13px 16px; font: 700 16px var(--font); justify-content: space-between; }
.cost { display: inline-flex; align-items: center; gap: 4px; background: rgba(19, 21, 44, .12); padding: 3px 8px; border-radius: 4px; font-variant-numeric: tabular-nums; }
.cost.free { text-decoration: line-through; opacity: .7; }
.cost-note { font-size: 12px; color: var(--text3); margin-top: 7px; text-align: center; min-height: 17px; }
.cost-note.warn { color: var(--warn); }

/* ---------- main ---------- */
.pg-main { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--bg1); }
.main-head { display: flex; align-items: center; gap: 12px; padding: 10px 16px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.key-bar { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 280px; max-width: 620px; background: var(--bg0); border: 1px solid var(--line); border-radius: var(--radius); padding: 4px 4px 4px 12px; color: var(--text3); }
.key-bar input { border: 0 !important; background: transparent !important; padding: 6px 0 !important; outline: none !important; font-family: var(--mono); font-size: 13px; color: var(--text); }
.remember { display: inline-flex; gap: 4px; align-items: center; font-size: 12px; white-space: nowrap; color: var(--text3); }
.account-pill { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text2); }
.pill-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text3); }
.account-pill.ok .pill-dot { background: var(--ok); box-shadow: 0 0 0 3px rgba(139, 224, 176, .15); }
.account-pill.bad .pill-dot { background: var(--danger); }
.anlas-pill { display: inline-flex; align-items: center; gap: 4px; padding: 3px 8px; border-radius: 4px; background: var(--bg3); color: var(--accent); font-weight: 700; font-variant-numeric: tabular-nums; }

.stage { flex: 1; min-height: 0; position: relative; display: grid; place-items: center; padding: 24px; overflow: auto; }
.stage-empty { text-align: center; color: var(--text3); max-width: 360px; }
.stage-empty h2 { font: 600 22px var(--serif); color: var(--text2); margin: 14px 0 6px; }
.stage-empty p { margin: 0; }
.empty-art { display: inline-grid; place-items: center; width: 84px; height: 84px; border-radius: 18px; border: 1px dashed var(--line2); }
.empty-art .gi { width: 36px; height: 36px; }
.stage-loading { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 14px; background: rgba(19, 21, 44, .72); z-index: 2; color: var(--accent); font: 600 16px var(--serif); }
.spinner { width: 42px; height: 42px; border-radius: 50%; border: 3px solid var(--bg4); border-top-color: var(--accent); animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.stage-grid { display: grid; gap: 12px; width: 100%; height: 100%; place-items: center; }
.stage-grid.n2 { grid-template-columns: repeat(2, 1fr); }
.stage-grid.n3, .stage-grid.n4 { grid-template-columns: repeat(2, 1fr); grid-template-rows: repeat(2, 1fr); }
.stage-grid img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 4px; box-shadow: 0 10px 40px rgba(0, 0, 0, .45); cursor: pointer; outline: 2px solid transparent; }
.stage-grid.n1 img { max-height: calc(100vh - 170px); }
.stage-grid:not(.n1) img { max-height: calc((100vh - 200px) / 2); }
.stage-grid img.selected { outline-color: var(--accent); }

.image-bar { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 10px 16px; border-top: 1px solid var(--line); flex-wrap: wrap; }
.bar-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.seed-chip { display: inline-flex; align-items: center; gap: 6px; color: var(--text2); font-family: var(--mono); font-size: 13px; cursor: pointer; }
.seed-chip:hover { color: var(--accent); }

/* ---------- history ---------- */
.pg-history { background: var(--bg2); border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.history-head { display: flex; align-items: center; gap: 6px; padding: 14px 10px; border-bottom: 1px solid var(--line); font: 600 14px var(--serif); color: var(--accent); }
.history-head span { flex: 1; }
.history-head .icon-btn { padding: 5px; }
.history-list { flex: 1; overflow-y: auto; padding: 10px; display: grid; gap: 8px; align-content: start; }
.history-list img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 4px; cursor: pointer; border: 2px solid transparent; background: var(--bg0); }
.history-list img.active { border-color: var(--accent); }

.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--bg4); border: 1px solid var(--line2); color: var(--text); padding: 10px 16px; border-radius: 6px; z-index: 20; max-width: min(560px, 90vw); box-shadow: 0 12px 30px rgba(0, 0, 0, .4); }
.toast.error { border-color: rgba(255, 122, 122, .5); color: #ffd2d2; }

@media (max-width: 1100px) {
  .pg { grid-template-columns: 360px minmax(0, 1fr); }
  .pg-history { display: none; }
}
@media (max-width: 760px) {
  .pg { grid-template-columns: 1fr; height: auto; }
  .pg-side { order: 2; border-right: 0; }
  .side-scroll { overflow: visible; }
  .pg-main { order: 1; min-height: 70vh; }
  .stage-grid.n1 img { max-height: 60vh; }
}
.image-tools.two { grid-template-columns: repeat(2, 1fr); }
