/* ------------------------------------------------------------------
   Folio — a reading library
   Identity: oxblood and graphite on bone, after the house monograph
   style. Serif for the text, sans for the apparatus. Hairline rules.
   Signature: the recto/verso spread — listing and preview sit as two
   leaves of an open codex, divided by a gutter.
   ------------------------------------------------------------------ */

:root,
html[data-theme="folio"] {
    --paper:   #f3f2ee;   /* bone, cooled */
    --leaf:    #fbfaf8;   /* the page itself */
    --ink:     #26262a;   /* graphite */
    --quiet:   #75727a;   /* apparatus */
    --accent:  #6e1d1d;   /* oxblood */
    --rule:    #ddd9d2;   /* hairline */
    --gutter:  rgba(38, 38, 42, 0.09);
}

html[data-theme="ledger"] {
    --paper:  #eef0f3;
    --leaf:   #ffffff;
    --ink:    #1e2530;
    --quiet:  #6b7684;
    --accent: #1d4b6e;
    --rule:   #dbe0e6;
    --gutter: rgba(30, 37, 48, 0.09);
}

html[data-theme="garden"] {
    --paper:  #eef1ec;
    --leaf:   #fcfdfb;
    --ink:    #1e2a21;
    --quiet:  #6b7a6f;
    --accent: #2f5d3a;
    --rule:   #d9e0d8;
    --gutter: rgba(30, 42, 33, 0.09);
}

html[data-theme="night"] {
    --paper:  #17171a;
    --leaf:   #1f1f23;
    --ink:    #e6e3dd;
    --quiet:  #918d87;
    --accent: #c08a6a;
    --rule:   #33333a;
    --gutter: rgba(0, 0, 0, 0.4);
}

/* ---- Type ------------------------------------------------------- */

:root {
    --serif: "EB Garamond", Garamond, "Adobe Garamond Pro", "Iowan Old Style",
             "Palatino Linotype", Palatino, Georgia, serif;
    --sans:  ui-sans-serif, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --mono:  ui-monospace, "Cascadia Mono", Consolas, "Liberation Mono", monospace;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: var(--serif);
    font-size: 17px;
    line-height: 1.55;
    background: var(--paper);
    color: var(--ink);
    -webkit-font-smoothing: antialiased;
}

/* Apparatus: anything that is not the text itself */
.topbar, thead th, .file-desc, .detail-facts, .chip,
.btn, .btn-small, .meta-form input, .login-form input,
.meta-filename, .crumbs, .login-error {
    font-family: var(--sans);
}

/* ---- Running head ----------------------------------------------- */

.topbar {
    display: flex;
    align-items: baseline;
    gap: 1.25rem;
    padding: 0.85rem 1.5rem 0.7rem;
    background: var(--paper);
    border-bottom: 1px solid var(--rule);
}

.topbar h1 {
    font-family: var(--serif);
    font-size: 1.05rem;
    font-weight: 600;
    margin: 0;
    letter-spacing: 0.14em;
    text-transform: uppercase;
}

.running-head {
    font-family: var(--sans);
    font-size: 0.7rem;
    letter-spacing: 0.16em;
    text-transform: uppercase;
    color: var(--quiet);
}

.crumbs { flex: 1; font-size: 0.82rem; text-align: right; }
.crumbs a { color: var(--accent); text-decoration: none; }
.crumbs a:hover { text-decoration: underline; }
.crumbs .sep { color: var(--quiet); margin: 0 0.35rem; }

/* ---- Colour scheme picker --------------------------------------- */

.theme-picker { display: flex; gap: 0.4rem; }
.theme-picker button {
    width: 13px;
    height: 13px;
    border-radius: 50%;
    border: 1px solid var(--rule);
    cursor: pointer;
    padding: 0;
}
.theme-picker button[data-set-theme="folio"]  { background: #6e1d1d; }
.theme-picker button[data-set-theme="ledger"] { background: #1d4b6e; }
.theme-picker button[data-set-theme="garden"] { background: #2f5d3a; }
.theme-picker button[data-set-theme="night"]  { background: #17171a; }
.theme-picker button.active { box-shadow: 0 0 0 2px var(--paper), 0 0 0 3px var(--ink); }

/* ---- The spread ------------------------------------------------- */

.layout {
    display: flex;
    align-items: stretch;
    max-width: 1500px;
    margin: 0 auto;
    background: var(--leaf);
    border-left: 1px solid var(--rule);
    border-right: 1px solid var(--rule);
    min-height: calc(100vh - 3.2rem);
}

.listing { flex: 1; min-width: 0; }

/* The gutter: the fold between the two leaves */
.preview {
    flex: 1.15;
    min-width: 0;
    display: flex;
    flex-direction: column;
    border-left: 1px solid var(--rule);
    box-shadow: inset 14px 0 18px -16px var(--gutter);
    position: sticky;
    top: 0;
    align-self: flex-start;
    height: calc(100vh - 3.2rem);
}
.preview[hidden] { display: none; }

/* ---- Listing ---------------------------------------------------- */

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 0.7rem 1.5rem; }

thead th {
    font-size: 0.66rem;
    text-transform: uppercase;
    letter-spacing: 0.16em;
    font-weight: 600;
    color: var(--quiet);
    border-bottom: 1px solid var(--rule);
    padding-top: 1rem;
    padding-bottom: 0.5rem;
}

tbody tr { border-bottom: 1px solid var(--rule); }
tbody tr:last-child { border-bottom: none; }
tbody tr:hover { background: color-mix(in srgb, var(--accent) 4%, transparent); }

td { font-size: 0.95rem; color: var(--quiet); font-variant-numeric: tabular-nums; }
td a { color: var(--ink); text-decoration: none; }
td a:hover { color: var(--accent); }

.row-dir a {
    font-family: var(--sans);
    font-size: 0.82rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--quiet);
}
.row-dir a:hover { color: var(--accent); }

.empty {
    color: var(--quiet);
    text-align: center;
    padding: 4rem 1rem;
    font-style: italic;
}

.file-meta { display: flex; flex-direction: column; gap: 0.15rem; }
.file-title {
    font-family: var(--serif);
    font-size: 1.08rem;
    line-height: 1.3;
}
.file-desc { font-size: 0.8rem; color: var(--quiet); line-height: 1.45; }

/* ---- Chips ------------------------------------------------------ */

.filter-bar {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
    padding: 0.8rem 1.5rem;
    border-bottom: 1px solid var(--rule);
}

.chip {
    font-size: 0.7rem;
    letter-spacing: 0.06em;
    padding: 0.12rem 0.6rem;
    border-radius: 2px;
    border: 1px solid var(--rule);
    background: transparent;
    color: var(--quiet);
    cursor: pointer;
}
.chip:hover { border-color: var(--accent); color: var(--accent); }
.chip-cat { text-transform: uppercase; letter-spacing: 0.11em; font-size: 0.65rem; }
.chip-active { background: var(--accent); border-color: var(--accent); color: var(--leaf); }
.chip-active:hover { color: var(--leaf); }
.chip-clear { border-style: dashed; }
.chip-mini { font-size: 0.65rem; }
a.chip { text-decoration: none; display: inline-flex; align-items: center; gap: 0.35em; }
.chip-count { font-variant-numeric: tabular-nums; opacity: 0.6; font-size: 0.9em; }

.archive-title {
    font-size: 1.5rem;
    font-weight: 500;
    margin: 1.5rem 1.5rem 0.2rem;
    letter-spacing: -0.01em;
}
.archive-note {
    font-family: var(--sans);
    font-size: 0.7rem;
    letter-spacing: 0.13em;
    text-transform: uppercase;
    color: var(--quiet);
    margin: 0 1.5rem 1.25rem;
}
.file-chips { display: flex; flex-wrap: wrap; gap: 0.25rem; margin-top: 0.3rem; }

/* ---- Controls --------------------------------------------------- */

.btn, .btn-small {
    font-family: var(--sans);
    border: 1px solid var(--accent);
    background: var(--accent);
    color: var(--leaf);
    border-radius: 2px;
    cursor: pointer;
    padding: 0.32rem 0.9rem;
    font-size: 0.8rem;
    letter-spacing: 0.04em;
}
.btn-small { font-size: 0.72rem; padding: 0.18rem 0.6rem; }
.btn-ghost { background: transparent; color: var(--accent); }
.btn:hover, .btn-small:hover { background: var(--ink); border-color: var(--ink); color: var(--leaf); }

.row-actions { white-space: nowrap; text-align: right; }
.row-actions .btn-small { margin-left: 0.25rem; }
.btn-small.btn-ghost { border-color: var(--rule); color: var(--quiet); }
.btn-small.btn-ghost:hover { background: transparent; color: var(--accent); border-color: var(--accent); }

/* ---- Admin ------------------------------------------------------ */

.admin-link {
    font-family: var(--sans);
    font-size: 0.7rem;
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: var(--quiet);
    text-decoration: none;
    cursor: pointer;
    list-style: none;
}
.admin-link:hover { color: var(--accent); }
.login-box { position: relative; }
.login-box summary::-webkit-details-marker { display: none; }

.login-form {
    position: absolute;
    right: 0;
    top: 1.7rem;
    background: var(--leaf);
    border: 1px solid var(--rule);
    border-radius: 2px;
    padding: 0.7rem;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.13);
    z-index: 10;
}
.login-form input[type="text"],
.login-form input[type="password"],
.meta-form input[type="text"] {
    font-family: var(--sans);
    font-size: 0.85rem;
    padding: 0.35rem 0.5rem;
    border: 1px solid var(--rule);
    border-radius: 2px;
    background: var(--paper);
    color: var(--ink);
}
.login-form input { width: 12rem; }
.login-form input:focus,
.meta-form input:focus { outline: 1px solid var(--accent); outline-offset: 0; }
.login-error { font-size: 0.72rem; color: var(--accent); max-width: 12rem; }

.meta-form { display: flex; flex-direction: column; gap: 0.35rem; padding: 0.25rem 0; }
.meta-form-actions { display: flex; align-items: center; gap: 0.5rem; }
.meta-filename { font-size: 0.68rem; color: var(--quiet); margin-left: auto; font-family: var(--mono); }

/* ---- Preview leaf ----------------------------------------------- */

.preview-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.85rem 1.5rem 0.7rem;
    border-bottom: 1px solid var(--rule);
}
#preview-name {
    font-family: var(--serif);
    font-size: 1.05rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.preview-actions { display: flex; gap: 0.4rem; }

.preview-body {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1.25rem;
    overflow: auto;
    min-height: 0;
}
.preview-body img {
    max-width: 100%;
    max-height: 100%;
    box-shadow: 0 2px 14px rgba(0, 0, 0, 0.16);
}
.preview-body iframe { width: 100%; height: 100%; border: none; background: var(--leaf); }

.print-frame { position: absolute; width: 0; height: 0; border: 0; visibility: hidden; }

/* ---- Detail page ------------------------------------------------ */

.site-home { color: inherit; text-decoration: none; }

.detail {
    max-width: 660px;
    margin: 0 auto;
    padding: 2.5rem 1.5rem 4rem;
    background: var(--leaf);
    min-height: calc(100vh - 3.2rem);
}
.detail-title {
    font-size: 1.9rem;
    font-weight: 500;
    line-height: 1.2;
    margin: 0 0 0.5rem;
    letter-spacing: -0.01em;
}
.detail-desc { margin: 0 0 0.9rem; color: var(--quiet); font-size: 1.02rem; }
.detail-chips { display: flex; flex-wrap: wrap; gap: 0.3rem; margin: 0 0 1.6rem; }
.detail-chips .chip { cursor: default; }

.detail-media { margin: 0 0 1rem; }
.detail-media img { max-width: 100%; height: auto; box-shadow: 0 2px 14px rgba(0, 0, 0, 0.16); }
.detail-media iframe {
    width: 100%;
    height: 78vh;
    border: 1px solid var(--rule);
    background: var(--leaf);
}

.detail-facts {
    font-size: 0.7rem;
    letter-spacing: 0.13em;
    text-transform: uppercase;
    color: var(--quiet);
    margin: 0 0 1.1rem;
    padding-top: 0.7rem;
    border-top: 1px solid var(--rule);
}
.detail-actions { display: flex; gap: 0.5rem; }
.detail-actions .btn { text-decoration: none; display: inline-block; }

/* ---- Rendered markdown ------------------------------------------ */

.md-body { background: var(--leaf); }
.md-content { max-width: 62ch; margin: 0 auto; padding: 1.5rem; line-height: 1.62; }
.detail .md-content { padding: 0; max-width: none; }
.md-content h1, .md-content h2, .md-content h3 {
    font-weight: 500;
    line-height: 1.25;
    margin: 1.5em 0 0.45em;
}
.md-content h1 { font-size: 1.6rem; }
.md-content h2 { font-size: 1.3rem; }
.md-content h3 { font-size: 1.08rem; }
.md-content p { margin: 0.75em 0; }
.md-content a { color: var(--accent); }
.md-content blockquote {
    margin: 1.1em 0;
    padding: 0.1em 0 0.1em 1.1em;
    border-left: 2px solid var(--accent);
    color: var(--quiet);
    font-style: italic;
}
.md-content code {
    font-family: var(--mono);
    font-size: 0.85em;
    background: var(--paper);
    border: 1px solid var(--rule);
    border-radius: 2px;
    padding: 0.08em 0.32em;
}
.md-content pre {
    background: var(--paper);
    border: 1px solid var(--rule);
    border-radius: 2px;
    padding: 0.85em 1em;
    overflow-x: auto;
    font-size: 0.88rem;
}
.md-content pre code { background: none; border: none; padding: 0; }
.md-content table { border-collapse: collapse; margin: 1.2em 0; font-size: 0.92rem; }
.md-content th, .md-content td { border: 1px solid var(--rule); padding: 0.4em 0.8em; }
.md-content th { font-family: var(--sans); font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.08em; }
.md-content img { max-width: 100%; }
.md-content hr { border: none; border-top: 1px solid var(--rule); margin: 2em 0; }
.listing > .md-content { max-width: 74ch; padding: 1.75rem 1.5rem 3rem; }
.listing > .md-content pre { white-space: pre-wrap; word-break: break-word; }

/* ---- Accessibility and responsiveness --------------------------- */

a:focus-visible, button:focus-visible, input:focus-visible, summary:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

@media (max-width: 900px) {
    body { font-size: 16px; }
    th, td { padding: 0.6rem 1rem; }
    .topbar { padding: 0.7rem 1rem; flex-wrap: wrap; gap: 0.6rem; }
    .running-head { display: none; }
    .crumbs { text-align: left; }
    .layout { flex-direction: column; min-height: 0; }
    .preview {
        position: static;
        height: auto;
        min-height: 60vh;
        width: 100%;
        border-left: none;
        border-top: 1px solid var(--rule);
        box-shadow: none;
    }
    .filter-bar { padding: 0.7rem 1rem; }
    .detail { padding: 1.75rem 1.25rem 3rem; }
    .detail-title { font-size: 1.5rem; }
}

@media (prefers-reduced-motion: reduce) {
    * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

@media print {
    .topbar, .detail-actions, .print-frame, .filter-bar { display: none; }
    .detail { padding: 0; max-width: none; background: none; }
    .md-content { max-width: none; padding: 0; }
}

/* ---- Password hash tool ----------------------------------------- */

.hash-form { display: flex; gap: 0.5rem; margin: 0 0 1.25rem; flex-wrap: wrap; }
.hash-form input[type="text"] {
    font-family: var(--mono);
    font-size: 0.9rem;
    padding: 0.4rem 0.6rem;
    border: 1px solid var(--rule);
    border-radius: 2px;
    background: var(--paper);
    color: var(--ink);
    flex: 1;
    min-width: 16rem;
}
.hash-form input:focus { outline: 1px solid var(--accent); }
.hash-out {
    background: var(--paper);
    border: 1px solid var(--rule);
    border-radius: 2px;
    padding: 0.8rem 1rem;
    overflow-x: auto;
    font-size: 0.82rem;
    white-space: pre-wrap;
    word-break: break-all;
}

/* ---- Accounts screen -------------------------------------------- */

.msg {
    font-family: var(--sans);
    font-size: 0.85rem;
    padding: 0.6rem 0.9rem;
    border: 1px solid var(--rule);
    border-left-width: 3px;
    border-radius: 2px;
    margin: 0 0 1.25rem;
}
.msg-ok { border-left-color: var(--accent); }
.msg-bad { border-left-color: #b91c1c; color: #7f1d1d; }

.stack-form { display: flex; flex-direction: column; gap: 0.45rem; margin: 0 0 2rem; max-width: 24rem; }
.stack-form input, .stack-form select {
    font-family: var(--sans);
    font-size: 0.9rem;
    padding: 0.4rem 0.55rem;
    border: 1px solid var(--rule);
    border-radius: 2px;
    background: var(--paper);
    color: var(--ink);
}
.stack-form input:focus, .stack-form select:focus { outline: 1px solid var(--accent); }

.accounts { width: 100%; margin: 0 0 2rem; }
.accounts th, .accounts td { padding: 0.5rem 0; }
.accounts th:first-child, .accounts td:first-child { padding-left: 0; }
.inline-form { display: inline; }
.detail h2.detail-title { font-size: 1.15rem; margin-top: 2rem; }
.detail h2.detail-title:first-of-type { margin-top: 0; }

/* ---- Settings screen -------------------------------------------- */

.settings-form label {
    font-family: var(--sans);
    font-size: 0.7rem;
    letter-spacing: 0.1em;
    text-transform: uppercase;
    color: var(--quiet);
    margin-top: 0.5rem;
}
.settings-form label:first-of-type { margin-top: 0; }
.check-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    text-transform: none !important;
    letter-spacing: normal !important;
    font-size: 0.85rem !important;
}

/* ---- Crawlers screen -------------------------------------------- */

.field-note {
    font-family: var(--sans);
    font-size: 0.78rem;
    color: var(--quiet);
    margin: -0.1rem 0 0.6rem;
}
.field-note a { color: var(--accent); }
.stack-form textarea {
    font-family: var(--sans);
    font-size: 0.9rem;
    padding: 0.4rem 0.55rem;
    border: 1px solid var(--rule);
    border-radius: 2px;
    background: var(--paper);
    color: var(--ink);
    resize: vertical;
}
.stack-form textarea:focus { outline: 1px solid var(--accent); }

/* ---- Installer next-steps list ---------------------------------- */

.install-next {
    font-family: var(--sans);
    font-size: 0.9rem;
    padding-left: 1.25rem;
    color: var(--ink);
}
.install-next li { margin: 0.35em 0; }
.install-next code {
    font-family: var(--mono);
    font-size: 0.88em;
    background: var(--paper);
    border: 1px solid var(--rule);
    border-radius: 2px;
    padding: 0.05em 0.35em;
}

.install-warn { margin: 0.9rem 1.5rem; }
