:root {
    --commentary: #4B71B7;
    --tanakh: #004E5F;
    --mishnah: #5A99B7;
    --talmud: #CCB479;
    --midrash: #5D956F;
    --halakhah: #802F3E;
    --kabbalah: #594176;
    --liturgy: #AB4E66;
    --philosophy: #7F85A9;
    --taanitic: #00827F;
    --chasidut: #97B386;
    --mussar: #7C416F;
    --responsa: #CB6158;
}

body {
    margin: 0;
    display: flex;
    flex-direction: column;
    height: 100vh;
}

#app {
    flex: 1;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.page-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    min-height: 120px;
}

.page-spinner {
    width: 40px;
    height: 40px;
    border: 4px solid #e0e0e0;
    border-top-color: #8a6a2a;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.refs-spinner {
    width: 24px;
    height: 24px;
    border-width: 3px;
}

header {
    display: flex;
    align-items: center;
    position: relative;
    box-shadow: 0px 5px 5px -5px;
    padding: 6px 20px;
    box-sizing: border-box;
}

.header-brand {
    display: flex;
    text-align: center;
    text-decoration: none;
}

#logo {
    width: 75px;
    height: 75px;
    display: inline-block;
}

#title {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
    font-size: 28px;
    font-weight: 700;
    letter-spacing: 0.3px;
    display: inline;
    color: #18345d;
}

#main-nav {
    position: absolute;
    inset-inline-end: 20px;
    top: 50%;
    transform: translateY(-50%);
}

/* ── User nav (sign in / sign up / account) ─────────── */

.user-nav {
    display: flex;
    align-items: center;
    gap: 10px;
}

.user-nav__name {
    color: #18345d;
    font-weight: 600;
    font-size: 14px;
}

.user-nav__btn {
    background: none;
    border: 1px solid #18345d;
    color: #18345d;
    padding: 6px 14px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 13px;
    transition: background 0.15s, color 0.15s;
}

.user-nav__btn:hover {
    background: #18345d;
    color: #fff;
}

.user-nav__link {
    text-decoration: none;
    color: #18345d;
    padding: 6px 14px;
    border: 1px solid transparent;
    border-radius: 4px;
    font-size: 13px;
    transition: background 0.15s, color 0.15s;
}

.user-nav__link:hover {
    background: #f0ede2;
}

.user-nav__link--signup {
    background: #18345d;
    color: #fff;
}

.user-nav__link--signup:hover {
    background: #0e2040;
    color: #fff;
}

/* ── Auth pages (login / signup) ─────────────────────── */

.auth-page {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-height: 100%;
    padding: 40px 20px;
    box-sizing: border-box;
    overflow: auto;
}

.auth-card {
    width: 100%;
    max-width: 360px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.12);
    padding: 32px 28px;
    box-sizing: border-box;
}

.auth-title {
    text-align: center;
    color: #18345d;
    margin: 0 0 24px;
    font-size: 22px;
}

.auth-form {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.auth-input {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 14px;
    box-sizing: border-box;
    font-family: inherit;
}

.auth-input:focus {
    outline: none;
    border-color: #18345d;
}

.auth-error {
    color: #802f3e;
    font-size: 13px;
    margin: 0;
}

.auth-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    background: #18345d;
    color: #fff;
    border: none;
    padding: 10px;
    border-radius: 4px;
    font-size: 14px;
    cursor: pointer;
    text-decoration: none;
    transition: background 0.15s;
}

.auth-btn:hover {
    background: #0e2040;
}

.auth-btn:disabled {
    opacity: 0.6;
    cursor: default;
}

.auth-btn--google {
    background: #fff;
    color: #3c4043;
    border: 1px solid #dadce0;
}

.auth-btn--google:hover {
    background: #f7f7f7;
}

.auth-divider {
    display: flex;
    align-items: center;
    color: #999;
    font-size: 12px;
    margin: 16px 0;
}

.auth-divider::before,
.auth-divider::after {
    content: '';
    flex: 1;
    border-bottom: 1px solid #e0e0e0;
}

.auth-divider span {
    padding: 0 10px;
}

.auth-footer {
    text-align: center;
    font-size: 13px;
    color: #666;
    margin: 16px 0 0;
}

.auth-link {
    color: #18345d;
    font-weight: 600;
    text-decoration: none;
}

.auth-link:hover {
    text-decoration: underline;
}

.pdf-reader {
    display: flex;
    width: 100%;
    flex: 1;
    overflow: hidden;
}

.chat-panel {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
}

.chat-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 16px;
    position: sticky;
    top: 0;
    z-index: 10;
    background: #18345d;
    color: #fff;
}

.chat-panel-title {
    font-size: 14px;
    font-weight: 600;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.chat-empty-hint {
    color: #888;
    font-size: 13px;
    text-align: center;
    margin: auto;
    max-width: 480px;
}

.chat-message {
    max-width: 70%;
    padding: 8px 14px;
    border-radius: 10px;
    font-size: 14px;
    line-height: 1.5;
}

.chat-message p {
    margin: 0;
}

.chat-markdown p {
    margin: 0 0 8px;
}

.chat-markdown p:last-child {
    margin-bottom: 0;
}

.chat-markdown ul,
.chat-markdown ol {
    margin: 4px 0 8px;
    padding-inline-start: 20px;
}

.chat-markdown li {
    margin-bottom: 2px;
}

.chat-markdown h1,
.chat-markdown h2,
.chat-markdown h3 {
    margin: 10px 0 6px;
    font-size: 1.05em;
}

.chat-markdown h1:first-child,
.chat-markdown h2:first-child,
.chat-markdown h3:first-child {
    margin-top: 0;
}

.chat-markdown strong {
    font-weight: 600;
}

.chat-markdown code {
    background: rgba(0, 0, 0, 0.07);
    padding: 1px 4px;
    border-radius: 3px;
    font-size: 0.9em;
}

.chat-markdown pre {
    background: rgba(0, 0, 0, 0.07);
    padding: 8px;
    border-radius: 4px;
    overflow-x: auto;
    margin: 6px 0;
}

.chat-markdown pre code {
    background: none;
    padding: 0;
}

.chat-markdown blockquote {
    margin: 6px 0;
    padding-inline-start: 10px;
    border-inline-start: 3px solid rgba(0, 0, 0, 0.15);
    color: inherit;
    opacity: 0.85;
}

.chat-markdown a {
    color: inherit;
    text-decoration: underline;
}

.chat-message--user {
    align-self: flex-end;
    background: #18345d;
    color: #fff;
}

.chat-message--assistant {
    align-self: flex-start;
    background: #f0ede0;
    color: #222;
}

.chat-message--pending {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 10px 14px;
}

.chat-input-row {
    flex-shrink: 0;
    display: flex;
    gap: 8px;
    padding: 12px 16px;
    border-top: 1px solid #e8e0cc;
}

.chat-input {
    flex: 1;
    resize: none;
    padding: 8px 12px;
    border: 1px solid #ccc;
    border-radius: 4px;
    font-size: 14px;
    font-family: inherit;
}

.chat-send-btn {
    background: #18345d;
    border: 1px solid #18345d;
    color: #fff;
    padding: 6px 18px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 13px;
    align-self: flex-end;
}

.chat-send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.chat-navigate-btn {
    display: block;
    margin-top: 8px;
    background: #fff;
    border: 1px solid #18345d;
    color: #18345d;
    padding: 6px 14px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 600;
}

.chat-navigate-btn:hover {
    background: #18345d;
    color: #fff;
}

.page-scan-pending-banner {
    margin: 40px auto;
    max-width: 420px;
    padding: 20px 24px;
    background: #f0ede0;
    border: 1px solid #d8d2b8;
    border-radius: 8px;
    color: #444;
    font-size: 14px;
    line-height: 1.5;
    text-align: center;
}

.reader-page-container {
    position: relative;
    display: inline-block;
    max-width: 100%;
    margin: 20px;
}

.reader-page-image {
    max-width: 100%;
    height: auto;
    display: block;
}

.reader-page-container .boxes-layer,
.reader-page-container .highlighted-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.reader-page-container .boxes-layer {
    pointer-events: auto;
}

.reader-page-container .box {
    pointer-events: auto;
}

.highlighted-box, .hover-box {
    background-color: var(--talmud);
    border: 1px solid var(--talmud);
    box-sizing: border-box;
    opacity: 0.2;
    position: absolute;
}

.box {
    position: absolute;
    transition: opacity .1s;
}

.boxes-layer {
    z-index: 2;
}

.accordion {
    font-size: 25px;
    text-align: justify;
}

.accordion-title {
    background-color: #eee;
    cursor: pointer;
    padding: 18px;
    width: 90%;
    border: none;
    outline: none;
    transition: 0.4s;
}

.accordion-title:hover {
    background-color: #ccc;
}

.accordion-panel {
    padding: 0 18px;
    overflow: hidden;
    transition: 0.4s;
}

.related-text {
    display: block;
    font-family: monospace;
    font-size: 25px;
    text-align: justify;
    margin-bottom: 18px;
}

.category, .sefara-link {
    margin-bottom: 18px;
}

.category-name {
    margin-bottom: 18px;
    padding: 5px;
}

.sefaria-link {
    transition: 0.3s;
}

.sefara-link:link, .sefara-link:active, .sefara-link:visited {
    text-decoration: none;
    background-color: #fff;
    color: #18345d;
}

.sefara-link:hover {
    text-decoration: none;
    color: #fff;
    background-color: #18345d;
}

/* ── Tractate Page ──────────────────────────────────── */

.tractate-page {
    max-width: 900px;
    margin: 0 auto;
    padding: 24px 20px 48px;
}

.tractate-nav {
    display: flex;
    align-items: center;
    gap: 14px;
    margin-bottom: 28px;
}

.back-btn {
    background: none;
    border: 1px solid #18345d;
    color: #18345d;
    padding: 6px 14px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 13px;
    transition: background 0.15s, color 0.15s;
}

.back-btn:hover {
    background: #18345d;
    color: #fff;
}

.tractate-breadcrumb {
    font-size: 12px;
    color: #aaa;
    letter-spacing: 0.5px;
}

.tractate-header {
    text-align: center;
    margin-bottom: 32px;
    padding-bottom: 20px;
    border-bottom: 2px solid #e8e0cc;
}

.tractate-he {
    font-size: 40px;
    color: #18345d;
    margin: 0;
    font-weight: bold;
    direction: rtl;
}

.tractate-en {
    font-size: 15px;
    color: #888;
    margin: 6px 0 0;
    letter-spacing: 1px;
}

.tractate-count {
    text-align: center;
    font-size: 13px;
    color: #aaa;
    margin: 0 0 20px;
    letter-spacing: 0.5px;
}

.amud-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: center;
}

.amud-card {
    width: 80px;
    height: 90px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    background: linear-gradient(160deg, #d9b55a 0%, #b8912e 60%, #8b6a18 100%);
    border: none;
    border-radius: 4px;
    cursor: pointer;
    box-shadow: 2px 3px 8px rgba(0,0,0,0.22);
    transition: transform 0.15s ease, box-shadow 0.15s ease;
    color: #fff;
}

.amud-card:hover {
    transform: translateY(-5px);
    box-shadow: 2px 8px 16px rgba(0,0,0,0.3);
}

.amud-he {
    font-size: 24px;
    font-weight: 700;
    line-height: 1;
    text-shadow: 0 1px 3px rgba(0,0,0,0.4);
    direction: rtl;
    letter-spacing: 1px;
}

.amud-latin {
    font-size: 11px;
    opacity: 0.65;
    letter-spacing: 0.5px;
}

.tractate-wip {
    text-align: center;
    padding: 60px 20px;
    color: #888;
}

.tractate-wip-icon {
    font-size: 52px;
    margin-bottom: 16px;
}

.tractate-wip-title {
    font-size: 20px;
    color: #555;
    margin: 0 0 12px;
    font-weight: 600;
}

.tractate-wip-text {
    font-size: 14px;
    color: #aaa;
    line-height: 1.7;
    margin: 0;
}

/* ── Viewport lock for library home ────────────────── */

body.library-page {
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
    margin: 0;
}

body.library-page header {
    flex-shrink: 0;
}

body.library-page #app {
    flex: 1;
    min-height: 0;
    overflow: hidden;
}

/* ── Library / Homepage ─────────────────────────────── */

.library-home {
    display: flex;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

.library-pane {
    width: 50%;
    height: 100%;
    display: flex;
    flex-direction: column;
    gap: 1.5vh;
    padding: 2vh 2%;
    box-sizing: border-box;
    border-right: 1px solid #e8e0cc;
    overflow: hidden;
}

.library-heading-wrap {
    text-align: center;
    flex-shrink: 0;
}

.library-heading {
    font-size: 3vh;
    color: #18345d;
    margin: 0;
    font-weight: bold;
    letter-spacing: 2px;
}

.library-subheading {
    font-size: 1.2vh;
    color: #888;
    margin: 0.5vh 0 0;
    letter-spacing: 1px;
    text-transform: uppercase;
}

.home-bookshelf {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-wrap: nowrap;
    align-items: stretch;
    gap: 0.4%;
    padding-bottom: 2%;
    border-bottom: 0.4vh solid #6b4c11;
    border-radius: 0 0 2px 2px;
    background: linear-gradient(to bottom, transparent 94%, rgba(107,76,17,0.15) 100%);
    overflow: hidden;
    box-sizing: border-box;
}

.library-promo {
    width: 50%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 5%;
    gap: 5%;
    box-sizing: border-box;
}

.promo-text {
    font-size: 2vw;
    color: #18345d;
    text-align: center;
    line-height: 1.7;
    max-width: 80%;
    margin: 0;
    font-weight: 500;
}

.dafyomi-btn {
    background: #18345d;
    color: #fff;
    border: none;
    padding: 2% 6%;
    font-size: 1.4vw;
    border-radius: 6px;
    cursor: pointer;
    letter-spacing: 0.4px;
    transition: background 0.15s, transform 0.12s;
}

.dafyomi-btn:hover {
    background: #0e2040;
    transform: translateY(-4%);
}

.book {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 2px 5px 5px 2px;
    box-shadow: 2px 3px 6px rgba(0,0,0,0.28), inset -2px 0 4px rgba(0,0,0,0.12);
    border-left: 3px solid rgba(0,0,0,0.18);
    transition: transform 0.18s ease, box-shadow 0.18s ease;
    position: relative;
    overflow: hidden;
}

.book::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    background: linear-gradient(to right, rgba(255,255,255,0.12) 0%, transparent 30%);
    pointer-events: none;
}

.book-available {
    background: linear-gradient(170deg, #d9b55a 0%, #b8912e 50%, #8b6a18 100%);
    cursor: pointer;
}

.book-available:hover {
    transform: translateY(-8%);
    box-shadow: 3px 14px 22px rgba(0,0,0,0.38), inset -2px 0 4px rgba(0,0,0,0.12);
}

.book-unavailable {
    background: linear-gradient(170deg, #d0ccc5 0%, #b0aba3 100%);
    cursor: default;
    opacity: 0.42;
}

.book-title-he {
    writing-mode: sideways-lr;
    font-size: 3vh;
    color: rgba(255,255,255,0.92);
    font-weight: 700;
    text-shadow: 0 1px 3px rgba(0,0,0,0.5);
    letter-spacing: 0.5px;
    max-height: 90%;
    overflow: hidden;
    direction: rtl;
}

/* ── Refs panel ─────────────────────────────────────── */

.refs-panel {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
    box-shadow: inset -1px 0px 10px -5px;
}

.refs-panel-header {
    position: sticky;
    top: 0;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    background: #18345d;
    color: #fff;
    padding: 10px 14px;
    flex-shrink: 0;
}

.refs-panel-title {
    font-size: 14px;
    font-weight: 600;
    line-height: 1.3;
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.refs-panel-title--link {
    cursor: pointer;
    text-decoration: underline;
    text-underline-offset: 3px;
    text-decoration-color: rgba(255,255,255,0.4);
}

.refs-panel-title--link:hover {
    text-decoration-color: rgba(255,255,255,0.9);
}

.chat-open-btn {
    background: none;
    border: 1px solid rgba(255,255,255,0.5);
    color: #fff;
    padding: 4px 10px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    flex-shrink: 0;
    transition: background 0.15s, border-color 0.15s;
}

.chat-open-btn:hover {
    background: rgba(255,255,255,0.15);
    border-color: #fff;
}

.refs-close-btn {
    background: none;
    border: none;
    color: rgba(255,255,255,0.7);
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
    padding: 0 2px;
    flex-shrink: 0;
    transition: color 0.15s;
}

.refs-close-btn:hover {
    color: #fff;
}

.refs-panel-body {
    flex: 1;
    overflow-y: auto;
    padding: 0 16px 16px;
}

.page-copyright {
    position: sticky;
    text-align: center;
    font-size: 11px;
    color: #888;
    padding: 6px 0;
    bottom: -1px;
    background-color: white;
}