/* Mind map: a canvas filling the screen with the controls floating over it,
   the maps panel to its left (over it on a phone), and the outline panel to
   its right (below it on a phone).
   Every colour is a token holding a light and a dark value. The theme button
   sets data-theme on <html>, which overrides the system preference. */

:root {
    color-scheme: light dark;
    --bg: light-dark(#f4f5f7, #121418);
    --dot: light-dark(#d3d7dd, #2b3038);
    --surface: light-dark(#ffffff, #1d2026);
    --hover: light-dark(#eef0f3, #2a2e36);
    --text: light-dark(#1d2025, #e7e9ec);
    --muted: light-dark(#69707a, #9aa1ab);
    --border: light-dark(#dde1e6, #343943);
    --edge: light-dark(#a9b0ba, #5b626d);
    --accent: light-dark(#3a5ee0, #7f9cff);
    --warning: light-dark(#a15c07, #f2b24c);
    --shadow-color: light-dark(rgb(15 20 30 / 0.08), rgb(0 0 0 / 0.4));
    --shadow: 0 1px 2px var(--shadow-color), 0 6px 20px var(--shadow-color);
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] { color-scheme: dark; }

* { box-sizing: border-box; }

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

body {
    overflow: hidden;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
}

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

/* --- Layout ------------------------------------------------------------- */

.app {
    position: fixed;
    inset: 0;
    display: flex;
}

/* The canvas and everything floating over it. Its width drives the toolbar's
   compact mode, so that also kicks in when the outline panel takes the room. */
.stage {
    position: relative;
    flex: 1;
    min-width: 0;
    overflow: hidden;
    container-type: inline-size;
}

/* --- Canvas ------------------------------------------------------------- */

.canvas {
    position: absolute;
    inset: 0;
    overflow: hidden;
    /* Every touch gesture is handled in app.js: drag, pan, pinch. */
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    /* Size and position follow the camera; set by view.js. */
    background-image: radial-gradient(circle, var(--dot) 1px, transparent 1.5px);
    background-size: 24px 24px;
}

.canvas.is-panning { cursor: grabbing; }
.canvas.is-dragging, .canvas.is-dragging .node { cursor: grabbing; }

.edges {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    overflow: visible;
    pointer-events: none;
}

/* A zero-size anchor at world (0, 0); nodes hang off it by transform. */
.nodes {
    position: absolute;
    left: 0;
    top: 0;
    transform-origin: 0 0;
}

/* --- Nodes -------------------------------------------------------------- */

.node {
    position: absolute;
    left: 0;
    top: 0;
    /* Inside a zero-width parent, an absolutely positioned box would shrink to its
       narrowest word. max-content keeps it on one line until max-width wraps it. */
    width: max-content;
    min-width: 44px;
    max-width: 280px;
    padding: 8px 14px;
    background: var(--surface);
    border: 1.5px solid color-mix(in srgb, var(--branch, var(--edge)) 60%, var(--border));
    border-radius: 10px;
    box-shadow: 0 1px 2px var(--shadow-color);
    font-size: 14px;
    line-height: 1.4;
    text-align: center;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.node.is-root {
    padding: 12px 20px;
    background: var(--text);
    color: var(--bg);
    border-color: var(--text);
    border-radius: 14px;
    font-size: 16px;
    font-weight: 600;
}

/* Not attached to anything yet. */
.node.is-loose { border-style: dashed; }

/* Has a description, which a click on the node opens. */
.node.has-description::after {
    content: "";
    position: absolute;
    top: -4px;
    right: -4px;
    width: 10px;
    height: 10px;
    border: 2px solid var(--bg);
    border-radius: 50%;
    background: var(--branch, var(--accent));
}

.node.is-selected {
    outline: 2px solid var(--accent);
    outline-offset: 3px;
}

.node.is-target {
    outline: 2px dashed var(--accent);
    outline-offset: 5px;
}

.node__label {
    min-height: 1.4em;
    outline: none;
}

.node.is-editing .node__label {
    cursor: text;
    user-select: text;
    -webkit-user-select: text;
}

/* --- Lines -------------------------------------------------------------- */

.edge path {
    fill: none;
    stroke: var(--branch, var(--edge));
    stroke-width: 2;
    stroke-linecap: round;
}

/* --- Toolbars ----------------------------------------------------------- */

.toolbar {
    position: absolute;
    display: flex;
    align-items: center;
    gap: 2px;
    padding: 4px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: var(--shadow);
}

.toolbar--main {
    top: max(12px, env(safe-area-inset-top));
    left: 50%;
    transform: translateX(-50%);
    max-width: calc(100% - 24px);
    overflow-x: auto;
    scrollbar-width: none;
}

.toolbar--zoom {
    right: max(12px, env(safe-area-inset-right));
    bottom: max(12px, env(safe-area-inset-bottom));
}

.toolbar button {
    flex: none;
    display: grid;
    place-items: center;
    width: 36px;
    height: 36px;
    padding: 0;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--text);
    font: inherit;
    cursor: pointer;
}

.toolbar button:hover { background: var(--hover); }

.toolbar button:disabled {
    background: transparent;
    opacity: 0.35;
    cursor: default;
}

.toolbar button[aria-pressed="true"] {
    background: color-mix(in srgb, var(--accent) 16%, transparent);
    color: var(--accent);
}

.toolbar .toolbar__level {
    width: 52px;
    font-size: 12px;
    font-variant-numeric: tabular-nums;
}

.toolbar__sep {
    flex: none;
    align-self: stretch;
    width: 1px;
    margin: 6px 4px;
    background: var(--border);
}

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

/* --- Description card --------------------------------------------------- */

/* Hangs under its node (above, when there is more room there), and moves with
   it when the map is panned or zoomed. Sized for reading: a line of about 70
   characters. description.js sets its max-height to the room on that side. */
.description {
    position: absolute;
    left: 0;
    top: 0;
    display: flex;
    flex-direction: column;
    width: min(560px, calc(100% - 24px));
    padding: 16px 20px 18px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    box-shadow: var(--shadow);
}

.description[hidden] { display: none; }
.description:focus-within { border-color: var(--accent); }

/* The pointer to the node: a square turned on its corner, half of it showing. */
.description::before {
    content: "";
    position: absolute;
    top: -7px;
    left: var(--pointer-x, 50%);
    width: 12px;
    height: 12px;
    margin-left: -6px;
    background: inherit;
    border: inherit;
    border-right: 0;
    border-bottom: 0;
    border-radius: 2px 0 0 0;
    transform: rotate(45deg);
}

.description.is-above::before {
    top: auto;
    bottom: -7px;
    border: inherit;
    border-top: 0;
    border-left: 0;
    border-radius: 0 0 2px 0;
}

.description__path {
    margin: 0 36px 2px 0;
    overflow: hidden;
    color: var(--muted);
    font-size: 12px;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.description__path[hidden] { display: none; }

.description__title {
    margin: 0 36px 10px 0;
    font-size: 17px;
    font-weight: 600;
    line-height: 1.3;
}

.description__close {
    position: absolute;
    top: 10px;
    right: 10px;
    display: grid;
    place-items: center;
    width: 32px;
    height: 32px;
    padding: 0;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
}

.description__close:hover {
    background: var(--hover);
    color: var(--text);
}

/* Reads as plain text until it is clicked. description.js grows it with its
   content, and only past the card's max-height lets it scroll. */
.description__text {
    display: block;
    flex: 0 1 auto;
    min-height: 1.65em;
    width: 100%;
    margin: 0;
    padding: 0;
    border: 0;
    outline: none;
    resize: none;
    overflow-y: hidden;
    background: transparent;
    color: var(--text);
    font: inherit;
    font-size: 15.5px;
    line-height: 1.65;
}

.description__text::placeholder { color: var(--muted); }

.description__text.is-scrolling {
    overflow-y: auto;
    padding-right: 8px;
}

/* --- Scrollbars --------------------------------------------------------- */

/* Thin, no track, a soft rounded thumb: also where the system is set to
   always show scrollbars. The -webkit- rules are for Safari. */
.description__text,
.outline__text,
.maps__list,
.help__body {
    scrollbar-width: thin;
    scrollbar-color: color-mix(in srgb, var(--muted) 45%, transparent) transparent;
}

:is(.description__text, .outline__text, .maps__list, .help__body)::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

:is(.description__text, .outline__text, .maps__list, .help__body)::-webkit-scrollbar-track {
    background: transparent;
}

:is(.description__text, .outline__text, .maps__list, .help__body)::-webkit-scrollbar-thumb {
    border-radius: 3px;
    background: color-mix(in srgb, var(--muted) 45%, transparent);
}

/* --- Side panels: maps and outline --------------------------------------- */

.side__head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 10px 6px 18px;
}

.side__title {
    margin: 0;
    font-size: 13px;
    font-weight: 600;
}

.side__close {
    display: grid;
    place-items: center;
    width: 32px;
    height: 32px;
    padding: 0;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
}

.side__close:hover {
    background: var(--hover);
    color: var(--text);
}

/* --- Maps panel --------------------------------------------------------- */

.maps {
    display: flex;
    flex-direction: column;
    flex: none;
    width: clamp(220px, 20vw, 280px);
    border-right: 1px solid var(--border);
    background: var(--surface);
    /* All buttons; a long press on a phone should not select their text. */
    user-select: none;
    -webkit-user-select: none;
}

.maps[hidden] { display: none; }

.maps__new {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 4px 10px 8px;
    padding: 8px 10px;
    border: 1px dashed var(--border);
    border-radius: 8px;
    background: transparent;
    color: var(--text);
    font: inherit;
    font-size: 13px;
    cursor: pointer;
}

.maps__new:hover { background: var(--hover); }

.maps__list {
    flex: 1;
    min-height: 0;
    margin: 0;
    padding: 0 6px 12px;
    overflow-y: auto;
    list-style: none;
}

.maps__item {
    display: flex;
    align-items: center;
    border-radius: 8px;
}

.maps__item:hover { background: var(--hover); }
.maps__item.is-current { background: color-mix(in srgb, var(--accent) 12%, transparent); }

.maps__open {
    flex: 1;
    min-width: 0;
    display: grid;
    gap: 1px;
    padding: 7px 4px 7px 12px;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--text);
    font: inherit;
    text-align: left;
    cursor: pointer;
}

.maps__name {
    overflow: hidden;
    font-size: 13.5px;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.is-current .maps__name {
    color: var(--accent);
    font-weight: 600;
}

.maps__meta {
    color: var(--muted);
    font-size: 11.5px;
}

/* Shown on hover, so a list of maps does not read as a list of delete buttons. */
.maps__delete {
    flex: none;
    display: grid;
    place-items: center;
    width: 30px;
    height: 30px;
    margin-right: 4px;
    padding: 0;
    border: 0;
    border-radius: 6px;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
    opacity: 0;
}

.maps__item:hover .maps__delete,
.maps__item:focus-within .maps__delete { opacity: 1; }

.maps__delete:hover {
    background: color-mix(in srgb, var(--text) 10%, transparent);
    color: var(--text);
}

.maps__hint {
    margin: 0;
    padding: 8px 18px 12px;
    border-top: 1px solid var(--border);
    color: var(--muted);
    font-size: 12px;
}

/* --- Outline panel ------------------------------------------------------ */

.outline {
    display: flex;
    flex-direction: column;
    flex: none;
    width: clamp(300px, 30vw, 420px);
    border-left: 1px solid var(--border);
    background: var(--surface);
}

.outline[hidden] { display: none; }

.outline__text {
    flex: 1;
    min-height: 0;
    width: 100%;
    margin: 0;
    padding: 6px 18px 18px;
    border: 0;
    outline: none;
    resize: none;
    background: transparent;
    color: var(--text);
    font: 13px/1.65 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    tab-size: 2;
}

.outline__status {
    min-height: 1.5em;
    margin: 0;
    padding: 6px 18px 0;
    border-top: 1px solid var(--border);
    color: var(--warning);
    font-size: 12px;
}

.outline__hint {
    margin: 0;
    padding: 4px 18px 12px;
    color: var(--muted);
    font-size: 12px;
}

/* --- Hint, toast, help -------------------------------------------------- */

.hint {
    position: absolute;
    left: 16px;
    bottom: 20px;
    margin: 0;
    color: var(--muted);
    font-size: 12px;
    pointer-events: none;
}

kbd {
    display: inline-block;
    min-width: 1.6em;
    padding: 1px 5px;
    border: 1px solid var(--border);
    border-bottom-width: 2px;
    border-radius: 5px;
    background: var(--surface);
    color: var(--text);
    font: 11px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
    text-align: center;
}

.toast {
    position: absolute;
    left: 50%;
    bottom: 72px;
    max-width: calc(100% - 32px);
    padding: 8px 14px;
    border-radius: 8px;
    background: var(--text);
    color: var(--bg);
    font-size: 13px;
    opacity: 0;
    transform: translate(-50%, 8px);
    transition: opacity 0.18s, transform 0.18s;
    pointer-events: none;
}

.toast.is-visible {
    opacity: 1;
    transform: translate(-50%, 0);
}

.help {
    width: min(540px, calc(100vw - 32px));
    padding: 0;
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--surface);
    color: var(--text);
    box-shadow: var(--shadow);
}

.help::backdrop { background: rgb(0 0 0 / 0.35); }

/* The dialog itself has no padding, so a click that lands on it is a click on the backdrop. */
.help__body {
    max-height: min(80vh, 680px);
    padding: 20px 24px 16px;
    overflow: auto;
    font-size: 13.5px;
    line-height: 1.5;
}

.help h2 {
    margin: 0 0 4px;
    font-size: 17px;
}

.help h3 {
    margin: 18px 0 8px;
    color: var(--muted);
    font-size: 11.5px;
    font-weight: 600;
    letter-spacing: 0.05em;
    text-transform: uppercase;
}

.help dl {
    display: grid;
    /* The same minimum in every list, so the descriptions line up from one section to the next. */
    grid-template-columns: minmax(7.5rem, max-content) 1fr;
    gap: 6px 16px;
    margin: 0;
}

.help dd {
    margin: 0;
    color: var(--muted);
}

.help code {
    font: 12px ui-monospace, SFMono-Regular, Menlo, monospace;
    color: var(--text);
}

.help ul {
    margin: 0;
    padding-left: 18px;
    color: var(--muted);
}

.help li + li { margin-top: 4px; }

.help__note {
    margin: 18px 0 0;
    color: var(--muted);
}

.help__actions {
    display: flex;
    justify-content: flex-end;
    margin-top: 16px;
}

.help__actions button {
    padding: 7px 16px;
    border: 0;
    border-radius: 8px;
    background: var(--text);
    color: var(--bg);
    font: inherit;
    font-weight: 500;
    cursor: pointer;
}

/* --- Small and touch screens -------------------------------------------- */

@container (max-width: 640px) {
    .hint { display: none; }
}

@media (pointer: coarse) {
    .hint,
    .outline__hint { display: none; }
    /* iOS zooms the page into any text field set below 16px when it gets focus. */
    .outline__text,
    .description__text { font-size: 16px; }
}

/* Fit all twelve buttons across a phone; narrower screens still scroll the bar sideways. */
@container (max-width: 480px) {
    .toolbar--main {
        gap: 0;
        padding: 3px;
        max-width: calc(100% - 16px);
    }
    .toolbar--main .toolbar__sep { display: none; }
    .toolbar--main button { width: 30px; }
}

/* On a phone the outline sits under the map instead of beside it,
   and the maps cover the whole screen until one is picked. */
@media (max-width: 720px) {
    .app { flex-direction: column; }
    .outline {
        width: auto;
        height: 42%;
        border-top: 1px solid var(--border);
        border-left: 0;
    }
    .maps {
        position: absolute;
        inset: 0;
        z-index: 10;
        width: auto;
        padding-top: env(safe-area-inset-top);
        border-right: 0;
    }
}

@media (pointer: coarse) {
    .maps__delete { opacity: 1; }
}
