:root {
  --paper: #f3f1ec;
  --ink: #101010;
  --mute: #5c5a55;
  --line: #cfcac0;
  --cyan: #0aa2b5;
  --clip: #1f6b4a;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: "Bricolage Grotesque", ui-sans-serif, system-ui, sans-serif;
  background: var(--paper);
  color: var(--ink);
  background-image:
    url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='0.04'/%3E%3C/svg%3E");
  overflow-x: hidden;
  overflow-y: auto;
}

button, a.dl, button.dl, input {
  font-family: inherit;
}

/* —— Gate —— */
.gate {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  background: var(--paper);
}

.gate-form {
  width: min(320px, 82vw);
  display: flex;
  flex-direction: row;
  align-items: stretch;
  gap: 8px;
}

.gate-form input {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--ink);
  background: transparent;
  padding: 14px 16px;
  font-size: 1rem;
  color: var(--ink);
  outline: none;
  letter-spacing: 0.14em;
  transition: border-color 220ms ease, color 220ms ease, box-shadow 220ms ease;
}

.gate-form input:focus {
  box-shadow: 3px 3px 0 var(--ink);
}

.gate-form input.is-bad {
  border-color: #c62828;
  color: #c62828;
  box-shadow: 3px 3px 0 #c62828;
  animation: gate-shake 420ms cubic-bezier(0.36, 0.07, 0.19, 0.97) both;
}

.gate-btn {
  appearance: none;
  flex-shrink: 0;
  width: 52px;
  border: 1px solid var(--ink);
  background: var(--ink);
  color: var(--paper);
  padding: 0;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: background 140ms ease, color 140ms ease, transform 100ms ease, box-shadow 140ms ease;
}

.gate-btn:hover {
  background: transparent;
  color: var(--ink);
  box-shadow: 3px 3px 0 var(--ink);
}

.gate-btn:active {
  transform: scale(0.96);
}

.gate-btn:disabled {
  opacity: 0.45;
  cursor: wait;
}

.gate-btn.is-loading {
  pointer-events: none;
  opacity: 0.55;
}

.gate-btn.is-loading .gate-icon {
  animation: gate-spin 700ms linear infinite;
}

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

.gate-icon {
  display: block;
}

@keyframes gate-shake {
  0%, 100% { transform: translateX(0); }
  15% { transform: translateX(-7px); }
  30% { transform: translateX(7px); }
  45% { transform: translateX(-5px); }
  60% { transform: translateX(5px); }
  75% { transform: translateX(-2px); }
  90% { transform: translateX(2px); }
}

/* —— App shell —— */
.shell {
  min-height: 100dvh;
  min-height: 100vh;
  height: 100dvh;
  height: 100vh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  width: min(100%, 1400px);
  margin: 0 auto;
  padding: clamp(12px, 2.2vh, 22px) clamp(14px, 2.5vw, 28px) clamp(8px, 1.4vh, 14px);
  gap: clamp(10px, 1.6vh, 18px);
}

.top {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: clamp(12px, 2vw, 24px);
  border-bottom: 1px solid var(--ink);
  padding-bottom: clamp(8px, 1.2vh, 14px);
  flex-wrap: wrap;
}

.top-actions {
  display: flex;
  align-items: flex-end;
  gap: clamp(10px, 1.6vw, 18px);
  flex-wrap: wrap;
}

.logout-btn {
  appearance: none;
  border: 1px solid var(--ink);
  background: transparent;
  color: var(--ink);
  padding: 7px 12px;
  font-size: clamp(0.75rem, 1.4vw, 0.82rem);
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
  transition: background 140ms ease, color 140ms ease, box-shadow 140ms ease;
}

.logout-btn:hover {
  background: var(--ink);
  color: var(--paper);
  box-shadow: 3px 3px 0 var(--ink);
}

h1 {
  margin: 0;
  font-size: clamp(1.25rem, 3.2vw, 2.15rem);
  font-weight: 700;
  letter-spacing: -0.03em;
  line-height: 1.05;
}

.tabs {
  display: flex;
  gap: 2px;
  flex-wrap: wrap;
}

.tab {
  appearance: none;
  background: transparent;
  border: 0;
  border-bottom: 2px solid transparent;
  padding: 8px 10px 10px;
  font-size: clamp(0.82rem, 1.6vw, 0.95rem);
  font-weight: 600;
  color: var(--mute);
  cursor: pointer;
  transition: color 160ms ease, border-color 160ms ease, transform 120ms ease;
}

.tab:hover { color: var(--ink); }
.tab:active { transform: translateY(1px); }
.tab.is-on {
  color: var(--ink);
  border-bottom-color: var(--ink);
}

main {
  min-height: 0;
  overflow: hidden;
}

.panel {
  display: none;
  height: 100%;
  min-height: 0;
  animation: rise 280ms ease both;
}
.panel.is-on { display: grid; }

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

/* —— Gallery —— */
#panel-gallery {
  grid-template-columns: minmax(180px, 220px) minmax(0, 1fr);
  gap: clamp(14px, 2vw, 22px);
  height: 100%;
}

.filters {
  display: flex;
  flex-direction: column;
  gap: clamp(14px, 2.2vh, 22px);
  padding-top: 2px;
  min-height: 0;
  overflow: auto;
}

.filter-block h2 {
  margin: 0 0 8px;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.7rem;
  font-weight: 500;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--mute);
}

.btn-row, .btn-col {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.btn-col { flex-direction: column; }

.chip {
  appearance: none;
  background: transparent;
  border: 1px solid var(--ink);
  color: var(--ink);
  padding: 8px 12px;
  font-size: clamp(0.8rem, 1.5vw, 0.9rem);
  font-weight: 600;
  cursor: pointer;
  text-align: left;
  transition: background 140ms ease, color 140ms ease, transform 100ms ease;
}

.chip:hover { background: rgba(16, 16, 16, 0.04); }
.chip:active { transform: scale(0.98); }
.chip.is-on {
  background: var(--ink);
  color: var(--paper);
}

.chip .chip-d {
  display: block;
  margin-top: 2px;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.65rem;
  font-weight: 400;
  opacity: 0.7;
}

.stage {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  gap: 10px;
  height: 100%;
}

.stage-frame {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  background: #0a0a0a;
  border: 1px solid var(--ink);
  overflow: hidden;
}

.stage-frame video {
  position: relative;
  z-index: 1;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #0a0a0a;
  display: block;
}

.stage-empty {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: grid;
  place-items: center;
  color: #888;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.8rem;
}

.stage-empty[hidden] {
  display: none !important;
  pointer-events: none;
}

.stage-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border-top: 1px solid var(--ink);
  padding-top: 10px;
  flex-shrink: 0;
}

.stage-copy { min-width: 0; }

.stage-title {
  margin: 0;
  font-size: clamp(0.92rem, 1.8vw, 1.05rem);
  font-weight: 700;
  letter-spacing: -0.02em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stage-sub {
  margin: 3px 0 0;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.7rem;
  color: var(--mute);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

a.dl,
button.dl {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 10px 16px;
  border: 1px solid var(--ink);
  background: var(--ink);
  color: var(--paper);
  text-decoration: none;
  font-weight: 700;
  font-size: 0.85rem;
  letter-spacing: 0.02em;
  cursor: pointer;
  transition: background 140ms ease, color 140ms ease, transform 100ms ease, opacity 140ms ease;
  white-space: nowrap;
  flex-shrink: 0;
}

a.dl:hover,
button.dl:hover:not(:disabled) {
  background: transparent;
  color: var(--ink);
}
a.dl:active,
button.dl:active:not(:disabled) { transform: scale(0.98); }

button.dl:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* —— Pipeline —— */
#panel-built {
  grid-template-rows: auto minmax(0, 1fr);
  gap: 10px;
  height: 100%;
}

.built-body {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(0, 1fr);
  gap: 10px;
  min-height: 0;
  overflow: hidden;
}

.built-head h2,
.vault-head h2 {
  margin: 0 0 4px;
  font-size: clamp(1.05rem, 2.2vw, 1.25rem);
  letter-spacing: -0.02em;
}

.built-head p,
.vault-head p {
  margin: 0;
  color: var(--mute);
  font-size: clamp(0.82rem, 1.6vw, 0.92rem);
}

.pipeline-wrap {
  min-height: 0;
  min-width: 0;
  height: 100%;
  overflow: auto;
  border: 1px solid var(--ink);
  background: #faf9f6;
  padding: clamp(10px, 1.6vh, 16px);
}

.pipeline {
  position: relative;
  width: 100%;
  height: 100%;
  min-width: 860px;
  min-height: 400px;
  box-sizing: border-box;
  transform-origin: top left;
}

.node-wide {
  width: clamp(120px, 18vw, 168px);
}

.node-hub {
  background: #f5f4ef;
  border-style: dashed;
}

.node-model {
  width: clamp(120px, 16vw, 160px);
  border-width: 2px;
}

/* Align Video generation + clips on the same vertical center */
.node-stack {
  left: var(--x);
  transform: translateX(-50%);
}

.node-stack.is-on {
  transform: translateX(-50%);
}

.node-sm {
  width: clamp(68px, 8vw, 88px);
  padding: 5px 6px;
}

.node-sm .node-k {
  font-size: 0.55rem;
  margin-bottom: 1px;
}

.node-sm .node-t {
  font-size: clamp(0.68rem, 1.1vw, 0.78rem);
}

.node-sm .node-d {
  display: none;
}

.node-clip {
  background: #e8f4ee;
  border-color: var(--clip);
  box-shadow: inset 3px 0 0 var(--clip);
  color: var(--ink);
}

.node-clip .node-k {
  color: var(--clip);
  opacity: 1;
}

.node-indicator {
  cursor: default;
  pointer-events: none;
}

.pipes {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.pipe {
  fill: none;
  stroke: var(--line);
  stroke-width: 1.5;
  stroke-dasharray: 4 5;
  transition: stroke 180ms ease;
}

.pipeline.is-lit .pipe { stroke: #b7b3a8; }

.node {
  position: absolute;
  left: var(--x);
  top: var(--y);
  width: clamp(108px, 14vw, 148px);
  appearance: none;
  text-align: left;
  background: var(--paper);
  border: 1px solid var(--ink);
  padding: clamp(8px, 1.2vh, 11px);
  cursor: pointer;
  transition: box-shadow 140ms ease, background 140ms ease;
  z-index: 1;
}

.node.is-on {
  box-shadow: 3px 3px 0 var(--ink);
  z-index: 2;
}

.node-cyan {
  border-color: var(--cyan);
  box-shadow: inset 3px 0 0 var(--cyan);
}

.node-cyan.is-on {
  background: #e7f7f9;
  box-shadow: 3px 3px 0 var(--cyan);
}

.node-result {
  background: var(--ink);
  color: var(--paper);
}

.node-result .node-d { color: #bdbdbd; }

.node-k {
  display: block;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.62rem;
  letter-spacing: 0.08em;
  margin-bottom: 3px;
  opacity: 0.65;
}

.node-t {
  display: block;
  font-weight: 700;
  font-size: clamp(0.78rem, 1.4vw, 0.95rem);
  letter-spacing: -0.02em;
}

.node-d {
  display: block;
  margin-top: 2px;
  font-size: clamp(0.65rem, 1.1vw, 0.75rem);
  color: var(--mute);
}

.node-detail {
  border: 1px solid var(--ink);
  background: #faf9f6;
  padding: clamp(12px, 1.8vh, 18px) clamp(14px, 2vw, 20px);
  min-height: 0;
  height: 100%;
  overflow: auto;
}

.node-detail-inner {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 100%;
}

.node-detail-copy {
  flex: 1 1 auto;
  min-width: 0;
}

.node-detail-media {
  border: 1px solid var(--ink);
  background: transparent;
  overflow: hidden;
  width: 100%;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
}

.node-detail-media[hidden],
.node-detail:not(.has-preview) .node-detail-media {
  display: none !important;
}

.node-detail-media.is-landscape {
  background: #0a0a0a;
}

.node-detail-media.is-empty::after {
  content: none;
}

.node-detail-media img {
  display: block;
  width: 100%;
  height: auto;
  max-height: min(42vh, 320px);
  object-fit: contain;
  background: transparent;
}

.detail-gallery {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 8px;
  background: var(--paper);
  border-top: 1px solid var(--ink);
}

.detail-thumb {
  appearance: none;
  border: 1px solid var(--ink);
  background: transparent;
  color: var(--ink);
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.62rem;
  padding: 4px 7px;
  cursor: pointer;
  transition: background 120ms ease, color 120ms ease;
}

.detail-thumb:hover {
  background: rgba(16, 16, 16, 0.06);
}

.detail-thumb.is-on {
  background: var(--ink);
  color: var(--paper);
}

.detail-caption {
  margin: 0;
  padding: 6px 8px 8px;
  background: var(--paper);
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.62rem;
  color: var(--mute);
  border-top: 1px solid var(--line);
}

.node-detail-head {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-bottom: 10px;
}

.node-detail-k {
  flex-shrink: 0;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.68rem;
  letter-spacing: 0.08em;
  padding: 4px 7px;
  border: 1px solid var(--ink);
  background: var(--paper);
}

.node-detail-t {
  margin: 0;
  font-size: clamp(0.95rem, 1.8vw, 1.08rem);
  letter-spacing: -0.02em;
}

.node-detail-tag {
  margin: 3px 0 0;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.68rem;
  color: var(--mute);
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.node-detail-body {
  margin: 0 0 10px;
  font-size: clamp(0.84rem, 1.5vw, 0.92rem);
  line-height: 1.5;
  color: var(--ink);
  max-width: 72ch;
}

.node-detail-list {
  margin: 0;
  padding: 0 0 0 1.1em;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.72rem;
  color: var(--mute);
  line-height: 1.55;
}

.node-detail-list[hidden] {
  display: none !important;
}

.node-detail-list li + li {
  margin-top: 4px;
}

.detail-boxes {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 4px;
}

.detail-boxes[hidden] {
  display: none !important;
}

.detail-box {
  border: 1px solid var(--ink);
  background: var(--paper);
  padding: 10px 11px;
  min-height: 0;
}

.detail-box h4 {
  margin: 0 0 6px;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.68rem;
  font-weight: 500;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--mute);
}

.detail-box p {
  margin: 0;
  font-size: clamp(0.78rem, 1.3vw, 0.86rem);
  line-height: 1.45;
  color: var(--ink);
}

.detail-cta {
  appearance: none;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  width: 100%;
  margin-top: 14px;
  padding: 16px 18px;
  border: 1px solid var(--ink);
  background: var(--ink);
  color: var(--paper);
  cursor: pointer;
  text-align: left;
  transition: background 140ms ease, color 140ms ease, box-shadow 140ms ease, transform 100ms ease;
}

.detail-cta[hidden] {
  display: none !important;
}

.detail-cta:hover {
  background: transparent;
  color: var(--ink);
  box-shadow: 3px 3px 0 var(--ink);
}

.detail-cta:active {
  transform: scale(0.99);
}

.detail-cta-label {
  font-size: clamp(0.95rem, 1.8vw, 1.1rem);
  font-weight: 700;
  letter-spacing: -0.02em;
}

.detail-cta-sub {
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.72rem;
  opacity: 0.75;
}

.node-detail.is-cyan .node-detail-k {
  border-color: var(--cyan);
  color: var(--cyan);
}

.node-detail.is-out .node-detail-k {
  background: var(--ink);
  color: var(--paper);
  border-color: var(--ink);
}

.node-detail.is-hub .node-detail-k {
  border-style: dashed;
}

.node-detail.is-model .node-detail-k {
  border-width: 2px;
  font-weight: 600;
}

.node-detail.is-clip .node-detail-k {
  border-color: var(--clip);
  color: var(--clip);
  background: #e8f4ee;
}

/* —— Vault —— */
#panel-vault {
  grid-template-rows: auto minmax(0, 1fr);
  gap: 12px;
  height: 100%;
  overflow: hidden;
}

.vault-body {
  min-height: 0;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.vault-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(220px, 100%), 1fr));
  gap: 10px;
}

.vault-card {
  appearance: none;
  background: transparent;
  border: 1px solid var(--ink);
  padding: 12px;
  text-align: left;
  cursor: pointer;
  transition: background 140ms ease, transform 100ms ease, box-shadow 140ms ease;
}

.vault-card:hover {
  background: rgba(16, 16, 16, 0.03);
  box-shadow: 3px 3px 0 var(--ink);
}

.vault-card.is-on {
  background: var(--ink);
  color: var(--paper);
}

.vault-card.is-on .vault-note,
.vault-card.is-on .vault-tag { color: #bdbdbd; }

.vault-tag {
  display: inline-block;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.62rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--mute);
  margin-bottom: 6px;
}

.vault-card strong {
  display: block;
  font-size: 0.95rem;
  letter-spacing: -0.02em;
}

.vault-note {
  margin: 6px 0 0;
  font-size: 0.78rem;
  color: var(--mute);
  line-height: 1.4;
}

.vault-player {
  border: 1px solid var(--ink);
  background: #0a0a0a;
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

.vault-player video {
  width: 100%;
  height: min(42vh, 420px);
  object-fit: contain;
  background: #0a0a0a;
}

.vault-player .stage-bar {
  background: var(--paper);
  padding: 10px 12px;
  border-top: 1px solid var(--ink);
}

.foot {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  border-top: 1px solid var(--line);
  padding-top: 8px;
  font-family: "IBM Plex Mono", ui-monospace, monospace;
  font-size: 0.68rem;
  color: var(--mute);
  flex-shrink: 0;
}

/* —— Breakpoints —— */
@media (max-width: 1100px) {
  .built-body {
    grid-template-columns: 1fr;
    overflow: auto;
  }

  .pipeline-wrap {
    height: auto;
    min-height: 300px;
    max-height: min(58vh, 520px);
  }

  .pipeline {
    height: auto;
    min-height: 400px;
  }

  .node-detail {
    height: auto;
    max-height: none;
  }

  .detail-boxes {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 900px) {
  .shell {
    height: auto;
    min-height: 100dvh;
    min-height: 100vh;
  }

  main { overflow: visible; }

  .panel.is-on { height: auto; }

  #panel-gallery {
    grid-template-columns: 1fr;
    gap: 14px;
  }

  .filters {
    flex-direction: row;
    flex-wrap: wrap;
    gap: 14px 18px;
    overflow: visible;
  }

  .filter-block { flex: 1 1 140px; }

  .btn-col { flex-direction: row; flex-wrap: wrap; }

  .stage-frame {
    flex: none;
    width: 100%;
    aspect-ratio: 16 / 9;
    max-height: 56vh;
  }

  #panel-built {
    height: auto;
  }

  .pipeline-wrap {
    max-height: none;
  }

  #panel-vault {
    height: auto;
    overflow: visible;
  }
}

@media (max-width: 560px) {
  .top { align-items: flex-start; }

  .top-actions {
    width: 100%;
    justify-content: space-between;
  }

  .tabs { flex: 1; }

  .tab { padding-left: 6px; padding-right: 6px; }

  h1 {
    font-size: clamp(1.05rem, 6vw, 1.45rem);
  }

  .stage-bar {
    flex-wrap: wrap;
  }

  a.dl,
  button.dl { width: 100%; }

  .node-detail-inner {
    flex-direction: column;
  }

  .detail-boxes {
    grid-template-columns: 1fr;
  }

  .pipeline-wrap {
    height: auto;
    min-height: 240px;
  }

  .pipeline {
    min-height: 380px;
    min-width: 760px;
  }

  .node-detail {
    height: auto;
    min-height: clamp(118px, 16vh, 152px);
  }
}
