:root {
    --background: #ffffff;
    --foreground: #0f172a;
    --card: #ffffff;
    --card-foreground: #0f172a;
    --popover: #ffffff;
    --popover-foreground: #0f172a;
    --primary: #2563eb;
    --primary-foreground: #eff6ff;
    --secondary: #e2e8f0;
    --secondary-foreground: #0f172a;
    --accent: #dbeafe;
    --accent-foreground: #0f172a;
    --muted: #e2e8f0;
    --muted-foreground: #475569;
    --border: #cbd5e1;
    --input: #cbd5e1;
    --ring: #60a5fa;
    --destructive: #dc2626;
    --destructive-foreground: #fff1f2;
    --scrollbar-thumb: color-mix(in oklab, var(--primary) 58%, var(--border));
    --scrollbar-track: color-mix(in oklab, var(--secondary) 78%, white);
    --scrollbar-thumb-hover: color-mix(in oklab, var(--primary) 76%, var(--border));
    --app-bg:
        radial-gradient(
            circle at top left,
            rgba(34, 197, 94, 0.12),
            transparent 24%
        ),
        radial-gradient(
            circle at top right,
            rgba(59, 130, 246, 0.1),
            transparent 26%
        ),
        linear-gradient(180deg, #f8fafc 0%, #eef2ff 100%);
}

html[data-theme="dark"] {
    --background: #07111f;
    --foreground: #e5eefc;
    --card: #0f1a2b;
    --card-foreground: #e5eefc;
    --popover: #0f1a2b;
    --popover-foreground: #e5eefc;
    --primary: #60a5fa;
    --primary-foreground: #08111d;
    --secondary: #18263a;
    --secondary-foreground: #dbe7fb;
    --accent: #163253;
    --accent-foreground: #e5eefc;
    --muted: #142033;
    --muted-foreground: #9cb0cc;
    --border: #28405f;
    --input: #28405f;
    --ring: #7dd3fc;
    --destructive: #ef4444;
    --destructive-foreground: #fff1f2;
    --scrollbar-thumb: color-mix(in oklab, var(--primary) 72%, var(--border));
    --scrollbar-track: color-mix(in oklab, var(--card) 62%, #020617);
    --scrollbar-thumb-hover: color-mix(in oklab, var(--primary) 84%, var(--border));
    --app-bg:
        radial-gradient(
            circle at top left,
            rgba(34, 197, 94, 0.15),
            transparent 24%
        ),
        radial-gradient(
            circle at top right,
            rgba(96, 165, 250, 0.18),
            transparent 26%
        ),
        linear-gradient(180deg, #06101d 0%, #0a1728 100%);
    color-scheme: dark;
}

body {
    min-height: 100vh;
    background: var(--app-bg);
    color: var(--foreground);
}

* {
    scrollbar-width: thin;
    scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);
}

*::-webkit-scrollbar {
    width: 1rem;
    height: 1rem;
}

*::-webkit-scrollbar-track {
    background: var(--scrollbar-track);
}

*::-webkit-scrollbar-thumb {
    border: 1px solid color-mix(in oklab, var(--scrollbar-track) 82%, transparent);
    border-radius: 999px;
    background: var(--scrollbar-thumb);
}

*::-webkit-scrollbar-thumb:hover {
    background: var(--scrollbar-thumb-hover);
}

.app-shell {
    max-width: 1280px;
    margin: 0 auto;
    padding: 1.25rem 0.85rem 2.5rem;
}

.app-stack {
    display: grid;
    gap: var(--app-stack-gap, 1rem);
}

.app-stack .app-stack {
    --app-stack-gap: 0.65rem;
}

.app-stack > *,
.app-grid > *,
.field-grid > * {
    min-width: 0;
}

.app-grid {
    display: grid;
    gap: 1rem;
}

.app-grid.cols-2 {
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
}

.workspace-grid {
    display: grid;
    gap: 1rem;
    grid-template-columns: minmax(320px, 420px) minmax(0, 1fr);
    align-items: start;
}

.workspace-sidebar {
    min-width: 0;
}

.workspace-upload-panel section,
.workspace-upload-panel form {
    display: grid;
    gap: 0.9rem;
}

.workspace-upload-panel input[type="file"].input {
    display: flex;
    align-items: center;
    min-height: 2.65rem;
    line-height: 1.2;
}

.workspace-upload-panel input[type="file"].input::file-selector-button {
    height: 100%;
    margin-right: 0.7rem;
}

.workspace-picker-item {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 0.4rem;
    align-items: stretch;
}

.workspace-picker-download {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2.2rem;
    min-width: 2.2rem;
    padding: 0;
}

.workspace-picker-download svg {
    width: 1rem;
    height: 1rem;
}

.workspace-detail-stack {
    min-width: 0;
}

.database-detail-header {
    align-items: center;
}

.panel-toggle-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    cursor: pointer;
}

.panel-toggle-btn {
    flex: 0 0 auto;
}

.database-delete-chip {
    background: color-mix(in oklab, var(--destructive) 10%, white);
    border-color: color-mix(in oklab, var(--destructive) 38%, var(--border));
    color: var(--foreground);
    font: inherit;
    line-height: 1;
    cursor: pointer;
}

html[data-theme="dark"] .database-delete-chip {
    background: color-mix(in oklab, var(--destructive) 28%, #1f0a10);
    border-color: color-mix(in oklab, var(--destructive) 52%, var(--border));
}

.database-file-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    min-width: 0;
}

.database-file-row > .muted {
    min-width: 0;
    overflow-wrap: anywhere;
}

.database-file-rename-form {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex: 0 0 auto;
    min-width: 0;
}

.database-file-rename-form .input {
    width: 16rem;
    max-width: 100%;
}

.workspace-detail-stack .table td .row-actions {
    flex-wrap: nowrap;
    justify-content: flex-start;
}

.workspace-detail-stack .table td .row-actions .input,
.workspace-detail-stack .table td .row-actions select {
    flex: 0 0 auto;
}

.workspace-detail-stack .table td .row-actions label {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    white-space: nowrap;
}

.workspace-detail-stack .table td,
.workspace-detail-stack .table th {
    white-space: nowrap;
}

.audit-detail-copy {
    margin: 0;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    word-break: break-word;
    font: inherit;
}

.audit-detail-action {
    flex: 0 0 auto;
}

.audit-detail-stack {
    display: grid;
    gap: 0.6rem;
    max-width: 100%;
    min-width: 0;
}

.audit-log-table {
    width: 100%;
    table-layout: fixed;
}

.audit-log-table th,
.audit-log-table td {
    white-space: normal;
    vertical-align: top;
}

.audit-log-table th:nth-child(1),
.audit-log-table td:nth-child(1),
.audit-log-table th:nth-child(2),
.audit-log-table td:nth-child(2),
.audit-log-table th:nth-child(3),
.audit-log-table td:nth-child(3),
.audit-log-table th:nth-child(4),
.audit-log-table td:nth-child(4) {
    white-space: nowrap;
}

.audit-log-summary-row {
    cursor: pointer;
}

.audit-log-summary-row:hover td {
    background: color-mix(in oklab, var(--accent) 14%, var(--card));
}

.audit-log-expanded-row td {
    background: color-mix(in oklab, var(--card) 96%, white);
    max-width: 0;
}

.hero-card {
    overflow: hidden;
    position: relative;
}

.hero-card::after {
    content: "";
    position: absolute;
    inset: auto -8% -35% auto;
    width: 220px;
    height: 220px;
    border-radius: 9999px;
    background: radial-gradient(
        circle,
        rgba(34, 197, 94, 0.16),
        transparent 70%
    );
    pointer-events: none;
}

.notice {
    border-radius: var(--radius);
    border: 1px solid var(--border);
    padding: 0.7rem 0.85rem;
    font-size: 0.9rem;
}

.notice.info {
    background: color-mix(in oklab, var(--secondary) 10%, white);
}

.notice.error {
    background: color-mix(in oklab, var(--destructive) 10%, white);
}

html[data-theme="dark"] .notice.info {
    background: color-mix(in oklab, var(--secondary) 70%, black);
}

html[data-theme="dark"] .notice.error {
    background: color-mix(in oklab, var(--destructive) 28%, #1f0a10);
}

.muted {
    color: var(--muted-foreground);
}

.code-pill {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    max-width: 100%;
    padding: 0.35rem 0.6rem;
    border-radius: 9999px;
    background: var(--secondary);
    color: var(--secondary-foreground);
    font-size: 0.85rem;
    white-space: normal;
    overflow-wrap: anywhere;
    word-break: break-word;
}

.theme-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: flex-start;
    padding: 0.2rem;
    border: 1px solid var(--border);
    border-radius: 9999px;
    background: color-mix(in oklab, var(--card) 88%, white);
    transition:
        background-color 0.2s ease,
        border-color 0.2s ease;
}

.theme-toggle-track {
    position: relative;
    display: inline-flex;
    align-items: center;
    width: 3.6rem;
    height: 2rem;
    border-radius: 9999px;
    background: color-mix(in oklab, var(--secondary) 82%, white);
}

.theme-toggle-thumb {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.55rem;
    height: 1.55rem;
    border-radius: 9999px;
    background: var(--primary);
    color: var(--primary-foreground);
    font-size: 0.95rem;
    transform: translateX(0);
    transition:
        transform 0.2s ease,
        background-color 0.2s ease,
        color 0.2s ease;
    box-shadow: 0 6px 14px rgba(15, 23, 42, 0.2);
}

.theme-toggle-thumb svg {
    width: 0.95rem;
    height: 0.95rem;
}

.theme-toggle[aria-checked="true"] .theme-toggle-thumb {
    transform: translateX(1.6rem);
}

html[data-theme="dark"] .theme-toggle {
    background: color-mix(in oklab, var(--card) 84%, #08111d);
}

html[data-theme="dark"] .theme-toggle-track {
    background: color-mix(in oklab, var(--accent) 58%, #091321);
}

.nav-list,
.meta-list {
    display: grid;
    gap: 0.45rem;
}

.nav-link {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.55rem;
    padding: 0.6rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: calc(var(--radius) - 2px);
    text-decoration: none;
    background: color-mix(in oklab, var(--card) 90%, white);
}

.nav-button {
    width: 100%;
    font: inherit;
    color: inherit;
    text-align: left;
    cursor: pointer;
}

.nav-link > span {
    min-width: 0;
}

.nav-link > span:first-child {
    overflow-wrap: anywhere;
    word-break: break-word;
}

.substance-list-copy {
    display: grid;
    gap: 0.15rem;
}

.substance-list-meta {
    font-size: 0.82rem;
    line-height: 1.25;
}

.nav-link.active {
    border-color: var(--ring);
    background: color-mix(in oklab, var(--accent) 48%, white);
}

html[data-theme="dark"] .nav-link {
    background: color-mix(in oklab, var(--card) 88%, #091321);
}

html[data-theme="dark"] .nav-link.active {
    background: color-mix(in oklab, var(--accent) 62%, #091321);
}

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

.row-card {
    display: grid;
    gap: 0.75rem;
}

.field-grid {
    display: grid;
    gap: 0.75rem;
}

.field-grid.cols-2 {
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}

.row-actions,
.toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: center;
    justify-content: space-between;
}

.toolbar-start,
.toolbar-end {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: center;
    min-width: 0;
}

.row-actions > *,
.toolbar > *,
.toolbar-start > *,
.toolbar-end > * {
    min-width: 0;
}

.row-actions .input,
.row-actions select,
.toolbar .input,
.toolbar select {
    flex: 1 1 14rem;
}

#database-panel .card,
#database-panel form,
#database-panel .notice,
#database-panel .table-scroll,
#database-panel table {
    min-width: 0;
}

#database-panel td,
#database-panel th {
    overflow-wrap: anywhere;
    word-break: break-word;
}

.empty-state {
    padding: 1.25rem 0.85rem;
    text-align: center;
    color: var(--muted-foreground);
}

.login-card {
    max-width: 48rem;
    margin: 2rem auto 0;
}

.login-form {
    display: grid;
    width: 100%;
    row-gap: 0.35rem;
}

.login-row {
    display: grid;
    grid-template-columns: minmax(5rem, 6.25rem) minmax(0, 1fr);
    gap: 0.35rem 0.5rem;
    align-items: center;
}

.login-row-password {
    align-items: start;
}

.login-password-stack {
    display: grid;
    gap: 0.4rem;
}

.login-password-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    min-width: 0;
}

.login-password-row .login-input {
    flex: 1 1 auto;
    min-width: 0;
}

.login-password-toggle {
    flex: 0 0 auto;
}

.login-input {
    width: 100%;
    border: 1px solid var(--border);
    background: color-mix(in oklab, var(--card) 92%, white);
    box-shadow: inset 0 0 0 1px
        color-mix(in oklab, var(--border) 60%, transparent);
}

html[data-theme="dark"] .login-input {
    background: color-mix(in oklab, var(--card) 86%, #08111d);
    border-color: color-mix(in oklab, var(--border) 90%, #8fb4e8);
    box-shadow: inset 0 0 0 1px color-mix(in oklab, var(--border) 80%, #8fb4e8);
}

.sticky-panel {
    position: sticky;
    top: 1rem;
}

.substances-shell {
    display: grid;
    gap: 1rem;
    --substances-panel-height: calc(100vh - 8rem);
}

.substances-db-panel {
    padding: 0.75rem;
}

.substances-db-toolbar {
    display: flex;
    align-items: end;
    justify-content: space-between;
    gap: 0.75rem;
}

.substances-db-form .input {
    width: 100%;
}

.substances-category-search {
    position: relative;
}

.database-user-search {
    flex: 1 1 18rem;
    min-width: min(18rem, 100%);
}

.database-user-search .input {
    width: 100%;
}

.database-user-search .substances-category-results {
    max-height: 12rem;
}

.substances-category-results {
    position: absolute;
    top: calc(100% + 0.35rem);
    left: 0;
    right: 0;
    z-index: 20;
    display: grid;
    gap: 0.2rem;
    max-height: 14rem;
    overflow: auto;
    padding: 0.35rem;
    border: 1px solid var(--border);
    border-radius: calc(var(--radius) - 2px);
    background: color-mix(in oklab, var(--card) 96%, white);
    box-shadow: 0 10px 24px rgba(15, 23, 42, 0.14);
}

.substances-category-option {
    width: 100%;
    padding: 0.45rem 0.55rem;
    border: 0;
    border-radius: calc(var(--radius) - 6px);
    background: transparent;
    color: inherit;
    font: inherit;
    text-align: left;
    cursor: pointer;
}

.substances-category-option:hover,
.substances-category-option.active {
    background: color-mix(in oklab, var(--accent) 54%, white);
}

.substances-category-chip-row {
    display: flex;
    align-items: center;
    margin-top: 0.45rem;
}

.substances-category-chip {
    display: inline-flex;
    align-items: center;
    gap: 0.45rem;
    max-width: 100%;
    padding: 0.35rem 0.7rem;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: color-mix(in oklab, var(--accent) 18%, var(--card));
    color: inherit;
    font: inherit;
    cursor: pointer;
}

.substances-category-chip:hover {
    background: color-mix(in oklab, var(--accent) 28%, var(--card));
}

.substances-category-chip-dismiss {
    display: inline-grid;
    place-items: center;
    width: 1.1rem;
    height: 1.1rem;
    border-radius: 999px;
    background: color-mix(in oklab, var(--accent) 34%, white);
    line-height: 1;
}

.substances-panels {
    display: grid;
    gap: 1rem;
    grid-template-columns: minmax(260px, 340px) minmax(0, 1fr);
    align-items: stretch;
}

.db-viewer-shell {
    gap: 1rem;
}

.db-viewer-panels {
    display: grid;
    gap: 1rem;
    grid-template-columns: minmax(320px, 420px) minmax(0, 1fr);
    align-items: stretch;
}

.substance-list {
    display: grid;
    gap: 0.45rem;
    flex: 1 1 auto;
    min-height: 0;
    overflow: auto;
    padding-right: 0.3rem;
    padding-bottom: 0.3rem;
}

#substances-list-panel {
    display: flex;
    flex-direction: column;
    align-self: stretch;
    min-height: 0;
    height: var(--substances-panel-height);
    overflow: hidden;
    position: sticky;
    top: 1rem;
    max-height: calc(100vh - 2rem);
}

#substances-list-panel > form {
    flex: 0 0 auto;
}

#substances-editor-panel {
    display: flex;
    flex-direction: column;
    align-self: stretch;
    min-height: var(--substances-panel-height);
}

.db-viewer-list-panel {
    display: flex;
    flex-direction: column;
    align-self: stretch;
    min-height: 0;
    height: var(--substances-panel-height);
    overflow: hidden;
    position: sticky;
    top: 1rem;
    max-height: calc(100vh - 2rem);
}

.db-viewer-list-panel > section {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
}

.db-viewer-list-panel .nav-link {
    white-space: nowrap;
}

.db-viewer-list-panel .substance-list {
    flex: 1 1 auto;
    min-height: 0;
    overflow: auto;
    padding-right: 0.3rem;
    padding-bottom: 0.3rem;
}

.table-scroll,
.substances-category-results,
.audit-detail-copy {
    padding-right: 0.3rem;
    padding-bottom: 0.3rem;
}

.db-viewer-rows-panel {
    display: flex;
    flex-direction: column;
    align-self: stretch;
    min-height: var(--substances-panel-height);
}

.db-viewer-rows-panel > section {
    flex: 1 1 auto;
    min-height: 0;
}

.db-viewer-rows-toolbar {
    margin-bottom: 1rem;
}

.db-viewer-pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.75rem;
    margin-top: 1rem;
    flex-wrap: wrap;
}

.is-disabled {
    pointer-events: none;
    opacity: 0.55;
}

.db-viewer-page-links {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
    flex-wrap: wrap;
}

.db-viewer-page-link,
.db-viewer-page-arrow {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 2rem;
    height: 2rem;
    padding: 0 0.6rem;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: color-mix(in oklab, var(--card) 90%, white);
    color: inherit;
    text-decoration: none;
}

.db-viewer-page-link.is-current {
    background: color-mix(in oklab, var(--accent) 50%, white);
    border-color: var(--ring);
    font-weight: 600;
}

.rich-editor-shell {
    display: grid;
    gap: 0.5rem;
    flex: 1 1 auto;
    min-height: 0;
}

.rich-toolbar {
    justify-content: flex-start;
}

.rich-editor {
    min-height: max(20rem, calc(100vh - 18rem));
    padding: 0.8rem;
    border: 1px solid var(--border);
    border-radius: calc(var(--radius) - 2px);
    background: #ffffff;
    color: #111827;
    line-height: 1.6;
    overflow: auto;
}

html[data-theme="dark"] .rich-editor {
    background: #ffffff;
    color: #111827;
}

.card {
    padding: 1rem;
}

.btn,
.btn-outline,
.input,
select,
textarea {
    padding: 0.55rem 0.7rem;
}

select,
textarea,
input.input {
    background: color-mix(in oklab, var(--card) 92%, white);
    color: var(--foreground);
}

html[data-theme="dark"] select,
html[data-theme="dark"] textarea,
html[data-theme="dark"] input.input {
    background: color-mix(in oklab, var(--card) 86%, #08111d);
    color: var(--foreground);
}

html[data-theme="dark"] .substances-category-results {
    background: color-mix(in oklab, var(--card) 92%, #08111d);
    box-shadow: 0 14px 28px rgba(0, 0, 0, 0.34);
}

html[data-theme="dark"] .substances-category-option:hover,
html[data-theme="dark"] .substances-category-option.active {
    background: color-mix(in oklab, var(--accent) 72%, #091321);
}

.badge,
.badge-outline,
.badge-secondary {
    padding: 0.22rem 0.45rem;
}

.card,
.input,
.table,
.btn,
.btn-outline,
.badge,
.badge-outline,
.badge-secondary {
    transition:
        background-color 0.2s ease,
        border-color 0.2s ease,
        color 0.2s ease;
}

html:not([data-theme-ready="true"]) .card,
html:not([data-theme-ready="true"]) .input,
html:not([data-theme-ready="true"]) .table,
html:not([data-theme-ready="true"]) .btn,
html:not([data-theme-ready="true"]) .btn-outline,
html:not([data-theme-ready="true"]) .badge,
html:not([data-theme-ready="true"]) .badge-outline,
html:not([data-theme-ready="true"]) .badge-secondary {
    transition: none;
}

.input-error {
    border-color: var(--destructive) !important;
    box-shadow: 0 0 0 2px color-mix(in oklab, var(--destructive) 22%, transparent);
}

.rich-editor:focus {
    outline: 2px solid color-mix(in oklab, var(--ring) 55%, transparent);
    outline-offset: 2px;
}

.rich-editor p,
.rich-editor ul,
.rich-editor ol {
    margin: 0 0 1rem;
}

.rich-editor ul,
.rich-editor ol {
    padding-left: 1.5rem;
}

textarea {
    min-height: 7rem;
}

@media (max-width: 768px) {
    .app-shell {
        padding-inline: 0.7rem;
        padding-top: 0.8rem;
    }

    .sticky-panel {
        position: static;
    }

    .substances-panels {
        grid-template-columns: 1fr;
    }

    .db-viewer-panels {
        grid-template-columns: 1fr;
    }

    .workspace-grid {
        grid-template-columns: 1fr;
    }

    .database-file-row {
        align-items: stretch;
        flex-direction: column;
    }

    .database-file-rename-form {
        width: 100%;
    }

    .database-file-rename-form .input {
        width: 100%;
    }

    #substances-list-panel {
        position: static;
        max-height: none;
    }

    .db-viewer-list-panel {
        position: static;
        max-height: none;
        height: auto;
    }

    .substances-db-toolbar {
        align-items: stretch;
        flex-direction: column;
    }

    .substances-db-form {
        width: 100%;
    }

    .login-row {
        grid-template-columns: 1fr;
    }
}
