/* ==========================================
   9. 章節導航條與浮動選單 (Nav Rail & Popovers)
   ========================================== */
.chapter-nav-rail {
    position: fixed;
    left: calc(var(--sidebar-width) + 20px);
    top: calc(50% - 64px);
    transform: translateY(-50%) translateZ(0);
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    z-index: 1002;
    width: 24px;
    height: 120px;
    display: none;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    -webkit-user-select: none;
    user-select: none;
    transition: transform 0.25s ease, filter 0.25s ease, height 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.chapter-nav-rail.two-ticks,
.chapter-nav-rail:has(.chapter-tick:nth-child(2):last-child) {
    height: 66px !important;
}

.chapter-nav-ticks {
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    height: 100%;
    padding: 6px 0;
    box-sizing: border-box;
    transform-origin: center center;
    contain: layout paint;
    transform: translateZ(0);
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
}

.chapter-nav-rail.two-ticks .chapter-nav-ticks,
.chapter-nav-rail:has(.chapter-tick:nth-child(2):last-child) .chapter-nav-ticks {
    justify-content: center;
    gap: 48px;
    padding: 0;
}

.chapter-nav-ticks.anim-expand {
    animation: chapterTicksExpand 0.52s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

.chapter-nav-ticks.anim-contract {
    animation: chapterTicksContract 0.52s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

@keyframes chapterTicksExpand {
    0% {
        transform: scaleY(0.2) translateZ(0);
        opacity: 0.2;
    }

    45% {
        transform: scaleY(1.25) translateZ(0);
        opacity: 1;
    }

    75% {
        transform: scaleY(0.96) translateZ(0);
        opacity: 1;
    }

    100% {
        transform: scaleY(1) translateZ(0);
        opacity: 1;
    }
}

@keyframes chapterTicksContract {
    0% {
        transform: scaleY(1.4) translateZ(0);
        opacity: 0.2;
    }

    45% {
        transform: scaleY(0.85) translateZ(0);
        opacity: 1;
    }

    75% {
        transform: scaleY(1.04) translateZ(0);
        opacity: 1;
    }

    100% {
        transform: scaleY(1) translateZ(0);
        opacity: 1;
    }
}

.chapter-tick {
    display: block;
    width: 16px;
    height: 3px;
    min-height: 3px;
    max-height: 3px;
    flex-shrink: 0;
    box-sizing: border-box;
    border-radius: 999px;
    background: var(--text-secondary);
    opacity: 0.35;
    transform: scaleX(1) translateZ(0);
    transform-origin: center center;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    contain: layout;
    will-change: transform, opacity;
    transition: transform 0.25s cubic-bezier(0.2, 0.8, 0.2, 1),
        background-color 0.25s ease,
        opacity 0.25s ease,
        box-shadow 0.25s ease;
}

.chapter-tick:only-child {
    margin: auto 0;
}

.chapter-nav-rail:active .chapter-tick:not(.active),
.chapter-nav-rail.active .chapter-tick:not(.active) {
    opacity: 0.65;
    transform: scaleX(1.1) translateZ(0);
}

.chapter-nav-rail.active {
    filter: drop-shadow(0 0 6px rgba(154, 135, 30, 0.45));
}

.chapter-tick.active {
    opacity: 1;
    transform: scaleX(1.25) translateZ(0);
    background: var(--primary);
    box-shadow: 0 0 8px var(--primary);
}

/* 浮動選單容器：精確容納 12 個項目 (16px padding + 12 * 38px + 11 * 3px = 505px) */
.chapter-popover {
    position: fixed;
    left: calc(var(--sidebar-width) + 58px);
    top: calc(50% - 64px);
    transform: translateY(-50%) scale(0.92);
    transform-origin: left center;
    z-index: 1002;
    min-width: 130px;
    max-width: 216px;
    max-height: min(540px, calc(100dvh - 32px));
    overflow-y: auto;
    padding: 8px 6px;
    border-radius: 18px;
    background: var(--sidebar-bg);
    border: 1px solid var(--glass-border);
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.1), var(--glass-inner-highlight);
    backdrop-filter: blur(var(--blur-val));
    -webkit-backdrop-filter: blur(var(--blur-val));
    opacity: 0;
    pointer-events: none;
    display: flex;
    flex-direction: column;
    gap: 3px;
    -webkit-user-select: none;
    user-select: none;
    box-sizing: border-box;
    transition: opacity 0.2s ease, transform 0.2s cubic-bezier(0.2, 0.8, 0.2, 1);
}

[data-theme="dark"] .chapter-popover {
    box-shadow: 0 16px 36px rgba(0, 0, 0, 0.38), var(--glass-inner-highlight);
}

.chapter-popover.active {
    opacity: 1;
    transform: translateY(-50%) scale(1);
    pointer-events: auto;
}

[data-no-glass] .chapter-popover {
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
}

/* 浮動選單項目：調校為 38px 比例與細緻點擊手感 */
.chapter-item {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    width: 100%;
    min-width: 0;
    height: 38px;
    min-height: 38px;
    padding: 0 12px;
    border-radius: 10px;
    font-size: 15.5px;
    font-weight: 700;
    color: var(--text-secondary);
    opacity: 1;
    background: transparent !important;
    cursor: pointer;
    transition: color 0.18s ease, opacity 0.15s ease;
    box-sizing: border-box;
    -webkit-user-select: none;
    user-select: none;
    outline: none;
    -webkit-tap-highlight-color: transparent;
    transform: none !important;
}

.chapter-item-text {
    flex: 1;
    min-width: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1;
    display: flex;
    align-items: center;
    height: 100%;
    font-size: 15.5px;
}

.chapter-item:active {
    opacity: 1;
}

.chapter-item.active {
    background: transparent !important;
    color: var(--primary) !important;
    opacity: 1;
}

/* 縮排卡片項目：Flex 同軸排列，小圓點與文字中心完全物理對齊，字重一致鎖定防止文字重排 */
.chapter-item.indented {
    display: flex;
    align-items: center;
    padding-left: 12px;
    padding-right: 10px;
    font-size: 15.5px;
    font-weight: 700;
    color: var(--text-secondary);
    opacity: 1;
    background: transparent !important;
    letter-spacing: 0.2px;
}

.chapter-item.indented::before {
    content: '';
    display: block;
    width: 4px;
    height: 4px;
    min-width: 4px;
    min-height: 4px;
    border-radius: 50%;
    background-color: var(--text-secondary);
    opacity: 0.65;
    margin-right: 10px;
    flex-shrink: 0;
    box-sizing: border-box;
    transform-origin: center center;
    transition: background-color 0.2s ease, opacity 0.2s ease;
    pointer-events: none;
}

@media (hover: hover) {
    .chapter-item.indented:hover:not(.active) {
        background: transparent !important;
        color: var(--text);
        opacity: 1;
    }

    [data-theme="dark"] .chapter-item.indented:hover:not(.active) {
        background: transparent !important;
        color: var(--text);
        opacity: 1;
    }

    .chapter-item.indented:hover:not(.active)::before {
        background-color: var(--primary);
        opacity: 1;
        transform: none;
    }
}

.chapter-item.indented.active {
    background: transparent !important;
    color: var(--primary) !important;
    opacity: 1;
    font-weight: 600;
}

.chapter-item.indented.active::before {
    background-color: var(--primary) !important;
    opacity: 1 !important;
    transform: none;
    box-shadow: none !important;
}

/* 分組標題：輕盈邊界、專屬字級與回彈彈性動效 */
.chapter-popover-header {
    font-size: 15.5px;
    font-weight: 700;
    color: var(--primary);
    padding: 8px 12px 4px 12px;
    letter-spacing: 0.8px;
    line-height: 1.4;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    border-radius: 8px;
}

.chapter-popover-header:not(:first-child) {
    margin-top: 8px;
}

.chapter-header-text {
    display: inline-block;
    transform-origin: left center;
    transition: transform 0.22s cubic-bezier(0.34, 1.56, 0.64, 1);
    will-change: transform;
}

@media (hover: hover) {
    .chapter-popover-header:hover .chapter-header-text {
        transform: scale(1.06) translateZ(0);
    }
}

.chapter-popover-header:active .chapter-header-text {
    transform: scale(0.95) translateZ(0) !important;
    transition: transform 0.06s cubic-bezier(0.2, 0.8, 0.2, 1) !important;
}

/* ==========================================
   10. 右鍵選單與文字工具列 (Context & Toolbar)
   ========================================== */
.context-menu,
.category-popover-menu {
    position: fixed;
    background: var(--glass-surface);
    backdrop-filter: blur(var(--blur-val-sm));
    -webkit-backdrop-filter: blur(var(--blur-val-sm));
    border: 1px solid var(--glass-border);
    border-radius: 12px;
    padding: 6px;
    min-width: 140px;
    box-shadow: var(--glass-shadow-soft), var(--glass-inner-highlight);
    z-index: 5000 !important;
    display: none;
    opacity: 0;
    transform: scale(0.95);
    transform-origin: top right;
    transition: opacity 0.15s ease, transform 0.15s cubic-bezier(0.2, 0.8, 0.2, 1);
    box-sizing: border-box;
    -webkit-user-select: none;
    user-select: none;
}

.context-menu {
    transform-origin: top left;
}

.context-menu.active,
.category-popover-menu.active {
    display: block;
    opacity: 1;
    transform: scale(1);
}

.context-item,
.category-popover-item {
    padding: 10px 14px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 15.5px;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 10px;
    transition: opacity 0.18s ease, color 0.18s ease, transform 0.1s ease;
    color: var(--text-secondary);
    opacity: 1;
    background: transparent !important;
    box-sizing: border-box;
    line-height: 1;
    -webkit-user-select: none;
    user-select: none;
    outline: none;
}

.context-item svg,
.category-popover-item svg {
    flex-shrink: 0;
    width: 16px;
    height: 16px;
}

.context-item.active,
.category-popover-item.active {
    background: transparent !important;
    color: var(--primary) !important;
    opacity: 1 !important;
}

.context-item.active svg,
.category-popover-item.active svg {
    color: var(--primary) !important;
}

.context-item:active,
.category-popover-item:active {
    background: transparent !important;
    transform: scale(0.95);
    opacity: 1 !important;
}

.context-item.danger,
.category-popover-item.danger {
    color: #ac4252;
    opacity: 1;
    font-weight: 700;
}

.context-item.danger svg,
.category-popover-item.danger svg {
    color: #ac4252;
}

.context-item.danger:active,
.category-popover-item.danger:active {
    background: transparent !important;
    color: #ac4252 !important;
    transform: scale(0.95);
    opacity: 1 !important;
}

.context-item.danger:active svg,
.category-popover-item.danger:active svg {
    color: #ac4252 !important;
}

@media (hover: hover) {

    .context-item.danger:hover,
    .category-popover-item.danger:hover {
        background: transparent !important;
        color: #ff5c6a !important;
        opacity: 1 !important;
    }

    .context-item.danger:hover svg,
    .category-popover-item.danger:hover svg {
        color: #ff5c6a !important;
    }
}

.text-toolbar {
    position: fixed;
    display: flex;
    gap: 8px;
    padding: 6px 10px;
    border-radius: 12px;
    z-index: 9999;
    opacity: 0;
    visibility: hidden;
    transform: translate(-50%, 8px);
    transition: opacity 0.2s, transform 0.2s, visibility 0.2s;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15), var(--glass-inner-highlight);
    backdrop-filter: blur(var(--blur-val-sm));
    -webkit-backdrop-filter: blur(var(--blur-val-sm));
}

.text-toolbar.active {
    opacity: 1;
    visibility: visible;
    transform: translate(-50%, 0);
}

.text-toolbar[data-placement="below"] {
    transform: translate(-50%, -8px);
}

.text-toolbar[data-placement="below"].active {
    transform: translate(-50%, 0);
}

.animated-action-btn {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

.text-toolbar .animated-action-btn {
    width: 36px;
    height: 36px;
}

.animated-action-btn svg {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    transition: opacity 0.4s cubic-bezier(0.34, 1.56, 0.64, 1), transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.animated-action-btn .icon-check {
    opacity: 0;
    transform: scale(0.3) rotate(-30deg);
}

.animated-action-btn .icon-main {
    opacity: 1;
    transform: scale(1) rotate(0);
}

.animated-action-btn.success .icon-check {
    opacity: 1;
    transform: scale(1) rotate(0);
}

.animated-action-btn.success .icon-main {
    opacity: 0;
    transform: scale(0.3) rotate(30deg);
}

/* 光筆劃線 (Marker Highlight / Underline) */
@keyframes markerSweep {
    0% {
        background-size: 0% 100%;
    }
    100% {
        background-size: 100% 100%;
    }
}

.marker-highlight,
mark.marker-highlight {
    --marker-color: rgba(228, 185, 35, 0.36);
    display: inline;
    background-color: transparent !important;
    background-image: linear-gradient(var(--marker-color), var(--marker-color));
    background-repeat: no-repeat;
    background-position: left bottom;
    background-size: 100% 100%;
    color: inherit;
    border-radius: 0;
    padding: 0;
    margin: 0;
    box-decoration-break: clone;
    -webkit-box-decoration-break: clone;
}

.marker-highlight.animate-draw,
mark.marker-highlight.animate-draw {
    animation: markerSweep var(--marker-duration, 0.52s) cubic-bezier(0.25, 0.1, 0.25, 1.0) both;
    will-change: background-size;
}

[data-theme="dark"] .marker-highlight,
[data-theme="dark"] mark.marker-highlight {
    --marker-color: rgba(255, 215, 64, 0.38);
    background-image: linear-gradient(var(--marker-color), var(--marker-color));
}

[data-no-glass] .marker-highlight.animate-draw,
[data-no-glass] mark.marker-highlight.animate-draw {
    animation: none !important;
    background-size: 100% 100% !important;
}

