:root {
  color-scheme: light;
  --ink: #171a21;
  --muted: #687386;
  --soft: #f3f6fa;
  --panel: #ffffff;
  --line: #dde3ec;
  --line-strong: #cbd4e2;
  --accent: #1769e8;
  --accent-soft: #e9f1ff;
  --teal: #0f9f86;
  --warn: #a96306;
  --danger: #bf3030;
  --dark: #11141b;
  --dark-2: #1b202b;
  --shadow: 0 16px 44px rgba(34, 45, 63, .10);
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  color: var(--ink);
  background: #eef2f7;
  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 { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .58; }

#app {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr);
  overflow-x: hidden;
}

.rail {
  background: #11151d;
  color: #f7f9fc;
  display: flex;
  flex-direction: column;
  padding: 18px 14px;
  gap: 18px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 8px 7px 16px;
  border-bottom: 1px solid rgba(255,255,255,.10);
}
.brand-mark {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: #fff;
  color: #11151d;
  font-weight: 900;
}
.brand strong, .brand small { display: block; }
.brand small { color: #9fa9ba; margin-top: 2px; }

.nav { display: grid; gap: 7px; }
.nav-item {
  width: 100%;
  min-height: 44px;
  border: 0;
  border-radius: 8px;
  padding: 0 11px;
  display: flex;
  align-items: center;
  gap: 10px;
  background: transparent;
  color: #cbd3df;
  text-align: left;
}
.nav-item:hover, .nav-item.active { background: #232a37; color: #fff; }
.nav-icon {
  width: 25px;
  height: 25px;
  display: grid;
  place-items: center;
  border: 1px solid rgba(255,255,255,.14);
  border-radius: 7px;
  font-weight: 800;
  font-size: 12px;
}

.runtime-card {
  margin-top: auto;
  border: 1px solid rgba(255,255,255,.10);
  background: #171c26;
  border-radius: 8px;
  padding: 12px;
  display: grid;
  gap: 10px;
}
.runtime-row {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  font-size: 12px;
  color: #aeb8c7;
}
.runtime-row b { color: #fff; }
.runtime-row .safe { color: #7ee3c7; }

.shell {
  min-width: 0;
  display: grid;
  grid-template-rows: 64px minmax(0, 1fr);
}

.topbar {
  background: rgba(255,255,255,.94);
  border-bottom: 1px solid var(--line);
  padding: 0 22px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  position: sticky;
  top: 0;
  z-index: 20;
}
.top-left {
  display: flex;
  align-items: baseline;
  gap: 13px;
  min-width: 0;
}
.top-left strong { white-space: nowrap; }
.top-left span {
  color: var(--muted);
  font-size: 13px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.top-left span.ok { color: var(--teal); }
.top-left span.warning { color: var(--warn); }
.top-left span.error { color: var(--danger); }

.top-actions {
  display: flex;
  align-items: center;
  gap: 9px;
  min-width: 0;
}
.token-input {
  width: 320px;
  max-width: 28vw;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 8px;
  padding: 10px 12px;
  outline: 0;
}
.token-input:focus, textarea:focus, select:focus, input:focus {
  border-color: #8ab4ff;
  box-shadow: 0 0 0 3px rgba(23,105,232,.10);
}

.secondary-btn, .primary-btn, .icon-btn {
  border-radius: 8px;
  min-height: 38px;
  padding: 0 13px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
}
.secondary-btn {
  border: 1px solid var(--line);
  background: #fff;
  color: #263040;
}
.secondary-btn:hover { border-color: var(--line-strong); background: #f8fafc; }
.primary-btn {
  border: 0;
  background: var(--dark);
  color: #fff;
  font-weight: 800;
}
.primary-btn:hover { background: #262d3a; }
.icon-btn {
  width: 40px;
  height: 40px;
  padding: 0;
  border: 1px solid var(--line);
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 22px;
  font-weight: 800;
}

.status-pill {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 0 10px;
  border-radius: 8px;
  font-size: 12px;
  white-space: nowrap;
}
.status-pill.neutral { background: #f2f5f9; color: #465266; }
.status-pill.ok { background: #e7f7f2; color: #08765f; }
.status-pill.warning { background: #fff4dc; color: #8c5308; }
.status-pill.error { background: #fff0f0; color: #a82727; }

.view { display: none; }
.active-view { display: block; }

.studio-grid {
  min-height: calc(100vh - 64px);
  padding: 14px;
  display: none;
  grid-template-columns: minmax(220px, 280px) minmax(360px, 1fr) minmax(240px, 300px);
  gap: 12px;
}
.studio-grid.active-view { display: grid; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  min-width: 0;
}
.panel-head, .composer-head, .page-title {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 14px;
}
.panel-head h2, .panel-subhead h3, .composer-head h1, .page-title h2 {
  margin: 0;
  letter-spacing: 0;
}
.panel-head h2, .page-title h2 { font-size: 21px; }
.panel-head p, .composer-head p, .page-title p, .panel-subhead span {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.material-panel, .queue-panel, .composer-panel { padding: 16px; }
.material-panel, .queue-panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow: hidden;
}
.composer-panel {
  display: grid;
  grid-template-rows: auto minmax(220px, 1fr) auto auto auto;
  gap: 14px;
}
.composer-head h1 { font-size: 25px; }

.drop-zone {
  min-height: 104px;
  border: 1px dashed #aebbd0;
  background: #f8fafc;
  border-radius: 8px;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 14px;
  color: #47556a;
}
.drop-zone strong, .drop-zone span { display: block; }
.drop-zone span { margin-top: 6px; color: var(--muted); font-size: 13px; }
.drop-zone.dragover {
  border-color: var(--accent);
  background: #eef5ff;
}
.add-file input { display: none; }

.asset-strip {
  display: grid;
  gap: 10px;
  overflow: auto;
  max-height: 310px;
  padding-right: 2px;
}
.empty-state {
  color: var(--muted);
  border: 1px solid var(--line);
  background: #fafbfc;
  border-radius: 8px;
  padding: 14px;
  font-size: 13px;
}

.asset-mini, .asset-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}
.asset-mini {
  display: grid;
  grid-template-columns: 64px minmax(0, 1fr);
  gap: 10px;
  padding: 9px;
}
.asset-thumb, .asset-preview {
  background: #eef2f7;
  overflow: hidden;
  border-radius: 8px;
}
.asset-thumb { width: 64px; height: 54px; }
.asset-thumb img, .asset-thumb video, .asset-preview img, .asset-preview video {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.asset-meta {
  min-width: 0;
  display: grid;
  gap: 5px;
  align-content: start;
}
.asset-meta strong, .asset-card strong {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.asset-meta small, .asset-card p {
  color: var(--muted);
  font-size: 12px;
  margin: 0;
}
.asset-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.tiny-btn {
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #f8fafc;
  color: #354154;
  padding: 5px 7px;
  font-size: 12px;
}
.uploading-text { color: var(--warn); }
.uploaded-text { color: var(--teal); }
.failed-text { color: var(--danger); }

.panel-subhead {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
  border-top: 1px solid var(--line);
  padding-top: 13px;
}
.panel-subhead h3 { font-size: 16px; }
.template-list { display: grid; gap: 9px; }
.template-card {
  width: 100%;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 8px;
  min-height: 68px;
  padding: 11px;
  text-align: left;
  display: grid;
  gap: 4px;
}
.template-card:hover, .template-card.selected {
  border-color: #9ec0ff;
  background: #f4f8ff;
}
.template-card strong { font-size: 14px; }
.template-card span { color: var(--muted); font-size: 12px; line-height: 1.35; }

.mode-switch {
  flex: 0 0 auto;
  display: inline-flex;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #f6f8fb;
}
.mode-switch button {
  border: 0;
  background: transparent;
  color: #435065;
  border-radius: 7px;
  padding: 8px 12px;
}
.mode-switch button.selected {
  background: #fff;
  color: var(--dark);
  box-shadow: 0 4px 12px rgba(20,30,45,.08);
  font-weight: 800;
}

.prompt-box {
  width: 100%;
  min-height: 250px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
  font-size: 17px;
  line-height: 1.55;
  color: #273143;
  outline: 0;
  background: #fff;
}

.control-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(104px, 1fr));
  gap: 10px;
}
.control-grid label, .ledger-box label {
  display: grid;
  gap: 6px;
  min-width: 0;
}
.control-grid span, .ledger-box span {
  color: var(--muted);
  font-size: 12px;
}
select, .inline-search input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 10px 11px;
  outline: 0;
}

.reference-chips {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  min-height: 38px;
}
.ref-chip {
  display: grid;
  grid-template-columns: auto minmax(90px, 190px) minmax(104px, 132px) auto auto;
  align-items: center;
  gap: 7px;
  border: 1px solid #d5e2f7;
  background: #f5f9ff;
  color: #264979;
  border-radius: 8px;
  padding: 6px 7px;
  max-width: 100%;
}
.ref-chip.failed {
  border-color: #ffd0d0;
  background: #fff7f7;
  color: var(--danger);
}
.ref-chip.uploading {
  border-color: #f8daa1;
  background: #fff9ed;
  color: var(--warn);
}
.ref-chip b {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 34px;
  height: 25px;
  border-radius: 7px;
  background: rgba(255,255,255,.75);
  font-size: 11px;
}
.ref-chip span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
}
.ref-chip select { padding: 6px 8px; font-size: 12px; }
.ref-chip button {
  border: 0;
  background: transparent;
  color: inherit;
  font-weight: 800;
  padding: 4px;
}

.submit-bar {
  border-top: 1px solid var(--line);
  padding-top: 14px;
  display: flex;
  justify-content: space-between;
  gap: 14px;
  align-items: center;
}
.estimate { display: grid; gap: 4px; }
.estimate strong { font-size: 16px; }
.estimate span { color: var(--muted); font-size: 12px; }
.submit-bar .primary-btn { min-width: 142px; min-height: 44px; }

.quick-stats {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}
.stat {
  border: 1px solid var(--line);
  background: #fafbfc;
  border-radius: 8px;
  padding: 10px;
}
.stat b { display: block; font-size: 20px; }
.stat span { color: var(--muted); font-size: 12px; }
.mini-job-list {
  display: grid;
  gap: 9px;
  overflow: auto;
  max-height: 420px;
  padding-right: 2px;
}
.mini-job, .job-card {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 8px;
  padding: 12px;
}
.mini-job strong, .job-card strong {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.mini-job p, .job-card p {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 12px;
}
.ledger-box {
  border-top: 1px solid var(--line);
  padding-top: 12px;
}
.inline-search {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 7px;
}

.canvas-view {
  height: calc(100vh - 64px);
  background: #10141c;
  position: relative;
  overflow: hidden;
  background-image: linear-gradient(rgba(255,255,255,.045) 1px, transparent 1px), linear-gradient(90deg, rgba(255,255,255,.045) 1px, transparent 1px);
  background-size: 34px 34px;
}
.canvas-toolbar {
  position: absolute;
  z-index: 4;
  left: 50%;
  top: 18px;
  transform: translateX(-50%);
  display: flex;
  gap: 7px;
  padding: 8px;
  border: 1px solid rgba(255,255,255,.10);
  background: rgba(27,32,43,.95);
  border-radius: 8px;
  box-shadow: 0 12px 34px rgba(0,0,0,.26);
}
.canvas-toolbar button {
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: #e6ebf4;
  padding: 9px 12px;
}
.canvas-toolbar button:hover, .canvas-toolbar .accent { background: #2b3445; }
.canvas-toolbar .accent { color: #fff; font-weight: 800; }
.canvas-board {
  position: relative;
  width: 100%;
  height: 100%;
  transform-origin: center;
}
.canvas-node {
  position: absolute;
  border-radius: 8px;
  color: #e7edf7;
  box-shadow: 0 16px 40px rgba(0,0,0,.25);
}
.canvas-node span { display: block; margin-bottom: 8px; color: #b9c3d4; }
.image-node {
  width: 330px;
  padding: 12px;
  background: #1b222e;
  border: 1px solid #303b4f;
}
.image-node img {
  display: block;
  width: 100%;
  height: 190px;
  object-fit: cover;
  border-radius: 8px;
  background: #fff;
}
.text-node {
  width: 440px;
  min-height: 150px;
  padding: 18px;
  background: #242b38;
  border: 1px solid #374257;
}
.text-node p { color: #aeb8c7; line-height: 1.6; margin-bottom: 0; }
.canvas-link {
  position: absolute;
  width: 130px;
  height: 3px;
  background: linear-gradient(90deg, #4c8cff, #55d7bd);
  border-radius: 999px;
}

.page-panel {
  padding: 22px;
}
.page-title {
  margin-bottom: 16px;
}
.agent-grid, .asset-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 14px;
}
.agent-card, .asset-card {
  padding: 15px;
  box-shadow: var(--shadow);
}
.agent-card h3 {
  margin: 0 0 8px;
  font-size: 18px;
}
.agent-card p {
  color: var(--muted);
  line-height: 1.55;
  min-height: 44px;
}
.agent-card button, .asset-card button, .job-card button {
  border: 1px solid var(--line);
  background: #f8fafc;
  border-radius: 8px;
  padding: 8px 10px;
  margin-right: 6px;
  margin-top: 8px;
}
.asset-preview {
  height: 160px;
  margin-bottom: 12px;
}
.job-list {
  display: grid;
  gap: 12px;
  max-width: 1120px;
}
.job-card pre {
  margin-top: 10px;
  max-height: 320px;
  overflow: auto;
  white-space: pre-wrap;
  background: #f7f9fc;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  color: #344156;
}

.muted { color: var(--muted); }
.ok { color: var(--teal); }
.warning { color: var(--warn); }
.error { color: var(--danger); }

@media (max-width: 1320px) {
  #app { grid-template-columns: 74px minmax(0, 1fr); }
  .brand span:not(.brand-mark), .nav-item span:not(.nav-icon), .runtime-card { display: none; }
  .rail { padding: 16px 10px; }
  .brand { justify-content: center; padding: 5px 0 14px; }
  .nav-item { justify-content: center; padding: 0; }
  .studio-grid { grid-template-columns: minmax(240px, 300px) minmax(380px, 1fr); }
  .queue-panel { grid-column: 1 / -1; }
  .mini-job-list { max-height: 260px; }
}

@media (max-width: 920px) {
  #app { display: block; }
  .rail {
    position: sticky;
    top: 0;
    z-index: 30;
    flex-direction: row;
    align-items: center;
    padding: 9px;
  }
  .brand { border: 0; padding: 0; }
  .nav { display: flex; overflow: auto; }
  .nav-item {
    min-width: 44px;
    width: auto;
  }
  .shell { grid-template-rows: auto minmax(0, 1fr); }
  .topbar {
    position: static;
    flex-direction: column;
    align-items: stretch;
    height: auto;
    padding: 12px;
  }
  .top-actions {
    display: grid;
    grid-template-columns: 1fr auto auto;
  }
  .status-pill { grid-column: 1 / -1; justify-self: start; }
  .token-input { width: 100%; max-width: none; }
  .studio-grid {
    display: none;
    grid-template-columns: 1fr;
    padding: 12px;
  }
  .studio-grid.active-view { display: grid; }
  .control-grid { grid-template-columns: 1fr 1fr; }
  .submit-bar { align-items: stretch; flex-direction: column; }
  .submit-bar .primary-btn { width: 100%; }
  .ref-chip { grid-template-columns: auto minmax(0, 1fr) auto; }
  .ref-chip select { grid-column: 1 / -1; }
  .canvas-toolbar {
    left: 12px;
    right: 12px;
    transform: none;
    overflow: auto;
  }
  .image-node { width: 260px; }
  .text-node { width: 300px; }
}

@media (max-width: 560px) {
  .top-actions { grid-template-columns: 1fr 1fr; }
  .token-input { grid-column: 1 / -1; }
  .control-grid { grid-template-columns: 1fr; }
  .mode-switch { width: 100%; }
  .mode-switch button { flex: 1; }
  .composer-head, .page-title { display: grid; }
}
