:root {
  color-scheme: dark;
  --bg: #07080e;
  --bg-raised: #0f111a;
  --surface: #161822;
  --surface-hover: #1c1f2e;
  --surface-active: #242738;
  --border: #1e2133;
  --border-light: #292d40;
  --text: #e8eaf4;
  --text-secondary: #9498b0;
  --text-muted: #565b73;
  --accent: #7c6ff7;
  --accent-light: #b4a5fb;
  --accent-glow: rgba(124, 111, 247, 0.25);
  --gradient: linear-gradient(135deg, #7c6ff7 0%, #5b9cf5 50%, #38cfde 100%);
  --success: #34d399;
  --success-bg: rgba(52, 211, 153, 0.12);
  --danger: #fb7185;
  --danger-bg: rgba(251, 113, 133, 0.12);
  --info: #60a5fa;
  --info-bg: rgba(96, 165, 250, 0.12);
  --radius: 10px;
  --radius-sm: 6px;
  --radius-lg: 14px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.5);
  --shadow: 0 8px 32px rgba(0,0,0,0.5);
  --shadow-lg: 0 24px 72px rgba(0,0,0,0.7);
  font-family: "Inter", ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  background-image:
    radial-gradient(ellipse at 20% 0%, rgba(124, 111, 247, 0.04) 0%, transparent 60%),
    radial-gradient(ellipse at 80% 100%, rgba(56, 207, 222, 0.03) 0%, transparent 60%);
  background-attachment: fixed;
}

button, input, select, textarea {
  font: inherit;
  color: inherit;
}

input[type="number"] { -moz-appearance: textfield; }
input::-webkit-outer-spin-button,
input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

/* ── Shell layout ────────────────────────────── */

.shell {
  display: grid;
  grid-template-columns: minmax(360px, 420px) minmax(0, 1fr);
  min-height: 100vh;
}

/* ── Sidebar ─────────────────────────────────── */

.sidebar {
  position: sticky;
  top: 0;
  max-height: 100vh;
  overflow-y: auto;
  background: var(--bg-raised);
  border-right: 1px solid var(--border);
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.sidebar::-webkit-scrollbar { width: 4px; }
.sidebar::-webkit-scrollbar-thumb {
  background: var(--border-light);
  border-radius: 2px;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-icon {
  color: var(--accent-light);
  display: flex;
}

.brand h1 {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.status-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--text-muted);
  transition: background 0.3s, box-shadow 0.3s;
}
.status-dot.ready { background: var(--success); box-shadow: 0 0 0 4px var(--success-bg); }
.status-dot.error { background: var(--danger); box-shadow: 0 0 0 4px var(--danger-bg); }
.status-dot.loading { background: var(--info); box-shadow: 0 0 0 4px var(--info-bg); animation: pulse-dot 0.8s ease-in-out infinite alternate; }

@keyframes pulse-dot {
  from { box-shadow: 0 0 0 2px var(--info-bg); }
  to { box-shadow: 0 0 0 6px rgba(96, 165, 250, 0.05); }
}

/* ── Tabs ───────────────────────────────────── */

.tabs {
  display: flex;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 3px;
  gap: 2px;
}

.tab {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  padding: 8px 0;
  font-size: 13px;
  font-weight: 600;
  border-radius: calc(var(--radius) - 2px);
  cursor: pointer;
  transition: all 0.2s ease;
}

.tab:hover { color: var(--text-secondary); }

.tab.active {
  background: var(--surface-active);
  color: var(--text);
  box-shadow: var(--shadow-sm);
  position: relative;
}

.tab.active::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 25%;
  right: 25%;
  height: 2px;
  background: var(--gradient);
  border-radius: 1px;
}

.tab:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ── Form ────────────────────────────────────── */

.form-body {
  display: flex;
  flex-direction: column;
  gap: 14px;
  flex: 1;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-group-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
}

.count-text {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 500;
}

input, select, textarea {
  width: 100%;
  border: 1px solid var(--border-light);
  background: var(--surface);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  outline: none;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
  font-size: 14px;
}

textarea {
  min-height: 120px;
  resize: vertical;
  line-height: 1.6;
}

input:focus, select:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

select:disabled, input:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.form-group.hidden { display: none; }

/* ── Upload zone ─────────────────────────────── */

.upload-zone {
  border: 2px dashed var(--border-light);
  border-radius: var(--radius);
  padding: 20px;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s ease;
  background: var(--surface);
}

.upload-zone:hover,
.upload-zone.drag-over {
  border-color: var(--accent);
  background: rgba(124, 111, 247, 0.06);
}

.upload-hint {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  color: var(--text-muted);
  font-size: 13px;
}

.upload-hint svg { opacity: 0.5; }

.upload-sub {
  font-size: 11px;
  color: var(--text-muted);
  opacity: 0.7;
}

/* ── Preview list ────────────────────────────── */

.preview-list {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.preview-item {
  position: relative;
  width: 72px;
  height: 72px;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 2px solid var(--border-light);
  flex-shrink: 0;
  animation: fade-in 0.2s ease;
}

.preview-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.preview-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 20px;
  height: 20px;
  border: 0;
  border-radius: 50%;
  background: rgba(0,0,0,0.7);
  color: var(--danger);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}

.preview-remove:hover { background: var(--danger); color: #fff; }

/* ── Settings ────────────────────────────────── */

.settings {
  border-top: 1px solid var(--border);
  padding-top: 14px;
}

.settings summary {
  cursor: pointer;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 700;
  list-style-position: outside;
  user-select: none;
}

.settings-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 12px;
}

.settings-grid .form-group { gap: 4px; }
.settings-grid label { font-size: 12px; }
.settings-grid input,
.settings-grid select { padding: 8px 10px; font-size: 13px; }

/* ── Buttons ─────────────────────────────────── */

.btn-submit {
  width: 100%;
  border: 0;
  border-radius: var(--radius);
  padding: 12px 20px;
  font-size: 15px;
  font-weight: 700;
  color: #fff;
  background: var(--gradient);
  cursor: pointer;
  transition: all 0.2s ease;
  box-shadow: 0 4px 16px var(--accent-glow);
  margin-top: auto;
}

.btn-submit:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 8px 28px rgba(108, 92, 231, 0.4);
}

.btn-submit:active:not(:disabled) { transform: translateY(0); }

.btn-submit:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  box-shadow: none;
}

.btn-submit.loading {
  background: var(--surface-active);
  color: var(--text-muted);
  box-shadow: none;
}

.btn-secondary {
  width: 100%;
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  padding: 10px 18px;
  color: var(--text-secondary);
  background: var(--surface);
  text-align: center;
  text-decoration: none;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s ease;
}

.btn-secondary:hover {
  color: var(--text);
  background: var(--surface-hover);
  border-color: var(--accent);
}

.btn-ghost {
  border: 1px solid var(--border-light);
  background: transparent;
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  padding: 5px 10px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease;
}

.btn-ghost:hover {
  background: var(--surface-hover);
  color: var(--text-secondary);
}

.btn-ghost:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ── Workspace ───────────────────────────────── */

.workspace {
  padding: 24px;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.results {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  align-content: start;
  gap: 16px;
}

.records-shell {
  min-height: 100vh;
  padding: 24px;
}

.records-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin: 0 auto 20px;
  max-width: 1200px;
}

.records-back {
  width: auto;
  min-width: 112px;
}

.records-query {
  display: grid;
  grid-template-columns: minmax(260px, 1fr) 160px;
  align-items: end;
  gap: 12px;
  margin: 0 auto 20px;
  max-width: 1200px;
}

.records-query .btn-submit {
  margin-top: 0;
}

.records-shell .results {
  max-width: 1200px;
  margin: 0 auto;
}

/* ── Empty state ─────────────────────────────── */

.empty-state {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  min-height: 60vh;
  color: var(--text-muted);
}

.empty-icon { opacity: 0.3; }

.empty-state strong {
  font-size: 16px;
  color: var(--text-secondary);
  font-weight: 600;
}

.empty-state span {
  font-size: 13px;
}

.empty-state.loading strong {
  color: var(--text);
}

/* ── Image cards ─────────────────────────────── */

.image-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  animation: rise 0.3s ease both;
  transition: border-color 0.25s ease, box-shadow 0.25s ease, transform 0.25s ease;
}

.image-card:hover {
  border-color: var(--accent);
  box-shadow: var(--shadow), 0 0 0 1px var(--accent-glow);
  transform: translateY(-2px);
}

.image-card-inner {
  position: relative;
  overflow: hidden;
  aspect-ratio: 1 / 1;
  background: var(--bg);
  cursor: pointer;
}

.image-card-inner img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  transition: transform 0.3s ease;
}

.image-card-inner.image-missing {
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  font-size: 13px;
}

.image-card:hover .image-card-inner img {
  transform: scale(1.04);
}

.image-card-overlay {
  position: absolute;
  inset: 0;
  background: linear-gradient(to top, rgba(0,0,0,0.7) 0%, transparent 50%);
  opacity: 0;
  transition: opacity 0.2s ease;
  display: flex;
  align-items: flex-end;
  padding: 14px;
  gap: 8px;
}

.image-card:hover .image-card-overlay { opacity: 1; }

.image-card-btn {
  border: 0;
  background: rgba(255,255,255,0.12);
  backdrop-filter: blur(6px);
  color: #fff;
  padding: 6px 14px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s;
}

.image-card-btn:hover { background: rgba(255,255,255,0.22); }

.image-card-meta {
  padding: 12px 14px;
}

.image-card-filename {
  font-size: 12px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.image-card-prompt {
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.5;
  margin-top: 6px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* ── Lightbox ────────────────────────────────── */

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0,0,0,0.92);
  backdrop-filter: blur(20px);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 40px;
  flex-direction: column;
  gap: 16px;
}

.lightbox.open { display: flex; }

.lightbox img {
  max-width: 90vw;
  max-height: 80vh;
  object-fit: contain;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
}

.lightbox-close {
  position: absolute;
  top: 20px;
  right: 24px;
  background: rgba(255,255,255,0.08);
  border: 0;
  color: #fff;
  font-size: 28px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  cursor: pointer;
  transition: background 0.15s;
  display: flex;
  align-items: center;
  justify-content: center;
}

.lightbox-close:hover { background: rgba(255,255,255,0.18); }

.lightbox-info {
  color: var(--text-muted);
  font-size: 13px;
  max-width: 500px;
  text-align: center;
  line-height: 1.5;
}

/* ── Toast ───────────────────────────────────── */

.toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  padding: 12px 18px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 600;
  animation: slide-in 0.25s ease both;
  max-width: 380px;
  box-shadow: var(--shadow-lg);
}

.toast.success { background: var(--success-bg); color: var(--success); border: 1px solid rgba(45, 212, 191, 0.2); }
.toast.error { background: var(--danger-bg); color: var(--danger); border: 1px solid rgba(248, 113, 113, 0.2); }
.toast.info { background: var(--info-bg); color: var(--info); border: 1px solid rgba(96, 165, 250, 0.2); }

.toast.removing {
  animation: slide-out 0.2s ease forwards;
}

@keyframes slide-in {
  from { opacity: 0; transform: translateX(40px); }
  to { opacity: 1; transform: translateX(0); }
}

@keyframes slide-out {
  from { opacity: 1; transform: translateX(0); }
  to { opacity: 0; transform: translateX(40px); }
}

/* ── Spinner ─────────────────────────────────── */

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid var(--border-light);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ── Animations ──────────────────────────────── */

@keyframes rise {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* ── Responsive ──────────────────────────────── */

@media (max-width: 900px) {
  .shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: static;
    max-height: none;
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }

  .settings-grid {
    grid-template-columns: 1fr;
  }

  .records-query {
    grid-template-columns: 1fr;
  }

  .records-toolbar {
    align-items: flex-start;
    flex-direction: column;
  }
}

@media (max-width: 540px) {
  .sidebar,
  .workspace {
    padding: 16px;
  }

  .results {
    grid-template-columns: 1fr;
  }

  .lightbox {
    padding: 16px;
  }
}
