/* The org view — a board's agents laid out as a company.
 *
 * Loads after agents.css. Nothing here restyles a tile: a node in the tree
 * wraps the same .cell (toolbar + tile + unlock overlay) the grid draws, so the
 * two views are one product in two arrangements. This file owns the tree —
 * the rows, the connectors, the team roll-up strip under a lead — and the
 * view switch above both views.
 *
 * Connector lines are borders on pseudo-elements, the classic CSS org-chart
 * construction: a vertical stub from a lead down to its row, a horizontal rail
 * across the row, a stub from the rail down to each report. No SVG, no
 * measuring, so it survives a live repaint and a window resize untouched.
 */

:root {
    --org-line: rgba(255, 255, 255, 0.16);
    --org-drop: rgba(79, 195, 247, 0.75);
    --org-node-w: 236px;
    --org-rise: 26px;   /* lead → rail */
    --org-fall: 26px;   /* rail → report */
    /* The cell's hover toolbar (agents.css: 24px + 6px margin) sits between
       the rail and the tile. The stub runs through it, or the connector looks
       broken by exactly that much. */
    --org-toolbar: 30px;
}

/* ── The switch ─────────────────────────────────────────────────────────── */
.board-head {
    max-width: 980px;
    margin: 0 auto 14px;
    display: flex;
    align-items: center;
    gap: 14px;
    flex-wrap: wrap;
}

.board-views {
    display: inline-flex;
    border: 1px solid var(--ad-border);
    border-radius: 7px;
    overflow: hidden;
    background: rgba(255, 255, 255, 0.02);
}

.board-view {
    appearance: none;
    background: transparent;
    border: none;
    padding: 6px 13px;
    font-family: var(--ad-mono);
    font-size: 10px;
    letter-spacing: 0.16em;
    text-transform: uppercase;
    color: rgba(255, 255, 255, 0.4);
    cursor: pointer;
    transition: color 0.15s, background 0.15s;
}

.board-view + .board-view { border-left: 1px solid var(--ad-border); }
.board-view:hover { color: var(--ad-text-2); }
.board-view.is-active { color: var(--ad-text); background: rgba(255, 255, 255, 0.07); }
.board-view[disabled] { cursor: default; opacity: 0.6; }
.board-view:focus-visible { outline: 2px solid var(--ad-running); outline-offset: -2px; }

.board-head-hint {
    font-size: 12px;
    line-height: 1.5;
    color: var(--ad-dim);
}

.board-head-hint code {
    font-family: var(--ad-mono);
    font-size: 11px;
    color: var(--ad-text-2);
    background: rgba(255, 255, 255, 0.05);
    padding: 1px 5px;
    border-radius: 4px;
}

/* The tree needs room the grid does not. The grid stays at 980 (agents.css);
   the org view opens the page out and scrolls sideways inside .org beyond
   that — the body never scrolls horizontally. */
.page:has(.org),
.page:has(.board-head--org) { max-width: 1440px; }
.page:has(.org) .board-head { max-width: none; }

/* ── The tree ───────────────────────────────────────────────────────────── */
.org {
    overflow-x: auto;
    overflow-y: visible;
    padding: 6px 0 28px;
    -webkit-overflow-scrolling: touch;
}

/* Inline-block so the scroll width is the tree's width, centred when it is
   narrower than the page. */
.org-scroll {
    display: inline-block;
    min-width: 100%;
    text-align: center;
}

.org-level {
    list-style: none;
    margin: 0;
    padding: var(--org-rise) 0 0;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    position: relative;
}

.org-level--root { padding-top: 0; }

/* Lead → rail: one vertical stub from the centre of the lead's cell. The row
   (ul) is inside the lead's node and centred on it, so 50% of the row is the
   centre of the lead. */
.org-level:not(.org-level--root)::before {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    width: 1px;
    height: var(--org-rise);
    background: var(--org-line);
}

.org-node {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: var(--org-fall) 8px 0;
    text-align: left;
}

.org-level--root > .org-node { padding-top: 0; }

/* The rail, in two halves so the ends of a row can drop theirs. */
.org-node::before,
.org-node::after {
    content: '';
    position: absolute;
    top: 0;
    width: 50%;
    height: var(--org-fall);
    border-top: 1px solid var(--org-line);
    pointer-events: none;
}

.org-node::before { left: 0; }
.org-node::after { right: 0; }
.org-node--first::before { border-top-color: transparent; }
.org-node--last::after { border-top-color: transparent; }
.org-level--root > .org-node::before,
.org-level--root > .org-node::after { display: none; }

/* Rail → report: the stub down to the TILE, through the toolbar strip. The
   toolbar's chips have their own backgrounds, so on hover they sit over the
   line rather than under it. */
.org-cell { position: relative; width: var(--org-node-w); }

.org-level:not(.org-level--root) > .org-node > .org-cell::before {
    content: '';
    position: absolute;
    top: calc(-1 * var(--org-fall));
    left: 50%;
    width: 1px;
    height: calc(var(--org-fall) + var(--org-toolbar));
    background: var(--org-line);
    pointer-events: none;
}

/* A cell in the tree is a grab handle. The tile's own controls keep their
   cursors. */
.org-cell[draggable="true"] { cursor: grab; }
.org-cell[draggable="true"]:active { cursor: grabbing; }
.org-cell .cell-tile { transition: outline-color 0.12s, box-shadow 0.12s; outline: 2px dashed transparent; outline-offset: 2px; }

/* ── The team strip under a lead ────────────────────────────────────────── */
/* The roll-up: how many report here and the team's WORST status, the same
   rule the badge and the cluster tile use. Reads --ad-c from its
   data-status, like every other status-coloured thing on the site. */
.org-team {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-top: 6px;
    padding: 5px 10px 5px 9px;
    border: 1px solid var(--ad-border);
    border-left: 3px solid var(--ad-c, var(--ad-unknown));
    border-radius: 8px;
    background: var(--ad-surface);
    font-family: var(--ad-mono);
    font-size: 10px;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--ad-dim);
}

.org-team-count {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
}

.org-team .ad-status--compact { flex-shrink: 0; }

/* ── Gaps: the drop slots between siblings ──────────────────────────────── */
/* At rest a gap is the spacing between nodes and carries the rail across.
   While something is being dragged it widens into a visible slot. */
.org-gap {
    position: relative;
    width: 6px;
    flex-shrink: 0;
    align-self: flex-start;
    /* Rail → tile → strip: about one node tall, so a slot is a slot and not
       a column running the height of the tree. */
    height: calc(var(--org-fall) + var(--org-toolbar) + 200px + 40px);
    border-radius: 8px;
    transition: width 0.12s, background 0.12s;
}

.org-gap::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    border-top: 1px solid var(--org-line);
    pointer-events: none;
}

.org-gap--first::before,
.org-gap--last::before,
.org-level--root > .org-gap::before { display: none; }

/* Rows with no parent have no rail, but their first/last gaps still exist as
   drop slots — a tree needs somewhere to drop "make this top level". */
body.org-dragging .org-gap {
    width: 30px;
    margin: 0 2px;
    border: 1px dashed rgba(255, 255, 255, 0.18);
    background: rgba(255, 255, 255, 0.025);
}

body.org-dragging .org-gap.is-over {
    border-color: var(--org-drop);
    background: rgba(79, 195, 247, 0.12);
}

/* Drop on a cell: the cell is the target, so the tile lights up. */
body.org-dragging .org-cell.is-over .cell-tile {
    outline-color: var(--org-drop);
    box-shadow: 0 0 0 6px rgba(79, 195, 247, 0.12);
}

/* The team being moved, and therefore everything that cannot receive it. */
.org-node.is-dragging > .org-cell { opacity: 0.45; }
.org-node--inert > .org-cell .cell-tile { opacity: 0.55; }

/* Nothing on the page should pick up the drag as text selection. */
body.org-dragging { user-select: none; -webkit-user-select: none; }

/* ── Toolbar additions in the org view ──────────────────────────────────── */
/* Same sleeping-until-hover rule as the other toolbar chips (agents.css). The
   select sits left, the grip and the others right. */
.cell-mgr {
    appearance: none;
    -webkit-appearance: none;
    margin-right: auto;
    max-width: 54%;
    padding: 3px 18px 3px 8px;
    background: rgba(6, 6, 12, 0.72)
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16' width='10' height='10'%3E%3Cpath fill='%23888d99' d='M3.3 5.8a1 1 0 011.4 0L8 9.1l3.3-3.3a1 1 0 111.4 1.4l-4 4a1 1 0 01-1.4 0l-4-4a1 1 0 010-1.4z'/%3E%3C/svg%3E")
        no-repeat right 6px center;
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 6px;
    font-family: var(--ad-mono);
    font-size: 9.5px;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: rgba(255, 255, 255, 0.5);
    cursor: pointer;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.cell-mgr:hover, .cell-mgr:focus { color: var(--ad-text); border-color: rgba(79, 195, 247, 0.45); outline: none; }
.cell-mgr option { background: #0c0c16; color: var(--ad-text); text-transform: none; letter-spacing: 0; }

.cell-grip {
    display: inline-flex;
    align-items: center;
    padding: 3px 7px;
    margin-right: 6px;
    background: rgba(6, 6, 12, 0.72);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 6px;
    color: rgba(255, 255, 255, 0.42);
    cursor: grab;
    font-size: 11px;
    line-height: 1;
}

.cell-grip:active { cursor: grabbing; }

/* ── Small screens: the tree turns on its side ──────────────────────────── */
/* A horizontal org chart on a phone is a scroll hunt. Under 640px the same
   markup renders as an indented outline — reports stacked under their lead,
   each level stepped in, with a rail down the left and a stub into each
   cell. Same nodes, same cells, same selects; only the connectors change.
   The select is the arrange control here; there is no drag on touch. */
@media (max-width: 640px) {
    .org { overflow-x: hidden; padding-bottom: 16px; }
    .org-scroll { display: block; min-width: 0; text-align: left; }

    .org-level {
        flex-direction: column;
        align-items: stretch;
        padding: 0 0 0 16px;
    }
    .org-level--root { padding-left: 0; }

    /* The rail: down the left of a lead's reports. It is painted from the
       top of the row; the last report's node covers what runs past its
       stub (below). */
    .org-level:not(.org-level--root)::before {
        top: 0;
        bottom: 0;
        left: 0;
        width: 1px;
        height: auto;
    }

    .org-node { padding: 10px 0 0; align-items: stretch; }
    .org-level--root > .org-node { padding-top: 0; }
    .org-level--root > .org-node + .org-gap + .org-node { padding-top: 14px; }
    .org-node::after { display: none; }

    /* The stub: rail → cell, at the height of the tile's name row. */
    .org-cell { width: auto; }
    .org-level:not(.org-level--root) > .org-node > .org-cell::before {
        top: calc(var(--org-toolbar) + 22px);
        left: -16px;
        width: 16px;
        height: 1px;
    }

    /* The last report paints over the rail below its own stub, so the rail
       ends where the team does rather than running on under it. */
    .org-node::before { display: none; }
    .org-level:not(.org-level--root) > .org-node--last::before {
        display: block;
        top: calc(10px + var(--org-toolbar) + 23px);
        bottom: 0;
        left: -16px;
        width: 1px;
        height: auto;
        border: none;
        background: var(--ad-bg);
    }

    .org-gap { width: auto; height: 4px; min-height: 0; align-self: stretch; }
    .org-gap::before { display: none; }
    body.org-dragging .org-gap { width: auto; height: 28px; margin: 2px 0; }

    .org-cell .cell-tile { height: 170px; }
    .board-head { gap: 10px; }
    .board-head-hint { flex-basis: 100%; }
}

@media (prefers-reduced-motion: reduce) {
    .org-gap, .org-cell .cell-tile, .board-view { transition: none; }
}
