:root {
  --bg: #F0EEE6;
  --panel: #FAFAF7;
  --panel-alt: #E8E6DD;
  --border: #D8D5C8;
  --text: #1A1915;
  --text-muted: #6F6E63;
  --accent: #CC785C;
  --accent-hover: #B5664B;
  --accent-text: #FFFFFF;
  --secondary: #E8E6DD;
  --secondary-hover: #D8D5C8;
  --danger: #A33B2A;
  --danger-hover: #8A2F22;
  --success: #4F7A4A;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 2px rgba(26, 25, 21, 0.04), 0 4px 12px rgba(26, 25, 21, 0.04);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Microsoft YaHei UI", "PingFang SC", "Helvetica Neue", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body { min-height: 100vh; }

button, input, select, textarea {
  font-family: inherit;
  font-size: inherit;
  touch-action: manipulation;
}

/* ─── 登录覆盖层 ─── */
.login-gate {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.login-form {
  width: 320px;
  max-width: 90vw;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 32px 28px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.login-title { margin: 0; font-size: 20px; font-weight: 700; text-align: center; }
.login-subtitle { margin: 0; color: var(--text-muted); text-align: center; font-size: 13px; }
.login-input {
  width: 100%;
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s ease;
}
.login-input:focus { border-color: var(--accent); }
.login-error { margin: 0; color: var(--danger); font-size: 12px; text-align: center; }

/* ─── 顶部栏 ─── */
.topbar {
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.topbar-inner {
  max-width: 1400px;
  margin: 0 auto;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.brand {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.apikey-bar {
  max-width: 1400px;
  margin: 0 auto;
  padding: 0 16px 10px;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
.apikey-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
}
.apikey-input {
  flex: 1;
  min-width: 200px;
  max-width: 480px;
  height: 34px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  outline: none;
  transition: border-color 0.15s ease;
}
.apikey-input:focus { border-color: var(--accent); }

.badge {
  display: inline-flex;
  align-items: center;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
}
button.badge {
  border: none;
  cursor: pointer;
  font: inherit;
}
.badge-muted { background: var(--panel-alt); color: var(--text-muted); }
.badge-success { background: #E3EDE0; color: var(--success); }
.badge-error { background: #F3DCD8; color: var(--danger); }
.api-status-gear {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--secondary);
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.api-status-gear:hover { background: var(--secondary-hover); border-color: var(--accent); }
.api-status-gear svg { display: block; }

/* ─── 按钮 ─── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: none;
  border-radius: var(--radius-sm);
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
  min-height: 40px;
  line-height: 1.2;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary {
  background: var(--accent);
  color: var(--accent-text);
}
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn-secondary {
  background: var(--secondary);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-secondary:hover:not(:disabled) { background: var(--secondary-hover); }
.btn-block { width: 100%; }
.btn-sm { padding: 6px 12px; min-height: 32px; font-size: 13px; }
.btn-xs { padding: 4px 10px; min-height: 26px; font-size: 12px; }

/* ─── 主布局 ─── */
.layout {
  max-width: 1400px;
  margin: 0 auto;
  padding: 16px;
  display: grid;
  grid-template-columns: minmax(420px, 560px) 1fr;
  gap: 16px;
  align-items: stretch;
}
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  min-height: 600px;
}
.panel-left { padding: 12px; }
.panel-right { padding: 12px; min-width: 0; }

/* ─── Tab ─── */
.tabs {
  display: flex;
  gap: 4px;
  background: var(--panel-alt);
  border-radius: var(--radius-sm);
  padding: 4px;
  margin-bottom: 12px;
}
.tab {
  flex: 1;
  padding: 10px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-weight: 500;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
  min-height: 40px;
}
.tab:hover { background: rgba(255, 255, 255, 0.5); }
.tab-active {
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 1px 2px rgba(26, 25, 21, 0.06);
}

.tab-body { display: flex; flex-direction: column; gap: 10px; flex: 1; }
.tab-body-hidden { display: none; }

/* ─── Prompt ─── */
.prompt-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.field-label {
  font-size: 13px;
  font-weight: 700;
  color: var(--text);
}
.prompt-input {
  width: 100%;
  min-height: 140px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  color: var(--text);
  resize: vertical;
  font-size: 14px;
  line-height: 1.6;
  outline: none;
  transition: border-color 0.15s ease;
}
.prompt-input:focus { border-color: var(--accent); }

/* ─── 选项 ─── */
.options {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  background: var(--panel-alt);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
}
.options-wrap { gap: 16px; }
.opt-group {
  display: flex;
  align-items: center;
  gap: 6px;
}
.opt-label {
  font-size: 13px;
  color: var(--text);
}
.select {
  height: 36px;
  padding: 0 28px 0 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--panel) url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'><path fill='%236F6E63' d='M4 6l4 4 4-4z'/></svg>") no-repeat right 8px center;
  background-size: 14px;
  appearance: none;
  color: var(--text);
  cursor: pointer;
  outline: none;
}
.select:focus { border-color: var(--accent); }
.checkbox {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  cursor: pointer;
}
.checkbox input { width: 16px; height: 16px; accent-color: var(--accent); }

.opt-strength input[type="range"] {
  width: 110px;
  accent-color: var(--accent);
}
.strength-val {
  display: inline-block;
  min-width: 36px;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  font-size: 13px;
}

/* ─── 参考图 ─── */
.ref-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  background: var(--panel-alt);
  padding: 8px 12px;
  border-radius: var(--radius-sm);
}
.ref-count {
  font-size: 12px;
  color: var(--text-muted);
  margin-left: auto;
}
.ref-preview {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 6px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  min-height: 96px;
}
.ref-preview:empty::before {
  content: "暂无参考图";
  color: var(--text-muted);
  font-size: 12px;
  display: flex;
  align-items: center;
  padding: 0 8px;
}
.ref-item {
  position: relative;
  flex: 0 0 auto;
}
.ref-item img {
  width: 80px;
  height: 80px;
  object-fit: cover;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  display: block;
}
.ref-item-remove {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--danger);
  color: #fff;
  border: none;
  cursor: pointer;
  font-size: 12px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
.ref-item-remove:hover { background: var(--danger-hover); }

/* ─── 结果区 ─── */
.panel-header {
  padding: 8px 4px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 8px;
}
.panel-header h2 {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
}
.result-area {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  min-height: 400px;
  background: var(--bg);
  border-radius: var(--radius-sm);
  padding: 12px;
  overflow: hidden;
}
.result-image {
  max-width: 100%;
  max-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.result-image img {
  max-width: 100%;
  max-height: 70vh;
  border-radius: var(--radius-sm);
  object-fit: contain;
  box-shadow: 0 4px 16px rgba(26, 25, 21, 0.1);
}
.empty-hint {
  color: var(--text-muted);
  font-size: 14px;
  text-align: center;
}
.panel-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-top: 8px;
}
.result-status {
  margin: 0;
  color: var(--text-muted);
  font-size: 13px;
  flex: 1;
  word-break: break-all;
}

/* ─── 加载动画 ─── */
.loading-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}
.loading-text {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}
.loading-bar {
  width: 240px;
  max-width: 80vw;
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
}
.loading-bar-fill {
  width: 30%;
  height: 100%;
  background: var(--accent);
  border-radius: 3px;
  animation: loading-slide 1.2s ease-in-out infinite;
}
@keyframes loading-slide {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(400%); }
}

/* ─── 历史抽屉 ─── */
.drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: 360px;
  max-width: 90vw;
  background: var(--panel);
  border-left: 1px solid var(--border);
  box-shadow: -4px 0 24px rgba(26, 25, 21, 0.08);
  z-index: 50;
  display: flex;
  flex-direction: column;
}
.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.drawer-head h3 {
  margin: 0;
  font-size: 15px;
  font-weight: 700;
}
.drawer-body {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.history-item {
  display: flex;
  gap: 10px;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg);
  cursor: pointer;
  transition: border-color 0.15s ease;
}
.history-item:hover { border-color: var(--accent); }
.history-item img {
  width: 56px;
  height: 56px;
  object-fit: cover;
  border-radius: var(--radius-sm);
  flex-shrink: 0;
}
.history-item-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.history-item-prompt {
  font-size: 12px;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  word-break: break-all;
}
.history-item-meta {
  font-size: 11px;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.history-item-delete {
  background: transparent;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 16px;
  padding: 0 4px;
  line-height: 1;
}
.history-item-delete:hover { color: var(--danger); }
.history-empty {
  text-align: center;
  color: var(--text-muted);
  font-size: 13px;
  padding: 24px;
}

/* ─── Toast ─── */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 100;
  max-width: 90vw;
  box-shadow: 0 4px 16px rgba(26, 25, 21, 0.2);
}
.toast-error { background: var(--danger); }

/* ─── 工具类 ─── */
.hidden { display: none !important; }

/* ─── 移动端 ─── */
@media (max-width: 768px) {
  .layout {
    grid-template-columns: 1fr;
    padding: 12px;
    gap: 12px;
  }
  .panel { min-height: auto; }
  .panel-left { padding: 10px; }
  .panel-right { min-height: 360px; }
  .topbar-inner { padding: 10px 12px; }
  .brand { font-size: 16px; }
  .prompt-input { min-height: 120px; }
  .options { gap: 8px; padding: 8px 10px; }
  .opt-group { font-size: 12px; }
  .select { font-size: 13px; height: 32px; }
  .btn { min-height: 44px; }
  .btn-primary { font-size: 15px; }
  .result-area { min-height: 300px; }
  .result-image img { max-height: 50vh; }
  .drawer { width: 100vw; max-width: 100vw; }
  .ref-count { width: 100%; text-align: right; margin-top: 4px; }
}

@media (max-width: 480px) {
  .options { flex-direction: column; align-items: stretch; }
  .opt-group { width: 100%; justify-content: space-between; }
  .opt-strength input[type="range"] { flex: 1; width: auto; }
}

/* ─── 暗色模式（系统跟随，可选） ─── */
@media (prefers-color-scheme: dark) {
  /* 保持亮色风格，不做暗色适配 */
}
