:root {
    /* Eggshell / cream */
    --bg: #f3eee4;
    --bg-deep: #ebe4d6;
    --bg-elevated: #faf7f0;
    --bg-soft: #f7f2e8;
    --bg-paper: #fffdf8;
    --border: rgba(92, 74, 48, 0.14);
    --border-strong: rgba(92, 74, 48, 0.22);
    --text: #2c2416;
    --text-soft: #3d3426;
    --muted: #7a6d58;
    --primary: #c45c26;
    --primary-strong: #a84818;
    --primary-soft: rgba(196, 92, 38, 0.12);
    --accent: #3f6f4e;
    --accent-soft: rgba(63, 111, 78, 0.12);
    --match: #fff3c4;
    --match-border: rgba(196, 140, 38, 0.45);
    --context: #f0ebe1;
    --danger: #b42318;
    --radius: 16px;
    --shadow: 0 10px 30px rgba(70, 52, 28, 0.08);
    --shadow-soft: 0 2px 8px rgba(70, 52, 28, 0.05);
    --font: "Segoe UI", system-ui, -apple-system, sans-serif;
    --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
    --library-width: 300px;
    --header-space: 5.5rem;
}

*,
*::before,
*::after {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    min-height: 100%;
    height: 100%;
    color: var(--text);
    font-family: var(--font);
    line-height: 1.5;
    background: var(--bg);
}

body {
    min-height: 100vh;
    position: relative;
}

/* Paper-grain noise overlay */
.noise {
    pointer-events: none;
    position: fixed;
    inset: 0;
    z-index: 0;
    opacity: 0.45;
    mix-blend-mode: multiply;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='4' stitchTiles='stitch'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
    background-repeat: repeat;
    background-size: 180px 180px;
}

.page {
    position: relative;
    z-index: 1;
    width: min(1400px, calc(100% - 1.5rem));
    margin: 0 auto;
    padding: 1rem 0 1.25rem;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    gap: 0.85rem;
}

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

a:hover {
    text-decoration: underline;
}

.hero {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    flex-shrink: 0;
}

.brand {
    display: flex;
    gap: 0.75rem;
    align-items: center;
}

.logo {
    font-size: 2.1rem;
    line-height: 1;
}

.hero h1 {
    margin: 0;
    font-size: clamp(1.55rem, 2.5vw, 1.95rem);
    letter-spacing: -0.03em;
    color: var(--text);
}

.tagline {
    margin: 0.1rem 0 0;
    color: var(--primary);
    font-weight: 650;
    font-size: 0.98rem;
}

.privacy-pill {
    margin: 0;
    padding: 0.45rem 0.8rem;
    border: 1px solid var(--border);
    border-radius: 999px;
    background: rgba(255, 253, 248, 0.72);
    color: var(--muted);
    font-size: 0.86rem;
    backdrop-filter: blur(4px);
}

/* Full-height two-column shell */
.layout {
    flex: 1;
    min-height: 0;
    display: grid;
    grid-template-columns: var(--library-width) minmax(0, 1fr);
    gap: 1rem;
    /* Fill remaining viewport under header/footer */
    height: calc(100vh - 8.5rem);
    max-height: calc(100vh - 8.5rem);
}

@media (max-width: 860px) {
    .layout {
        grid-template-columns: 1fr;
        height: auto;
        max-height: none;
    }

    .library-panel {
        min-height: 360px;
        max-height: 50vh;
    }

    .workspace-panel {
        min-height: 70vh;
    }
}

.panel {
    background: rgba(250, 247, 240, 0.88);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 1rem 1.05rem;
    display: flex;
    flex-direction: column;
    gap: 0.85rem;
    min-height: 0;
    backdrop-filter: blur(2px);
}

.library-panel {
    height: 100%;
    overflow: hidden;
}

.workspace-panel {
    height: 100%;
    overflow: hidden;
    padding-bottom: 0.85rem;
}

.panel-head h2 {
    margin: 0;
    font-size: 1.08rem;
    color: var(--text);
}

.panel-head p {
    margin: 0.25rem 0 0;
}

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

.small {
    font-size: 0.84rem;
}

.hidden {
    display: none !important;
}

/* Library */
.dropzone {
    display: block;
    border: 1.5px dashed rgba(196, 92, 38, 0.4);
    border-radius: 14px;
    background: rgba(255, 253, 248, 0.65);
    cursor: pointer;
    transition:
        border-color 0.15s ease,
        background 0.15s ease,
        transform 0.15s ease;
    flex-shrink: 0;
}

.dropzone:hover,
.dropzone.dragover {
    border-color: var(--primary);
    background: var(--primary-soft);
    transform: translateY(-1px);
}

.dropzone-inner {
    display: grid;
    gap: 0.15rem;
    justify-items: center;
    padding: 1.1rem 0.75rem;
    text-align: center;
}

.dropzone-inner strong {
    font-size: 0.98rem;
}

.status {
    min-height: 1.25rem;
    font-size: 0.88rem;
    color: var(--muted);
    flex-shrink: 0;
}

.status.error {
    color: var(--danger);
}

.status.ok {
    color: var(--accent);
}

.progress {
    height: 7px;
    border-radius: 999px;
    background: rgba(92, 74, 48, 0.1);
    overflow: hidden;
    flex-shrink: 0;
}

.progress.hidden {
    display: none;
}

.progress-bar {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, var(--primary), #e0a15a);
    transition: width 0.2s ease;
}

.file-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: grid;
    gap: 0.5rem;
    flex: 1;
    min-height: 0;
    overflow: auto;
    align-content: start;
}

.file-list:empty::after {
    content: "No files yet.";
    color: var(--muted);
    font-size: 0.9rem;
}

.file-item {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 0.3rem 0.5rem;
    align-items: center;
    padding: 0.65rem 0.7rem;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--bg-paper);
    box-shadow: var(--shadow-soft);
    cursor: pointer;
    transition:
        border-color 0.15s ease,
        background 0.15s ease;
}

.file-item:hover,
.file-item:focus-within {
    border-color: rgba(196, 92, 38, 0.35);
}

.file-item.active {
    border-color: var(--primary);
    background: var(--primary-soft);
}

.file-item .name {
    font-weight: 650;
    word-break: break-word;
    font-size: 0.92rem;
}

.file-item .meta {
    grid-column: 1 / -1;
    color: var(--muted);
    font-size: 0.78rem;
}

.file-item .remove {
    border: 0;
    background: transparent;
    color: var(--muted);
    cursor: pointer;
    font-size: 0.8rem;
    padding: 0.2rem 0.35rem;
    border-radius: 8px;
}

.file-item .remove:hover {
    color: var(--danger);
    background: rgba(180, 35, 24, 0.08);
}

.row.actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    margin-top: auto;
    flex-shrink: 0;
}

.btn {
    appearance: none;
    border: 1px solid var(--border-strong);
    background: var(--bg-paper);
    color: var(--text);
    border-radius: 10px;
    padding: 0.55rem 0.85rem;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    transition:
        background 0.15s ease,
        border-color 0.15s ease,
        opacity 0.15s ease;
}

.btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

.btn.primary {
    background: linear-gradient(180deg, #d9783a, var(--primary));
    border-color: transparent;
    color: #fffaf3;
}

.btn.primary:hover:not(:disabled) {
    filter: brightness(1.04);
}

.btn.ghost:hover:not(:disabled) {
    border-color: rgba(196, 92, 38, 0.4);
    background: var(--primary-soft);
}

.btn.small {
    padding: 0.35rem 0.65rem;
    font-size: 0.82rem;
}

label.btn {
    display: inline-flex;
    align-items: center;
}

/* Workspace */
.workspace-top {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    flex-shrink: 0;
}

.search-form {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 0.55rem;
}

#search-input {
    width: 100%;
    border: 1px solid var(--border-strong);
    background: var(--bg-paper);
    color: var(--text);
    border-radius: 12px;
    padding: 0.75rem 0.9rem;
    font: inherit;
    outline: none;
}

#search-input:focus {
    border-color: rgba(196, 92, 38, 0.55);
    box-shadow: 0 0 0 3px rgba(196, 92, 38, 0.12);
}

#search-input:disabled {
    opacity: 0.55;
}

.model-status {
    margin-top: -0.2rem;
}

.workspace-body {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.results {
    list-style: none;
    margin: 0;
    padding: 0 0.15rem 0.5rem 0;
    display: grid;
    gap: 0.85rem;
    overflow: auto;
    flex: 1;
    align-content: start;
}

.results:not(:empty) + .empty {
    display: none;
}

.result {
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--bg-paper);
    padding: 0.85rem 0.95rem;
    box-shadow: var(--shadow-soft);
}

.result-head {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    gap: 0.5rem 0.75rem;
    align-items: center;
    margin-bottom: 0.55rem;
}

.result-source {
    font-weight: 700;
    font-size: 0.95rem;
}

.result-score {
    font-family: var(--mono);
    font-size: 0.78rem;
    color: var(--accent);
    white-space: nowrap;
    background: var(--accent-soft);
    padding: 0.15rem 0.45rem;
    border-radius: 999px;
}

.result-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    width: 100%;
}

.context-block {
    display: grid;
    gap: 0.35rem;
    border: 1px solid var(--border);
    border-radius: 12px;
    overflow: hidden;
    background: var(--context);
}

.context-chunk {
    margin: 0;
    padding: 0.55rem 0.7rem;
    font-size: 0.9rem;
    white-space: pre-wrap;
    word-break: break-word;
    color: var(--text-soft);
    border-top: 1px solid transparent;
    cursor: pointer;
    transition: background 0.12s ease;
}

.context-chunk + .context-chunk {
    border-top-color: var(--border);
}

.context-chunk:hover {
    background: rgba(255, 253, 248, 0.7);
}

.context-chunk.is-match {
    background: var(--match);
    border-left: 3px solid var(--primary);
    color: var(--text);
    font-weight: 500;
}

.context-chunk .chunk-label {
    display: block;
    font-family: var(--mono);
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--muted);
    margin-bottom: 0.2rem;
}

.context-chunk.is-match .chunk-label {
    color: var(--primary-strong);
}

.result-meta {
    margin: 0.5rem 0 0;
    color: var(--muted);
    font-size: 0.8rem;
}

.empty {
    margin: 0.5rem 0 0;
}

/* Document reader */
.doc-toolbar {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 0.35rem 0.75rem;
    align-items: center;
    margin-bottom: 0.75rem;
    flex-shrink: 0;
}

.doc-toolbar #doc-back {
    grid-row: 1 / span 2;
}

.doc-title {
    font-weight: 700;
    font-size: 1rem;
    word-break: break-word;
}

.doc-meta {
    grid-column: 2;
}

.doc-layout {
    flex: 1;
    min-height: 0;
    display: grid;
    grid-template-columns: 88px minmax(0, 1fr);
    gap: 0.75rem;
}

.chunk-selector {
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: rgba(255, 253, 248, 0.7);
    padding: 0.5rem 0.4rem;
    overflow: hidden;
}

.chunk-selector-label {
    text-align: center;
    font-weight: 600;
    flex-shrink: 0;
}

.chunk-selector-list {
    flex: 1;
    min-height: 0;
    overflow: auto;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    padding: 0.1rem;
}

.chunk-pill {
    appearance: none;
    border: 1px solid transparent;
    background: transparent;
    color: var(--muted);
    border-radius: 8px;
    padding: 0.35rem 0.25rem;
    font: inherit;
    font-family: var(--mono);
    font-size: 0.75rem;
    font-weight: 650;
    cursor: pointer;
    text-align: center;
}

.chunk-pill:hover {
    background: var(--primary-soft);
    color: var(--text);
}

.chunk-pill.active {
    background: var(--primary);
    color: #fffaf3;
    border-color: transparent;
}

.chunk-pill.is-hit {
    box-shadow: inset 0 0 0 1px var(--match-border);
}

.doc-reader {
    min-height: 0;
    overflow: auto;
    border: 1px solid var(--border);
    border-radius: 12px;
    background: var(--bg-paper);
    padding: 0.35rem;
    scroll-behavior: smooth;
}

.doc-chunk {
    padding: 0.75rem 0.85rem;
    border-radius: 10px;
    border: 1px solid transparent;
    margin-bottom: 0.2rem;
    scroll-margin-top: 1rem;
    cursor: pointer;
    transition:
        background 0.12s ease,
        border-color 0.12s ease;
}

.doc-chunk:hover {
    background: var(--bg-soft);
}

.doc-chunk.active {
    background: var(--match);
    border-color: var(--match-border);
    box-shadow: 0 0 0 1px rgba(196, 140, 38, 0.15);
}

.doc-chunk .chunk-label {
    display: block;
    font-family: var(--mono);
    font-size: 0.72rem;
    font-weight: 650;
    color: var(--muted);
    margin-bottom: 0.3rem;
}

.doc-chunk.active .chunk-label {
    color: var(--primary-strong);
}

.doc-chunk .chunk-text {
    margin: 0;
    white-space: pre-wrap;
    word-break: break-word;
    font-size: 0.94rem;
    color: var(--text-soft);
    line-height: 1.55;
}

.doc-chunk.active .chunk-text {
    color: var(--text);
}

.footer {
    flex-shrink: 0;
    text-align: center;
    padding-bottom: 0.25rem;
}

.footer p {
    margin: 0.15rem 0;
}

@media (max-width: 640px) {
    .doc-layout {
        grid-template-columns: 1fr;
        grid-template-rows: auto 1fr;
    }

    .chunk-selector {
        max-height: 88px;
    }

    .chunk-selector-list {
        flex-direction: row;
        flex-wrap: nowrap;
        overflow-x: auto;
        overflow-y: hidden;
    }

    .chunk-pill {
        min-width: 2.4rem;
    }
}
