:root {
  color-scheme: light;
  --bg: #fff;
  --panel: #ffffff;
  --ink: #20201d;
  --muted: #67655c;
  --line: #ddd7cb;
  --accent: #19706b;
  --accent-ink: #ffffff;
  --danger: #a33a2c;
  --node: #f1f2ef;
  --node-alt: #f6f7f5;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
  margin: 0;
  overflow: hidden;
}

body {
  background: var(--bg);
  color: var(--ink);
  touch-action: none;
}

body.modal-open {
  overflow: hidden;
}

button,
input,
textarea {
  font: inherit;
}

button {
  min-height: 38px;
  border: 1px solid var(--ink);
  border-radius: 6px;
  background: var(--ink);
  color: #fff;
  padding: 0 13px;
  cursor: pointer;
  white-space: nowrap;
}

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

input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  background-clip: padding-box;
  color: var(--ink);
  outline: 1px solid transparent;
  outline-offset: -1px;
}

input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(25, 112, 107, 0.16);
}

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

.login-view {
  position: relative;
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  overflow: hidden;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.58), rgba(244, 248, 246, 0.72)),
    #f5f8f6;
}

.login-backdrop {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

.story-map {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  opacity: 0.7;
}

.story-map-desktop {
  width: 1100px;
  height: 760px;
}

.story-map-mobile {
  display: none;
}

.story-lines {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.story-edge {
  fill: none;
  stroke: rgba(25, 112, 107, 0.38);
  stroke-width: 2.4;
  stroke-linecap: round;
}

.story-card {
  position: absolute;
  min-width: 130px;
  min-height: 46px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 18px;
  border: 1.3px solid rgba(25, 112, 107, 0.4);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.96);
  color: #254744;
  font-size: 17px;
  font-weight: 800;
  line-height: 1;
  text-align: center;
  white-space: nowrap;
  box-shadow: 0 10px 18px rgba(31, 30, 25, 0.09);
  will-change: transform;
  backface-visibility: hidden;
}

.story-root,
.story-simple {
  min-width: 220px;
  min-height: 52px;
  border-color: rgba(25, 112, 107, 0.5);
  border-radius: 12px;
  background: rgba(238, 249, 246, 0.97);
}

.story-node-a,
.story-node-e {
  border-color: rgba(38, 83, 154, 0.42);
  color: #2f456c;
}

.story-node-b {
  min-width: 116px;
  border-color: rgba(156, 100, 32, 0.42);
  color: #6b4b22;
}

.story-node-d {
  min-width: 116px;
  border-color: rgba(96, 96, 96, 0.38);
  color: #4b4c49;
}

.login-panel {
  position: relative;
  z-index: 1;
  width: min(420px, 100%);
  display: grid;
  gap: 20px;
  padding: 28px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 24px 70px rgba(31, 30, 25, 0.12);
}

.login-title {
  display: flex;
  align-items: center;
  gap: 12px;
}

.login-title img {
  width: 42px;
  height: 42px;
  display: block;
  flex: 0 0 auto;
  object-fit: contain;
}

.login-panel h1 {
  margin: 0;
  font-size: 34px;
  line-height: 1;
}

.muted,
.status {
  color: var(--muted);
}

.status {
  min-height: 18px;
  font-size: 13px;
}

.status.error {
  color: var(--danger);
  font-weight: 700;
}

label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.login-panel input {
  height: 44px;
  padding: 0 12px;
  background: #fff;
  border-color: var(--line);
  outline-color: var(--line);
  -webkit-appearance: none;
  appearance: none;
}

.login-panel input::placeholder {
  color: #9b968b;
}

.login-panel input:-webkit-autofill,
.login-panel input:-webkit-autofill:hover,
.login-panel input:-webkit-autofill:focus {
  -webkit-text-fill-color: var(--ink);
  border: 1px solid var(--line) !important;
  outline: 1px solid var(--line);
  outline-offset: -1px;
  -webkit-box-shadow:
    0 0 0 1000px #fff inset,
    0 0 0 1px var(--line) inset;
  box-shadow:
    0 0 0 1000px #fff inset,
    0 0 0 1px var(--line) inset;
  caret-color: var(--ink);
  transition: background-color 9999s ease-in-out 0s;
}

.workspace {
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  min-width: 0;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 16px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.9);
}

.brand {
  min-width: 170px;
  display: flex;
  align-items: baseline;
  gap: 10px;
}

.brand-logo {
  width: 24px;
  height: 24px;
  display: block;
  flex: 0 0 auto;
  object-fit: contain;
}

.brand h1 {
  margin: 0;
  font-size: 24px;
  line-height: 1;
}

.map-nav {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 2px;
}

.map-nav-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-left: auto;
  flex-shrink: 0;
}

.map-tabs-wrapper {
  min-width: 0;
  display: flex;
  align-items: center;
  flex: 1;
  gap: 6px;
}

.map-tabs {
  min-width: 0;
  display: flex;
  gap: 6px;
  overflow: hidden;
  flex: 1;
}

.map-tabs-overflow {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  position: relative;
}

.map-overflow-toggle {
  width: 30px;
  height: 30px;
  min-width: 30px;
  min-height: 30px;
  display: inline-grid;
  place-items: center;
  padding: 0;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}

.map-overflow-toggle:hover,
.map-overflow-toggle:focus-visible {
  border-color: var(--accent);
  background: #eaf5ef;
}

.map-overflow-menu {
  position: fixed;
  z-index: 999;
  min-width: 180px;
  max-width: min(320px, calc(100vw - 24px));
  max-height: 280px;
  overflow-y: auto;
  padding: 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  box-shadow: 0 12px 32px rgba(31, 30, 25, 0.18);
}

.map-overflow-count {
  padding: 4px 10px 8px;
  margin-bottom: 4px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
  text-align: right;
}

.map-overflow-menu button {
  width: 100%;
  min-height: 34px;
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 4px 10px;
  border: none;
  background: transparent;
  color: var(--ink);
  border-radius: 4px;
  text-align: left;
}

.map-overflow-menu button:hover {
  background: #eaf5ef;
}

.map-overflow-menu button.active {
  background: #d0ebe3;
  font-weight: 700;
}

.map-tab {
  min-height: 36px;
  flex-shrink: 0;
  white-space: nowrap;
  border-color: transparent;
  background: transparent;
  color: var(--ink);
}

.map-tab.active {
  border-color: rgba(25, 112, 107, 0.22);
  background: #eaf5ef;
  color: #174f4b;
}

.map-tab.truncated {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.new-map-button {
  width: 30px;
  height: 30px;
  min-width: 30px;
  min-height: 30px;
  display: inline-grid;
  place-items: center;
  padding: 0;
  border-radius: 999px;
  border-color: var(--accent);
  background: var(--accent);
  color: var(--accent-ink);
  flex-shrink: 0;
}

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

.ghost-button {
  background: transparent;
  color: var(--ink);
  border-color: var(--line);
}

.raw-toggle-button {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.raw-toggle-button::after {
  content: "";
  position: absolute;
  left: -12px;
  top: 50%;
  width: 8px;
  height: 8px;
  display: none;
  border-radius: 999px;
  background: var(--danger);
  box-shadow: 0 0 0 2px #fff;
  transform: translateY(-50%);
}

.raw-toggle-button.attention::after {
  display: block;
}

.raw-toggle-button.active {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--accent-ink);
}

.map-stage {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: auto;
  background: #fff;
  overscroll-behavior: contain;
  touch-action: none;
  cursor: grab;
}

.map-stage.panning {
  cursor: grabbing;
  user-select: none;
}

.raw-stage {
  min-width: 0;
  min-height: 0;
  padding: 18px;
  background: #fff;
}

.raw-editor {
  width: 100%;
  height: 100%;
  min-height: 0;
  resize: none;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  font: 14px/1.55 ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  white-space: pre;
  overflow: auto;
  outline: 1px solid transparent;
  outline-offset: -1px;
  tab-size: 2;
}

.raw-editor:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(25, 112, 107, 0.16);
}

#map-svg {
  display: block;
  min-width: 100%;
  min-height: 100%;
}

.edge {
  fill: none;
  stroke: #aab8b5;
  stroke-width: 2;
}

.edge.new {
  animation: edge-arrive 460ms ease-out both;
}

.node rect {
  fill: var(--node);
  stroke: #cbd6d3;
  stroke-width: 1.2;
}

.node.depth-0 rect {
  fill: var(--node);
  stroke: #cbd6d3;
}

.node.depth-1 rect {
  fill: var(--node-alt);
}

.node.selected rect {
  stroke: #20201d;
  stroke-width: 2.4;
}

.node.new {
  animation: node-bounce 520ms cubic-bezier(.2, 1.45, .32, 1) both;
  transform-box: fill-box;
  transform-origin: center;
}

.node-input {
  height: 100%;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--ink);
  padding: 2px 6px;
  font-size: 13px;
  font-weight: 700;
  line-height: 18px;
  text-align: center;
  cursor: pointer;
  overflow: hidden;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.node-display {
  display: flex;
  align-items: center;
  justify-content: center;
}

.node-editing {
  cursor: text;
  outline: 0;
  caret-color: var(--ink);
}

.node.depth-0 .node-input {
  color: var(--ink);
  font-size: 16px;
  font-weight: 800;
}

.node-input:focus {
  box-shadow: none;
  background: rgba(255, 255, 255, 0.28);
  cursor: text;
}

.node:not(.depth-0) .node-input:focus {
  background: #fff;
}

.node-actions {
  position: fixed;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 8px;
  transform: translate(-50%, -100%);
  padding: 6px;
  border: 1px solid rgba(32, 32, 29, 0.08);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 12px 32px rgba(31, 30, 25, 0.18);
}

.node-tooltip {
  position: fixed;
  z-index: 19;
  max-width: min(360px, calc(100vw - 32px));
  transform: translate(-50%, -100%);
  padding: 9px 11px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.96);
  color: var(--ink);
  font-size: 13px;
  font-weight: 700;
  line-height: 1.35;
  box-shadow: 0 12px 30px rgba(31, 30, 25, 0.16);
  white-space: normal;
  overflow-wrap: anywhere;
  pointer-events: none;
}

.node-action {
  width: 40px;
  height: 40px;
  min-height: 40px;
  display: inline-grid;
  place-items: center;
  padding: 0;
  border-radius: 999px;
  border: 1px solid transparent;
  color: #fff;
  box-shadow: 0 7px 18px rgba(31, 30, 25, 0.14);
}

.node-action.add {
  background: #19706b;
  border-color: #19706b;
}

.node-action.edit {
  background: #eef0ee;
  border-color: #d8ddd9;
  color: #343a37;
}

.node-action.delete {
  background: var(--danger);
  border-color: var(--danger);
}

.node-action:disabled {
  opacity: 0.35;
}

@keyframes node-bounce {
  0% {
    opacity: 0;
    transform: scale(.64);
  }
  55% {
    opacity: 1;
    transform: scale(1.08);
  }
  78% {
    transform: scale(.97);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

@keyframes edge-arrive {
  0% {
    opacity: 0;
    stroke-dasharray: 1;
    stroke-dashoffset: 1;
  }
  100% {
    opacity: 1;
    stroke-dasharray: 1;
    stroke-dashoffset: 0;
  }
}

.modal {
  width: min(420px, calc(100vw - 32px));
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
  color: var(--ink);
  background: #fff;
  box-shadow: 0 24px 70px rgba(31, 30, 25, 0.18);
}

.modal::backdrop {
  background: rgba(32, 32, 29, 0.28);
}

.modal form {
  display: grid;
  gap: 18px;
  padding: 22px;
}

.modal h2 {
  margin: 0;
  font-size: 22px;
}

.modal input {
  height: 42px;
  padding: 0 12px;
}

.modal-actions {
  justify-content: flex-end;
}

.confirm-overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(32, 32, 29, 0.32);
  touch-action: none;
}

.confirm-modal {
  width: min(460px, 100%);
  display: grid;
  gap: 22px;
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  box-shadow: 0 24px 70px rgba(31, 30, 25, 0.22);
}

.confirm-modal h2 {
  margin: 0;
  font-size: 22px;
  line-height: 1.2;
}

.confirm-actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.confirm-actions button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.delete-confirm-button {
  border-color: var(--danger);
  background: var(--danger);
  color: #fff;
}

@media (max-width: 820px) {
  .login-view {
    align-items: center;
    padding: 18px;
  }

  .story-map {
    opacity: 0.62;
  }

  .story-map-desktop {
    display: none;
  }

  .story-map-mobile {
    display: block;
    width: 390px;
    height: calc(100vh - 18px);
  }

  .story-map-mobile [data-from="simple"],
  .story-map-mobile [data-story-node="simple"],
  .story-map-mobile [data-story-node="plain"],
  .story-map-mobile [data-story-node="anywhere"] {
    display: none;
  }

  .story-card {
    min-width: 112px;
    min-height: 44px;
    padding: 0 14px;
    font-size: 15px;
  }

  .story-root,
  .story-simple {
    min-width: 196px;
    min-height: 48px;
  }

  .login-panel {
    padding: 24px;
  }

  .login-title img {
    width: 38px;
    height: 38px;
  }

  .topbar {
    grid-template-columns: 1fr;
    align-items: stretch;
  }

  .brand {
    flex-wrap: wrap;
  }

  .map-nav {
    flex-wrap: wrap;
  }

  .map-nav-actions {
    margin-left: 0;
  }
}
