:root {
  --bg: #0c0d10;
  --panel: #14161b;
  --panel-2: #1a1d24;
  --border: #262a33;
  --border-2: #333846;
  --text: #e8eaf0;
  --text-dim: #9aa0ad;
  --text-faint: #666c7a;
  --accent: #ff7a1a;
  --accent-2: #ffab5e;
  --accent-glow: rgba(255, 122, 26, 0.25);
  --danger: #ff4d5e;
  --ok: #3fd07a;
  --radius: 12px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
}

.app {
  display: grid;
  grid-template-columns: 360px 1fr;
  height: 100vh;
}

/* ---------- Panel ---------- */
.panel {
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 22px 20px 28px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 4px;
}
.brand-mark {
  width: 34px;
  height: 34px;
  border-radius: 9px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 0 18px var(--accent-glow);
  flex-shrink: 0;
}
.brand h1 {
  font-size: 17px;
  margin: 0;
  letter-spacing: -0.01em;
}
.brand-sub {
  font-size: 11px;
  color: var(--text-faint);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 7px;
}
.field > span,
.field .label-row > span:first-child {
  font-size: 12px;
  color: var(--text-dim);
  font-weight: 500;
}
.label-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}
.counter {
  font-size: 11px;
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
}

.field-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

textarea, input[type="text"], input[type="number"], select {
  width: 100%;
  background: var(--panel-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 9px;
  padding: 10px 11px;
  font-size: 13px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
textarea { resize: vertical; line-height: 1.5; }
textarea:focus, input:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}
select { appearance: none; cursor: pointer;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%239aa0ad' stroke-width='2'><path d='M6 9l6 6 6-6'/></svg>");
  background-repeat: no-repeat;
  background-position: right 11px center;
  padding-right: 30px;
}

.custom-size { display: flex; align-items: center; gap: 8px; }
.custom-size .x { color: var(--text-faint); }

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  height: 5px;
  border-radius: 4px;
  background: var(--border-2);
  cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent-glow);
}

/* ---------- Switch (batch toggle) ---------- */
.switch-row {
  display: flex;
  align-items: center;
  gap: 9px;
  cursor: pointer;
  user-select: none;
  flex-wrap: wrap;
}
.switch-row input { display: none; }
.switch-track {
  width: 38px;
  height: 22px;
  border-radius: 11px;
  background: var(--border-2);
  position: relative;
  transition: background 0.15s;
  flex-shrink: 0;
}
.switch-thumb {
  position: absolute;
  top: 2px; left: 2px;
  width: 18px; height: 18px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.15s;
}
.switch-row input:checked + .switch-track { background: var(--accent); }
.switch-row input:checked + .switch-track .switch-thumb { transform: translateX(16px); }
.switch-text { font-size: 13px; font-weight: 600; color: var(--text); }
.switch-hint { font-size: 11px; color: var(--text-faint); flex-basis: 100%; padding-left: 47px; }

/* ---------- Attachments ---------- */
.dropzone {
  border: 1.5px dashed var(--border-2);
  border-radius: 9px;
  padding: 16px 12px;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  background: var(--panel-2);
}
.dropzone:hover { border-color: var(--accent); }
.dropzone.dragover { border-color: var(--accent); background: rgba(255, 122, 26, 0.08); }
.dz-text { font-size: 12px; color: var(--text-faint); }

.attach-previews {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}
.attach-previews:empty { margin-top: 0; }
.attach-thumb {
  position: relative;
  width: 58px;
  height: 58px;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border-2);
  background: var(--panel-2);
}
.attach-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.attach-thumb .rm {
  position: absolute;
  top: 2px; right: 2px;
  width: 18px; height: 18px;
  border-radius: 50%;
  border: none;
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.attach-thumb .rm:hover { background: var(--danger); }

.refs-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}
.refs-label { font-size: 10.5px; color: var(--text-faint); text-transform: uppercase; letter-spacing: 0.04em; }
.ref-thumb {
  width: 40px; height: 40px;
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid var(--border);
  cursor: pointer;
}
.ref-thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }

.warn {
  font-size: 12px;
  color: #ffcf8a;
  background: rgba(255, 122, 26, 0.09);
  border: 1px solid rgba(255, 122, 26, 0.3);
  border-radius: 9px;
  padding: 9px 11px;
  line-height: 1.4;
}

.generate {
  margin-top: 4px;
  width: 100%;
  border: none;
  border-radius: 10px;
  padding: 13px 16px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #1a1200;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: transform 0.1s, opacity 0.15s, box-shadow 0.15s;
  box-shadow: 0 6px 20px var(--accent-glow);
}
.generate:hover { transform: translateY(-1px); }
.generate:active { transform: translateY(0); }
.generate:disabled { opacity: 0.55; cursor: not-allowed; transform: none; }
.btn-cost { font-size: 12px; font-weight: 600; opacity: 0.75; font-variant-numeric: tabular-nums; }

.hint { font-size: 11px; color: var(--text-faint); line-height: 1.4; }

/* ---------- Gallery ---------- */
.gallery-wrap {
  padding: 22px 26px;
  overflow-y: auto;
}
.gallery-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 18px;
  gap: 12px;
  flex-wrap: wrap;
}
.gallery-head h2 { margin: 0; font-size: 16px; font-weight: 600; }

.ws-bar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.ws-label { font-size: 11px; color: var(--text-faint); text-transform: uppercase; letter-spacing: 0.05em; }
.ws-select {
  width: auto;
  min-width: 150px;
  padding: 7px 30px 7px 11px;
  font-weight: 600;
  font-size: 13px;
}
.danger-ghost:hover { border-color: var(--danger); color: var(--danger); }

.ghost {
  background: transparent;
  border: 1px solid var(--border-2);
  color: var(--text-dim);
  border-radius: 8px;
  padding: 7px 13px;
  font-size: 12px;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}
.ghost:hover { border-color: var(--accent); color: var(--text); }

.status {
  padding: 11px 14px;
  border-radius: 9px;
  margin-bottom: 16px;
  font-size: 13px;
}
.status.loading { background: rgba(255,122,26,0.09); border: 1px solid rgba(255,122,26,0.3); color: var(--accent-2); }
.status.error { background: rgba(255,77,94,0.1); border: 1px solid rgba(255,77,94,0.35); color: #ff9aa5; }

.gallery {
  display: flex;
  flex-direction: column;
  gap: 22px;
}
.gen-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
}
.gen-card.error { border-color: rgba(255,77,94,0.4); }
.gen-top {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 12px;
}
.gen-prompt {
  font-size: 13px;
  line-height: 1.5;
  color: var(--text);
  flex: 1;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.gen-actions { display: flex; gap: 8px; flex-shrink: 0; align-items: flex-start; }
.icon-btn {
  background: transparent;
  border: 1px solid var(--border-2);
  color: var(--text-dim);
  border-radius: 7px;
  padding: 5px 10px;
  font-size: 11px;
  cursor: pointer;
  transition: 0.15s;
}
.icon-btn:hover { border-color: var(--accent); color: var(--text); }
.icon-btn.danger:hover { border-color: var(--danger); color: var(--danger); }

.badges { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.badge {
  font-size: 10.5px;
  color: var(--text-dim);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 3px 8px;
  font-variant-numeric: tabular-nums;
}
.badge.err { color: #ff9aa5; border-color: rgba(255,77,94,0.35); background: rgba(255,77,94,0.08); }

.thumbs {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px;
}
.thumb {
  position: relative;
  aspect-ratio: 1;
  border-radius: 9px;
  overflow: hidden;
  cursor: pointer;
  background: var(--panel-2) url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='24' height='24' fill='none' stroke='%23444' stroke-width='1.5'><rect x='3' y='3' width='18' height='18' rx='2'/><circle cx='8.5' cy='8.5' r='1.5'/><path d='M21 15l-5-5L5 21'/></svg>") center/28px no-repeat;
  border: 1px solid var(--border);
  transition: transform 0.12s, border-color 0.12s;
}
.thumb:hover { transform: scale(1.02); border-color: var(--accent); }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb-dl {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 28px;
  height: 28px;
  border-radius: 7px;
  background: rgba(0, 0, 0, 0.62);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  border: 1px solid rgba(255, 255, 255, 0.16);
  z-index: 2;
  transition: background 0.15s, color 0.15s, transform 0.1s;
}
.thumb-dl:hover { background: var(--accent); color: #1a1200; }
.thumb-dl:active { transform: scale(0.92); }
.checker { background-image:
  linear-gradient(45deg, #2a2d35 25%, transparent 25%),
  linear-gradient(-45deg, #2a2d35 25%, transparent 25%),
  linear-gradient(45deg, transparent 75%, #2a2d35 75%),
  linear-gradient(-45deg, transparent 75%, #2a2d35 75%);
  background-size: 16px 16px;
  background-position: 0 0, 0 8px, 8px -8px, -8px 0px;
}

/* Pending / loading card */
.loading-card { border-color: rgba(255, 122, 26, 0.35); }
.badge.load {
  color: var(--accent-2);
  border-color: rgba(255, 122, 26, 0.4);
  background: rgba(255, 122, 26, 0.1);
}
.thumb-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--panel-2);
  position: relative;
  overflow: hidden;
}
.thumb-loading::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.05), transparent);
  animation: shimmer 1.3s infinite;
}
@keyframes shimmer {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(100%); }
}
.spinner {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 3px solid var(--border-2);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
  z-index: 1;
}
@keyframes spin { to { transform: rotate(360deg); } }

.empty { color: var(--text-faint); text-align: center; padding: 60px 0; font-size: 14px; }

/* ---------- Lightbox ---------- */
.lightbox { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; }
.lb-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,0.82); backdrop-filter: blur(4px); }
.lb-content {
  position: relative;
  max-width: min(1100px, 94vw);
  max-height: 92vh;
  display: grid;
  grid-template-columns: 1fr 300px;
  gap: 0;
  background: var(--panel);
  border: 1px solid var(--border-2);
  border-radius: 14px;
  overflow: hidden;
}
.lb-image { background: #000; display: flex; align-items: center; justify-content: center; padding: 12px; }
.lb-image img { max-width: 100%; max-height: 88vh; object-fit: contain; }
.lb-meta { padding: 20px; overflow-y: auto; border-left: 1px solid var(--border); font-size: 12.5px; }
.lb-meta h3 { margin: 0 0 4px; font-size: 13px; color: var(--text-dim); }
.lb-meta .row { margin-bottom: 14px; }
.lb-meta .val { color: var(--text); line-height: 1.5; word-break: break-word; }
.lb-meta .kv { display: flex; justify-content: space-between; padding: 4px 0; border-bottom: 1px solid var(--border); }
.lb-meta .kv span:first-child { color: var(--text-faint); }
.lb-meta .kv span:last-child { color: var(--text); font-variant-numeric: tabular-nums; }
.lb-close {
  position: absolute; top: 12px; right: 12px; z-index: 2;
  background: rgba(0,0,0,0.5); border: 1px solid var(--border-2);
  color: var(--text); border-radius: 7px; padding: 6px 12px; cursor: pointer; font-size: 12px;
}
.lb-actions { display: flex; gap: 8px; margin-top: 8px; }
.lb-actions a, .lb-actions button {
  flex: 1; text-align: center; text-decoration: none;
  background: var(--panel-2); border: 1px solid var(--border-2);
  color: var(--text); border-radius: 8px; padding: 9px; font-size: 12px; cursor: pointer;
}
.lb-actions a:hover, .lb-actions button:hover { border-color: var(--accent); }

.hidden { display: none !important; }

/* Mobile tab bar — hidden on desktop */
.mobile-tabs { display: none; }

@media (max-width: 900px) {
  /* Switch to a single-column, tabbed layout so History is reachable */
  .app { display: flex; flex-direction: column; }

  .mobile-tabs {
    display: flex;
    flex: none;
    background: var(--panel);
    border-bottom: 1px solid var(--border);
  }
  .mobile-tabs button {
    flex: 1;
    padding: 14px 10px;
    background: transparent;
    border: none;
    border-bottom: 2px solid transparent;
    color: var(--text-dim);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
  }
  .mobile-tabs button.active { color: var(--text); border-bottom-color: var(--accent); }

  .panel, .gallery-wrap {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
  }
  .panel { border-right: none; border-bottom: none; }
  .gallery-wrap { padding: 16px; }

  /* Show only the active view */
  body.m-history .panel { display: none; }
  body.m-create .gallery-wrap { display: none; }

  .lb-content { grid-template-columns: 1fr; }
  .lb-meta { border-left: none; border-top: 1px solid var(--border); }
  .lb-image img { max-height: 60vh; }
}
