:root {
  --bg: #f7f7f4;
  --panel: #ffffff;
  --ink: #1d2329;
  --muted: #66717d;
  --line: #d9ded8;
  --accent: #176b5b;
  --accent-dark: #0d4d42;
  --hot: #b6ff3b;
  --violet: #7c3aed;
  --warn: #9f5b16;
  --error: #a53737;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  letter-spacing: 0;
}

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

button,
.primary-link,
.secondary-link {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  color: var(--ink);
  cursor: pointer;
  padding: 8px 12px;
  text-decoration: none;
}

button.primary,
.primary-link {
  background: var(--accent);
  border-color: var(--accent);
  color: white;
}

button.primary:hover,
.primary-link:hover {
  background: var(--accent-dark);
}

button.is-active {
  border-color: var(--accent);
  color: var(--accent-dark);
}

button:disabled,
input:disabled,
select:disabled,
textarea:disabled {
  cursor: not-allowed;
  opacity: 0.58;
}

.busy-label {
  display: none;
}

#generateSubmitBtn.is-submitting .busy-label {
  display: inline;
}

#generateSubmitBtn.is-submitting .idle-label {
  display: none;
}

.auth-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background:
    radial-gradient(circle at 25% 20%, rgba(182, 255, 59, 0.18), transparent 28%),
    radial-gradient(circle at 80% 10%, rgba(124, 58, 237, 0.18), transparent 30%),
    #070807;
  color: #f7ffe8;
}

.auth-shell {
  width: min(560px, calc(100vw - 32px));
  background: rgba(12, 14, 12, 0.88);
  border: 1px solid rgba(182, 255, 59, 0.28);
  border-radius: 8px;
  padding: 34px;
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.48);
}

.auth-shell h1 {
  font-size: clamp(36px, 7vw, 72px);
  line-height: 0.94;
  margin-top: 0;
  margin-bottom: 18px;
}

.auth-shell p {
  color: #aeb7aa;
  max-width: 460px;
}

.fal-mark {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  margin-bottom: 26px;
  color: var(--hot);
  font-weight: 800;
  letter-spacing: 0;
}

.fal-mark span {
  color: white;
}

.auth-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-top: 28px;
}

.auth-grid span {
  aspect-ratio: 1;
  border-radius: 6px;
  background:
    linear-gradient(135deg, rgba(182, 255, 59, 0.24), rgba(124, 58, 237, 0.16)),
    #11140f;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.topbar {
  min-height: 72px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  position: sticky;
  top: 0;
  z-index: 2;
}

.topbar h1 {
  font-size: 20px;
  line-height: 1.2;
  margin: 0;
}

.brand-zone {
  display: grid;
  grid-template-columns: auto auto;
  align-items: center;
  gap: 6px 14px;
}

.brand-zone #userMeta {
  grid-column: 1 / -1;
}

.main-tabs {
  display: inline-flex;
  gap: 6px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f1f4ef;
}

.main-tabs button {
  min-width: 72px;
  padding: 6px 10px;
}

.main-tabs button.is-active {
  background: var(--ink);
  border-color: var(--ink);
  color: white;
}

.library-tabs {
  display: inline-flex;
  gap: 6px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f1f4ef;
}

.library-tabs button {
  padding: 6px 10px;
}

.library-tabs button.is-active {
  background: var(--ink);
  border-color: var(--ink);
  color: white;
}

.topbar span,
.muted,
.status-line {
  color: var(--muted);
  font-size: 13px;
}

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

.layout {
  display: grid;
  grid-template-columns: 360px minmax(360px, 1fr) 420px;
  gap: 16px;
  padding: 16px;
  align-items: start;
}

.postprocess-page {
  display: grid;
  grid-template-columns: 280px minmax(420px, 1fr) 340px;
  gap: 16px;
  padding: 16px;
  align-items: start;
}

.postprocess-page[hidden] {
  display: none;
}

body[data-page="generate"] #postprocessPage,
body[data-page="postprocess"] #generatePage {
  display: none !important;
}

.postprocess-sidebar,
.postprocess-work,
.postprocess-controls {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
}

.postprocess-sidebar {
  display: grid;
  gap: 10px;
}

.post-tool {
  text-align: left;
}

.post-tool.is-active {
  background: #eff7f2;
}

.postprocess-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.post-canvas-wrap,
.sprite-preview-wrap {
  background:
    linear-gradient(45deg, #d8ded8 25%, transparent 25%),
    linear-gradient(-45deg, #d8ded8 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #d8ded8 75%),
    linear-gradient(-45deg, transparent 75%, #d8ded8 75%);
  background-color: #f6f7f5;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0;
  background-size: 20px 20px;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: auto;
  min-height: 360px;
  display: grid;
  place-items: center;
}

#postCanvas {
  max-width: 100%;
  height: auto;
  cursor: crosshair;
  touch-action: none;
}

.postprocess-controls {
  display: grid;
  gap: 14px;
}

.sprite-preview-wrap {
  min-height: 180px;
  margin-top: 12px;
}

.sprite-detect-summary {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  line-height: 1.45;
}

#spritePreviewCanvas {
  max-width: 100%;
  height: auto;
  image-rendering: pixelated;
}

.tool-panel,
.library-panel,
.detail-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
}

h2 {
  font-size: 16px;
  margin: 0 0 12px;
}

h3 {
  font-size: 14px;
  margin: 16px 0 8px;
}

form {
  display: grid;
  gap: 12px;
}

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

textarea,
input,
select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: white;
  color: var(--ink);
  padding: 8px 10px;
}

textarea {
  resize: vertical;
}

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

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

.row > * {
  min-width: 0;
}

.library-head {
  display: grid;
  grid-template-columns: auto auto minmax(220px, 1fr);
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.library-head input {
  max-width: 320px;
}

.library-head .row {
  justify-content: flex-end;
}

.library-head select {
  max-width: 180px;
}

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

.generation-queue {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  margin-bottom: 12px;
  background: #fbfbf8;
}

.generation-queue h3 {
  margin-top: 0;
}

#generationQueueList {
  display: grid;
  gap: 8px;
}

.generation-job {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 8px;
  align-items: center;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: white;
}

.generation-job.running {
  border-color: var(--accent);
}

.generation-job.completed {
  background: #f2f6ef;
}

.generation-job.failed {
  border-color: var(--error);
}

.generation-job p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.3;
}

.generation-job span {
  border-radius: 999px;
  background: #e8f0e5;
  color: #4f5d53;
  font-size: 11px;
  padding: 3px 7px;
}

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

.card-open {
  display: block;
  width: 100%;
  padding: 0;
  border: 0;
  background: transparent;
  text-align: left;
}

.card-actions {
  padding: 0 10px 10px;
}

.card-actions button {
  width: 100%;
  font-size: 12px;
  padding: 7px 9px;
}

.thumb {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  display: block;
  background: #e8ebe6;
}

.card-body {
  padding: 10px;
}

.card-body strong {
  display: block;
  font-size: 12px;
  margin-bottom: 4px;
}

.card-body p {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.mini-chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 8px;
}

.mini-chip,
.group-chip small,
.quality-pill {
  border-radius: 999px;
  background: #e8f0e5;
  color: #4f5d53;
  font-size: 11px;
  padding: 2px 6px;
}

.quality-pill {
  display: inline-block;
  margin-bottom: 6px;
  background: #10130e;
  color: var(--hot);
}

.reference-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(92px, 1fr));
  gap: 8px;
  max-height: 260px;
  overflow: auto;
}

.ref-option {
  position: relative;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px;
  background: #fbfbf8;
}

.ref-option.is-selected {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}

.ref-option strong {
  position: absolute;
  top: 6px;
  right: 6px;
  display: grid;
  place-items: center;
  min-width: 22px;
  height: 22px;
  border-radius: 999px;
  background: var(--accent);
  color: white;
  font-size: 12px;
}

.ref-option img {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  display: block;
  border-radius: 4px;
  margin-bottom: 6px;
}

.selected-reference-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  margin: 10px 0;
  background: #fbfbf8;
}

.selected-reference-panel h3 {
  margin-top: 0;
}

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

.selected-ref-item {
  display: grid;
  grid-template-columns: auto 52px minmax(0, 1fr) auto auto auto;
  gap: 8px;
  align-items: center;
  padding: 7px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: white;
}

.selected-ref-item img {
  width: 52px;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  border-radius: 5px;
}

.selected-ref-item p {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.3;
}

.selected-ref-item button {
  padding: 6px 8px;
}

.ref-order {
  display: grid;
  place-items: center;
  min-width: 24px;
  height: 24px;
  border-radius: 999px;
  background: var(--ink);
  color: white;
  font-size: 12px;
  font-weight: 700;
}

.empty-selected-ref {
  color: var(--muted);
  font-size: 13px;
}

.drop-zone {
  display: grid;
  place-items: center;
  min-height: 112px;
  border: 1px dashed #95a39a;
  border-radius: 8px;
  background: #f2f6ef;
  color: #506057;
  text-align: center;
  padding: 14px;
}

.drop-zone.dragging {
  border-color: var(--accent);
  background: #e8f4ed;
}

.group-management {
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  padding: 12px 0;
  margin-bottom: 12px;
}

.section-head {
  display: grid;
  grid-template-columns: minmax(120px, 180px) 1fr;
  gap: 12px;
  align-items: start;
  margin-bottom: 10px;
}

.section-head h2 {
  margin: 8px 0 0;
}

.group-create-form {
  display: grid;
  grid-template-columns: minmax(120px, 1fr) minmax(160px, 1.4fr) 48px auto;
  gap: 8px;
}

.group-list {
  display: grid;
  gap: 8px;
  max-height: 220px;
  overflow: auto;
}

.group-chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
}

.group-chip span {
  width: 10px;
  height: 10px;
  border-radius: 999px;
}

.group-row {
  display: grid;
  grid-template-columns: 12px minmax(120px, 1fr) minmax(140px, 1.4fr) 48px auto auto auto;
  gap: 8px;
  align-items: center;
}

.group-row input,
.group-create-form input {
  min-width: 0;
}

.group-color-dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
}

.group-count {
  border-radius: 999px;
  background: #e8f0e5;
  color: #4f5d53;
  font-size: 11px;
  padding: 3px 7px;
  text-align: center;
}

.group-editor {
  display: grid;
  gap: 8px;
  margin: 12px 0;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfbf8;
}

.detail-image {
  width: 100%;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #e8ebe6;
}

.image-open-button {
  display: block;
  width: 100%;
  border: 0;
  padding: 0;
  background: transparent;
}

.kv {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}

.kv div {
  border-top: 1px solid var(--line);
  padding-top: 8px;
}

.kv strong {
  display: block;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 4px;
}

pre {
  white-space: pre-wrap;
  word-break: break-word;
  background: #f2f4f0;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px;
  max-height: 280px;
  overflow: auto;
}

dialog {
  width: min(860px, calc(100vw - 32px));
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px;
}

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

.image-dialog-body {
  display: grid;
  place-items: center;
  max-height: 72vh;
  overflow: auto;
  background: #111;
  border-radius: 8px;
}

.image-dialog-body img {
  display: block;
  max-width: 100%;
  max-height: 72vh;
  object-fit: contain;
}

.dialog-actions,
.annotator-tools {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-top: 12px;
}

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

.canvas-wrap {
  max-height: 72vh;
  overflow: auto;
  background: #20231f;
  border-radius: 8px;
  margin-top: 12px;
  padding: 10px;
}

#annotatorCanvas {
  display: block;
  max-width: 100%;
  height: auto;
  margin: 0 auto;
  background: white;
  cursor: crosshair;
  touch-action: none;
}

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

.user-row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 10px;
  align-items: center;
  border-top: 1px solid var(--line);
  padding: 10px 0;
}

.badge {
  display: inline-block;
  padding: 3px 7px;
  border-radius: 999px;
  background: #eef2ed;
  font-size: 12px;
}

.badge.failed {
  color: var(--error);
}

@media (max-width: 1100px) {
  .layout,
  .postprocess-page {
    grid-template-columns: 1fr;
  }

  .library-head {
    grid-template-columns: 1fr;
  }

  .library-head .row {
    justify-content: stretch;
  }

  .section-head,
  .group-create-form,
  .group-row,
  .generation-job,
  .selected-ref-item {
    grid-template-columns: 1fr;
  }

  .group-color-dot,
  .group-count,
  .ref-order {
    justify-self: start;
  }
}
