:root {
    --sidebar-width: 4.5em;
    --sidebar-width-active: var(--sidebar-width);
}

#sidebar {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 9999;
    height: 100vh;
    width: var(--sidebar-width-active);
    background: var(--header-background);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    padding: 1.5em 0;
    transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    overflow: visible;
    color: var(--text-color);

    border-top-right-radius: 12px;
    border-bottom-right-radius: 12px;
}

#sidebar .sidebar-edge-hint {
    display: none;
    position: absolute;
    top: 50%;
    right: -1.5px;
    transform: translateY(-50%);
    width: 4px;
    height: 100px;
    background: var(--text-color);
    border-radius: 20px;
    opacity: 0.15;
    transition: opacity 0.3s ease;
    align-items: center;
    justify-content: center;
    pointer-events: auto;
    cursor: pointer;
    z-index: 100;
}

#sidebar .sidebar-edge-hint svg {
    display: none;
}

#sidebar:not(.deployed):hover .sidebar-edge-hint {
    opacity: 0.4 !important;
}

#sidebar.deployed .sidebar-edge-hint {
    opacity: 0;
    pointer-events: none;
    visibility: hidden;
}


@media (max-width: 768px) {
    #sidebar:not(.deployed) #sidebar-toggle svg {
        display: none;
    }

    #sidebar .sidebar-edge-hint {
        display: flex;
        position: fixed;
        left: 0;
        right: auto;
        width: 40px;
        height: 140px;
        opacity: 0.25;
        background: var(--text-color);
        box-shadow: none;
        border-radius: 0 20px 20px 0;
        touch-action: none;
        user-select: none;
        -webkit-user-select: none;
    }

    #sidebar .sidebar-edge-hint.is-scrolling {
        opacity: 0.06;
    }

    #sidebar .sidebar-edge-hint::after {
        content: '';
        position: absolute;
        top: -30px;
        bottom: -30px;
        left: 0;
        right: -30px;
        /* Large hitbox towards the right */
        pointer-events: auto;
    }

    #sidebar.deployed .sidebar-edge-hint {
        opacity: 0;
        pointer-events: none;
    }
}

#sidebar.deployed {
    --sidebar-width-active: 16em;
    width: var(--sidebar-width-active);
    align-items: flex-start;
}

#sidebar section {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
}

#sidebar.deployed section {
    align-items: flex-start;
}

#sidebar section:first-child svg {
    width: 2.5em;
    height: 2.5em;

    min-width: 2.5em;
    flex-shrink: 0;

    margin-left: 1em;
    margin-right: 1em;

}


#sidebar #sidebar-toggle {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    padding: 0;
    width: 100%;
}

#sidebar h1 {
    font-size: 1.2rem;
    font-weight: 600;
    margin: 0;
    white-space: nowrap;
    opacity: 0;
    max-width: 0;
    overflow: hidden;
    transform: translateX(-10px);
    transition: opacity 0.3s ease, transform 0.3s ease, max-width 0.3s ease;
    pointer-events: none;
}

#sidebar.deployed h1 {
    opacity: 1;
    max-width: 200px;
    transform: translateX(0);
    pointer-events: auto;
}

#sidebar h1.beta-title {
    color: var(--beta-color, #8b5cf6) !important;
}

#sidebar .beta-badge {
    background-color: var(--beta-color, #8b5cf6);
    color: white;
    font-size: 0.65rem;
    padding: 2px 8px;
    border-radius: 6px;
    margin-left: 8px;
    vertical-align: middle;
    text-transform: uppercase;
    font-weight: 700;
    letter-spacing: 0.05em;
    box-shadow: 0 2px 4px rgba(139, 92, 246, 0.3);
}

#sidebar.deployed section:first-child {
    padding-left: 0;
    gap: 0;
}

#sidebar.deployed section:first-child h1 {
    margin: 0;
}


.links-nav-container {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 0.5em;
    margin-top: 0;
}

.links-section {
    flex-grow: 1;
    justify-content: flex-start !important;
    padding-top: 1em;
    overflow-y: auto;
    overflow-x: hidden;
    width: 100%;
}

#sidebar a {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    width: 100%;
    padding: 0.5em 1.25em;
    text-decoration: none;
    color: var(--text-color);
    transition: background-color 0.2s ease, color 0.2s ease;
    border-left: 3px solid transparent;
    box-sizing: border-box;
}

#sidebar a:hover,
#sidebar a.active {
    background-color: var(--border-hard);
    color: var(--primary-color);
    border-left-color: transparent;
    /* Remove the solid bar */
}

/* Sidebar SVG icon handles */
#sidebar a>svg,
.sidebar-item svg {
    width: 1.5em;
    height: 1.5em;
    min-width: 1.5em;
    flex-shrink: 0;
    margin-right: 1em;
    transition: all 0.2s ease;
    stroke: currentColor;
    /* Update stroke color to primary */
}

#sidebar a .etu-toolbox-icon-shell {
    fill: var(--text-color);
    transition: fill 0.2s ease;
}

#sidebar a .etu-toolbox-icon-accent {
    fill: var(--primary-color);
    transition: fill 0.2s ease;
}

#sidebar a:hover .etu-toolbox-icon-shell,
#sidebar a.active .etu-toolbox-icon-shell {
    fill: var(--primary-color);
}

#sidebar a:hover .etu-toolbox-icon-accent,
#sidebar a.active .etu-toolbox-icon-accent {
    fill: var(--primary-color);
}

#sidebar .sidebar-custom-icon {
    width: 1.5em;
    height: 1.5em;
    min-width: 1.5em;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-right: 1em;
}

#sidebar .sidebar-custom-icon svg {
    width: 1.5em !important;
    height: 1.5em !important;
    max-width: 100%;
    max-height: 100%;
    display: block;
}

#sidebar .sidebar-custom-icon .sidebar-custom-svg {
    width: 1.5em !important;
    height: 1.5em !important;
}

#sidebar .sidebar-custom-icon.adapt-colors svg {
    stroke: currentColor;
}

#sidebar .sidebar-custom-icon.adapt-colors svg *[stroke]:not([stroke="none"]) {
    stroke: currentColor !important;
}

#sidebar .sidebar-custom-icon.adapt-colors svg *[fill]:not([fill="none"]):not([fill="transparent"]) {
    fill: currentColor !important;
}

#sidebar .sidebar-custom-icon.preserve-colors svg {
    fill: initial;
    stroke: initial;
}

/* Only apply fill to icons that are intended to be filled */
#sidebar a>svg:not([fill="none"]),
.sidebar-item svg:not([fill="none"]) {
    fill: currentColor;
}

#sidebar a>span:not(.sidebar-custom-icon):not(.sidebar-reorder-handle) {
    white-space: nowrap;
    opacity: 0;
    max-width: 0;
    overflow: hidden;
    transform: translateX(-10px);
    transition: opacity 0.3s ease, transform 0.3s ease, max-width 0.3s ease;
    font-size: 0.95rem;
    pointer-events: none;
}

#sidebar.deployed a>span:not(.sidebar-custom-icon):not(.sidebar-reorder-handle) {
    opacity: 1;
    max-width: 200px;
    transform: translateX(0);
    pointer-events: auto;
}

.separator {
    width: 80%;
    height: 1px;
    background-color: var(--text-muted);
    opacity: 0.15;
    margin: 1em auto;
}

.bottom-section {
    margin-top: auto;
}

.button-log-in {
    background-color: var(--primary-color);
    color: var(--text-on-accent) !important;
    border-radius: 50px;
    width: 85%;
    max-width: 200px;
    height: 2.8em;
    margin: 1em auto;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.8em;
    font-size: 0.9rem;
    transition: all 0.3s ease;
}

#sidebar.deployed .button-log-in {
    width: 85%;
    justify-content: center;
}

.button-log-in:hover {
    background-color: var(--primary-color) !important;
    filter: brightness(1.1);
}

.button-log-in svg {
    fill: var(--text-on-accent);
    width: 1.2em !important;
    height: 1.2em !important;
    flex-shrink: 0;
}

.invert {
    filter: var(--invert-filter);
    transition: filter 0.3s ease;
}

#sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: var(--overlay-bg);
    z-index: 9998;
    opacity: 0;
    transition: opacity 0.3s ease;
}

#sidebar-overlay.active {
    opacity: 1;
}

.page-back-link.is-fixed-global {
    position: fixed !important;
    top: 1.25rem;
    left: 1.25rem;
    z-index: 10010;
    background: var(--bg-secondary);
    box-shadow: var(--shadow-md);
    border: 1px solid var(--border-soft);
    padding: 0.65rem 0.85rem;
    border-radius: 12px;
    transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

@media (min-width: 769px) {

    #sidebar.deployed~.page-back-link.is-fixed-global,
    .layout-with-sidebar .page-back-link.is-fixed-global {
        /* Align with the sidebar edge + margin */
        left: calc(var(--sidebar-width-active, 4.5em) + 1.25rem);
    }
}

@media (max-width: 768px) {
    .page-back-link.is-fixed-global {
        top: 0.75rem;
        left: 0.75rem;
        padding: 0.5rem 0.75rem;
        font-size: 0.85rem;
    }

    #sidebar.deployed~.page-back-link.is-fixed-global {
        left: calc(16em + 0.75rem);
    }
}

.sidebar-context-menu {
    position: fixed;
    z-index: 10020;
    background: var(--card-bg);
    border: 1px solid var(--border-hard);
    border-radius: var(--border-radius-md);
    box-shadow: var(--shadow-lg);
    min-width: 170px;
    padding: 0.5rem;
    /* Add padding to container */
    overflow: hidden;
    backdrop-filter: none;
}

.sidebar-context-menu[hidden],
.sidebar-link-modal[hidden],
.sidebar-context-menu button[hidden] {
    display: none !important;
}

.sidebar-context-menu button {
    width: 100%;
    border: 0;
    background: transparent;
    color: var(--text-color);
    padding: 0.6rem 1rem;
    text-align: left;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 12px;
    border-radius: var(--border-radius-sm, 6px);
    /* Rounded items */
    transition: background-color 0.2s;
}

.sidebar-context-menu button svg {
    width: 1rem;
    height: 1rem;
    flex-shrink: 0;
}

.sidebar-context-menu button:hover {
    background: var(--hover-bg);
}

.sidebar-link-modal {
    --app-modal-z-index: 10030;
    --app-modal-backdrop: var(--overlay-bg);
    --app-modal-backdrop-blur: 0px;
}

.sidebar-link-modal-backdrop {
    pointer-events: auto;
}

.sidebar-link-modal-dialog {
    --app-modal-width: 560px;
    --app-modal-max-height: calc(100vh - 3rem);
    --app-modal-surface: var(--card-bg);
    --app-modal-text: var(--text-color);
    --app-modal-border: var(--border-hard);
    --app-modal-radius: 16px;
    overflow: auto;
    padding: 1.2rem 1.2rem 1rem;
}

.sidebar-link-modal-close {
    font-size: 1.7rem;
    --app-modal-close-bg: transparent;
    --app-modal-close-color: var(--text-muted);
}

.sidebar-modal-label {
    display: block;
    margin-top: 0.8rem;
    margin-bottom: 0.4rem;
    font-size: 0.86rem;
    color: color-mix(in srgb, var(--text-muted) 88%, #fff 12%);
    font-weight: 600;
    letter-spacing: 0.01em;
}

.sidebar-modal-input {
    width: 100%;
    border-radius: 12px;
    border: 1px solid color-mix(in srgb, var(--border-hard) 70%, transparent);
    background: color-mix(in srgb, var(--bg-secondary) 92%, transparent);
    color: var(--text-color);
    padding: 0.65rem 0.78rem;
    transition: border-color 0.2s ease, box-shadow 0.2s ease, background 0.2s ease;
}

.sidebar-modal-input:focus {
    outline: none;
    border-color: color-mix(in srgb, var(--primary-color) 66%, #fff 34%);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary-color) 22%, transparent);
    background: color-mix(in srgb, var(--bg-secondary) 98%, transparent);
}

.sidebar-file-upload {
    margin-top: 0.12rem;
    border: 1px dashed color-mix(in srgb, var(--border-hard) 85%, transparent);
    border-radius: 12px;
    padding: 0.75rem;
    display: flex;
    align-items: center;
    gap: 0.62rem;
    background: color-mix(in srgb, var(--bg-secondary) 90%, transparent);
    cursor: pointer;
    transition: border-color 0.2s ease, background 0.2s ease;
}

.sidebar-file-upload:hover {
    border-color: color-mix(in srgb, var(--primary-color) 55%, transparent);
    background: color-mix(in srgb, var(--bg-secondary) 96%, transparent);
}

.sidebar-modal-file-input {
    display: none;
}

.sidebar-file-upload-btn {
    border-radius: 10px;
    border: 1px solid color-mix(in srgb, var(--border-hard) 80%, transparent);
    background: color-mix(in srgb, var(--primary-color) 18%, transparent);
    color: color-mix(in srgb, var(--text-color) 90%, #fff 10%);
    padding: 0.4rem 0.68rem;
    font-size: 0.84rem;
    white-space: nowrap;
}

.sidebar-file-upload-name {
    font-size: 0.82rem;
    color: var(--text-muted);
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar-file-preview {
    width: 5.5rem;
    height: 5.5rem;
    margin-top: 0.8rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid color-mix(in srgb, var(--border-hard) 80%, transparent);
    border-radius: 50%;
    background: var(--bg-secondary);
    box-shadow: 0 8px 22px rgb(0 0 0 / 18%);
    overflow: hidden;
}

.sidebar-file-preview[hidden] {
    display: none;
}

.sidebar-file-preview img,
.sidebar-file-preview svg {
    display: block;
    width: 100%;
    height: 100%;
    padding: 0.9rem;
    object-fit: contain;
}

.sidebar-default-icons {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
}

.sidebar-default-icon {
    border: 0;
    background: transparent;
    padding: 0.12rem;
    border-radius: 50%;
    cursor: pointer;
}

.sidebar-default-icon input {
    display: none;
}

.sidebar-default-icon .icon-preview {
    width: 1.55rem;
    height: 1.55rem;
    color: var(--primary-color);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    opacity: 0.78;
    transition: opacity 0.2s ease, filter 0.2s ease, transform 0.2s ease;
}

.sidebar-default-icon .icon-preview svg {
    width: 100%;
    height: 100%;
}

.sidebar-default-icon:has(input:checked) {
    transform: scale(1.08);
}

.sidebar-default-icon:has(input:checked) .icon-preview {
    opacity: 1;
}

.sidebar-default-icon.is-selected {
    transform: scale(1.08);
}

.sidebar-default-icon.is-selected .icon-preview {
    opacity: 1;
    filter: drop-shadow(0 0 0.45rem color-mix(in srgb, var(--primary-color) 65%, transparent));
}

.sidebar-modal-checkbox {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    margin-top: 0.62rem;
    font-size: 0.86rem;
    color: color-mix(in srgb, var(--text-color) 92%, #fff 8%);
}

.sidebar-modal-checkbox:has(input:disabled) {
    opacity: 0.45;
    cursor: not-allowed;
}

.sidebar-modal-checkbox input:disabled {
    cursor: not-allowed;
}

.sidebar-modal-actions {
    margin-top: 1.1rem;
    display: flex;
    justify-content: flex-end;
    gap: 0.45rem;
}

.sidebar-modal-btn {
    border: 1px solid var(--border-hard);
    background: var(--bg-secondary);
    color: var(--text-color);
    border-radius: 10px;
    padding: 0.52rem 0.8rem;
    cursor: pointer;
}

.sidebar-modal-btn.accent {
    border-color: transparent;
    background: var(--primary-color);
    color: var(--text-on-accent, #fff);
}

body.sidebar-modal-open {
    overflow: hidden;
}

#sidebar-custom-links.reorder-active .sidebar-custom-link {
    cursor: grab;
}

#sidebar .sidebar-custom-link {
    gap: 0;
    transition: transform 0.18s ease, opacity 0.18s ease, background-color 0.18s ease;
    -webkit-touch-callout: none;
}

#sidebar .sidebar-add-link-entry {
    display: none;
    appearance: none;
    -webkit-appearance: none;
    align-items: center;
    justify-content: flex-start;
    gap: 0;
    width: calc(100% - 1rem);
    margin: 0.2rem 0.5rem 0;
    padding: 0.55em 0.95em;
    border: 1px dashed color-mix(in srgb, var(--text-muted) 55%, transparent);
    border-radius: 14px;
    background: color-mix(in srgb, var(--bg-secondary) 35%, transparent);
    color: color-mix(in srgb, var(--text-color) 78%, var(--primary-color) 22%);
    text-align: left;
    font: inherit;
    cursor: pointer;
    box-sizing: border-box;
    transition: border-color 0.2s ease, background-color 0.2s ease, color 0.2s ease, transform 0.2s ease;
}

#sidebar.deployed .sidebar-add-link-entry {
    display: flex;
}

#sidebar .sidebar-add-link-entry:hover,
#sidebar .sidebar-add-link-entry:focus-visible {
    border-color: color-mix(in srgb, var(--primary-color) 55%, transparent);
    background: color-mix(in srgb, var(--primary-color) 10%, var(--bg-secondary));
    color: var(--primary-color);
    transform: translateY(-1px);
}

#sidebar .sidebar-add-link-entry:focus-visible {
    outline: none;
}

#sidebar .sidebar-add-link-icon {
    color: inherit;
    opacity: 0.9;
}

#sidebar .sidebar-add-link-label {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 0.92rem;
    font-weight: 500;
}

#sidebar .sidebar-custom-label {
    min-width: 0;
}

#sidebar .sidebar-reorder-handle {
    margin-left: auto;
    width: 18px;
    height: 18px;
    min-width: 18px;
    display: none;
    align-items: center;
    justify-content: center;
    opacity: 0.45;
    color: var(--text-muted);
    cursor: grab;
    transition: opacity 0.2s ease, color 0.2s ease;
}

#sidebar .sidebar-reorder-handle svg {
    width: 100%;
    height: 100%;
}

#sidebar.deployed .sidebar-custom-link .sidebar-reorder-handle {
    display: inline-flex;
}

#sidebar .sidebar-custom-link:hover .sidebar-reorder-handle,
#sidebar-custom-links.reorder-active .sidebar-reorder-handle {
    opacity: 0.95;
    color: var(--primary-color);
}

.sidebar-custom-link.drag-source {
    opacity: 0.7;
}

.sidebar-custom-link.drag-source .sidebar-reorder-handle {
    cursor: grabbing;
}

.sidebar-custom-link.drag-over {
    outline: 1px dashed var(--primary-color);
    outline-offset: -3px;
}

@media (max-width: 768px) {
    #sidebar {
        width: 0;
        max-width: calc(100vw - 1rem);
        overflow: visible;
        border-right: none;
        box-shadow: none;
    }

    #sidebar> :not(#sidebar-toggle) {
        transition: opacity 0.18s ease, visibility 0.18s ease;
    }

    #sidebar:not(.deployed)> :not(#sidebar-toggle):not(.sidebar-edge-hint) {
        opacity: 0;
        visibility: hidden;
        pointer-events: none;
    }

    #sidebar.deployed {
        width: min(16em, calc(100vw - 1rem));
    }

    #sidebar-overlay {
        display: block;
        pointer-events: none;
    }

    #sidebar-overlay.active {
        pointer-events: auto;
    }

    .button-log-in {
        margin-bottom: 3em;
    }

    .sidebar-default-icons {
        gap: 0.25rem;
    }
}
