:root {
    color-scheme: dark;
    --bg: #0f1418;
    --panel: #151d23;
    --panel-2: #1b252d;
    --line: #2d3b45;
    --text: #eef4f6;
    --muted: #91a2ad;
    --teal: #36d1b7;
    --amber: #f3b44d;
    --red: #ff6b6b;
    --green: #7bd88f;
    --shadow: 0 18px 42px rgba(0, 0, 0, .3);
}

* {
    box-sizing: border-box;
}

html {
    min-width: 320px;
}

body {
    margin: 0;
    min-height: 100vh;
    background: var(--bg);
    color: var(--text);
    font: 16px/1.5 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

body.drawer-open {
    overflow: hidden;
}

a {
    color: var(--teal);
    text-decoration: none;
}

a:hover {
    color: #7ce8d8;
}

input,
select,
textarea,
button {
    font: inherit;
}

input,
select,
textarea {
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #0d1216;
    color: var(--text);
    padding: 13px 14px;
    outline: none;
}

textarea {
    resize: vertical;
}

input:focus,
select:focus,
textarea:focus {
    border-color: var(--teal);
    box-shadow: 0 0 0 3px rgba(54, 209, 183, .12);
}

label span,
.editor-pane > span,
.preview-pane > span {
    display: block;
    margin-bottom: 8px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 800;
    text-transform: uppercase;
}

table {
    width: 100%;
    border-collapse: collapse;
}

th,
td {
    padding: 14px;
    border-bottom: 1px solid var(--line);
    text-align: left;
    vertical-align: middle;
}

th {
    color: var(--muted);
    font-size: 12px;
    text-transform: uppercase;
}

td small {
    display: block;
    color: var(--muted);
}

.app-shell {
    min-height: 100vh;
}

.topbar {
    min-height: 60px;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 14px;
    border-bottom: 1px solid var(--line);
    background: rgba(15, 20, 24, .98);
    position: sticky;
    top: 0;
    z-index: 30;
}

.brand {
    color: var(--text);
    font-size: 20px;
    font-weight: 900;
}

.mobile-new-button {
    margin-left: auto;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    background: var(--teal);
    color: #06211c;
    font-weight: 900;
    padding: 0 15px;
}

.topbar-actions {
    display: none;
}

.user-chip {
    color: var(--muted);
    font-size: 13px;
}

.menu-button {
    position: relative;
    width: 46px;
    min-height: 44px;
    padding: 0;
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    background: var(--panel-2);
    color: var(--text);
    border: 1px solid var(--line);
    border-radius: 8px;
    cursor: pointer;
}

.menu-button span {
    width: 20px;
    height: 2px;
    border-radius: 999px;
    background: currentColor;
}

.menu-button strong {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
}

.drawer-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .62);
    opacity: 0;
    pointer-events: none;
    transition: opacity .22s ease;
    z-index: 35;
}

.drawer-overlay.is-open {
    opacity: 1;
    pointer-events: auto;
}

.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(86vw, 320px);
    transform: translateX(-102%);
    transition: transform .24s ease;
    border-right: 1px solid var(--line);
    background: #11181d;
    padding: 16px;
    z-index: 40;
    box-shadow: var(--shadow);
}

.sidebar.is-open {
    transform: translateX(0);
}

.drawer-head {
    min-height: 46px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 16px;
}

.drawer-head strong {
    font-size: 20px;
}

.drawer-close {
    min-height: 44px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel-2);
    color: var(--text);
    padding: 0 14px;
}

.nav {
    display: grid;
    gap: 8px;
}

.nav a,
.drawer-logout button {
    min-height: 48px;
    width: 100%;
    display: flex;
    align-items: center;
    border-radius: 8px;
    color: var(--muted);
    background: transparent;
    border: 0;
    padding: 0 13px;
    font-weight: 800;
    text-align: left;
}

.drawer-logout {
    margin: 8px 0 0;
}

.drawer-logout button {
    cursor: pointer;
    color: var(--red);
}

.nav a:hover,
.nav a.is-active,
.drawer-logout button:hover {
    background: var(--panel-2);
    color: var(--text);
}

.main {
    width: 100%;
    padding: 18px 14px 28px;
    overflow-x: hidden;
}

.page-heading {
    display: grid;
    gap: 14px;
    margin-bottom: 20px;
}

.page-heading h1,
.login-panel h1,
.empty-state h1 {
    margin: 0;
    font-size: 28px;
    line-height: 1.15;
}

.eyebrow {
    margin: 0 0 6px;
    color: var(--teal);
    font-size: 12px;
    font-weight: 900;
    text-transform: uppercase;
}

.muted,
.empty {
    color: var(--muted);
}

.button,
button {
    border: 0;
    border-radius: 8px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 44px;
    padding: 0 15px;
    font-weight: 900;
    white-space: nowrap;
}

.button-primary {
    background: var(--teal);
    color: #06211c;
}

.button-secondary {
    background: var(--panel-2);
    color: var(--text);
    border: 1px solid var(--line);
}

.button-ghost {
    background: transparent;
    color: var(--muted);
    border: 1px solid var(--line);
}

.button-ghost:hover,
.button-secondary:hover {
    color: var(--text);
    border-color: #526672;
}

.danger {
    color: var(--red);
}

.button-row,
.row-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.panel,
.metric-card,
.document-card {
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    box-shadow: var(--shadow);
}

.panel {
    padding: 16px;
}

.panel-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 12px;
}

.panel-heading h2 {
    margin: 0;
    font-size: 18px;
}

.stats-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 14px;
    margin-bottom: 16px;
}

.metric-card {
    padding: 18px;
}

.metric-card span {
    display: block;
    color: var(--muted);
    margin-bottom: 10px;
}

.metric-card strong {
    display: block;
    font-size: 26px;
    line-height: 1.1;
    overflow-wrap: anywhere;
}

.content-grid,
.reader-layout {
    display: grid;
    grid-template-columns: 1fr;
    gap: 16px;
}

.category-list {
    display: grid;
    gap: 10px;
}

.category-pill {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding: 13px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #10171c;
    color: var(--text);
}

.status {
    display: inline-flex;
    align-items: center;
    min-height: 26px;
    padding: 0 9px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 900;
    text-transform: capitalize;
}

.status-draft {
    color: var(--amber);
    background: rgba(243, 180, 77, .14);
}

.status-published {
    color: var(--green);
    background: rgba(123, 216, 143, .14);
}

.status-archived {
    color: var(--muted);
    background: rgba(145, 162, 173, .14);
}

.filter-bar,
.editor-toolbar,
.form-stack {
    display: grid;
    grid-template-columns: 1fr;
    gap: 14px;
}

.filter-bar,
.editor-toolbar {
    align-items: end;
    margin-bottom: 18px;
}

.table-wrap {
    overflow-x: auto;
}

.actions-col {
    width: 190px;
}

.documents-table-panel {
    display: none;
}

.document-cards {
    display: grid;
    gap: 12px;
}

.document-card {
    display: grid;
    gap: 14px;
    padding: 16px;
}

.document-card-title {
    display: block;
    color: var(--text);
    font-size: 18px;
    font-weight: 900;
    line-height: 1.25;
}

.document-card p {
    margin: 5px 0 0;
    color: var(--muted);
}

.document-card-meta,
.document-card-actions {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

.document-card-meta {
    color: var(--muted);
    font-size: 14px;
}

.document-card-actions .button {
    flex: 1;
}

.document-form {
    display: grid;
    gap: 18px;
}

.editor-tabs {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

.editor-tabs button {
    min-height: 46px;
    border: 1px solid var(--line);
    background: var(--panel-2);
    color: var(--muted);
}

.editor-tabs button.is-active {
    border-color: var(--teal);
    background: rgba(54, 209, 183, .14);
    color: var(--text);
}

.editor-grid {
    display: block;
}

.editor-pane,
.preview-pane {
    display: none;
}

.editor-pane.is-active,
.preview-pane.is-active {
    display: block;
}

.editor-pane textarea,
.preview-pane .markdown-body {
    width: 100%;
    height: 60vh;
    min-height: 60vh;
}

.editor-pane textarea {
    font-family: "Cascadia Code", Consolas, monospace;
    font-size: 16px;
    line-height: 1.6;
    padding: 16px;
}

.preview-pane {
    min-width: 0;
}

.markdown-body {
    overflow: auto;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #0d1216;
    padding: 18px;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3 {
    line-height: 1.2;
}

.markdown-body code {
    border-radius: 5px;
    background: #192229;
    color: #b5f4e9;
    padding: 2px 5px;
}

.markdown-body pre {
    overflow: auto;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: #080c0f;
    padding: 14px;
}

.markdown-body pre code {
    background: transparent;
    padding: 0;
}

.markdown-body table {
    margin: 18px 0;
}

.document-reader {
    min-height: 60vh;
}

.versions-panel {
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    padding: 16px;
    align-self: start;
}

.versions-panel h2 {
    margin: 0 0 14px;
    font-size: 18px;
}

.version-item {
    display: grid;
    gap: 3px;
    padding: 12px 0;
    border-bottom: 1px solid var(--line);
}

.version-item span,
.version-item small {
    color: var(--muted);
}

.narrow-form {
    max-width: 720px;
}

.settings-list {
    max-width: 720px;
    display: grid;
    gap: 14px;
}

.settings-list div {
    display: grid;
    gap: 5px;
    border-bottom: 1px solid var(--line);
    padding-bottom: 14px;
}

.auth-page {
    display: grid;
    place-items: center;
    padding: 20px;
}

.login-panel {
    width: min(430px, 100%);
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
    box-shadow: var(--shadow);
    padding: 24px;
}

.flash {
    border-radius: 8px;
    margin-bottom: 18px;
    padding: 12px 14px;
    border: 1px solid var(--line);
    background: var(--panel-2);
}

.flash-success {
    border-color: rgba(123, 216, 143, .45);
    color: var(--green);
}

.flash-error {
    border-color: rgba(255, 107, 107, .45);
    color: var(--red);
}

.empty-state {
    display: grid;
    justify-items: start;
    gap: 12px;
    padding: 52px 0;
}

.empty-state.compact {
    padding: 18px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel);
}

@media (min-width: 640px) {
    .main {
        padding: 24px;
    }

    .stats-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .filter-bar,
    .editor-toolbar {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .document-card-actions .button {
        flex: 0 0 auto;
    }
}

@media (min-width: 769px) {
    .app-shell {
        display: grid;
        grid-template-columns: 260px 1fr;
        grid-template-rows: 64px 1fr;
    }

    .topbar {
        grid-column: 1 / -1;
        min-height: 64px;
        padding: 0 22px;
    }

    .menu-button,
    .mobile-new-button,
    .drawer-head,
    .drawer-overlay,
    .drawer-logout {
        display: none;
    }

    .topbar-actions {
        margin-left: auto;
        display: flex;
        align-items: center;
        gap: 12px;
    }

    .sidebar {
        position: static;
        width: auto;
        transform: none;
        transition: none;
        box-shadow: none;
        padding: 18px;
        z-index: auto;
    }

    .main {
        padding: 28px;
    }

    .page-heading {
        display: flex;
        align-items: flex-start;
        justify-content: space-between;
        gap: 18px;
        margin-bottom: 24px;
    }

    .page-heading h1,
    .login-panel h1,
    .empty-state h1 {
        font-size: 32px;
    }

    .stats-grid {
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 16px;
        margin-bottom: 18px;
    }

    .content-grid {
        grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr);
        gap: 18px;
    }

    .reader-layout {
        grid-template-columns: minmax(0, 1fr) 290px;
        gap: 18px;
    }

    .filter-bar {
        grid-template-columns: minmax(220px, 1fr) 190px 160px auto;
    }

    .editor-toolbar {
        grid-template-columns: minmax(220px, 1fr) minmax(180px, .7fr) 180px 140px auto;
    }

    .documents-table-panel {
        display: block;
    }

    .document-cards {
        display: none;
    }

    .editor-tabs {
        display: none;
    }

    .editor-grid {
        display: grid;
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
        gap: 18px;
        min-height: 620px;
    }

    .editor-pane,
    .preview-pane,
    .editor-pane.is-active,
    .preview-pane.is-active {
        display: block;
    }

    .editor-pane textarea,
    .preview-pane .markdown-body {
        height: 620px;
        min-height: 620px;
    }

    .settings-list div {
        display: flex;
        justify-content: space-between;
        gap: 18px;
    }
}

@media (min-width: 1180px) {
    .stats-grid {
        grid-template-columns: repeat(4, minmax(0, 1fr));
    }
}
