/* Sidebar / TOC */
.sidebar {
    position: sticky;
    top: var(--gap);
    align-self: flex-start;
    width: min(400px, calc(100vw - 50px));
    max-height: calc(100dvh - 2 * var(--gap));
    background: var(--bg-elevated);
    border-radius: var(--radius);
    border: var(--border-subtle-line);
    box-shadow: var(--shadow-soft);
    padding: var(--gap);
    font-size: 0.9rem;
    z-index: 20;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}

.toc-toggle {
    display: none;
}

.toc-header {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 10px;
    margin: 0 0 12px;
    padding-bottom: 10px;
    border-bottom: var(--border-subtle-line);
}

.toc-logo-link {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: 10px;
    text-decoration: none;
    flex-shrink: 0;
}

.toc-logo {
    display: block;
    width: 40px;
    height: 40px;
    border-radius: 10px;
}

.sidebar h2 {
    margin: 0;
    font-size: 0.95rem;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-soft);
    text-align: left;
}


#toc-list {
    list-style: none;
    margin: 0;
    padding: 0;
}

.sidebar .toc-group {
    margin-bottom: 16px;
    padding-bottom: 12px;
    border-bottom: var(--border-subtle-line);
}

.sidebar .toc-group:last-child {
    margin-bottom: 0;
    padding-bottom: 0;
    border-bottom: none;
}

.sidebar .toc-group-title {
    margin: 8px 0 6px;
    padding: 0 4px;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    color: var(--text-soft);
}

.sidebar .toc-group-list {
    list-style: none;
    margin: 0;
    padding: 0;
}

#toc-list li {
    margin-bottom: 6px;
}

.sidebar .nav-row {
    display: flex;
    align-items: center;
    gap: 8px;
}

.sidebar a {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px 12px;
    border-radius: 999px;
    text-decoration: none;
    color: var(--text-muted);
    transition:
        background 0.1s,
        color 0.1s,
        transform 0.08s;
    line-height: 1.2;
}

.sidebar a:hover {
    color: var(--info);
    transform: translateX(2px);
}

.sidebar a.active {
    color: var(--info);
    font-weight: 800;
}

.sidebar .nav-arrow {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-right: 0;
    transition: transform 0.1s;
    width: 12px;
    height: 12px;
    padding: 0;
    border: none;
    background: transparent;
    color: inherit;
    cursor: pointer;
}

.sidebar .nav-arrow.collapsed {
    transform: rotate(-90deg);
}

.sidebar .sub {
    padding-left: 0;
    font-size: 0.85em;
    margin-top: 4px;
    margin-bottom: 2px;
}

.sidebar .sub.sub-h4 {
    padding-left: 14px;
}

.sidebar .sub-list {
    list-style: none;
    margin: 4px 0 0;
    padding: 0 0 0 32px;
    overflow: hidden;
    transition:
        max-height 0.1s ease,
        opacity 0.1s ease;
    opacity: 1;
    position: relative;
}

.sidebar .sub-list.collapsed {
    max-height: 0;
    opacity: 0;
}

.toc-toggle:focus-visible,
.sidebar .nav-arrow:focus-visible {
    outline: 2px solid var(--info);
    outline-offset: 2px;
}

.toc-search {
    position: relative;
    margin: 0 0 12px;
}

.toc-search input {
    width: 100%;
    border: var(--border-subtle-line);
    border-radius: 999px;
    padding: 8px 34px 8px 12px;
    background: var(--bg-elevated);
    color: var(--text-main);
    font-size: 0.83rem;
    line-height: 1.2;
}

/* Prevent Safari from showing its built-in search clear button (we render our own). */
.toc-search input[type="search"]::-webkit-search-decoration,
.toc-search input[type="search"]::-webkit-search-cancel-button,
.toc-search input[type="search"]::-webkit-search-results-button,
.toc-search input[type="search"]::-webkit-search-results-decoration {
    -webkit-appearance: none;
    appearance: none;
    display: none;
}

.toc-search input::placeholder {
    color: var(--text-soft);
}

.toc-search input:focus-visible {
    outline: 2px solid var(--info);
    outline-offset: 2px;
}

.toc-search-clear {
    position: absolute;
    top: 50%;
    right: 5px;
    transform: translateY(-50%);
    width: 24px;
    height: 24px;
    border: 1px solid transparent;
    border-radius: 999px;
    background: transparent;
    color: var(--text-soft);
    cursor: pointer;
    font-size: 1rem;
    line-height: 1;
    padding: 0;
}

.toc-search-clear:hover {
    color: var(--text-main);
    border-color: var(--border-subtle);
}

.toc-search-results {
    margin: 0 0 12px;
    --search-heading-scale: 0.85;
    --section-h2-size: calc(var(--section-h2-size-base) * var(--search-heading-scale));
    --section-h3-size: calc(var(--section-h3-size-base) * var(--search-heading-scale));
    --section-h4-size: calc(var(--section-h4-size-base) * var(--search-heading-scale));
}

.sidebar.search-active #toc-list {
    display: none;
}

.toc-search-results-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.toc-search-result {
    width: 100%;
    border: var(--border-subtle-line);
    border-radius: var(--radius);
    background: var(--bg-elevated);
    color: var(--text-main);
    text-align: left;
    padding: 8px 10px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    gap: 5px;
    transition:
        border-color 0.1s,
        transform 0.08s,
        background 0.1s;
}

.toc-search-result.procedure {
    margin: 0;
}

.toc-search-result .procedure-skill-badge {
    font-size: 0.6rem;
    padding: 2px 8px;
    min-width: 88px;
}


.toc-search-result[search-kind="content"] .toc-search-result-title-text {
    font-weight: 600;
    color: var(--text-main);
}

.toc-search-result:hover {
    border-color: var(--border-subtle);
    background: #f3f4f6;
    transform: translateX(1px);
}

.toc-search-result:focus-visible {
    outline: 2px solid var(--info);
    outline-offset: 2px;
}

.toc-search-result-title {
    flex: 1;
    min-width: 0;
    font-size: 0.8rem;
    font-weight: 700;
    line-height: 1.3;
    display: inline-flex;
    align-items: flex-start;
    gap: 5px;
}

.toc-search-result-title-text {
    min-width: 0;
}

.toc-search-result-title-heading {
    color: var(--text-main);
    display: inline-block;
}

.toc-search-result[search-heading-level="3"] .toc-search-result-title-heading,
.toc-search-result[search-kind="content"][search-heading-level="3"] .toc-search-result-title-text {
    text-decoration: underline;
    text-decoration-color: var(--section-h3-accent);
    text-decoration-thickness: 2px;
    text-underline-offset: 4px;
}

.toc-search-result[search-kind="content"] .toc-search-result-title-text {
    color: var(--text-main);
}

.toc-search-result-procedure-icon {
    flex: 0 0 auto;
    font-size: 0.74rem;
    line-height: 1.2;
    opacity: 0.9;
}

.toc-search-result-meta {
    font-size: 0.63rem;
    color: var(--text-soft);
    letter-spacing: 0.02em;
}

.toc-search-result-snippet {
    font-size: 0.68rem;
    color: var(--text-muted);
    line-height: 1.35;
}

.toc-search-result mark {
    background: var(--info-soft);
    color: inherit;
    border-radius: 3px;
}

.toc-search-empty {
    margin: 0;
    padding: 7px 9px;
    font-size: 0.74rem;
    color: var(--text-soft);
    border: 1px dashed var(--border-subtle);
    border-radius: 10px;
    background: #f9fafb;
}

.search-target-flash {
    animation: search-target-flash 2s ease-out;
}

@keyframes search-target-flash {
    0% {
        background: var(--info-soft);
    }
    50% {
        background: var(--info-soft);
    }
    100% {
        background: transparent;
    }
}

@media (max-width: 860px) {
    .sidebar {
        position: fixed;
        top: calc(0.7 * var(--gap));
        left: calc(-1 * var(--radius));
        right: auto;
        opacity: 0;
        pointer-events: none;
        transform: translateX(-110%);
        transition:
            opacity 0.2s ease,
            transform 0.2s ease;
        padding-left: 36px;
    }

    .sidebar.is-open {
        opacity: 1;
        pointer-events: auto;
        transform: translateX(0);
    }

    main.toc-dim {
        opacity: 0.2;
        pointer-events: none;
    }

    .toc-toggle {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        position: fixed;
        top: calc(0.7 * var(--gap));
        right: calc(0.7 * var(--gap));
        z-index: 40;
        padding: 10px 14px;
        border-radius: var(--radius);
        border: var(--border-subtle-line);
        background: var(--bg-elevated);
        color: var(--text-main);
        box-shadow: var(--shadow-soft);
        font-size: 1.2rem;
        font-weight: 700;
        line-height: 1;
        cursor: pointer;
    }

    .sidebar a {
        padding: 10px 12px;
    }

    .sidebar .nav-arrow {
        margin-right: 6px;
    }

    .toc-search input {
        font-size: 16px;
    }
}
