/* Entry pages (create a room, join a room) and the two-column chat room. */
:root {
    color-scheme: light dark;
    --font: system-ui, -apple-system, "Segoe UI", sans-serif;
    --bg: #fbfbfc;
    --surface: #ffffff;
    --text: #17181c;
    --muted: #61636b;
    --border: #dcdde2;
    --accent: #2f5bd8;
    --accent-text: #ffffff;
    --error: #b3261e;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #16171a;
        --surface: #1e2024;
        --text: #eceef2;
        --muted: #a0a3ad;
        --border: #34363d;
        --accent: #7ea1ff;
        --accent-text: #11131a;
        --error: #f2b8b5;
    }
}

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

.column {
    max-width: 32rem;
    margin: 0 auto;
    padding: 3rem 1rem;
}

h1 {
    font-size: 1.75rem;
    margin: 0 0 0.5rem;
}

.lede {
    margin: 0 0 1.5rem;
    color: var(--muted);
}

.muted {
    color: var(--muted);
    font-size: 0.9rem;
}

.card {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 0.6rem;
    padding: 1.25rem;
}

label {
    font-size: 0.9rem;
    font-weight: 600;
}

input {
    font: inherit;
    color: inherit;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 0.4rem;
    padding: 0.5rem 0.65rem;
}

label + input {
    margin-bottom: 0.75rem;
}

input:focus-visible,
button:focus-visible,
.button:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

button,
.button {
    font: inherit;
    font-weight: 600;
    display: inline-block;
    margin-top: 0.5rem;
    padding: 0.55rem 1rem;
    border: 0;
    border-radius: 0.4rem;
    background: var(--accent);
    color: var(--accent-text);
    text-decoration: none;
    text-align: center;
    cursor: pointer;
}

.error {
    margin: 0 0 0.5rem;
    color: var(--error);
    font-size: 0.9rem;
}

/* --- The room: conversation on the left, people on the right (story 3). --- */

.room {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 14rem;
    gap: 1.5rem;
    max-width: 58rem;
    margin: 0 auto;
    padding: 2rem 1rem;
    align-items: start;
}

.chat {
    min-width: 0;
}

.chat-header {
    margin-bottom: 1.5rem;
}

.chat-header .muted {
    margin: 0;
}

.messages {
    display: flex;
    flex-direction: column;
    list-style: none;
    margin: 0;
    padding: 0;
}

/* Spacing is a margin on the message rather than a `gap` on the list, because
   the two cases want different amounts: a new speaker needs air, a second line
   from the same speaker should hug the one above it. */
.message {
    display: flex;
    flex-direction: column;
    max-width: 80%;
    margin-top: 0.75rem;
}

.messages > :first-child {
    margin-top: 0;
}

.message.grouped {
    margin-top: 0.375rem;
}

/* The whole story of this layout: mine on the right, theirs on the left. */
.message.theirs {
    align-self: flex-start;
    align-items: flex-start;
}

.message.mine {
    align-self: flex-end;
    align-items: flex-end;
}

/* The name belongs to the bubble under it, so it sits tight against it: at
   the page's 1.5 line height it would otherwise add half a line of air above
   every bubble that carries one, and the conversation would read as if it
   were double spaced wherever somebody new spoke. */
.message .author {
    margin: 0 0 0.15rem 0.65rem;
    font-size: 0.8rem;
    line-height: 1.2;
    font-weight: 600;
    color: var(--muted);
}

.bubble {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 0.9rem;
    padding: 0.5rem 0.75rem;
}

.message.mine .bubble {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--accent-text);
}

.bubble .body {
    margin: 0;
    /* Keeps a pasted wall of text, or a long unbroken URL, inside the bubble. */
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.stamp {
    display: block;
    margin-top: 0.15rem;
    font-size: 0.72rem;
    color: var(--muted);
}

.message.mine .stamp {
    color: var(--accent-text);
    opacity: 0.8;
}

/* The empty-state line, and how it retires itself the moment a message
   arrives beside it. */
.messages .empty {
    align-self: flex-start;
    max-width: 100%;
}

.messages .empty:not(:only-child) {
    display: none;
}

.people {
    position: sticky;
    top: 2rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 0.6rem;
    padding: 1rem;
}

.people h2 {
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--muted);
    margin: 0 0 0.5rem;
}

.people p {
    margin: 0;
}

.people-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

/* On a phone the sidebar goes above the conversation rather than beside it. */
@media (max-width: 40rem) {
    .room {
        grid-template-columns: minmax(0, 1fr);
    }

    .people {
        position: static;
        order: -1;
    }
}

/* --- The composer and the link card (story 4). --- */

.composer {
    margin-top: 1rem;
    /* Sticky rather than fixed: it stays at the bottom of the conversation
       column as it scrolls, without covering the last message. */
    position: sticky;
    bottom: 0;
    padding: 0.75rem 0;
    background: var(--bg);
}

/* One fieldset around every control, so a post in flight can disable them
   all at once (`hx-disable` on the form). */
.composer-fields {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    min-width: 0;
    margin: 0;
    padding: 0;
    border: 0;
}

.composer-fields:disabled {
    opacity: 0.6;
}

.composer-row {
    display: flex;
    /* Centred, not stretched: the controls are given the same height below,
       so there is nothing left to stretch. */
    align-items: center;
    gap: 0.5rem;
}

/* The field and the button are one strip, so they have to agree on their
   height -- and they are told to, rather than left to `align-items: stretch`,
   which browsers do not apply to a button the way they apply it to a div.
   `border-box` so the number below is the whole box, and a transparent border
   on the button so it has the same inner room as the bordered field. */
.composer-row input:not([type="file"]),
.composer-row button,
.composer .attach {
    box-sizing: border-box;
    height: 2.625rem;
    margin: 0;
    border: 1px solid transparent;
    line-height: 1.5;
}

.composer-row input:not([type="file"]) {
    flex: 1;
    min-width: 0;
    border-color: var(--border);
}

.composer-row button {
    padding: 0 1rem;
}

/* The focus ring is drawn inside the field: offset outside, it would stand
   2px proud of the strip and make the field read as the taller of the two. */
.composer-row input:not([type="file"]):focus-visible {
    outline-offset: -2px;
}

/* --- Attaching an image (story 7). --- */

/* The file input's label, drawn as a square button to the left of the field.
   The input itself is visually hidden but still takes focus, so its ring is
   drawn on the label that stands for it. */
.composer .attach {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: none;
    width: 2.625rem;
    padding: 0;
    border-color: var(--border);
    border-radius: 0.4rem;
    background: var(--surface);
    color: var(--muted);
    cursor: pointer;
}

.composer .attach:hover {
    color: var(--accent);
    border-color: var(--accent);
}

.composer input[type="file"]:focus-visible + .attach {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.composer-attachment {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.5rem;
    border: 1px solid var(--border);
    border-radius: 0.6rem;
    background: var(--surface);
}

/* `hidden` has to win over the `display` above. */
.composer-attachment[hidden] {
    display: none;
}

.composer-attachment img {
    max-height: 5rem;
    max-width: 8rem;
    border-radius: 0.4rem;
    object-fit: cover;
}

.composer-attachment .attachment-remove {
    margin: 0;
    padding: 0.35rem 0.75rem;
    background: transparent;
    color: var(--muted);
    border: 1px solid var(--border);
    font-weight: 500;
}

.composer-attachment .attachment-remove:hover {
    color: var(--error);
    border-color: var(--error);
}

.composer-notice {
    margin: 0.75rem 0 0;
}

/* Visible to a screen reader, gone from the page: the composer's field is
   labelled by its placeholder for everyone else. */
.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    margin: -1px;
    padding: 0;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
}

.link-card {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
    margin-top: 0.5rem;
    padding: 0.5rem 0.65rem;
    border: 1px solid var(--border);
    border-radius: 0.6rem;
    background: var(--bg);
    color: inherit;
    text-decoration: none;
}

/* Inside your own bubble the card sits on the accent colour, so it borrows
   the page background back rather than tinting itself. */
.message.mine .link-card {
    color: var(--text);
}

.link-card:hover,
.link-card:focus-visible {
    border-color: var(--accent);
}

.link-title {
    font-weight: 600;
    /* A long headline wraps to two lines and then stops. */
    overflow-wrap: anywhere;
}

.link-description {
    font-size: 0.85rem;
    color: var(--muted);
    overflow-wrap: anywhere;
}

.link-host {
    font-size: 0.75rem;
    color: var(--muted);
}

/* --- Image messages (story 7). After the link card, whose margins they adjust. --- */

/* An image message: the picture fills the top of the bubble, and the caption,
   if there is one, sits under it with the bubble's usual padding. */
.bubble.with-image {
    padding: 0.25rem;
    overflow: hidden;
}

.bubble.with-image .body,
.bubble.with-image .link-card,
.bubble.with-image .stamp {
    margin-left: 0.5rem;
    margin-right: 0.5rem;
}

.bubble.with-image .stamp {
    margin-bottom: 0.25rem;
}

.bubble.with-image .body {
    margin-top: 0.35rem;
}

/* The width and height attributes give the picture its size and shape
   before it loads; `height: auto` keeps that shape while the width is capped.
   Two caps: the width that makes the picture 22rem tall, from `--ratio`
   (width over height, set in the markup), and the bubble's own width. The
   first sits on the link because it is a plain length, which the bubble can
   size itself to; a percentage it would ignore, and grow to its full width
   around a narrow portrait. */
.posted-image {
    display: block;
    max-width: calc(22rem * var(--ratio));
}

.posted-image img {
    display: block;
    height: auto;
    max-width: 100%;
    max-height: 22rem;
    border-radius: 0.7rem;
}
