:root {
  color-scheme: light;
  --bg: #f4f1eb;
  --grid: rgba(55, 65, 81, 0.055);
  --surface: #fffdf9;
  --surface-soft: #f8f5ef;
  --surface-strong: #eee8dc;
  --field: #fffefa;
  --ink: #202932;
  --muted: #707780;
  --line: #d9d2c6;
  --line-strong: #b9af9f;
  --primary: #2f6f82;
  --primary-strong: #225869;
  --primary-soft: #e3f1f4;
  --accent: #9b6a20;
  --accent-soft: #fff2cf;
  --danger: #b42318;
  --ok: #16724f;
  --shadow: 0 16px 36px rgba(31, 38, 46, 0.1);
  --shadow-soft: 0 8px 22px rgba(31, 38, 46, 0.07);
  --radius: 8px;
  --radius-sm: 4px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    color-scheme: dark;
    --bg: #111315;
    --grid: rgba(232, 226, 215, 0.045);
    --surface: #181b1f;
    --surface-soft: #1f2328;
    --surface-strong: #2a3036;
    --field: #14171a;
    --ink: #ebe7de;
    --muted: #aaa39a;
    --line: #343941;
    --line-strong: #4a515b;
    --primary: #79aebd;
    --primary-strong: #9dc6d0;
    --primary-soft: #1d343b;
    --accent: #d4a64e;
    --accent-soft: #352a16;
    --danger: #ff8b7f;
    --ok: #74d0a3;
    --shadow: 0 18px 42px rgba(0, 0, 0, 0.28);
    --shadow-soft: 0 10px 28px rgba(0, 0, 0, 0.22);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111315;
  --grid: rgba(232, 226, 215, 0.045);
  --surface: #181b1f;
  --surface-soft: #1f2328;
  --surface-strong: #2a3036;
  --field: #14171a;
  --ink: #ebe7de;
  --muted: #aaa39a;
  --line: #343941;
  --line-strong: #4a515b;
  --primary: #79aebd;
  --primary-strong: #9dc6d0;
  --primary-soft: #1d343b;
  --accent: #d4a64e;
  --accent-soft: #352a16;
  --danger: #ff8b7f;
  --ok: #74d0a3;
  --shadow: 0 18px 42px rgba(0, 0, 0, 0.28);
  --shadow-soft: 0 10px 28px rgba(0, 0, 0, 0.22);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  scroll-behavior: smooth;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  font-family: "IBM Plex Sans", "HarmonyOS Sans SC", "Microsoft YaHei", "Segoe UI", sans-serif;
  background-color: var(--bg);
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 32px 32px;
}

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

button,
input,
select,
textarea {
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--field);
  color: var(--ink);
}

input,
select,
textarea {
  width: 100%;
  padding: 11px 12px;
  outline: none;
  transition: border-color 160ms ease, box-shadow 160ms ease, background-color 160ms ease;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 20%, transparent);
}

textarea {
  resize: vertical;
}

button {
  min-height: 42px;
  padding: 9px 15px;
  background: var(--field);
  cursor: pointer;
  transition: none;
}

button:disabled {
  cursor: progress;
  opacity: 0.58;
  transform: none;
}

a {
  color: var(--primary-strong);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

label {
  display: grid;
  gap: 8px;
}

label > span,
summary,
.field-title {
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

summary {
  cursor: pointer;
  width: fit-content;
}

details {
  display: grid;
  gap: 10px;
}

details[open] summary {
  color: var(--primary-strong);
}

details textarea {
  margin-top: 10px;
  font-family: "Cascadia Mono", "Consolas", monospace;
  font-size: 13px;
}

.shell {
  width: min(1280px, calc(100vw - 32px));
  margin: 0 auto;
  padding: 24px 0 64px;
}

.gate {
  min-height: 100svh;
  display: grid;
  align-content: center;
  gap: 18px;
}

.gate .brand,
.gate .auth-panel {
  width: min(440px, 100%);
  margin-inline: auto;
}

.brand,
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
}

.brand {
  padding: 8px 0 10px;
}

.topbar {
  margin-bottom: 18px;
  padding: 12px 0 18px;
  border-bottom: 1px solid var(--line);
}

.brand span,
.topbar strong {
  font-size: clamp(26px, 4vw, 38px);
  line-height: 1;
  letter-spacing: -0.04em;
}

.topbar > div:first-child {
  display: grid;
  gap: 8px;
  min-width: 0;
}

.topbar span {
  color: var(--muted);
  font-size: 14px;
}

.brand-actions,
.top-actions,
.row-actions,
.card-actions,
.modal-actions,
.action-row {
  display: flex;
  align-items: center;
  gap: 9px;
  flex-wrap: wrap;
}

.brand-actions a,
.theme-toggle,
.plain {
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-soft);
  color: var(--ink);
}

.brand-actions a {
  min-height: 42px;
  display: inline-flex;
  align-items: center;
  padding: 9px 15px;
}

.theme-toggle {
  min-width: 74px;
}

.panel {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-soft);
  padding: 22px;
}

.auth-panel {
  display: grid;
  gap: 16px;
}

.auth-panel button,
.admin-grid form > button,
#modelCreateForm button {
  justify-self: start;
}

.hidden,
.visually-hidden {
  display: none !important;
}

.studio {
  display: grid;
  grid-template-columns: minmax(360px, 440px) minmax(0, 1fr);
  gap: 22px;
  align-items: start;
}

.input-panel,
.output-panel,
.stack {
  display: grid;
  gap: 18px;
}

.output-column {
  display: grid;
  gap: 22px;
  min-width: 0;
}

.section-title {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--ink);
  font-size: 21px;
  font-weight: 850;
  letter-spacing: -0.02em;
}

.accent-title::before {
  content: "";
  width: 4px;
  height: 22px;
  border-radius: 2px;
  background: var(--accent);
}

.prompt-field {
  position: relative;
}

.prompt-field textarea {
  min-height: 190px;
  line-height: 1.65;
}

.prompt-field small {
  justify-self: end;
  color: var(--muted);
}

.reference-section {
  display: grid;
  gap: 10px;
}

.upload-zone {
  min-height: 150px;
  display: grid;
  place-items: center;
  text-align: center;
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius);
  background: var(--surface-soft);
  padding: 18px;
  cursor: pointer;
  transition: border-color 160ms ease, background-color 160ms ease;
}

.upload-zone.is-dragging {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.upload-zone.is-disabled {
  cursor: not-allowed;
  opacity: 0.62;
}

.upload-zone input {
  display: none;
}

.upload-icon {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  margin-bottom: 4px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--field);
  color: var(--primary-strong);
  font-size: 24px;
}

.upload-zone small,
.meta {
  color: var(--muted);
  line-height: 1.55;
}

.reference-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.reference-thumb {
  position: relative;
  width: 84px;
  aspect-ratio: 1;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--surface-soft);
  animation: popIn 180ms ease both;
}

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

.reference-thumb button {
  position: absolute;
  top: 5px;
  right: 5px;
  width: 26px;
  height: 26px;
  min-height: 26px;
  padding: 0;
  border-radius: var(--radius-sm);
  border-color: transparent;
  background: rgba(0, 0, 0, 0.68);
  color: #fff;
}

.choice-row {
  display: flex;
  flex-wrap: wrap;
  gap: 9px;
}

.choice-pill {
  min-height: 38px;
  border-radius: var(--radius-sm);
  color: var(--muted);
  padding: 8px 13px;
  background: var(--field);
}

.choice-pill.active {
  color: var(--primary-strong);
  border-color: var(--primary);
  background: var(--primary-soft);
  font-weight: 800;
}

.compact-row {
  display: grid;
  grid-template-columns: 120px 1fr;
  gap: 14px;
  align-items: start;
}

.primary-action {
  width: 100%;
  min-height: 56px;
  border: 0;
  background: var(--primary);
  color: #fff;
  font-size: 18px;
  font-weight: 850;
  box-shadow: var(--shadow-soft);
}

.primary-action:disabled {
  animation: waiting 1.5s ease-in-out infinite;
}

.status-line {
  min-height: 1.4em;
  margin: 0;
  color: var(--primary-strong);
}

.notice {
  border: 1px solid color-mix(in srgb, var(--accent) 32%, var(--line));
  border-radius: var(--radius-sm);
  background: var(--accent-soft);
  color: var(--accent);
  padding: 12px 14px;
  line-height: 1.65;
}

.warning-notice {
  border: 1px solid color-mix(in srgb, var(--accent) 32%, var(--line));
  border-radius: var(--radius-sm);
  background: var(--accent-soft);
  color: var(--accent);
  padding: 12px 14px;
  line-height: 1.65;
}

.job-status {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-soft);
  overflow: hidden;
}

.job-status > div:first-child {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 15px;
  border-bottom: 1px solid var(--line);
}

.job-status span {
  color: var(--muted);
}

.thinking-steps {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
}

.thinking-steps span {
  position: relative;
  padding: 13px 14px 13px 32px;
  transition: color 180ms ease, background-color 180ms ease;
}

.thinking-steps span::before {
  content: "";
  position: absolute;
  left: 14px;
  top: 50%;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--line-strong);
  transform: translateY(-50%);
}

.thinking-steps span.active {
  color: var(--primary-strong);
  background: var(--surface);
}

.thinking-steps span.active::before {
  background: var(--primary);
  animation: dotPulse 900ms ease-in-out infinite;
}

.result-grid,
.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
}

.empty {
  display: grid;
  place-items: center;
  color: var(--muted);
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius);
  padding: 34px;
  min-height: 140px;
  background: var(--surface-soft);
}

.result-card,
.card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
  box-shadow: none;
  animation: riseIn 220ms ease both;
  transition: none;
}

.image-button {
  width: 100%;
  min-height: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background:
    linear-gradient(45deg, var(--surface-soft) 25%, transparent 25%),
    linear-gradient(-45deg, var(--surface-soft) 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, var(--surface-soft) 75%),
    linear-gradient(-45deg, transparent 75%, var(--surface-soft) 75%),
    var(--field);
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
  background-size: 20px 20px;
}

.image-button img,
.card img {
  width: 100%;
  aspect-ratio: 4 / 3;
  object-fit: contain;
  display: block;
}

.card-body {
  padding: 13px;
  display: grid;
  gap: 10px;
  font-size: 13px;
}

.download {
  display: inline-flex;
  align-items: center;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 8px 13px;
  color: var(--ink);
  text-decoration: none;
  background: var(--field);
}

.download:hover {
  text-decoration: none;
}

.error {
  color: var(--danger);
  min-height: 1.2em;
  margin: 0;
}

.token-line {
  color: var(--ok);
  word-break: break-all;
}

.token-strong {
  font-family: "Cascadia Mono", "Consolas", monospace;
  word-break: break-all;
}

.admin-grid {
  display: grid;
  grid-template-columns: minmax(320px, 0.78fr) minmax(420px, 1.22fr);
  gap: 22px;
  margin-bottom: 22px;
}

.admin-grid form,
.admin-grid .panel {
  display: grid;
  gap: 14px;
}

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

.form-grid.compact {
  gap: 12px;
}

.table-wrap {
  overflow: auto;
}

.user-card-list {
  display: grid;
  gap: 14px;
}

.user-card {
  display: grid;
  gap: 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 16px;
  box-shadow: none;
}

.user-card-head,
.user-token-box {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
}

.user-menu {
  position: relative;
  flex: 0 0 auto;
}

.user-menu summary,
.user-submenu summary {
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--field);
  padding: 8px 12px;
  color: var(--ink);
}

.user-menu-panel {
  position: absolute;
  right: 0;
  z-index: 5;
  min-width: 168px;
  display: grid;
  gap: 8px;
  margin-top: 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow-soft);
  padding: 10px;
}

.user-menu-panel button {
  width: 100%;
  justify-content: flex-start;
}

.user-submenu {
  display: grid;
  gap: 10px;
}

.user-submenu[open] summary {
  color: var(--primary-strong);
  border-color: var(--primary);
  background: var(--primary-soft);
}

.user-title {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1 1 360px;
  min-width: 0;
}

.user-title input {
  max-width: 360px;
  font-size: 18px;
  font-weight: 800;
}

.quota-badge {
  flex: 0 0 auto;
  border: 1px solid color-mix(in srgb, var(--primary) 35%, var(--line));
  border-radius: 999px;
  background: var(--primary-soft);
  color: var(--primary-strong);
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 800;
}

.user-token-box {
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--surface-soft);
  padding: 13px;
}

.user-token-box > div {
  min-width: 0;
}

.user-token-box .token-strong {
  margin-top: 6px;
}

.user-card-grid {
  display: grid;
  grid-template-columns: minmax(170px, 0.8fr) repeat(2, minmax(180px, 1fr)) repeat(2, minmax(140px, 0.7fr));
  gap: 12px;
  align-items: stretch;
}

.user-switches,
.user-stat-box {
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--field);
  padding: 12px;
}

.user-switches {
  display: grid;
  gap: 12px;
  align-content: center;
}

.user-stat-box {
  display: grid;
  gap: 6px;
}

.image-stat-button {
  min-height: auto;
  text-align: left;
  cursor: pointer;
}

.user-stat-box span,
.user-stat-box small {
  color: var(--muted);
}

.user-stat-box strong {
  font-size: 24px;
  line-height: 1;
}

.user-models {
  display: grid;
  gap: 9px;
}

.user-images-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 14px;
  margin-bottom: 16px;
}

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

table {
  width: 100%;
  min-width: 980px;
  border-collapse: separate;
  border-spacing: 0;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 10px;
  text-align: left;
  vertical-align: top;
  background: var(--surface);
}

th {
  position: sticky;
  top: 0;
  z-index: 1;
  background: var(--surface-soft);
  color: var(--muted);
  font-size: 12px;
}

td input,
td select {
  min-width: 110px;
  padding: 8px;
}

.check-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.compact-checks {
  max-width: 100%;
}

.check {
  display: flex;
  align-items: center;
  gap: 8px;
}

.check input {
  width: auto;
}

.pill {
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  background: var(--field);
  padding: 7px 10px;
}

.inline-check {
  align-content: end;
  min-height: 100%;
}

.model-list {
  display: grid;
  gap: 18px;
}

.model-card {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  padding: 16px;
  display: grid;
  gap: 14px;
}

.model-head {
  display: flex;
  justify-content: space-between;
  gap: 14px;
  border-bottom: 1px solid var(--line);
  padding-bottom: 12px;
}

.model-head strong {
  display: block;
  font-size: 20px;
}

.model-head span {
  color: var(--muted);
}

.span-2 {
  grid-column: span 2;
}

.image-modal {
  width: min(96vw, 1120px);
  height: min(92vh, 860px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--ink);
  padding: 16px;
  box-shadow: var(--shadow);
}

.image-modal::backdrop {
  background: rgba(17, 19, 21, 0.68);
}

.image-modal img {
  width: 100%;
  height: calc(100% - 62px);
  object-fit: contain;
  display: block;
}

.modal-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 38px;
  height: 38px;
  min-height: 38px;
  padding: 0;
  border-radius: var(--radius-sm);
  background: var(--surface);
}

.modal-actions {
  justify-content: center;
  padding-top: 12px;
}

@keyframes riseIn {
  from {
    opacity: 0;
    transform: translateY(8px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes popIn {
  from {
    opacity: 0;
    transform: scale(0.96);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes waiting {
  0%,
  100% {
    filter: saturate(0.92);
  }
  50% {
    filter: saturate(1.15);
  }
}

@keyframes dotPulse {
  0%,
  100% {
    transform: translateY(-50%) scale(0.85);
    opacity: 0.72;
  }
  50% {
    transform: translateY(-50%) scale(1.22);
    opacity: 1;
  }
}

@media (max-width: 1040px) {
  .studio,
  .admin-grid {
    grid-template-columns: 1fr;
  }

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

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

@media (max-width: 720px) {
  .shell {
    width: min(100vw - 20px, 1280px);
    padding: 12px 0 48px;
  }

  .topbar,
  .brand,
  .model-head,
  .user-card-head,
  .user-token-box,
  .user-title {
    align-items: flex-start;
    flex-direction: column;
  }

  .top-actions,
  .brand-actions,
  .user-card-head .row-actions {
    width: 100%;
  }

  .top-actions > *,
  .brand-actions > *,
  .user-card-head .row-actions > * {
    flex: 1 1 auto;
    justify-content: center;
  }

  .user-title input {
    max-width: none;
  }

  .panel {
    padding: 18px;
  }

  .form-grid,
  .form-grid.compact,
  .compact-row,
  .user-card-grid {
    grid-template-columns: 1fr;
  }

  .span-2 {
    grid-column: auto;
  }

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

  .thinking-steps {
    grid-template-columns: 1fr;
  }

  .job-status > div:first-child {
    display: grid;
  }

  .primary-action {
    position: sticky;
    bottom: 10px;
    z-index: 2;
  }
}

@media (max-width: 480px) {
  .brand span,
  .topbar strong {
    font-size: 27px;
  }

  .panel {
    border-radius: var(--radius);
    padding: 16px;
  }

  .choice-pill {
    flex: 1 1 calc(50% - 9px);
  }

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

  .modal-actions {
    align-items: stretch;
    flex-direction: column;
  }
}

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