/*
 * Documents (documents.js): the Documents window and its viewer, a document NowPaw wrote as a card in the chat, the
 * download menu, and a document as it is printed or saved as a web page.
 *
 * The rules between `sheet:start` and `sheet:end` are a document's own: a downloaded web page carries them inline, so
 * they use nothing from outside that part but the page's colour tokens, each with a fallback.
 */

/* An icon button: the download menu's, Delete, and Back. */
button.doc-icon {
    flex: none;
    width: 30px;
    height: 30px;
    padding: 0;
}

button.doc-icon .icon {
    width: 15px;
    height: 15px;
}

/* The download menu: a popover, so no window clips it; placed at its button by documents.js. */
.download-menu {
    position: fixed;
    inset: auto;
    min-width: 210px;
    margin: 0;
    padding: 4px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--surface);
    color: var(--text);
    box-shadow: 0 8px 28px rgb(0 0 0 / 16%);
}

.download-choice {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 16px;
    width: 100%;
    padding: 8px 10px;
    border-radius: 8px;
    background: transparent;
    color: var(--text);
    font-size: 0.875rem;
    font-weight: 500;
    text-align: left;
}

.download-choice:hover,
.download-choice:focus-visible {
    background: var(--surface-2);
}

.download-choice:focus-visible {
    outline-offset: -2px;
}

.download-detail {
    color: var(--muted);
    font-size: 0.75rem;
    font-weight: 400;
    white-space: nowrap;
}

/* Under one of NowPaw's answers: its download button, quiet until the answer is pointed at. */
.answer-tools {
    display: flex;
    margin-top: 6px;
}

.answer-tools .download-button {
    opacity: 0.6;
}

.message.assistant:hover .answer-tools .download-button,
.answer-tools .download-button:focus-visible,
.answer-tools .download-button[aria-expanded='true'] {
    opacity: 1;
}

@media (hover: none) {
    .answer-tools .download-button {
        opacity: 1;
    }
}

/* A file above the message box while its text is read and kept. */
.attachment.reading {
    border-style: dashed;
}

.attachment-state {
    color: var(--muted);
    font-size: 0.75rem;
    white-space: nowrap;
}

/* In the chat: a document NowPaw wrote. */
.doc-card {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
    max-width: 440px;
    padding: 10px 10px 10px 12px;
    border: 1px solid var(--line);
    border-radius: var(--card-radius);
    background: var(--surface);
}

.doc-card-icon,
.doc-kind {
    display: grid;
    flex: none;
    place-items: center;
    width: 34px;
    height: 34px;
    border-radius: 9px;
    background: color-mix(in srgb, var(--accent) 14%, transparent);
    color: var(--accent);
}

.doc-card-icon .icon,
.doc-kind .icon {
    width: 18px;
    height: 18px;
}

.doc-card-text {
    display: grid;
    flex: 1;
    min-width: 0;
}

.doc-card-title {
    overflow: hidden;
    font-size: 0.9375rem;
    font-weight: 600;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.doc-card-detail {
    color: var(--muted);
    font-size: 0.75rem;
}

/* The Documents window: the list. */
.documents-body {
    container-type: inline-size;
}

.doc-window {
    display: flex;
    flex-direction: column;
    gap: 10px;
    min-height: 100%;
}

.doc-list {
    display: grid;
    margin: 0;
    padding: 0;
    list-style: none;
}

.doc-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px 8px;
    padding: 6px 0;
    border-bottom: 1px solid var(--line);
}

.doc-row:last-child {
    border-bottom: 0;
}

.doc-open {
    display: flex;
    flex: 1 1 200px;
    align-items: center;
    justify-content: flex-start;
    gap: 10px;
    min-width: 0;
    padding: 4px 6px;
    border-radius: 10px;
    background: transparent;
    color: var(--text);
    font-weight: 400;
    text-align: left;
}

.doc-open:hover {
    background: var(--surface-2);
}

.doc-words {
    display: grid;
    min-width: 0;
}

.doc-name,
.doc-title {
    overflow: hidden;
    margin: 0;
    font-size: 0.875rem;
    font-weight: 600;
    line-height: 1.35;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.doc-meta {
    color: var(--muted);
    font-size: 0.75rem;
    line-height: 1.35;
}

.doc-actions {
    display: flex;
    flex: none;
    align-items: center;
    gap: 6px;
    margin-left: auto;
}

/* The kinds, told apart by colour as well as by name (in the details under each). */
.doc-kind.pdf {
    background: color-mix(in srgb, #c0392b 14%, transparent);
    color: #b03224;
}

.doc-kind.docx {
    background: color-mix(in srgb, #2b5797 14%, transparent);
    color: #2b5797;
}

.doc-kind.html {
    background: color-mix(in srgb, #c05a12 14%, transparent);
    color: #a94f10;
}

.doc-kind.text {
    background: var(--surface-2);
    color: var(--muted);
}

@media (prefers-color-scheme: dark) {
    .doc-kind.pdf {
        color: #f08a7e;
    }

    .doc-kind.docx {
        color: #8fb3ec;
    }

    .doc-kind.html {
        color: #f0a36a;
    }
}

/* Delete, asked in the row. */
.doc-confirm {
    display: flex;
    flex-basis: 100%;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    border-radius: 10px;
    background: var(--warn-bg);
    color: var(--warn-text);
}

.doc-confirm p {
    flex: 1;
    min-width: 12em;
    margin: 0;
    font-size: 0.8125rem;
}

.doc-note,
.doc-usage {
    margin: 0;
    color: var(--muted);
    font-size: 0.75rem;
}

.doc-usage {
    margin-top: auto;
}

.doc-empty {
    display: grid;
    flex: 1;
    place-content: center;
    justify-items: center;
    gap: 6px;
    padding: 16px;
    color: var(--muted);
    text-align: center;
}

.doc-empty .icon {
    width: 28px;
    height: 28px;
}

.doc-empty strong {
    color: var(--text);
}

.doc-empty p {
    max-width: 26em;
    margin: 0;
    font-size: 0.8125rem;
}

/* A narrow window: the buttons go under the name, which then has the window's width. */
@container (max-width: 600px) {
    .doc-row .doc-actions {
        flex-basis: 100%;
        justify-content: flex-end;
    }

    .doc-open {
        flex-basis: 100%;
    }
}

/* The Documents window: one document. */
.doc-viewer {
    display: flex;
    flex-direction: column;
    gap: 10px;
    height: 100%;
}

.doc-viewer-head {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px 8px;
}

.doc-viewer-head .doc-words {
    flex: 1 1 160px;
}

.doc-viewer-body {
    flex: 1;
    min-height: 120px;
    overflow: auto;
    padding: 20px 22px;
    border: 1px solid var(--line);
    border-radius: var(--card-radius);
    background: var(--surface);
}

.doc-plain {
    margin: 0;
    font: 0.8125rem/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

@container (max-width: 600px) {
    .doc-viewer-head .doc-actions {
        flex-basis: 100%;
        justify-content: flex-end;
    }
}

@container (max-width: 420px) {
    .doc-viewer-body {
        padding: 14px;
    }
}

/* Printing: a frame of this page's origin, off screen but laid out, at about a printed page's width. */
.print-frame {
    position: fixed;
    top: 0;
    left: -10000px;
    width: 794px;
    height: 1123px;
    border: 0;
}

/* sheet:start */
.doc-sheet {
    --doc-text: var(--text, #1c1c1a);
    --doc-muted: var(--muted, #6a6a64);
    --doc-line: var(--line, #e0dfd9);
    --doc-code: var(--code-bg, #f0efe9);
    --doc-link: var(--accent, #2e6b4c);
    max-width: 46rem;
    margin: 0 auto;
    color: var(--doc-text);
    font: 0.9375rem/1.6 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
    overflow-wrap: break-word;
}

.doc-sheet > * {
    margin: 0 0 0.85em;
}

.doc-sheet > :last-child {
    margin-bottom: 0;
}

.doc-sheet h1,
.doc-sheet h2,
.doc-sheet h3,
.doc-sheet h4,
.doc-sheet h5,
.doc-sheet h6 {
    line-height: 1.3;
    font-weight: 650;
}

.doc-sheet h1 {
    margin-bottom: 0.6em;
    font-size: 1.75em;
    letter-spacing: -0.01em;
}

.doc-sheet h2 {
    margin-top: 1.5em;
    padding-bottom: 0.25em;
    border-bottom: 1px solid var(--doc-line);
    font-size: 1.35em;
}

.doc-sheet h3 {
    margin-top: 1.3em;
    font-size: 1.15em;
}

.doc-sheet h4,
.doc-sheet h5,
.doc-sheet h6 {
    margin-top: 1.1em;
    font-size: 1em;
}

.doc-sheet h6 {
    color: var(--doc-muted);
}

.doc-sheet > h1:first-child,
.doc-sheet > h2:first-child,
.doc-sheet > h3:first-child {
    margin-top: 0;
}

.doc-sheet ul,
.doc-sheet ol {
    padding-left: 1.6em;
}

.doc-sheet ul {
    list-style: disc;
}

.doc-sheet ol {
    list-style: decimal;
}

.doc-sheet li + li {
    margin-top: 0.25em;
}

.doc-sheet code {
    padding: 0.1em 0.35em;
    border-radius: 4px;
    background: var(--doc-code);
    font: 0.875em/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.doc-sheet pre {
    overflow-x: auto;
    padding: 12px 14px;
    border-radius: 8px;
    background: var(--doc-code);
    font: 0.84em/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    white-space: pre;
}

.doc-sheet pre code {
    padding: 0;
    background: none;
    font: inherit;
}

.doc-sheet blockquote {
    margin-left: 0;
    margin-right: 0;
    padding: 0.1em 0 0.1em 14px;
    border-left: 3px solid var(--doc-line);
    color: var(--doc-muted);
}

.doc-sheet hr {
    margin: 1.6em 0;
    border: 0;
    border-top: 1px solid var(--doc-line);
}

.doc-sheet a {
    color: var(--doc-link);
    text-decoration: underline;
    text-underline-offset: 2px;
}

.doc-sheet table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9em;
}

.doc-sheet th,
.doc-sheet td {
    padding: 6px 10px;
    border: 1px solid var(--doc-line);
    text-align: left;
    vertical-align: top;
}

.doc-sheet th {
    background: var(--doc-code);
    font-weight: 600;
}

.doc-sheet .align-center {
    text-align: center;
}

.doc-sheet .align-right {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

/* Printed, or saved as a web page: dark on white, whatever the theme of the page it came from. */
.doc-print {
    margin: 0;
    background: #ffffff;
    color: #1c1c1a;
}

.doc-print .doc-sheet {
    --doc-text: #1c1c1a;
    --doc-muted: #57574f;
    --doc-line: #d4d3cc;
    --doc-code: #f3f2ed;
    --doc-link: #1f5c3f;
    padding: 48px 24px 64px;
}

.widget-print .print-widget {
    display: block;
    width: 100%;
    height: 100vh;
    border: 0;
}

@media print {
    @page {
        margin: 18mm 16mm;
    }

    .doc-print .doc-sheet {
        max-width: none;
        padding: 0;
        font-size: 10.5pt;
    }

    .doc-sheet pre {
        overflow: visible;
        white-space: pre-wrap;
    }

    .doc-sheet h1,
    .doc-sheet h2,
    .doc-sheet h3,
    .doc-sheet h4,
    .doc-sheet h5,
    .doc-sheet h6 {
        break-after: avoid;
    }

    .doc-sheet pre,
    .doc-sheet blockquote,
    .doc-sheet tr {
        break-inside: avoid;
    }

    .doc-sheet thead {
        display: table-header-group;
    }
}
/* sheet:end */
