:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --ink: #1f2933;
  --muted: #5f6875;
  --line: #dfe3e8;
  --brand: #2f6df6;
  --brand-ink: #1748be;
  --danger: #b42318;
  --ok: #067647;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.06), 0 4px 16px rgba(16, 24, 40, 0.06);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; color-scheme: light; }
body {
  min-width: 280px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB",
    "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}
body.modal-open { overflow: hidden; }

.app-toast {
  position: fixed;
  right: 16px;
  bottom: max(16px, env(safe-area-inset-bottom));
  left: 16px;
  z-index: 70;
  width: fit-content;
  max-width: min(520px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 10px 14px;
  border-radius: 10px;
  background: rgba(31, 41, 51, 0.96);
  box-shadow: 0 8px 24px rgba(16, 24, 40, 0.24);
  color: #fff;
  font-size: 13px;
  line-height: 1.45;
  text-align: center;
}

.sr-only {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

.app-header {
  text-align: center;
  padding: 22px 16px 8px;
}
.app-header h1 {
  font-size: 19px;
  margin: 0;
  font-weight: 700;
}
.app-header .sub {
  margin: 6px 0 0;
  font-size: 13px;
  color: var(--muted);
}
.account-status {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  margin-top: 9px;
  padding: 5px 7px 5px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--muted);
  font-size: 12px;
}
.account-status span {
  max-width: min(55vw, 300px);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.account-logout,
.ai-cancel {
  min-height: 44px;
  padding: 8px 12px;
  border: 0;
  border-radius: 999px;
  background: #eef3ff;
  color: var(--brand-ink);
  font: inherit;
  cursor: pointer;
}

.ai-progress-panel {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 7px 12px;
  width: min(612px, calc(100% - 28px));
  margin: 4px auto 0;
  padding: 10px 12px;
  border: 1px solid #c7d7fe;
  border-radius: 12px;
  background: #f4f7ff;
  color: var(--brand-ink);
  font-size: 12px;
  line-height: 1.45;
}
.ai-progress-panel > div { min-width: 0; }
.ai-progress-panel strong { display: block; font-size: 12.5px; }
.ai-progress-panel span { display: block; color: var(--muted); }
.ai-progress-panel progress {
  grid-column: 1 / -1;
  width: 100%;
  height: 8px;
  accent-color: var(--brand);
}
.ai-progress-panel .ai-cancel { grid-column: 2; grid-row: 1; align-self: center; white-space: nowrap; }
.ai-progress-panel.done { border-color: #a6e0c5; background: #f0fdf6; }
.ai-progress-panel.warning { border-color: #f2c6bf; background: #fff6f4; }

#app {
  max-width: 640px;
  margin: 0 auto;
  padding: 12px 14px calc(60px + env(safe-area-inset-bottom));
}

.screen { display: none; }
.screen.active { display: block; animation: fade 0.25s ease; }
@keyframes fade {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 16px;
}
.card.center { text-align: center; }
.card h2 { font-size: 16px; margin: 0 0 10px; }
.hint { font-size: 12.5px; color: var(--muted); line-height: 1.6; margin: 0 0 12px; }

.field-label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.field-label-spaced { margin-top: 14px; }

input[type="text"], input[type="email"], input[type="url"], textarea {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 16px;
  font-family: inherit;
  background: #fff;
  color: var(--ink);
}
input[type="file"] { width: 100%; font-size: 13px; margin-bottom: 6px; }
textarea { resize: vertical; }

.ai-consent {
  display: flex;
  gap: 10px;
  margin-top: 14px;
  padding: 12px;
  border: 1px solid #c7d7fe;
  border-radius: 11px;
  background: #f7f9ff;
  cursor: pointer;
}
.ai-consent input { flex: 0 0 auto; width: 18px; height: 18px; margin: 1px 0 0; accent-color: var(--brand); }
.ai-consent span { min-width: 0; }
.ai-consent strong { display: block; color: var(--ink); font-size: 13px; }
.ai-consent small { display: block; margin-top: 4px; color: var(--muted); font-size: 11.5px; line-height: 1.55; }

:where(button, a, input, textarea, summary):focus-visible {
  outline: 3px solid rgba(47, 109, 246, 0.55);
  outline-offset: 3px;
}
input:focus, textarea:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 1px var(--brand);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 44px;
  margin-top: 12px;
  padding: 10px 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
  font-family: inherit;
  font-size: 15px;
  font-weight: 600;
  line-height: 1.35;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
  transition: transform 0.05s ease, background 0.15s ease, border-color 0.15s ease;
}
.btn:hover:not(:disabled) { background: #f8fafc; border-color: #c8ced6; }
.btn:active:not(:disabled) { transform: scale(0.99); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--brand-ink); border-color: var(--brand-ink); }
.btn:disabled { opacity: 0.58; cursor: not-allowed; }
.btn.small { width: auto; min-height: 44px; margin: 6px 6px 0 0; padding: 8px 14px; font-size: 13px; }
.btn.text-button { border-color: transparent; color: var(--brand-ink); background: transparent; }
.demo-button { margin-top: 10px; }

.warn, .success { font-size: 12.5px; line-height: 1.5; margin: 10px 0 0; }
.warn { color: var(--danger); }
.success { color: var(--ok); }
.dev-code {
  margin-top: 10px;
  padding: 10px 12px;
  background: #fff7ed;
  border: 1px dashed #fdba74;
  border-radius: 10px;
  font-size: 13px;
  color: #9a3412;
}
.dev-code strong { font-size: 16px; letter-spacing: 2px; }

.faq { margin-top: 14px; font-size: 13px; color: var(--muted); }
.faq summary { display: flex; min-height: 44px; align-items: center; cursor: pointer; color: var(--brand-ink); }
.faq p { line-height: 1.6; }

/* 加载 */
.spinner {
  width: 34px;
  height: 34px;
  border: 3px solid var(--line);
  border-top-color: var(--brand);
  border-radius: 50%;
  margin: 6px auto 12px;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-status p:first-child { margin: 0 0 5px; }
.loading-count { margin-bottom: 8px; }
#analysis-progress { width: 100%; height: 12px; accent-color: var(--brand); }

.grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-top: 14px;
}
.grid > img,
.loading-thumb {
  display: block;
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  border-radius: 8px;
  background: #eef1f5;
}

/* 投票组 */
#vote-groups { display: flex; flex-direction: column; gap: 16px; }
.vote-group { border: 1px solid var(--line); border-radius: 12px; padding: 10px; }
.vote-group .pair { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.vote-option {
  display: block;
  width: 100%;
  min-width: 0;
  padding: 0;
  border: 3px solid transparent;
  border-radius: 11px;
  background: #eef1f5;
  color: inherit;
  cursor: pointer;
  overflow: hidden;
}
.vote-option img {
  display: block;
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: contain;
  background: #eef1f5;
}
.vote-option.selected {
  border-color: var(--brand);
  box-shadow: 0 0 0 2px rgba(47, 109, 246, 0.24);
}
.progress { margin: 16px 0 4px; font-size: 13px; color: var(--muted); text-align: center; }

/* 偏好条 */
.bar-row { display: flex; align-items: center; gap: 10px; margin: 10px 0; font-size: 13px; }
.bar-row .name { width: 72px; flex: 0 0 72px; color: var(--muted); }
.bar-track { flex: 1; height: 10px; background: #eef1f5; border-radius: 6px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--brand); border-radius: 6px; }

/* 快捷要求 */
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; }
.chip {
  min-height: 44px;
  padding: 7px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--ink);
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  line-height: 1.2;
  cursor: pointer;
}
.chip:hover { border-color: #aeb7c2; }
.chip.active { background: var(--brand); border-color: var(--brand); color: #fff; }
.require-status { margin-top: 10px; margin-bottom: 0; }
.model-disclaimer {
  margin: -3px 0 13px;
  padding: 8px 10px;
  border-radius: 8px;
  background: #f4f7ff;
  color: var(--muted);
  font-size: 11.5px;
  line-height: 1.5;
}

/* 结果网格 */
.grid.results { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.result-card { position: relative; min-width: 0; }
.result-preview {
  display: block;
  width: 100%;
  padding: 0;
  border: 0;
  border-radius: 10px;
  background: transparent;
  cursor: zoom-in;
}
.result-preview img {
  display: block;
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  border-radius: 10px;
  background: #eef1f5;
}
.result-check {
  position: absolute;
  top: 6px;
  left: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 2px solid var(--brand);
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.94);
  color: var(--brand);
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
}
.result-card.checked .result-check { background: var(--brand); color: #fff; }
.result-card.checked .result-preview img { box-shadow: inset 0 0 0 3px var(--brand), 0 0 0 3px var(--brand); }
.result-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 6px;
}
.result-meta span {
  max-width: 100%;
  padding: 2px 5px;
  overflow: hidden;
  border-radius: 5px;
  background: #eef1f5;
  color: var(--muted);
  font-size: 10px;
  line-height: 1.35;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.result-meta .quality-warning { background: #fff0ed; color: var(--danger); }

.result-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  margin-top: 16px;
  gap: 10px;
}
.result-actions .btn { width: auto; flex: 1 1 120px; margin-top: 0; }
#sel-count { flex: 1 1 auto; font-size: 13px; color: var(--muted); }

/* 最终结果 */
.final-card {
  min-width: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
}
.final-card img {
  display: block;
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  background: #eef1f5;
}
.photo-name {
  margin: 7px 8px 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.35;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.final-card .btn { width: calc(100% - 16px); min-height: 44px; margin: 7px 8px 8px; padding: 8px; }

/* 灯箱 */
.lightbox {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
  background: rgba(0, 0, 0, 0.88);
}
.lightbox[hidden] { display: none !important; }
.lightbox-inner {
  display: flex;
  width: min(92vw, 900px);
  max-height: 90vh;
  flex-direction: column;
  align-items: center;
  overflow: auto;
  text-align: center;
}
.lightbox-inner img {
  display: block;
  max-width: 100%;
  max-height: 70vh;
  border-radius: 10px;
  object-fit: contain;
  background: #222;
}
.lightbox-error {
  color: #fff;
  background: rgba(180, 35, 24, 0.94);
  padding: 12px 16px;
  border-radius: 10px;
  font-size: 14px;
  margin: 0 0 12px;
}
.lightbox-actions { display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 8px; }
.lightbox-actions .btn { width: auto; margin: 8px 0 0; }

@media (max-width: 420px) {
  .app-header { padding-top: 17px; }
  #app { padding-right: 10px; padding-left: 10px; }
  .card { padding: 16px 13px; }
  .grid.results, .grid.final-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .result-actions .btn { flex-basis: 100%; }
  .lightbox-inner { width: 100%; }
  .ai-progress-panel { grid-template-columns: 1fr; }
  .ai-progress-panel .ai-cancel { grid-column: 1; grid-row: auto; justify-self: start; }
}

@media (max-width: 330px) {
  .vote-group .pair { grid-template-columns: 1fr; }
  .grid.results, .grid.final-grid { grid-template-columns: 1fr 1fr; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
