/* === Fonts === */
@font-face {
    font-family: "Iosevka Etoile";
    src: url("/_/fonts/IosevkaEtoile.woff2") format("woff2");
    font-display: swap;
}
@font-face {
    font-family: "Iosevka Slab";
    src: url("/_/fonts/IosevkaSlab.woff2") format("woff2");
    font-display: swap;
}

/* === Theme === */
:root {
    --bg: #2e2e2e;
    --bg-panel: #434343;
    --bg-panel-hover: #585858;
    --text: #e6e6e6;
    --text-muted: #cfcfcf;
    --primary: #87458a;
    --primary-hover: #d64d95;
    --secondary: #c48401;
    --link: #01b6c4;
    --link-hover: #d6d24d;
    --danger: #c40141;
    --danger-hover: #d03467;

    --border: 1px solid var(--secondary);
    --border-dashed: 1px dashed var(--secondary);

    --space-1: 0.25rem;
    --space-2: 0.5rem;
    --space-3: 0.75rem;
    --space-4: 1rem;
    --space-5: 1.5rem;

    --font-xs: 0.75rem;
    --font-sm: 0.85rem;
}

/* === Base === */
body {
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    margin: 0;
    padding: 0;
    overflow: auto;
    font: 1rem "Iosevka Etoile", monospace;
    display: flex;
    flex-direction: column;
}
* { box-sizing: border-box; margin: 0; padding: 0; }

a { color: var(--text); text-decoration: none; }
a:hover { color: var(--primary-hover); }
.content a { color: var(--link); }
.content a:hover { color: var(--link-hover); }
.content a.broken { color: var(--primary-hover); text-decoration: line-through; }
.content a.broken:hover { color: var(--link-hover); }

hr { border: none; border-top: var(--border-dashed); margin: var(--space-4) 0; }

/* === Layout primitives ===
   .row and .col are shared flex recipes; gap-* modifiers cover the cases
   where the default rhythm doesn't fit. */
.row { display: flex; align-items: center; gap: var(--space-2); }
.col { display: flex; flex-direction: column; gap: var(--space-3); }
.gap-1 { gap: var(--space-1); }
.gap-2 { gap: var(--space-2); }
.gap-3 { gap: var(--space-3); }
.gap-4 { gap: var(--space-4); }
.divider-dashed { border-bottom: var(--border-dashed); }

/* === Page layout ===
   Note: sticky positioning on .sidebar depends on no ancestor having
   overflow: auto/hidden. If you add scroll containment above this, sticky
   will silently break. */
.page-wrap {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 14rem;
    gap: var(--space-5);
    max-width: 1280px;
    margin: 0 auto;
    padding: 0 var(--space-4);
    width: 100%;
    flex: 1;
    align-items: start;
}
main { padding: var(--space-5) var(--space-4); width: 100%; flex: 1; min-width: 0; }

/* === Header / footer ===
   Three-column grid (breadcrumbs left, search centre, actions right) so the
   centre stays reserved even when search is hidden in editor mode. Mobile
   (≤1100px) collapses to a two-row layout — see responsive block below. */
header {
    padding: var(--space-3) var(--space-4);
    border-bottom: var(--border-dashed);
    display: grid;
    grid-template-columns: 1fr minmax(0, auto) 1fr;
    grid-template-areas: "crumbs search actions";
    align-items: center;
    gap: var(--space-2);
}
footer {
    padding: var(--space-3) var(--space-4);
    border-top: var(--border-dashed);
    display: flex;
    align-items: center;
    gap: var(--space-2);
    flex-wrap: wrap;
}
.breadcrumb { grid-area: crumbs; gap: var(--space-1); min-width: 0; }
.header-actions { grid-area: actions; justify-content: flex-end; flex-wrap: wrap; }
.logo { width: 1.1em; height: 1.1em; vertical-align: center; }
.nav-up {
    display: inline-flex;
    align-items: center;
    color: var(--secondary);
    padding: 0 var(--space-1);
}
.nav-up:hover { color: var(--primary-hover); }

/* === Panel ===
   Bordered container recipe shared by listings, sidebar widgets, the tree
   picker, the floating TOC. .panel-floating raises the background for menus
   and modals that sit over content. */
.panel { border: var(--border); background: var(--bg); }
.panel-floating { background: var(--bg-panel); }
.panel-header {
    font-size: var(--font-xs);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    border-bottom: var(--border-dashed);
    padding-bottom: var(--space-1);
    margin-bottom: 0.4rem;
}
.panel-body {
    padding: var(--space-4);
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    word-break: break-word;
}
.panel-footer {
    padding: 0.6rem var(--space-4);
    border-top: var(--border-dashed);
    display: flex;
    justify-content: space-between;
    gap: var(--space-2);
}

/* === Buttons === */
.btn {
    background: none;
    border: none;
    color: var(--text);
    font: inherit;
    cursor: pointer;
    padding: 0;
    text-decoration: none;
    display: inline-block;
    white-space: nowrap;
}
.btn::before { content: "["; color: var(--secondary); }
.btn::after { content: "]"; color: var(--secondary); }
.btn:hover { color: var(--primary-hover); }
.btn-small { font-size: 0.65rem; font-weight: normal; vertical-align: middle; }
.btn-tool { font-size: var(--font-sm); padding: 0 0.15rem; }
.btn-block {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    width: 100%;
    border: none;
    padding: 0.3rem var(--space-3);
    white-space: nowrap;
}
.btn-fab {
    background: var(--bg-panel);
    border: var(--border);
    width: 3rem;
    height: 3rem;
    font-size: 1.5rem;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.btn-fab::before, .btn-fab::after { content: none; }
.btn-fab:hover { background: var(--bg-panel-hover); color: var(--primary-hover); }
.danger { color: var(--danger); }
.danger:hover { color: var(--danger-hover); }

/* === Form controls ===
   .input baseline is shared by search-input, modal inputs, and the editor
   textarea. Hosts layer their own background / font-family / sizing on top. */
.input {
    width: 100%;
    background: var(--bg-panel);
    border: var(--border);
    color: var(--text);
    font: inherit;
    padding: 0.3rem var(--space-2);
    outline: none;
}
.input:focus { border-color: var(--primary-hover); }

/* === Typography utilities === */
.muted { color: var(--text-muted); font-size: var(--font-sm); }
.small { font-size: var(--font-sm); }
.caption {
    font-size: var(--font-xs);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* === Markdown content === */
.content { margin-bottom: 2rem; }
.content h1, .content h2, .content h3,
.content h4, .content h5, .content h6,
main > h2 {
    color: var(--text);
    margin: 1.25rem 0 var(--space-2);
    line-height: 1.3;
}
.content h1 {
    font-size: 1.75rem;
    border-bottom: var(--border-dashed);
    padding-bottom: var(--space-1);
}
.content h2, main > h2 { font-size: 1.4rem; }
.content h3 { font-size: 1.15rem; }
.content p { margin: var(--space-3) 0; }
.content ul, .content ol { margin: var(--space-3) 0 var(--space-3) var(--space-5); }
.content li { margin: var(--space-1) 0; }
.content blockquote {
    border-left: 3px solid var(--secondary);
    padding: var(--space-1) var(--space-4);
    color: var(--text-muted);
    margin: var(--space-3) 0;
}
.content code {
    font-family: "Iosevka Etoile", monospace;
    font-size: 0.875em;
    background: var(--bg-panel);
    padding: 0.1em 0.35em;
}
.content pre {
    background: var(--bg-panel);
    border: var(--border);
    padding: var(--space-4);
    overflow-x: auto;
    margin: var(--space-3) 0;
}
.content pre code { background: none; padding: 0; }
.content hr { margin: var(--space-5) 0; }
.content img { max-width: 100%; }
.content li:has(> input.task-checkbox:checked) {
    color: var(--text-muted);
    text-decoration: line-through;
}

.dropdown.heading-anchor { margin-right: 0.4em; font-weight: normal; }
.heading-anchor .dropdown-toggle {
    background: none;
    border: none;
    padding: 0;
    cursor: pointer;
    color: var(--text-muted);
    font: inherit;
    font-weight: normal;
}
.heading-anchor .dropdown-toggle:hover { color: var(--primary-hover); }

/* === Data tables ===
   Shared style for the file listing, search-suggestion dropdown, and
   markdown content tables. .data-table-grid adds per-cell borders + a
   header band for content (markdown) tables. */
.data-table { width: 100%; border-collapse: collapse; }
.data-table th,
.data-table td { padding: 0.4rem var(--space-3); text-align: left; }
.data-table:not(.data-table-grid) tbody tr + tr { border-top: var(--border); }
.data-table:not(.data-table-grid) tbody tr:hover,
.data-table tr.is-active { background: var(--bg-panel-hover); }
.data-table tbody tr.is-empty,
.data-table tbody tr.is-empty:hover {
    color: var(--text-muted);
    background: none;
    cursor: default;
}
.data-table-grid th,
.data-table-grid td { border: var(--border); }
.data-table-grid th { background: var(--bg-panel); color: var(--text); }
.content .data-table-grid { margin: var(--space-3) 0; font-size: 0.9rem; }

/* File listing rows */
.list-item { font-size: 0.95rem; }
.list-item > td { padding: 0.6rem var(--space-4); }
.list-item td.icon { width: 1.25rem; text-align: center; }
.list-item td.name { overflow-wrap: anywhere; }
.list-item td.name a { color: inherit; display: block; }
.list-item td.meta {
    color: var(--text-muted);
    font-size: 0.8rem;
    white-space: nowrap;
    text-align: right;
}

/* === Dropdown menu === */
.dropdown { position: relative; }
.dropdown-menu {
    position: absolute;
    top: 100%;
    left: 0;
    z-index: 100;
    min-width: 9rem;
    display: none;
    background: var(--bg-panel);
    border: var(--border);
}
.dropdown-menu.align-right { left: auto; right: 0; }
.dropdown-menu.open-up { top: auto; bottom: 100%; margin-bottom: 0.4rem; }
.dropdown-menu.is-open { display: block; }
.dropdown-menu.scrollable { max-height: 23rem; overflow-y: auto; }

/* === Suggestion dropdown (header search + editor link picker) ===
   Anchored to a position:relative host. Mirrors .dropdown-menu visuals with
   a dashed border and bg-panel-hover for the active row. */
.suggest-host { position: relative; }
.suggest-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    z-index: 200;
    background: var(--bg-panel);
    border: var(--border-dashed);
    border-top: none;
    display: none;
}
.suggest-dropdown.is-open { display: block; }
.suggest-row { cursor: pointer; }
.suggest-row > td { padding: 0.4rem 0.6rem; }
.suggest-name, .suggest-path { display: block; }
.suggest-name { color: var(--text); }
.suggest-path { color: var(--text-muted); font-size: 0.8rem; margin-top: 0.1rem; }
.suggest-footer > td { color: var(--link); font-size: var(--font-sm); }

/* === Editor toolbar === */
.editor-toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-1);
    border: var(--border);
    border-bottom: none;
    padding: 0.4rem 0.6rem;
    background: var(--bg-panel-hover);
}
.toolbar-sep {
    width: 1px;
    background: var(--secondary);
    margin: 0 0.2rem;
    align-self: stretch;
}

/* === Edit form === */
.edit-form { display: flex; flex-direction: column; }
body.editor-wide .page-wrap { max-width: none; }
.editor-textarea {
    min-height: 60vh;
    background: var(--bg);
    border-top: none;
    font-family: "Iosevka Slab", monospace;
    font-size: 0.9rem;
    line-height: 1.6;
    padding: var(--space-4);
    resize: vertical;
}

/* === Search === */
.search-form {
    grid-area: search;
    display: flex;
    gap: var(--space-1);
    position: relative;
    justify-self: center;
    width: 24rem;
    max-width: 100%;
}
.search-input { font-size: 0.9rem; }
.search-card {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    padding-bottom: var(--space-4);
    margin-bottom: var(--space-4);
    border-bottom: var(--border-dashed);
    word-break: break-word;
}
.search-card:last-child { border-bottom: none; }
.search-card a { color: var(--link); font-size: 1.1rem; }
.search-card a:hover { color: var(--link-hover); }

/* === Floating action button ===
   Standalone FAB buttons (page TOC) are mobile-only. Wrapped FABs (search
   actions dropdown) stay visible on desktop. */
.fab { position: fixed; bottom: var(--space-4); right: var(--space-4); z-index: 50; }
button.fab { display: none; }

/* === Companion status === */
.companion-status { margin-left: auto; }
.companion-icon { font-size: 0.9rem; line-height: 1; padding: 0.1rem 0.3rem; }
.companion-on { color: var(--link); }
.companion-off { color: var(--text-muted); }
.companion-flyout { min-width: 14rem; padding: 0.4rem; }
.companion-line { padding: var(--space-1) var(--space-2); font-size: var(--font-sm); }

/* === Photo grid === */
.photo-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    gap: 0.4rem;
    margin-top: var(--space-3);
}
.photo-grid a { display: block; line-height: 0; }
.photo-grid img {
    width: 100%;
    height: 140px;
    object-fit: cover;
    display: block;
    background: var(--bg-panel) url("/_/icons/thumb-placeholder.svg") center/2rem no-repeat;
}

.empty { padding: var(--space-4); text-align: center; }

/* === Scrollbars === */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: #111; }
::-webkit-scrollbar-thumb { background: var(--primary); }
::-webkit-scrollbar-thumb:hover { background: var(--primary-hover); }

/* === Sidebar === */
.sidebar {
    position: sticky;
    top: var(--space-4);
    align-self: start;
    max-height: calc(100vh - 2rem);
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
    margin-top: var(--space-5);
}
aside.sidebar:empty { display: none; }

/* Density modifier for panels in the sidebar (smaller font, tighter padding).
   The .panel class applied alongside provides the border + background. */
.panel-sidebar { padding: var(--space-2) var(--space-3); font-size: var(--font-sm); }
.actions { display: flex; flex-direction: column; gap: 0.15rem; }

/* === Table of contents (floating variant) ===
   Default rendering is a fixed floating panel; inside .sidebar it becomes
   static via the .panel-sidebar density class. */
.toc {
    position: fixed;
    top: var(--space-4);
    right: var(--space-4);
    width: 14rem;
    max-height: calc(100vh - 6rem);
    overflow-y: auto;
    padding: var(--space-2) var(--space-3);
    font-size: var(--font-sm);
}
.sidebar .toc { position: static; width: auto; max-height: none; }
.toc ul { list-style: none; margin: 0; padding: 0; }
.toc li { margin: 0.15rem 0; }
.toc a {
    color: var(--link);
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.toc a:hover { color: var(--link-hover); }
.toc-h3 { padding-left: 0.8rem; }
.toc-h4 { padding-left: 1.6rem; }

/* === Modal === */
.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: flex-start;
    justify-content: center;
    z-index: 1000;
    padding: var(--space-4);
}
.modal {
    width: 100%;
    max-width: 500px;
    display: flex;
    flex-direction: column;
    margin-top: 6rem;
    position: relative;
}
.modal.is-dragged { margin: 0; }
/* Modal headers use larger font + bright color + drag handle; everything
   else comes from .panel-header. */
.modal .panel-header {
    font-size: var(--font-sm);
    color: var(--text);
    padding: 0.6rem var(--space-4);
    margin-bottom: 0;
    cursor: move;
    user-select: none;
}
/* Modal-scoped input variant: dark bg + slab font + slightly more padding. */
.modal .input {
    background: var(--bg);
    font-family: "Iosevka Slab", monospace;
    padding: 0.4rem 0.6rem;
}

/* === Tree picker === */
.tree-picker { max-height: 60vh; overflow-y: auto; }
.tree-row {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: 0.4rem var(--space-2);
    cursor: pointer;
    min-height: 2rem;
}
.tree-row:hover, .tree-row.is-selected { background: var(--bg-panel-hover); }
.tree-row.is-selected {
    border-left: 3px solid var(--primary);
    padding-left: calc(var(--space-2) - 3px);
}
.tree-row.is-disabled { color: var(--text-muted); cursor: default; }
.tree-row.is-disabled:hover { background: none; }
.tree-chevron, .tree-marker { text-align: center; flex-shrink: 0; }
.tree-chevron { width: 1.25rem; color: var(--secondary); }
.tree-chevron.is-leaf { visibility: hidden; }
.tree-marker { width: var(--space-4); color: var(--text-muted); }
.tree-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-children { padding-left: var(--space-5); }
.tree-selected-path {
    font-size: var(--font-sm);
    padding: var(--space-1) 0;
    word-break: break-all;
}

/* === Movie info box === */
.movie-info { margin: var(--space-3) 0; }
.movie-info::after { content: ""; display: block; clear: both; }
.movie-info .movie-poster {
    float: right;
    max-width: 200px;
    margin: 0 0 var(--space-3) var(--space-4);
}
.movie-info table { width: auto; }
@media (max-width: 600px) {
    .movie-info .movie-poster {
        float: none;
        display: block;
        margin: 0 auto var(--space-3);
    }
}

/* === Diary calendar === */
.diary-cal-nav {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.2rem;
    margin-bottom: 0.4rem;
    font-size: var(--font-sm);
}
.diary-cal-nav .diary-cal-drop + .diary-cal-heading { margin-left: var(--space-3); }
/* Anchor the month/year dropdowns to the nav row instead of the ▾ button so
   the menu spans the full panel width rather than overflowing the 14rem
   sidebar with its default min-width: 9rem. */
.diary-cal-nav { position: relative; }
.diary-cal-nav .diary-cal-drop { position: static; }
.diary-cal-nav .dropdown-menu { left: 0; right: 0; min-width: 0; }
.diary-cal-heading { color: var(--link); }
.diary-cal-heading:hover { color: var(--link-hover); }
.diary-cal-grid {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.8rem;
    margin-bottom: 0.4rem;
}
.diary-cal-grid th, .diary-cal-grid td {
    text-align: center;
    padding: 0.1rem 0.15rem;
    font-weight: normal;
    color: var(--text-muted);
}
.diary-cal-grid td a { color: var(--link); display: block; }
.diary-cal-grid td a:hover { color: var(--link-hover); }
.diary-cal-grid td.cal-empty a { color: var(--text-muted); }
.diary-cal-grid td.cal-empty a:hover { color: var(--link-hover); }
.diary-cal-grid td.cal-today { background: var(--bg-panel); }
.diary-cal-grid td.cal-current,
.diary-cal-grid td.cal-current a { color: var(--primary-hover); }
.btn-block.cal-current { color: var(--primary-hover); }

/* === Responsive === */
@media (max-width: 1100px) {
    .page-wrap { grid-template-columns: 1fr; }
    /* Single-row mobile header: logo + Up icon are compact so search can
       take the middle flex column, with actions on the right. */
    header { grid-template-columns: auto 1fr auto; }
    .search-form { width: 100%; max-width: none; justify-self: stretch; }
    /* Sidebar on mobile is a floating overlay toggled by the FAB. The aside
       itself is the scroll container; children render at natural height. */
    .sidebar {
        position: fixed;
        bottom: 5rem;
        right: var(--space-4);
        top: auto;
        left: auto;
        width: calc(100% - 2rem);
        max-width: 20rem;
        max-height: calc(100vh - 8rem);
        overflow-y: auto;
        display: none;
        z-index: 60;
    }
    .sidebar.is-open { display: flex; }
    button.fab { display: inline-flex; }
}

@media (max-width: 600px) {
    header, footer { padding: var(--space-2) var(--space-3); }
    main { padding: var(--space-4) var(--space-3); }
    .editor-textarea { min-height: 50vh; }
    .sidebar { width: calc(100% - 1.5rem); }
    .modal-backdrop { padding: var(--space-2); align-items: flex-start; }
    .modal { max-width: none; margin-top: var(--space-4); }
    .modal .panel-header { cursor: default; }
    /* On mobile, switch .list-item from a table row to a CSS grid so the
       meta cell wraps to its own line indented under the name. */
    .list-item {
        display: grid;
        grid-template-columns: 1.25rem 1fr;
        gap: 0 var(--space-3);
        padding: 0.6rem var(--space-4);
    }
    .list-item > td { padding: 0; }
    .list-item td.icon { grid-row: 1; grid-column: 1; }
    .list-item td.name { grid-row: 1; grid-column: 2; }
    .list-item td.meta { grid-row: 2; grid-column: 2; text-align: left; }
}
