:root {
  color-scheme: light;
  --paper: #f5f0e5;
  --panel: #fbf8f0;
  --canvas: #f9f6ed;
  --ink: #222b26;
  --muted: #585b51;
  --edge: #dcd6c8;
  --accent: #294f82;
  --accent-hover: #1f406c;
  --selection: #e8edf4;
  --danger: #8b382e;
  --radius: 8px;
  font-family: system-ui, sans-serif;
  color: var(--ink);
  background: var(--paper);
  font-synthesis: none;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
}

[hidden], .mobile-only { display: none !important; }
.symbols { position: absolute; width: 0; height: 0; overflow: hidden; }

h1, h2, p, ul { margin: 0; }
button, input { font: inherit; }
button { color: inherit; }

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

.masthead {
  min-height: 64px;
  padding: env(safe-area-inset-top) 24px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid var(--edge);
  background: var(--panel);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  font-family: Georgia, serif;
  font-size: 25px;
  letter-spacing: -0.04em;
  white-space: nowrap;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: var(--radius);
  border: 1px solid #d1d9e2;
  background: #eaf0f5;
  color: var(--accent);
}

.icon {
  width: 18px;
  height: 18px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.65;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.brand-mark .icon { width: 22px; height: 22px; }

.workspace {
  position: relative;
  display: grid;
  grid-template-columns: 252px minmax(0, 1fr);
  min-height: 0;
}

.library {
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 28px 16px max(16px, env(safe-area-inset-bottom));
  background: var(--paper);
  border-right: 1px solid var(--edge);
}

.library-heading {
  display: grid;
  gap: 16px;
  padding: 0 4px 16px;
}

.library-heading h2 { font-size: 15px; font-weight: 650; }

.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 40px;
  padding: 0 12px;
  border: 1px solid transparent;
  border-radius: var(--radius);
  background: transparent;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color 130ms ease, border-color 130ms ease;
}

.primary { color: #fff; background: var(--accent); }
.primary:hover:not(:disabled) { background: var(--accent-hover); }
.subtle:hover:not(:disabled) { background: #ece8de; }
.icon-button { width: 40px; padding: 0; }
.button:disabled { opacity: .36; cursor: default; }
.destructive:hover:not(:disabled) { color: var(--danger); background: #f3e7df; }

.map-list {
  min-height: 0;
  overflow: auto;
  list-style: none;
  padding: 4px;
  display: grid;
  gap: 4px;
}

.map-list-button {
  width: 100%;
  min-height: 40px;
  padding: 8px 10px;
  text-align: start;
  overflow-wrap: anywhere;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius);
  font-size: 14px;
  cursor: pointer;
}

.map-list-button:hover { background: #ede8dd; }
.map-list-button[aria-current="true"] {
  background: var(--selection);
  border-color: #d1dbe7;
  color: var(--accent);
  box-shadow: inset 2px 0 var(--accent);
  font-weight: 600;
}

.library-tools {
  display: grid;
  margin-top: auto;
  padding: 12px 4px 0;
  border-top: 1px solid var(--edge);
}

.library-tools .button { justify-content: flex-start; padding-inline: 8px; }
.library-tools p { color: var(--muted); font-size: 12px; padding: 12px 8px 4px; }

.main {
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  min-width: 0;
  min-height: 0;
  background: var(--panel);
}

.editor {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  min-height: 0;
}

.editor-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 24px 28px 12px;
}

h1 {
  font-family: Georgia, serif;
  font-size: clamp(28px, 4vw, 43px);
  line-height: 1.12;
  letter-spacing: -.04em;
  font-weight: 400;
  overflow-wrap: anywhere;
}

.editor-heading h1 {
  overflow: hidden;
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
}

.map-actions, .editor-tools, .branch-tools, .dialog-actions { display: flex; align-items: center; gap: 4px; }
.editor-tools {
  padding: 0 28px 12px;
  gap: 8px;
  flex-wrap: wrap;
  border-bottom: 1px solid var(--edge);
}

.editor-tools .primary {
  background: var(--selection);
  border-color: #d0dae7;
  color: var(--accent-hover);
}
.editor-tools .primary:hover:not(:disabled) { background: #dce6f2; }
.branch-tools { border-left: 1px solid var(--edge); padding-left: 4px; }

.viewport, .empty {
  background-color: var(--canvas);
  background-image: radial-gradient(circle, #dcd5c6 0.8px, transparent 0.9px);
  background-size: 20px 20px;
}

.viewport {
  min-height: 0;
  min-width: 0;
  overflow: auto;
  overscroll-behavior: contain;
}

.viewport:focus-visible { outline-offset: -3px; }

.surface {
  display: grid;
  place-items: center;
  width: max-content;
  min-width: 100%;
  min-height: 100%;
  padding: 18px;
}

.map {
  position: relative;
  display: grid;
  grid-template-columns: 1fr max-content 1fr;
  align-items: center;
  gap: 48px;
  width: max-content;
  zoom: var(--zoom, 1);
}

.connections {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  fill: none;
  stroke-width: 1.6;
  opacity: .74;
  pointer-events: none;
}

.wing, .children {
  display: grid;
  gap: 16px;
  list-style: none;
  padding: 0;
}

.wing.left { justify-self: end; }
.wing.right { justify-self: start; }

.branch {
  display: grid;
  align-items: center;
  gap: 40px;
}

.right .branch { grid-template-columns: 156px auto; }
.left .branch { grid-template-columns: auto 156px; }
.left .branch > .node { grid-column: 2; grid-row: 1; }
.left .branch > .children { grid-column: 1; grid-row: 1; }

.node {
  position: relative;
  z-index: 1;
  min-height: 44px;
  padding: 8px 12px;
  background: var(--panel);
  border: 1px solid #cfc9bc;
  border-radius: var(--radius);
  text-align: start;
  font-size: 14px;
  line-height: 1.35;
  font-weight: 570;
  overflow-wrap: anywhere;
  cursor: pointer;
}

.left .node { border-right: 3px solid var(--branch-color); }
.right .node { border-left: 3px solid var(--branch-color); }
.node:hover { background: #fffdf8; }
.node[aria-pressed="true"] {
  background: #edf2f8;
  box-shadow: inset 0 0 0 1px var(--accent);
  border-top-color: var(--accent);
  border-bottom-color: var(--accent);
}

.node.root {
  width: 192px;
  padding: 16px;
  min-height: 60px;
  border: 1px solid var(--ink);
  background: var(--ink);
  border-radius: 12px;
  text-align: center;
  font-size: 16px;
  color: #fffaf0;
}
.node.root:hover { background: #35433a; }
.node.root[aria-pressed="true"] { box-shadow: 0 0 0 2px var(--canvas), 0 0 0 4px var(--accent); }

.empty {
  overflow: auto;
  display: grid;
  place-items: center;
  padding: 24px 20px;
}

.empty-content {
  display: grid;
  justify-items: center;
  gap: 12px;
  text-align: center;
}

.empty-content p {
  max-width: 32ch;
  line-height: 1.55;
  color: var(--muted);
  font-size: 15px;
}

.empty-content .button { margin-top: 8px; }
.empty-map { width: min(66vw, 288px); height: auto; margin-bottom: 8px; }
.empty-map path { fill: none; stroke-width: 1.5; stroke-linecap: round; }
.empty-left, .empty-leaf.left { stroke: #a05436; }
.empty-right, .empty-leaf.right { stroke: #3d69a1; }
.empty-leaf.left { fill: #f6e9df; }
.empty-leaf.right { fill: #eaf0f5; }
.empty-root { fill: var(--ink); }
.empty-map .empty-ink { stroke: #fffcf3; stroke-width: 1.8; }

.footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  min-height: 50px;
  padding: 4px 16px max(4px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--edge);
}

.status {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.status.error, .dialog-error { color: var(--danger); }
.zoom-controls { display: flex; align-items: center; gap: 4px; margin-left: auto; }
#zoom-level {
  min-width: 40px;
  text-align: center;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

#zoom-fit { border-left: 1px solid var(--edge); border-radius: 0 var(--radius) var(--radius) 0; }

dialog {
  width: min(100% - 32px, 420px);
  max-height: calc(100% - 32px);
  padding: 24px;
  background: var(--panel);
  color: var(--ink);
  border: 1px solid var(--edge);
  border-radius: 16px;
  box-shadow: 0 16px 64px #25232226;
}

dialog::backdrop { background: #1e231e52; }
dialog form { display: grid; gap: 12px; }

dialog h2 {
  font-family: Georgia, serif;
  font-size: 26px;
  font-weight: 400;
  letter-spacing: -.025em;
}

dialog label, dialog p { font-size: 14px; line-height: 1.5; }

dialog input {
  min-width: 0;
  height: 44px;
  padding: 0 12px;
  border: 1px solid #b8b1a3;
  border-radius: var(--radius);
  color: var(--ink);
  background: #fffdf7;
  font-size: 16px;
}

.dialog-actions { justify-content: flex-end; gap: 8px; margin-top: 4px; }
.no-script { padding: 16px; }

@media (max-width: 720px), (pointer: coarse) and (max-width: 1024px) {
  .mobile-only:not([hidden]) { display: inline-flex !important; }
  .desktop-only { display: none; }
  .masthead { min-height: 56px; padding-inline: 16px; }
  .brand { font-size: 22px; }
  .brand-mark { width: 32px; height: 32px; }
  .workspace { grid-template-columns: minmax(0, 1fr); }
  .library {
    display: none;
    position: absolute;
    z-index: 3;
    inset: 0 auto 0 0;
    width: min(320px, 85%);
    padding-top: 20px;
  }
  .library.open { display: flex; }
  #sidebar-dismiss {
    position: absolute;
    z-index: 2;
    inset: 0;
    border: 0;
    background: #1e231e52;
  }
  .editor-heading { padding: 16px 12px 8px 16px; gap: 8px; }
  .editor-tools { padding: 0 12px 8px; gap: 4px; }
  .button { min-height: 44px; }
  .icon-button, #export-map { width: 44px; padding: 0; }
  .editor-tools .button:not(.icon-button) { padding-inline: 6px; gap: 6px; }
  .editor-tools .branch-tools { padding-left: 2px; gap: 0; }
  .footer { padding-inline: 12px; }
  .zoom-controls { gap: 0; }
  #zoom-level { min-width: 32px; }
}

@media (max-height: 380px) {
  body {
    height: auto;
    min-height: 100dvh;
    grid-template-rows: auto minmax(320px, 1fr);
    overflow: auto;
  }
  .editor { grid-template-rows: auto auto minmax(160px, 1fr); }
}

@media (display-mode: standalone) {
  [data-action="install"] { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .button { transition: none; }
}
