:root {
    --bg: #0f1115;
    --surface: #161922;
    --surface-2: #1e222d;
    --border: #2a2f3d;
    --text: #e7e9ee;
    --muted: #8a90a2;
    --accent: #6aa6ff;
    --accent-on: #0b0d12;
    --mine: #3a6ebf;
    --mine-text: #ffffff;
    --error: #c4453a;
    --online: #6ec27c;
    --ring: rgba(106, 166, 255, 0.35);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.30), 0 10px 30px rgba(0, 0, 0, 0.22);

    --container: 920px;
    --radius: 12px;
    --radius-sm: 8px;
    --radius-pill: 999px;
}

@media (prefers-color-scheme: light) {
    :root {
        --bg: #f7f7fa;
        --surface: #ffffff;
        --surface-2: #f0f1f5;
        --border: #d8dbe3;
        --text: #1a1c22;
        --muted: #6b7280;
        --accent: #3b82f6;
        --accent-on: #ffffff;
        --mine: #3b82f6;
        --mine-text: #ffffff;
        --error: #c4453a;
        --online: #2f9e54;
        --ring: rgba(59, 130, 246, 0.30);
        --shadow: 0 1px 2px rgba(16, 24, 40, 0.08), 0 10px 28px rgba(16, 24, 40, 0.10);
    }
}

html[data-theme="light"] {
    --bg: #f7f7fa;
    --surface: #ffffff;
    --surface-2: #f0f1f5;
    --border: #d8dbe3;
    --text: #1a1c22;
    --muted: #6b7280;
    --accent: #3b82f6;
    --accent-on: #ffffff;
    --mine: #3b82f6;
    --mine-text: #ffffff;
    --error: #c4453a;
    --online: #2f9e54;
    --ring: rgba(59, 130, 246, 0.30);
    --shadow: 0 1px 2px rgba(16, 24, 40, 0.08), 0 10px 28px rgba(16, 24, 40, 0.10);
}

html[data-theme="dark"] {
    --bg: #0f1115;
    --surface: #161922;
    --surface-2: #1e222d;
    --border: #2a2f3d;
    --text: #e7e9ee;
    --muted: #8a90a2;
    --accent: #6aa6ff;
    --accent-on: #0b0d12;
    --mine: #3a6ebf;
    --mine-text: #ffffff;
    --error: #c4453a;
    --online: #6ec27c;
    --ring: rgba(106, 166, 255, 0.35);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.30), 0 10px 30px rgba(0, 0, 0, 0.22);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
    margin: 0;
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;
}

a { color: var(--accent); }

:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
    border-radius: 4px;
}

@media (prefers-reduced-motion: reduce) {
    * { transition: none !important; animation: none !important; scroll-behavior: auto !important; }
}

.theme-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 32px;
    background: transparent;
    border: 1px solid var(--border);
    color: var(--muted);
    padding: 4px 12px;
    border-radius: var(--radius-pill);
    font-size: 13px;
    cursor: pointer;
    transition: color 0.15s ease, border-color 0.15s ease, opacity 0.15s ease;
}

.theme-toggle:hover { color: var(--text); border-color: var(--text); }

.theme-toggle svg {
    width: 16px;
    height: 16px;
    display: block;
}

[data-sound-toggle] .i-off { display: none; }
[data-sound-toggle][data-muted="true"] .i-on { display: none; }
[data-sound-toggle][data-muted="true"] .i-off { display: block; }
[data-sound-toggle][data-muted="true"] { opacity: 0.6; }

[data-theme-toggle] .i-dark { display: none; }
html[data-theme="dark"] [data-theme-toggle] .i-light { display: none; }
html[data-theme="dark"] [data-theme-toggle] .i-dark { display: block; }

.session-chats-remove svg {
    width: 16px;
    height: 16px;
    display: block;
}

button.primary, .primary {
    background: var(--accent);
    color: var(--accent-on);
    border: 0;
    padding: 11px 20px;
    border-radius: var(--radius-sm);
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: filter 0.15s ease, transform 0.05s ease;
}

button.primary:hover { filter: brightness(1.08); }
button.primary:active { transform: translateY(1px); }

input[type="text"] {
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface-2);
    color: var(--text);
    font-size: 16px;
    transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

input[type="text"]:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--ring);
}

body.landing {
    display: grid;
    place-items: center;
    min-height: 100vh;
    min-height: 100dvh;
}

body.landing main {
    text-align: center;
    padding: 24px;
}

body.landing h1 {
    font-size: clamp(40px, 11vw, 56px);
    margin: 0 0 8px;
    letter-spacing: -1px;
}

body.landing .lede {
    color: var(--muted);
    margin: 0 0 28px;
}

.muted-link {
    margin-top: 16px;
    color: var(--muted);
    font-size: 14px;
}

.muted-link a {
    color: var(--muted);
    text-decoration: underline;
}

.ttl-field {
    margin: 0 0 20px;
    font-size: 14px;
    color: var(--muted);
}

.ttl-field label {
    display: block;
    margin-bottom: 6px;
}

.ttl-inputs {
    display: flex;
    gap: 8px;
    justify-content: center;
}

.ttl-inputs input,
.ttl-inputs select {
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    background: var(--surface-2);
    color: var(--text);
    font-size: 14px;
}

.ttl-inputs input {
    width: 88px;
    text-align: center;
}

body.chat-page {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
}

.chat-header {
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 12px 24px;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
}

.chat-header .inner,
.chat-main > .inner,
.send {
    max-width: var(--container);
    margin: 0 auto;
    width: 100%;
}

.chat-header .inner {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0;
}

.chat-header .brand {
    font-weight: 700;
    text-decoration: none;
    letter-spacing: -0.3px;
}

.chat-header .brand-link {
    color: var(--muted);
    text-decoration: none;
    font-size: 14px;
}

.chat-header .brand-link:hover { color: var(--text); }

.chat-header .spacer {
    flex: 1;
}

.chat-header .header-break {
    display: none;
}

.chat-header .me {
    color: var(--text);
    background: var(--surface-2);
    padding: 4px 12px;
    border-radius: var(--radius-pill);
    font-size: 14px;
    max-width: 9rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-header .online {
    color: var(--muted);
    font-size: 13px;
    white-space: nowrap;
}

.chat-header .online::before {
    content: "";
    display: inline-block;
    width: 7px;
    height: 7px;
    margin-right: 6px;
    border-radius: 50%;
    background: var(--online);
    vertical-align: middle;
}

.chat-header .ttl-note {
    color: var(--muted);
    font-size: 13px;
    white-space: nowrap;
}

.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.chat-main > .inner {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    padding: 0 24px;
}

.join-card {
    margin: 48px auto;
    max-width: 440px;
    width: 100%;
    padding: 24px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
}

.join-card h2 { margin: 0 0 8px; }
.join-card p { color: var(--muted); margin: 0 0 16px; }

.join-card form {
    display: flex;
    gap: 12px;
}

.join-card input {
    flex: 1;
    min-width: 0;
    padding: 11px 14px;
}

.error-message {
    color: var(--error);
    font-size: 14px;
    margin: 12px 0 0;
}

.session-chats {
    padding: 24px 0;
}

.session-chats h2 { margin: 0 0 8px; }
.session-chats .muted { color: var(--muted); margin: 0 0 24px; max-width: 60ch; }

.session-chats-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.session-chats-item {
    display: grid;
    grid-template-columns: auto 1fr auto auto;
    align-items: baseline;
    column-gap: 12px;
    padding: 12px 14px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    transition: border-color 0.15s ease;
}

.session-chats-item:hover { border-color: var(--muted); }
.session-chats-item.selected { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }

.session-chats-id {
    font-family: ui-monospace, "JetBrains Mono", Consolas, monospace;
    font-size: 13px;
    color: var(--muted);
}

.session-chats-link {
    color: var(--text);
    text-decoration: none;
}

.session-chats-link:hover { color: var(--accent); }

.session-chats-when {
    color: var(--muted);
    font-size: 12px;
}

.session-chats-remove {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--muted);
    padding: 5px 12px;
    border-radius: var(--radius-pill);
    font-size: 12px;
    cursor: pointer;
    transition: color 0.15s ease, border-color 0.15s ease;
}

.session-chats-remove:hover { color: var(--error); border-color: var(--error); }

.chat-room {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
}

.chat-room.drag-over::after {
    content: 'Drop file to attach';
    position: absolute;
    inset: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px dashed var(--accent);
    border-radius: 12px;
    background: color-mix(in srgb, var(--accent) 8%, var(--surface));
    color: var(--accent);
    font-weight: 600;
    pointer-events: none;
    z-index: 5;
}

.messages {
    flex: 1;
    overflow-y: auto;
    overscroll-behavior: contain;
    list-style: none;
    margin: 0;
    padding: 16px 0;
    display: flex;
    flex-direction: column;
    gap: 6px;
    scrollbar-width: thin;
    scrollbar-color: var(--border) transparent;
}

.messages::-webkit-scrollbar { width: 8px; }
.messages::-webkit-scrollbar-track { background: transparent; }
.messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: var(--radius-pill); }
.messages::-webkit-scrollbar-thumb:hover { background: var(--muted); }

.message {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: auto auto;
    column-gap: 12px;
    row-gap: 2px;
    align-items: baseline;
    padding: 8px 12px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    max-width: 70%;
    width: fit-content;
    align-self: flex-start;
    box-shadow: var(--shadow);
}

.message .author {
    grid-column: 1;
    grid-row: 1;
    color: var(--muted);
    font-size: 13px;
    font-weight: 600;
    overflow-wrap: anywhere;
}

.message .authorship {
    margin-left: 4px;
    font-weight: 700;
    cursor: help;
}
.message .authorship.verified { color: var(--online); }
.message .authorship.warn { color: var(--error); }

.message time {
    grid-column: 2;
    grid-row: 1;
    color: var(--muted);
    font-size: 12px;
    white-space: nowrap;
}

.message .body {
    grid-column: 1 / -1;
    grid-row: 2;
    color: var(--text);
    white-space: pre-wrap;
    word-break: break-word;
}

.message.mine {
    align-self: flex-end;
    background: var(--mine);
    border-color: transparent;
    color: var(--mine-text);
}

.message.mine .author { color: rgba(255, 255, 255, 0.8); }
.message.mine .body { color: var(--mine-text); }
.message.mine time { color: rgba(255, 255, 255, 0.7); }

.send {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: 12px;
    padding: 12px 24px;
    border-top: 1px solid var(--border);
    background: var(--surface);
}

.send:has(.attach[hidden]) {
    grid-template-columns: minmax(0, 1fr) auto;
}

.send input {
    padding: 11px 14px;
    min-width: 0;
}

.send .attach {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 10px;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 8px;
    cursor: pointer;
    color: var(--muted);
    transition: color 120ms ease, background-color 120ms ease;
}

.send .attach svg {
    width: 18px;
    height: 18px;
    display: block;
}

.send .attach:hover {
    color: var(--text);
    background: var(--surface-2, rgba(127, 127, 127, 0.1));
}
.send .attach:disabled { opacity: 0.5; cursor: default; }

.message .body.attachment {
    margin-top: 2px;
}

.message .attachment-image {
    display: block;
    max-width: min(320px, 100%);
    max-height: 320px;
    border-radius: 8px;
    cursor: zoom-in;
    user-select: none;
    -webkit-user-select: none;
    -webkit-user-drag: none;
    -webkit-touch-callout: none;
}

.lightbox {
    padding: 0;
    border: 0;
    background: transparent;
    max-width: 100vw;
    max-height: 100vh;
    cursor: zoom-out;
    color: var(--text);
}

.lightbox::backdrop {
    background: rgba(0, 0, 0, 0.85);
}

.lightbox img {
    display: block;
    max-width: 100vw;
    max-height: 100vh;
    border-radius: 4px;
    user-select: none;
    -webkit-user-drag: none;
}

.upload-progress {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 24px;
    border-top: 1px solid var(--border);
    background: var(--surface);
    font-size: 13px;
    color: var(--muted);
}

.upload-progress .bar {
    flex: 1;
    height: 6px;
    background: var(--surface-2);
    border-radius: 3px;
    overflow: hidden;
}

.upload-progress .bar > span {
    display: block;
    height: 100%;
    background: var(--accent);
    width: 0;
    transition: width 120ms ease;
}

.upload-progress .cancel {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--muted);
    padding: 3px 10px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
}

.upload-progress .cancel:hover { color: var(--text); border-color: var(--text); }

.join-hint {
    display: block;
    margin-top: 6px;
    color: var(--muted);
    font-size: 12px;
}

.message .attachment-file {
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: transparent;
    color: var(--text);
    cursor: pointer;
    font-size: 14px;
    text-align: left;
}

.message .attachment-file:hover { background: var(--surface-2, rgba(127, 127, 127, 0.1)); }
.message .attachment-file:disabled { opacity: 0.6; cursor: default; }

.typing {
    max-width: var(--container);
    width: 100%;
    margin: 0 auto;
    padding: 2px 24px 6px;
    color: var(--muted);
    font-size: 13px;
    font-style: italic;
}

.conn-status {
    padding: 6px 24px;
    font-size: 12px;
    border-top: 1px solid var(--border);
    background: var(--surface);
    color: var(--muted);
    text-align: center;
}

.conn-status[data-state="online"]     { color: var(--online); }
.conn-status[data-state="offline"]    { color: var(--muted); }
.conn-status[data-state="connecting"] { color: var(--muted); }
.conn-status[data-state="error"]      { color: var(--error); }

@media (max-width: 640px) {
    .chat-header {
        padding: 10px 14px;
        gap: 10px;
    }

    .chat-header .inner { gap: 10px; flex-wrap: wrap; }

    .chat-header .brand-link { display: none; }

    .chat-header .header-break {
        display: block;
        flex-basis: 100%;
        height: 0;
    }

    .chat-header .me {
        width: fit-content;
        max-width: 70%;
        margin-top: 2px;
    }

    .theme-toggle { min-height: 38px; padding: 6px 12px; }

    .chat-main > .inner { padding: 0 14px; }

    .join-card {
        margin: 24px auto;
        padding: 20px;
    }

    .join-card form { flex-direction: column; }

    .join-card .primary { width: 100%; }

    .message { max-width: 88%; }

    .send {
        padding: 10px 14px;
        gap: 10px;
    }

    .conn-status { padding: 6px 14px; }

    .typing { padding-left: 14px; padding-right: 14px; }

    .session-chats-item {
        grid-template-columns: 1fr auto;
        row-gap: 4px;
    }

    .session-chats-id { grid-column: 1; }
    .session-chats-remove { grid-column: 2; grid-row: 1 / span 2; align-self: center; }
    .session-chats-link { grid-column: 1 / -1; }
    .session-chats-when { grid-column: 1 / -1; }
}
