/* ── Theme Variables ── */
:root {
    --bg-primary: #080f1e;
    --bg-card: linear-gradient(160deg, rgba(14, 28, 52, 0.95), rgba(8, 16, 32, 0.95));
    --bg-input: #0c1a30;
    --bg-hover: #162d4a;
    --text-primary: #f0f4f8;
    --text-secondary: #d4dfe8;
    --text-muted: #9bbcd6;
    --text-placeholder: #5a7a96;
    --accent: #4da3ff;
    --accent-glow: rgba(77, 163, 255, 0.5);
    --accent-soft: rgba(77, 163, 255, 0.2);
    --border: #1e3a5c;
    --border-hover: #4da3ff;
    --border-subtle: #1a3a5c;
    --connector: #2a8fc2;
    --avatar-bg: linear-gradient(135deg, #1a4a6e, #0f3050);
    --avatar-border: #3a9fd4;
    --avatar-glow: rgba(58, 159, 212, 0.3);
    --badge-bg: #1a3a5c;
    --badge-border: #2a5a8c;
    --badge-text: #4da3ff;
    --worksite-text: #5a8ab0;
    --shadow-card: rgba(0, 0, 0, 0.5);
    --shadow-card-hover: rgba(77, 163, 255, 0.2);
    --dept-text: #8aaec8;
}

[data-theme="light"] {
    --bg-primary: #f4f6f9;
    --bg-card: linear-gradient(160deg, #ffffff, #f8fafc);
    --bg-input: #ffffff;
    --bg-hover: #e8edf4;
    --text-primary: #0f1720;
    --text-secondary: #2d3d4f;
    --text-muted: #3d5168;
    --text-placeholder: #94a3b8;
    --accent: #2563eb;
    --accent-glow: rgba(37, 99, 235, 0.35);
    --accent-soft: rgba(37, 99, 235, 0.1);
    --border: #d1d9e6;
    --border-hover: #2563eb;
    --border-subtle: #dfe5ee;
    --connector: #93b4d4;
    --avatar-bg: linear-gradient(135deg, #dbeafe, #bfdbfe);
    --avatar-border: #60a5fa;
    --avatar-glow: rgba(96, 165, 250, 0.25);
    --badge-bg: #e0ecf7;
    --badge-border: #b8d0e8;
    --badge-text: #2563eb;
    --worksite-text: #6b8299;
    --shadow-card: rgba(0, 0, 0, 0.06);
    --shadow-card-hover: rgba(37, 99, 235, 0.12);
    --dept-text: #4a6580;
}

/* ── Base ── */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
body {
    font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg-primary);
    color: var(--text-secondary);
    min-height: 100vh;
    overflow-x: auto;
    padding: 0 8px 8px;
    transition: background 0.3s, color 0.3s;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}
h1 {
    text-align: center;
    font-size: 22px;
    font-weight: 700;
    letter-spacing: -0.3px;
    margin-bottom: 14px;
    color: var(--accent);
}

/* ── Top bar ── */
.top-bar {
    position: sticky;
    top: 0;
    z-index: 100;
    background: var(--bg-primary);
    padding: 10px 0 14px;
    transition: background 0.3s;
}
.controls {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin-bottom: 20px;
}
.controls button {
    font-size: 12px;
    font-weight: 500;
    color: var(--accent);
    background: var(--bg-input);
    padding: 7px 14px;
    border-radius: 6px;
    border: 1px solid var(--border-subtle);
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}
.controls button:hover {
    background: var(--bg-hover);
    border-color: var(--accent);
}

/* ── Theme toggle ── */
.theme-toggle {
    font-size: 12px;
    font-weight: 500;
    color: var(--accent);
    background: var(--bg-input);
    padding: 7px 14px;
    border-radius: 6px;
    border: 1px solid var(--border-subtle);
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}
.theme-toggle:hover {
    background: var(--bg-hover);
    border-color: var(--accent);
}

/* ── Search ── */
.search-box {
    display: flex;
    justify-content: center;
    margin-bottom: 12px;
}
.search-box input {
    font-size: 13px;
    color: var(--text-primary);
    background: var(--bg-input);
    padding: 8px 16px;
    border-radius: 6px;
    border: 1px solid var(--border-subtle);
    outline: none;
    width: 300px;
    transition: border-color 0.15s, background 0.3s, color 0.3s;
}
.search-box input::placeholder {
    color: var(--text-placeholder);
}
.search-box input:focus {
    border-color: var(--accent);
}
.card.highlight {
    border-color: var(--accent);
    box-shadow: 0 0 20px var(--accent-glow);
}

/* ── Pan / zoom wrapper ── */
.tree-wrapper {
    overflow: hidden;
    padding-bottom: 8px;
    cursor: grab;
    user-select: none;
}
.tree-wrapper.dragging {
    cursor: grabbing;
}
.tree {
    display: flex;
    justify-content: center;
    min-width: fit-content;
}

/* ── Horizontal tree layout ── */
.tree ul {
    display: flex;
    justify-content: center;
    padding-top: 48px;
    position: relative;
    list-style: none;
}
.tree ul.children-container {
    display: none;
}
li.expanded > ul.children-container {
    display: flex;
}
.tree li {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 0 10px;
    position: relative;
}

/* Horizontal bar connecting siblings */
.tree li::before,
.tree li::after {
    content: "";
    position: absolute;
    top: 0;
    width: 50%;
    border-top: 2px solid var(--connector);
}
.tree li::before { left: 0; }
.tree li::after { right: 0; }

/* Vertical drop from bar to card */
.tree li .card {
    margin-top: 18px;
    position: relative;
}
.tree li .card::before {
    content: "";
    position: absolute;
    top: -18px;
    left: 50%;
    transform: translateX(-50%);
    height: 18px;
    border-left: 2px solid var(--connector);
}

/* Vertical line from parent down to children bar */
.tree ul::before {
    content: "";
    position: absolute;
    top: 0;
    left: 50%;
    height: 48px;
    border-left: 2px solid var(--connector);
}

/* Clean up first/last/only child lines */
.tree li:first-child::before { border-top: none; }
.tree li:last-child::after { border-top: none; }
.tree li:only-child::before,
.tree li:only-child::after { border: none; }

/* Root level: no connector lines */
.tree > ul::before { display: none; }
.tree > ul > li::before,
.tree > ul > li::after { display: none; }
.tree > ul > li > .card { margin-top: 0; }
.tree > ul > li > .card::before { display: none; }

/* ── Card styling ── */
.card {
    position: relative;
    z-index: 2;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
    padding: 16px 18px;
    min-width: 170px;
    max-width: 210px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: var(--bg-card);
    box-shadow: 0 4px 16px var(--shadow-card);
    transition: border-color 0.2s, box-shadow 0.2s, background 0.3s;
}
.card:hover {
    border-color: var(--accent);
    box-shadow: 0 6px 24px var(--shadow-card-hover);
}

/* ── Avatar ── */
.avatar-placeholder {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    background: var(--avatar-bg);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 15px;
    font-weight: 700;
    color: #ffffff;
    margin-bottom: 4px;
    border: 2px solid var(--avatar-border);
    box-shadow: 0 0 8px var(--avatar-glow);
    transition: background 0.3s, border-color 0.3s;
}
[data-theme="light"] .avatar-placeholder {
    color: #2563eb;
}

/* ── Card text ── */

.card-name {
    font-size: 13px;
    font-weight: 700;
    color: var(--text-primary);
    text-align: center;
    line-height: 1.35;
    margin-bottom: 2px;
}
.card-title {
    font-size: 11.5px;
    font-weight: 500;
    color: var(--text-muted);
    text-align: center;
    word-break: break-word;
    line-height: 1.3;
}
.card-dept {
    font-size: 10px;
    font-weight: 650;
    color: var(--dept-text);
    text-align: center;
    text-transform: uppercase;
    letter-spacing: 0.4px;
    line-height: 1.2;
    margin-top: 2px;
}

/* ── Expand button ── */
.expand-btn {
    font-size: 10px;
    font-weight: 500;
    color: var(--accent);
    background: var(--bg-input);
    padding: 3px 10px;
    border-radius: 10px;
    margin-top: 4px;
    border: 1px solid var(--border-subtle);
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}
.expand-btn:hover {
    background: var(--bg-hover);
    border-color: var(--accent);
}

/* ── Company badges & worksite ── */
.company-badges {
    display: flex;
    flex-wrap: wrap;
    gap: 2px;
    justify-content: center;
    margin-top: 4px;
}
.badge {
    font-size: 9px;
    font-weight: 500;
    padding: 1px 6px;
    border-radius: 4px;
    background: var(--badge-bg);
    color: var(--badge-text);
    border: 1px solid var(--badge-border);
    word-break: normal;
    transition: background 0.3s, color 0.3s, border-color 0.3s;
}
.worksite {
    font-size: 11px;
    font-weight: 500;
    color: var(--text-muted);
    font-style: normal;
    margin-top: 2px;
}
