:root {
  color-scheme: light;
  --bg: #f5f5f7;
  --surface: #ffffff;
  --surface-soft: #fbfbfd;
  --surface-muted: #f2f2f7;
  --ink: #1d1d1f;
  --ink-soft: #3a3a3c;
  --muted: #86868b;
  --line: rgba(60, 60, 67, 0.14);
  --blue: #007aff;
  --blue-hover: #006ee6;
  --danger: #ff3b30;
  --radius: 8px;
  --shadow: 0 18px 55px rgba(0, 0, 0, 0.08);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family:
    -apple-system, BlinkMacSystemFont, "SF Pro Text", "SF Pro Display", "Segoe UI",
    system-ui, sans-serif;
}

button,
textarea,
select {
  font: inherit;
}

button,
a,
select,
textarea {
  -webkit-tap-highlight-color: transparent;
}

button,
select {
  cursor: pointer;
}

svg {
  display: block;
  width: 17px;
  height: 17px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

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

.sidebar,
.image-workspace {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.86);
  box-shadow: var(--shadow);
  backdrop-filter: blur(22px);
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: calc(100vh - 24px);
  padding: 14px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 42px;
}

.brand-mark {
  display: grid;
  width: 36px;
  height: 36px;
  place-items: center;
  border-radius: var(--radius);
  background: var(--ink);
  color: #fff;
  font-size: 17px;
  font-weight: 700;
}

.brand h1 {
  margin: 0;
  font-size: 17px;
  line-height: 1.2;
  letter-spacing: 0;
}

.new-image,
.history-toggle,
.send-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border: 0;
  border-radius: var(--radius);
  background: var(--blue);
  color: #fff;
  font-weight: 650;
  transition:
    background 150ms ease,
    transform 150ms ease,
    opacity 150ms ease;
}

.new-image {
  min-height: 38px;
  padding: 0 12px;
}

.history-toggle {
  display: none;
  min-height: 38px;
  padding: 0 12px;
  background: var(--surface-muted);
  color: var(--ink);
}

.new-image:hover,
.history-toggle:hover,
.send-button:hover {
  background: var(--blue-hover);
}

.history-toggle:hover {
  background: rgba(0, 0, 0, 0.07);
}

.new-image:active,
.history-toggle:active,
.send-button:active,
.history-item:active {
  transform: scale(0.99);
}

.history-panel {
  display: grid;
  gap: 8px;
  min-height: 0;
}

.history-panel h2 {
  margin: 0;
  padding: 0 4px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

.history-list {
  display: grid;
  gap: 4px;
  max-height: min(58vh, 560px);
  overflow-y: auto;
  padding-right: 2px;
}

.history-item {
  display: grid;
  gap: 3px;
  width: 100%;
  min-height: 52px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--ink);
  padding: 9px 10px;
  text-align: left;
  transition:
    background 150ms ease,
    transform 150ms ease;
}

.history-item:hover {
  background: rgba(0, 0, 0, 0.04);
}

.history-item.active {
  background: var(--surface-muted);
}

.history-item span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
  font-weight: 600;
}

.history-item small {
  color: var(--muted);
  font-size: 12px;
}

.status-panel {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: auto;
  border-top: 1px solid var(--line);
  padding: 12px 4px 0;
  color: var(--muted);
  font-size: 13px;
}

.status-dot {
  width: 8px;
  height: 8px;
  flex: 0 0 auto;
  border-radius: 999px;
  background: #34c759;
}

.view {
  display: none;
}

.view.active {
  display: grid;
}

.image-workspace {
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-height: calc(100vh - 24px);
  overflow: hidden;
}

.workspace-header {
  display: flex;
  align-items: center;
  min-height: 58px;
  border-bottom: 1px solid var(--line);
  padding: 0 clamp(16px, 3vw, 34px);
}

.workspace-header h2 {
  margin: 0;
  font-size: 20px;
  line-height: 1.15;
  letter-spacing: 0;
}

.image-empty {
  display: grid;
  place-items: center;
  min-height: 100%;
  color: var(--muted);
  text-align: center;
}

.image-empty h2 {
  margin: 0;
  color: var(--muted);
  font-size: clamp(28px, 5vw, 56px);
  font-weight: 650;
  line-height: 1.05;
  letter-spacing: 0;
}

.image-empty p {
  margin: 0;
}

.typing {
  display: inline-flex;
  gap: 5px;
  align-items: center;
}

.typing span {
  width: 6px;
  height: 6px;
  border-radius: 999px;
  background: var(--muted);
  animation: bounce 900ms infinite ease-in-out;
}

.typing span:nth-child(2) {
  animation-delay: 120ms;
}

.typing span:nth-child(3) {
  animation-delay: 240ms;
}

@keyframes bounce {
  0%,
  80%,
  100% {
    transform: translateY(0);
    opacity: 0.4;
  }

  40% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

.image-form {
  display: grid;
  gap: 10px;
  border-top: 1px solid var(--line);
  background: rgba(251, 251, 253, 0.92);
  padding: 12px clamp(16px, 4vw, 54px);
}

.image-panel {
  min-height: calc(100vh - 24px);
}

.image-stage {
  display: grid;
  place-items: center;
  overflow-y: auto;
  padding: 24px clamp(16px, 4vw, 54px);
}

.image-error h2,
.image-error p {
  color: var(--danger);
}

.image-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 320px), 1fr));
  gap: 14px;
  width: min(100%, 1040px);
  align-items: start;
}

.generated-image {
  display: grid;
  gap: 10px;
  width: 100%;
  margin: 0;
  border-radius: var(--radius);
  background: var(--surface);
  padding: 10px;
  box-shadow: 0 1px 0 var(--line);
}

.generated-image img {
  display: block;
  width: 100%;
  max-height: 68vh;
  object-fit: contain;
  border-radius: 7px;
  background: var(--surface-muted);
}

.generated-image figcaption {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
  overflow-wrap: anywhere;
}

.image-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

.image-actions a {
  display: inline-flex;
  min-height: 34px;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  background: var(--surface-muted);
  color: var(--blue);
  padding: 0 12px;
  font-size: 14px;
  font-weight: 650;
  text-decoration: none;
}

.result-reference {
  width: 34px;
  height: 34px;
  object-fit: cover;
  border-radius: 7px;
  background: var(--surface-muted);
}

.reference-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

#referenceImage {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.reference-button,
.reference-preview {
  display: inline-flex;
  min-height: 38px;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  padding: 0 12px;
  font-size: 14px;
  font-weight: 650;
  box-shadow: 0 1px 0 var(--line);
  cursor: pointer;
}

.reference-preview {
  gap: 6px;
  padding: 4px;
}

.reference-preview img {
  width: 42px;
  height: 30px;
  object-fit: cover;
  border-radius: 6px;
}

.reference-preview button {
  display: grid;
  width: 28px;
  height: 28px;
  place-items: center;
  border: 0;
  border-radius: 999px;
  background: var(--surface-muted);
  color: var(--muted);
}

.image-controls {
  display: grid;
  grid-template-columns: repeat(2, minmax(120px, 180px)) 96px;
  gap: 10px;
  align-items: end;
}

.image-controls label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}

select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  outline: 0;
}

select {
  min-height: 38px;
  padding: 0 11px;
}

textarea {
  max-height: 180px;
  resize: none;
  padding: 12px 13px;
  line-height: 1.5;
}

textarea::placeholder {
  color: var(--muted);
}

select:focus,
textarea:focus {
  border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.16);
}

.send-button {
  min-height: 42px;
  align-self: start;
}

.image-submit {
  min-height: 38px;
}

.send-button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.loader {
  width: 32px;
  height: 32px;
  margin-bottom: 14px;
  border: 3px solid rgba(60, 60, 67, 0.16);
  border-top-color: var(--blue);
  border-radius: 999px;
  animation: spin 900ms linear infinite;
}

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

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

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

  .sidebar {
    position: sticky;
    top: 8px;
    z-index: 5;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 40px 40px;
    align-items: center;
    min-height: auto;
    gap: 8px;
    padding: 8px;
  }

  .brand {
    min-height: 34px;
  }

  .brand-mark {
    width: 32px;
    height: 32px;
    font-size: 15px;
  }

  .brand h1 {
    font-size: 15px;
  }

  .history-panel {
    display: none;
  }

  .history-open .history-panel {
    display: grid;
    grid-column: 1 / -1;
    max-height: 34vh;
    overflow: hidden;
    border-top: 1px solid var(--line);
    padding-top: 8px;
  }

  .history-open .history-list {
    display: grid;
    max-height: calc(34vh - 28px);
    overflow-x: hidden;
    overflow-y: auto;
  }

  .history-open .history-item {
    min-width: 0;
  }

  .new-image {
    min-width: 40px;
    min-height: 32px;
    padding: 0 10px;
  }

  .status-panel {
    display: none;
  }

  .history-toggle {
    display: inline-flex;
    min-width: 40px;
    min-height: 32px;
    padding: 0;
  }

  .image-panel,
  .image-workspace {
    min-height: calc(100vh - 64px);
  }

  .workspace-header {
    display: none;
  }

  .image-stage {
    padding: 16px 12px;
  }

  .image-form {
    padding: 10px;
  }

  .send-button {
    width: 100%;
  }

  .image-controls {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 520px) {
  .app-shell {
    padding: 6px;
  }

  .sidebar {
    top: 6px;
    grid-template-columns: minmax(0, 1fr) 34px 34px;
    padding: 6px;
  }

  .brand h1 {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .new-image span {
    display: none;
  }

  .new-image {
    min-width: 34px;
    min-height: 30px;
    padding: 0;
  }

  .history-toggle {
    min-width: 34px;
    min-height: 30px;
  }

  .image-panel,
  .image-workspace {
    min-height: calc(100vh - 52px);
  }

  .image-empty h2 {
    font-size: 32px;
  }
}
