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

body {
  min-height: 100vh;
  background: linear-gradient(135deg, #0f0c29, #302b63, #24243e);
  font-family: 'Segoe UI', system-ui, sans-serif;
  color: #e0e0e0;
  display: flex;
  gap: 24px;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 24px;
}

/* ── Card ── */
.card {
  background: rgba(255, 255, 255, 0.06);
  backdrop-filter: blur(18px);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 20px;
  padding: 36px 40px;
  width: 100%;
  max-width: 640px;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.5);
  flex-shrink: 0;
}

h1 {
  font-size: 1.7rem;
  font-weight: 700;
  letter-spacing: -0.5px;
  margin-bottom: 28px;
  background: linear-gradient(90deg, #a78bfa, #60a5fa);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

h2 {
  font-size: 1rem;
  font-weight: 600;
  color: #9ca3af;
  margin-bottom: 14px;
}

label {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: #9ca3af;
  margin-bottom: 6px;
}

input, textarea {
  width: 100%;
  background: rgba(255, 255, 255, 0.07);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 10px;
  color: #f3f4f6;
  padding: 10px 14px;
  font-size: 0.95rem;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
  margin-bottom: 18px;
}

/* API Key 输入框包裹层 */
.input-wrapper {
  position: relative;
  margin-bottom: 18px;
}
.input-wrapper input {
  margin-bottom: 0;
  padding-right: 42px;
  width: 100%;
  box-sizing: border-box;
}
.eye-btn {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  color: #9ca3af;
  padding: 4px;
  display: flex;
  align-items: center;
  transition: color 0.2s;
}
.eye-btn:hover {
  color: #a78bfa;
}

input:focus, textarea:focus {
  border-color: #a78bfa;
  box-shadow: 0 0 0 3px rgba(167, 139, 250, 0.2);
}

textarea {
  resize: vertical;
  min-height: 90px;
  font-family: inherit;
}

.source-preview {
  margin: -6px 0 16px;
  border: 1px dashed rgba(255, 255, 255, 0.2);
  border-radius: 10px;
  padding: 8px;
  background: rgba(255, 255, 255, 0.03);
}

.source-preview img {
  width: 100%;
  max-height: 220px;
  border-radius: 8px;
  object-fit: contain;
  display: block;
}

/* ── Buttons ── */
.btn-primary {
  width: 100%;
  padding: 13px;
  border: none;
  border-radius: 12px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  background: linear-gradient(135deg, #7c3aed, #2563eb);
  color: #fff;
  transition: opacity 0.2s, transform 0.1s;
  letter-spacing: 0.02em;
}

.btn-primary:hover:not(:disabled) { opacity: 0.88; }
.btn-primary:active:not(:disabled) { transform: scale(0.98); }
.btn-primary:disabled { opacity: 0.45; cursor: not-allowed; }

.primary-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.btn-edit {
  background: linear-gradient(135deg, #0d9488, #0891b2);
}

.btn-secondary {
  flex: 1;
  padding: 10px;
  border-radius: 10px;
  border: 1px solid rgba(255,255,255,0.15);
  background: rgba(255,255,255,0.07);
  color: #d1d5db;
  font-size: 0.88rem;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-secondary:hover { background: rgba(255,255,255,0.12); }

/* Retry — 琥珀色调 */
.btn-retry {
  border-color: rgba(251, 191, 36, 0.35);
  color: #fbbf24;
}
.btn-retry:hover { background: rgba(251, 191, 36, 0.12); }

/* ── Key 档位选择 ── */
.key-mode-row {
  display: flex;
  gap: 8px;
  margin-bottom: 14px;
}
.mode-btn {
  flex: 1;
  padding: 8px 4px 6px;
  border: 1px solid rgba(139, 92, 246, 0.3);
  border-radius: 8px;
  background: transparent;
  color: #6b7280;
  cursor: pointer;
  font-size: 0.76rem;
  font-weight: 600;
  line-height: 1.4;
  text-align: center;
  transition: all 0.2s;
}
.mode-btn.active {
  background: rgba(139, 92, 246, 0.18);
  border-color: #8b5cf6;
  color: #c4b5fd;
}
.mode-btn:hover:not(.active) {
  border-color: rgba(139, 92, 246, 0.55);
  color: #a78bfa;
}
.key-label-hint {
  color: #6b7280;
  font-size: 0.75em;
  font-weight: normal;
  text-transform: none;
  letter-spacing: 0;
}

.unlock-hint {
  display: block;
  margin-top: 4px;
  font-size: 0.8em;
  color: #34d399;
}

/* Use as source — 绿色调 */
.btn-use-source {
  border-color: rgba(52, 211, 153, 0.35);
  color: #34d399;
}
.btn-use-source:hover { background: rgba(52, 211, 153, 0.12); }

/* ── Status ── */
#status {
  margin-top: 16px;
  font-size: 0.88rem;
  min-height: 20px;
  text-align: center;
  color: #9ca3af;
}

#status.error   { color: #f87171; }
#status.loading { color: #60a5fa; }

.spinner {
  display: inline-block;
  width: 14px; height: 14px;
  border: 2px solid rgba(96,165,250,0.3);
  border-top-color: #60a5fa;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  margin-right: 6px;
  vertical-align: middle;
}

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

/* ── Result ── */
#result {
  margin-top: 28px;
  display: none;
}

#result img {
  width: 100%;
  border-radius: 14px;
  border: 1px solid rgba(255,255,255,0.1);
  box-shadow: 0 8px 32px rgba(0,0,0,0.4);
}

.actions {
  display: flex;
  gap: 10px;
  margin-top: 14px;
}

.save-hint {
  margin-top: 8px;
  font-size: 0.78rem;
  color: #6b7280;
  text-align: center;
}

/* ── History Panel ── */
.history-panel {
  background: rgba(255, 255, 255, 0.04);
  backdrop-filter: blur(18px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  padding: 28px 24px;
  width: 260px;
  flex-shrink: 0;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.4);
  max-height: 90vh;
  overflow-y: auto;
}

.history-panel::-webkit-scrollbar { width: 4px; }
.history-panel::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.15); border-radius: 4px; }

.history-grid {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.history-item {
  position: relative;
  border-radius: 10px;
  overflow: hidden;
  cursor: pointer;
  border: 1px solid rgba(255,255,255,0.08);
  transition: transform 0.15s, border-color 0.15s;
}

.history-item:hover {
  transform: scale(1.02);
  border-color: #a78bfa;
}

.history-item img {
  width: 100%;
  display: block;
  object-fit: cover;
  aspect-ratio: 1;
}

.history-item .ts {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 4px 6px;
  background: rgba(0,0,0,0.55);
  font-size: 0.65rem;
  color: #9ca3af;
  text-align: center;
}

@media (max-width: 960px) {
  body { flex-direction: column; align-items: center; }
  .history-panel { width: 100%; max-width: 640px; max-height: none; }
  .history-grid { flex-direction: row; flex-wrap: wrap; }
  .history-item { width: calc(33% - 7px); }
  .primary-actions { grid-template-columns: 1fr; }
  .param-row { grid-template-columns: 1fr; }
  .compare-row { grid-template-columns: 1fr; }
}

/* ── Upload hint ── */
.upload-hint {
  margin: -10px 0 14px;
  font-size: 0.75rem;
  color: #6b7280;
}

/* ── Clear source image button ── */
.clear-btn {
  display: block;
  margin: 6px 0 0 auto;
  background: none;
  border: none;
  color: #f87171;
  font-size: 0.8rem;
  cursor: pointer;
  padding: 2px 4px;
  transition: opacity 0.2s;
}
.clear-btn:hover { opacity: 0.75; }

/* ── Before/After compare panel ── */
.compare-title {
  font-size: 0.8rem;
  font-weight: 600;
  color: #9ca3af;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin: 28px 0 12px;
}

.compare-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.compare-col img {
  width: 100%;
  border-radius: 10px;
  border: 1px solid rgba(255,255,255,0.1);
  box-shadow: 0 4px 16px rgba(0,0,0,0.3);
  display: block;
}

.compare-label {
  font-size: 0.78rem;
  color: #9ca3af;
  text-align: center;
  margin-bottom: 6px;
}

/* ── Param row (size / quality selects) ── */
.param-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 18px;
}

select {
  width: 100%;
  background: rgba(255, 255, 255, 0.07);
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 10px;
  color: #f3f4f6;
  padding: 10px 36px 10px 14px;
  font-size: 0.95rem;
  outline: none;
  appearance: none;
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%239ca3af' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  transition: border-color 0.2s, box-shadow 0.2s;
}

select:focus {
  border-color: #a78bfa;
  box-shadow: 0 0 0 3px rgba(167, 139, 250, 0.2);
}

/* ── Prompt history tags ── */
.prompt-history {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: -12px 0 18px;
}

.prompt-tag {
  background: rgba(167, 139, 250, 0.12);
  border: 1px solid rgba(167, 139, 250, 0.3);
  border-radius: 20px;
  color: #c4b5fd;
  font-size: 0.78rem;
  padding: 3px 10px;
  cursor: pointer;
  transition: background 0.2s, border-color 0.2s;
  white-space: nowrap;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.prompt-tag:hover {
  background: rgba(167, 139, 250, 0.25);
  border-color: #a78bfa;
}
