:root {
  color-scheme: dark;
  --bg: #0b1020;
  --panel: #121a2d;
  --panel-2: #17213a;
  --line: #26324f;
  --text: #e8eefc;
  --muted: #93a1c0;
  --accent: #5b9dff;
  --accent-2: #7c6cff;
  --danger: #ff6b81;
  --ok: #4bd6a1;
  --radius: 14px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: radial-gradient(circle at 15% 0%, #172445 0, var(--bg) 45%);
  color: var(--text);
  font: 14px/1.55 system-ui, -apple-system, "Segoe UI", "Noto Sans SC", sans-serif;
}

h1, h2 { margin: 0; line-height: 1.25; }
h1 { font-size: 18px; }
h2 { font-size: 14px; letter-spacing: .02em; }
.sub, .hint, .meta { color: var(--muted); }
.sub { margin: 3px 0 0; font-size: 12px; }
.hint { font-size: 12px; margin: 6px 0; }

.topbar {
  flex: 0 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  padding: 10px 20px;
  border-bottom: 1px solid var(--line);
  background: rgba(10, 15, 30, .78);
  backdrop-filter: blur(10px);
  z-index: 10;
}

.top-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.rail-nav { display: inline-flex; gap: 6px; }
.rail-nav button { min-width: 38px; color: var(--accent); border-color: rgba(91, 157, 255, .45); font-size: 15px; }
.rail-nav button:hover { border-color: var(--accent); background: rgba(91, 157, 255, .12); }
.controls .status { margin-top: auto; }

.rail {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: stretch;
  gap: 14px;
  padding: 14px 20px 16px;
  overflow-x: auto;
  overflow-y: hidden;
  overscroll-behavior-x: contain;
}

.rail > .panel {
  flex: 0 0 auto;
  height: 100%;
  min-height: 0;
  overflow-y: auto;
}

.panel {
  background: linear-gradient(180deg, rgba(255,255,255,.025), transparent 35%), var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: 0 14px 30px rgba(0, 0, 0, .22);
}

.controls { width: clamp(320px, 23vw, 360px); display: flex; flex-direction: column; }
.controls #description { flex: 1 1 0; min-height: 140px; resize: none; }
.styles-panel { width: clamp(400px, 32vw, 500px); display: flex; flex-direction: column; }
.prompt-panel { width: clamp(380px, 32vw, 500px); display: flex; flex-direction: column; }
.result-panel { width: clamp(340px, 28vw, 440px); display: flex; flex-direction: column; }
.history-panel { width: clamp(300px, 25vw, 380px); display: flex; flex-direction: column; }

.panel-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}
.panel-head h2 { margin: 0; }
.step {
  color: var(--accent);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .08em;
  white-space: nowrap;
}

textarea, input, select {
  width: 100%;
  border: 1px solid var(--line);
  background: #0c1325;
  color: var(--text);
  border-radius: 10px;
  padding: 10px 11px;
  font: inherit;
  outline: none;
}
textarea { resize: vertical; min-height: 100px; }
textarea:focus, input:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(91, 157, 255, .14);
}

.field { display: flex; flex-direction: column; gap: 6px; flex: 1; min-width: 0; }
.field > span { color: var(--muted); font-size: 12px; }
.field.small { max-width: 150px; }
.row { display: flex; gap: 10px; align-items: flex-end; flex-wrap: wrap; margin-top: 10px; }
.checks { align-items: center; gap: 14px; color: var(--muted); }
.checks label { display: inline-flex; gap: 6px; align-items: center; white-space: nowrap; }
.checks input { width: auto; }
.advanced { margin-top: 12px; border-top: 1px solid var(--line); padding-top: 10px; }
.advanced summary { cursor: pointer; color: var(--muted); }
.advanced[open] summary { margin-bottom: 6px; }

button, .ghost {
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  padding: 9px 13px;
  border-radius: 10px;
  cursor: pointer;
  font: inherit;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: .15s ease;
}
button:hover, .ghost:hover { border-color: #3b4d76; transform: translateY(-1px); }
button:disabled { opacity: .5; cursor: not-allowed; transform: none; }
button.primary, .primary {
  border-color: transparent;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: white;
  font-weight: 600;
}
button.ghost, .ghost { background: transparent; }
button.small, .small { padding: 6px 10px; font-size: 12px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }
.disabled { opacity: .45; pointer-events: none; }
.danger { color: #ffc0c9; border-color: rgba(255, 107, 129, .42); }
.danger:hover { border-color: rgba(255, 107, 129, .8); }

.status {
  margin-top: 12px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--muted);
  background: #0c1325;
  min-height: 40px;
}
.status.busy { border-color: rgba(91, 157, 255, .5); color: var(--text); }
.status.error { border-color: rgba(255, 107, 129, .55); color: #ffc0c9; }
.status.ok { border-color: rgba(75, 214, 161, .45); color: #b9ffe6; }

.styles-head {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
}
.styles-head .panel-head { margin-bottom: 0; }
.styles-tools { display: flex; gap: 8px; flex-wrap: wrap; }
.styles-tools input { flex: 1 1 160px; min-width: 0; }
.styles-tools select { width: 118px; flex: 0 0 auto; }

.styles-grid {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(104px, 1fr));
  align-content: start;
  gap: 10px;
  overflow-y: auto;
  padding: 4px 3px 8px;
}
.style-card {
  border: 1px solid var(--line);
  background: #0c1325;
  border-radius: 11px;
  padding: 0;
  overflow: hidden;
  text-align: left;
  position: relative;
  display: block;
  cursor: pointer;
}
.style-card img {
  display: block;
  width: 100%;
  aspect-ratio: 3 / 4;
  object-fit: cover;
  background: #0a1020;
}
.style-card .name {
  display: block;
  padding: 7px 8px 20px;
  font-size: 11px;
  line-height: 1.3;
  color: var(--text);
  word-break: break-word;
}
.style-card .posts {
  position: absolute;
  right: 6px;
  bottom: 5px;
  font-size: 10px;
  color: var(--muted);
}
.style-card.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(91, 157, 255, .28), 0 8px 18px rgba(0,0,0,.3);
}
.style-card.selected::after {
  content: "✓";
  position: absolute;
  top: 6px;
  right: 6px;
  background: var(--accent);
  color: #061025;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-weight: 800;
  font-size: 12px;
}
.pager { display: flex; align-items: center; justify-content: center; gap: 12px; padding-top: 10px; color: var(--muted); }

.prompt-panel .prompt-head { margin-bottom: 2px; }
.prompt-head { display: flex; justify-content: space-between; gap: 12px; align-items: center; flex-wrap: wrap; }
.prompt-head .panel-head { margin-bottom: 0; }
.prompt-head .row { margin-top: 0; }
.prompt-grid {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-rows: 1fr 1fr;
  gap: 10px;
  margin-top: 8px;
}
.prompt-grid .field { height: 100%; min-height: 0; }
.prompt-grid textarea { flex: 1 1 auto; min-height: 90px; resize: none; }

.result { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; gap: 10px; }
.result img {
  flex: 1 1 auto;
  min-height: 120px;
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #0a1020;
}
.result img:not([src]) { display: none; }
.meta { font-size: 12px; word-break: break-word; white-space: pre-wrap; }

.history {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(88px, 1fr));
  align-content: start;
  gap: 8px;
  overflow-y: auto;
}
.history-card { border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: #0c1325; cursor: pointer; }
.history-card img { width: 100%; aspect-ratio: 1; object-fit: cover; display: block; }
.history-card span { display: block; padding: 4px 6px; font-size: 10px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.preset-block { margin-top: 12px; border-top: 1px solid var(--line); padding-top: 10px; }
.preset-block .hint { margin: 0 0 8px; }
.presets { display: flex; gap: 10px; flex-wrap: wrap; }
.preset {
  width: 100%;
  text-align: left;
  align-items: flex-start;
  flex-direction: column;
  gap: 3px;
  padding: 10px 12px;
  background: #0c1325;
}
.preset:hover { border-color: rgba(91, 157, 255, .55); }
.preset-title { font-weight: 650; color: var(--text); }
.preset-desc { font-size: 11px; color: var(--muted); }

.rail, .panel, .styles-grid, .history {
  scrollbar-width: thin;
  scrollbar-color: #3a5488 transparent;
}
.rail { scrollbar-width: auto; }
.rail::-webkit-scrollbar, .panel::-webkit-scrollbar,
.styles-grid::-webkit-scrollbar, .history::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}
.rail::-webkit-scrollbar-thumb, .panel::-webkit-scrollbar-thumb,
.styles-grid::-webkit-scrollbar-thumb, .history::-webkit-scrollbar-thumb {
  background: #3a5488;
  border-radius: 999px;
}
.rail::-webkit-scrollbar-track, .panel::-webkit-scrollbar-track,
.styles-grid::-webkit-scrollbar-track, .history::-webkit-scrollbar-track {
  background: transparent;
}

dialog {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 14px;
  padding: 0;
  width: min(460px, 92vw);
}
.dialog-body { padding: 18px; }
.dialog-body h2 { margin-bottom: 12px; }
dialog::backdrop { background: rgba(2, 6, 16, .7); backdrop-filter: blur(3px); }

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  max-width: min(420px, 90vw);
  padding: 11px 14px;
  border-radius: 10px;
  background: #1b2740;
  border: 1px solid var(--line);
  color: var(--text);
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: .2s ease;
  z-index: 50;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { border-color: rgba(255, 107, 129, .6); }

.spinner {
  width: 12px;
  height: 12px;
  border: 2px solid rgba(255,255,255,.25);
  border-top-color: white;
  border-radius: 50%;
  display: inline-block;
  animation: spin .8s linear infinite;
  vertical-align: -2px;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 900px) {
  .topbar { padding: 10px 14px; align-items: flex-start; }
  .topbar .sub { display: none; }
  .top-actions { gap: 6px; }
  .rail { padding: 12px; gap: 10px; }
  .controls, .styles-panel, .prompt-panel, .result-panel, .history-panel { width: 86vw; }
  .prompt-grid { grid-template-columns: 1fr; }
}
