:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e4e7eb;
  --border-strong: #d0d4da;
  --text: #1a1d23;
  --text-2: #5a6270;
  --text-3: #8a92a0;
  --primary: #4361ee;
  --primary-hover: #3a56d4;
  --primary-soft: #eef2ff;
  --danger: #e5484d;
  --danger-soft: #fff0f0;
  --success: #2b9d4f;
  --radius: 10px;
  --radius-sm: 6px;
  --radius-lg: 16px;
  --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-lg: 0 10px 40px rgba(0,0,0,0.12);
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

button { font-family: inherit; cursor: pointer; }
input, select, textarea { font-family: inherit; }

/* ===== 认证页 ===== */
.auth-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}
.auth-card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 40px 36px;
  width: 100%;
  max-width: 380px;
  box-shadow: var(--shadow-lg);
  text-align: center;
}
.auth-logo {
  font-size: 24px;
  font-weight: 700;
  color: var(--primary);
  margin-bottom: 4px;
}
.auth-sub { color: var(--text-2); font-size: 14px; margin-bottom: 28px; }
.auth-form { display: flex; flex-direction: column; gap: 16px; }
.auth-error { color: var(--danger); font-size: 13px; min-height: 18px; }
.auth-hint { color: var(--text-3); font-size: 12px; margin-top: 20px; line-height: 1.5; }

.field { display: flex; flex-direction: column; gap: 6px; text-align: left; }
.field-label { font-size: 13px; font-weight: 500; color: var(--text-2); }
.field input, .field select, .field textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  background: var(--surface);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
}
.field input:focus, .field select:focus, .field textarea:focus {
  border-color: var(--primary);
}

/* ===== 按钮 ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 18px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  font-weight: 500;
  transition: all 0.15s;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn-block { width: 100%; padding: 11px; }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.btn-danger { color: var(--danger); border-color: var(--border-strong); }
.btn-danger:hover { background: var(--danger-soft); border-color: var(--danger); }
.btn-ghost { border-color: transparent; }
.btn-ghost:hover { background: var(--surface-2); }

/* ===== 主框架 ===== */
.app { min-height: 100vh; display: flex; flex-direction: column; }
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  height: 56px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 50;
}
.topbar-brand { font-weight: 700; font-size: 17px; color: var(--primary); }
.topbar-nav { display: flex; gap: 4px; align-items: center; }
.nav-tab {
  padding: 8px 14px;
  border: none;
  background: transparent;
  border-radius: var(--radius-sm);
  font-size: 14px;
  color: var(--text-2);
  cursor: pointer;
}
.nav-tab:hover { background: var(--surface-2); color: var(--text); }
.nav-tab.active { background: var(--primary-soft); color: var(--primary); font-weight: 500; }
.topbar-right { display: flex; align-items: center; gap: 12px; }
.user-chip { font-size: 13px; color: var(--text-2); }
.role-badge {
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 4px;
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: 500;
}
.role-badge.admin { background: #fff4e6; color: #c4610a; }

/* ===== 主界面布局 ===== */
.main { display: flex; flex: 1; overflow: hidden; }
.panel {
  width: 360px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 20px;
  overflow-y: auto;
  flex-shrink: 0;
}
.content { flex: 1; overflow-y: auto; padding: 24px; }

.section-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 12px;
}
.panel-block { margin-bottom: 24px; }
.panel-block .field { margin-bottom: 14px; }

/* 提示词输入框（核心输入区，单独强化） */
#prompt {
  width: 100%;
  min-height: 180px;
  padding: 12px 14px;
  border: 1.5px solid var(--border-strong);
  border-radius: var(--radius);
  font-size: 14px;
  line-height: 1.65;
  background: var(--surface);
  color: var(--text);
  outline: none;
  resize: vertical;
  transition: border-color 0.15s, box-shadow 0.15s;
}
#prompt:hover { border-color: var(--primary); }
#prompt:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}
#prompt::placeholder { color: var(--text-3); }

.mode-switch { display: flex; gap: 4px; background: var(--surface-2); padding: 3px; border-radius: var(--radius-sm); margin-bottom: 16px; }
.mode-switch button {
  flex: 1;
  padding: 7px;
  border: none;
  background: transparent;
  border-radius: 4px;
  font-size: 13px;
  color: var(--text-2);
}
.mode-switch button.active { background: var(--surface); color: var(--text); font-weight: 500; box-shadow: var(--shadow); }

/* 风格预设 */
.style-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.style-chip {
  padding: 8px 4px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius-sm);
  font-size: 12px;
  color: var(--text-2);
  cursor: pointer;
  text-align: center;
}
.style-chip:hover { border-color: var(--border-strong); }
.style-chip.active { background: var(--primary-soft); border-color: var(--primary); color: var(--primary); }

/* 尺寸/批量按钮组 */
.btn-group { display: flex; gap: 6px; flex-wrap: wrap; }
.btn-group button {
  flex: 1;
  min-width: 0;
  padding: 8px 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: var(--text-2);
  cursor: pointer;
}
.btn-group button.active { background: var(--primary); border-color: var(--primary); color: #fff; }

.upload-zone {
  border: 2px dashed var(--border-strong);
  border-radius: var(--radius);
  padding: 24px;
  text-align: center;
  color: var(--text-3);
  font-size: 13px;
  cursor: pointer;
  transition: border-color 0.15s;
}
.upload-zone:hover { border-color: var(--primary); color: var(--primary); }
.upload-preview { max-width: 100%; max-height: 160px; border-radius: var(--radius-sm); margin-top: 10px; }

/* 参考图缩略图（最多4张，带删除叉叉） */
.upload-previews {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-top: 12px;
}
.upload-previews:empty { display: none; margin-top: 0; }
.ref-thumb {
  position: relative;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--surface-2);
}
.ref-thumb img {
  width: 100%;
  height: 110px;
  object-fit: cover;
  display: block;
}
.ref-remove {
  position: absolute;
  top: 5px;
  right: 5px;
  width: 22px;
  height: 22px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  transition: background 0.15s;
}
.ref-remove:hover { background: var(--danger); }

/* 滑块 */
.range-row { display: flex; align-items: center; gap: 10px; }
.range-row input[type=range] { flex: 1; }
.range-val { font-size: 13px; color: var(--text-2); min-width: 36px; text-align: right; font-variant-numeric: tabular-nums; }

/* ===== 结果展示 ===== */
.view-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.view-title { font-size: 18px; font-weight: 600; }

.result-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; }
.result-empty { text-align: center; color: var(--text-3); padding: 80px 20px; font-size: 14px; }

.img-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  break-inside: avoid;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}
.img-card img { width: 100%; display: block; cursor: zoom-in; }
.img-meta { padding: 10px 12px; }
.img-prompt { font-size: 12px; color: var(--text-2); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.img-actions { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.img-actions .btn-sm { padding: 4px 9px; font-size: 12px; }

.loading-state { display: flex; flex-direction: column; align-items: center; gap: 12px; padding: 60px; color: var(--text-3); }
.spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== 生成任务条 ===== */
.task-list { display: flex; flex-direction: column; gap: 14px; }
.task-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 14px 18px;
  box-shadow: var(--shadow);
}
.task-card.task-running { border-left-color: var(--primary); }
.task-card.task-done { border-left-color: var(--success); }
.task-card.task-failed { border-left-color: var(--danger); }
.task-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.task-status-chip {
  flex-shrink: 0;
  font-size: 12px;
  font-weight: 500;
  padding: 2px 10px;
  border-radius: 10px;
}
.task-running .task-status-chip { background: var(--primary-soft); color: var(--primary); }
.task-done .task-status-chip { background: #e8f7ee; color: var(--success); }
.task-failed .task-status-chip { background: var(--danger-soft); color: var(--danger); }
.task-prompt {
  font-size: 14px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 46%;
}
.task-meta { font-size: 12px; color: var(--text-3); margin-left: auto; flex-shrink: 0; }
.task-progress { display: flex; align-items: center; gap: 12px; margin-top: 12px; }
.task-progress-bar {
  flex: 1;
  height: 6px;
  background: var(--surface-2);
  border-radius: 3px;
  overflow: hidden;
}
.task-progress-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), #6d8bff);
  border-radius: 3px;
  transition: width 0.4s ease;
}
.task-progress-text { font-size: 12px; color: var(--text-2); flex-shrink: 0; font-variant-numeric: tabular-nums; }
.task-error { margin-top: 10px; font-size: 13px; color: var(--danger); }
.task-images { display: flex; gap: 10px; margin-top: 12px; flex-wrap: wrap; }
.task-images img {
  width: 96px;
  height: 96px;
  object-fit: cover;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  cursor: zoom-in;
  transition: transform 0.15s;
}
.task-images img:hover { transform: scale(1.04); }

/* 右下角任务悬浮条 */
.task-float {
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 120;
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 24px;
  padding: 10px 20px;
  box-shadow: var(--shadow-lg);
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  animation: toast-in 0.25s;
}
.task-float:hover { border-color: var(--primary); color: var(--primary); }
.task-float-spinner {
  width: 14px;
  height: 14px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  flex-shrink: 0;
}

/* 画廊瀑布流 */
.masonry { columns: 4; column-gap: 16px; }
@media (max-width: 1200px) { .masonry { columns: 3; } }
@media (max-width: 900px) { .masonry { columns: 2; } }

/* 生成结果大图网格 */
.result-grid-lg {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(360px, 1fr));
  gap: 20px;
}
.result-grid-lg .img-card { margin-bottom: 0; }
.result-grid-lg .img-prompt { font-size: 13px; -webkit-line-clamp: 2; }

/* 大图查看 */
.lightbox {
  position: fixed; inset: 0; background: rgba(0,0,0,0.85);
  display: flex; align-items: center; justify-content: center;
  z-index: 200; padding: 24px;
}
.lightbox img { max-width: 90vw; max-height: 90vh; border-radius: 8px; }
.lightbox-close {
  position: absolute; top: 16px; right: 20px; color: #fff;
  font-size: 28px; background: none; border: none; cursor: pointer;
}
.lightbox-dl {
  position: absolute; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: rgba(255,255,255,0.92); border-color: transparent;
}
.lightbox-dl:hover { background: #fff; }

/* ===== 管理后台 ===== */
.admin-table { width: 100%; border-collapse: collapse; background: var(--surface); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.admin-table th, .admin-table td { padding: 12px 16px; text-align: left; font-size: 14px; border-bottom: 1px solid var(--border); }
.admin-table th { background: var(--surface-2); color: var(--text-2); font-weight: 500; font-size: 13px; }
.admin-table tr:last-child td { border-bottom: none; }
.status-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; }
.status-on { background: var(--success); }
.status-off { background: var(--danger); }

.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.4);
  display: flex; align-items: center; justify-content: center; z-index: 150;
}
.modal {
  background: var(--surface); border-radius: var(--radius-lg);
  padding: 28px; width: 100%; max-width: 420px; box-shadow: var(--shadow-lg);
}
.modal h3 { font-size: 17px; margin-bottom: 18px; }
.modal .field { margin-bottom: 14px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: #fff; padding: 10px 20px;
  border-radius: var(--radius-sm); font-size: 14px; z-index: 300;
  animation: toast-in 0.2s;
}
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 10px); } }

/* ===== API 管理 ===== */
.api-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
  margin-bottom: 14px;
  box-shadow: var(--shadow);
}
.api-card.api-default { border-color: var(--primary); border-width: 1.5px; }
.api-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; flex-wrap: wrap; gap: 8px; }
.api-name { font-size: 15px; font-weight: 600; margin-right: 8px; }
.api-info { font-size: 13px; color: var(--text-2); margin: 4px 0; word-break: break-all; }
.model-tag {
  display: inline-block;
  padding: 2px 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 12px;
  color: var(--text);
  margin: 2px 4px 2px 0;
}

/* 手机专用元素默认隐藏，电脑端沿用原来的布局。 */
.mobile-only { display: none; }

@media (max-width: 768px) {
  .mobile-only { display: inline-flex; }
  .desktop-only { display: none; }
  .app { min-height: 100svh; }

  /* 品牌和账户占一行，导航独立一行；管理入口可横向滑动。 */
  .topbar {
    height: auto;
    min-height: 100px;
    flex-wrap: wrap;
    gap: 6px 12px;
    padding: 10px 16px 8px;
    padding-top: max(10px, env(safe-area-inset-top));
  }
  .topbar-brand { font-size: 17px; white-space: nowrap; }
  .account-toggle { min-height: 36px; gap: 8px; margin-left: auto; }
  .topbar-nav {
    order: 3;
    width: 100%;
    min-width: 0;
    gap: 6px;
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scrollbar-width: none;
  }
  .topbar-nav::-webkit-scrollbar { display: none; }
  .nav-tab { flex: 1 0 auto; min-height: 42px; padding: 9px 16px; white-space: nowrap; }
  .topbar-right {
    display: none;
    position: absolute;
    top: 54px;
    right: 16px;
    width: min(260px, calc(100vw - 32px));
    padding: 12px;
    gap: 10px;
    flex-wrap: wrap;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    z-index: 1;
  }
  .topbar-right.is-open { display: flex; }
  .user-chip { width: 100%; overflow-wrap: anywhere; }
  .topbar-right .btn { flex: 1; min-height: 42px; white-space: nowrap; }

  /* 只保留整页滚动，避免表单被半屏高度截断。 */
  .main { flex-direction: column; overflow: visible; min-width: 0; }
  .panel {
    width: 100%;
    max-height: none;
    overflow: visible;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 16px;
    scroll-margin-top: 112px;
  }
  .app:not([data-view="generate"]) .panel { display: none; }
  .content {
    flex: none;
    min-width: 0;
    width: 100%;
    overflow: visible;
    padding: 20px 16px max(88px, calc(72px + env(safe-area-inset-bottom)));
    scroll-margin-top: 112px;
  }
  .panel-block { margin-bottom: 18px; }
  .panel-block .field { margin-bottom: 0; }
  .section-title { margin-bottom: 8px; color: var(--text-2); letter-spacing: 0; }
  .mode-switch { margin-bottom: 18px; border-radius: 10px; }
  .mode-switch button { min-height: 42px; font-size: 14px; border-radius: 7px; }
  .field input, .field select, .field textarea { min-width: 0; min-height: 44px; font-size: 16px; }
  #prompt { display: block; min-height: 128px; font-size: 16px; padding: 12px; }
  .style-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; }
  .style-chip { min-height: 42px; padding: 8px 2px; font-size: 13px; }
  .btn-group { gap: 8px; }
  .btn-group button { min-height: 42px; padding: 8px 4px; white-space: nowrap; }
  .range-row input { min-width: 0; min-height: 40px; }
  .upload-zone { padding: 20px 12px; }
  .ref-remove { width: 32px; height: 32px; font-size: 20px; }
  #genBtn { min-height: 48px; border-radius: 10px; font-size: 16px; }
  .view-head { gap: 12px; flex-wrap: wrap; }
  .view-title { font-size: 17px; }
  .view-head .btn { min-height: 40px; white-space: nowrap; }
  .result-empty { padding: 28px 8px; font-size: 14px; }
  .result-empty .mobile-only { display: inline; }
  .loading-state { padding: 32px 16px; }

  .masonry { columns: 2; column-gap: 12px; }
  .result-grid, .result-grid-lg { grid-template-columns: minmax(0, 1fr); gap: 12px; }
  .img-card { margin-bottom: 12px; }
  .img-meta { padding: 10px; }
  .img-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
  .img-actions .btn-sm { min-height: 40px; padding: 6px 2px; white-space: nowrap; }
  .task-card { padding: 12px; min-width: 0; }
  .task-head { gap: 8px; }
  .task-prompt { flex: 1; min-width: 0; max-width: none; }
  .task-meta { width: 100%; margin-left: 0; overflow-wrap: anywhere; }
  .task-error { overflow-wrap: anywhere; }
  .task-images { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
  .task-images img { width: 100%; height: auto; aspect-ratio: 1; }
  .task-float {
    right: 16px;
    bottom: max(12px, env(safe-area-inset-bottom));
    max-width: calc(100vw - 32px);
    min-height: 44px;
    padding: 10px 14px;
  }
  .toast { bottom: max(70px, calc(60px + env(safe-area-inset-bottom))); width: max-content; max-width: calc(100vw - 32px); text-align: center; }

  .modal-overlay { padding: 16px; }
  .modal { padding: 22px 18px; max-height: calc(100dvh - 32px); overflow-y: auto; }
  .modal-actions .btn { min-height: 44px; }
  .lightbox { padding: 64px 16px 88px; }
  .lightbox img { max-width: 100%; max-height: calc(100dvh - 152px); }
  .lightbox-close { min-width: 44px; min-height: 44px; right: 12px; top: max(12px, env(safe-area-inset-top)); }
  .lightbox-dl { min-height: 44px; bottom: max(24px, env(safe-area-inset-bottom)); }
  #adminList { max-width: 100%; overflow-x: auto; }
  .admin-table { min-width: 560px; }
  .admin-table .btn { min-height: 40px; white-space: nowrap; }
  .api-card { padding: 14px; }
  .api-head > div { max-width: 100%; display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
  .api-head .btn { min-height: 40px; }
  .api-name { overflow-wrap: anywhere; }
  .auth-page { min-height: 100svh; padding: 24px 16px; }
  .auth-card { padding: 28px 24px; }
  .auth-form .btn { min-height: 46px; font-size: 16px; }
}

@media (max-width: 359px) {
  .topbar { padding-left: 12px; padding-right: 12px; }
  .panel { padding: 14px 12px; }
  .content { padding-left: 12px; padding-right: 12px; }
  .masonry { columns: 1; }
}
