:root {
  color-scheme: light;
  --bg: #f5f7fb;
  --panel: #ffffff;
  --ink: #162033;
  --muted: #667085;
  --line: #d8dee9;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #d92d20;
  --shadow: 0 18px 50px rgba(25, 35, 60, 0.12);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
}

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

.auth-screen {
  display: grid;
  min-height: 100%;
  place-items: center;
  background:
    linear-gradient(rgba(245, 247, 251, 0.88), rgba(245, 247, 251, 0.92)),
    linear-gradient(rgba(37, 99, 235, 0.08) 1px, transparent 1px),
    linear-gradient(90deg, rgba(37, 99, 235, 0.08) 1px, transparent 1px);
  background-size: auto, 32px 32px, 32px 32px;
  padding: 24px;
}

.login-card {
  display: grid;
  width: min(100%, 390px);
  gap: 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: var(--shadow);
  padding: 22px;
}

.login-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 6px;
}

.login-header strong,
.login-header span {
  display: block;
}

.login-header span:not(.brand-mark) {
  margin-top: 3px;
  color: var(--muted);
  font-size: 13px;
}

.login-error {
  margin: 0;
  border: 1px solid rgba(217, 45, 32, 0.22);
  border-radius: 7px;
  background: #fff1f0;
  color: var(--danger);
  padding: 9px 10px;
  font-size: 13px;
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  color: var(--ink);
  padding: 0 12px;
  cursor: pointer;
}

button:hover {
  border-color: #9aa7bd;
}

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

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

.app-shell {
  display: grid;
  grid-template-rows: 64px minmax(0, 1fr);
  height: 100%;
}

.app-shell.viewer-mode {
  grid-template-rows: minmax(0, 1fr);
}

.viewer-mode .topbar,
.viewer-mode .map-directory,
.viewer-mode .inspector,
.viewer-mode .canvas-help {
  display: none !important;
}

.viewer-mode .workspace {
  grid-template-columns: minmax(0, 1fr);
}

.viewer-mode .canvas-panel {
  padding: 0;
}

.viewer-mode .canvas {
  border: 0;
  border-radius: 0;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.92);
  padding: 0 18px;
  backdrop-filter: blur(10px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 220px;
}

.brand-mark {
  display: grid;
  width: 36px;
  height: 36px;
  place-items: center;
  border-radius: 8px;
  background: #111827;
  color: #fff;
  font-weight: 800;
}

.brand strong,
.brand span {
  display: block;
  line-height: 1.2;
}

.brand span:not(.brand-mark) {
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
}

.toolbar {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  min-width: 0;
  overflow-x: auto;
  padding: 8px 0;
}

.toolbar-separator {
  width: 1px;
  height: 24px;
  background: var(--line);
  flex: 0 0 auto;
}

.readonly-badge {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  border: 1px solid #b7c6df;
  border-radius: 999px;
  background: #eef4ff;
  color: #1d4ed8;
  padding: 0 10px;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}

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

.map-directory {
  overflow-y: auto;
  border-right: 1px solid var(--line);
  background: var(--panel);
  padding: 16px;
}

.directory-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 14px;
}

.directory-header h2 {
  margin: 0;
  font-size: 14px;
}

.map-list {
  display: grid;
  gap: 8px;
  margin-top: 12px;
}

.backup-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-auto-rows: 38px;
  gap: 8px;
  margin-top: 12px;
}

.backup-actions button,
.file-import-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 38px;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  color: var(--ink);
  padding: 0 10px;
  cursor: pointer;
  font-size: 13px;
  text-align: center;
}

.backup-actions button:hover,
.file-import-button:hover {
  border-color: #9aa7bd;
}

.backup-actions button:disabled,
.file-import-button.is-disabled {
  cursor: not-allowed;
  opacity: 0.46;
}

.backup-actions button:last-of-type {
  grid-column: 1 / -1;
}

.data-status {
  margin-top: 10px;
}

.color-palette-field {
  display: grid;
  gap: 8px;
  margin-bottom: 12px;
  color: #344054;
  font-size: 13px;
}

.color-palette {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 7px;
}

.color-swatch {
  width: 100%;
  min-height: 30px;
  border: 1px solid rgba(15, 23, 42, 0.18);
  border-radius: 7px;
  padding: 0;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.48);
}

.color-swatch:hover {
  border-color: #64748b;
}

.color-swatch.is-selected {
  border-color: var(--primary);
  box-shadow:
    0 0 0 3px rgba(37, 99, 235, 0.22),
    inset 0 0 0 1px rgba(255, 255, 255, 0.58);
}

.map-list-item {
  display: grid;
  gap: 3px;
  width: 100%;
  min-height: auto;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  padding: 10px;
  text-align: left;
}

.map-list-item.is-active {
  border-color: var(--primary);
  background: #eef4ff;
}

.map-list-item strong {
  overflow: hidden;
  color: var(--ink);
  font-size: 13px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.map-list-item span {
  color: var(--muted);
  font-size: 12px;
}

.canvas-panel {
  min-width: 0;
  min-height: 0;
  padding: 16px;
}

.canvas {
  position: relative;
  height: 100%;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background-color: #f9fbff;
  background-image:
    linear-gradient(rgba(37, 99, 235, 0.08) 1px, transparent 1px),
    linear-gradient(90deg, rgba(37, 99, 235, 0.08) 1px, transparent 1px);
  background-size: 32px 32px;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.9);
  cursor: grab;
}

.canvas:active {
  cursor: grabbing;
}

.world {
  position: absolute;
  top: 0;
  left: 0;
  width: 5000px;
  height: 3600px;
  transform-origin: 0 0;
  will-change: transform;
}

.world.is-zooming {
  transition: transform 120ms ease-out;
}

.edges-layer,
.nodes-layer {
  position: absolute;
  inset: 0;
}

.edges-layer {
  inset: auto;
  left: -100000px;
  top: -100000px;
  width: 200000px;
  height: 200000px;
  overflow: visible;
}

.nodes-layer {
  pointer-events: none;
}

.edge-path {
  cursor: pointer;
  fill: none;
  pointer-events: stroke;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.edge-path.is-selected {
  filter: drop-shadow(0 0 4px rgba(37, 99, 235, 0.45));
}

.edge-hitbox {
  cursor: pointer;
  fill: none;
  pointer-events: stroke;
  stroke: transparent;
  stroke-width: 18;
}

.edge-label {
  cursor: pointer;
  overflow: visible;
  pointer-events: auto;
}

.edge-label-box {
  display: grid;
  width: 100%;
  height: 100%;
  place-items: center;
  border: 1px solid rgba(15, 23, 42, 0.12);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.94);
  box-shadow: 0 8px 18px rgba(15, 23, 42, 0.1);
  color: #263244;
  font-size: 12px;
  font-weight: 700;
  line-height: 1.2;
  padding: 0 10px;
  text-align: center;
}

.edge-label.is-selected .edge-label-box {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.18);
}

.node {
  position: absolute;
  display: grid;
  align-items: center;
  width: 184px;
  min-height: 76px;
  padding: 14px 16px;
  border: 2px solid rgba(15, 23, 42, 0.18);
  box-shadow: 0 10px 24px rgba(15, 23, 42, 0.12);
  color: #111827;
  pointer-events: auto;
  cursor: move;
  user-select: none;
  overflow: visible;
}

.node-content {
  min-width: 0;
  overflow-wrap: anywhere;
}

.node.has-tree-list {
  align-items: start;
  padding: 14px;
  text-align: left;
}

.node.has-tree-list .node-content {
  display: grid;
  width: 100%;
  gap: 10px;
}

.node--pill.has-tree-list,
.node--circle.has-tree-list {
  border-radius: 18px;
  place-items: stretch;
}

.node:hover {
  border-color: rgba(37, 99, 235, 0.55);
}

.node.is-selected {
  outline: 3px solid rgba(37, 99, 235, 0.24);
  border-color: var(--primary);
}

.node--rounded {
  border-radius: 8px;
}

.node--pill {
  min-height: 72px;
  border-radius: 999px;
  text-align: center;
}

.node--circle {
  width: 136px;
  height: 136px;
  min-height: 136px;
  border-radius: 50%;
  text-align: center;
  place-items: center;
}

.node--note {
  border-radius: 6px 20px 6px 6px;
}

.node--note::after {
  position: absolute;
  top: 0;
  right: 0;
  width: 24px;
  height: 24px;
  border-radius: 0 18px 0 5px;
  background: rgba(255, 255, 255, 0.42);
  content: "";
}

.node--folder {
  align-items: center;
  border-radius: 7px 8px 8px 8px;
  padding-top: 18px;
}

.node--folder::before {
  position: absolute;
  top: -14px;
  left: -2px;
  width: 88px;
  height: 18px;
  border: 2px solid rgba(15, 23, 42, 0.18);
  border-bottom: 0;
  border-radius: 9px 9px 0 0;
  background: inherit;
  content: "";
}

.node--folder::after {
  position: absolute;
  top: 2px;
  left: 0;
  right: 0;
  height: 12px;
  border-radius: 7px 7px 0 0;
  background: rgba(255, 255, 255, 0.22);
  content: "";
  pointer-events: none;
}

.node h3,
.node p {
  margin: 0;
}

.node h3 {
  font-size: 18px;
  line-height: 1.25;
}

.node p,
.node a {
  font-size: 14px;
  line-height: 1.45;
}

.node a {
  color: #0f3f99;
  font-weight: 700;
  text-decoration: none;
  word-break: break-word;
}

.node a:hover {
  text-decoration: underline;
}

.node-tree {
  width: 100%;
  min-width: 0;
  border-top: 1px solid rgba(15, 23, 42, 0.14);
  padding-top: 9px;
  font-size: 13px;
  line-height: 1.35;
  text-align: left;
}

.node-tree-list {
  position: relative;
  display: grid;
  gap: 5px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.node-tree-list .node-tree-list {
  margin: 5px 0 0 17px;
  border-left: 1px dotted rgba(71, 85, 105, 0.5);
  padding-left: 18px;
}

.node-tree-item {
  position: relative;
  min-width: 0;
}

.node-tree-list .node-tree-list > .node-tree-item::before {
  position: absolute;
  top: 12px;
  left: -18px;
  width: 13px;
  border-top: 1px dotted rgba(71, 85, 105, 0.5);
  content: "";
}

.node-tree-row {
  display: flex;
  align-items: flex-start;
  gap: 7px;
  min-width: 0;
  min-height: 20px;
}

.node-tree-icon {
  position: relative;
  flex: 0 0 10px;
  width: 8px;
  height: 8px;
  margin-top: 6px;
  border: 1px solid rgba(96, 116, 146, 0.58);
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.78);
}

.node-tree-row.has-children .node-tree-icon {
  flex-basis: 15px;
  width: 15px;
  height: 13px;
  margin-top: 3px;
  border-color: rgba(180, 133, 20, 0.62);
  border-radius: 3px;
  background: linear-gradient(#fde68a, #facc15);
  box-shadow: none;
}

.node-tree-row.has-children .node-tree-icon::before {
  position: absolute;
  top: -4px;
  left: 1px;
  width: 8px;
  height: 5px;
  border: 1px solid rgba(180, 133, 20, 0.5);
  border-bottom: 0;
  border-radius: 3px 3px 0 0;
  background: #fde68a;
  content: "";
}

.node-tree-label {
  min-width: 0;
  color: #1f2937;
  font-size: 13px;
  font-weight: 600;
  overflow-wrap: anywhere;
}

a.node-tree-label {
  color: #0f3f99;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.node-link-button {
  position: absolute;
  right: 12px;
  bottom: 10px;
  min-height: 39px;
  border: 1px solid rgba(37, 99, 235, 0.34);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.94);
  color: #1d4ed8;
  padding: 0 14px;
  box-shadow: 0 5px 14px rgba(15, 23, 42, 0.14);
  cursor: pointer;
  font-size: 14px;
  font-weight: 700;
  white-space: nowrap;
}

.node-link-button:hover {
  border-color: var(--primary);
  background: #eef4ff;
}

.node-note-toggle {
  position: absolute;
  top: 8px;
  right: 8px;
  min-height: 36px;
  border: 1px solid rgba(100, 116, 139, 0.24);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.9);
  color: #475467;
  padding: 0 12px;
  font-size: 13px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
}

.node-note-toggle:hover {
  border-color: rgba(37, 99, 235, 0.5);
  color: #1d4ed8;
}

.node-note {
  position: absolute;
  left: 0;
  top: calc(100% + 10px);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-width: 180px;
  min-height: 80px;
  border: 1px solid rgba(15, 23, 42, 0.16);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.98);
  box-shadow: 0 12px 28px rgba(15, 23, 42, 0.14);
  color: #263244;
  cursor: default;
  padding: 10px 12px;
  transform: none;
  z-index: 4;
}

.node-note-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 24px;
}

.node-note strong {
  display: block;
  color: #344054;
  font-size: 12px;
}

.node-note-close {
  display: inline-grid;
  width: 24px;
  min-width: 24px;
  height: 24px;
  min-height: 24px;
  place-items: center;
  border: 1px solid rgba(100, 116, 139, 0.22);
  border-radius: 50%;
  background: #fff;
  color: #475467;
  padding: 0;
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
}

.node-note-close:hover {
  border-color: var(--primary);
  color: var(--primary);
}

.node-note p {
  overflow: auto;
  margin: 0;
  color: #263244;
  font-size: 13px;
  line-height: 1.45;
  white-space: pre-wrap;
}

.node-note-resize {
  position: absolute;
  right: -6px;
  bottom: -6px;
  width: 14px;
  height: 14px;
  border: 2px solid #fff;
  border-radius: 50%;
  background: #64748b;
  box-shadow: 0 4px 10px rgba(15, 23, 42, 0.22);
  cursor: nwse-resize;
}

.node-resize-handle {
  position: absolute;
  right: -6px;
  bottom: -6px;
  width: 16px;
  height: 16px;
  border: 2px solid #fff;
  border-radius: 50%;
  background: var(--primary);
  box-shadow: 0 4px 10px rgba(15, 23, 42, 0.22);
  cursor: nwse-resize;
}

.node-resize-handle:hover {
  background: var(--primary-dark);
}

.node.is-readonly .node-resize-handle {
  display: none;
}

.inspector {
  overflow-y: auto;
  border-left: 1px solid var(--line);
  background: var(--panel);
  padding: 16px;
}

.panel-section {
  border-bottom: 1px solid var(--line);
  padding: 0 0 18px;
  margin-bottom: 18px;
}

.panel-section:last-child {
  border-bottom: 0;
  margin-bottom: 0;
}

.panel-section h2 {
  margin: 0 0 12px;
  font-size: 14px;
  letter-spacing: 0;
}

.muted {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
}

.readonly-notice {
  margin-top: 10px;
}

label {
  display: grid;
  gap: 7px;
  margin-bottom: 12px;
  color: #344054;
  font-size: 13px;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  color: var(--ink);
  padding: 10px 11px;
}

input[type="color"] {
  height: 42px;
  padding: 4px;
}

textarea {
  min-height: 92px;
  resize: vertical;
}

.tree-editor-field {
  display: grid;
  gap: 9px;
  margin-top: 4px;
}

.tree-editor-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  color: #344054;
  font-size: 13px;
}

.tree-editor-header > span {
  font-weight: 700;
}

.tree-editor-header button {
  min-height: 32px;
  padding: 0 10px;
  white-space: nowrap;
}

.tree-editor {
  display: grid;
  gap: 8px;
}

.tree-editor-group {
  display: grid;
  gap: 8px;
  min-width: 0;
}

.tree-editor-item {
  display: grid;
  grid-template-columns: 14px minmax(0, 1fr);
  gap: 6px;
  align-items: start;
  margin-left: calc(var(--tree-depth, 0) * 18px);
}

.tree-editor-branch {
  grid-row: 1;
  width: 14px;
  height: 100%;
  min-height: 76px;
  border-left: 1px dotted rgba(71, 85, 105, 0.46);
  border-bottom: 1px dotted rgba(71, 85, 105, 0.46);
}

.tree-editor-main {
  grid-column: 2;
  min-width: 0;
}

.tree-editor-title-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 5px;
  align-items: center;
  min-width: 0;
}

.tree-editor-title-row input {
  min-width: 0;
  padding: 8px 9px;
  font-size: 12px;
}

.tree-editor-actions {
  display: flex;
  justify-content: flex-end;
  gap: 5px;
  margin-top: 5px;
}

.tree-link-button,
.tree-icon-button {
  display: inline-grid;
  place-items: center;
  height: 34px;
  min-height: 34px;
  border-radius: 7px;
  padding: 0;
  font-size: 12px;
  font-weight: 800;
  white-space: nowrap;
}

.tree-link-button {
  min-width: 54px;
  padding: 0 8px;
  color: #1d4ed8;
}

.tree-link-button.is-active {
  border-color: rgba(37, 99, 235, 0.42);
  background: #eef4ff;
  color: #1d4ed8;
}

.tree-icon-button {
  width: 34px;
  font-size: 16px;
}

.tree-icon-button.danger {
  border-color: rgba(217, 45, 32, 0.24);
  color: var(--danger);
}

.status-grid {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  color: var(--muted);
  font-size: 13px;
}

.status-grid strong {
  color: var(--ink);
}

.context-menu {
  position: fixed;
  z-index: 20;
  min-width: 178px;
  overflow: hidden;
  border: 1px solid rgba(15, 23, 42, 0.12);
  border-radius: 8px;
  background: #fff;
  box-shadow: var(--shadow);
  padding: 6px;
}

.context-menu button {
  display: block;
  width: 100%;
  min-height: 34px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  text-align: left;
}

.context-menu button:hover {
  background: #eef4ff;
}

.context-menu button.danger {
  color: var(--danger);
}

.note-editor-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  background: rgba(15, 23, 42, 0.28);
  padding: 20px;
}

.note-editor-card {
  display: grid;
  width: min(92vw, 620px);
  gap: 12px;
  border: 1px solid rgba(15, 23, 42, 0.14);
  border-radius: 8px;
  background: #fff;
  box-shadow: var(--shadow);
  padding: 18px;
}

.note-editor-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.note-editor-header h2 {
  margin: 0;
  font-size: 16px;
}

.note-editor-header button {
  display: inline-grid;
  width: 34px;
  min-width: 34px;
  height: 34px;
  min-height: 34px;
  place-items: center;
  padding: 0;
  font-size: 18px;
}

.note-editor-card textarea {
  min-height: 240px;
  resize: vertical;
}

.note-editor-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.canvas-help {
  position: absolute;
  left: 16px;
  bottom: 14px;
  border: 1px solid rgba(15, 23, 42, 0.1);
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.9);
  color: var(--muted);
  padding: 8px 10px;
  font-size: 12px;
  pointer-events: none;
}

@media (max-width: 900px) {
  body {
    overflow: auto;
  }

  .app-shell {
    min-height: 100%;
    height: auto;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
    height: auto;
    padding: 12px;
  }

  .toolbar {
    justify-content: flex-start;
    width: 100%;
  }

  .workspace {
    grid-template-columns: 1fr;
  }

  .map-directory {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .canvas-panel {
    height: 66vh;
    padding: 12px;
  }

  .viewer-mode .canvas-panel {
    height: 100vh;
    padding: 0;
  }

  .inspector {
    border-left: 0;
    border-top: 1px solid var(--line);
  }
}
