:root {
    --brand: #0e1015;
    --accent: #28b0ff;
    --on-accent: #0e1015;
    --link: #0b6fae;
    --bg: #ffffff;
    --bg-soft: #f1f5f9;
    --border: #e2e8f0;
    --text: #0e1015;
    --muted: #64748b;
    --bubble-them: #ffffff;
    --bubble-me: #28b0ff;
    --danger: #dc2626;
    --ok: #22c55e;
    color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0b1120;
        --bg-soft: #111a2e;
        --border: #1e293b;
        --text: #e2e8f0;
        --muted: #94a3b8;
        --bubble-them: #1e293b;
    }
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    overscroll-behavior: none;
}

button {
    font: inherit;
    cursor: pointer;
}

[hidden] {
    display: none !important;
}

.screen {
    /* --app-h is the visible height above the iPhone keyboard (set in app.js) */
    height: var(--app-h, 100dvh);
}
html,
body {
    overflow: hidden;
}

/* Login */
#login {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    background: var(--brand);
}
.login-card {
    width: 100%;
    max-width: 340px;
    background: var(--bg);
    border-radius: 18px;
    padding: 28px 24px;
    text-align: center;
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.login-card img {
    margin: 0 auto;
    border-radius: 14px;
}
.login-card h1 {
    margin: 0;
    font-size: 22px;
}
.login-card p {
    margin: 0 0 8px;
    color: var(--muted);
}
input,
textarea {
    font: inherit;
    color: var(--text);
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 12px;
    font-size: 16px;
    width: 100%;
}
.login-card button,
dialog button {
    background: var(--accent);
    color: var(--on-accent);
    border: none;
    border-radius: 10px;
    padding: 12px 16px;
    font-weight: 600;
}
.error {
    color: var(--danger);
    font-size: 14px;
    min-height: 1em;
}

/* Layout */
#app {
    display: flex;
}
#sidebar {
    width: 360px;
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    min-height: 0;
}
#thread {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    background: var(--bg-soft);
}
#thread-view {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
}

.bar {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    padding-top: calc(12px + env(safe-area-inset-top));
    border-bottom: 1px solid var(--border);
    background: var(--bg);
}
.bar h1 {
    margin: 0;
    font-size: 22px;
    flex: 1;
}
.icon-btn {
    background: none;
    border: none;
    color: var(--text);
    padding: 6px;
    border-radius: 8px;
    display: flex;
}
.icon-btn svg {
    width: 22px;
    height: 22px;
}
.icon-btn.danger {
    color: var(--danger);
}
.conn {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--muted);
}
.conn.on {
    background: var(--ok);
}

.banner {
    margin: 10px 12px 0;
    padding: 12px;
    border-radius: 12px;
    background: #fef3c7;
    color: #78350f;
    font-size: 14px;
    line-height: 1.4;
}
.banner button {
    margin-top: 8px;
    background: #78350f;
    color: #fff;
    border: none;
    border-radius: 8px;
    padding: 8px 12px;
    font-weight: 600;
}

.tabs {
    display: flex;
    gap: 6px;
    padding: 10px 12px;
}
.tabs button {
    flex: 1;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--muted);
    border-radius: 999px;
    padding: 7px;
    font-size: 14px;
}
.tabs button.active {
    background: var(--text);
    color: var(--bg);
    border-color: var(--text);
}

#list {
    list-style: none;
    margin: 0;
    padding: 0;
    overflow-y: auto;
    flex: 1;
}
#list li {
    display: flex;
    gap: 12px;
    padding: 12px 14px;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
    align-items: center;
}
#list li.active {
    background: var(--bg-soft);
}
.av {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--accent);
    color: var(--on-accent);
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    flex: none;
    position: relative;
}
.av .dot {
    position: absolute;
    bottom: 0;
    right: 0;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: var(--ok);
    border: 2px solid var(--bg);
}
.li-body {
    flex: 1;
    min-width: 0;
}
.li-top {
    display: flex;
    justify-content: space-between;
    gap: 8px;
}
.li-name {
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.li-time {
    font-size: 12px;
    color: var(--muted);
    flex: none;
}
.li-last {
    font-size: 14px;
    color: var(--muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    margin-top: 2px;
}
li.unread .li-name,
li.unread .li-last {
    color: var(--text);
    font-weight: 700;
}
.unread-pill {
    background: var(--accent);
    color: var(--on-accent);
    font-size: 12px;
    font-weight: 700;
    border-radius: 999px;
    padding: 1px 7px;
    margin-left: 6px;
}

.empty {
    margin: auto;
    padding: 32px;
    text-align: center;
    color: var(--muted);
    line-height: 1.5;
}

/* Thread */
.thread-bar {
    padding-left: 8px;
}
.who {
    flex: 1;
    min-width: 0;
}
.who .name {
    font-weight: 700;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.who .meta {
    font-size: 12.5px;
    color: var(--muted);
}
.pill-btn {
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text);
    border-radius: 999px;
    padding: 6px 12px;
    font-size: 13px;
}
.info {
    font-size: 12.5px;
    color: var(--muted);
    padding: 8px 14px;
    border-bottom: 1px solid var(--border);
    background: var(--bg);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.info a {
    color: var(--accent);
}
#messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-height: 0;
}
.msg {
    max-width: 78%;
    padding: 9px 13px;
    border-radius: 18px;
    font-size: 15.5px;
    line-height: 1.4;
    white-space: pre-wrap;
    word-wrap: break-word;
}
.msg a {
    color: inherit;
}
.msg.visitor {
    align-self: flex-start;
    background: var(--bubble-them);
    border: 1px solid var(--border);
    border-bottom-left-radius: 5px;
}
.msg.admin {
    align-self: flex-end;
    background: var(--bubble-me);
    color: var(--on-accent);
    border-bottom-right-radius: 5px;
}
.stamp {
    align-self: center;
    font-size: 11.5px;
    color: var(--muted);
    margin: 8px 0 2px;
}
.typing {
    font-size: 13px;
    color: var(--muted);
    padding: 0 16px 6px;
    font-style: italic;
}
.quick {
    display: flex;
    gap: 6px;
    overflow-x: auto;
    padding: 0 12px 8px;
    scrollbar-width: none;
}
.quick button {
    flex: none;
    border: 1px solid var(--border);
    background: var(--bg);
    color: var(--text);
    border-radius: 999px;
    padding: 6px 12px;
    font-size: 13px;
    max-width: 240px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
#composer {
    display: flex;
    gap: 8px;
    align-items: flex-end;
    padding: 10px 12px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
    background: var(--bg);
    border-top: 1px solid var(--border);
}
#composer textarea {
    resize: none;
    border-radius: 20px;
    padding: 10px 14px;
    max-height: 140px;
}
#composer #send {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    border: none;
    background: var(--accent);
    color: var(--on-accent);
    display: flex;
    align-items: center;
    justify-content: center;
    flex: none;
}
#composer #send svg,
#composer .tool svg {
    width: 19px;
    height: 19px;
}

/* Settings */
dialog {
    border: none;
    border-radius: 16px;
    padding: 20px;
    width: min(520px, calc(100vw - 24px));
    background: var(--bg);
    color: var(--text);
}
dialog::backdrop {
    background: rgba(0, 0, 0, 0.5);
}
dialog h2 {
    margin: 0 0 8px;
}
dialog h3 {
    margin: 18px 0 6px;
    font-size: 15px;
}
dialog .row {
    display: flex;
    gap: 8px;
    margin-top: 10px;
    justify-content: flex-end;
    flex-wrap: wrap;
}
dialog button.secondary {
    background: var(--bg-soft);
    color: var(--text);
}
.muted {
    color: var(--muted);
    font-size: 14px;
    margin: 0 0 6px;
}
pre {
    background: var(--bg-soft);
    padding: 10px;
    border-radius: 8px;
    font-size: 12.5px;
    white-space: pre-wrap;
    word-break: break-all;
    user-select: all;
}

/* Phone layout: one pane at a time */
@media (max-width: 760px) {
    #sidebar {
        width: 100%;
        border-right: none;
    }
    #thread {
        display: none;
    }
    #app.in-thread #sidebar {
        display: none;
    }
    #app.in-thread #thread {
        display: flex;
    }
}
@media (min-width: 761px) {
    #back {
        display: none;
    }
}

#composer .tool {
    width: 40px;
    height: 42px;
    border: none;
    background: transparent;
    color: var(--muted);
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex: none;
}
#composer .tool:disabled {
    opacity: 0.4;
}
.info a,
.ml {
    color: var(--link);
}
.ml {
    font-size: 12.5px;
    padding: 6px 14px;
    border-bottom: 1px solid var(--border);
    background: var(--bg);
}
.ml.ok {
    color: #1d6b3a;
}
.ml.bad {
    color: var(--danger);
}
.msg img {
    display: block;
    max-width: 100%;
    max-height: 260px;
    border-radius: 10px;
}
.msg .auto {
    display: block;
    font-size: 11.5px;
    opacity: 0.75;
    margin-top: 4px;
}
@media (prefers-color-scheme: dark) {
    :root {
        --link: #7cc8ff;
    }
}

.typing-bubble {
    display: flex;
    gap: 4px;
    align-items: center;
    padding: 13px 15px;
}
.typing-bubble span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--muted);
    animation: dmg-bounce 1.2s infinite;
}
.typing-bubble span:nth-child(2) {
    animation-delay: 0.2s;
}
.typing-bubble span:nth-child(3) {
    animation-delay: 0.4s;
}
@keyframes dmg-bounce {
    0%, 60%, 100% {
        transform: none;
        opacity: 0.45;
    }
    30% {
        transform: translateY(-4px);
        opacity: 1;
    }
}
.li-last.is-typing {
    color: var(--link);
    font-style: italic;
}
@media (prefers-reduced-motion: reduce) {
    .typing-bubble span {
        animation: none;
    }
}

/* Short chats sit at the bottom, next to the reply box, like iMessage */
#messages > :first-child {
    margin-top: auto;
}

/* AI suggestions, refreshed after each visitor message */
.quick button.ai {
    border-color: var(--accent);
    color: var(--link);
    background: color-mix(in srgb, var(--accent) 10%, var(--bg));
    max-width: 270px;
    white-space: normal;
    text-align: left;
    line-height: 1.35;
    border-radius: 14px;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
}
.quick button.ai.pending {
    opacity: 0.7;
    font-style: italic;
}

/* Visitor location */
.li-loc {
    font-size: 12px;
    color: var(--muted);
    margin-top: 1px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.info .where {
    color: var(--text);
    font-weight: 600;
}
