:root {
    color-scheme: light dark;
    --page: #e6e9ef;
    --bg: #f2f4f7;
    --surface: #ffffff;
    --surface-2: #eef0f4;
    --text: #16181d;
    --muted: #667085;
    --border: #e0e4ea;
    --accent: #4263eb;
    --accent-hover: #3654d4;
    --on-accent: #ffffff;
    --theirs: #ffffff;
    --danger: #d92d20;
    --online: #2f9e44;
    --whatsapp: #0f7f48;
    --shadow: 0 1px 1.5px rgba(16, 24, 40, 0.08);
}

@media (prefers-color-scheme: dark) {
    :root {
        --page: #090b0e;
        --bg: #0f1216;
        --surface: #171a20;
        --surface-2: #20242c;
        --text: #e8eaef;
        --muted: #98a0ae;
        --border: #282d37;
        --accent-hover: #5672f0;
        --theirs: #20242c;
        --danger: #f97066;
        --online: #40c057;
        --shadow: none;
    }
}

* {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

html,
body {
    height: 100%;
    margin: 0;
    overflow: hidden;
    overscroll-behavior: none;
}

/* Both screens are pinned to the part of the page that is actually visible.
   app.js keeps the two variables in step with the on-screen keyboard: on iOS
   the keyboard does not resize the page, it shortens the visible area and
   slides it down, so a height alone would leave the composer stranded. */
.login,
.app {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--vv-height, 100dvh);
    transform: translateY(var(--vv-top, 0px));
}

body {
    font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
    background: var(--page);
    color: var(--text);
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
}

button {
    font: inherit;
    color: inherit;
    cursor: pointer;
}

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

/* ---- login ---- */

.login {
    display: grid;
    place-items: center;
    padding: 24px 16px;
    overflow-y: auto;
}

.login-card {
    width: 100%;
    max-width: 340px;
    display: grid;
    gap: 12px;
    padding: 32px 26px 28px;
    text-align: center;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 22px;
    box-shadow: 0 12px 32px rgba(16, 24, 40, 0.08);
}

.login-mark {
    margin: 0 auto 4px;
}

.login-card h1 {
    margin: 0;
    font-size: 22px;
    letter-spacing: -0.01em;
}

.login-hint {
    margin: 0 0 8px;
    color: var(--muted);
    font-size: 15px;
}

.login-card input {
    width: 100%;
    padding: 12px 14px;
    font: inherit;
    color: var(--text);
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 12px;
    text-align: center;
}

.login-card input:focus {
    outline: none;
    border-color: var(--accent);
}

.primary-btn {
    padding: 12px 14px;
    font-weight: 600;
    color: var(--on-accent);
    background: var(--accent);
    border: 0;
    border-radius: 12px;
}

.primary-btn:hover {
    background: var(--accent-hover);
}

.primary-btn:disabled {
    opacity: 0.6;
    cursor: default;
}

.login-error {
    margin: 2px 0 0;
    color: var(--danger);
    font-size: 14px;
}

/* ---- app shell ---- */

.app {
    max-width: 780px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    background: var(--bg);
}

@media (min-width: 781px) {
    .app {
        border-inline: 1px solid var(--border);
    }
}

.topbar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 10px 10px 14px;
    padding-top: max(10px, env(safe-area-inset-top));
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    /* Dragging the header or composer must not drag the page around under the keyboard. */
    touch-action: none;
}

.avatar {
    position: relative;
    flex: none;
    width: 40px;
    height: 40px;
    display: grid;
    place-items: center;
    font-weight: 600;
    color: var(--on-accent);
    background: var(--accent);
    border-radius: 50%;
}

.avatar.online::after {
    content: "";
    position: absolute;
    right: -1px;
    bottom: -1px;
    width: 12px;
    height: 12px;
    background: var(--online);
    border: 2px solid var(--surface);
    border-radius: 50%;
}

.peer {
    flex: 1;
    min-width: 0;
}

.peer-name {
    font-weight: 600;
    line-height: 1.25;
}

.peer-status {
    min-height: 18px;
    font-size: 13px;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

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

.icon-btn {
    flex: none;
    width: 40px;
    height: 40px;
    display: grid;
    place-items: center;
    color: var(--muted);
    background: transparent;
    border: 0;
    border-radius: 50%;
}

.pill-btn {
    flex: none;
    display: inline-flex;
    align-items: center;
    gap: 7px;
    height: 36px;
    padding: 0 13px;
    font-size: 14px;
    font-weight: 500;
    color: var(--text);
    background: var(--surface-2);
    border: 0;
    border-radius: 999px;
}

@media (hover: hover) {

    .icon-btn:hover,
    .pill-btn:hover {
        color: var(--text);
        background: var(--border);
    }
}

.notice {
    padding: 6px 12px;
    font-size: 13px;
    text-align: center;
    color: #5f4300;
    background: #ffe9a8;
}

/* ---- messages ---- */

.stage {
    position: relative;
    flex: 1;
    min-height: 0;
    display: flex;
}

.messages {
    flex: 1;
    overflow-y: auto;
    padding: 6px 12px 10px;
    overscroll-behavior: contain;
    /* Scroll position is restored by hand when older messages are prepended. */
    overflow-anchor: none;
}

.list {
    display: flex;
    flex-direction: column;
}

.empty {
    margin-top: 28vh;
    text-align: center;
    color: var(--muted);
}

.day {
    align-self: center;
    margin: 14px 0 8px;
    padding: 3px 11px;
    font-size: 12px;
    color: var(--muted);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 999px;
}

.msg {
    display: flex;
    margin-top: 8px;
}

.msg.mine {
    justify-content: flex-end;
}

.msg.mine+.msg.mine,
.msg.theirs+.msg.theirs {
    margin-top: 3px;
}

.bubble {
    max-width: min(78%, 520px);
    padding: 7px 12px 6px;
    color: var(--text);
    background: var(--theirs);
    border-radius: 18px;
    border-bottom-left-radius: 6px;
    box-shadow: var(--shadow);
}

.msg.mine .bubble {
    color: var(--on-accent);
    background: var(--accent);
    border-radius: 18px;
    border-bottom-right-radius: 6px;
    box-shadow: none;
}

.bubble.has-photo {
    width: min(78%, 300px);
    padding: 4px 4px 6px;
}

.text {
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

.has-photo .text {
    padding: 4px 8px 0;
}

.text a {
    color: inherit;
    text-decoration: underline;
}

.meta {
    margin-top: 2px;
    font-size: 11px;
    text-align: right;
    opacity: 0.65;
}

.has-photo .meta {
    padding: 0 8px;
}

.photo {
    display: block;
    width: 100%;
    padding: 0;
    overflow: hidden;
    background: var(--surface-2);
    border: 0;
    border-radius: 14px;
}

.photo img {
    display: block;
    width: 100%;
    height: auto;
    max-height: 380px;
    object-fit: cover;
}

.photo.unsized img {
    min-height: 140px;
}

.msg.pending .bubble {
    opacity: 0.6;
}

.msg.failed .bubble {
    cursor: pointer;
    color: #ffffff;
    background: var(--danger);
}

.msg.failed .meta {
    opacity: 1;
}

/* ---- floating bits over the message area ---- */

.floating {
    position: absolute;
    left: 50%;
    z-index: 2;
    transform: translateX(-50%);
    padding: 6px 14px;
    font-size: 13px;
    white-space: nowrap;
    border-radius: 999px;
    box-shadow: 0 4px 14px rgba(16, 24, 40, 0.18);
}

.history-loader {
    top: 10px;
    color: var(--muted);
    background: var(--surface);
    border: 1px solid var(--border);
}

.jump {
    bottom: 12px;
    font-weight: 500;
    color: var(--on-accent);
    background: var(--accent);
    border: 0;
}

.toast {
    bottom: 12px;
    max-width: calc(100% - 24px);
    white-space: normal;
    text-align: center;
    color: #ffffff;
    background: #1d2129;
    border-radius: 12px;
}

/* ---- composer ---- */

.composer {
    padding: 8px 8px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    background: var(--surface);
    border-top: 1px solid var(--border);
    touch-action: none;
}

.composer-row {
    display: flex;
    align-items: flex-end;
    gap: 4px;
}

.composer textarea {
    flex: 1;
    min-width: 0;
    max-height: 140px;
    padding: 9px 14px;
    font: inherit;
    line-height: 1.35;
    color: var(--text);
    background: var(--surface-2);
    border: 1px solid transparent;
    border-radius: 20px;
    resize: none;
}

.composer textarea:focus {
    outline: none;
    border-color: var(--accent);
}

.send-btn {
    flex: none;
    width: 40px;
    height: 40px;
    margin-left: 4px;
    display: grid;
    place-items: center;
    color: var(--on-accent);
    background: var(--accent);
    border: 0;
    border-radius: 50%;
}

.send-btn:hover {
    background: var(--accent-hover);
}

.attachment {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 8px;
    padding: 6px 2px 6px 6px;
    background: var(--surface-2);
    border-radius: 14px;
}

.attachment img {
    width: 52px;
    height: 52px;
    object-fit: cover;
    border-radius: 10px;
}

.attachment-label {
    flex: 1;
    font-size: 14px;
    color: var(--muted);
}

.app.dragging .stage {
    outline: 2px dashed var(--accent);
    outline-offset: -8px;
}

/* ---- dialogs ---- */

dialog {
    padding: 0;
    color: var(--text);
    border: 0;
}

dialog::backdrop {
    background: rgba(8, 10, 14, 0.55);
}

.sheet {
    width: min(420px, calc(100vw - 24px));
    max-height: min(560px, calc(100dvh - 48px));
    background: var(--surface);
    border-radius: 20px;
    box-shadow: 0 24px 60px rgba(0, 0, 0, 0.3);
}

.sheet-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 10px 12px 18px;
    border-bottom: 1px solid var(--border);
}

.sheet-head h2 {
    margin: 0;
    font-size: 17px;
}

.contacts-list {
    padding: 4px 0;
}

.contacts-note {
    margin: 0;
    padding: 22px 18px;
    text-align: center;
    color: var(--muted);
}

.contact {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px 12px 18px;
}

.contact+.contact {
    border-top: 1px solid var(--border);
}

.contact-info {
    flex: 1;
    min-width: 0;
}

.contact-name {
    font-weight: 600;
}

.contact-number {
    font-size: 14px;
    color: var(--muted);
    font-variant-numeric: tabular-nums;
}

.wa-btn {
    flex: none;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 13px;
    font-size: 14px;
    font-weight: 600;
    color: #ffffff;
    text-decoration: none;
    background: var(--whatsapp);
    border-radius: 999px;
}

.lightbox {
    width: 100vw;
    max-width: 100vw;
    height: 100dvh;
    max-height: 100dvh;
    background: rgba(0, 0, 0, 0.92);
    cursor: zoom-out;
}

.lightbox[open] {
    display: grid;
    place-items: center;
}

.lightbox img {
    max-width: 100vw;
    max-height: 100dvh;
    object-fit: contain;
}
