:root {
  color-scheme: light;
  --bg: #f3f5f6;
  --surface: #ffffff;
  --surface-muted: #f7f8f8;
  --text: #17201e;
  --muted: #697672;
  --border: #d9dfdd;
  --primary: #0d6b57;
  --primary-hover: #095543;
  --accent: #d86f3d;
  --danger: #b33b32;
  --shadow: 0 10px 28px rgba(20, 38, 33, .08);
  font-family: Inter, "Segoe UI", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
}

button, input, select, textarea { font: inherit; }
button, select, input[type="range"] { cursor: pointer; }

.topbar {
  height: 68px;
  padding: 0 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand { display: flex; align-items: center; gap: 11px; }
.brand-mark {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 6px;
  background: var(--primary);
  color: white;
  font-size: 16px;
  font-weight: 700;
}
.brand h1 { margin: 0; font-size: 16px; line-height: 1.25; letter-spacing: 0; }
.brand p { margin: 2px 0 0; font-size: 11px; color: var(--muted); }
.top-actions { display: flex; align-items: center; gap: 14px; }
.status { display: flex; align-items: center; gap: 7px; color: var(--muted); font-size: 13px; }
.status > span { width: 7px; height: 7px; background: #22a06b; border-radius: 50%; }

.workspace {
  min-height: calc(100vh - 68px);
  display: grid;
  grid-template-columns: minmax(320px, 410px) 1fr;
}

.controls {
  padding: 24px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow: auto;
}

.control-section { padding: 0 0 22px; margin: 0 0 22px; border-bottom: 1px solid var(--border); }
.control-section h2, .section-heading label { margin: 0 0 13px; font-size: 13px; font-weight: 700; }
.section-heading { display: flex; justify-content: space-between; align-items: center; }
.section-heading h2 { margin: 0; }
.section-heading span { color: var(--muted); font-size: 11px; }

.visually-hidden {
  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;
}

.reference-dropzone {
  min-height: 82px;
  padding: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 11px;
  border: 1px dashed #aebbb7;
  border-radius: 6px;
  background: var(--surface-muted);
  color: var(--muted);
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.reference-dropzone:hover, .reference-dropzone.is-dragging {
  border-color: var(--primary);
  background: #f0f8f5;
}
.reference-dropzone svg { width: 22px; height: 22px; color: var(--primary); flex: 0 0 auto; }
.reference-dropzone strong, .reference-dropzone small { display: block; }
.reference-dropzone strong { margin-bottom: 3px; color: var(--text); font-size: 12px; }
.reference-dropzone small { font-size: 10px; font-weight: 400; }

.reference-list {
  margin-top: 12px;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
}
.reference-item { position: relative; min-width: 0; }
.reference-thumb {
  position: relative;
  aspect-ratio: 1;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: #e9edec;
}
.reference-thumb img { width: 100%; height: 100%; display: block; object-fit: cover; }
.reference-index {
  position: absolute;
  top: 4px;
  left: 4px;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  display: grid;
  place-items: center;
  border-radius: 4px;
  background: rgba(16, 28, 25, .82);
  color: white;
  font-size: 10px;
  font-weight: 700;
}
.reference-actions { margin-top: 5px; display: flex; justify-content: center; gap: 3px; }
.reference-action {
  width: 25px;
  height: 25px;
  padding: 0;
  display: grid;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  color: var(--muted);
}
.reference-action:hover:not(:disabled) { color: var(--text); border-color: #aebbb7; }
.reference-action.danger:hover { color: var(--danger); border-color: #e0aaa5; }
.reference-action:disabled { opacity: .35; cursor: not-allowed; }
.reference-action svg { width: 13px; height: 13px; }

.identity-toggle {
  margin-top: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  cursor: pointer;
}
.identity-toggle > span:first-child { min-width: 0; }
.identity-toggle strong, .identity-toggle small { display: block; }
.identity-toggle strong { color: var(--text); font-size: 12px; }
.identity-toggle small { margin-top: 3px; color: var(--muted); font-size: 10px; font-weight: 400; }
.identity-toggle input { position: absolute; opacity: 0; pointer-events: none; }
.identity-toggle .switch {
  position: relative;
  width: 36px;
  height: 20px;
  flex: 0 0 auto;
  border-radius: 10px;
  background: #c5cecb;
  transition: background .15s;
}
.identity-toggle .switch::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: white;
  box-shadow: 0 1px 3px rgba(15, 30, 26, .22);
  transition: transform .15s;
}
.identity-toggle input:checked + .switch { background: var(--primary); }
.identity-toggle input:checked + .switch::after { transform: translateX(16px); }
.identity-toggle input:focus-visible + .switch { outline: 3px solid rgba(13, 107, 87, .18); }
.identity-toggle.is-disabled { opacity: .52; cursor: not-allowed; }

textarea, input[type="text"], input[type="url"], input[type="password"], .secret-input, select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--surface);
  color: var(--text);
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
textarea:focus, input[type="text"]:focus, input[type="url"]:focus, input[type="password"]:focus, select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(13, 107, 87, .12);
}
textarea { resize: vertical; padding: 11px 12px; line-height: 1.55; }
#prompt { min-height: 148px; }
input[type="text"], input[type="url"], input[type="password"], select { height: 40px; padding: 0 10px; }

.secret-input { display: flex; align-items: center; overflow: hidden; }
.secret-input:focus-within { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(13, 107, 87, .12); }
.secret-input input { min-width: 0; flex: 1; border: 0; box-shadow: none !important; }
.secret-toggle { width: 39px; height: 38px; border: 0; background: transparent; color: var(--muted); display: grid; place-items: center; }
.secret-toggle:hover { color: var(--text); background: var(--surface-muted); }
.secret-toggle svg { width: 16px; height: 16px; }
.privacy-note { margin: 10px 0 0; display: flex; gap: 7px; color: var(--muted); font-size: 11px; line-height: 1.45; }
.privacy-note svg { width: 14px; height: 14px; flex: 0 0 auto; color: var(--primary); }

.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px 12px; }
.field { display: block; }
.field > span:first-child { display: block; margin: 0 0 7px; font-size: 12px; color: var(--muted); }
.control-section > .field + .field { margin-top: 14px; }

.range-field output { color: var(--primary); font-weight: 700; }
input[type="range"] { width: 100%; accent-color: var(--primary); }
.range-labels { display: flex; justify-content: space-between; color: var(--muted); font-size: 10px; }

.folder-state {
  min-height: 38px;
  margin-top: 12px;
  padding: 9px 10px;
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px dashed var(--border);
  border-radius: 5px;
  color: var(--muted);
  font-size: 12px;
}
.folder-state svg { width: 15px; height: 15px; flex: 0 0 auto; }
.save-section { margin-bottom: 18px; }

.button {
  min-height: 38px;
  padding: 0 13px;
  border: 1px solid transparent;
  border-radius: 5px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-weight: 650;
  font-size: 13px;
  transition: background .15s, border-color .15s, transform .15s;
}
.button:active { transform: translateY(1px); }
.button:disabled { cursor: not-allowed; opacity: .58; }
.button svg { width: 16px; height: 16px; }
.button-primary { background: var(--primary); color: white; }
.button-primary:hover:not(:disabled) { background: var(--primary-hover); }
.button-secondary { border-color: var(--border); background: var(--surface); color: var(--text); }
.button-secondary:hover:not(:disabled) { background: var(--surface-muted); border-color: #b9c4c0; }
.generate-button { width: 100%; min-height: 46px; font-size: 14px; }

.results { padding: 28px clamp(20px, 4vw, 54px) 48px; min-width: 0; }
.results-heading { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-bottom: 24px; }
.results-heading h2 { margin: 0; font-size: 18px; letter-spacing: 0; }
.results-heading p { margin: 5px 0 0; color: var(--muted); font-size: 12px; }

.empty-state, .loading-state {
  min-height: 480px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--muted);
}
.empty-icon {
  width: 54px;
  height: 54px;
  display: grid;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--surface);
  color: var(--primary);
  box-shadow: var(--shadow);
}
.empty-icon svg { width: 24px; height: 24px; }
.empty-state h3, .loading-state h3 { margin: 17px 0 6px; font-size: 15px; color: var(--text); }
.empty-state p, .loading-state p { max-width: 390px; margin: 0; font-size: 12px; line-height: 1.6; }

.progress-ring {
  width: 48px;
  height: 48px;
  padding: 4px;
  border: 2px solid #cfd8d5;
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .85s linear infinite;
}
.progress-ring span { display: block; width: 100%; height: 100%; border-radius: 50%; background: var(--surface); }
.elapsed { margin-top: 18px; font-size: 11px; color: var(--muted); }
@keyframes spin { to { transform: rotate(360deg); } }

.error-state {
  padding: 14px;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  border: 1px solid #edc7c3;
  border-radius: 6px;
  background: #fff7f6;
  color: var(--danger);
}
.error-state svg { width: 19px; height: 19px; flex: 0 0 auto; }
.error-state h3 { margin: 0 0 3px; font-size: 13px; }
.error-state p { margin: 0; font-size: 12px; line-height: 1.5; }

.gallery { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }
.image-item { margin: 0; min-width: 0; }
.image-frame {
  position: relative;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #e9edec;
  box-shadow: var(--shadow);
}
.image-frame img { width: 100%; height: auto; display: block; }
.image-actions {
  position: absolute;
  right: 10px;
  bottom: 10px;
  opacity: 0;
  transition: opacity .15s;
}
.image-frame:hover .image-actions, .image-actions:focus-within { opacity: 1; }
.icon-button {
  width: 36px;
  height: 36px;
  padding: 0;
  border: 0;
  border-radius: 5px;
  display: grid;
  place-items: center;
  background: rgba(18, 27, 25, .86);
  color: white;
}
.icon-button svg { width: 17px; height: 17px; }
.image-item figcaption { margin-top: 8px; display: flex; justify-content: space-between; gap: 10px; color: var(--muted); font-size: 11px; }

.toast {
  position: fixed;
  right: 20px;
  bottom: 20px;
  max-width: min(360px, calc(100vw - 40px));
  padding: 11px 14px;
  border-radius: 5px;
  background: #17201e;
  color: white;
  box-shadow: var(--shadow);
  font-size: 12px;
  z-index: 30;
}

@media (max-width: 900px) {
  .workspace { grid-template-columns: 360px 1fr; }
  .results { padding-inline: 22px; }
  .gallery { grid-template-columns: 1fr; }
}

@media (max-width: 720px) {
  .topbar { height: auto; min-height: 64px; padding: 10px 14px; }
  .status { display: none; }
  .top-actions .button span { display: none; }
  .top-actions .button { width: 40px; padding: 0; }
  .workspace { display: block; }
  .controls { padding: 20px 16px; border-right: 0; border-bottom: 1px solid var(--border); }
  .results { min-height: 520px; padding: 22px 16px 36px; }
  .empty-state, .loading-state { min-height: 360px; }
  .gallery { grid-template-columns: 1fr; gap: 22px; }
  .image-actions { opacity: 1; }
}

@media (max-width: 390px) {
  .field-grid { grid-template-columns: 1fr; }
  .results-heading { align-items: flex-start; }
  #downloadAll span { display: none; }
  #downloadAll { width: 40px; padding: 0; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
