:root {
  color-scheme: light;
  --bg: #f5f5f0;
  --card: #ffffff;
  --ink: #1e1e1c;
  --text: #2c2c2a;
  --muted: #8a8a82;
  --line: #e6e6dd;
  --soft: #fafaf7;
  --chip: #f3f3ee;
  --danger: #b84747;
  --shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.08), 0 8px 20px -8px rgba(0, 0, 0, 0.04);
}

* {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100svh;
  color: var(--text);
  background:
    radial-gradient(circle at top left, rgba(255, 232, 185, 0.9), transparent 28rem),
    radial-gradient(circle at bottom right, rgba(206, 232, 219, 0.75), transparent 26rem),
    var(--bg);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

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

button {
  border: 0;
  cursor: pointer;
}

h1,
h2,
p {
  margin: 0;
}

.app {
  min-height: 100vh;
  min-height: 100svh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 28px 18px;
}

.factory-card {
  width: min(780px, 100%);
  display: flex;
  flex-direction: column;
  gap: 22px;
  padding: 34px;
  border: 1px solid rgba(255, 255, 255, 0.75);
  border-radius: 32px;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
}

.title-section {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 14px;
  padding-bottom: 12px;
  border-bottom: 1px solid #eaeae5;
}

.header-actions {
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  flex-wrap: wrap;
}

.profile-button {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  gap: 9px;
  padding: 5px 12px 5px 5px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: var(--ink);
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.05);
}

.profile-avatar,
.profile-panel-avatar {
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: #1e1e1c;
  color: #fff;
  font-weight: 800;
}

.profile-avatar {
  width: 34px;
  height: 34px;
  font-size: 14px;
}

.profile-meta {
  display: grid;
  gap: 1px;
  text-align: left;
}

.profile-meta strong {
  max-width: 112px;
  overflow: hidden;
  color: var(--ink);
  font-size: 13px;
  line-height: 1.1;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.profile-meta small {
  color: #d08b00;
  font-size: 12px;
  font-weight: 800;
  line-height: 1.1;
}

h1 {
  margin-top: 5px;
  color: var(--ink);
  font-size: clamp(30px, 7vw, 46px);
  font-weight: 650;
  line-height: 1.02;
  letter-spacing: 0;
}

.subtitle {
  color: var(--muted);
  font-size: 13px;
  font-weight: 550;
}

.status-pill {
  flex: 0 0 auto;
  min-height: 32px;
  display: inline-grid;
  place-items: center;
  padding: 0 13px;
  border-radius: 999px;
  background: #f0f0e9;
  color: #5e5e55;
  font-size: 12px;
  font-weight: 650;
}

.auth-modal[hidden],
.profile-modal[hidden] {
  display: none;
}

.auth-modal,
.profile-modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(30, 30, 28, 0.38);
  backdrop-filter: blur(8px);
}

.auth-dialog,
.profile-dialog {
  width: min(440px, 100%);
  display: grid;
  gap: 14px;
  padding: 20px;
  border: 1px solid rgba(255, 255, 255, 0.72);
  border-radius: 26px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: var(--shadow);
}

.auth-dialog {
  gap: 16px;
  padding: 22px;
  border-radius: 24px;
}

.profile-dialog {
  overflow: hidden;
  padding: 0;
  max-height: min(760px, calc(100vh - 36px));
  grid-template-rows: auto auto auto minmax(0, 1fr);
}

.auth-dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.auth-kicker {
  margin-bottom: 5px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 850;
}

.auth-dialog-header h2 {
  color: var(--ink);
  font-size: 22px;
  line-height: 1.15;
}

.icon-button {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: #f1f1eb;
  color: #62625a;
  font-size: 24px;
  line-height: 1;
}

.auth-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  padding: 5px;
  border-radius: 999px;
  background: #f1f1eb;
}

.auth-tab {
  min-height: 38px;
  border-radius: 999px;
  background: transparent;
  color: #66685f;
  font-weight: 750;
}

.auth-tab.active {
  background: #fff;
  color: var(--ink);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.05);
}

.auth-form {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

.auth-form label {
  display: grid;
  gap: 6px;
  color: #66685f;
  font-size: 12px;
  font-weight: 700;
}

.auth-form input {
  min-width: 0;
  height: 42px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #fff;
  color: var(--ink);
  padding: 0 12px;
  outline: 0;
}

.password-field {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

.password-field input {
  width: 100%;
}

.field-action {
  min-width: 72px;
  min-height: 42px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #f6f6f1;
  color: var(--ink);
  font-size: 13px;
  font-weight: 800;
}

.auth-form input:focus {
  border-color: #b6c8ba;
  box-shadow: 0 0 0 3px rgba(182, 200, 186, 0.25);
}

.auth-hint {
  color: var(--muted);
  font-size: 13px;
}

.profile-cover {
  position: relative;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 14px;
  align-items: center;
  padding: 22px;
  background: linear-gradient(135deg, #1e1e1c, #4f5f52);
  color: #fff;
}

.profile-cover .icon-button {
  position: absolute;
  top: 14px;
  right: 14px;
  background: rgba(255, 255, 255, 0.16);
  color: #fff;
}

.profile-panel-avatar {
  width: 62px;
  height: 62px;
  background: #fff;
  color: var(--ink);
  font-size: 24px;
}

.profile-kicker {
  margin-bottom: 4px;
  color: rgba(255, 255, 255, 0.72);
  font-size: 12px;
  font-weight: 800;
}

.profile-cover h2 {
  max-width: 260px;
  overflow: hidden;
  font-size: 25px;
  line-height: 1.1;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.profile-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  padding: 16px;
}

.profile-grid div {
  min-height: 74px;
  display: grid;
  align-content: center;
  gap: 6px;
  padding: 13px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: #fafaf8;
}

.profile-grid span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 750;
}

.profile-grid strong {
  min-width: 0;
  overflow-wrap: anywhere;
  color: var(--ink);
  font-size: 16px;
}

#profilePanelBalance {
  color: #d08b00;
  font-size: 24px;
}

.profile-actions {
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px 16px;
}

.profile-records-section {
  display: grid;
  gap: 10px;
  padding: 0 16px 16px;
  min-height: 0;
  overflow: hidden;
}

.profile-records-toggle {
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: #fafaf8;
  color: var(--ink);
  font-weight: 800;
}

.profile-records-toggle strong {
  color: var(--muted);
  font-size: 12px;
}

.profile-records-panel {
  display: grid;
  gap: 10px;
  min-height: 0;
}

.profile-records-panel .records-list {
  max-height: min(280px, 38vh);
  overflow-y: auto;
  padding-right: 3px;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.danger-text {
  color: var(--danger);
}

.image-preview-area {
  min-height: 330px;
  height: clamp(280px, 44svh, 430px);
  display: grid;
  place-items: center;
  overflow: hidden;
  padding: 16px;
  border-radius: 28px;
  background: #fafaf8;
  box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.02), 0 0 0 1px rgba(0, 0, 0, 0.03);
}

.result {
  width: 100%;
  min-height: 100%;
  display: grid;
  place-items: center;
}

.empty-state {
  display: grid;
  justify-items: center;
  gap: 12px;
  color: #a8a89a;
  text-align: center;
}

.empty-state span {
  width: 70px;
  height: 70px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: #fff;
  color: #b5b5a8;
  font-size: 32px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.04);
}

.result img {
  display: block;
  max-width: 100%;
  max-height: 58vh;
  border-radius: 20px;
  background: #fff;
  box-shadow: 0 16px 35px rgba(0, 0, 0, 0.08);
  object-fit: contain;
}

.result.error {
  place-items: start;
  color: var(--danger);
  font-size: 14px;
  line-height: 1.55;
}

.call-status {
  display: grid;
  gap: 9px;
  padding: 12px 14px;
  border: 1px solid #dfdfd5;
  border-radius: 18px;
  background: #fbfbf7;
}

.call-status-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.call-status-row strong {
  color: var(--ink);
  font-size: 13px;
}

.call-status-row span {
  min-width: 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
  text-align: right;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.call-progress {
  height: 6px;
  overflow: hidden;
  border-radius: 999px;
  background: #ededdf;
}

.call-progress span {
  width: 0;
  height: 100%;
  display: block;
  border-radius: inherit;
  background: #1e1e1c;
  transition: width 0.25s ease;
}

.composer {
  display: grid;
  gap: 10px;
}

.mode-switch {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  padding: 5px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #f3f3ee;
}

.mode-button {
  min-height: 40px;
  border-radius: 999px;
  background: transparent;
  color: #66685f;
  font-weight: 750;
}

.mode-button.active {
  background: #fff;
  color: var(--ink);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.05);
}

.upload-panel[hidden] {
  display: none;
}

.upload-panel {
  display: grid;
  grid-template-columns: 92px minmax(0, 1fr);
  gap: 10px;
  align-items: start;
}

.upload-button {
  min-height: 92px;
  display: grid;
  place-items: center;
  gap: 5px;
  padding: 10px;
  border: 1px dashed #c8c8bc;
  border-radius: 20px;
  background: rgba(250, 250, 247, 0.92);
  color: #4a4a44;
  text-align: center;
}

.upload-button input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
}

.upload-button span {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: #fff;
  color: var(--ink);
  font-size: 22px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.05);
}

.upload-button strong {
  font-size: 13px;
}

.source-list {
  min-height: 92px;
  display: flex;
  gap: 10px;
  overflow-x: auto;
  padding-bottom: 2px;
  scrollbar-width: none;
}

.source-list::-webkit-scrollbar {
  display: none;
}

.source-thumb {
  flex: 0 0 92px;
  position: relative;
  margin: 0;
  overflow: hidden;
  border-radius: 20px;
  background: #fff;
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.08);
}

.source-thumb img {
  width: 100%;
  height: 92px;
  display: block;
  object-fit: cover;
}

.source-thumb figcaption {
  position: absolute;
  top: 8px;
  left: 8px;
  padding: 5px 9px;
  border-radius: 999px;
  background: rgba(30, 30, 28, 0.82);
  color: #fff;
  font-size: 12px;
  font-weight: 800;
}

.source-hint {
  grid-column: 1 / -1;
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.prompt-label {
  color: #4a4a44;
  font-size: 13px;
  font-weight: 650;
}

.composer-box {
  display: grid;
  gap: 12px;
  padding: 14px;
  border: 1px solid #e2e2da;
  border-radius: 28px;
  background: rgba(250, 250, 247, 0.92);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.025);
}

textarea {
  width: 100%;
  min-height: 126px;
  resize: vertical;
  border: 0;
  outline: none;
  background: transparent;
  color: var(--text);
  font-size: 16px;
  line-height: 1.55;
}

textarea::placeholder {
  color: #b5b5a8;
}

.quick-prompts {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 1px;
  scrollbar-width: none;
}

.quick-prompts::-webkit-scrollbar {
  display: none;
}

.quick-tag,
.tool-button,
.pill-select,
.btn,
.text-button,
.generate-btn {
  border-radius: 999px;
  font-weight: 650;
}

.quick-tag {
  flex: 0 0 auto;
  min-height: 36px;
  padding: 0 13px;
  border: 1px solid var(--line);
  background: #fff;
  color: #4a4a44;
  font-size: 13px;
}

.quick-tag.active,
.quick-tag:hover {
  background: #e9e9e2;
  border-color: #c5c5b8;
  color: var(--ink);
}

.edit-only[hidden] {
  display: none;
}

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

.tool-button,
.pill-select {
  min-height: 42px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 0 13px;
  border: 1px solid var(--line);
  background: #fff;
  color: #4a4a44;
  font-size: 13px;
}

.pill-select span {
  color: var(--muted);
}

.pill-select select {
  max-width: 74px;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--ink);
  font-weight: 650;
}

.generate-btn {
  min-height: 44px;
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 0 22px;
  background: var(--ink);
  color: #fff;
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.1);
}

.generate-btn:hover {
  background: #000;
}

.generate-btn:active,
.btn:active,
.tool-button:active,
.quick-tag:active {
  transform: scale(0.97);
}

.generate-btn:disabled {
  cursor: wait;
  opacity: 0.55;
}

.loading-indicator {
  width: 14px;
  height: 14px;
  display: inline-block;
  border: 2px solid rgba(255, 255, 255, 0.34);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
}

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

.action-buttons {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

.btn {
  min-height: 46px;
  padding: 0 18px;
  border: 1px solid #d6d6cc;
  background: #fff;
  color: var(--ink);
}

.btn:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.btn[hidden] {
  display: none;
}

.btn-download {
  background: var(--ink);
  color: #fff;
  border-color: transparent;
}

.gallery-section,
.records-section {
  display: grid;
  gap: 10px;
}

.gallery-heading,
.records-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.gallery-heading h2,
.records-heading h2 {
  color: #4a4a44;
  font-size: 14px;
}

.text-button {
  min-height: 30px;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
}

.gallery {
  min-height: 74px;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px;
}

.gallery button {
  aspect-ratio: 1;
  overflow: hidden;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: #fafaf8;
}

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

.gallery-empty {
  grid-column: 1 / -1;
  display: grid;
  place-items: center;
  border: 1px dashed var(--line);
  border-radius: 18px;
  color: var(--muted);
  font-size: 13px;
}

.records-list {
  display: grid;
  gap: 8px;
}

.record-item,
.records-empty {
  border: 1px solid var(--line);
  border-radius: 18px;
  background: #fafaf8;
}

.record-item {
  display: grid;
  gap: 7px;
  padding: 11px 12px;
}

.record-top,
.record-bottom {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.record-title {
  min-width: 0;
  overflow: hidden;
  color: var(--ink);
  font-size: 13px;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.record-time,
.record-meta,
.record-prompt {
  color: var(--muted);
  font-size: 12px;
}

.record-prompt {
  overflow: hidden;
  line-height: 1.4;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.record-badge {
  flex: 0 0 auto;
  padding: 4px 8px;
  border-radius: 999px;
  background: #eeeeE7;
  color: #5f6258;
  font-size: 12px;
  font-weight: 850;
}

.record-badge.success,
.record-badge.succeeded {
  background: #e5f4e9;
  color: #25713a;
}

.record-badge.error,
.record-badge.failed {
  background: #f8e7e7;
  color: #a53e3e;
}

.record-badge.pending,
.record-badge.queued,
.record-badge.processing {
  background: #fff4d8;
  color: #8a6500;
}

.records-empty {
  display: grid;
  place-items: center;
  min-height: 56px;
  color: var(--muted);
  font-size: 13px;
}

.hint-text {
  color: #98988a;
  font-size: 12px;
  line-height: 1.45;
}

.raw-output {
  display: none;
}

@media (max-width: 680px) {
  html {
    overflow-x: hidden;
    touch-action: manipulation;
  }

  body {
    min-height: 100vh;
    min-height: 100svh;
    overflow-x: hidden;
    background: var(--bg);
  }

  button,
  textarea,
  select,
  input {
    font-size: 16px;
  }

  .app {
    align-items: stretch;
    min-height: 100vh;
    min-height: 100svh;
    padding: 0;
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
  }

  .factory-card {
    width: 100%;
    min-height: 100vh;
    min-height: 100svh;
    gap: 14px;
    padding: 12px;
    border: 0;
    border-radius: 0;
    background: #fff;
    box-shadow: none;
    backdrop-filter: none;
  }

  .title-section {
    position: sticky;
    top: 0;
    z-index: 5;
    justify-content: flex-end;
    padding: 4px 0 10px;
    background: #fff;
  }

  .status-pill {
    min-height: 30px;
    padding: 0 10px;
  }

  .header-actions {
    width: 100%;
    justify-content: flex-end;
  }

  .profile-button {
    max-width: 100%;
    min-height: 42px;
  }

  .profile-meta strong {
    max-width: 160px;
    font-size: 14px;
  }

  .profile-meta small {
    font-size: 13px;
  }

  .image-preview-area {
    min-height: 220px;
    height: clamp(220px, 38svh, 320px);
    padding: 10px;
    border-radius: 18px;
  }

  .result {
    min-height: 100%;
  }

  .result img {
    max-height: calc(38svh - 24px);
    border-radius: 14px;
  }

  .composer-box {
    border-radius: 18px;
    padding: 12px;
  }

  .upload-panel {
    grid-template-columns: 86px minmax(0, 1fr);
  }

  .upload-button,
  .source-list {
    min-height: 86px;
  }

  .source-thumb {
    flex-basis: 86px;
  }

  .source-thumb img {
    height: 86px;
  }

  textarea {
    min-height: 104px;
    max-height: 180px;
    font-size: 16px;
  }

  .quick-tag,
  .tool-button,
  .pill-select,
  .text-button,
  .prompt-label,
  .source-hint,
  .hint-text,
  .auth-hint {
    font-size: 14px;
  }

  .pill-select select {
    max-width: none;
    font-size: 16px;
  }

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

  .tool-button,
  .pill-select,
  .generate-btn {
    min-height: 44px;
    justify-content: center;
  }

  .generate-btn {
    grid-column: 1 / -1;
    margin-left: 0;
  }

  .action-buttons {
    grid-template-columns: 1fr;
  }

  .gallery {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
  }
}

@media (max-width: 380px) {
  .app {
    padding: 0;
  }

  .factory-card {
    min-height: 100vh;
    min-height: 100svh;
    border-radius: 0;
  }

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

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