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

:root {
  --bg: #1a1a2e;
  --bg2: #16213e;
  --bg3: #0f3460;
  --accent: #f5c518;
  --accent-hover: #d4a90e;
  --text: #e0e0e0;
  --text-dim: #888;
  --danger: #e74c3c;
  --success: #2ecc71;
  --radius: 10px;
}

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

/* Header */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--bg2);
  border-bottom: 2px solid var(--accent);
  position: sticky;
  top: 0;
  z-index: 100;
}

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

.logo {
  width: 36px;
  height: 36px;
}

header h1 {
  font-size: 1.3rem;
  color: var(--accent);
}

/* Buttons */
.btn {
  padding: 8px 18px;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 600;
  transition: background 0.2s, transform 0.1s;
}

.btn:active {
  transform: scale(0.97);
}

.btn-install {
  background: var(--accent);
  color: #000;
}

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

.btn-generate {
  width: 100%;
  padding: 14px;
  font-size: 1.1rem;
  background: var(--accent);
  color: #000;
}

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

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

.btn-small {
  padding: 5px 12px;
  font-size: 0.8rem;
}

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.btn-danger:hover {
  background: #c0392b;
}

/* Main */
main {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.section {
  margin-bottom: 20px;
}

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

label {
  display: block;
  margin-bottom: 6px;
  font-weight: 600;
  color: var(--accent);
}

.hint {
  font-weight: 400;
  color: var(--text-dim);
  font-size: 0.85rem;
}

/* Toggle */
.toggle-label {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 0;
}

.toggle-label input {
  display: none;
}

.toggle-switch {
  position: relative;
  width: 42px;
  height: 24px;
  background: #333;
  border-radius: 12px;
  flex-shrink: 0;
  transition: background 0.2s;
}

.toggle-switch::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  background: #888;
  border-radius: 50%;
  transition: transform 0.2s, background 0.2s;
}

.toggle-label input:checked + .toggle-switch {
  background: var(--accent);
}

.toggle-label input:checked + .toggle-switch::after {
  transform: translateX(18px);
  background: #000;
}

.toggle-label .hint {
  font-weight: 400;
}

h2 {
  color: var(--accent);
  font-size: 1.1rem;
}

/* Textarea */
textarea {
  width: 100%;
  background: var(--bg2);
  color: var(--text);
  border: 1px solid #333;
  border-radius: var(--radius);
  padding: 12px;
  font-size: 1rem;
  resize: vertical;
  font-family: inherit;
}

textarea:focus {
  outline: none;
  border-color: var(--accent);
}

/* Resolution */
.resolution-group {
  display: flex;
  gap: 8px;
}

.btn-res {
  flex: 1;
  padding: 10px;
  background: var(--bg2);
  color: var(--text);
  border: 1px solid #333;
  border-radius: var(--radius);
  cursor: pointer;
  font-weight: 600;
  transition: all 0.2s;
}

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

.btn-res:hover:not(.active) {
  border-color: var(--accent);
}

/* Drop zone */
.drop-zone {
  border: 2px dashed #444;
  border-radius: var(--radius);
  padding: 40px 20px;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s;
}

.drop-zone:hover,
.drop-zone.dragover {
  border-color: var(--accent);
  background: rgba(245, 197, 24, 0.05);
}

.drop-text {
  color: var(--text-dim);
}

.drop-text .link {
  color: var(--accent);
  text-decoration: underline;
  cursor: pointer;
}

/* Previews */
.previews {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.preview-item {
  position: relative;
  width: 80px;
  height: 80px;
  border-radius: 8px;
  overflow: hidden;
}

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

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

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

/* Queue */
.queue-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.queue-item {
  background: var(--bg2);
  border-radius: var(--radius);
  padding: 12px;
  border-left: 4px solid #555;
}

.queue-item.pending {
  border-left-color: var(--text-dim);
}

.queue-item.processing {
  border-left-color: var(--accent);
}

.queue-item.completed {
  border-left-color: var(--success);
}

.queue-item.error {
  border-left-color: var(--danger);
}

.queue-item.cancelled {
  border-left-color: #555;
  opacity: 0.6;
}

.queue-item.cancelled .status-label {
  color: #999;
}

.queue-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.queue-item-content {
  flex: 1;
  min-width: 0;
}

.queue-actions {
  flex-shrink: 0;
}

.queue-btn {
  width: 28px;
  height: 28px;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
}

.queue-btn-cancel {
  background: var(--danger);
  color: #fff;
}

.queue-btn-cancel:hover {
  background: #c0392b;
}

.queue-btn-retry {
  background: var(--accent);
  color: #000;
}

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

.btn-cancel-all {
  background: transparent;
  border: 1px solid var(--danger);
  color: var(--danger);
  padding: 4px 12px;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.8rem;
  margin-bottom: 8px;
  transition: background 0.2s, color 0.2s;
}

.btn-cancel-all:hover {
  background: var(--danger);
  color: #fff;
}

.queue-prompt {
  font-size: 0.9rem;
  margin-bottom: 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.queue-status {
  font-size: 0.8rem;
  color: var(--text-dim);
}

.queue-status .status-label {
  font-weight: 600;
}

.queue-item.processing .status-label {
  color: var(--accent);
}

.queue-item.completed .status-label {
  color: var(--success);
}

.queue-item.error .status-label {
  color: var(--danger);
}

.progress-bar {
  height: 4px;
  background: #333;
  border-radius: 2px;
  margin-top: 8px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
  transition: width 0.3s;
  animation: pulse 1.5s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

/* History */
.history-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
}

.history-card {
  background: var(--bg2);
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid #333;
}

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

.history-card-body {
  padding: 12px;
}

.history-card-prompt {
  font-size: 0.85rem;
  color: var(--text);
  margin-bottom: 6px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.history-card-meta {
  font-size: 0.75rem;
  color: var(--text-dim);
  margin-bottom: 8px;
}

.history-card-sources {
  display: flex;
  gap: 4px;
  margin-bottom: 8px;
}

.history-card-sources img {
  width: 36px;
  height: 36px;
  object-fit: cover;
  border-radius: 4px;
  border: 1px solid #444;
}

.history-card-actions {
  display: flex;
  gap: 6px;
}

.history-card-actions .btn {
  flex: 1;
  padding: 6px;
  font-size: 0.8rem;
}

.btn-rerun {
  background: var(--bg3);
  color: var(--text);
}

.btn-rerun:hover {
  background: var(--accent);
  color: #000;
}

.btn-reprompt {
  background: #8e44ad;
  color: #fff;
}

.btn-reprompt:hover {
  background: #732d91;
}

.btn-reference {
  background: #e67e22;
  color: #fff;
}

.btn-reference:hover {
  background: #d35400;
}

.btn-download {
  background: var(--success);
  color: #000;
}

.btn-download:hover {
  background: #27ae60;
}

.history-card-error {
  color: var(--danger);
  font-size: 0.8rem;
  margin-bottom: 8px;
}

/* Lightbox */
.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.9);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.lightbox[hidden] {
  display: none;
}

.lightbox img {
  max-width: 90vw;
  max-height: 90vh;
  border-radius: var(--radius);
}

.lightbox-close {
  position: absolute;
  top: 16px;
  right: 20px;
  background: none;
  border: none;
  color: #fff;
  font-size: 2rem;
  cursor: pointer;
}

/* Prompt header */
.prompt-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
}

.prompt-header label {
  margin-bottom: 0;
}

.prompt-actions {
  display: flex;
  gap: 6px;
}

.btn-fav-save {
  background: var(--bg3);
  color: var(--accent);
}

.btn-fav-save:hover {
  background: var(--accent);
  color: #000;
}

.btn-fav-open {
  background: var(--accent);
  color: #000;
}

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

/* Favorites Panel */
.favorites-panel {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.85);
  z-index: 200;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overflow-y: auto;
  padding: 40px 16px;
}

.favorites-panel[hidden] {
  display: none;
}

.favorites-container {
  background: var(--bg);
  border: 1px solid #333;
  border-radius: var(--radius);
  width: 100%;
  max-width: 700px;
  max-height: 85vh;
  overflow-y: auto;
  padding: 20px;
}

.favorites-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.favorites-header .lightbox-close {
  position: static;
  color: var(--text);
}

.favorites-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 12px;
}

.favorites-empty {
  text-align: center;
  color: var(--text-dim);
  padding: 32px 0;
}

.btn-fav-new {
  display: block;
  width: 100%;
  padding: 10px;
  margin-bottom: 12px;
  background: var(--accent);
  color: #000;
  font-weight: 700;
  font-size: 1rem;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
}

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

.favorite-card {
  background: var(--bg2);
  border: 1px solid #333;
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  transition: border-color 0.2s, transform 0.1s;
}

.favorite-card:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
}

.favorite-card-thumb {
  width: 100%;
  height: 75px;
  object-fit: cover;
  display: block;
  background: var(--bg3);
}

.favorite-card-thumb-placeholder {
  width: 100%;
  height: 75px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg3);
  color: var(--text-dim);
  font-size: 1.5rem;
}

.favorite-card-body {
  padding: 8px 10px;
}

.favorite-card-title {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.favorite-card-prompt {
  font-size: 0.75rem;
  color: var(--text-dim);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.favorite-card-actions {
  display: flex;
  gap: 4px;
  padding: 0 10px 8px;
}

.favorite-card-actions .btn {
  flex: 1;
  padding: 4px;
  font-size: 0.7rem;
}

.btn-fav-use {
  background: var(--accent);
  color: #000;
}

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

.btn-fav-edit {
  background: var(--bg3);
  color: var(--text);
}

.btn-fav-edit:hover {
  background: #1a4a8a;
}

.btn-fav-delete {
  background: var(--bg3);
  color: var(--danger);
}

.btn-fav-delete:hover {
  background: var(--danger);
  color: #fff;
}

/* Favorite Form */
.favorite-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.favorite-form label {
  color: var(--accent);
  font-size: 0.85rem;
  margin-bottom: 2px;
}

.favorite-form input[type="text"],
.favorite-form textarea {
  width: 100%;
  background: var(--bg2);
  color: var(--text);
  border: 1px solid #333;
  border-radius: var(--radius);
  padding: 10px;
  font-size: 0.9rem;
  font-family: inherit;
}

.favorite-form input[type="text"]:focus,
.favorite-form textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.favorite-form-thumb-preview {
  display: flex;
  align-items: center;
  gap: 12px;
}

.favorite-form-thumb-preview img {
  width: 150px;
  height: 75px;
  object-fit: cover;
  border-radius: 6px;
  border: 1px solid #444;
}

.favorite-form-thumb-preview .placeholder {
  width: 150px;
  height: 75px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg3);
  border-radius: 6px;
  color: var(--text-dim);
  font-size: 0.8rem;
}

.favorite-form-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.btn-fav-cancel {
  background: var(--bg3);
  color: var(--text);
}

.btn-fav-cancel:hover {
  background: #1a4a8a;
}

.btn-fav-submit {
  background: var(--accent);
  color: #000;
}

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

/* Responsive */
@media (max-width: 600px) {
  main {
    padding: 12px;
  }

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

  header h1 {
    font-size: 1.1rem;
  }

  .prompt-header {
    flex-wrap: wrap;
    gap: 6px;
  }

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

  .favorites-container {
    padding: 14px;
  }
}
