:root {
  color-scheme: light;
  --canvas: #f5f5f7;
  --surface: #ffffff;
  --surface-raised: #fbfbfd;
  --text: #1d1d1f;
  --text-secondary: #6e6e73;
  --text-tertiary: #86868b;
  --separator: #d2d2d7;
  --separator-strong: #b8b8bd;
  --accent: #0071e3;
  --accent-hover: #0077ed;
  --accent-soft: #eaf3ff;
  --upload: #34c759;
  --upload-soft: #edf9f0;
  --creative: #f4a75b;
  --creative-hover: #ec963f;
  --creative-soft: #fff5e9;
  --creative-ink: #3b2200;
  --jade: var(--accent);
  --jade-deep: var(--accent-hover);
  --soft: var(--accent-soft);
  --danger: #c4473d;
  --preview: #111214;
  --shadow-soft: 0 1px 2px rgba(0, 0, 0, 0.04), 0 8px 28px rgba(0, 0, 0, 0.06);
  --shadow-control: 0 1px 2px rgba(0, 0, 0, 0.04);
}

html {
  background: var(--canvas);
}

body {
  background: var(--canvas);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", "PingFang SC", "Helvetica Neue", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

::selection {
  color: var(--text);
  background: rgba(0, 113, 227, 0.2);
}

button,
input,
textarea,
select {
  letter-spacing: 0;
}

button {
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible,
input:focus-visible,
textarea:focus-visible,
select:focus-visible,
.upload-zone:focus-within {
  outline: 3px solid rgba(0, 113, 227, 0.28);
  outline-offset: 2px;
}

.app-shell {
  width: 100%;
  max-width: 1480px;
  padding: 0 36px 52px;
}

.topbar {
  position: sticky;
  z-index: 20;
  top: 0;
  min-height: 64px;
  padding: 0;
  border-bottom: 1px solid rgba(0, 0, 0, 0.1);
  background: rgba(245, 245, 247, 0.82);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
}

.brand {
  gap: 10px;
  color: var(--text);
  font-size: 16px;
  font-weight: 650;
}

.brand-mark {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  color: var(--surface);
  background: var(--text);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.16), 0 1px 3px rgba(0, 0, 0, 0.12);
  font-size: 15px;
  font-weight: 650;
}

.connection {
  gap: 7px;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 500;
}

.connection i {
  width: 7px;
  height: 7px;
  background: #30a46c;
  box-shadow: 0 0 0 3px rgba(48, 164, 108, 0.12);
}

.workspace {
  grid-template-columns: minmax(360px, 440px) minmax(520px, 1fr);
  align-items: start;
  gap: 54px;
  padding-top: 42px;
}

.controls {
  padding: 8px 0 48px;
}

.section-heading h1 {
  color: var(--text);
  font-size: 34px;
  line-height: 1.15;
  font-weight: 680;
}

.section-heading p {
  margin: 9px 0 32px;
  color: var(--text-secondary);
  font-size: 14px;
  line-height: 1.5;
}

.field-label {
  margin: 20px 0 8px;
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
}

.field-label span {
  color: var(--text-tertiary);
  font-weight: 400;
}

input,
textarea,
select {
  border: 1px solid var(--separator);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-control);
  transition: border-color 160ms ease, box-shadow 160ms ease, background-color 160ms ease;
}

input,
select {
  height: 46px;
  padding: 0 13px;
}

.select-control {
  position: relative;
}

.select-control select {
  -webkit-appearance: none;
  appearance: none;
  padding-right: 44px;
}

.select-chevron {
  position: absolute;
  top: 0;
  right: 8px;
  bottom: 0;
  display: grid;
  place-items: center;
  width: 28px;
  color: var(--text-secondary);
  pointer-events: none;
}

.select-chevron::before {
  width: 7px;
  height: 7px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  content: "";
  transform: translateY(-2px) rotate(45deg);
}

.select-control:focus-within .select-chevron {
  color: var(--accent);
}

textarea {
  min-height: 142px;
  padding: 13px 14px;
  line-height: 1.55;
}

input::placeholder,
textarea::placeholder {
  color: var(--text-tertiary);
}

input:hover,
textarea:hover,
select:hover {
  border-color: var(--separator-strong);
}

input:focus,
textarea:focus,
select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px rgba(0, 113, 227, 0.14);
}

.key-field input {
  padding-right: 50px;
}

.icon-button {
  top: 5px;
  right: 5px;
  width: 36px;
  height: 36px;
  border-radius: 8px;
  color: var(--text-secondary);
  transition: color 150ms ease, background-color 150ms ease;
}

.icon-button:hover {
  color: var(--text);
  background: rgba(0, 0, 0, 0.05);
}

.field-note {
  margin: 8px 0 0;
  color: var(--text-tertiary);
  font-size: 11px;
  line-height: 1.45;
}

.remember-key {
  gap: 8px;
  margin-top: 11px;
  color: var(--text-secondary);
  font-size: 12px;
}

.remember-key input {
  width: 16px;
  height: 16px;
  border-radius: 4px;
  box-shadow: none;
  accent-color: var(--upload);
}

.mode-tabs {
  gap: 3px;
  margin-top: 28px;
  padding: 3px;
  border: 1px solid rgba(0, 0, 0, 0.04);
  border-radius: 8px;
  background: #e8e8ed;
}

.mode-tab {
  height: 38px;
  border-radius: 6px;
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 560;
  transition: color 160ms ease, background-color 160ms ease, box-shadow 160ms ease;
}

.mode-tab:hover {
  color: var(--text);
}

.mode-tab.active {
  color: var(--text);
  background: var(--surface);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.14);
}

.field-row {
  grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.25fr);
  gap: 14px;
}

.upload-zone {
  min-height: 82px;
  gap: 14px;
  padding: 13px 14px;
  border: 1px solid var(--separator);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text-secondary);
  box-shadow: var(--shadow-control);
  transition: border-color 160ms ease, background-color 160ms ease, box-shadow 160ms ease;
}

.upload-zone:hover,
.upload-zone.dragover {
  border-color: var(--upload);
  background: var(--upload-soft);
  box-shadow: 0 0 0 4px rgba(52, 199, 89, 0.1);
}

.upload-zone:focus-within {
  outline-color: rgba(52, 199, 89, 0.28);
}

.upload-icon {
  flex-basis: 36px;
  height: 36px;
  border-radius: 8px;
  color: var(--upload);
  background: var(--upload-soft);
  font-size: 20px;
  font-weight: 500;
}

.upload-zone strong {
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
}

.upload-zone small {
  margin-top: 4px;
  color: var(--text-tertiary);
  font-size: 11px;
}

.upload-thumb {
  width: 52px;
  height: 52px;
  border: 1px solid var(--separator);
  border-radius: 6px;
  background: var(--surface-raised);
}

.generation-actions {
  gap: 10px;
  margin-top: 30px;
}

.primary-button,
.cancel-button {
  height: 50px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 650;
}

.primary-button {
  margin-top: 30px;
  color: var(--creative-ink);
  background: var(--creative);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08), 0 6px 18px rgba(244, 167, 91, 0.2);
  transition: background-color 150ms ease, transform 150ms ease, box-shadow 150ms ease;
}

.primary-button:hover {
  background: var(--creative-hover);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1), 0 8px 20px rgba(255, 149, 0, 0.26);
}

.primary-button:focus-visible {
  outline-color: rgba(244, 167, 91, 0.38);
}

.primary-button:active {
  transform: scale(0.99);
}

.primary-button:disabled {
  color: rgba(255, 255, 255, 0.8);
  background: #8e8e93;
  box-shadow: none;
}

.cancel-button {
  border-color: var(--danger);
  color: #ffffff;
  background: var(--danger);
  transition: opacity 150ms ease, background-color 150ms ease;
}

.cancel-button:hover {
  background: #a93a32;
}

.cancel-button:disabled {
  color: var(--text-tertiary);
  border-color: var(--separator);
  background: #e8e8ed;
}

.preview-panel {
  position: sticky;
  top: 92px;
  min-width: 0;
}

.preview-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 54px;
  padding: 8px 10px 8px 14px;
  border: 1px solid var(--separator);
  border-bottom: 0;
  border-radius: 8px 8px 0 0;
  background: var(--surface-raised);
}

.preview-heading {
  display: flex;
  align-items: center;
  gap: 11px;
  min-width: 0;
}

.preview-heading h2 {
  margin: 0;
  color: var(--text);
  font-size: 14px;
  font-weight: 650;
}

.preview-heading span {
  display: flex;
  align-items: center;
  gap: 7px;
  min-width: 0;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
}

.preview-heading span::before {
  flex: 0 0 auto;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--creative);
  content: "";
}

.result-frame {
  width: 100%;
  height: clamp(520px, calc(100vh - 190px), 740px);
  min-height: 520px;
  border: 1px solid rgba(0, 0, 0, 0.12);
  border-radius: 0 0 8px 8px;
  background: var(--preview);
  box-shadow: var(--shadow-soft);
}

.result-frame.empty {
  border-style: solid;
  background: var(--surface);
}

.result-frame:not(.empty) {
  background: var(--preview);
}

.result-frame img {
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  object-fit: contain;
}

.empty-state {
  gap: 9px;
  padding: 28px;
  color: var(--text-tertiary);
  text-align: center;
}

.empty-state strong {
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
}

.empty-state span {
  color: var(--text-secondary);
  font-size: 12px;
}

.sparkle {
  width: 52px;
  height: 52px;
  margin-bottom: 8px;
  border: 1px solid rgba(255, 149, 0, 0.18);
  border-radius: 8px;
  color: var(--creative);
  background: var(--creative-soft);
  font-size: 22px;
}

.loading-state {
  gap: 11px;
  color: #f5f5f7;
  background: rgba(17, 18, 20, 0.82);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
}

.loading-state strong {
  font-size: 14px;
  font-weight: 600;
}

.loading-state small {
  color: #b8b8bd;
  font-size: 12px;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid rgba(244, 167, 91, 0.28);
  border-top-color: var(--creative);
}

.result-actions {
  flex-wrap: wrap;
  gap: 8px;
  padding-top: 12px;
}

.secondary-button,
.text-button {
  height: 38px;
  padding: 0 14px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 600;
  transition: color 150ms ease, border-color 150ms ease, background-color 150ms ease;
}

.preview-toolbar .result-actions {
  flex-wrap: nowrap;
  gap: 6px;
  margin-left: auto;
  padding-top: 0;
}

.preview-toolbar .result-actions > button {
  min-width: 76px;
  height: 34px;
  padding: 0 11px;
}

.secondary-button {
  border: 1px solid var(--separator);
  color: var(--text);
  background: var(--surface);
  box-shadow: var(--shadow-control);
}

.secondary-button:hover {
  border-color: var(--separator-strong);
  background: var(--surface-raised);
}

.secondary-button:disabled {
  color: var(--text-tertiary);
  border-color: var(--separator);
  background: transparent;
  box-shadow: none;
  opacity: 0.65;
}

.text-button {
  color: var(--text-secondary);
}

.text-button:hover {
  color: var(--accent);
  background: var(--accent-soft);
}

.secondary-button:active,
.text-button:active {
  transform: scale(0.98);
}

.compare-hold-button {
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

.compare-hold-button.active {
  border-color: var(--creative);
  color: var(--creative-ink);
  background: var(--creative-soft);
  box-shadow: inset 0 0 0 1px rgba(244, 167, 91, 0.12);
}

.final-prompt-panel {
  margin-top: 18px;
  padding-top: 18px;
  border-top: 1px solid var(--separator);
}

.final-prompt-panel label {
  margin-bottom: 8px;
  color: var(--text);
  font-size: 12px;
  font-weight: 600;
}

.final-prompt-panel textarea {
  min-height: 110px;
  color: var(--text-secondary);
  background: var(--surface);
  font-size: 12px;
}

.edit-source-choice {
  gap: 9px 12px;
  margin-top: 13px;
  color: var(--text-secondary);
  font-size: 12px;
}

.edit-source-choice > span {
  font-weight: 600;
}

.edit-source-choice > div {
  gap: 3px;
  padding: 3px;
  border: 1px solid rgba(0, 0, 0, 0.04);
  border-radius: 8px;
  background: #e8e8ed;
}

.edit-source-choice button {
  min-width: 92px;
  height: 32px;
  border-radius: 6px;
  color: var(--text-secondary);
  font-size: 11px;
  font-weight: 560;
}

.edit-source-choice button.active {
  color: var(--text);
  background: var(--surface);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.14);
}

.secondary-settings {
  margin-top: 24px;
  padding: 18px 0 4px;
  border-top: 1px solid var(--separator);
}

.settings-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.settings-heading h2 {
  margin: 0;
  color: var(--text-secondary);
  font-size: 12px;
  line-height: 1.4;
  font-weight: 650;
}

.settings-grid,
.upload-grid {
  display: grid;
  gap: 12px;
}

.settings-grid {
  grid-template-columns: 1fr;
}

.upload-grid {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.setting-field {
  min-width: 0;
}

.setting-field > .field-label {
  margin-top: 0;
}

.prompt-enhance {
  display: flex;
  align-items: center;
  gap: 8px;
  width: max-content;
  margin-top: 10px;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 560;
  cursor: pointer;
  transition: color 150ms ease;
}

.prompt-enhance:hover {
  color: var(--text);
}

.prompt-enhance input {
  flex: 0 0 auto;
  width: 16px;
  height: 16px;
  padding: 0;
  border-radius: 4px;
  box-shadow: none;
  accent-color: var(--upload);
}

.upload-grid {
  margin-top: 18px;
}

.upload-grid .upload-zone {
  min-height: 92px;
}

.upload-zone > span:not(.upload-icon) {
  min-width: 0;
}

.upload-zone strong,
.upload-zone small {
  overflow-wrap: anywhere;
}

@media (hover: none) {
  .primary-button:hover {
    background: var(--creative);
  }
}

@media (max-width: 1040px) {
  .workspace {
    grid-template-columns: minmax(330px, 400px) minmax(440px, 1fr);
    gap: 36px;
  }
}

@media (max-width: 880px) {
  .app-shell {
    padding: 0 24px 42px;
  }

  .workspace {
    grid-template-columns: 1fr;
    gap: 34px;
    padding-top: 32px;
  }

  .controls {
    width: 100%;
    max-width: 640px;
    margin: 0 auto;
    padding-bottom: 0;
  }

  .preview-panel {
    position: static;
    order: initial;
    width: 100%;
    max-width: 760px;
    margin: 0 auto;
  }

  .result-frame {
    height: min(72vh, 680px);
    min-height: 440px;
  }
}

@media (max-width: 560px) {
  .app-shell {
    padding: 0 16px 32px;
  }

  .topbar {
    min-height: 58px;
  }

  .brand {
    font-size: 15px;
  }

  .brand-mark {
    width: 30px;
    height: 30px;
  }

  .connection {
    font-size: 11px;
  }

  .workspace {
    gap: 30px;
    padding-top: 26px;
  }

  .section-heading h1 {
    font-size: 28px;
  }

  .section-heading p {
    margin-bottom: 26px;
  }

  .field-row {
    grid-template-columns: 1fr;
    gap: 0;
  }

  .settings-grid,
  .upload-grid {
    grid-template-columns: 1fr;
  }

  .preview-toolbar {
    align-items: stretch;
    flex-direction: column;
    gap: 8px;
    padding: 10px;
  }

  .preview-toolbar .result-actions {
    width: 100%;
  }

  .upload-zone {
    min-height: 78px;
  }

  .result-frame {
    width: 100%;
    height: auto;
    min-height: 340px;
    aspect-ratio: 1 / 1;
  }

  .result-actions {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
  }

  .result-actions > button {
    width: 100%;
    padding: 0 5px;
    font-size: 11px;
  }

  .preview-toolbar .result-actions > button {
    min-width: 0;
  }

  .edit-source-choice {
    align-items: stretch;
  }

  .edit-source-choice > div {
    width: 100%;
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --canvas: #161617;
    --surface: #252527;
    --surface-raised: #2c2c2e;
    --text: #f5f5f7;
    --text-secondary: #a1a1a6;
    --text-tertiary: #86868b;
    --separator: #424245;
    --separator-strong: #5a5a5f;
    --accent: #2997ff;
    --accent-hover: #40a9ff;
    --accent-soft: #102d49;
    --upload: #30d158;
    --upload-soft: #15351f;
    --creative: #f6b36f;
    --creative-hover: #ffc184;
    --creative-soft: #3a2b1d;
    --creative-ink: #321b00;
    --preview: #09090a;
    --shadow-soft: 0 1px 2px rgba(0, 0, 0, 0.32), 0 10px 32px rgba(0, 0, 0, 0.24);
    --shadow-control: 0 1px 2px rgba(0, 0, 0, 0.2);
  }

  .topbar {
    border-bottom-color: rgba(255, 255, 255, 0.12);
    background: rgba(22, 22, 23, 0.82);
  }

  .icon-button:hover {
    background: rgba(255, 255, 255, 0.08);
  }

  .mode-tabs,
  .edit-source-choice > div,
  .cancel-button:disabled {
    border-color: rgba(255, 255, 255, 0.04);
    background: #2c2c2e;
  }

  .result-frame,
  .upload-zone,
  input,
  textarea,
  select {
    border-color: var(--separator);
  }

  .result-frame.empty {
    background: #202022;
  }

  .preview-heading span::before {
    background: var(--creative);
  }
}

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