:root {
    color-scheme: light;
    --bg: #f4f7fb;
    --surface: #ffffff;
    --text: #172033;
    --muted: #667085;
    --line: #dce3ec;
    --blue: #2878ff;
    --blue-soft: #eaf2ff;
    --green: #20a66a;
    --green-soft: #e5f7ee;
    --danger: #d64545;
    --shadow: 0 14px 45px rgba(31, 45, 72, .1);
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
    margin: 0;
    overflow: hidden;
    background: var(--bg);
    color: var(--text);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    letter-spacing: 0;
}

button, input, textarea { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; }

.app {
    width: min(1120px, 100%);
    height: 100dvh;
    margin: auto;
    background: var(--surface);
    box-shadow: var(--shadow);
}

.join-view {
    height: 100%;
    display: grid;
    place-content: center;
    justify-items: center;
    padding: 24px;
    text-align: center;
}

.join-view[hidden], .chat-view[hidden] { display: none; }

.brand-mark {
    display: grid;
    width: 56px;
    height: 56px;
    place-items: center;
    border-radius: 8px;
    background: var(--blue);
    color: white;
    font-size: 28px;
    font-weight: 800;
}

.join-view h1 { margin: 20px 0 6px; font-size: 26px; }
.join-view p { margin: 0 0 28px; color: var(--muted); line-height: 1.6; }
.join-notes { display: grid; gap: 5px; margin-top: 14px; color: var(--muted); font-size: 12px; }

.join-form { display: grid; width: min(100%, 320px); gap: 12px; }

.join-form input {
    width: 100%;
    padding: 15px;
    border: 1px solid var(--line);
    border-radius: 8px;
    outline: none;
    text-align: center;
    color: var(--text);
    font-size: 28px;
    font-weight: 750;
    letter-spacing: .35em;
}

.join-form input:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(40, 120, 255, .14); }

.join-form button {
    padding: 14px;
    border: 0;
    border-radius: 8px;
    background: var(--blue);
    color: white;
    font-weight: 700;
}

.chat-view { display: grid; height: 100%; grid-template-rows: auto 1fr auto; }

.topbar {
    position: relative;
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: space-between;
    min-height: 176px;
    padding: 20px 28px;
    border-bottom: 1px solid var(--line);
    background: rgba(255, 255, 255, .96);
}

.identity { display: flex; min-width: 0; align-items: flex-start; flex-direction: column; }
.eyebrow { color: var(--muted); font-size: 13px; font-weight: 650; }
.tagline { margin-top: 4px; color: var(--muted); font-size: 13px; }
.session-note { margin-top: 3px; color: #8a94a3; font-size: 11px; }
.room-summary { display: flex; align-items: center; gap: 18px; margin-top: 10px; }
.room-line { font-size: 19px; white-space: nowrap; }
.room-line strong { margin-left: 8px; color: var(--blue); font-size: 48px; line-height: 1; }

.expiry-box {
    display: flex;
    min-width: 218px;
    height: 34px;
    align-items: center;
    gap: 5px;
    padding: 0 9px;
    border: 1px solid var(--line);
    border-radius: 7px;
    color: var(--muted);
    font-size: 12px;
    white-space: nowrap;
}
.expiry-box svg { width: 16px; fill: none; stroke: currentColor; stroke-linecap: round; stroke-linejoin: round; stroke-width: 1.8; }
.expiry-box strong { min-width: 42px; color: var(--text); font-variant-numeric: tabular-nums; text-align: center; }
.expiry-box.warning { border-color: #e7b85d; background: #fff9e8; color: #8a6218; }
.expiry-box.critical { border-color: #e79b9b; background: #fff3f3; color: var(--danger); }
.expiry-box.critical strong { color: var(--danger); }
.expiry-icon-button {
    display: grid;
    width: 25px;
    height: 25px;
    margin-left: 2px;
    place-items: center;
    border: 0;
    border-left: 1px solid var(--line);
    background: transparent;
    color: var(--muted);
}
.expiry-icon-button[hidden] { display: none; }
.expiry-icon-button:hover { color: var(--blue); }
.expiry-icon-button svg { width: 17px; }

.connection { display: flex; align-items: center; gap: 8px; margin-top: 10px; color: var(--muted); font-size: 13px; }
.status-separator { color: var(--line); }
.status-dot { width: 9px; height: 9px; border-radius: 50%; background: #9ca3af; }
.status-dot.online { background: var(--green); animation: pulse 1.5s infinite; }
.status-dot.offline { background: var(--danger); }

@keyframes pulse {
    50% { box-shadow: 0 0 0 6px rgba(32, 166, 106, 0); }
    0%, 100% { box-shadow: 0 0 0 0 rgba(32, 166, 106, .25); }
}

.qr-panel { display: flex; align-items: center; gap: 14px; margin-left: auto; margin-right: 46px; color: var(--muted); font-size: 13px; }
.qr-panel[hidden] { display: none; }
.qr-panel strong, .qr-panel span { display: block; }
.qr-panel strong { margin-bottom: 4px; color: var(--text); }
.qr-expiry { margin-top: 6px; color: var(--blue); font-variant-numeric: tabular-nums; }
#qrcode { padding: 6px; border: 1px solid var(--line); border-radius: 6px; background: white; }
#qrcode img, #qrcode canvas { display: block; width: 96px !important; height: 96px !important; }

.icon-button {
    position: absolute;
    top: 18px;
    right: 22px;
    display: grid;
    width: 38px;
    height: 38px;
    place-items: center;
    border: 1px solid var(--line);
    border-radius: 7px;
    background: transparent;
    color: var(--muted);
}
.icon-button:hover { border-color: #f1b4b4; color: var(--danger); background: #fff5f5; }
.icon-button[hidden] { display: none; }
.icon-button svg, .send-button svg { width: 20px; fill: none; stroke: currentColor; stroke-linecap: round; stroke-linejoin: round; stroke-width: 1.8; }

.messages {
    min-height: 0;
    overflow-y: auto;
    padding: 28px;
    scroll-behavior: smooth;
    background: #eef1f4;
}

.empty-state {
    display: flex;
    height: 100%;
    min-height: 220px;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    color: var(--muted);
    text-align: center;
}
.empty-state strong { margin: 14px 0 5px; color: var(--text); }
.empty-state.hidden { display: none; }
.waiting-dots { display: flex; gap: 5px; }
.waiting-dots i { width: 8px; height: 8px; border-radius: 50%; background: var(--blue); animation: bounce 1.2s infinite; }
.waiting-dots i:nth-child(2) { animation-delay: .15s; }
.waiting-dots i:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 50% { transform: translateY(-7px); opacity: .45; } }

.waiting-banner {
    position: sticky;
    top: 0;
    z-index: 1;
    width: fit-content;
    margin: 0 auto 18px;
    padding: 9px 13px;
    display: flex;
    align-items: center;
    gap: 10px;
    border: 1px solid var(--line);
    border-radius: 7px;
    background: var(--surface);
    color: var(--muted);
    box-shadow: 0 4px 14px rgba(31, 45, 72, .08);
    font-size: 13px;
}
.waiting-banner[hidden] { display: none; }
.waiting-banner .waiting-dots i { width: 6px; height: 6px; }

.pairing-banner {
    position: sticky;
    top: 0;
    z-index: 2;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
    width: min(620px, 100%);
    margin: 0 auto 20px;
    padding: 13px 15px;
    border: 1px solid #f1c56d;
    border-radius: 8px;
    background: #fff9e8;
    color: #765413;
    box-shadow: 0 4px 14px rgba(31, 45, 72, .08);
}
.pairing-banner[hidden] { display: none; }
.pairing-banner strong, .pairing-banner span { display: block; }
.pairing-banner span { margin-top: 3px; color: var(--muted); font-size: 12px; }
.pairing-actions { display: flex; flex: 0 0 auto; gap: 8px; }
.primary-button, .secondary-button { padding: 8px 12px; border-radius: 6px; font-weight: 650; }
.primary-button { border: 1px solid var(--blue); background: var(--blue); color: white; }
.secondary-button { border: 1px solid var(--line); background: var(--surface); color: var(--text); }

.session-expiry-banner {
    position: sticky;
    top: 0;
    z-index: 3;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    width: min(620px, 100%);
    margin: 0 auto 18px;
    padding: 11px 13px;
    border: 1px solid #e7b85d;
    border-radius: 8px;
    background: #fff9e8;
    color: #765413;
    box-shadow: 0 4px 14px rgba(31, 45, 72, .08);
}
.session-expiry-banner[hidden] { display: none; }
.session-expiry-banner strong, .session-expiry-banner span { display: block; }
.session-expiry-banner span { margin-top: 3px; color: var(--muted); font-size: 12px; }

.message-row { display: flex; margin: 0 0 16px; }
.message-row.self { justify-content: flex-end; }
.bubble {
    position: relative;
    max-width: min(72%, 680px);
    padding: 10px 38px 7px 13px;
    border-radius: 7px;
    color: #182230;
    box-shadow: 0 1px 3px rgba(31, 45, 72, .09);
    transition: opacity .18s, box-shadow .18s;
}
.message-row.other .bubble { border: 1px solid #e1e5ea; background: #ffffff; }
.message-row.self .bubble { background: #95ec69; }
.message-row.other .bubble::before, .message-row.self .bubble::before {
    position: absolute;
    top: 13px;
    width: 0;
    height: 0;
    border: 6px solid transparent;
    content: "";
}
.message-row.other .bubble::before { left: -12px; border-right-color: #ffffff; }
.message-row.self .bubble::before { right: -12px; border-left-color: #95ec69; }
.message-row.sending .bubble { opacity: .68; }
.message-row.failed .bubble { box-shadow: 0 0 0 1px var(--danger); }
.bubble-content { margin: 0; overflow-wrap: anywhere; white-space: pre-wrap; line-height: 1.55; }
.bubble-meta { display: flex; justify-content: flex-end; margin-top: 5px; color: #697386; font-size: 11px; }
.delivery-status { margin-left: 2px; }
.retry-button { padding: 0; border: 0; background: transparent; color: var(--danger); font-size: 11px; font-weight: 700; }

.copy-button {
    position: absolute;
    top: 7px;
    right: 8px;
    width: 22px;
    height: 22px;
    border: 0;
    border-radius: 4px;
    background: rgba(255, 255, 255, .55);
    opacity: 0;
    transition: opacity .15s;
}
.copy-button::before, .copy-button::after {
    position: absolute;
    width: 8px;
    height: 9px;
    border: 1.5px solid #667085;
    border-radius: 2px;
    content: "";
}
.copy-button::before { top: 5px; left: 5px; }
.copy-button::after { top: 8px; left: 8px; background: rgba(255, 255, 255, .8); }
.bubble:hover .copy-button, .copy-button:focus-visible { opacity: 1; }

.composer {
    position: relative;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 48px;
    gap: 10px;
    padding: 14px 18px calc(14px + env(safe-area-inset-bottom));
    border-top: 1px solid var(--line);
    background: var(--surface);
}

.composer textarea {
    width: 100%;
    min-width: 0;
    max-height: 130px;
    resize: none;
    padding: 12px 64px 12px 14px;
    border: 1px solid var(--line);
    border-radius: 8px;
    outline: none;
    color: var(--text);
    line-height: 1.5;
}
.composer textarea:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(40, 120, 255, .12); }
.composer textarea:disabled { background: var(--bg); color: var(--muted); }
.counter { position: absolute; right: 82px; bottom: calc(26px + env(safe-area-inset-bottom)); color: var(--muted); font-size: 11px; }

.send-button {
    display: grid;
    width: 48px;
    height: 48px;
    place-items: center;
    align-self: end;
    border: 0;
    border-radius: 8px;
    background: var(--blue);
    color: white;
}
.send-button:disabled { cursor: wait; opacity: .55; }

.toast {
    position: fixed;
    left: 50%;
    bottom: 88px;
    z-index: 10;
    max-width: calc(100% - 32px);
    padding: 9px 14px;
    border-radius: 7px;
    background: #172033;
    color: white;
    opacity: 0;
    pointer-events: none;
    transform: translate(-50%, 8px);
    transition: .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 700px) {
    .app { width: 100%; box-shadow: none; }
    .topbar { min-height: 106px; padding: 13px 58px 13px 16px; }
    .eyebrow, .tagline, .session-note, .qr-panel { display: none; }
    .room-summary { gap: 10px; margin-top: 0; }
    .room-line { font-size: 14px; }
    .room-line strong { font-size: 28px; }
    .expiry-box { min-width: 0; height: 30px; padding: 0 7px; }
    #expiry-label { display: none; }
    .connection { margin-top: 6px; }
    .icon-button { top: 16px; right: 14px; }
    .messages { padding: 18px 14px; }
    .pairing-banner { align-items: stretch; flex-direction: column; gap: 10px; }
    .session-expiry-banner { align-items: stretch; flex-direction: column; gap: 10px; }
    .pairing-actions { display: grid; grid-template-columns: 1fr 1fr; }
    .bubble { max-width: 86%; }
    .copy-button { opacity: 1; }
    .composer { padding-left: 10px; padding-right: 10px; }
    .counter { right: 72px; }
}

@media (prefers-color-scheme: dark) {
    :root {
        color-scheme: dark;
        --bg: #11151c;
        --surface: #1a202a;
        --text: #edf2f7;
        --muted: #a8b0bd;
        --line: #343c49;
        --blue-soft: #193252;
        --green-soft: #183c30;
        --shadow: none;
    }
    .topbar { background: rgba(26, 32, 42, .96); }
    .messages { background: #141922; }
    #qrcode { background: white; }
    .icon-button:hover { background: #382529; }
    .pairing-banner { border-color: #705e34; background: #2c281e; color: #f4d995; }
    .session-expiry-banner { border-color: #705e34; background: #2c281e; color: #f4d995; }
    .expiry-box.warning { border-color: #705e34; background: #2c281e; }
    .expiry-box.critical { border-color: #704044; background: #322125; }
    .message-row.other .bubble { border-color: #343c49; background: #252c36; color: var(--text); }
    .message-row.other .bubble::before { border-right-color: #252c36; }
    .message-row.self .bubble { background: #1f6b4f; color: #f4fff9; }
    .message-row.self .bubble::before { border-left-color: #1f6b4f; }
    .bubble-meta { color: #b7c0cc; }
}
