/* ===== CSS Variables ===== */
:root {
  --bg-primary: #1a1a2e;
  --bg-secondary: #16213e;
  --bg-card: #1e2a45;
  --bg-input: #0f1629;
  --bg-hover: #253555;
  --text-primary: #e0e6f0;
  --text-secondary: #8892a8;
  --text-muted: #5a6478;
  --border: #2a3555;
  --border-focus: #4a7dff;
  --accent: #4a7dff;
  --accent-hover: #6190ff;
  --accent-bg: rgba(74, 125, 255, 0.12);
  --danger: #ff4757;
  --danger-hover: #ff6b7a;
  --success: #2ed573;
  --warning: #ffa502;
  --orange: #e67e22;
  --orange-hover: #f39c12;
  --radius: 8px;
  --radius-lg: 12px;
  --shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
  --transition: 0.2s ease;
}

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

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

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

/* ===== Header ===== */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
}

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

.header-left .logo { font-size: 24px; }

.header-left h1 {
  font-size: 18px;
  font-weight: 600;
  background: linear-gradient(135deg, #4a7dff, #a855f7);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.header-right {
  display: flex;
  gap: 8px;
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 16px;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:disabled {
  background: var(--text-muted);
  cursor: not-allowed;
}

.btn-outline {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
}
.btn-outline:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}
.btn-outline:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

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

.btn-accent {
  background: var(--orange);
  color: #fff;
}
.btn-accent:hover { background: var(--orange-hover); }

.btn-sm {
  padding: 4px 12px;
  font-size: 12px;
}

.btn-icon {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius);
  transition: all var(--transition);
  display: flex;
  align-items: center;
}
.btn-icon:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.btn-icon-sm {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px;
  font-size: 14px;
  border-radius: 4px;
  transition: all var(--transition);
}
.btn-icon-sm:hover { color: var(--text-primary); }

/* ===== Settings Panel ===== */
.settings-panel {
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  padding: 16px 24px;
  display: none;
}
.settings-panel.open { display: block; }

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

.setting-group label {
  display: block;
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 6px;
  font-weight: 500;
}

.setting-group input {
  width: 100%;
  padding: 8px 12px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 14px;
  transition: border-color var(--transition);
}
.setting-group input:focus {
  outline: none;
  border-color: var(--border-focus);
}

.input-wrapper {
  position: relative;
}
.input-wrapper input { padding-right: 36px; }
.input-wrapper .btn-icon-sm {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
}

.settings-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.save-indicator {
  font-size: 12px;
  color: var(--success);
  animation: fadeInOut 2s forwards;
}

@keyframes fadeInOut {
  0% { opacity: 0; }
  20% { opacity: 1; }
  80% { opacity: 1; }
  100% { opacity: 0; }
}

/* ===== Tasks Container ===== */
.tasks-container {
  display: flex;
  gap: 20px;
  padding: 20px 24px;
  overflow-x: auto;
  min-height: calc(100vh - 120px);
  align-items: flex-start;
}

/* ===== Task Card ===== */
.task-card {
  flex: 0 0 340px;
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  overflow: hidden;
  transition: box-shadow var(--transition);
}
.task-card:hover { box-shadow: var(--shadow); }
.task-card.generating {
  border-color: var(--accent);
  box-shadow: 0 0 20px rgba(74, 125, 255, 0.15);
}
.task-card.error {
  border-color: var(--danger);
}
.task-card.success {
  border-color: var(--success);
}

.task-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: rgba(0, 0, 0, 0.2);
  border-bottom: 1px solid var(--border);
}

.task-title {
  font-size: 15px;
  font-weight: 600;
}

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

.task-body {
  padding: 12px 16px 16px;
}

.task-body.collapsed {
  display: none;
}

/* ===== Reference Images ===== */
.ref-images-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 8px;
}

.ref-image-slot {
  aspect-ratio: 1;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all var(--transition);
  position: relative;
  overflow: hidden;
}
.ref-image-slot:hover {
  border-color: var(--accent);
  background: var(--accent-bg);
}
.ref-image-slot.has-image {
  border-style: solid;
  border-color: var(--border);
}

.ref-image-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  color: var(--text-muted);
  font-size: 10px;
}

.ref-image-preview {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: var(--radius);
}

.ref-image-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  border: none;
  border-radius: 50%;
  width: 18px;
  height: 18px;
  font-size: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition);
}
.ref-image-remove:hover { background: var(--danger); }

.ref-images-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

.ref-count {
  margin-left: auto;
  font-size: 12px;
  color: var(--text-muted);
}

/* ===== Result Area ===== */
.result-area {
  min-height: 180px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 12px;
  overflow: hidden;
  position: relative;
  background: var(--bg-input);
}

.result-placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  color: var(--text-muted);
}
.result-placeholder .result-icon { font-size: 48px; }

.result-images {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 8px;
  padding: 8px;
  width: 100%;
}

.result-image-wrapper {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  transition: transform var(--transition);
}
.result-image-wrapper:hover { transform: scale(1.02); }

.result-image-wrapper img {
  width: 100%;
  height: auto;
  display: block;
  border-radius: var(--radius);
}

.result-image-actions {
  position: absolute;
  bottom: 4px;
  right: 4px;
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity var(--transition);
}
.result-image-wrapper:hover .result-image-actions { opacity: 1; }

.result-image-actions button {
  background: rgba(0, 0, 0, 0.7);
  color: #fff;
  border: none;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 11px;
  cursor: pointer;
}

/* Loading */
.result-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 20px;
}

.spinner {
  width: 36px;
  height: 36px;
  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); } }

.loading-text {
  font-size: 13px;
  color: var(--text-secondary);
}

.loading-time {
  font-size: 12px;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

/* Error */
.result-error {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 16px;
  text-align: center;
  width: 100%;
}

.error-icon { font-size: 32px; }

.error-message {
  color: var(--danger);
  font-size: 13px;
  word-break: break-word;
}

.error-detail {
  width: 100%;
  max-height: 200px;
  overflow-y: auto;
  background: rgba(0, 0, 0, 0.3);
  padding: 8px;
  border-radius: var(--radius);
  font-size: 11px;
  color: var(--text-secondary);
  white-space: pre-wrap;
  word-break: break-all;
  text-align: left;
}

/* ===== Prompt Section ===== */
.prompt-section {
  margin-bottom: 12px;
}

.prompt-input {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 13px;
  line-height: 1.5;
  resize: vertical;
  min-height: 60px;
  font-family: inherit;
  transition: border-color var(--transition);
}
.prompt-input:focus {
  outline: none;
  border-color: var(--border-focus);
}
.prompt-input::placeholder { color: var(--text-muted); }

/* ===== Controls Section ===== */
.controls-section {
  margin-bottom: 12px;
}

.controls-row {
  display: flex;
  gap: 8px;
  align-items: end;
  margin-bottom: 8px;
}

.control-group {
  flex: 1;
}

.control-group label {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.control-group select {
  width: 100%;
  padding: 6px 8px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 12px;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  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='%238892a8' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  padding-right: 24px;
}
.control-group select:focus {
  outline: none;
  border-color: var(--border-focus);
}

.model-display {
  display: block;
  padding: 4px 8px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-muted);
  font-size: 10px;
  font-family: 'SF Mono', 'Fira Code', monospace;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.control-group-wide { flex: 2; }

.provider-select {
  font-weight: 600;
}

/* ===== Action Section ===== */
.action-section {
  display: flex;
  gap: 8px;
  align-items: center;
}

.generate-btn {
  flex: 1;
}

/* ===== Add Task Area ===== */
.add-task-area {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 50;
}

.add-task-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 28px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(74, 125, 255, 0.4);
  transition: all var(--transition);
}
.add-task-btn:hover {
  background: var(--accent-hover);
  transform: translateY(-2px);
  box-shadow: 0 6px 24px rgba(74, 125, 255, 0.5);
}

/* ===== Debug Panel ===== */
.debug-panel {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 320px;
  background: var(--bg-secondary);
  border-top: 2px solid var(--accent);
  z-index: 200;
  display: flex;
  flex-direction: column;
  transition: height var(--transition);
}

.debug-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
}
.debug-header h3 {
  font-size: 14px;
  font-weight: 600;
}

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

.debug-filters {
  display: flex;
  gap: 4px;
  padding: 6px 16px;
  border-bottom: 1px solid var(--border);
}

.debug-filter {
  padding: 3px 10px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text-muted);
  font-size: 11px;
  cursor: pointer;
  transition: all var(--transition);
}
.debug-filter:hover { color: var(--text-primary); }
.debug-filter.active {
  background: var(--accent-bg);
  border-color: var(--accent);
  color: var(--accent);
}

.debug-logs {
  flex: 1;
  overflow-y: auto;
  padding: 8px 16px;
  font-family: 'SF Mono', 'Fira Code', 'Cascadia Code', monospace;
  font-size: 12px;
  line-height: 1.6;
}

.debug-entry {
  padding: 3px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.03);
  display: flex;
  gap: 8px;
}

.debug-entry.hidden { display: none; }

.debug-time {
  color: var(--text-muted);
  white-space: nowrap;
  flex-shrink: 0;
}

.debug-level {
  padding: 0 6px;
  border-radius: 3px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  white-space: nowrap;
  flex-shrink: 0;
}
.debug-level.info { color: var(--accent); background: var(--accent-bg); }
.debug-level.warn { color: var(--warning); background: rgba(255, 165, 2, 0.12); }
.debug-level.error { color: var(--danger); background: rgba(255, 71, 87, 0.12); }
.debug-level.request { color: var(--success); background: rgba(46, 213, 115, 0.12); }

.debug-msg {
  color: var(--text-secondary);
  word-break: break-all;
}
.debug-msg .highlight { color: var(--text-primary); font-weight: 500; }

.debug-entry .debug-expand-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 10px;
  padding: 0 4px;
}
.debug-entry .debug-expand-btn:hover { color: var(--accent); }

.debug-detail {
  padding: 4px 0 4px 140px;
  font-size: 11px;
  color: var(--text-muted);
  white-space: pre-wrap;
  word-break: break-all;
}

/* ===== Modal ===== */
.modal {
  position: fixed;
  inset: 0;
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.8);
}

.modal-content {
  position: relative;
  background: var(--bg-card);
  border-radius: var(--radius-lg);
  max-width: 90vw;
  max-height: 90vh;
  overflow: auto;
}

.modal-close {
  position: absolute;
  top: 8px;
  right: 8px;
  background: rgba(0, 0, 0, 0.5);
  color: #fff;
  border: none;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  font-size: 16px;
  cursor: pointer;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.image-preview-content {
  padding: 0;
}
.image-preview-content img {
  max-width: 85vw;
  max-height: 80vh;
  display: block;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
}

.preview-actions {
  padding: 12px 16px;
  display: flex;
  justify-content: center;
}

/* ===== Toast Notification ===== */
.toast-container {
  position: fixed;
  top: 60px;
  right: 24px;
  z-index: 400;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 10px 16px;
  border-radius: var(--radius);
  font-size: 13px;
  animation: toastIn 0.3s ease, toastOut 0.3s ease 3.7s forwards;
  max-width: 360px;
  word-break: break-word;
  box-shadow: var(--shadow);
}
.toast.info { background: var(--accent); color: #fff; }
.toast.success { background: var(--success); color: #fff; }
.toast.warning { background: var(--warning); color: #1a1a2e; }
.toast.error { background: var(--danger); color: #fff; }

@keyframes toastIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}
@keyframes toastOut {
  from { opacity: 1; }
  to { opacity: 0; }
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
  .settings-grid { grid-template-columns: 1fr; }
  .tasks-container { padding: 12px; gap: 12px; }
  .task-card { flex: 0 0 300px; }
  .controls-row { flex-wrap: wrap; }
}

@media (max-width: 480px) {
  .app-header { padding: 10px 12px; }
  .header-left h1 { font-size: 15px; }
  .task-card { flex: 0 0 280px; }
}
