:root {
  --bg: #ffffff;
  --ink: #111111;
  --muted: #777b80;
  --line: #dedede;
  --soft: #f6f8f7;
  --soft-2: #f2f5f3;
  --accent: #02d06f;
  --accent-dark: #06a85d;
  --danger: #d93636;
  --shadow: 0 18px 54px rgba(20, 24, 20, 0.08);
  --radius: 24px;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  min-width: 320px;
}

body {
  min-height: 100vh;
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", Arial, sans-serif;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  border: 0;
}

.page {
  width: min(1160px, calc(100% - 32px));
  margin: 0 auto;
  padding: 16px 0 32px;
}

.hero {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
}

h1 {
  margin: 0;
  font-size: clamp(42px, 6vw, 58px);
  line-height: 0.95;
  letter-spacing: 0;
  font-weight: 900;
}

.hero p {
  margin: 14px 0 0;
  color: #494d50;
  font-size: 14px;
}

.workspace {
  display: grid;
  grid-template-columns: 43% 1fr;
  gap: 18px;
  align-items: stretch;
}

.left-stack {
  display: grid;
  gap: 18px;
}

.panel {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: #fff;
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.02);
}

.config-panel {
  padding: 20px;
}

.prompt-panel {
  padding: 12px;
}

.panel-title {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 18px;
}

.panel-title h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 900;
}

.icon {
  display: inline-grid;
  width: 18px;
  height: 18px;
  place-items: center;
  color: #111;
  font-weight: 900;
}

label {
  display: block;
  margin: 14px 0 7px;
  color: #858585;
  font-size: 12px;
  font-weight: 700;
}

.field-label-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 14px 0 7px;
}

.field-label-row label {
  margin: 0;
}

.fetch-models-btn {
  border: 1px solid #d6d6d6;
  border-radius: 8px;
  padding: 4px 8px;
  background: #fff;
  color: #333;
  font-size: 12px;
  line-height: 1.25;
  cursor: pointer;
}

.fetch-models-btn:hover:not(:disabled) {
  border-color: #9ee032;
  background: #f7ffe9;
}

.fetch-models-btn:disabled {
  cursor: not-allowed;
  opacity: 0.6;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--soft);
  color: #151515;
  font-size: 14px;
  outline: 0;
  transition: border-color 0.15s, box-shadow 0.15s, background 0.15s;
}

input,
select {
  height: 40px;
  padding: 0 12px;
}

select option {
  font-size: 13px;
}

input:focus,
select:focus,
textarea:focus {
  border-color: #9ee032;
  box-shadow: 0 0 0 3px rgba(158, 224, 50, 0.18);
  background: #fff;
}

.input-with-icon {
  position: relative;
}

.input-with-icon span {
  position: absolute;
  top: 50%;
  left: 12px;
  transform: translateY(-50%);
  color: #777;
  pointer-events: none;
}

.input-with-icon input {
  padding-left: 38px;
}

.key-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 42px;
  gap: 8px;
  align-items: center;
}

.key-toggle {
  position: relative;
  display: inline-grid;
  width: 42px;
  height: 40px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
  color: #202020;
  cursor: pointer;
  transition: transform 0.16s, border-color 0.16s, box-shadow 0.16s;
}

.key-toggle:hover {
  transform: translateY(-1px);
  border-color: #bfc6c1;
}

.key-toggle:focus-visible {
  outline: 0;
  border-color: #9ee032;
  box-shadow: 0 0 0 3px rgba(158, 224, 50, 0.18);
}

.key-toggle svg {
  width: 19px;
  height: 19px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.key-toggle.visible::after {
  content: "";
  position: absolute;
  width: 24px;
  height: 2px;
  border-radius: 999px;
  background: currentColor;
  transform: rotate(-35deg);
}

.form-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(190px, 0.95fr) 96px;
  gap: 14px;
  align-items: start;
}

textarea {
  min-height: 132px;
  resize: vertical;
  padding: 18px 16px;
  border: 0;
  border-radius: 18px;
  background: var(--soft);
  line-height: 1.55;
}

.custom-field {
  margin-top: 8px;
}

.custom-size {
  display: grid;
  grid-template-columns: minmax(78px, 1fr) auto minmax(78px, 1fr);
  gap: 8px;
  align-items: center;
  margin-top: 8px;
}

.custom-size span {
  color: #777;
  font-size: 12px;
}

.custom-size input {
  height: 34px;
  padding: 0 9px;
}

.prompt-label {
  margin: 4px 0 8px 4px;
  color: #111;
  font-size: 13px;
  font-weight: 800;
}

.reference-row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 58px;
  padding: 10px 8px 2px;
}

.ghost-btn,
.primary-btn,
.switch-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  cursor: pointer;
  white-space: nowrap;
  transition: transform 0.16s, background 0.16s, border-color 0.16s, color 0.16s;
}

.ghost-btn {
  min-height: 38px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: #202020;
  font-weight: 800;
  font-size: 13px;
}

.ghost-btn:hover,
.primary-btn:hover,
.switch-btn:hover {
  transform: translateY(-1px);
}

.ghost-btn.small {
  min-height: 32px;
  padding: 0 12px;
  font-size: 12px;
}

.reference-list {
  display: flex;
  gap: 8px;
  min-width: 0;
  overflow-x: auto;
}

.reference-chip {
  position: relative;
  flex: 0 0 46px;
  width: 46px;
  height: 46px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--soft);
}

.reference-chip img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.reference-chip button {
  position: absolute;
  top: 3px;
  right: 3px;
  display: grid;
  width: 17px;
  height: 17px;
  place-items: center;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.92);
  color: #111;
  cursor: pointer;
  font-size: 12px;
  line-height: 1;
}

.action-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 8px;
}

.action-row p {
  margin: 0;
  color: #8a8d91;
  font-size: 12px;
}

.primary-btn {
  min-height: 46px;
  padding: 0 26px;
  border-radius: 999px;
  background: var(--accent);
  color: #07130d;
  font-weight: 900;
}

.primary-btn:disabled {
  cursor: wait;
  transform: none;
  background: #98e9bd;
}

.output-panel {
  display: flex;
  min-height: 0;
  flex-direction: column;
  padding: 20px;
}

.output-panel .panel-title {
  margin-bottom: 14px;
}

.view-switch {
  display: inline-flex;
  gap: 4px;
  margin-left: auto;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--soft);
}

.switch-btn {
  min-height: 30px;
  padding: 0 12px;
  border-radius: 999px;
  background: transparent;
  color: #60676d;
  font-size: 12px;
  font-weight: 800;
}

.switch-btn.active {
  background: #111;
  color: #fff;
}

.result-view {
  display: flex;
  min-height: 0;
  flex: 1;
  flex-direction: column;
}

.result-view[hidden] {
  display: none !important;
}

.result-canvas {
  position: relative;
  display: grid;
  min-height: 0;
  flex: 1;
  place-items: center;
  overflow: hidden;
  border: 1px dashed #cfd7d0;
  border-radius: 24px;
  background: var(--soft-2);
  color: #777;
}

.save-image-btn {
  position: absolute;
  right: 16px;
  bottom: 16px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 38px;
  padding: 0 16px;
  border-radius: 999px;
  background: rgba(17, 17, 17, 0.88);
  color: #fff;
  font-size: 13px;
  font-weight: 800;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.18);
  cursor: pointer;
}

.save-image-btn:hover {
  background: #111;
}

.result-canvas.empty img {
  display: none;
}

.result-canvas img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #f4f4f4;
}

.result-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 10px;
  width: 100%;
  height: 100%;
  padding: 10px;
}

.result-grid img {
  border-radius: 14px;
  background: #eee;
}

.result-strip {
  display: flex;
  gap: 9px;
  min-height: 0;
  margin-top: 10px;
  overflow-x: auto;
}

.thumb {
  position: relative;
  flex: 0 0 86px;
  height: 64px;
  overflow: hidden;
  border: 2px solid transparent;
  border-radius: 10px;
  background: #f1f1f1;
  cursor: pointer;
}

.thumb.active {
  border-color: var(--accent);
}

.thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.thumb button {
  position: absolute;
  right: 4px;
  bottom: 4px;
  padding: 3px 5px;
  border-radius: 6px;
  background: rgba(17, 17, 17, 0.78);
  color: #fff;
  font-size: 11px;
  cursor: pointer;
}

.history-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
  color: #5d6368;
  font-size: 13px;
}

.history-view {
  position: relative;
  min-height: 0;
  flex: 1;
  padding-bottom: 42px;
}

.history-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
  max-height: none;
  overflow-y: auto;
  padding-right: 2px;
}

.history-card {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
}

.history-load {
  display: block;
  width: 100%;
  padding: 0;
  background: transparent;
  cursor: pointer;
}

.history-card img {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
  background: #f2f2f2;
}

.history-meta {
  padding: 9px;
}

.history-prompt {
  display: -webkit-box;
  min-height: 34px;
  overflow: hidden;
  color: #202020;
  font-size: 12px;
  line-height: 1.4;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}

.history-sub {
  margin-top: 5px;
  color: #8b8f94;
  font-size: 11px;
}

.history-rule {
  position: absolute;
  right: 2px;
  bottom: 0;
  max-width: min(520px, 100%);
  color: #8b8f94;
  font-size: 12px;
  line-height: 1.45;
  text-align: right;
}

.toast-stack {
  position: fixed;
  top: 18px;
  right: 18px;
  z-index: 20;
  display: grid;
  gap: 10px;
}

.toast {
  width: min(380px, calc(100vw - 36px));
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
  box-shadow: var(--shadow);
  color: #222;
  font-size: 13px;
}

.toast strong {
  display: block;
  margin-bottom: 2px;
}

.toast.error strong {
  color: var(--danger);
}

.toast.success strong {
  color: var(--accent-dark);
}

@media (max-width: 980px) {
  .workspace {
    grid-template-columns: 1fr;
  }

  .output-panel {
    min-height: 560px;
  }

  .result-canvas {
    min-height: 460px;
  }
}

@media (max-width: 640px) {
  .page {
    width: min(100% - 20px, 1160px);
    padding-top: 16px;
  }

  .hero {
    display: block;
  }

  .config-panel,
  .output-panel {
    padding: 14px;
  }

  .form-grid,
  .action-row,
  .reference-row {
    grid-template-columns: 1fr;
    display: grid;
  }

  .primary-btn,
  .ghost-btn {
    width: 100%;
  }

  .result-canvas {
    min-height: 340px;
  }
}
