:root {
  color-scheme: light;
  --topbar: #1d2733;
  --topbar-text: #dbe3ea;
  --bg: #eceff2;
  --surface: #fff;
  --line: #d9dee3;
  --ink: #1f2a37;
  --muted: #66717d;
  --sheet-border: #c6ccd3;
  --red: #e2272c;
  --red-hover: #c91f24;
  --warn-ink: #8a2f17;
  --warn-bg: #fff5f2;
  --warn-line: #efb9a9;
  --ok-ink: #1f4d38;
  --focus: #2563eb;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  overflow: hidden;
}

button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; }

:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.icon-library { position: absolute; width: 0; height: 0; }

.icon {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.app {
  width: 100%;
  height: 100vh;
  height: 100dvh;
  display: grid;
  /* One column pinned to the screen width: wide content (chip row, zoomed
     sheet) scrolls inside its own box instead of widening the page. */
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto auto auto minmax(0, 1fr) auto;
  grid-template-areas: "top" "tools" "tabs" "stage" "composer";
}

.topbar { grid-area: top; }
.toolbar { grid-area: tools; }
.tabs { grid-area: tabs; }
.stage { grid-area: stage; }
.composer-area { grid-area: composer; }

/* Top bar */
.topbar {
  min-height: 56px;
  padding: env(safe-area-inset-top) max(16px, env(safe-area-inset-right)) 0 max(18px, env(safe-area-inset-left));
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: #fff;
  background: var(--topbar);
}

.logo {
  font-size: 23px;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.project {
  position: relative;
  display: flex;
  align-items: center;
  gap: 4px;
  min-width: 0;
  color: var(--topbar-text);
}

.project-name {
  min-width: 0;
  max-width: 52vw;
  padding: 6px 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: inherit;
  font-size: 15px;
}

.project-name:not(:disabled):hover { background: rgb(255 255 255 / 0.1); }

.icon-button {
  width: 42px;
  height: 42px;
  flex: none;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--ink);
}

.icon-button:not(:disabled):hover { background: #eef1f4; }
.icon-button:disabled { color: #b4bbc3; }
.icon-button.on-dark { color: #fff; }
.icon-button.on-dark:not(:disabled):hover { background: rgb(255 255 255 / 0.1); }

.menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  z-index: 10;
  min-width: 220px;
  padding: 6px;
  display: grid;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  box-shadow: 0 12px 32px rgb(15 23 32 / 0.18);
}

.menu button {
  padding: 10px 12px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--ink);
  text-align: left;
  font-size: 14px;
}

.menu button:not(:disabled):hover { background: #f1f3f5; }
.menu button:disabled { color: #aab1b9; }

.menu-status {
  margin: 0 0 4px;
  padding: 6px 12px 10px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 12.5px;
  line-height: 1.4;
}

.menu-status[data-state="error"] { color: var(--warn-ink); }

.menu hr {
  width: 100%;
  margin: 4px 0;
  border: 0;
  border-top: 1px solid var(--line);
}

/* Open projects as tabs, with a "New tab" button (D-024). */
.tabs {
  display: flex;
  align-items: flex-end;
  gap: 4px;
  min-height: 40px;
  padding: 6px max(8px, env(safe-area-inset-right)) 0 max(8px, env(safe-area-inset-left));
  overflow-x: auto;
  scrollbar-width: none;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
}

.tab {
  flex: none;
  display: flex;
  align-items: center;
  max-width: 220px;
  height: 34px;
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: 8px 8px 0 0;
  background: #e2e6ea;
  color: var(--muted);
}

.tab[data-active="true"] {
  background: var(--surface);
  color: var(--ink);
}

.tab-name {
  min-width: 0;
  height: 100%;
  padding: 0 4px 0 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  border: 0;
  background: transparent;
  font-size: 13.5px;
}

.tab-name:last-child { padding-right: 14px; }

.tab-close {
  flex: none;
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: inherit;
}

.tab-new {
  flex: none;
  align-self: center;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 30px;
  margin-left: 4px;
  padding: 0 12px 0 9px;
  border: 1px solid #cfd5dc;
  border-radius: 15px;
  background: var(--surface);
  color: var(--ink);
  font-size: 13px;
  white-space: nowrap;
}

.tab-close .icon, .tab-new .icon { width: 16px; height: 16px; stroke-width: 2; }
.tab-close:hover { background: rgb(15 23 32 / 0.07); }
.tab-new:hover { background: #f4f6f8; }

/* Dialogs: project details, projects on this device */
.dialog {
  width: min(440px, calc(100% - 32px));
  max-height: calc(100dvh - 32px);
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 20px 48px rgb(15 23 32 / 0.28);
}

.dialog::backdrop { background: rgb(15 23 32 / 0.35); }

.dialog-body {
  display: grid;
  gap: 12px;
  padding: 18px;
}

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

.dialog-note {
  margin: -4px 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.4;
}

.dialog label {
  display: grid;
  gap: 4px;
  min-width: 0;
  color: var(--muted);
  font-size: 13px;
}

.dialog input {
  width: 100%;
  min-width: 0;
  height: 42px;
  padding: 0 12px;
  border: 1px solid #d0d6dc;
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  font-size: 16px;
}

.field-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 9.5rem);
  gap: 10px;
}

.dialog-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 4px;
}

.dialog-actions button {
  height: 42px;
  padding: 0 18px;
  border-radius: 21px;
  font-size: 14px;
}

.primary { border: 0; background: var(--topbar); color: #fff; }
.primary:hover { background: #2b3949; }
.secondary { border: 1px solid #cfd5dc; background: var(--surface); }
.secondary:hover { background: #f4f6f8; }

.project-list {
  display: grid;
  gap: 6px;
  max-height: 50dvh;
  margin: 0;
  padding: 0;
  overflow: auto;
  list-style: none;
}

.project-item {
  display: flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  border-radius: 10px;
}

.project-item[data-current="true"] { border-color: #a9b8c7; background: #f6f8fa; }

.project-open {
  flex: 1;
  min-width: 0;
  display: grid;
  gap: 2px;
  padding: 10px 12px;
  border: 0;
  background: transparent;
  text-align: left;
}

.project-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 15px;
  font-weight: 600;
}

.project-meta { color: var(--muted); font-size: 12.5px; }

.project-delete {
  flex: none;
  height: 36px;
  margin-right: 8px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--surface);
  color: var(--warn-ink);
  font-size: 13px;
}

.project-delete[data-armed="true"] { border-color: var(--warn-line); background: var(--warn-bg); }

/* Toolbar */
.toolbar {
  min-height: 50px;
  padding: 0 max(8px, env(safe-area-inset-right)) 0 max(8px, env(safe-area-inset-left));
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.tools { display: flex; gap: 2px; }
.icon-button[aria-pressed="true"] { background: #eef1f4; }

.sheet-size {
  padding-right: 8px;
  color: #47525e;
  font-size: 15px;
  white-space: nowrap;
}

/* Sheet */
.stage {
  position: relative;
  min-height: 0;
}

.sheet-viewport {
  position: absolute;
  inset: 0;
  overflow: auto;
  display: flex;
  padding: clamp(10px, 2.4vw, 28px);
  touch-action: pan-x pan-y;
  overscroll-behavior: contain;
}

.sheet {
  position: relative;
  flex: none;
  margin: auto;
  aspect-ratio: 1120 / 792;
  border: 1px solid var(--sheet-border);
  background: #fff;
  box-shadow: 0 1px 3px rgb(15 23 32 / 0.08);
}

#sheet, #sheet svg {
  display: block;
  width: 100%;
  height: 100%;
}

/* Tap a symbol or a line for suggestions (D-037). */
#sheet [data-node]:not([data-type="junction"]),
#sheet [data-edge] { cursor: pointer; }
#sheet [data-node].is-selected { filter: drop-shadow(0 0 2px #1f6feb) drop-shadow(0 0 1px #1f6feb); }
#sheet [data-edge].is-selected { stroke: #1f6feb; stroke-width: 3.6; }

/* Centre of the drawing area (left of the title-block column). */
.center-status,
.ready-message {
  position: absolute;
  top: 50%;
  left: 39.6%;
  transform: translate(-50%, -50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  pointer-events: none;
}

.ready-message {
  gap: clamp(6px, 1.4vw, 14px);
  color: var(--ink);
}

.ready-icon {
  width: clamp(34px, 6vw, 64px);
  height: clamp(34px, 6vw, 64px);
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
}

.ready-message strong {
  font-size: clamp(16px, 2.6vw, 28px);
  font-weight: 650;
  letter-spacing: -0.01em;
  white-space: nowrap;
}

.center-status {
  width: min(70%, 300px);
  gap: 10px;
  color: var(--muted);
}

.center-status p {
  margin: 0;
  font-size: clamp(12px, 1.5vw, 14px);
}

.center-status[data-state="failed"] { color: var(--warn-ink); }
.center-status[data-state="failed"] .progress { display: none; }

.progress {
  width: 100%;
  height: 4px;
  overflow: hidden;
  border-radius: 2px;
  background: #e3e7eb;
}

.progress span {
  display: block;
  width: 0;
  height: 100%;
  background: var(--topbar);
  transition: width 0.2s ease;
}

/* Prompt */
.composer-area {
  padding: 8px max(12px, env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  background: var(--bg);
}

.assist, .composer {
  width: 100%;
  max-width: 860px;
  margin: 0 auto;
}

.assist { display: grid; grid-template-columns: minmax(0, 1fr); gap: 8px; }
.assist:has(> :not([hidden])) { margin-bottom: 8px; }

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

.feedback-row:not(:has(> :not([hidden]))) { display: none; }

.feedback {
  flex: 1;
  margin: 0;
  padding: 9px 13px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--ok-ink);
  font-size: 14px;
  line-height: 1.4;
  overflow-wrap: anywhere;
}

.feedback[data-state="error"] {
  border-color: var(--warn-line);
  background: var(--warn-bg);
  color: var(--warn-ink);
}

.feedback[data-state="info"] { color: var(--ink); }

.details-toggle {
  flex: none;
  height: 38px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  color: var(--muted);
  font-size: 13px;
}

.details {
  max-height: 30vh;
  overflow: auto;
  padding: 9px 13px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
}

.model-reading, .graph-diff {
  margin: 0;
  color: var(--muted);
  font: 12px/1.5 ui-monospace, "SFMono-Regular", Consolas, monospace;
  overflow-wrap: anywhere;
}

.graph-diff { padding-left: 16px; margin-top: 6px; color: var(--ink); }

.quick-starts {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
}

.chip {
  flex: none;
  min-height: 36px;
  padding: 0 14px;
  border: 1px solid #cfd5dc;
  border-radius: 18px;
  background: var(--surface);
  color: var(--ink);
  font-size: 13.5px;
}

.chip:not(:disabled):hover { background: #f4f6f8; }
.chip:disabled { color: #aab1b9; }

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

.attach { color: #4b5563; }

#prompt {
  flex: 1;
  min-width: 0;
  height: 54px;
  padding: 0 22px;
  border: 1px solid #d0d6dc;
  border-radius: 27px;
  background: var(--surface);
  font-size: 16px;
}

#prompt::placeholder { color: #7b8590; }
#prompt:disabled { background: #f6f7f8; }

.action-button {
  width: 58px;
  height: 58px;
  flex: none;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: var(--red);
  color: #fff;
  box-shadow: 0 6px 16px rgb(226 39 44 / 0.32);
}

.action-button:not(:disabled):hover { background: var(--red-hover); }
.action-button:disabled { opacity: 0.55; box-shadow: none; }
.action-button .icon { width: 26px; height: 26px; stroke-width: 2; }

@media (min-width: 900px) {
  .topbar { padding-inline: 24px; }
  .toolbar { padding-inline: 14px; }
}

@media (max-width: 420px) {
  .icon-button { width: 40px; height: 40px; }
  .sheet-size { font-size: 14px; }
  #prompt { height: 50px; padding: 0 18px; }
  .action-button { width: 54px; height: 54px; }
}
