:root {
  color-scheme: light;
  --bg: #f3f6fb;
  --panel: #ffffff;
  --panel-soft: #f8fbff;
  --text: #111827;
  --muted: #687385;
  --line: #dce5f1;
  --line-strong: #c8d5e6;
  --accent: #2563eb;
  --accent-dark: #1d4ed8;
  --accent-soft: #eaf2ff;
  --danger: #b42318;
  --shadow: 0 2px 12px rgba(15, 23, 42, 0.06);
  --shadow-strong: 0 18px 42px rgba(15, 23, 42, 0.09);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: "Microsoft YaHei", Arial, sans-serif;
}

button,
textarea,
input {
  font: inherit;
}

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

.app {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 430px minmax(0, 1fr);
  gap: 22px;
  padding: 22px;
}

.preview-panel,
.control-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.preview-panel {
  order: 2;
  min-width: 0;
  display: grid;
  grid-template-rows: auto minmax(360px, 1fr) auto;
  padding: 18px;
}

.control-panel {
  order: 1;
  align-self: start;
  padding: 18px;
}

.preview-header,
.panel-header {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.preview-header {
  flex-direction: row;
  justify-content: space-between;
  gap: 14px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line);
}

.preview-header p,
.panel-header p {
  margin: 0;
  color: var(--muted);
  line-height: 1.55;
}

.eyebrow {
  margin: 0;
  color: var(--accent);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  color: var(--text);
  letter-spacing: 0;
  line-height: 1.18;
}

h1 {
  font-size: 30px;
}

.secret-provider-button {
  appearance: none;
  border: 0;
  padding: 0;
  margin: 0;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  font: inherit;
  font-weight: inherit;
  line-height: inherit;
  letter-spacing: 0;
}

.secret-provider-button.is-active {
  color: var(--accent);
}

h2 {
  font-size: 24px;
}

.preview-tools {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.preview-tools span {
  min-height: 34px;
  display: inline-flex;
  align-items: center;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--panel-soft);
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.preview-tools button,
.secondary-button {
  min-height: 36px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  font-weight: 800;
}

.preview-tools button {
  padding: 0 14px;
}

#savedButton span {
  color: var(--accent-dark);
  font-weight: 900;
}

.canvas-shell {
  min-height: 420px;
  display: grid;
  align-content: center;
  padding: 16px 0;
}

.empty-state,
.loading-state,
.error-state {
  min-height: 420px;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background:
    linear-gradient(135deg, rgba(37, 99, 235, 0.08), transparent 40%),
    var(--panel-soft);
  color: var(--muted);
  text-align: center;
  padding: 20px;
}

.empty-state p,
.loading-state p {
  margin: 0;
}

.empty-visual {
  width: min(320px, 58vw);
  aspect-ratio: 4 / 3;
  border: 1px solid var(--line);
  border-radius: 8px;
  background:
    linear-gradient(135deg, #dbeafe, transparent 56%),
    linear-gradient(315deg, #fef3c7, transparent 54%),
    #ffffff;
  box-shadow: inset 0 0 0 10px rgba(255, 255, 255, 0.7);
}

.spinner {
  width: 42px;
  height: 42px;
  border-radius: 999px;
  border: 4px solid rgba(37, 99, 235, 0.14);
  border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}

.error-state {
  min-height: 180px;
  color: var(--danger);
  border-color: rgba(180, 35, 24, 0.24);
  background: #fff7f7;
}

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

.result-card {
  position: relative;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-soft);
  aspect-ratio: 1 / 1;
}

.result-card img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  background: #ffffff;
}

.result-actions {
  position: absolute;
  right: 12px;
  bottom: 12px;
  left: 12px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.result-action-button {
  min-height: 38px;
  border: 1px solid rgba(255, 255, 255, 0.38);
  border-radius: 8px;
  background: rgba(15, 23, 42, 0.78);
  color: #ffffff;
  cursor: pointer;
  font-weight: 900;
  backdrop-filter: blur(6px);
}

.preview-note {
  margin: 0;
  padding-top: 12px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
}

.panel-header {
  padding-bottom: 14px;
  border-bottom: 1px solid var(--line);
}

.controls {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  padding-top: 14px;
}

.upload-tile {
  position: relative;
  min-height: 148px;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 8px;
  overflow: hidden;
  padding: 14px 10px;
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  background: var(--panel-soft);
  color: var(--muted);
  cursor: pointer;
  text-align: center;
  transition:
    border-color 0.2s ease,
    background 0.2s ease,
    box-shadow 0.2s ease;
}

.upload-tile:hover,
.upload-tile.is-dragging {
  border-color: var(--accent);
  background: var(--accent-soft);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.08);
}

.upload-tile input {
  display: none;
}

.upload-kicker {
  min-width: 42px;
  min-height: 26px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 12px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent-dark);
  font-size: 12px;
  font-weight: 900;
}

.upload-tile strong {
  color: var(--text);
  font-size: 16px;
  line-height: 1.25;
}

.upload-tile small {
  max-width: 260px;
  font-size: 12px;
  line-height: 1.5;
}

.upload-tile.has-image {
  min-height: 150px;
  padding: 0;
  border-style: solid;
  background: #ffffff;
}

.upload-tile.has-image img {
  width: 100%;
  height: 100%;
  min-height: 150px;
  object-fit: cover;
  display: block;
}

.upload-caption {
  position: absolute;
  left: 10px;
  right: 10px;
  bottom: 10px;
  overflow: hidden;
  padding: 8px 10px;
  border-radius: 8px;
  background: rgba(15, 23, 42, 0.76);
  color: #ffffff;
  font-size: 12px;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.prompt-field {
  display: grid;
  grid-column: 1 / -1;
  gap: 8px;
}

.prompt-field span {
  color: var(--text);
  font-weight: 900;
}

textarea {
  width: 100%;
  min-height: 154px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px 13px;
  background: var(--panel-soft);
  color: var(--text);
  outline: none;
  line-height: 1.65;
}

textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
}

.actions {
  display: grid;
  grid-column: 1 / -1;
  grid-template-columns: 1fr auto 1.35fr;
  gap: 10px;
}

.secondary-button,
.ratio-button,
.primary-button {
  min-height: 44px;
  padding: 0 14px;
}

.ratio-button {
  min-width: 112px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: var(--panel-soft);
  color: var(--text);
  cursor: pointer;
  font-weight: 900;
  white-space: nowrap;
}

.ratio-button span {
  color: var(--accent-dark);
}

.primary-button {
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--accent);
  color: #ffffff;
  cursor: pointer;
  font-weight: 900;
  box-shadow: 0 8px 18px rgba(37, 99, 235, 0.18);
}

.primary-button:hover {
  background: var(--accent-dark);
}

.secondary-button:hover,
.ratio-button:hover,
.preview-tools button:hover {
  border-color: var(--accent);
  color: var(--accent-dark);
}

.primary-button:disabled {
  cursor: not-allowed;
  opacity: 0.65;
}

.text-result {
  white-space: pre-wrap;
  margin: 14px 0 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  color: var(--muted);
  background: var(--panel-soft);
}

.hidden {
  display: none;
}

body.modal-open {
  overflow: hidden;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(15, 23, 42, 0.46);
}

.modal-backdrop.hidden {
  display: none;
}

.modal-panel {
  width: min(880px, 100%);
  max-height: min(760px, 92vh);
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow-strong);
  padding: 18px;
}

.small-modal {
  width: min(460px, 100%);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}

.icon-button {
  width: 38px;
  height: 38px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: var(--panel-soft);
  color: var(--text);
  cursor: pointer;
  font-size: 24px;
  line-height: 1;
}

.modal-note {
  margin: 12px 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
}

.ratio-options {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 8px;
  margin-top: 14px;
}

.ratio-option {
  min-height: 64px;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 2px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-soft);
  color: var(--muted);
  cursor: pointer;
  text-align: center;
  -webkit-tap-highlight-color: transparent;
}

.ratio-option strong {
  color: var(--text);
  font-size: 15px;
}

.ratio-option small {
  font-size: 11px;
}

.ratio-option.is-selected {
  border-color: var(--accent);
  background: var(--accent-soft);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.08);
}

.ratio-option.is-selected strong {
  color: var(--accent-dark);
}

.saved-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 12px;
}

.saved-empty {
  grid-column: 1 / -1;
  min-height: 160px;
  display: grid;
  place-items: center;
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  color: var(--muted);
  background: var(--panel-soft);
}

.saved-card {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-soft);
}

.saved-card img {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  display: block;
  background: #ffffff;
}

.saved-meta {
  padding: 8px 10px 0;
  color: var(--muted);
  font-size: 12px;
}

.saved-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  padding: 10px;
}

.saved-actions button {
  min-height: 34px;
  border: 1px solid var(--line-strong);
  border-radius: 8px;
  background: #ffffff;
  color: var(--text);
  cursor: pointer;
  font-weight: 800;
}

.saved-actions button:first-child {
  border-color: var(--accent);
  background: var(--accent);
  color: #ffffff;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (max-width: 980px) {
  .app {
    grid-template-columns: 1fr;
    gap: 14px;
    padding: 14px;
  }

  .control-panel {
    order: -1;
  }

  .preview-panel {
    order: 2;
  }

  .preview-header {
    align-items: flex-start;
  }

  h1 {
    font-size: 25px;
  }
}

@media (max-width: 560px) {
  .preview-panel,
  .control-panel {
    padding: 14px;
  }

  .preview-header {
    display: grid;
  }

  .preview-tools {
    width: 100%;
    display: grid;
    grid-template-columns: 1fr auto;
    justify-content: stretch;
  }

  .canvas-shell {
    min-height: 260px;
  }

  .empty-state,
  .loading-state {
    min-height: 260px;
  }

  .controls {
    gap: 10px;
  }

  .upload-tile,
  .upload-tile.has-image,
  .upload-tile.has-image img {
    min-height: 136px;
  }

  .upload-tile strong {
    font-size: 15px;
  }

  .upload-tile small {
    font-size: 11px;
  }

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

  .primary-button {
    grid-column: 1 / -1;
  }

  .ratio-button {
    min-width: 0;
  }

  .modal-backdrop {
    padding: 10px;
    align-items: end;
  }

  .modal-panel {
    max-height: 88vh;
    padding: 14px;
  }

  .saved-grid {
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }

  .saved-actions {
    grid-template-columns: 1fr;
  }

  .small-modal {
    width: 100%;
  }

  .ratio-options {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}
