/* The web interface (§7).
 *
 * Plain CSS, no build step and no framework — the same reasoning as the
 * hand-written HTTP in this crate. What a mail client needs from its styling
 * is a legible column of text, a list that scans quickly, and the platform's
 * own type; none of that wants a toolchain.
 *
 * Light and dark are the reader's choice, not ours: `color-scheme` plus one
 * media query, so form controls and scrollbars follow too. */

:root {
    color-scheme: light dark;

    --bg: #ffffff;
    --bg-sunken: #f6f6f7;
    --bg-raised: #ffffff;
    --line: #e3e3e6;
    --text: #1c1c1e;
    --text-dim: #6b6b70;
    --accent: #0a66d0;
    --accent-soft: #e8f0fc;
    --unread: #0a66d0;
    --flagged: #d99408;
    --danger: #c0392b;

    --pane-folders: 15rem;
    --pane-list: 24rem;
    --radius: 7px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #1c1c1e;
        --bg-sunken: #161618;
        --bg-raised: #232326;
        --line: #333338;
        --text: #e8e8ea;
        --text-dim: #96969c;
        --accent: #64a6f5;
        --accent-soft: #23324a;
        --unread: #64a6f5;
        --flagged: #e0b23f;
        --danger: #ef7a6d;
    }
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    height: 100%;
    overflow: hidden;
    background: var(--bg);
    color: var(--text);
    /* The platform's own text, rendered by the platform. */
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
                 "Helvetica Neue", "Noto Sans", Arial, sans-serif;
    font-size: 14px;
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
}

body {
    display: grid;
    grid-template-rows: auto 1fr auto;
}

/* ---- the bars ---- */

.bar {
    display: flex;
    align-items: center;
    gap: .5rem;
    padding: .45rem .6rem;
    background: var(--bg-sunken);
    border-bottom: 1px solid var(--line);
}

.bar.status {
    border-bottom: none;
    border-top: 1px solid var(--line);
    font-size: 12px;
    color: var(--text-dim);
    min-height: 1.9rem;
}

#search-form { flex: 1; display: flex; }

#search {
    width: 100%;
    padding: .4rem .7rem;
    font: inherit;
    color: inherit;
    background: var(--bg-raised);
    border: 1px solid var(--line);
    border-radius: var(--radius);
}

#search:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: -1px;
    border-color: transparent;
}

button {
    font: inherit;
    color: inherit;
    background: var(--bg-raised);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: .35rem .7rem;
    cursor: pointer;
}

button:hover { background: var(--accent-soft); }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

button.icon { padding: .35rem .55rem; }

#error { color: var(--danger); }
#host { margin-left: auto; }

#update { margin-left: auto; display: flex; align-items: center; gap: .5rem; }
#update:not([hidden]) + #host { margin-left: 0; }
#update button { padding: .1rem .5rem; font-size: 12px; }
#update.is-failed #update-text { color: var(--danger); }

.spinner {
    width: .85rem;
    height: .85rem;
    border: 2px solid var(--line);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin .7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
    .spinner { animation: none; }
}

/* ---- the three panes ---- */

main {
    display: grid;
    grid-template-columns: var(--pane-folders) var(--pane-list) 1fr;
    min-height: 0;
}

nav#folders, #list-pane, #reader {
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
}

nav#folders {
    background: var(--bg-sunken);
    border-right: 1px solid var(--line);
    padding: .5rem;
}

.folder {
    display: flex;
    justify-content: space-between;
    gap: .5rem;
    width: 100%;
    text-align: left;
    background: none;
    border: none;
    border-radius: var(--radius);
    padding: .35rem .5rem;
}

.folder.is-current { background: var(--accent-soft); color: var(--accent); }
.folder .count { color: var(--text-dim); font-variant-numeric: tabular-nums; }
.folder.is-current .count { color: inherit; }

#list-pane { border-right: 1px solid var(--line); }

ol#list { margin: 0; padding: 0; list-style: none; }
ol#list:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }

li.message {
    padding: .5rem .7rem;
    border-bottom: 1px solid var(--line);
    cursor: pointer;
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 0 .5rem;
}

li.message:hover { background: var(--bg-sunken); }
li.message[aria-selected="true"] { background: var(--accent-soft); }

li.message .from { font-weight: 500; }
li.message.unread .from,
li.message.unread .subject { font-weight: 700; }
li.message.unread .from { color: var(--unread); }

li.message .date {
    color: var(--text-dim);
    font-size: 12px;
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
}

li.message .subject,
li.message .preview {
    grid-column: 1 / -1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

li.message .preview { color: var(--text-dim); font-size: 13px; }
li.message .marks { color: var(--flagged); }

/* §7: one list, a colour per account, only when there is more than one. */
li.message.multi-account {
    border-left: 3px solid hsl(var(--account-hue) 55% 48%);
    padding-left: calc(.7rem - 3px);
}

@media (prefers-color-scheme: dark) {
    li.message.multi-account {
        border-left-color: hsl(var(--account-hue) 50% 62%);
    }
}

/* ---- the reading pane ---- */

#reader { display: flex; flex-direction: column; }

.head {
    padding: .9rem 1.1rem .6rem;
    border-bottom: 1px solid var(--line);
}

.head h1 { margin: 0 0 .35rem; font-size: 1.15rem; line-height: 1.35; }
.head .who { color: var(--text-dim); }
.head .who strong { color: var(--text); font-weight: 600; }

.actions { display: flex; flex-wrap: wrap; gap: .35rem; margin-top: .6rem; }

.notice {
    display: flex;
    align-items: center;
    gap: .6rem;
    margin: .6rem 1.1rem 0;
    padding: .45rem .7rem;
    background: var(--bg-sunken);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    color: var(--text-dim);
}

.notice button { padding: .2rem .55rem; font-size: 13px; white-space: nowrap; }
.notice .text { flex: 1; }

/* The message itself renders in a sandboxed frame: it is someone else's
   markup, and the sandbox is what keeps it from being anything more than
   markup (§9). */
iframe.body {
    flex: 1;
    width: 100%;
    border: none;
    background: var(--bg);
}

.attachments {
    display: flex;
    flex-wrap: wrap;
    gap: .4rem;
    padding: .5rem 1.1rem;
    border-top: 1px solid var(--line);
    color: var(--text-dim);
}

.empty {
    margin: auto;
    padding: 2rem;
    color: var(--text-dim);
    text-align: center;
}

/* ---- a phone, which is what the QR code in `serve` is for ---- */

@media (max-width: 45rem) {
    main { grid-template-columns: 1fr; }

    nav#folders, #list-pane, #reader { display: none; }
    body[data-pane="folders"] nav#folders,
    body[data-pane="list"] #list-pane,
    body[data-pane="reader"] #reader { display: block; }
    body[data-pane="reader"] #reader { display: flex; }

    #back { display: block; }
}

@media (min-width: 45.01rem) {
    #back { display: none !important; }
}

/* ---- the first launch (§9) ---- */

/* The guide takes the middle row the panes usually have. `main` is not merely
 * covered but removed from the grid, so nothing behind it can be tabbed into
 * while there is nothing there to read. */
body[data-state="setup"] > main { display: none; }

/* Every control in the top bar acts on mail: a search that can only return
 * nothing, a Write that cannot send. The bar goes with them rather than
 * staying as an empty strip above the guide. The status bar stays — it is
 * where the address to open on a phone is. */
body[data-state="setup"] > header { display: none; }

/* With the top bar and the panes both gone, the guide is the row that has to
 * grow. Restated rather than inherited: the default `auto 1fr auto` counts
 * rows off the children that are left, which would hand the spare height to
 * the status bar. */
body[data-state="setup"] { grid-template-rows: 1fr auto; }

/* `hidden` has to beat the `display` below it. The attribute's rule comes from
 * the browser's own stylesheet and is less specific than an id, so without
 * this the guide stays on screen underneath the mailbox once an account
 * appears. */
#setup[hidden] { display: none; }

#setup {
    min-height: 0;
    overflow-y: auto;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1.5rem;
}

.setup-card {
    max-width: 34rem;
    background: var(--bg-raised);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    padding: 1.4rem 1.6rem;
}

.setup-card h1 {
    margin: 0 0 .6rem;
    font-size: 1.25rem;
    font-weight: 600;
}

.setup-card p { margin: 0 0 .8rem; }

.setup-card h1 + p { margin-top: 0; }

/* The form. Labels above their fields rather than beside them, unlike the
 * compose window's `.field`: this is filled in once, by someone who has not
 * seen the interface before, and a boxed input that says what it is beats a
 * dense row that saves a line. */
.setup-field {
    display: flex;
    flex-direction: column;
    gap: .25rem;
    margin-bottom: .8rem;
    flex: 1;
    min-width: 0;
}

.setup-field label {
    color: var(--text-dim);
    font-size: 13px;
}

.setup-field input {
    width: 100%;
    padding: .45rem .6rem;
    font: inherit;
    color: inherit;
    background: var(--bg);
    border: 1px solid var(--line);
    border-radius: var(--radius);
}

.setup-field input:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: -1px;
    border-color: transparent;
}

.setup-field.narrow { flex: 0 0 6rem; }

.setup-row { display: flex; gap: .6rem; }

/* Same reason as `.field[hidden]`: `display: flex` beats the user agent's
 * rule for the attribute, so hiding these needs saying twice. */
#setup-servers[hidden],
#setup-form[hidden],
#setup-guide[hidden],
#setup-error[hidden] { display: none; }

#setup-servers {
    padding-top: .2rem;
    margin-top: .4rem;
    border-top: 1px solid var(--line);
}

#setup-servers h2 {
    margin: .9rem 0 .5rem;
    font-size: 12px;
    font-weight: 600;
    letter-spacing: .04em;
    text-transform: uppercase;
    color: var(--text-dim);
}

.setup-check {
    display: flex;
    align-items: center;
    gap: .45rem;
    margin-bottom: .8rem;
    color: var(--text-dim);
    font-size: 13px;
}

.setup-check input { margin: 0; }

/* Same reason as `.field[hidden]` below: the flex `display` on `.setup-field`
   beats the user agent's rule for the attribute. */
#setup-smtp-user-field[hidden] { display: none; }

.setup-actions {
    display: flex;
    gap: .6rem;
    align-items: center;
    margin-top: 1rem;
}

/* No empty line where a result has not been asked for yet. */
#setup-status:empty { display: none; }

.setup-error {
    margin: .8rem 0 0;
    color: var(--danger);
    font-size: 13px;
}

.setup-command {
    margin: 0 0 .6rem;
    padding: .6rem .8rem;
    background: var(--bg-sunken);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    /* The command is the point of the page, so it wraps rather than scrolls
     * out of sight on a phone. */
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    user-select: all;
}

.setup-command code,
.setup-note code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 13px;
}

.setup-note {
    color: var(--text-dim);
    font-size: 13px;
}

.setup-card p:last-child { margin-bottom: 0; }

/* ---- the compose window (§7) ---- */

dialog#compose-window {
    width: min(46rem, 92vw);
    max-height: 88vh;
    padding: 0;
    border: 1px solid var(--line);
    border-radius: calc(var(--radius) + 3px);
    background: var(--bg);
    color: var(--text);
    box-shadow: 0 12px 40px rgb(0 0 0 / .28);
}

dialog#compose-window::backdrop { background: rgb(0 0 0 / .35); }

#compose-form {
    display: flex;
    flex-direction: column;
    max-height: 88vh;
}

.field {
    display: flex;
    align-items: center;
    gap: .6rem;
    padding: .35rem .8rem;
    border-bottom: 1px solid var(--line);
}

/* `display: flex` above beats the user agent's `[hidden] { display: none }`,
   so hiding a field needs saying twice. */
.field[hidden] { display: none; }

.field label {
    width: 4rem;
    color: var(--text-dim);
    flex: none;
}

.field input, .field select {
    flex: 1;
    min-width: 0;
    padding: .3rem 0;
    font: inherit;
    color: inherit;
    background: none;
    border: none;
}

.field input:focus-visible, .field select:focus-visible { outline: none; }
.field:focus-within { background: var(--bg-sunken); }

#compose-body {
    flex: 1;
    min-height: 14rem;
    padding: .8rem;
    font: inherit;
    color: inherit;
    background: none;
    border: none;
    resize: none;
}

#compose-body:focus-visible { outline: none; }

.compose-actions {
    display: flex;
    align-items: center;
    gap: .5rem;
    padding: .6rem .8rem;
    border-top: 1px solid var(--line);
    background: var(--bg-sunken);
}

.compose-actions .grow { flex: 1; }
#compose-note { color: var(--text-dim); font-size: 13px; }

button.primary {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

button.primary:hover { filter: brightness(1.08); background: var(--accent); }
button:disabled { opacity: .55; cursor: default; }
