/* Reset and Base Styles */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    scroll-behavior: smooth;
    scroll-padding-top: 100px;
    -webkit-text-size-adjust: 100%;
}

/* Respect reduced motion preferences */
@media (prefers-reduced-motion: reduce) {
    html {
        scroll-behavior: auto;
    }

    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    font-size: 16px;
    line-height: 1.7;
    color: #333;
    background-color: #fafafa;
    min-height: 100vh;
}

/* Skip Link for Accessibility */
.skip-link {
    position: absolute;
    top: -100px;
    left: 50%;
    transform: translateX(-50%);
    background: #1a365d;
    color: white;
    padding: 12px 24px;
    border-radius: 0 0 8px 8px;
    text-decoration: none;
    font-weight: 600;
    z-index: 10000;
    transition: top 0.2s;
}

.skip-link:focus {
    top: 0;
    outline: 3px solid #fbbf24;
    outline-offset: 2px;
}

/* Focus Styles for Accessibility */
*:focus {
    outline: 2px solid #3b82f6;
    outline-offset: 2px;
}

*:focus:not(:focus-visible) {
    outline: none;
}

*:focus-visible {
    outline: 3px solid #3b82f6;
    outline-offset: 2px;
}

/* Header Navigation */
.header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 70px;
    background: #1a365d;
    color: white;
    display: flex;
    align-items: center;
    padding: 0 clamp(15px, 4vw, 30px);
    z-index: 1000;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.15);
}

.header-title {
    font-size: clamp(1rem, 3vw, 1.25rem);
    font-weight: 600;
    margin-right: clamp(20px, 4vw, 40px);
    white-space: nowrap;
}

/* Mobile Menu Toggle Button */
.mobile-menu-toggle {
    display: none;
    background: transparent;
    border: 2px solid rgba(255, 255, 255, 0.5);
    color: white;
    padding: 8px;
    border-radius: 6px;
    cursor: pointer;
    margin-left: auto;
    min-width: 44px;
    min-height: 44px;
    align-items: center;
    justify-content: center;
}

.mobile-menu-toggle:hover,
.mobile-menu-toggle:focus {
    background: rgba(255, 255, 255, 0.15);
    border-color: white;
}

.mobile-menu-toggle svg {
    width: 24px;
    height: 24px;
}

.mobile-menu-toggle .icon-close {
    display: none;
}

.mobile-menu-toggle[aria-expanded="true"] .icon-menu {
    display: none;
}

.mobile-menu-toggle[aria-expanded="true"] .icon-close {
    display: block;
}

.header-nav {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    align-items: center;
}

.header-nav a {
    color: rgba(255, 255, 255, 0.9);
    text-decoration: none;
    padding: 10px 16px;
    border-radius: 6px;
    font-size: 0.9rem;
    transition: background-color 0.2s, color 0.2s;
    min-height: 44px;
    display: flex;
    align-items: center;
}

.header-nav a:hover,
.header-nav a:focus {
    background: rgba(255, 255, 255, 0.15);
    color: white;
}

.header-nav a.active {
    background: rgba(255, 255, 255, 0.2);
    color: white;
    font-weight: 500;
}

/* Visually Hidden (for screen readers) */
.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* Main Layout */
.layout {
    display: flex;
    margin-top: 70px;
    min-height: calc(100vh - 70px);
}

/* Sidebar / Table of Contents */
.sidebar {
    width: 300px;
    min-width: 300px;
    background: white;
    border-right: 1px solid #e2e8f0;
    position: sticky;
    top: 70px;
    height: calc(100vh - 70px);
    overflow-y: auto;
    padding: 24px 0;
}

/* Mobile TOC Toggle */
.toc-toggle {
    display: none;
    width: 100%;
    background: #f8fafc;
    border: none;
    border-bottom: 1px solid #e2e8f0;
    padding: 16px 20px;
    font-size: 0.9rem;
    font-weight: 600;
    color: #1a365d;
    cursor: pointer;
    text-align: left;
    min-height: 48px;
    align-items: center;
    justify-content: space-between;
}

.toc-toggle:hover,
.toc-toggle:focus {
    background: #f1f5f9;
}

.toc-toggle svg {
    width: 20px;
    height: 20px;
    transition: transform 0.2s;
}

.toc-toggle[aria-expanded="true"] svg {
    transform: rotate(180deg);
}

.toc-content {
    display: block;
}

.sidebar-title {
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #64748b;
    padding: 0 20px;
    margin-bottom: 12px;
}

.toc-list {
    list-style: none;
}

.toc-list li {
    margin: 0;
}

.toc-list a {
    display: block;
    padding: 10px 20px;
    color: #475569;
    text-decoration: none;
    font-size: 0.9rem;
    border-left: 3px solid transparent;
    transition: all 0.2s;
    min-height: 44px;
    display: flex;
    align-items: center;
}

.toc-list a:hover,
.toc-list a:focus {
    background: #f1f5f9;
    color: #1a365d;
    border-left-color: #cbd5e1;
}

.toc-list a.active {
    background: #eff6ff;
    color: #1a365d;
    border-left-color: #3b82f6;
    font-weight: 500;
}

.toc-divider {
    height: 1px;
    background: #e2e8f0;
    margin: 16px 20px;
}

.toc-section-title {
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #94a3b8;
    padding: 16px 20px 8px;
}

/* Main Content Area */
.content {
    flex: 1;
    max-width: 900px;
    padding: clamp(20px, 5vw, 60px) clamp(20px, 5vw, 60px);
    background: white;
    margin: 20px;
    margin-left: 0;
    border-radius: 0 12px 12px 0;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

/* Document Header */
.document-header {
    text-align: center;
    margin-bottom: 40px;
    padding-bottom: 30px;
    border-bottom: 2px solid #e2e8f0;
}

.document-title {
    font-size: clamp(1.5rem, 5vw, 2.25rem);
    font-weight: 700;
    color: #1a365d;
    margin-bottom: 16px;
    line-height: 1.2;
}

.document-meta {
    color: #64748b;
    font-size: clamp(0.875rem, 2vw, 0.95rem);
}

.document-meta p {
    margin: 4px 0;
}

.document-intro {
    font-size: 1rem;
    color: #475569;
    margin-top: 20px;
    max-width: 700px;
    margin-left: auto;
    margin-right: auto;
}

/* Context Reference Box (for Addendums) */
.context-reference {
    background: #f0f9ff;
    border: 1px solid #bae6fd;
    border-left: 4px solid #0ea5e9;
    border-radius: 0 8px 8px 0;
    padding: 16px 20px;
    margin-bottom: 30px;
}

.context-reference-label {
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #0369a1;
    margin-bottom: 8px;
}

.context-reference-text {
    color: #0c4a6e;
    font-style: italic;
}

.context-reference a {
    color: #0369a1;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.context-reference a:hover,
.context-reference a:focus {
    color: #075985;
}

/* Section Styling */
.section {
    margin-bottom: 40px;
    padding-bottom: 30px;
    border-bottom: 1px solid #e2e8f0;
}

.section:last-child {
    border-bottom: none;
}

.section-title {
    font-size: clamp(1.25rem, 3vw, 1.5rem);
    font-weight: 700;
    color: #1e293b;
    margin-bottom: 20px;
    padding-top: 10px;
}

.subsection {
    margin-bottom: 20px;
}

.subsection-title {
    font-size: 1rem;
    font-weight: 600;
    color: #334155;
    margin-bottom: 8px;
}

.subsection p {
    color: #475569;
    margin-bottom: 12px;
}

/* Emphasized Text */
strong, .emphasis {
    font-weight: 600;
    color: #1e293b;
}

/* Lists */
.content ul, .content ol {
    margin: 12px 0 16px 24px;
    color: #475569;
}

.content li {
    margin-bottom: 8px;
    padding-left: 4px;
}

.content li ul, .content li ol {
    margin-top: 8px;
    margin-bottom: 8px;
}

/* Links in Content */
.content a {
    color: #2563eb;
    text-decoration: underline;
    text-underline-offset: 2px;
    text-decoration-thickness: 1px;
}

.content a:hover,
.content a:focus {
    color: #1d4ed8;
    text-decoration-thickness: 2px;
}

.see-addendum {
    display: inline-block;
    background: #eff6ff;
    color: #1d4ed8;
    padding: 8px 16px;
    border-radius: 4px;
    font-size: 0.9rem;
    font-weight: 500;
    margin-top: 8px;
    text-decoration: none;
    min-height: 44px;
    line-height: 1.5;
}

.see-addendum:hover,
.see-addendum:focus {
    background: #dbeafe;
    text-decoration: none;
}

/* Back to Top Button */
.back-to-top {
    position: fixed;
    bottom: clamp(20px, 4vw, 40px);
    right: clamp(20px, 4vw, 40px);
    width: 50px;
    height: 50px;
    min-width: 50px;
    min-height: 50px;
    background: #1a365d;
    color: white;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    transition: all 0.3s;
    opacity: 0;
    visibility: hidden;
    z-index: 999;
}

.back-to-top.visible {
    opacity: 1;
    visibility: visible;
}

.back-to-top:hover,
.back-to-top:focus {
    background: #2d4a7c;
    transform: translateY(-3px);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.25);
}

.back-to-top svg {
    width: 24px;
    height: 24px;
}

/* Addendum Subtitle */
.addendum-subtitle {
    font-size: 1rem;
    color: #64748b;
    margin-top: -10px;
    margin-bottom: 30px;
}

/* ========================================
   RESPONSIVE DESIGN
   ======================================== */

/* Tablet breakpoint */
@media (max-width: 1024px) {
    .sidebar {
        width: 260px;
        min-width: 260px;
    }

    .content {
        padding: 30px;
    }
}

/* Small tablet / large mobile */
@media (max-width: 768px) {
    html {
        scroll-padding-top: 80px;
    }

    .header {
        height: auto;
        min-height: 60px;
        flex-wrap: wrap;
        padding: 12px 16px;
        gap: 10px;
    }

    .header-title {
        margin-right: auto;
    }

    .mobile-menu-toggle {
        display: flex;
    }

    .header-nav {
        display: none;
        width: 100%;
        flex-direction: column;
        gap: 4px;
        padding-top: 12px;
        border-top: 1px solid rgba(255, 255, 255, 0.2);
        margin-top: 4px;
    }

    .header-nav.open {
        display: flex;
    }

    .header-nav a {
        width: 100%;
        justify-content: flex-start;
        padding: 12px 16px;
        font-size: 1rem;
    }

    .layout {
        flex-direction: column;
        margin-top: 60px;
    }

    .sidebar {
        width: 100%;
        min-width: 100%;
        position: relative;
        top: 0;
        height: auto;
        max-height: none;
        border-right: none;
        border-bottom: 1px solid #e2e8f0;
        padding: 0;
    }

    .toc-toggle {
        display: flex;
    }

    .toc-content {
        display: none;
        padding: 12px 0;
    }

    .toc-content.open {
        display: block;
    }

    .sidebar-title {
        padding-top: 12px;
    }

    .content {
        margin: 0;
        border-radius: 0;
        padding: 24px 20px;
    }

    .document-header {
        margin-bottom: 30px;
        padding-bottom: 24px;
    }

    .section {
        margin-bottom: 30px;
        padding-bottom: 24px;
    }

    .back-to-top {
        width: 50px;
        height: 50px;
    }
}

/* Mobile breakpoint */
@media (max-width: 480px) {
    .header {
        padding: 10px 12px;
    }

    .content {
        padding: 20px 16px;
    }

    .toc-list a {
        padding: 12px 16px;
    }

    .sidebar-title,
    .toc-section-title {
        padding-left: 16px;
        padding-right: 16px;
    }

    .toc-divider {
        margin-left: 16px;
        margin-right: 16px;
    }

    .context-reference {
        padding: 14px 16px;
    }

    .see-addendum {
        display: block;
        text-align: center;
    }
}

/* Safe area insets for notched devices */
@supports (padding: env(safe-area-inset-bottom)) {
    .back-to-top {
        bottom: calc(clamp(20px, 4vw, 40px) + env(safe-area-inset-bottom));
        right: calc(clamp(20px, 4vw, 40px) + env(safe-area-inset-right));
    }

    .header {
        padding-left: calc(clamp(15px, 4vw, 30px) + env(safe-area-inset-left));
        padding-right: calc(clamp(15px, 4vw, 30px) + env(safe-area-inset-right));
    }

    @media (max-width: 768px) {
        .content {
            padding-left: calc(20px + env(safe-area-inset-left));
            padding-right: calc(20px + env(safe-area-inset-right));
            padding-bottom: calc(24px + env(safe-area-inset-bottom));
        }
    }
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    .header-nav a {
        border: 1px solid transparent;
    }

    .header-nav a:hover,
    .header-nav a:focus,
    .header-nav a.active {
        border-color: white;
    }

    .toc-list a {
        border: 1px solid transparent;
    }

    .toc-list a:hover,
    .toc-list a:focus,
    .toc-list a.active {
        border-color: #1a365d;
    }

    .content a {
        text-decoration-thickness: 2px;
    }
}

/* Print Styles */
@media print {
    .header, .sidebar, .back-to-top, .skip-link {
        display: none !important;
    }

    .layout {
        display: block;
        margin-top: 0;
    }

    .content {
        max-width: 100%;
        margin: 0;
        padding: 20px;
        box-shadow: none;
    }

    .section {
        page-break-inside: avoid;
    }

    .content a {
        color: #000;
    }

    .content a[href^="http"]::after {
        content: " (" attr(href) ")";
        font-size: 0.8em;
        color: #666;
    }
}
