/* ---------- global ---------- */
:root {
    /* default: white & gold theme */
    --bg: #ffffff;
    --panel: #ffffff;
    --sidebar-bg: #fbf7ec;
    --ink: #1a1a1a;
    --ink-dim: #6a6a6a;
    --rule: #e9dfc2;
    --accent: #b8892b;         /* muted gold */
    --accent-strong: #8a6412;  /* deeper gold for headings */
    --accent-soft: #fbf3dc;
    --tag: #f5ecd0;
    --mark: #ffe98c;
    --link: #8a6412;
    --sidebar-w: 280px;
    --reader-w: 46%;
    --topbar-h: 56px;
}
[data-theme="sepia"] {
    --bg: #fafaf7;
    --panel: #ffffff;
    --sidebar-bg: #f2efe6;
    --ink: #1b1b1a;
    --ink-dim: #666;
    --rule: #e2e2dc;
    --accent: #8b6f47;
    --accent-strong: #6a4f2a;
    --accent-soft: #f4ede1;
    --tag: #e8e4d5;
    --mark: #fff59d;
    --link: #8b6f47;
}
[data-theme="night"] {
    --bg: #171513;
    --panel: #1f1c18;
    --sidebar-bg: #1a1816;
    --ink: #e8e2d4;
    --ink-dim: #8a8275;
    --rule: #332e25;
    --accent: #d4a84b;
    --accent-strong: #e8c070;
    --accent-soft: #2a241a;
    --tag: #2a241a;
    --mark: #6b5518;
    --link: #e8c070;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
    font-family: -apple-system, 'Sarabun', 'Prompt', BlinkMacSystemFont, 'Segoe UI', sans-serif;
    font-size: 16px;
    color: var(--ink);
    background: var(--bg);
    line-height: 1.6;
}
a { color: var(--link); text-decoration: none; }
a:hover { text-decoration: underline; }
button { font-family: inherit; }

/* ---------- top bar ---------- */
.topbar {
    position: sticky; top: 0; z-index: 20;
    display: flex; align-items: center; gap: 12px;
    height: var(--topbar-h);
    padding: 0 16px;
    background: var(--panel);
    border-bottom: 1px solid var(--rule);
    box-shadow: 0 1px 0 var(--accent-soft);
}
.brand {
    font-weight: 600; font-size: 17px; white-space: nowrap;
}
.brand a { color: var(--ink); }
.brand span { color: var(--accent); }
.brand em { color: var(--ink-dim); font-style: normal; font-weight: 400; margin-left: 6px; }

.searchbar { flex: 1; display: flex; gap: 6px; align-items: center; max-width: 720px; position: relative; }
.searchbar input {
    flex: 1;
    height: 40px;
    border: 1px solid var(--rule);
    border-radius: 20px;
    padding: 0 18px;
    font-size: 16px;
    background: #fff;
    outline: none;
    transition: border-color .15s;
}
.searchbar input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

/* Search submit button — on desktop it's a plain iconbtn-style button sitting
   next to the input (preserves original topbar layout). Mobile overrides turn
   it into a gold circle floating inside the input. */
.searchbar-inline-btn {
    height: 40px; min-width: 40px; width: 40px;
    border: 1px solid var(--rule);
    background: var(--panel);
    color: var(--ink);
    border-radius: 8px;
    display: inline-flex; align-items: center; justify-content: center;
    cursor: pointer; flex-shrink: 0; padding: 0;
    transition: background .1s;
}
.searchbar-inline-btn svg { width: 18px; height: 18px; display: block; }
.searchbar-inline-btn:hover { background: var(--accent-soft); }

/* Panel toggles (☰ left / 📖 right) — default desktop look matches .iconbtn.
   Mobile overrides (gold gradient, active states) live in the 820px block. */
.panel-toggle {
    display: inline-flex;
    align-items: center; justify-content: center;
    height: 40px; min-width: 40px; width: 40px;
    border: 1px solid var(--rule);
    background: var(--panel);
    color: var(--ink);
    border-radius: 8px;
    cursor: pointer; flex-shrink: 0; padding: 0;
    transition: background .1s;
}
.panel-toggle svg { width: 20px; height: 20px; display: block; }
.panel-toggle:hover { background: var(--accent-soft); }

.iconbtn {
    height: 40px; min-width: 40px;
    border: 1px solid var(--rule);
    background: var(--panel);
    border-radius: 8px;
    cursor: pointer; font-size: 18px;
    color: var(--ink);
    transition: background .1s;
}
.iconbtn:hover { background: var(--accent-soft); }

/* Donate button — matches landing page btn-donate for brand consistency.
 * Gold gradient with subtle sheen on hover. */
.btn-donate {
    display: inline-flex; align-items: center;
    height: 36px; padding: 0 18px;
    border: 0; border-radius: 8px;
    background: linear-gradient(135deg, #d4a04a 0%, #f4d175 50%, #b8892b 100%);
    background-size: 200% 100%; background-position: 0% 50%;
    color: #fff !important; text-decoration: none;
    font-weight: 700; font-size: 14px; letter-spacing: .3px;
    text-shadow: 0 1px 1px rgba(125,90,16,.3);
    box-shadow: 0 1px 2px rgba(125,90,16,.15), inset 0 1px 0 rgba(255,255,255,.25);
    transition: all .2s;
}
.btn-donate:hover {
    background-position: 100% 50%;
    box-shadow: 0 4px 12px rgba(125,90,16,.25), inset 0 1px 0 rgba(255,255,255,.25);
    transform: translateY(-1px);
}
.btn-donate:active { transform: translateY(0); }
[data-theme="night"] .btn-donate { color: #1a1a1a !important; text-shadow: none; }

/* ---- settings menu ---- */
.menu-wrap { position: relative; }
.menu {
    position: absolute; top: calc(100% + 6px); right: 0;
    min-width: 200px;
    background: var(--panel);
    border: 1px solid var(--rule);
    border-radius: 8px;
    padding: 6px;
    box-shadow: 0 6px 24px rgba(0,0,0,.08);
    z-index: 30;
}
.menu[hidden] { display: none; }
.menu-head {
    font-size: 11px;
    color: var(--ink-dim);
    padding: 6px 10px 4px;
    text-transform: uppercase;
    letter-spacing: .5px;
}
.menu-item, .menu-link {
    display: flex; align-items: center; gap: 10px;
    width: 100%;
    padding: 7px 10px;
    border: 0; background: none;
    border-radius: 6px;
    font-size: 14px; font-family: inherit;
    color: var(--ink);
    cursor: pointer; text-align: left;
    text-decoration: none;
}
.menu-item:hover, .menu-link:hover { background: var(--accent-soft); }
.menu-item.active { color: var(--accent-strong); font-weight: 600; }
.menu-item.active::after { content: '✓'; margin-left: auto; color: var(--accent); }
.menu-sep { height: 1px; background: var(--rule); margin: 6px 0; }
.swatch {
    display: inline-block; width: 16px; height: 16px;
    border-radius: 4px; border: 1px solid var(--rule);
}
.swatch-gold  { background: linear-gradient(135deg, #fff 0 50%, #b8892b 50% 100%); }
.swatch-sepia { background: linear-gradient(135deg, #fafaf7 0 50%, #8b6f47 50% 100%); }
.swatch-night { background: linear-gradient(135deg, #1f1c18 0 50%, #d4a84b 50% 100%); }

/* ---------- layout ---------- */
.layout {
    display: grid;
    grid-template-columns: var(--sidebar-w) minmax(0, 1fr) var(--reader-w);
    height: calc(100vh - var(--topbar-h));
}
.layout.sidebar-collapsed { grid-template-columns: 0 minmax(0, 1fr) var(--reader-w); }
.layout.sidebar-collapsed .sidebar { transform: translateX(-100%); }

.sidebar {
    background: var(--sidebar-bg);
    border-right: 1px solid var(--rule);
    overflow-y: auto;
    transition: transform .2s ease;
}
.toc-head {
    position: sticky; top: 0;
    padding: 12px 16px;
    background: var(--sidebar-bg);
    border-bottom: 1px solid var(--rule);
    font-weight: 600;
    font-size: 14px;
    color: var(--accent);
    text-transform: uppercase;
    letter-spacing: .5px;
}
.toc { padding: 8px 0 24px 0; font-size: 14px; }
.toc details { padding: 2px 0; }
.toc details > summary {
    list-style: none;
    padding: 4px 14px;
    cursor: pointer;
    user-select: none;
    font-weight: 500;
    color: var(--ink);
}
.toc details > summary::before {
    content: '▸'; display: inline-block; width: 16px;
    transition: transform .15s;
    color: var(--ink-dim);
}
.toc details[open] > summary::before { transform: rotate(90deg); }
.toc .pitaka > summary { font-weight: 700; font-size: 15px; color: var(--accent); }
.toc .nikaya > summary { padding-left: 24px; color: var(--ink); font-weight: 600; font-size: 14px; }
.toc .book > summary { padding-left: 24px; font-size: 13.5px; font-weight: 500; }
.toc .nikaya .book > summary { padding-left: 42px; }
.toc .vol > summary {
    padding-left: 42px;
    font-size: 12.5px;
    display: flex;
    flex-direction: column;
    gap: 1px;
    color: var(--ink-dim);
}
.toc .nikaya .vol > summary { padding-left: 60px; }
.toc .vol-title { font-weight: 600; color: var(--ink); }
.toc .vol-pages { color: var(--ink-dim); font-weight: 400; font-size: 11px; }
.toc .vol-subtitle { font-size: 11px; color: var(--ink-dim); line-height: 1.3; }
.toc .abbr-tag {
    float: right;
    background: var(--accent-soft);
    color: var(--accent);
    padding: 0 8px;
    border-radius: 10px;
    font-size: 11px;
    font-weight: 500;
    margin-left: 8px;
    margin-right: 8px;
}
.toc .sections { padding: 2px 0 8px 0; }
.toc .nikaya .vol .sections { padding-left: 76px; }
.toc .vol .sections { padding-left: 58px; }
.toc .section-btn {
    width: 100%;
    text-align: left;
    border: 0; background: none;
    padding: 3px 12px 3px 0;
    cursor: pointer;
    display: flex; justify-content: space-between; align-items: baseline;
    gap: 8px;
    font-size: 12.5px;
    color: var(--ink);
    line-height: 1.45;
}
.toc .section-btn:hover { background: var(--accent-soft); color: var(--accent); }
.toc .section-btn .section-title {
    flex: 1; min-width: 0;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.toc .section-btn .section-page {
    color: var(--accent);
    font-weight: 600;
    font-size: 11.5px;
    font-variant-numeric: tabular-nums;
}
.toc .no-sections {
    padding: 4px 12px 4px 58px;
    color: var(--ink-dim);
    font-size: 12px;
    font-style: italic;
}
.toc ul { list-style: none; margin: 0; padding: 0; }

/* ---------- results ---------- */
.results {
    overflow-y: auto;
    padding: 20px 24px;
    background: var(--bg);
}
.welcome h1 { font-size: 28px; margin: 24px 0 8px; color: var(--accent); }
.welcome p { color: var(--ink-dim); max-width: 620px; }
.welcome .hint kbd {
    font-family: 'Sarabun', inherit;
    background: var(--panel); border: 1px solid var(--rule);
    padding: 1px 8px; border-radius: 4px; font-size: 14px;
}

.result-meta {
    padding: 8px 0 16px 0;
    color: var(--ink-dim);
    font-size: 14px;
    border-bottom: 1px solid var(--rule);
    margin-bottom: 12px;
}
.result {
    padding: 14px 0 14px 0;
    border-bottom: 1px solid var(--rule);
    cursor: pointer;
}
.result:hover { background: #fff; }
.result:hover .result-title { color: var(--link); text-decoration: underline; }
.result.active { background: #fff; border-left: 3px solid var(--accent); padding-left: 10px; }
.result-title { font-size: 17px; font-weight: 600; color: var(--link); margin: 0 0 2px; }
.result-cite {
    font-size: 13px;
    margin-bottom: 6px;
    font-family: inherit;
}
.result-cite .mm {
    background: var(--accent-soft);
    color: var(--accent);
    padding: 1px 10px;
    border-radius: 12px;
    font-weight: 600;
    font-size: 12.5px;
}
/* clamp to 3 lines */
.result-snippet {
    color: var(--ink);
    font-size: 15px;
    line-height: 1.55;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
    max-height: calc(1.55em * 3);
}

/* pitaka filter buttons */
.filters {
    display: flex; flex-wrap: wrap; gap: 8px;
    margin: 4px 0 14px;
}
.filter-btn {
    background: var(--panel);
    border: 1px solid var(--rule);
    color: var(--ink);
    padding: 6px 14px;
    border-radius: 18px;
    font-size: 14px;
    cursor: pointer;
    display: inline-flex; align-items: center; gap: 6px;
    transition: background .1s, border-color .1s;
}
.filter-btn:hover:not([disabled]):not(.dim) { background: var(--accent-soft); }
.filter-btn.active {
    background: var(--accent); color: #fff; border-color: var(--accent);
}
.filter-btn.active .badge { background: #fff3; color: #fff; }
.filter-btn.dim, .filter-btn[disabled] { opacity: .45; cursor: not-allowed; }
.filter-btn .badge {
    background: var(--accent-soft);
    color: var(--accent);
    padding: 1px 10px;
    border-radius: 10px;
    font-size: 12px;
    font-weight: 600;
}
mark { background: var(--mark); color: inherit; padding: 0 2px; border-radius: 2px; }

/* pagination */
.pager {
    display: flex; gap: 8px; justify-content: center;
    margin: 18px 0 40px;
}
.pager button {
    padding: 8px 14px; border: 1px solid var(--rule); background: var(--panel);
    border-radius: 6px; cursor: pointer;
}
.pager button[disabled] { opacity: .4; cursor: not-allowed; }
.pager .current { padding: 8px 12px; color: var(--ink-dim); font-size: 14px; }

/* ---------- reader ---------- */
.reader {
    background: var(--panel);
    border-left: 1px solid var(--rule);
    overflow-y: auto;
    display: flex; flex-direction: column;
}
.reader-empty { padding: 32px 24px; color: var(--ink-dim); font-size: 14px; }
.reader-head {
    position: sticky; top: 0; z-index: 5;
    background: var(--panel);
    border-bottom: 1px solid var(--rule);
    padding: 10px 16px;
    display: flex; align-items: center; gap: 8px;
}
.reader-cite {
    flex: 1; min-width: 0;
    font-size: 13px;
}
.reader-cite .section {
    font-weight: 600; font-size: 15px; color: var(--ink);
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.reader-cite .mm {
    background: var(--accent-soft);
    color: var(--accent);
    padding: 1px 10px;
    border-radius: 12px;
    font-weight: 600;
    font-size: 12px;
    margin-right: 6px;
}
.reader-cite .range {
    color: var(--ink-dim);
    font-size: 12px;
}
.navbtn {
    height: 34px; min-width: 34px;
    border: 1px solid var(--rule); background: var(--panel);
    border-radius: 6px; cursor: pointer; font-size: 16px;
}
.navbtn[disabled] { opacity: .3; cursor: not-allowed; }
.navbtn:hover:not([disabled]) { background: var(--accent-soft); }

.reader-body b.gloss {
    font-weight: 700;
    color: var(--accent);
}
.reader-body {
    padding: 20px 28px 80px;
    font-size: 17px; line-height: 1.85;
    white-space: pre-wrap;
    word-break: break-word;
    font-family: 'Sarabun', serif;
}

.crossref {
    margin: 24px 0;
    padding: 12px 16px;
    background: var(--accent-soft);
    border-left: 3px solid var(--accent);
    border-radius: 0 6px 6px 0;
    font-size: 14px;
}
.crossref h4 { margin: 0 0 6px; font-size: 14px; color: var(--accent); }
.crossref .mcu-match { margin: 4px 0; }
.crossref .mcu-match a { color: var(--link); }
.crossref .nomatch { color: #b5651d; }

/* ---------- responsive ---------- */

/* Desktop default: hide right toggle AND the inline 🔍 submit button.
   Desktop users press Enter to search; reader is always visible. Both
   controls flip to visible inside the mobile media query. */
#toggleReader { display: none; }
.searchbar-inline-btn { display: none; }
/* Theme/settings button — hidden per user request */
.menu-wrap { display: none; }

@media (max-width: 1100px) {
    :root { --reader-w: 40%; --sidebar-w: 240px; }
}

/* Mobile (<=820px) — 2-row topbar + sidebar/reader slide-in overlays.
   Desktop layout above this width is UNCHANGED from the original. */
@media (max-width: 820px) {
    :root { --sidebar-w: 0; --topbar-h: 108px; }

    /* Mobile styling for toggles — gold gradient pill */
    .panel-toggle {
        border-radius: 10px;
        background: linear-gradient(180deg, var(--panel) 0%, var(--accent-soft) 100%);
        color: var(--accent-strong);
        box-shadow: inset 0 1px 0 rgba(255,255,255,.7), 0 1px 1px rgba(125,90,16,.05);
    }
    .panel-toggle:hover { background: var(--accent-soft); border-color: var(--accent); }
    .panel-toggle.active {
        background: linear-gradient(135deg, #d4a04a 0%, #b8892b 100%);
        color: #fff;
        border-color: var(--accent-strong);
        box-shadow: 0 2px 4px rgba(125,90,16,.2);
    }
    [data-theme="night"] .panel-toggle.active {
        background: linear-gradient(135deg, #d4a84b 0%, #a07a25 100%);
        color: #1a1a1a;
    }
    /* Mobile: inline 🔍 becomes a gold circle floating inside the input */
    .searchbar-inline-btn {
        display: inline-flex;
        position: absolute;
        right: 5px; top: 50%; transform: translateY(-50%);
        width: 32px; height: 32px; min-width: 32px;
        border: 0; border-radius: 50%;
        background: linear-gradient(135deg, #d4a04a 0%, #f4d175 50%, #b8892b 100%);
        color: #fff;
        box-shadow: 0 1px 2px rgba(125,90,16,.25), inset 0 1px 0 rgba(255,255,255,.3);
    }
    .searchbar-inline-btn svg { width: 15px; height: 15px; }
    .searchbar-inline-btn:hover {
        background: linear-gradient(135deg, #b8892b 0%, #f4d175 50%, #d4a04a 100%);
        transform: translateY(-50%) scale(1.06);
    }
    /* Input gets padding for the floating inline button */
    .searchbar input { padding: 0 46px 0 18px; }

    /* 2-row topbar (brand+menu+donate on top, toggles+search below) */
    .topbar {
        flex-wrap: wrap;
        height: auto;
        min-height: var(--topbar-h);
        padding: 8px 12px;
        row-gap: 8px;
        gap: 8px;
        align-items: center;
    }
    .brand { order: 1; flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; font-size: 15px; }
    .menu-wrap { order: 2; }
    .btn-donate { order: 3; }
    /* Pseudo-break forces wrap before the toggles row */
    .topbar::before {
        content: '';
        flex: 0 0 100%;
        height: 0;
        order: 4;
    }
    #toggleSidebar { order: 5; }
    .searchbar { order: 6; flex: 1 1 0; margin: 0; max-width: none; min-width: 0; }
    #toggleReader { order: 7; display: inline-flex; }
    .searchbar-inline-btn { display: inline-flex; }

    /* Layout becomes single column — both panels slide-in from sides */
    .layout {
        grid-template-columns: 1fr;
        grid-template-rows: auto;
        height: calc(100vh - var(--topbar-h));
    }
    .sidebar {
        position: fixed;
        top: var(--topbar-h); left: 0; bottom: 0;
        width: min(300px, 82vw);
        z-index: 40;
        transform: translateX(-100%);
        transition: transform .22s ease-out;
        border-right: 1px solid var(--rule);
        box-shadow: 6px 0 24px rgba(0,0,0,.14);
    }
    .layout.sidebar-open .sidebar { transform: translateX(0); }

    .reader {
        position: fixed;
        top: var(--topbar-h); right: 0; bottom: 0;
        width: min(460px, 82vw);
        z-index: 40;
        transform: translateX(100%);
        transition: transform .22s ease-out;
        border-left: 1px solid var(--rule);
        border-top: 0;
        box-shadow: -6px 0 24px rgba(0,0,0,.14);
    }
    .layout.reader-open .reader { transform: translateX(0); }

    body.sidebar-open::before,
    body.reader-open::before {
        content: '';
        position: fixed;
        left: 0; right: 0; top: var(--topbar-h); bottom: 0;
        background: rgba(0,0,0,.28);
        z-index: 35;
    }

    .brand em { display: inline; }  /* keep subtitle — shows current section */
    .results.hidden { display: none; }
}
