/* ========== Reset & Base ========== */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg-primary: #0f0f1a;
  --bg-secondary: #1a1a2e;
  --bg-tertiary: #252540;
  --text-primary: #e8e8f0;
  --text-secondary: #9898b0;
  --accent: #6c63ff;
  --accent-hover: #7b73ff;
  --accent-glow: rgba(108, 99, 255, 0.3);
  --border: #2a2a45;
  --success: #4ade80;
  --radius: 12px;
  --radius-sm: 8px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

/* ========== App Layout ========== */
.app {
  max-width: 1400px;
  margin: 0 auto;
  padding: 20px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-header {
  text-align: center;
  padding: 24px 0 32px;
  position: relative;
}

.back-link {
  position: absolute;
  left: 0;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 0.85rem;
  transition: color 0.2s;
}
.back-link:hover {
  color: var(--accent);
}

.app-header h1 {
  font-size: 2rem;
  font-weight: 700;
  background: linear-gradient(135deg, #6c63ff, #a78bfa);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.subtitle {
  color: var(--text-secondary);
  font-size: 0.9rem;
  margin-top: 4px;
}

.main-layout {
  display: grid;
  grid-template-columns: 340px 1fr;
  gap: 24px;
  flex: 1;
}

/* ========== Panels ========== */
.panel {
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
}

.panel-controls {
  display: flex;
  flex-direction: column;
  gap: 20px;
  height: fit-content;
  position: sticky;
  top: 20px;
}

/* ========== Controls ========== */
.control-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.control-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: 8px;
}

.control-row {
  display: flex;
  gap: 12px;
}

.flex-1 {
  flex: 1;
}

.input {
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  padding: 10px 14px;
  font-size: 0.9rem;
  width: 100%;
  transition: border-color 0.2s;
}

.input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.prompt-input {
  resize: vertical;
  min-height: 80px;
  font-family: inherit;
}

.number-input {
  -moz-appearance: textfield;
}

.number-input::-webkit-inner-spin-button,
.number-input::-webkit-outer-spin-button {
  -webkit-appearance: none;
}

.hint {
  font-size: 0.75rem;
  color: var(--text-secondary);
}

.text-center {
  text-align: center;
}

/* ========== Model Tabs ========== */
.model-tabs {
  display: flex;
  gap: 8px;
}

.model-tab {
  flex: 1;
  padding: 10px 0;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.model-tab:hover {
  border-color: var(--accent);
  color: var(--text-primary);
}

.model-tab.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  box-shadow: 0 0 12px var(--accent-glow);
}

/* ========== Seed ========== */
.seed-random {
  cursor: pointer;
  font-size: 1rem;
  transition: transform 0.2s;
}

.seed-random:hover {
  transform: rotate(180deg);
}

/* ========== Button ========== */
.btn {
  padding: 12px 24px;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.btn-generate {
  background: linear-gradient(135deg, #6c63ff, #8b5cf6);
  color: #fff;
  font-size: 1rem;
  padding: 14px;
  box-shadow: 0 4px 16px var(--accent-glow);
}

.btn-generate:hover {
  transform: translateY(-1px);
  box-shadow: 0 6px 24px var(--accent-glow);
}

.btn-generate:active {
  transform: translateY(0);
}

.btn-generate:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
}

.btn-download {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: 1px solid var(--border);
}

.btn-download:hover {
  border-color: var(--accent);
}

/* ========== Preview Area ========== */
.preview-area {
  position: relative;
  background: var(--bg-tertiary);
  border-radius: var(--radius-sm);
  min-height: 400px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.placeholder {
  text-align: center;
  color: var(--text-secondary);
}

.placeholder-icon {
  font-size: 3rem;
  margin-bottom: 12px;
  opacity: 0.5;
}

.result-image {
  max-width: 100%;
  max-height: 70vh;
  object-fit: contain;
  border-radius: var(--radius-sm);
}

.hidden {
  display: none !important;
}

/* ========== Loading ========== */
.loading-overlay {
  position: absolute;
  inset: 0;
  background: rgba(15, 15, 26, 0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: 10;
}

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

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

/* ========== Preview Actions ========== */
.preview-actions {
  display: flex;
  gap: 12px;
  padding-top: 16px;
}

/* ========== History ========== */
.history-section {
  margin-top: 32px;
}

.section-title {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 12px;
  color: var(--text-primary);
}

.history-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 10px;
}

.history-item {
  position: relative;
  border-radius: var(--radius-sm);
  overflow: hidden;
  cursor: pointer;
  aspect-ratio: 1;
  border: 2px solid transparent;
  transition: all 0.2s;
}

.history-item:hover {
  border-color: var(--accent);
  transform: scale(1.03);
}

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

.history-item .history-model {
  position: absolute;
  bottom: 4px;
  right: 4px;
  background: rgba(0, 0, 0, 0.7);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.65rem;
  color: #ccc;
}

/* ========== Footer ========== */
.app-footer {
  text-align: center;
  padding: 24px 0;
  color: var(--text-secondary);
  font-size: 0.85rem;
}

.app-footer a {
  color: var(--accent);
  text-decoration: none;
}

.app-footer a:hover {
  text-decoration: underline;
}

/* ========== Prompt Footer ========== */
.prompt-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.72rem;
  color: var(--text-secondary);
}

.char-count.warn {
  color: #f59e0b;
}

.char-count.danger {
  color: #ef4444;
}

.enter-hint {
  color: var(--text-secondary);
}

/* ========== Size Presets ========== */
.size-presets {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.preset-btn {
  padding: 6px 10px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s;
}

.preset-btn:hover {
  border-color: var(--accent);
  color: var(--text-primary);
}

.preset-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* ========== Error Overlay ========== */
.error-overlay {
  position: absolute;
  inset: 0;
  background: rgba(15, 15, 26, 0.9);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  z-index: 10;
  padding: 24px;
}

.error-msg {
  color: #ef4444;
  text-align: center;
  font-size: 0.9rem;
  max-width: 320px;
}

.btn-retry {
  background: var(--accent);
  color: #fff;
  padding: 10px 20px;
}

.btn-retry:hover {
  background: var(--accent-hover);
}

/* ========== Copy Button ========== */
.btn-copy {
  background: var(--bg-tertiary);
  color: var(--text-primary);
  border: 1px solid var(--border);
}

.btn-copy:hover {
  border-color: var(--accent);
}

.btn-copy.copied {
  border-color: var(--success);
  color: var(--success);
}

/* ========== Cooldown ========== */
.btn-generate.cooldown {
  background: var(--bg-tertiary);
  color: var(--text-secondary);
  box-shadow: none;
}

/* ========== Responsive ========== */
@media (max-width: 900px) {
  .main-layout {
    grid-template-columns: 1fr;
  }

  .panel-controls {
    position: static;
  }

  .app-header h1 {
    font-size: 1.5rem;
  }
}