:root {
  --bg: #efe8dc;
  --sidebar: #f7f1e7;
  --canvas: #fbf7f1;
  --ink: #241c16;
  --muted: #6f6256;
  --line: #b7aa9a;
  --card: #fffdf9;
  --card-border: #e4d8c8;
  --accent: #1d6b4a;
  --accent-ink: #f4fbf7;
  --danger: #9c3a32;
  --shadow: 0 1px 2px rgba(48, 32, 16, 0.05), 0 10px 28px rgba(48, 32, 16, 0.05);
  --col: 250px;
  --link: 56px;
  --font: "Segoe UI Variable", "Segoe UI", sans-serif;
  --display: Palatino, "Palatino Linotype", "Book Antiqua", Georgia, serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  color: var(--ink);
  background: var(--bg);
  font-family: var(--font);
}

button, input, textarea, select { font: inherit; color: inherit; }

button { cursor: pointer; }

#app {
  display: flex;
  height: 100%;
  min-height: 0;
}

#side-backdrop {
  position: fixed;
  inset: 0;
  z-index: 39;
  background: rgba(36, 28, 22, 0.28);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.22s ease;
}

#app.is-menu-open #side-backdrop {
  opacity: 1;
  pointer-events: auto;
}

#side-menu {
  position: fixed;
  z-index: 40;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(280px, calc(100vw - 48px));
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 18px 16px 24px;
  background: #fffdf9;
  box-shadow: 16px 0 40px rgba(36, 28, 22, 0.16);
  overflow: auto;
  transform: translateX(-100%);
  visibility: hidden;
  transition: transform 0.22s ease, visibility 0s linear 0.22s;
}

#app.is-menu-open #side-menu {
  transform: translateX(0);
  visibility: visible;
  transition: transform 0.22s ease, visibility 0s;
}

.side-menu-head {
  display: flex;
  align-items: center;
  min-height: 36px;
  padding-left: 40px;
}

.side-menu-head .brand-kicker { margin: 0; }

.menu-bar {
  display: flex;
  align-items: center;
  min-height: 36px;
}

#app.is-menu-open .menu-toggle {
  position: fixed;
  z-index: 46;
  top: 20px;
  left: 16px;
  background: #fffdf9;
}

.side-links {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.side-link {
  display: block;
  padding: 10px 12px;
  border-radius: 10px;
  color: inherit;
  text-decoration: none;
  font-weight: 700;
}

.side-link:hover { background: #efe4d6; }

.side-link.is-current {
  background: #e7f3ec;
  color: #18583d;
}

.toolbar-row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 1;
}

#sidebar {
  width: 320px;
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 28px 22px;
  background: var(--sidebar);
  border-right: 1px solid #e3d6c6;
  overflow: auto;
}

.brand h1 {
  margin: 4px 0 8px;
  font-family: var(--display);
  font-size: 32px;
  font-weight: 600;
  letter-spacing: -0.03em;
}

.brand-kicker, .role, .add-kicker, .graph-meta, .hint, .empty-list {
  color: var(--muted);
}

.brand-kicker {
  margin: 0;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.brand-copy, .hint, .empty-copy {
  margin: 0;
  line-height: 1.45;
}

.stack-form, .add-form, .rename-form, .empty-card form {
  display: flex;
  gap: 8px;
}

.stack-form, .empty-card form { flex-direction: column; }

label, .add-kicker {
  font-size: 13px;
  font-weight: 600;
}

input, select {
  width: 100%;
  border: 1px solid #ddcfbf;
  background: white;
  border-radius: 10px;
  padding: 10px 12px;
  outline: none;
}

input:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(29, 107, 74, 0.16);
}

.btn, .icon-btn, .graph-open {
  border: 0;
  background: transparent;
}

.btn {
  border-radius: 10px;
  padding: 10px 14px;
  font-weight: 650;
  white-space: nowrap;
  flex: none;
}

.btn.primary {
  background: var(--accent);
  color: var(--accent-ink);
}

.btn.primary:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn.ghost {
  background: #efe6da;
}

.btn.danger, .icon-btn.danger:hover {
  background: var(--danger);
  color: white;
}

.graph-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
}

.graph-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px;
  border-radius: 12px;
}

.graph-item.is-active { background: #e7f3ec; }

.graph-open {
  flex: 1;
  min-width: 0;
  text-align: left;
  padding: 8px 10px;
  border-radius: 10px;
}

.graph-name, .label {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.graph-name { font-weight: 700; }

.graph-meta { font-size: 12px; }

#main {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

#toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 18px 24px;
  border-bottom: 1px solid #eadfd2;
  background: rgba(251, 247, 241, 0.92);
}

.graph-heading {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.graph-heading h1 {
  margin: 0;
  font-family: var(--display);
  font-size: 28px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.add-pop {
  position: fixed;
  z-index: 30;
  width: min(320px, calc(100vw - 24px));
  padding: 14px;
  border: 1px solid #e4d8c8;
  border-radius: 14px;
  background: #fffdf9;
  box-shadow: 0 16px 40px rgba(36, 28, 22, 0.18);
}

.add-pop-title {
  margin: 0 0 10px;
  font-size: 14px;
  line-height: 1.4;
}

.add-pop-title strong { font-weight: 700; }

.add-pop-form {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
}

.add-place-label {
  margin: 0;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.add-place {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.add-place .btn { flex: 1; }

.add-pop-form textarea {
  flex: 1;
  min-width: 0;
  min-height: 42px;
  max-height: 160px;
  resize: none;
  overflow: hidden;
  line-height: 1.4;
  border: 1px solid #ddcfbf;
  background: white;
  border-radius: 10px;
  padding: 10px 12px;
  outline: none;
}

.add-pop-form textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(29, 107, 74, 0.16);
}

#canvas {
  flex: 1;
  min-height: 0;
  overflow: auto;
  background-color: var(--canvas);
  background-image: radial-gradient(#ddd2c4 1.1px, transparent 1.1px);
  background-size: 22px 22px;
}

.tree-wrap, .empty-state {
  min-height: 100%;
  padding: 40px 48px;
  box-sizing: border-box;
}

.tree-wrap {
  display: flex;
  align-items: safe center;
  justify-content: safe flex-start;
  width: max-content;
  min-width: 100%;
}

.empty-state {
  display: grid;
  place-items: center;
}

.empty-card {
  width: min(420px, 100%);
  padding: 28px;
  border: 1px solid var(--card-border);
  border-radius: 18px;
  background: var(--card);
  box-shadow: var(--shadow);
}

.empty-card h2 {
  margin: 0 0 8px;
  font-family: var(--display);
  font-size: 32px;
  font-weight: 600;
}

.board {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

.column-backs {
  position: absolute;
  z-index: 0;
  left: 0;
  top: -14px;
  bottom: -22px;
  display: flex;
  pointer-events: none;
}

.column-back {
  width: var(--col);
  margin-right: var(--link);
  border-radius: 18px;
  flex: none;
}

.column-back:last-child { margin-right: 0; }

.level-row {
  position: relative;
  z-index: 1;
  display: flex;
  align-items: center;
  margin-bottom: 16px;
}

.level-tag {
  width: var(--col);
  flex: none;
  display: flex;
  justify-content: center;
}

.level-chip {
  border: 1px solid #d9cbbd;
  background: #fffdf9;
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: #5c5148;
}

.level-chip:hover { border-color: var(--accent); color: var(--accent); }

.level-form { width: min(140px, 100%); }

.level-form input {
  padding: 6px 8px;
  text-align: center;
  font-weight: 700;
}

.level-bridge {
  width: var(--link);
  flex: none;
}

.column-skip {
  width: var(--skip, 0px);
  flex: none;
}

.board > .row {
  position: relative;
  z-index: 1;
}

.row {
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

.kids, .node { flex-shrink: 0; }

.node.is-leaf { grid-template-columns: minmax(0, 1fr) auto; }

.kids {
  display: flex;
  flex-direction: column;
  margin-left: 28px;
  padding-left: 28px;
}

.kids > .row {
  position: relative;
  padding: 10px 0;
}

.kids > .row::before,
.kids > .row::after,
.node.has-children::after {
  content: "";
  position: absolute;
  background: var(--line);
}

.kids > .row::before {
  left: -28px;
  top: 0;
  bottom: 0;
  width: 2px;
}

.kids > .row:first-child::before { top: 50%; }
.kids > .row:last-child::before { bottom: 50%; }
.kids > .row:only-child::before { display: none; }

.kids > .row::after {
  left: -28px;
  top: calc(50% - 1px);
  width: calc(28px + var(--skip, 0px));
  height: 2px;
}

.node {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  width: var(--col);
  min-width: var(--col);
  max-width: var(--col);
  min-height: 58px;
  padding: 8px 8px 8px 10px;
  border: 1px solid var(--card-border);
  border-radius: 14px;
  background: var(--card);
  box-shadow: var(--shadow);
  text-align: left;
  cursor: default;
}

.node.is-dragging { opacity: 0.45; }

.node.is-drop {
  box-shadow: 0 0 0 3px rgba(29, 107, 74, 0.28), var(--shadow);
}

.node-sort { flex: none; }

.node.is-root .node-sort { color: var(--accent-ink); }

.node.is-root {
  background: var(--accent);
  border-color: #18583d;
  color: var(--accent-ink);
}

.node.is-adding,
.node.is-tagging,
.node.is-parenting {
  box-shadow: 0 0 0 3px rgba(29, 107, 74, 0.22), var(--shadow);
}

.node.has-children::after {
  left: 100%;
  top: calc(50% - 1px);
  width: 28px;
  height: 2px;
}

.role {
  display: block;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: rgba(244, 251, 247, 0.76);
}

.label {
  font-weight: 700;
  cursor: default;
}

.node-text {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.node-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.node-tag {
  max-width: 100%;
  padding: 2px 7px;
  border-radius: 999px;
  background: #efe4d6;
  color: var(--ink);
  font-size: 11px;
  font-weight: 700;
  line-height: 1.3;
  overflow-wrap: anywhere;
}

.node.is-root .node-tag {
  background: rgba(255, 255, 255, 0.18);
  color: var(--accent-ink);
}

.label.is-truncated { cursor: help; }

.label.is-truncated:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

.node.is-root .label.is-truncated:focus-visible { outline-color: var(--accent-ink); }

.label-tip {
  position: fixed;
  z-index: 35;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
  max-width: min(320px, calc(100vw - 24px));
  padding: 10px 12px;
  border: 1px solid #e4d8c8;
  border-radius: 12px;
  background: #fffdf9;
  color: var(--ink);
  box-shadow: 0 12px 32px rgba(36, 28, 22, 0.16);
  font-weight: 700;
  line-height: 1.4;
  white-space: normal;
  overflow-wrap: anywhere;
  pointer-events: none;
}

.label-tip.is-pinned { pointer-events: auto; }

.label-tip-text {
  margin: 0;
  overflow-wrap: anywhere;
}

.label-tip-actions {
  display: flex;
  gap: 6px;
}

.label-tip-tag,
.label-tip-copy { padding: 6px 12px; }

.node-actions, .heading-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.icon-btn, .chevron {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: 28px;
  height: 28px;
  border-radius: 8px;
  color: var(--muted);
}

.chevron {
  width: auto;
  padding: 0 6px;
  border: 0;
  background: #f3ebe1;
  color: var(--ink);
}

.node.is-root .chevron {
  background: rgba(255, 255, 255, 0.16);
  color: white;
}

.icon-btn:hover, .chevron:hover { background: #efe4d6; }

.node.is-root .icon-btn { color: var(--accent-ink); }
.node.is-root .icon-btn:hover { background: rgba(255, 255, 255, 0.14); }
.node.is-root .icon-btn.danger:hover { background: var(--danger); color: white; }

.node-menu {
  position: fixed;
  z-index: 30;
  min-width: 148px;
  padding: 6px;
  border: 1px solid #e4d8c8;
  border-radius: 12px;
  background: #fffdf9;
  box-shadow: 0 16px 40px rgba(36, 28, 22, 0.18);
}

.node-menu button {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 8px 10px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  font-weight: 650;
  text-align: left;
}

.node-menu button:hover { background: #f3ebe1; }

.node-menu button.danger { color: var(--danger); }

.node-menu button.danger:hover { background: #fdecea; }

.tag-pop,
.parent-pop {
  position: fixed;
  z-index: 30;
  width: min(280px, calc(100vw - 24px));
  padding: 14px;
  border: 1px solid #e4d8c8;
  border-radius: 14px;
  background: #fffdf9;
  box-shadow: 0 16px 40px rgba(36, 28, 22, 0.18);
}

.tag-pop {
  max-height: min(520px, calc(100vh - 24px));
  overflow: auto;
}

.pop-rule {
  border: 0;
  border-top: 1px solid #e4d8c8;
  margin: 14px 0 12px;
}

.status-label {
  margin: 0 0 8px;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.status-list { margin-bottom: 12px; }

.status-choice {
  border-color: transparent;
}

.tag-choice.status-choice.is-on {
  box-shadow: inset 0 0 0 2px #241c16;
}

.node.is-root .node-status {
  color: unset;
}

.tag-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 12px;
}

.tag-empty {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.4;
}

.tag-choice {
  max-width: 100%;
  padding: 6px 10px;
  border: 1px solid #e4d8c8;
  border-radius: 999px;
  background: white;
  font-size: 13px;
  font-weight: 700;
  line-height: 1.3;
  overflow-wrap: anywhere;
}

.tag-choice.is-on {
  background: var(--accent);
  border-color: #18583d;
  color: var(--accent-ink);
}

.parent-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  max-height: 240px;
  overflow: auto;
}

.parent-choice {
  width: 100%;
  padding: 8px 10px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  font-weight: 700;
  text-align: left;
  line-height: 1.3;
  overflow-wrap: anywhere;
}

.parent-choice:hover { background: #f3ebe1; }

.parent-choice.is-on,
.parent-choice.is-on:hover {
  background: var(--accent);
  color: var(--accent-ink);
}

.tag-create {
  display: flex;
  align-items: center;
  gap: 8px;
}

.tag-create input {
  flex: 1;
  min-width: 0;
  border: 1px solid #ddcfbf;
  background: white;
  border-radius: 10px;
  padding: 10px 12px;
  outline: none;
}

.tag-create input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(29, 107, 74, 0.16);
}

.page-wrap {
  min-height: 100%;
  padding: 40px 48px;
  box-sizing: border-box;
}

.tag-set-card {
  width: min(720px, 100%);
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 28px;
  border: 1px solid var(--card-border);
  border-radius: 18px;
  background: var(--card);
  box-shadow: var(--shadow);
}

.tag-groups {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.tag-group h2 {
  margin: 0 0 8px;
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
}

.tag-set-card .tag-list { margin-bottom: 0; }

.form-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.tag-set-picker {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}

.tag-set-picker select {
  width: min(280px, 46vw);
  font-weight: 650;
}

.empty-card .btn {
  display: inline-block;
  margin-top: 16px;
  text-align: center;
  text-decoration: none;
}

#arrange { display: none; }

#app.is-tagged #canvas { display: none; }

#app.is-tagged #arrange {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--sidebar);
}

.tagged-board {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 24px 28px 28px;
  box-sizing: border-box;
}

.tag-columns {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: stretch;
  gap: 16px;
  overflow-x: auto;
  overflow-y: hidden;
}

.tag-column {
  flex: 0 0 250px;
  width: 250px;
  min-height: 0;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--card-border);
  border-radius: 16px;
  background: var(--card);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.tag-column.is-dragging { opacity: 0.45; }

.tag-column.is-drop {
  box-shadow: 0 0 0 3px rgba(29, 107, 74, 0.28), var(--shadow);
}

.tag-column-head {
  flex: none;
  padding: 10px 10px 12px;
  border-bottom: 1px solid #eadfd2;
}

.tag-column-tools {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
  margin-bottom: 8px;
}

.drag-handle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 8px;
  color: var(--muted);
  cursor: grab;
}

.drag-handle:active { cursor: grabbing; }

.icon-btn:disabled {
  opacity: 0.35;
  cursor: default;
}

.tag-column-head h2 {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  line-height: 1.3;
  overflow-wrap: anywhere;
}

.tag-column-head p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}

.tag-column-body {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 8px;
}

.node-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.node-list li {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 4px 4px 10px;
  border-radius: 10px;
  font-weight: 650;
  line-height: 1.35;
  cursor: grab;
}

.node-list li + li { margin-top: 2px; }

.node-list li.is-dragging { opacity: 0.45; }

.node-list li.is-tagging {
  box-shadow: inset 0 0 0 2px rgba(29, 107, 74, 0.45);
}

.node-list li.is-drop { background: #e7f3ec; }

.node-list .node-label {
  flex: 1;
  min-width: 0;
  overflow-wrap: anywhere;
  cursor: pointer;
}

.node-graph {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
}

.node-order {
  display: flex;
  flex: none;
  gap: 2px;
}

.tag-column-body .tag-empty {
  padding: 8px 10px;
}

.icon {
  width: 16px;
  height: 16px;
  display: block;
}

.badge {
  min-width: 18px;
  padding: 0 5px;
  border-radius: 999px;
  background: #efe4d6;
  font-size: 11px;
  font-weight: 700;
}

.rename-form input {
  width: 140px;
  padding: 6px 8px;
}

.node .rename-form {
  flex-direction: column;
  align-items: stretch;
  min-width: 0;
}

.node .rename-form input {
  width: 100%;
  min-width: 0;
}

.node .rename-form .btn {
  padding: 6px 12px;
}

.rename-actions {
  display: flex;
  gap: 6px;
}

.node.is-root .rename-form .btn.primary {
  background: #fffdf9;
  color: #18583d;
}

.modal-back {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(36, 28, 22, 0.35);
}

.modal {
  width: min(420px, 100%);
  padding: 22px;
  border-radius: 16px;
  background: white;
  box-shadow: var(--shadow);
}

.modal p { margin: 0 0 16px; line-height: 1.45; }

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

#toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%) translateY(12px);
  max-width: min(460px, calc(100% - 32px));
  padding: 12px 14px;
  border-radius: 12px;
  background: #241c16;
  color: white;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.16s ease, transform 0.16s ease;
}

#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

@media (max-width: 800px) {
  #app { flex-direction: column; }
  #sidebar {
    width: 100%;
    max-height: 42vh;
    border-right: 0;
    border-bottom: 1px solid #e3d6c6;
  }
  #toolbar {
    align-items: stretch;
    flex-direction: column;
  }
  .tag-set-picker select { width: 100%; }
}
