:root {
  color-scheme: light;
  --bg: #f7f7f5;
  --text: #1f2933;
  --muted: #667085;
  --line: #8b45bd;
  --selected: #2563eb;
  --node: #9fb0c1;
  --panel: rgba(255, 255, 255, 0.86);
  --surface: #ffffff;
  --button: #ffffff;
  --border: rgba(0, 0, 0, 0.16);
  --shadow: 4px 5px 0 rgba(0, 0, 0, 0.22), 0 2px 6px rgba(0, 0, 0, 0.14);
  --node-icon-hover: #173f73;
  --node-icon-active: #9ed8ff;
}

body.dark {
  color-scheme: dark;
  --bg: #0f172a;
  --text: #e5e7eb;
  --muted: #a8b3c5;
  --line: #c084fc;
  --selected: #60a5fa;
  --node: #475569;
  --panel: rgba(15, 23, 42, 0.88);
  --surface: #111827;
  --button: #1e293b;
  --border: rgba(255, 255, 255, 0.16);
  --shadow: 4px 5px 0 rgba(0, 0, 0, 0.45), 0 2px 8px rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
}

body {
  margin: 0;
  overflow: hidden;
  color: var(--text);
  background: var(--bg);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
textarea {
  font: inherit;
}

button {
  color: var(--text);
  background: var(--button);
}

.toolbar {
  position: fixed;
  top: 14px;
  right: 14px;
  z-index: 20;
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  max-width: calc(100vw - 28px);
  padding: 7px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
  box-shadow: 0 2px 14px rgba(0, 0, 0, 0.16);
  backdrop-filter: blur(8px);
}

.toolbar button,
.dialog-actions button,
.icon-button {
  min-width: 36px;
  height: 36px;
  padding: 0 11px;
  border: 1px solid var(--border);
  border-radius: 9px;
  font-size: 13px;
  font-weight: 650;
  cursor: pointer;
}

.toolbar button:hover:not(:disabled),
.dialog-actions button:hover:not(:disabled),
.icon-button:hover:not(:disabled) {
  color: var(--line);
  border-color: var(--line);
}

button.primary {
  color: #fff;
  background: var(--line);
  border-color: var(--line);
}

button.primary:hover:not(:disabled) {
  color: #fff;
  filter: brightness(1.08);
}

button:disabled {
  opacity: 0.42;
  cursor: not-allowed;
}

.tree-area {
  position: relative;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  cursor: grab;
}

.tree-area.dragging {
  cursor: grabbing;
}

.tree-scaler {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  will-change: transform;
}

.tree-canvas {
  position: relative;
}

.lines-layer {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 0;
  overflow: visible;
  pointer-events: none;
}

.lines-layer line {
  stroke: var(--line);
  stroke-width: 2;
  stroke-linecap: round;
}

.node-wrap {
  position: absolute;
  z-index: 2;
  width: 180px;
  min-height: 72px;
}

.node-card {
  position: relative;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 180px;
  min-height: 72px;
  max-height: 96px;
  padding: 11px 15px;
  overflow: hidden;
  border: 1px solid rgba(0, 0, 0, 0.55);
  border-radius: 8px;
  color: #fff;
  background: var(--node);
  box-shadow: var(--shadow);
  font-weight: 750;
  line-height: 1.12;
  text-align: center;
  cursor: pointer;
  user-select: none;
}

.node-card:hover {
  outline: 3px solid color-mix(in srgb, var(--line) 18%, transparent);
}

.node-card.selected {
  border-color: var(--selected);
  outline: 4px solid color-mix(in srgb, var(--selected) 34%, transparent);
}

.node-title {
  width: 100%;
  max-height: 72px;
  overflow: hidden;
  overflow-wrap: anywhere;
  white-space: normal;
  font-size: 22px;
}

.depth-0 .node-title { font-size: 28px; }
.depth-1 .node-title { font-size: 30px; }
.depth-3 .node-title,
.depth-4 .node-title,
.depth-5 .node-title { font-size: 18px; }

.node-action {
  position: absolute;
  top: -16px;
  z-index: 4;
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  padding: 0;
  overflow: visible;
  border: 0;
  border-radius: 50%;
  color: var(--line);
  background: #ffffff;
  box-shadow: none;
  line-height: 1;
  cursor: pointer;
  opacity: 0;
  transform: scale(0.9);
  transition:
    opacity 120ms,
    transform 120ms,
    color 120ms;
}

.node-action:hover,
.node-action:focus-visible {
  color: var(--node-icon-hover);
  background: #ffffff;
  outline: none;
}

.node-action:active {
  color: var(--node-icon-active);
  background: #ffffff;
}

.edit-text {
  left: -16px;
}

.edit-text svg,
.add-child svg {
  display: block;
  width: 22px;
  height: 22px;
  overflow: visible;
  color: inherit;
  flex: none;
}

.add-child {
  right: -16px;
}

.node-wrap:hover .node-action,
.node-wrap:focus-within .node-action,
.node-wrap:has(.node-card.selected) .node-action,
.node-action:hover {
  opacity: 1;
  transform: scale(1);
}

.edit-input {
  width: 100%;
  padding: 5px 6px;
  border: 0;
  border-radius: 5px;
  outline: 0;
  color: #fff;
  background: rgba(15, 23, 42, 0.45);
  font: inherit;
  text-align: center;
}

.modal[hidden] {
  display: none;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 24px;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.56);
  backdrop-filter: blur(3px);
}

.text-dialog {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  width: min(760px, calc(100vw - 32px));
  max-height: min(82vh, 820px);
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.42);
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--border);
}

.dialog-header h2 {
  min-width: 0;
  margin: 0;
  overflow-wrap: anywhere;
  font-size: 20px;
  line-height: 1.25;
}

.icon-button {
  flex: 0 0 auto;
  padding: 0;
  font-size: 25px;
  line-height: 1;
}

.node-text-input {
  display: block;
  width: 100%;
  min-height: 360px;
  height: min(58vh, 600px);
  padding: 18px 20px;
  resize: none;
  overflow: auto;
  border: 0;
  outline: 0;
  color: var(--text);
  background: var(--surface);
  font-size: 16px;
  line-height: 1.55;
  white-space: pre-wrap;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 9px;
  padding: 14px 20px 18px;
  border-top: 1px solid var(--border);
}


.versions-dialog {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  width: min(720px, calc(100vw - 32px));
  max-height: min(84vh, 820px);
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.42);
}

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

.version-create {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.version-create label {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

.version-create-row {
  display: flex;
  gap: 9px;
}

.version-create-row input {
  min-width: 0;
  flex: 1;
  height: 38px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 9px;
  outline: 0;
  color: var(--text);
  background: var(--button);
}

.version-create-row input:focus {
  border-color: var(--line);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--line) 18%, transparent);
}

.version-create-row button,
.version-open-button,
.version-overwrite-button,
.version-delete-button {
  height: 38px;
  padding: 0 13px;
  border: 1px solid var(--border);
  border-radius: 9px;
  font-size: 13px;
  font-weight: 650;
  cursor: pointer;
}

.versions-list {
  display: flex;
  flex-direction: column;
  gap: 9px;
  min-height: 150px;
  padding: 16px 20px 20px;
  overflow: auto;
}

.versions-loading,
.versions-empty {
  padding: 24px 8px;
  color: var(--muted);
  text-align: center;
}

.version-item {
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: 66px;
  padding: 12px 13px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--button);
}

.version-item.main-version {
  border-color: color-mix(in srgb, var(--line) 52%, var(--border));
  background: color-mix(in srgb, var(--line) 13%, var(--surface));
}

.version-info {
  min-width: 0;
  flex: 1;
}

.version-name-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.version-name {
  min-width: 0;
  overflow: hidden;
  font-weight: 750;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.version-badge {
  flex: 0 0 auto;
  padding: 2px 7px;
  border-radius: 999px;
  color: var(--line);
  background: color-mix(in srgb, var(--line) 13%, transparent);
  font-size: 11px;
  font-weight: 750;
}

.version-date {
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.version-open-button:hover:not(:disabled),
.version-create-row button:hover:not(:disabled) {
  color: var(--line);
  border-color: var(--line);
}

.version-open-button:disabled {
  opacity: 0.55;
  cursor: default;
}

@media (max-width: 760px) {
  .toolbar {
    top: 10px;
    right: 10px;
    left: 10px;
  }

  .modal {
    padding: 12px;
  }

  .text-dialog,
  .versions-dialog {
    width: 100%;
    max-height: 88vh;
  }

  .node-text-input {
    min-height: 300px;
    height: 62vh;
  }

  .version-create-row {
    flex-direction: column;
  }

  .version-create-row button {
    width: 100%;
  }
}

/* Version actions */
.version-actions {
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  gap: 7px;
}

.version-open-button,
.version-overwrite-button,
.version-delete-button {
  height: 38px;
  padding: 0 13px;
  border: 1px solid var(--border);
  border-radius: 9px;
  font-size: 13px;
  font-weight: 650;
  cursor: pointer;
}

.version-overwrite-button {
  color: #fff;
  border-color: var(--line);
  background: var(--line);
}

.version-overwrite-button:hover:not(:disabled) {
  color: #fff;
  filter: brightness(1.08);
}

.version-delete-button,
button.danger {
  color: #b42318;
  border-color: color-mix(in srgb, #b42318 40%, var(--border));
  background: var(--button);
}

.version-delete-button:hover:not(:disabled),
button.danger:hover:not(:disabled) {
  color: #8f1d14;
  border-color: #b42318;
  background: color-mix(in srgb, #b42318 8%, var(--button));
}

.version-item.current-version {
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--selected) 52%, transparent);
}

.choice-dialog {
  position: relative;
  z-index: 1;
  width: min(460px, calc(100vw - 32px));
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.42);
}

.choice-message {
  margin: 0;
  padding: 18px 20px 22px;
  color: var(--text);
  line-height: 1.5;
  white-space: pre-wrap;
}

.duplicate-modal {
  z-index: 100;
}

@media (max-width: 760px) {
  .version-item {
    align-items: stretch;
    flex-direction: column;
  }

  .version-actions {
    width: 100%;
  }

  .version-actions button {
    flex: 1;
  }
}


/* Save confirmation */
.save-choice-modal {
  z-index: 90;
}

.save-choice-actions {
  flex-wrap: wrap;
}

#saveAsVersionBtn {
  color: var(--line);
  border-color: color-mix(in srgb, var(--line) 55%, var(--border));
}

#saveAsVersionBtn:hover:not(:disabled) {
  color: var(--line);
  border-color: var(--line);
  background: color-mix(in srgb, var(--line) 8%, var(--button));
}

@media (max-width: 560px) {
  .save-choice-actions {
    align-items: stretch;
    flex-direction: column-reverse;
  }

  .save-choice-actions button {
    width: 100%;
  }
}


/* Focus Mode control */
#exitFocusBtn[hidden] {
  display: none;
}
