/* ═══════════════════════════════════════════
   DESPERADO'S DESTINY — GUIDEBOOK v2.0
   D&D Beyond-inspired layout with western grit
   ═══════════════════════════════════════════ */

:root {
    /* Core palette */
    --bg-darkest:    #0f0b08;
    --bg-dark:       #1a1410;
    --bg-panel:      #221b14;
    --bg-surface:    #2a2118;
    --bg-hover:      #342a1f;
    --bg-active:     #3d3025;

    /* Text */
    --text-primary:  #e8dcc8;
    --text-secondary:#b5a48e;
    --text-muted:    #7a6b58;
    --text-heading:  #f0e6d2;

    /* Accents */
    --accent:        #c23a22;    /* Blood red */
    --accent-hover:  #d94a32;
    --accent-glow:   rgba(194, 58, 34, 0.15);
    --gold:          #c9a84c;
    --gold-dim:      #8a7235;
    --gold-glow:     rgba(201, 168, 76, 0.1);

    /* Borders */
    --border:        #3d3025;
    --border-light:  #4a3d2e;

    /* Dimensions */
    --topbar-h:      56px;
    --sidebar-w:     280px;
    --toc-w:         220px;

    /* Transitions */
    --speed:         0.2s;
}

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

html { font-size: 16px; scroll-behavior: smooth; }

body {
    background: var(--bg-darkest);
    color: var(--text-primary);
    font-family: 'Crimson Text', Georgia, serif;
    font-size: 1.125rem;
    line-height: 1.7;
    overflow: hidden;
    height: 100vh;
}

/* ═══════════════════════════════════════════
   TOP BAR
   ═══════════════════════════════════════════ */
.topbar {
    position: fixed;
    top: 0; left: 0; right: 0;
    height: var(--topbar-h);
    background: var(--bg-dark);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    padding: 0 16px;
    gap: 16px;
    z-index: 1000;
}

/* Hamburger */
.hamburger {
    display: none;
    flex-direction: column;
    justify-content: center;
    gap: 5px;
    width: 36px;
    height: 36px;
    background: none;
    border: none;
    cursor: pointer;
    padding: 6px;
}
.hamburger span {
    display: block;
    width: 100%;
    height: 2px;
    background: var(--text-secondary);
    border-radius: 2px;
    transition: var(--speed);
}
.hamburger.active span:nth-child(1) { transform: rotate(45deg) translate(5px, 5px); }
.hamburger.active span:nth-child(2) { opacity: 0; }
.hamburger.active span:nth-child(3) { transform: rotate(-45deg) translate(5px, -5px); }

/* Logo */
.logo {
    display: flex;
    align-items: center;
    gap: 8px;
    text-decoration: none;
    color: var(--text-heading);
    flex-shrink: 0;
}
.logo-icon {
    font-size: 1.4rem;
    color: var(--accent);
}
.logo-text {
    font-family: 'Rye', serif;
    font-size: 1.1rem;
    letter-spacing: 0.5px;
}

/* Search */
.search-wrapper {
    position: relative;
    flex: 1;
    max-width: 480px;
}
.search-wrapper input {
    width: 100%;
    padding: 8px 14px;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text-primary);
    font-family: 'Inter', sans-serif;
    font-size: 0.85rem;
    outline: none;
    transition: border-color var(--speed);
}
.search-wrapper input::placeholder { color: var(--text-muted); }
.search-wrapper input:focus { border-color: var(--gold-dim); }

/* Search Results Dropdown */
.search-results {
    position: absolute;
    top: calc(100% + 6px);
    left: 0; right: 0;
    background: var(--bg-panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    max-height: 420px;
    overflow-y: auto;
    display: none;
    box-shadow: 0 12px 32px rgba(0,0,0,0.5);
    z-index: 1001;
}
.search-results.visible { display: block; }

.search-result {
    display: block;
    padding: 10px 14px;
    text-decoration: none;
    border-bottom: 1px solid var(--border);
    transition: background var(--speed);
}
.search-result:last-child { border-bottom: none; }
.search-result:hover { background: var(--bg-hover); }

.sr-title {
    display: block;
    font-family: 'Inter', sans-serif;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--gold);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.sr-section {
    display: block;
    font-size: 0.95rem;
    color: var(--text-primary);
    font-weight: 700;
}
.sr-snippet {
    display: block;
    font-size: 0.82rem;
    color: var(--text-muted);
    margin-top: 2px;
    line-height: 1.4;
}
.search-empty {
    padding: 20px;
    text-align: center;
    color: var(--text-muted);
    font-family: 'Inter', sans-serif;
    font-size: 0.85rem;
}

/* Topbar link */
.topbar-link {
    font-family: 'Inter', sans-serif;
    font-size: 0.8rem;
    font-weight: 500;
    color: var(--text-secondary);
    text-decoration: none;
    padding: 6px 12px;
    border: 1px solid var(--border);
    border-radius: 4px;
    white-space: nowrap;
    transition: all var(--speed);
    flex-shrink: 0;
}
.topbar-link:hover {
    color: var(--text-primary);
    border-color: var(--gold-dim);
    background: var(--gold-glow);
}

/* ═══════════════════════════════════════════
   LAYOUT
   ═══════════════════════════════════════════ */
.layout {
    display: flex;
    height: calc(100vh - var(--topbar-h));
    margin-top: var(--topbar-h);
}

/* ═══════════════════════════════════════════
   SIDEBAR
   ═══════════════════════════════════════════ */
.sidebar {
    width: var(--sidebar-w);
    min-width: var(--sidebar-w);
    background: var(--bg-dark);
    border-right: 1px solid var(--border);
    overflow-y: auto;
    padding: 16px 0;
    flex-shrink: 0;
}

/* Scrollbar */
.sidebar::-webkit-scrollbar { width: 4px; }
.sidebar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* Category */
.nav-category { margin-bottom: 4px; }

.nav-category-header {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    padding: 10px 20px;
    background: none;
    border: none;
    color: var(--text-secondary);
    font-family: 'Inter', sans-serif;
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 1.2px;
    cursor: pointer;
    transition: color var(--speed);
}
.nav-category-header:hover { color: var(--text-primary); }

.cat-icon {
    font-size: 0.9rem;
    color: var(--gold-dim);
}

.chevron {
    margin-left: auto;
    width: 0; height: 0;
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    border-top: 5px solid var(--text-muted);
    transition: transform var(--speed);
}
.nav-category.collapsed .chevron { transform: rotate(-90deg); }

/* Links */
.nav-links {
    overflow: hidden;
    max-height: 500px;
    transition: max-height 0.3s ease;
}
.nav-category.collapsed .nav-links { max-height: 0; }

.nav-link {
    display: block;
    padding: 7px 20px 7px 44px;
    color: var(--text-secondary);
    text-decoration: none;
    font-family: 'Inter', sans-serif;
    font-size: 0.88rem;
    font-weight: 400;
    border-left: 2px solid transparent;
    transition: all var(--speed);
    cursor: pointer;
}
.nav-link:hover {
    color: var(--text-primary);
    background: var(--bg-hover);
}
.nav-link.active {
    color: var(--gold);
    border-left-color: var(--gold);
    background: var(--gold-glow);
    font-weight: 500;
}

/* ═══════════════════════════════════════════
   MAIN CONTENT
   ═══════════════════════════════════════════ */
.main {
    flex: 1;
    overflow-y: auto;
    padding: 0;
    background: var(--bg-darkest);
}

/* Scrollbar */
.main::-webkit-scrollbar { width: 8px; }
.main::-webkit-scrollbar-track { background: var(--bg-dark); }
.main::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
.main::-webkit-scrollbar-thumb:hover { background: var(--border-light); }

/* Breadcrumb */
.content-header {
    position: sticky;
    top: 0;
    background: var(--bg-dark);
    border-bottom: 1px solid var(--border);
    padding: 10px 32px;
    z-index: 10;
}
.breadcrumb {
    font-family: 'Inter', sans-serif;
    font-size: 0.78rem;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    gap: 6px;
}
.bc-sep { color: var(--border-light); }
.bc-page { color: var(--text-secondary); }

/* Content layout: article + toc rail */
.content-layout {
    display: flex;
    max-width: 1100px;
    margin: 0 auto;
    position: relative;
}

/* ═══════════════════════════════════════════
   TABLE OF CONTENTS (right rail)
   ═══════════════════════════════════════════ */
.toc-rail {
    width: var(--toc-w);
    min-width: var(--toc-w);
    padding: 32px 0 32px 20px;
    position: sticky;
    top: 44px;
    height: fit-content;
    max-height: calc(100vh - var(--topbar-h) - 80px);
    overflow-y: auto;
    order: 2;
    flex-shrink: 0;
}
.toc-rail.hidden { display: none; }

.toc-rail::-webkit-scrollbar { width: 2px; }
.toc-rail::-webkit-scrollbar-thumb { background: var(--border); }

.toc-header {
    font-family: 'Inter', sans-serif;
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-muted);
    margin-bottom: 12px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--border);
}

.toc-link {
    display: block;
    padding: 4px 0;
    font-family: 'Inter', sans-serif;
    font-size: 0.78rem;
    color: var(--text-muted);
    text-decoration: none;
    border-left: 2px solid transparent;
    padding-left: 10px;
    transition: all var(--speed);
    line-height: 1.5;
}
.toc-link:hover { color: var(--text-secondary); }
.toc-link.active {
    color: var(--gold);
    border-left-color: var(--gold);
}
.toc-indent { padding-left: 22px; font-size: 0.74rem; }

/* ═══════════════════════════════════════════
   ARTICLE
   ═══════════════════════════════════════════ */
.article {
    flex: 1;
    min-width: 0;
    padding: 32px 40px 80px;
    order: 1;
    transition: opacity 0.15s;
}
.article.loading { opacity: 0.3; }

/* Loading & Error */
.loading-state, .error-state {
    text-align: center;
    padding: 80px 20px;
    color: var(--text-muted);
}
.loading-spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--border);
    border-top-color: var(--gold);
    border-radius: 50%;
    margin: 0 auto 16px;
    animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.error-state h2 {
    color: var(--accent);
    border: none;
    font-size: 1.5rem;
}

/* ─── Typography ─── */
.article h1 {
    font-family: 'Rye', serif;
    font-size: 2rem;
    color: var(--gold);
    border-bottom: 2px solid var(--border);
    padding-bottom: 12px;
    margin: 0 0 24px;
    text-transform: uppercase;
    letter-spacing: 1px;
    line-height: 1.3;
}

.article h2 {
    font-family: 'Rye', serif;
    font-size: 1.4rem;
    color: var(--text-heading);
    border-bottom: 1px solid var(--border);
    padding-bottom: 8px;
    margin: 40px 0 16px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.article h3 {
    font-family: 'Inter', sans-serif;
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--gold);
    margin: 28px 0 10px;
    text-transform: none;
    border: none;
    padding: 0;
}

.article h4 {
    font-family: 'Inter', sans-serif;
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin: 20px 0 8px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    border: none;
}

.article p {
    margin: 0 0 16px;
    color: var(--text-primary);
    text-align: left;
}

.article strong {
    color: var(--text-heading);
    font-weight: 700;
}

.article em {
    color: var(--text-secondary);
    font-style: italic;
}

.article a {
    color: var(--gold);
    text-decoration: underline;
    text-decoration-color: var(--gold-dim);
    text-underline-offset: 2px;
}
.article a:hover { color: var(--accent-hover); }

/* ─── Lists ─── */
.article ul, .article ol {
    margin: 0 0 16px 24px;
    color: var(--text-primary);
}
.article li {
    margin-bottom: 6px;
    line-height: 1.6;
}
.article li::marker { color: var(--gold-dim); }

/* ─── Tables ─── */
.article table {
    width: 100%;
    border-collapse: collapse;
    margin: 20px 0;
    font-size: 0.95rem;
}
.article thead th {
    background: var(--bg-surface);
    color: var(--gold);
    font-family: 'Inter', sans-serif;
    font-size: 0.78rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: 10px 14px;
    text-align: left;
    border-bottom: 2px solid var(--border-light);
}
.article tbody td {
    padding: 10px 14px;
    border-bottom: 1px solid var(--border);
    color: var(--text-primary);
    vertical-align: top;
}
.article tbody tr:hover { background: var(--bg-hover); }

/* ─── Code / Blockquotes ─── */
.article code {
    background: var(--bg-surface);
    padding: 2px 6px;
    border-radius: 3px;
    font-size: 0.88rem;
    color: var(--gold);
    font-family: 'Special Elite', monospace;
}

.article blockquote {
    border-left: 3px solid var(--accent);
    background: var(--accent-glow);
    padding: 12px 20px;
    margin: 16px 0;
    border-radius: 0 4px 4px 0;
}
.article blockquote p { margin: 0; color: var(--text-secondary); }

/* ─── Horizontal rules ─── */
.article hr {
    border: none;
    border-top: 1px solid var(--border);
    margin: 32px 0;
}

/* ─── Suit styling ─── */
.suit { font-weight: bold; font-size: 1.15em; }
.spades { color: #aaa; }
.hearts { color: var(--accent); }
.clubs { color: #8a9a6a; }
.diamonds { color: var(--gold); }
.star { color: var(--gold); }

/* ═══════════════════════════════════════════
   BACK TO TOP
   ═══════════════════════════════════════════ */
.back-to-top {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 40px;
    height: 40px;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-secondary);
    font-size: 1.2rem;
    cursor: pointer;
    opacity: 0;
    visibility: hidden;
    transition: all var(--speed);
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
}
.back-to-top.visible { opacity: 1; visibility: visible; }
.back-to-top:hover {
    background: var(--bg-hover);
    color: var(--gold);
    border-color: var(--gold-dim);
}

/* ═══════════════════════════════════════════
   SIDEBAR OVERLAY (mobile)
   ═══════════════════════════════════════════ */
.sidebar-overlay {
    display: none;
    position: fixed;
    top: var(--topbar-h);
    left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.6);
    z-index: 99;
    backdrop-filter: blur(2px);
}
.sidebar-overlay.visible { display: block; }

/* ═══════════════════════════════════════════
   RESPONSIVE
   ═══════════════════════════════════════════ */

/* Tablet: hide toc rail */
@media (max-width: 1100px) {
    .toc-rail { display: none; }
}

/* Mobile */
@media (max-width: 768px) {
    .hamburger { display: flex; }

    .logo-text { display: none; }

    .topbar-link { display: none; }

    .sidebar {
        position: fixed;
        top: var(--topbar-h);
        left: 0;
        bottom: 0;
        z-index: 100;
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        box-shadow: none;
    }
    .sidebar.open {
        transform: translateX(0);
        box-shadow: 8px 0 24px rgba(0,0,0,0.4);
    }

    .article {
        padding: 20px 16px 60px;
    }

    .article h1 { font-size: 1.5rem; }
    .article h2 { font-size: 1.2rem; }

    .content-header { padding: 8px 16px; }

    .search-wrapper { max-width: 100%; }

    .toc-rail { display: none; }
}

/* Small mobile */
@media (max-width: 480px) {
    .article table {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .article h1 { font-size: 1.3rem; }
}
