:root {
  color-scheme: light;
  --bg: #f4f6f8;
  --panel: #ffffff;
  --line: #d9dee6;
  --text: #1d2733;
  --muted: #647283;
  --accent: #126a5d;
  --accent-strong: #0d594f;
  --danger: #b42318;
  --focus: #2a7abf;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Microsoft YaHei", "Segoe UI", Arial, sans-serif;
  font-size: 14px;
}

.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-panel {
  width: min(380px, 100%);
  display: grid;
  gap: 14px;
  padding: 24px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 14px 32px rgba(18, 30, 45, 0.08);
}

.login-panel h1 {
  margin: 0 0 4px;
  font-size: 22px;
}

.app {
  height: 100vh;
  display: grid;
  grid-template-rows: 58px 1fr;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}

.search {
  width: min(480px, 45vw);
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 12px;
  font-size: 14px;
}

.top-actions,
.dialog-actions,
.media-title,
.media-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.top-actions {
  flex-wrap: wrap;
  justify-content: flex-end;
}

.user-badge {
  min-height: 28px;
  display: inline-flex;
  align-items: center;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--muted);
  background: #fbfcfd;
  white-space: nowrap;
}

.btn,
.icon-btn {
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  padding: 0 14px;
  cursor: pointer;
}

.btn:hover,
.icon-btn:hover {
  border-color: #a8b2bf;
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn.primary:hover {
  background: var(--accent-strong);
}

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

.main-grid {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(430px, 34%) 1fr;
}

.order-list {
  min-height: 0;
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: grid;
  grid-template-rows: 38px 1fr;
}

.list-head,
.order-row {
  display: grid;
  grid-template-columns: 1.15fr 0.72fr 0.72fr 0.95fr 72px;
  gap: 8px;
  align-items: center;
}

.is-admin .list-head,
.is-admin .order-row {
  grid-template-columns: 1.05fr 0.62fr 0.62fr 0.82fr 0.78fr 72px;
}

.list-head {
  padding: 0 12px;
  color: var(--muted);
  border-bottom: 1px solid var(--line);
  font-size: 13px;
}

.rows {
  min-height: 0;
  overflow: auto;
}

.order-row {
  width: 100%;
  min-height: 42px;
  border: 0;
  border-bottom: 1px solid #edf0f4;
  background: transparent;
  text-align: left;
  padding: 6px 12px;
  cursor: pointer;
  font: inherit;
}

.order-row span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.order-row.active {
  background: #e8f2f0;
  box-shadow: inset 3px 0 0 var(--accent);
}

.order-row.generation-running {
  background: #fff8e6;
}

.order-row.generation-done {
  background: #edf8f0;
}

.order-row.generation-error {
  background: #fff0ef;
}

.order-row.active.generation-running,
.order-row.active.generation-done,
.order-row.active.generation-error {
  box-shadow: inset 3px 0 0 var(--accent);
}

.generation-status {
  justify-self: start;
  max-width: 72px;
  border-radius: 999px;
  padding: 3px 7px;
  font-size: 12px;
  line-height: 1.2;
}

.generation-running .generation-status {
  background: #f7c948;
  color: #4d3500;
}

.generation-done .generation-status {
  background: #bfe8ca;
  color: #164923;
}

.generation-error .generation-status {
  background: #f3b7b1;
  color: #7b1f17;
}

.workspace {
  position: relative;
  min-height: 0;
  overflow: auto;
  padding: 18px 18px 88px;
}

.form {
  display: grid;
  grid-template-columns: repeat(4, minmax(140px, 1fr));
  gap: 12px;
  align-items: end;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--text);
  font: inherit;
}

input,
select {
  height: 34px;
  padding: 0 10px;
}

textarea {
  min-height: 210px;
  resize: vertical;
  padding: 10px;
  line-height: 1.55;
}

.temp-textarea {
  min-height: 96px;
}

input:focus,
select:focus,
textarea:focus,
.paste-box:focus {
  outline: 2px solid color-mix(in srgb, var(--focus), transparent 72%);
  border-color: var(--focus);
}

.text-field {
  grid-column: 1 / span 3;
}

.text-field:has(#generateText) {
  grid-column: 1 / span 3;
  grid-row: 2;
}

.text-field:has(#tempText) {
  grid-column: 1 / span 3;
  grid-row: 4;
}

.generate-actions {
  grid-column: 1 / span 3;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.main-generate-actions {
  grid-row: 3;
}

.temp-generate-actions {
  grid-row: 5;
}

.template-tools {
  grid-column: 4;
  grid-row: 2;
  align-self: start;
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  padding-top: 22px;
}

.template-tools .btn {
  width: 100%;
}

label:has(#size) {
  grid-column: 1;
  grid-row: 6;
}

.media-area {
  margin-top: 16px;
}

.media-title {
  justify-content: space-between;
  margin: 18px 0 8px;
}

.media-actions {
  flex-wrap: wrap;
  justify-content: flex-end;
}

.media-title h2 {
  margin: 0;
  font-size: 16px;
}

.size-copy-input {
  width: 86px;
  height: 34px;
  text-align: center;
  padding: 0 8px;
}

.gallery {
  min-height: 132px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(128px, 1fr));
  gap: 12px;
}

.gallery.empty {
  border: 1px dashed #b7c0cc;
  border-radius: 8px;
  background: #fbfcfd;
}

.thumb {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  color: inherit;
  text-decoration: none;
}

.thumb.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(18, 106, 93, 0.18);
}

.thumb.ai-picked {
  border-color: #2f6df6;
  box-shadow: 0 0 0 2px rgba(47, 109, 246, 0.28);
}

.pick-order,
.pick-check {
  position: absolute;
  top: 6px;
  z-index: 1;
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border-radius: 3px;
  font-size: 13px;
  font-weight: 700;
}

.pick-order {
  left: 6px;
  background: #fff;
  color: #2f6df6;
  border: 1px solid #2f6df6;
}

.pick-order:empty {
  display: none;
}

.pick-check {
  right: 6px;
  background: #2f6df6;
  color: #fff;
}

.pick-check:empty {
  display: block;
  background: #fff;
  border: 1px solid #c6ced8;
}

.thumb img {
  display: block;
  width: 100%;
  aspect-ratio: 2 / 3;
  object-fit: contain;
  background: #f8fafb;
}

.thumb div {
  padding: 7px 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
  font-size: 12px;
}

.generate-btn {
  position: sticky;
  left: 100%;
  bottom: 18px;
  display: block;
  height: 54px;
  min-width: 150px;
  margin: 18px 0 0 auto;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 10px 24px rgba(18, 106, 93, 0.24);
}

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

.paste-dialog,
.manage-dialog,
.user-dialog {
  width: min(620px, calc(100vw - 32px));
  border: 0;
  border-radius: 8px;
  padding: 0;
}

.manage-dialog {
  width: min(880px, calc(100vw - 32px));
}

.user-dialog {
  width: min(960px, calc(100vw - 32px));
}

.paste-dialog::backdrop,
.manage-dialog::backdrop,
.user-dialog::backdrop {
  background: rgba(20, 28, 38, 0.42);
}

.paste-panel,
.manage-panel,
.user-panel {
  display: grid;
  gap: 12px;
  padding: 16px;
}

.dialog-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-head h2 {
  margin: 0;
  font-size: 18px;
}

.icon-btn {
  width: 34px;
  padding: 0;
}

.paste-box {
  min-height: 180px;
  border: 1px dashed #aab4c0;
  border-radius: 8px;
  display: grid;
  place-items: center;
  color: var(--muted);
  background: #fbfcfd;
}

.paste-preview {
  max-width: calc(100% - 16px);
  max-height: 320px;
  object-fit: contain;
}

.dialog-actions {
  justify-content: flex-end;
}

.dialog-spacer {
  flex: 1;
}

.manage-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}

.manage-block,
.template-manager {
  display: grid;
  gap: 8px;
}

.manage-block h3,
.template-manager h3 {
  margin: 0;
  font-size: 14px;
}

.manage-block textarea {
  min-height: 112px;
}

.size-editor {
  display: grid;
  gap: 6px;
}

.size-editor-head,
.size-editor-row {
  display: grid;
  grid-template-columns: minmax(90px, 1.2fr) minmax(70px, 0.8fr) minmax(70px, 0.8fr) auto;
  gap: 6px;
  align-items: center;
}

.size-editor-head {
  color: var(--muted);
  font-size: 12px;
}

.size-editor-rows {
  display: grid;
  gap: 6px;
  max-height: 180px;
  overflow: auto;
}

.size-editor-row input {
  min-width: 0;
}

.size-editor-row .btn {
  padding: 7px 10px;
}

.template-list-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.template-edit-grid {
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: 12px;
}

#manageTemplateSelect {
  height: 252px;
  padding: 6px;
}

.template-editor {
  display: grid;
  gap: 10px;
}

#manageTemplateText {
  min-height: 196px;
}

.user-editor,
.user-balance-tools {
  display: grid;
  grid-template-columns: repeat(6, minmax(100px, 1fr));
  gap: 10px;
  align-items: end;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 34px;
}

.checkbox-label input {
  width: auto;
  height: auto;
}

.user-editor-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.user-balance-tools {
  grid-template-columns: minmax(120px, 180px) 1fr auto;
  padding-top: 8px;
  border-top: 1px solid var(--line);
}

.user-rows {
  max-height: 360px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.user-row {
  width: 100%;
  min-height: 38px;
  display: grid;
  grid-template-columns: 1fr 1fr 90px 100px 80px;
  gap: 8px;
  align-items: center;
  border: 0;
  border-bottom: 1px solid #edf0f4;
  background: #fff;
  padding: 7px 10px;
  text-align: left;
  font: inherit;
  cursor: pointer;
}

.user-row:last-child {
  border-bottom: 0;
}

.user-row.active {
  background: #e8f2f0;
  box-shadow: inset 3px 0 0 var(--accent);
}

.user-row span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  max-width: min(620px, calc(100vw - 32px));
  padding: 10px 14px;
  border-radius: 8px;
  background: #18212d;
  color: #fff;
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.2);
}

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

  .order-list {
    min-height: 260px;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

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

  .text-field {
    grid-column: 1 / -1;
  }

  .text-field:has(#generateText),
  .text-field:has(#tempText),
  .template-tools,
  .generate-actions,
  label:has(#size) {
    grid-column: 1 / -1;
    grid-row: auto;
  }
}

@media (max-width: 640px) {
  .topbar {
    align-items: stretch;
    flex-direction: column;
    height: auto;
  }

  .app {
    height: auto;
    min-height: 100vh;
    grid-template-rows: auto 1fr;
  }

  .search {
    width: 100%;
  }

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

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

  .manage-grid,
  .template-edit-grid,
  .user-editor,
  .user-balance-tools,
  .user-row {
    grid-template-columns: 1fr;
  }
}
