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

:root {
  --bg: #0f0f13;
  --surface: #1a1a23;
  --surface2: #22222e;
  --surface3: #2a2a38;
  --border: #2e2e40;
  --text: #e4e4ed;
  --text2: #9898a8;
  --primary: #6366f1;
  --primary-hover: #818cf8;
  --success: #22c55e;
  --error: #ef4444;
  --radius: 10px;
  --radius-sm: 6px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  height: 100vh;
  overflow: hidden;
}

.app {
  display: flex;
  height: 100vh;
}

/* ── Sidebar ── */
.sidebar {
  width: 240px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  gap: 24px;
  flex-shrink: 0;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
}
.logo h1 {
  font-size: 20px;
  font-weight: 700;
  background: linear-gradient(135deg, #6366f1, #a78bfa);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.config-section {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.label-sm {
  font-size: 11px;
  font-weight: 600;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.key-input-row {
  display: flex;
  gap: 6px;
}
.key-input-row input {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface2);
  color: var(--text);
  font-size: 13px;
  outline: none;
}
.key-input-row input:focus {
  border-color: var(--primary);
}
.key-status {
  font-size: 12px;
  color: var(--text2);
}

.nav-section {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 14px;
  color: var(--text2);
  transition: all 0.15s;
}
.nav-item:hover {
  background: var(--surface2);
  color: var(--text);
}
.nav-item.active {
  background: var(--primary);
  color: #fff;
}

/* ── Main ── */
.main {
  flex: 1;
  overflow-y: auto;
  padding: 28px 32px;
}

.tab-content {
  display: none;
}
.tab-content.active {
  display: block;
}

/* ── Generate Layout ── */
.generate-layout {
  display: grid;
  grid-template-columns: 420px 1fr;
  gap: 28px;
  height: calc(100vh - 56px);
}

.form-panel {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  border: 1px solid var(--border);
  overflow-y: auto;
}
.form-panel h2 {
  font-size: 18px;
  margin-bottom: 20px;
}

.form-group {
  margin-bottom: 16px;
}
.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text2);
  margin-bottom: 6px;
}

.form-row {
  display: flex;
  gap: 12px;
}
.form-group.half {
  flex: 1;
}

input[type="text"],
input[type="url"],
input[type="password"],
textarea,
select {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface2);
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s;
}
input:focus, textarea:focus, select:focus {
  border-color: var(--primary);
}
textarea {
  resize: vertical;
  font-family: inherit;
}
select {
  cursor: pointer;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%239898a8'%3E%3Cpath d='M2 4l4 4 4-4'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 30px;
}

/* Type selector */
.type-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.type-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface2);
  cursor: pointer;
  font-size: 13px;
  transition: all 0.15s;
}
.type-btn:hover {
  border-color: var(--primary);
}
.type-btn.active {
  border-color: var(--primary);
  background: rgba(99, 102, 241, 0.12);
  color: var(--primary-hover);
}
.type-icon {
  font-size: 18px;
}

.ref-preview {
  margin-top: 8px;
  max-width: 200px;
  border-radius: var(--radius-sm);
  overflow: hidden;
  display: none;
}
.ref-preview img {
  width: 100%;
  height: auto;
  display: block;
}

.btn-generate {
  width: 100%;
  padding: 12px;
  border: none;
  border-radius: var(--radius-sm);
  background: var(--primary);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  transition: background 0.15s;
  margin-top: 8px;
}
.btn-generate:hover {
  background: var(--primary-hover);
}
.btn-generate:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-sm {
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface2);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s;
}
.btn-sm:hover {
  background: var(--surface3);
  border-color: var(--primary);
}

.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255,255,255,0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

.error-msg {
  margin-top: 10px;
  font-size: 13px;
  color: var(--error);
  display: none;
}

/* ── Result Panel ── */
.result-panel {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  border: 1px solid var(--border);
  overflow-y: auto;
}
.result-panel h2 {
  font-size: 18px;
  margin-bottom: 16px;
}

.result-gallery {
  min-height: 300px;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: var(--text2);
}
.empty-icon {
  font-size: 48px;
  margin-bottom: 12px;
  opacity: 0.5;
}
.empty-hint {
  font-size: 13px;
  margin-top: 4px;
  opacity: 0.6;
}

/* Gallery Grid */
.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 16px;
}
.gallery-item {
  background: var(--surface2);
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--border);
  transition: border-color 0.15s;
}
.gallery-item:hover {
  border-color: var(--primary);
}
.gallery-item img,
.gallery-item video {
  width: 100%;
  display: block;
}
.gallery-item .item-info {
  padding: 10px 12px;
  font-size: 12px;
  color: var(--text2);
}
.gallery-item .item-info .item-label {
  display: block;
  margin-bottom: 4px;
  color: var(--text);
  font-weight: 500;
}
.gallery-item .item-actions {
  display: flex;
  gap: 6px;
}
.gallery-item .item-actions a {
  color: var(--primary);
  text-decoration: none;
  font-size: 12px;
}
.gallery-item .item-actions a:hover {
  text-decoration: underline;
}

.result-info {
  margin-top: 16px;
  padding: 12px;
  background: var(--surface2);
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: var(--text2);
  display: none;
}

/* ── History ── */
.history-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.history-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}
.history-item .history-prompt {
  font-weight: 500;
  margin-bottom: 4px;
}
.history-item .history-meta {
  font-size: 12px;
  color: var(--text2);
  margin-bottom: 8px;
}
.history-item .history-images {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.history-item .history-images img,
.history-item .history-images video {
  width: 120px;
  border-radius: 4px;
}

/* ── Models ── */
.model-list {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.model-category {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}
.model-category h3 {
  font-size: 15px;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.model-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 10px;
}
.model-card {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
  font-size: 13px;
}
.model-card .model-name {
  font-weight: 600;
  color: var(--text);
}
.model-card .model-tag {
  display: inline-block;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 11px;
  background: var(--primary);
  color: #fff;
  margin-left: 6px;
}
.model-card .model-specs {
  margin-top: 6px;
  font-size: 12px;
  color: var(--text2);
}
.model-card .model-specs span {
  display: inline-block;
  margin-right: 8px;
}

/* ── Keys 管理 ── */
.keys-layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 20px;
}
.keys-left {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}
.keys-right {
  overflow-y: auto;
}
.key-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 12px;
}
.key-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px;
  font-size: 13px;
}
.key-card.key-current {
  border-color: var(--primary);
}
.key-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
}
.key-label {
  font-weight: 600;
}
.key-tag {
  display: inline-block;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 11px;
  background: var(--primary);
  color: #fff;
}
.key-mask {
  font-family: monospace;
  color: var(--text2);
  font-size: 12px;
  margin-bottom: 6px;
}
.key-credits {
  margin-bottom: 4px;
}
.key-meta {
  font-size: 11px;
  color: var(--text2);
  margin-bottom: 8px;
}
.btn-danger {
  border-color: var(--error) !important;
  color: var(--error) !important;
}
.btn-danger:hover {
  background: rgba(239,68,68,0.1) !important;
}

/* ── Scrollbar ── */
::-webkit-scrollbar {
  width: 6px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 3px;
}
::-webkit-scrollbar-thumb:hover {
  background: var(--text2);
}

/* ── Responsive ── */
@media (max-width: 900px) {
  .generate-layout {
    grid-template-columns: 1fr;
  }
  .sidebar {
    width: 64px;
    overflow: hidden;
  }
  .sidebar .logo h1,
  .sidebar .nav-item span:last-child,
  .sidebar .config-section {
    display: none;
  }
}
