:root {
    --bg-primary: #1a1a1a;
    --bg-secondary: #2a2a2a;
    --text-primary: #f0f0f0;
    --text-secondary: #a0a0a0;
    --accent-primary: #3b82f6;
    /* Tailwind blue-500 */
    --accent-secondary: #f59e0b;
    /* Retained original amber for branding */
    --border-color: #444;
    --bg-table-header: #333;
    --bg-table-cell: #222;
    --bg-module-row: rgba(59, 130, 246, 0.1);
    /* Updated to new accent */
    --text-module-row: var(--accent-primary);
    --toc-width-collapsed: 4rem;
    --toc-width-expanded: 20rem;
}

body.light-mode {
    --bg-primary: #ffffff;
    --bg-secondary: #f0f2f5;
    --text-primary: #1a1a1a;
    --text-secondary: #555;
    --accent-primary: #2563eb;
    /* Tailwind blue-600 */
    --accent-secondary: #fd7e14;
    /* Retained original orange for branding */
    --border-color: #dee2e6;
    --bg-table-header: #e9ecef;
    --bg-table-cell: #ffffff;
    --bg-module-row: rgba(37, 99, 235, 0.05);
    /* Updated to new accent */
    --text-module-row: var(--accent-primary);
}

.page-container {
    display: none;
}

.page-container.active {
    display: block;
}

body {
    font-family: 'Inter', sans-serif;
    background-color: var(--bg-primary);
    color: var(--text-secondary);
    line-height: 1.7;
    font-size: clamp(1.0rem, 2.3vw, 1.125rem);
    padding-top: 4rem;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
}

h1,
h2,
h3,
h4,
h5 {
    font-family: 'Lora', serif;
    color: var(--text-primary);
    font-weight: 700;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 0.75rem;
    margin-bottom: 1.5rem;
    margin-top: 3rem;
    /* Added spacing above headers */
    scroll-margin-top: 5rem;
    line-height: 1.3;
}

h1 {
    font-size: clamp(2.5rem, 6vw, 3rem);
    border-bottom: none;
}

h2 {
    font-size: clamp(2rem, 5vw, 2.5rem);
}

h3 {
    font-size: clamp(1.5rem, 4vw, 2rem);
}

h4 {
    font-size: clamp(1.2rem, 3vw, 1.5rem);
}

h5 {
    font-size: clamp(1.1rem, 3vw, 1.25rem);
}

h2 .rule-number,
h3 .rule-number,
h4 .rule-number,
h5 .rule-number {
    color: var(--accent-secondary);
    font-weight: 600;
    margin-right: 0.75rem;
}

.toc-link .rule-number {
    color: var(--accent-secondary);
    margin-right: 0.5rem;
}

a {
    color: var(--accent-primary);
    text-decoration: none;
    border-bottom: 1px dotted rgba(59, 130, 246, 0.5);
    transition: color 0.2s, border-color 0.2s;
}

body.light-mode a {
    border-bottom-color: rgba(37, 99, 235, 0.5);
}

a:hover {
    color: var(--text-primary);
    border-bottom-color: var(--text-primary);
}

blockquote {
    background-color: var(--bg-secondary);
    border-left: 4px solid var(--accent-primary);
    padding: 1.5rem;
    margin: 2rem 0;
    font-style: italic;
    border-radius: 0 8px 8px 0;
}

/* ADDED: Styling for embedded images */
.content-image-figure {
    margin: 3rem 0;
    text-align: center;
}

.content-image-figure img {
    max-width: 100%;
    height: auto;
    border-radius: 12px;
    border: 1px solid var(--border-color);
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
}

.content-image-figure figcaption {
    margin-top: 1rem;
    font-size: 0.9rem;
    font-style: italic;
    color: var(--text-secondary);
}

.table-responsive-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    margin: 2.5rem 0;
    position: relative;
    scrollbar-width: thin;
    scrollbar-color: var(--accent-primary) transparent;
}

.table-responsive-wrapper::-webkit-scrollbar {
    height: 6px;
}

.table-responsive-wrapper::-webkit-scrollbar-thumb {
    background-color: var(--accent-primary);
    border-radius: 3px;
}

table {
    width: 100%;
    border-collapse: collapse;
    margin: 0;
}

th,
td {
    border: none;
    border-bottom: 1px solid var(--border-color);
    padding: 1rem;
    text-align: left;
}

tr:last-child td {
    border-bottom: none;
}

th {
    /* MODIFIED for theme-awareness */
    background-color: var(--bg-table-header);
    color: var(--text-primary);
    font-weight: 600;
}

td {
    background-color: var(--bg-table-cell);
}

strong {
    color: var(--text-primary);
    font-weight: 600;
}

/* ADDED: Minimum touch target size for structured tables */
.table-structured th,
.table-structured td {
    padding: 0.75rem 0.5rem;
    min-height: 44px;
    /* WCAG minimum for touch targets */
    vertical-align: middle;
}

/* ADDED: Theme-aware zebra striping for better readability */
body:not(.light-mode) .table-structured tbody tr:nth-child(odd) {
    background-color: rgba(255, 255, 255, 0.04);
}

body.light-mode .table-structured tbody tr:nth-child(odd) {
    background-color: rgba(0, 0, 0, 0.04);
}

/* ADDED: Sticky header for horizontally scrolling tables */
.table-structured thead th {
    position: sticky;
    top: 0;
    z-index: 10;
    /* A stronger border to separate from scrolling content */
    border-bottom: 2px solid var(--border-color);
}

.section-divider {
    height: 1px;
    background-color: var(--border-color);
    border: 0;
    margin: 5rem 0;
    text-align: center;
}

.section-divider::after {
    content: '⚔️';
    display: inline-block;
    position: relative;
    top: -1.1rem;
    font-size: 1.5rem;
    padding: 0 1rem;
    background: var(--bg-primary);
    color: var(--text-secondary);
}

.tooltip {
    position: relative;
    border-bottom: 1px dotted var(--accent-primary);
    cursor: help;
}

.tooltip:before {
    content: attr(data-tooltip);
    position: absolute;
    bottom: 125%;
    left: 50%;
    transform: translateX(-50%);
    padding: 0.75rem;
    border-radius: 8px;
    background-color: var(--bg-secondary);
    color: var(--text-primary);
    font-size: 0.875rem;
    line-height: 1.4;
    /* MODIFIED for tooltip overflow guard */
    min-width: clamp(160px, 45vw, 250px);
    text-align: left;
    visibility: hidden;
    opacity: 0;
    transition: opacity 0.2s, visibility 0.2s;
    z-index: 10;
    border: 1px solid var(--border-color);
}

.tooltip:hover:before {
    visibility: visible;
    opacity: 1;
}

#back-to-top {
    position: fixed;
    bottom: 2rem;
    right: 2rem;
    background-color: rgba(26, 26, 26, 0.7);
    backdrop-filter: blur(5px);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-radius: 50%;
    width: 3rem;
    height: 3rem;
    display: none;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
    z-index: 50;
}

body.light-mode #back-to-top {
    background-color: rgba(255, 255, 255, 0.7);
}

#back-to-top:hover {
    background-color: var(--accent-primary);
    color: #fff;
    transform: translateY(-3px);
}

#toc-container {
    width: var(--toc-width-collapsed);
    transition: width 0.3s ease-in-out;
}

#toc-container.is-expanded {
    width: var(--toc-width-expanded);
}

#toc-container:not(.is-expanded) #toc-title {
    writing-mode: vertical-rl;
    text-align: center;
    opacity: 1 !important;
    margin: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translateX(-50%) translateY(-50%) rotate(180deg);
    white-space: nowrap;
    font-size: 1.5rem;
    color: var(--text-primary);
}

#app-wrapper {
    transition: padding-left 0.3s ease-in-out;
}

#mobile-sticky-header {
    position: fixed;
    top: 4rem;
    left: 0;
    right: 0;
    background-color: rgba(26, 26, 26, 0.9);
    backdrop-filter: blur(5px);
    color: var(--text-primary);
    padding: 0.5rem 1rem;
    text-align: center;
    z-index: 40;
    border-bottom: 1px solid var(--border-color);
    font-family: 'Lora', serif;
    transition: transform 0.3s ease;
    transform: translateY(-100%);
}

body.light-mode #mobile-sticky-header {
    background-color: rgba(255, 255, 255, 0.9);
}

#mobile-sticky-header.visible {
    transform: translateY(0);
}

#top-nav {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 4rem;
    background-color: rgba(17, 17, 17, 0.8);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--border-color);
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: space-between;
    /* Changed from center */
    padding: 0 1rem;
    transition: transform 0.3s ease-in-out;
}

body.light-mode #top-nav {
    background-color: rgba(248, 249, 250, 0.8);
}

#top-nav.top-nav--hidden {
    transform: translateY(-100%);
}

.nav-link {
    display: flex;
    align-items: center;
    height: 100%;
    padding: 0 1.25rem;
    font-family: 'Lora', serif;
    font-size: 1.1rem;
    color: var(--text-secondary);
    text-decoration: none;
    border-bottom: 2px solid transparent;
    transition: all 0.3s ease;
}

.nav-link:hover {
    color: var(--text-primary);
}

.nav-link.active {
    color: var(--accent-secondary);
    border-bottom-color: var(--accent-secondary);
}

/* MODIFIED: Mobile Bottom Nav Item Styling */
.nav-icon {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    flex-shrink: 0;
    width: 80px;
    height: 100%;
    padding: 0.25rem;
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-decoration: none;
    border: none;
}

.nav-icon.active {
    color: var(--accent-secondary);
}

.nav-icon svg {
    width: 1.25rem;
    height: 1.25rem;
    margin-bottom: 0.25rem;
}

.module-row {
    background-color: var(--bg-module-row);
}

.module-row td,
.module-row th {
    color: var(--text-module-row);
    font-style: italic;
}

.module-icon {
    cursor: help;
    font-style: normal;
    display: inline-block;
    opacity: 0.7;
    /* ADDED for better tap-target size */
    padding: 0.5rem;
    vertical-align: middle;
    border-radius: 4px;
}

#theme-toggle {
    color: var(--text-primary);
    position: absolute;
    right: 1rem;
    top: 50%;
    transform: translateY(-50%);
}

.theme-icon {
    width: 1.5rem;
    height: 1.5rem;
}

#lang-switch {
    position: absolute;
    right: 4rem;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-primary);
    text-decoration: none !important;
    border-bottom: none;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 2.5rem;
    height: 2.5rem;
    border-radius: 9999px;
    transition: background-color 0.2s;
}

#lang-switch:hover {
    background-color: rgba(255, 255, 255, 0.1);
    border-bottom: none;
}

body.light-mode #lang-switch:hover {
    background-color: rgba(0, 0, 0, 0.1);
}

.table-view-toggle {
    display: none;
    /* Hidden by default */
    margin: 0 0 1rem 0;
    /* Spacing below the button */
    padding: 0.5rem 1rem;
    font-size: 0.875rem;
    font-weight: 600;
    border: none;
    background-color: var(--accent-primary);
    color: white;
    border-radius: 6px;
    cursor: pointer;
    transition: background-color 0.2s;
}

.table-view-toggle:hover {
    background-color: #2563eb;
    /* Tailwind blue-600 */
}

body.light-mode .table-view-toggle:hover {
    background-color: #1d4ed8;
    /* Tailwind blue-700 */
}

.theme-icon {
    width: 1.5rem;
    height: 1.5rem;
}

#mobile-toc-close:focus-visible {
    outline: 2px solid var(--accent-primary);
    outline-offset: 2px;
}

#mobile-toc-panel {
    z-index: 110;
    /* ADDED for better mobile UX */
    overscroll-behavior: contain;
    padding-bottom: 6rem;
    /* Space for bottom close bar */
}

/* ADDED to hide scrollbar on mobile nav */
#bottom-nav::-webkit-scrollbar {
    display: none;
}

#bottom-nav {
    -ms-overflow-style: none;
    /* IE and Edge */
    scrollbar-width: none;
    /* Firefox */
}

/* Form Styles */
.form-input,
.form-textarea {
    background-color: var(--bg-secondary);
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    width: 100%;
    border-radius: 8px;
    padding: 0.75rem 1rem;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.form-input:focus,
.form-textarea:focus {
    outline: none;
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 2px var(--accent-primary);
}

.form-label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 600;
    color: var(--text-primary);
}

.form-button {
    background-color: var(--accent-secondary);
    color: #111;
    font-weight: bold;
    padding: 0.75rem 1.5rem;
    border-radius: 8px;
    border: none;
    cursor: pointer;
    transition: background-color 0.2s;
}

.form-button:hover {
    background-color: #d97706;
    /* A slightly darker amber */
}

body.light-mode .form-button:hover {
    background-color: #fb923c;
    /* A slightly lighter orange */
}

.required-asterisk {
    color: var(--accent-secondary);
    font-weight: bold;
    margin-left: 0.25rem;
}

.info-card {
    background-color: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 1.5rem 2rem;
    margin-top: 2rem;
}

@media (max-width: 768px) {
    h2[id],
    h3[id],
    h4[id],
    h5[id] {
        scroll-margin-top: 5rem;
    }

    .table-view-toggle {
        display: inline-block;
        /* Show on mobile */
    }

    .table-structured th,
    .table-structured td {
        white-space: nowrap;
    }

    /* Hides the original header when card-view is active */
    .table-responsive-wrapper.card-view-active>table thead {
        display: none;
    }

    /* Transform rows into cards when card-view is active */
    .table-responsive-wrapper.card-view-active>table tr {
        display: block;
        border: 1px solid var(--border-color);
        border-radius: 8px;
        margin-bottom: 1rem;
        padding: 0.5rem;
    }

    .table-responsive-wrapper.card-view-active>table tr:last-child {
        margin-bottom: 0;
    }

    /* Transform cells into labeled blocks when card-view is active */
    .table-responsive-wrapper.card-view-active>table td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        border: none;
        border-bottom: 1px solid var(--border-color);
        padding: 0.8rem 0.5rem;
        text-align: right;
        white-space: normal;
    }

    .table-responsive-wrapper.card-view-active>table td:last-child {
        border-bottom: none;
    }

    /* Add the data-label as a pseudo-element when card-view is active */
    .table-responsive-wrapper.card-view-active>table td:before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--text-primary);
        text-align: left;
        margin-right: 1rem;
    }

    /* ADDED: Swipe hint for scrollable tables */
    .table-responsive-wrapper::after {
        content: '→ Swipe to see more';
        position: absolute;
        top: 50%;
        right: 0.5rem;
        transform: translateY(-50%);
        background: linear-gradient(to right, transparent, var(--bg-table-cell) 30%);
        padding: 0.5rem 1rem 0.5rem 2rem;
        color: var(--text-secondary);
        font-size: 0.8rem;
        pointer-events: none;
        opacity: 0;
        transition: opacity 0.3s;
        border-radius: 4px;
        display: flex;
        align-items: center;
    }

    .table-responsive-wrapper.is-scrollable::after {
        opacity: 0.8;
    }
}

@media (max-width: 480px) {
    body {
        --text-secondary: #d0d0d0;
    }

    /* ADDED for better phone legibility */
    body {
        font-size: 1.125rem;
        line-height: 1.7;
    }

    .max-w-4xl {
        max-width: 65ch;
    }

    .tooltip:before {
        bottom: auto;
        top: 100%;
        margin-top: 0.5rem;
    }

    /* ADDED: Table squeeze relief */
    .table-responsive-wrapper>table>td {
        padding-left: 42%;
    }

    .table-responsive-wrapper>table>td:before {
        width: 40%;
        white-space: normal;
        /* Allow label wrapping */
    }
}

@media (max-width: 390px) {
    h1 {
        font-size: 2.25rem;
    }

    #top-nav {
        height: 3.5rem;
    }
}

@media print {
    body {
        padding-top: 0;
        background-color: #ffffff;
        color: #000000;
        font-size: 10pt;
    }

    #top-nav,
    #toc-container,
    #mobile-toc-toggle,
    #mobile-sticky-header,
    #back-to-top {
        display: none !important;
    }

    /* Fixed print layout bug */
    #app-wrapper {
        padding-left: 0 !important;
    }

    .page-container:not(.active) {
        display: none !important;
    }

    .page-container.active {
        display: block !important;
    }

    .info-card,
    blockquote {
        background-color: #f1f3f5 !important;
        border-color: #dee2e6 !important;
    }

    a {
        color: #007bff !important;
        text-decoration: underline !important;
    }
}

/* Added for JS-controlled visibility to respect responsive classes */
.js-hidden {
    display: none !important;
}

/* Added support for reduced motion accessibility */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

