:root {
  --bg: #0f0b16;
  --surface: #171122;
  --surface-2: #1e1730;
  --line: #2c2242;
  --text: #ece8f5;
  --muted: #a297bd;
  --accent: #9d5cff;
  --accent-ink: #ffffff;
  --accent-soft: rgba(157, 92, 255, 0.16);
  --ok: #52d99a;
  --warn: #f5b43f;
  --danger: #ff6b81;
  --radius: 14px;
  --shadow: 0 18px 40px rgba(0, 0, 0, 0.45);
  --check-a: #1b1528;
  --check-b: #241c36;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f1fa;
    --surface: #ffffff;
    --surface-2: #f7f4fd;
    --line: #e2dcf0;
    --text: #1d1630;
    --muted: #6b6288;
    --accent: #7b3fe4;
    --accent-soft: rgba(123, 63, 228, 0.12);
    --ok: #17924f;
    --warn: #a96a00;
    --danger: #c62a45;
    --shadow: 0 14px 32px rgba(40, 20, 80, 0.12);
    --check-a: #ece8f4;
    --check-b: #f8f6fc;
    color-scheme: light;
  }
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 ui-sans-serif, system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.mono {
  font-family: ui-monospace, 'JetBrains Mono', 'SF Mono', Menlo, Consolas, monospace;
  font-variant-numeric: tabular-nums;
}

.muted {
  color: var(--muted);
  margin: 0;
}

.error {
  color: var(--danger);
  margin: 0;
  font-size: 14px;
}

.boot {
  display: grid;
  place-items: center;
  height: 100dvh;
  color: var(--muted);
}

/* ---- brand ---------------------------------------------------------------- */

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 16px;
  letter-spacing: -0.01em;
}

.wordmark {
  font-weight: 400;
  white-space: nowrap;
}

.wordmark b {
  font-weight: 700;
}

/* The mark is an aperture: a pupil inside a ring that slowly breathes. */
.mark {
  position: relative;
  width: 18px;
  height: 18px;
  flex: 0 0 18px;
}

.mark::before,
.mark::after {
  content: '';
  position: absolute;
  border-radius: 50%;
}

.mark::before {
  inset: 6px;
  background: var(--accent);
}

.mark::after {
  inset: 1px;
  border: 2px solid var(--accent);
  animation: breathe 3.2s ease-in-out infinite;
}

@keyframes breathe {
  0%,
  100% {
    transform: scale(1);
    opacity: 0.9;
  }
  50% {
    transform: scale(0.82);
    opacity: 0.55;
  }
}

@media (prefers-reduced-motion: reduce) {
  .mark::after {
    animation: none;
  }
}

.brand-lg {
  font-size: 22px;
}

.brand-lg .mark {
  width: 26px;
  height: 26px;
  flex-basis: 26px;
}

.brand-lg .mark::before {
  inset: 9px;
}

/* ---- login ---------------------------------------------------------------- */

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

.login-card {
  width: min(380px, 100%);
  display: grid;
  gap: 14px;
}

/* ---- shell ---------------------------------------------------------------- */

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

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

.topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.pill {
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 240px;
}

.pill.live {
  color: var(--ok);
  border-color: color-mix(in srgb, var(--ok) 40%, var(--line));
}

.pill.busy {
  color: var(--warn);
  border-color: color-mix(in srgb, var(--warn) 40%, var(--line));
}

.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 400px 1fr;
}

.panel {
  border-right: 1px solid var(--line);
  overflow-y: auto;
  padding: 14px;
  display: grid;
  gap: 14px;
  align-content: start;
}

.gallery-wrap {
  overflow-y: auto;
  padding: 14px;
}

/* ---- cards & controls ------------------------------------------------------ */

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

.card h2 {
  margin: 0 0 12px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.create {
  display: grid;
  gap: 14px;
}

.create h2 {
  margin: 0;
}

.field {
  display: grid;
  gap: 6px;
  margin: 0;
}

.field > span {
  font-size: 13px;
  color: var(--muted);
}

.field > span em {
  font-style: normal;
}

input[type='text'],
input[type='password'],
input[type='number'],
textarea {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
}

textarea {
  resize: vertical;
  min-height: 96px;
}

input:focus-visible,
textarea:focus-visible,
button:focus-visible,
.drop:focus-visible,
.btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

button,
.btn {
  font: inherit;
  border-radius: 10px;
  padding: 9px 14px;
  border: 1px solid var(--line);
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}

.ghost {
  background: transparent;
}

.ghost:hover:not(:disabled) {
  background: var(--surface-2);
}

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

.note {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
}

.note b {
  color: var(--text);
  font-weight: 600;
}

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

.seed-row input[type='number'] {
  width: 160px;
  margin-left: auto;
  padding: 7px 10px;
}

.check {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-size: 14px;
}

.check input {
  accent-color: var(--accent);
  width: 16px;
  height: 16px;
}

/* ---- dial ------------------------------------------------------------------ */

.dial-sep {
  color: var(--accent);
  font-weight: 400;
}

.dial {
  display: grid;
  gap: 4px;
}

.dial input[type='range'] {
  width: 100%;
  margin: 6px 0 0;
  accent-color: var(--accent);
  height: 22px;
  cursor: pointer;
}

.dial-ticks {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  font-size: 12px;
  color: var(--muted);
  text-align: center;
}

.dial-ticks span:first-child {
  text-align: left;
}

.dial-ticks span:last-child {
  text-align: right;
}

.dial-ticks span.on {
  color: var(--accent);
  font-weight: 600;
}

/* ---- segmented ------------------------------------------------------------- */

.segmented {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.segmented button {
  padding: 6px 10px;
  font-size: 13px;
  border-radius: 8px;
}

.segmented button.on {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--text);
}

/* ---- references ------------------------------------------------------------ */

.refs {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
  gap: 8px;
}

.refs:empty {
  display: none;
}

.ref {
  position: relative;
  aspect-ratio: 1;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--line);
}

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

.ref b {
  position: absolute;
  left: 4px;
  top: 4px;
  font-size: 11px;
  padding: 1px 6px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
}

.ref button {
  position: absolute;
  right: 4px;
  top: 4px;
  padding: 0 6px;
  font-size: 12px;
  line-height: 18px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  border: 0;
}

.drop {
  display: block;
  padding: 12px;
  border: 1px dashed var(--line);
  border-radius: 10px;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
  cursor: pointer;
}

.drop.over {
  border-color: var(--accent);
  background: var(--accent-soft);
}

/* ---- gpu card ---------------------------------------------------------------- */

.gpu-card p {
  margin: 0;
}

.gpu-card #gpu-stage {
  font-weight: 600;
  margin-bottom: 10px;
}

.bar {
  height: 6px;
  border-radius: 99px;
  background: var(--surface-2);
  overflow: hidden;
  margin-bottom: 8px;
}

.bar i {
  display: block;
  height: 100%;
  width: 0;
  background: var(--accent);
  border-radius: 99px;
  transition: width 0.6s ease;
}

/* ---- gallery ----------------------------------------------------------------- */

.empty {
  max-width: 460px;
  margin: 15vh auto 0;
  text-align: center;
}

.empty p {
  margin: 0 0 8px;
}

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

.tile {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--line);
  aspect-ratio: 1;
  cursor: pointer;
  padding: 0;
  display: block;
  width: 100%;
}

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

.tile .state {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  gap: 8px;
  padding: 16px;
  text-align: center;
  font-size: 13px;
  color: var(--muted);
}

.tile .state strong {
  color: var(--text);
  font-size: 14px;
}

.tile .state .bar {
  width: 140px;
  margin: 0 auto;
}

.tile.failed .state strong {
  color: var(--danger);
}

.tile .cap {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 18px 10px 8px;
  font-size: 12px;
  color: #fff;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.72));
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  opacity: 0;
  transition: opacity 0.15s;
}

.tile:hover .cap,
.tile:focus-visible .cap {
  opacity: 1;
}

.more {
  display: block;
  margin: 16px auto;
}

.checker {
  background-color: var(--check-a);
  background-image: linear-gradient(45deg, var(--check-b) 25%, transparent 25%, transparent 75%, var(--check-b) 75%),
    linear-gradient(45deg, var(--check-b) 25%, transparent 25%, transparent 75%, var(--check-b) 75%);
  background-size: 20px 20px;
  background-position: 0 0, 10px 10px;
}

/* ---- viewer ------------------------------------------------------------------ */

.viewer {
  width: min(1200px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

.viewer::backdrop {
  background: rgba(5, 3, 10, 0.75);
}

.viewer-body {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  max-height: calc(100dvh - 26px);
}

.viewer-img {
  display: grid;
  place-items: center;
  min-height: 300px;
  overflow: auto;
}

.viewer-img img {
  max-width: 100%;
  max-height: calc(100dvh - 28px);
  display: block;
}

.viewer-side {
  padding: 18px;
  border-left: 1px solid var(--line);
  display: grid;
  gap: 12px;
  align-content: start;
  overflow-y: auto;
}

.viewer-prompt {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  padding-right: 28px;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chips span {
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
}

.viewer-refs {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.viewer-refs:empty {
  display: none;
}

.viewer-refs img {
  width: 56px;
  height: 56px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--line);
}

.viewer-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.viewer-close {
  position: absolute;
  top: 10px;
  right: 10px;
  padding: 4px 10px;
}

/* ---- small screens ------------------------------------------------------------ */

@media (max-width: 860px) {
  .app {
    height: auto;
    min-height: 100dvh;
  }

  .layout {
    display: block;
  }

  .panel {
    border-right: 0;
    overflow: visible;
  }

  .gallery-wrap {
    overflow: visible;
  }

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

  .viewer-body {
    grid-template-columns: 1fr;
  }

  .viewer-side {
    border-left: 0;
    border-top: 1px solid var(--line);
  }

  .hide-sm {
    display: none;
  }

  .topbar {
    padding: 10px 12px;
  }

  .topbar-right {
    gap: 6px;
  }

  .topbar-right button {
    padding: 5px 8px;
    font-size: 13px;
    white-space: nowrap;
  }

  .pill {
    max-width: 130px;
    font-size: 12px;
    padding: 3px 8px;
  }
}
