:root {
    --ink: #1c1612;
    --muted: #6b5e52;
    --paper: #f6f1e8;
    --paper-2: #efe6d8;
    --card: #fffaf3;
    --line: #e0d4c4;
    --wine: #7a2e2a;
    --wine-dark: #5c1f1c;
    --wine-soft: #f3e4e0;
    --forest: #3d5a45;
    --warn: #9a6700;
    --warn-bg: #f8edc8;
    --danger: #9b1c1c;
    --radius: 12px;
    --shadow: 0 10px 30px rgba(28, 22, 18, 0.08);
    --sidebar: #241c18;
    --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
    --sans: "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
.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;
}
body {
    font-family: var(--sans);
    color: var(--ink);
    background: var(--paper);
    line-height: 1.5;
}
a { color: var(--wine); text-decoration: none; }
a:hover { text-decoration: underline; }
img { max-width: 100%; }

.app-shell {
    display: grid;
    grid-template-columns: 240px 1fr;
    min-height: 100vh;
}
.sidebar {
    background: var(--sidebar);
    color: #f3ebe3;
    padding: 22px 18px;
    display: flex;
    flex-direction: column;
    gap: 18px;
}
.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    color: inherit;
    text-decoration: none;
    font-weight: 650;
    letter-spacing: 0.04em;
}
.brand-mark {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    background: var(--wine);
    display: grid;
    place-items: center;
    font-size: 0.78rem;
    font-weight: 700;
}
.brand-mark-lg { width: 48px; height: 48px; margin: 0 auto 12px; font-size: 0.9rem; }
.side-nav { display: flex; flex-direction: column; gap: 4px; }
.side-nav a {
    color: #d9cdc2;
    text-decoration: none;
    padding: 8px 10px;
    border-radius: 8px;
}
.side-nav a:hover, .side-nav a.is-active { background: rgba(255,255,255,0.08); color: #fff; }
.side-book {
    border-top: 1px solid rgba(255,255,255,0.1);
    padding-top: 14px;
}
.side-book-label { margin: 0; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.08em; color: #b4a497; }
.side-book-title { margin: 4px 0 10px; font-weight: 650; }
.side-foot {
    margin-top: auto;
    display: flex;
    justify-content: space-between;
    gap: 8px;
    font-size: 0.85rem;
    color: #b4a497;
}
.side-foot a { color: #f3ebe3; }

.app-main { min-width: 0; }
.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    padding: 20px 28px 0;
}
.topbar h1 { margin: 0; font-family: var(--serif); font-size: 1.7rem; font-weight: 600; }
.topbar-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.page { padding: 20px 28px 48px; }

.lede, .muted, .hint { color: var(--muted); }
.hint { font-size: 0.88rem; }
.stack { display: flex; flex-direction: column; gap: 12px; }
label { display: flex; flex-direction: column; gap: 6px; font-weight: 600; font-size: 0.92rem; }
input, textarea, select {
    font: inherit;
    padding: 9px 11px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #fff;
    color: var(--ink);
}
textarea { resize: vertical; }
.btn, button.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    border: 1px solid var(--line);
    background: #fff;
    color: var(--ink);
    border-radius: 9px;
    padding: 8px 14px;
    font: inherit;
    cursor: pointer;
    text-decoration: none;
}
.btn:hover { text-decoration: none; background: var(--paper-2); }
.btn-primary { background: var(--wine); border-color: var(--wine); color: #fff; }
.btn-primary:hover { background: var(--wine-dark); }
.btn-small { padding: 5px 10px; font-size: 0.85rem; }
.btn-ghost { background: transparent; }
.btn-icon {
    border: 0; background: transparent; font-size: 1.4rem; cursor: pointer; color: var(--muted);
}
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.toolbar { margin-bottom: 16px; }

.card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 16px;
}
.card {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 18px;
    box-shadow: var(--shadow);
}
.card h2 { margin: 0 0 8px; font-size: 1.15rem; font-family: var(--serif); }
.card-foot { font-size: 0.85rem; color: var(--muted); margin: 12px 0 0; }
.cover { width: 100%; height: 140px; object-fit: cover; border-radius: 8px; margin-bottom: 10px; }
.badge {
    display: inline-block;
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 2px 8px;
    border-radius: 999px;
    background: var(--paper-2);
    color: var(--muted);
}
.badge-open { background: #e5efe8; color: var(--forest); }
.badge-closed { background: var(--wine-soft); color: var(--wine); }
.badge-warn { background: var(--warn-bg); color: var(--warn); }
.card-meta { display: flex; gap: 6px; margin-bottom: 8px; }

.empty-state {
    text-align: center;
    padding: 48px 16px;
    background: var(--card);
    border: 1px dashed var(--line);
    border-radius: var(--radius);
}
.empty-inline { margin: 12px 0; padding: 24px; }

.banner {
    padding: 12px 14px;
    border-radius: 10px;
    margin-bottom: 16px;
    display: flex;
    gap: 12px;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
}
.banner-warn { background: var(--warn-bg); color: #5c3d00; }
.alert { padding: 10px 12px; border-radius: 8px; margin: 0 0 12px; }
.alert-error { background: #fde8e8; color: var(--danger); }

.timeline { max-width: 820px; }
.timeline-node {
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 18px 20px;
    box-shadow: var(--shadow);
    position: relative;
}
.timeline-head { display: flex; gap: 10px; align-items: baseline; flex-wrap: wrap; }
.timeline-head h2 { margin: 0; font-family: var(--serif); font-size: 1.25rem; }
.timeline-gap {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    padding: 12px 8px 12px 28px;
    border-left: 2px dashed var(--line);
    margin-left: 18px;
}
.cast-line { font-size: 0.88rem; color: var(--forest); }
.text-chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 0; list-style: none; }
.text-chips li {
    background: var(--wine-soft);
    color: var(--wine-dark);
    border-radius: 999px;
    padding: 2px 10px;
    font-size: 0.8rem;
}

.table { width: 100%; border-collapse: collapse; background: var(--card); border-radius: var(--radius); overflow: hidden; }
.table th, .table td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); }
.check-list { display: flex; flex-direction: column; gap: 8px; }
.check-row { flex-direction: row; align-items: center; font-weight: 500; gap: 8px; }

.settings-form { max-width: 640px; display: flex; flex-direction: column; gap: 18px; }
fieldset { border: 1px solid var(--line); border-radius: var(--radius); background: var(--card); padding: 16px; }
legend { font-weight: 650; padding: 0 6px; }
.form-status { color: var(--forest); }

.auth-body {
    min-height: 100vh;
    display: grid;
    place-items: center;
    background:
        radial-gradient(900px 400px at 80% -10%, rgba(122, 46, 42, 0.18), transparent 60%),
        var(--paper);
}
.auth-card {
    width: min(420px, 92vw);
    background: var(--card);
    padding: 32px;
    border-radius: 18px;
    box-shadow: var(--shadow);
}
.auth-card h1 { font-family: var(--serif); margin: 0 0 6px; text-align: center; }
.auth-card .muted { text-align: center; }
.auth-card form { display: flex; flex-direction: column; gap: 12px; margin-top: 18px; }

.modal-root[hidden] { display: none; }
.modal-root { position: fixed; inset: 0; z-index: 40; display: grid; place-items: center; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(28,22,18,0.45); }
.modal {
    position: relative;
    width: min(720px, 94vw);
    max-height: 90vh;
    overflow: auto;
    background: var(--card);
    border-radius: 16px;
    box-shadow: 0 24px 60px rgba(0,0,0,0.25);
}
.modal-head {
    display: flex; justify-content: space-between; align-items: center;
    padding: 14px 18px; border-bottom: 1px solid var(--line);
}
.modal-head h2 { margin: 0; font-size: 1.1rem; }
.modal-body { padding: 18px; }

.reader { max-width: 680px; margin: 0 auto; font-family: var(--serif); }
.reader-title h2 { font-size: 2rem; margin: 0 0 12px; }
.reader-blurb { font-style: italic; color: var(--muted); }
.reader-chapter { padding: 28px 0; border-top: 1px solid var(--line); }
.reader-prose { font-size: 1.12rem; line-height: 1.75; }
.reader-appendix { margin-top: 20px; background: var(--paper-2); padding: 12px 14px; border-radius: 10px; }
.lyric { white-space: pre-wrap; font-family: var(--sans); font-size: 0.92rem; }
.reader-toolbar { display: flex; gap: 10px; align-items: end; margin-bottom: 20px; max-width: 680px; margin-left: auto; margin-right: auto; }

.chat-box { border-top: 1px solid var(--line); padding-top: 12px; margin-top: 12px; }
.chat-log { min-height: 40px; font-size: 0.9rem; color: var(--muted); }
.chat-input { display: flex; gap: 8px; }
.chat-input input { flex: 1; }
.hidden { display: none !important; }
.busy { opacity: 0.65; pointer-events: none; }

.pwa-banner {
    position: fixed;
    z-index: 50;
    left: 12px;
    right: 12px;
    bottom: 12px;
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    align-items: center;
    justify-content: space-between;
    background: var(--sidebar);
    color: #f3ebe3;
    padding: 14px 16px;
    border-radius: 14px;
    box-shadow: 0 16px 40px rgba(0, 0, 0, 0.28);
}
.pwa-banner[hidden] { display: none !important; }
.pwa-banner-text { display: flex; flex-direction: column; gap: 2px; min-width: 200px; }
.pwa-banner-text span { font-size: 0.88rem; color: #d9cdc2; }
.pwa-banner-actions { display: flex; gap: 8px; }
.pwa-banner .btn { background: #fff; }
.pwa-banner .btn-primary { background: var(--wine); color: #fff; border-color: var(--wine); }
@media (display-mode: standalone) {
    .pwa-banner { display: none !important; }
}

@media (max-width: 840px) {
    .app-shell { grid-template-columns: 1fr; }
    .sidebar { flex-direction: row; flex-wrap: wrap; align-items: center; }
    .side-nav { flex-direction: row; flex-wrap: wrap; }
    .side-book, .side-foot { width: 100%; }
    .topbar, .page { padding-left: 16px; padding-right: 16px; }
}
