:root {
    --bg: #f3f4f6;
    --card: #ffffff;
    --ink: #111827;
    --muted: #6b7280;
    --line: #e5e7eb;
    --brand: #111827;
    --incoming: #ffffff;
    --outgoing: #dbeafe;
    --ok: #dcfce7;
}

* { box-sizing: border-box; }
html { height: 100%; }
body {
    margin: 0;
    min-height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
    background: var(--bg);
    color: var(--ink);
    -webkit-font-smoothing: antialiased;
}
a { color: inherit; }

.topbar {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 14px 18px;
    padding-top: max(14px, env(safe-area-inset-top));
    background: var(--brand);
    color: white;
    box-shadow: 0 1px 8px rgba(0,0,0,.18);
}
.topbar h1 {
    margin: 0;
    font-size: 20px;
    line-height: 1.2;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.topbar a {
    color: white;
    text-decoration: none;
    opacity: .95;
    font-size: 14px;
    white-space: nowrap;
}
.topbar .back-link::before { content: "‹ "; font-size: 20px; line-height: 0; }
.thread-topbar { align-items: flex-start; }
.inline-form { margin-top: 4px; }
.link-button {
    appearance: none;
    border: 0;
    background: transparent;
    color: rgba(255,255,255,.9);
    padding: 0;
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    text-decoration: underline;
}

.layout {
    width: 100%;
    max-width: 980px;
    margin: 18px auto;
    padding: 0 14px 24px;
}
.panel {
    background: var(--card);
    border-radius: 16px;
    padding: 16px;
    margin-bottom: 14px;
    box-shadow: 0 1px 8px rgba(0,0,0,.07);
}
.panel h2 { margin: 0 0 12px; font-size: 18px; }
.panel-title-row { display:flex; align-items:center; justify-content:space-between; gap:12px; }
.small-note { font-size: 12px; }

.new-form { display: flex; gap: 10px; }
input, textarea {
    width: 100%;
    padding: 13px 14px;
    border: 1px solid #d1d5db;
    border-radius: 12px;
    font-size: 16px;
    background: #fff;
}
textarea { font-family: inherit; }
button {
    padding: 13px 18px;
    border: 0;
    border-radius: 12px;
    background: var(--brand);
    color: white;
    font-size: 16px;
    font-weight: 650;
    cursor: pointer;
    touch-action: manipulation;
}
button:active { transform: scale(.99); }

.conversation-list { display: grid; gap: 10px; }
.conversation-row {
    display: block;
    text-decoration: none;
    border: 1px solid var(--line);
    border-radius: 16px;
    padding: 14px;
    background: #fff;
    min-height: 92px;
}
.conversation-row:hover { background: #f9fafb; }
.conversation-row.unread {
    border-color: var(--brand);
    background: #f8fafc;
    box-shadow: 0 1px 7px rgba(17,24,39,.17);
}
.row-main { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.row-main strong { font-size: 17px; }
.row-badges { display:flex; align-items:center; gap:6px; flex-shrink: 0; }
.badge, .unread-pill { font-size: 11px; border-radius: 999px; padding: 4px 8px; white-space: nowrap; font-weight: 700; }
.badge.in { background: var(--ok); color: #166534; }
.badge.out { background: var(--outgoing); color: #1d4ed8; }
.unread-pill { background: var(--brand); color: white; }
.preview {
    margin-top: 8px;
    color: #374151;
    line-height: 1.35;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.unread .preview, .unread strong { font-weight: 750; }
.date, .muted, small { color: var(--muted); }
.date { font-size: 12px; margin-top: 7px; }

.thread-page { background: #eef2f7; }
.thread-wrap {
    max-width: 880px;
    margin: 0 auto;
    min-height: calc(100vh - 58px);
    display: flex;
    flex-direction: column;
}
.thread {
    flex: 1;
    padding: 18px 14px 130px;
}
.message { display: flex; margin: 9px 0; }
.message.inbound { justify-content: flex-start; }
.message.outbound { justify-content: flex-end; }
.bubble {
    max-width: min(76%, 620px);
    padding: 10px 13px 8px;
    border-radius: 19px;
    box-shadow: 0 1px 3px rgba(0,0,0,.09);
    overflow-wrap: anywhere;
}
.inbound .bubble { background: var(--incoming); border-bottom-left-radius: 6px; }
.outbound .bubble { background: var(--outgoing); border-bottom-right-radius: 6px; }
.bubble p { margin: 0 0 7px; white-space: pre-wrap; line-height: 1.35; }
.bubble small { display: block; font-size: 11px; text-align: right; }
.message-meta {
    display: flex !important;
    justify-content: flex-end;
    align-items: center;
    gap: 6px;
    min-height: 14px;
}
.status-icon {
    font-weight: 800;
    line-height: 1;
    letter-spacing: -1px;
    opacity: .95;
}
.status-icon.sent, .status-icon.delivered { color: #2563eb; }
.status-icon.failed { color: #dc2626; }
.delivery-status { opacity: .9; }

.send-box {
    position: fixed;
    left: 50%;
    bottom: 0;
    transform: translateX(-50%);
    width: 100%;
    max-width: 880px;
    display: flex;
    align-items: flex-end;
    gap: 9px;
    padding: 10px 12px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    background: rgba(255,255,255,.96);
    border-top: 1px solid var(--line);
    box-shadow: 0 -2px 12px rgba(0,0,0,.08);
    backdrop-filter: blur(10px);
}
.send-box textarea {
    min-height: 48px;
    max-height: 140px;
    resize: none;
    border-radius: 18px;
}
.send-box button {
    min-width: 82px;
    border-radius: 18px;
}

.login-body { min-height: 100vh; display: grid; place-items: center; padding: 18px; }
.login-card { width: 100%; max-width: 420px; background: white; padding: 28px; border-radius: 18px; box-shadow: 0 2px 12px rgba(0,0,0,.12); }
.login-card h1 { margin-top: 0; }
.login-card label { display: block; margin: 14px 0 6px; }
.login-card button { width: 100%; margin-top: 16px; }
.error { color: #b91c1c; }

@media (max-width: 640px) {
    body { background: #fff; }
    .topbar { padding-left: 14px; padding-right: 14px; }
    .topbar h1 { font-size: 18px; }
    .layout { margin: 0; padding: 12px 10px 20px; }
    .panel { border-radius: 14px; box-shadow: none; border: 1px solid var(--line); padding: 13px; }
    .panel-title-row { display:block; }
    .small-note { display: block; margin-top: -4px; margin-bottom: 8px; }
    .new-form { flex-direction: column; }
    .new-form button { width: 100%; }
    .conversation-list { gap: 8px; }
    .conversation-row { border-radius: 14px; min-height: 86px; padding: 13px; }
    .row-main strong { font-size: 16px; }
    .badge.out, .badge.in { display: none; }
    .thread-page { background: #eef2f7; }
    .thread { padding: 12px 8px 112px; }
    .bubble { max-width: 86%; font-size: 16px; }
    .send-box { gap: 7px; padding-left: 8px; padding-right: 8px; }
    .send-box button { min-width: 70px; padding-left: 14px; padding-right: 14px; }
}

.media-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 8px;
    max-width: 100%;
}

.media-thumb-link {
    display: inline-block;
    text-decoration: none;
    max-width: 100%;
}

.media-thumb,
.bubble img.media-thumb {
    width: 180px;
    height: 180px;
    max-width: 100%;
    border-radius: 14px;
    display: block;
    object-fit: cover;
}

.bubble img {
    max-width: 100%;
    height: auto;
}

.media-fallback {
    display: none;
    font-size: 13px;
    text-decoration: underline;
}

@media (max-width: 640px) {
    .media-thumb,
    .bubble img.media-thumb {
        width: 150px;
        height: 150px;
        max-width: 58vw;
    }
}


.attach-button {
    flex: 0 0 auto;
    width: 46px;
    height: 46px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 999px;
    background: #eef2ff;
    color: var(--brand);
    border: 1px solid #d1d5db;
    font-size: 26px;
    font-weight: 700;
    cursor: pointer;
    user-select: none;
    margin-bottom: 1px;
}
.attach-button input {
    display: none;
}
.attach-button:hover { background: #e0e7ff; }
.attach-button:active { transform: scale(.98); }

@media (max-width: 640px) {
    .attach-button {
        width: 42px;
        height: 42px;
        font-size: 23px;
    }
}

/* Phase 11 MMS attachment preview */
.send-box {
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.composer-row {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
}
.attachment-preview {
    display: none;
    align-items: center;
    gap: 10px;
    max-width: 100%;
    background: #f8fafc;
    border: 1px solid #d5dbe5;
    border-radius: 14px;
    padding: 8px 10px;
}
.attachment-preview.show {
    display: flex;
}
.attachment-preview img {
    width: 64px;
    height: 64px;
    object-fit: cover;
    border-radius: 10px;
    border: 1px solid #d5dbe5;
    flex: 0 0 auto;
}
.attachment-name {
    font-size: 13px;
    color: #334155;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
}
.attachment-clear {
    border: none;
    background: #e2e8f0;
    color: #0f172a;
    width: 28px;
    height: 28px;
    border-radius: 999px;
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
}
.app-alert {
    max-width: 900px;
    margin: 10px auto;
    padding: 10px 14px;
    border-radius: 10px;
    background: #fee2e2;
    color: #991b1b;
    border: 1px solid #fecaca;
}
@media (max-width: 700px) {
    .attachment-preview img {
        width: 52px;
        height: 52px;
    }
    .composer-row {
        gap: 6px;
    }
}


/* Phase 12 composer and MMS preview fixes */
.thread {
    padding-bottom: 150px;
}

.media-grid {
    align-items: flex-start;
}

.media-thumb,
.bubble img.media-thumb {
    width: 180px;
    height: 180px;
    max-width: min(100%, 220px);
    object-fit: cover;
}

.attachment-preview {
    display: none !important;
    width: 100%;
}
.attachment-preview.has-attachment {
    display: flex !important;
}
.attachment-preview img:not([src]),
.attachment-preview img[src=""] {
    display: none !important;
}
.attachment-preview img {
    display: none;
}
.attachment-preview.has-attachment img[src] {
    display: block;
}

@media (max-width: 640px) {
    .thread {
        padding-bottom: 150px;
    }
    .media-thumb,
    .bubble img.media-thumb {
        width: 150px;
        height: 150px;
        max-width: 56vw;
    }
    .send-box textarea {
        min-height: 50px;
    }
}

/* Phase 14: stable MMS composer and non-destructive new message notice */
.new-message-banner {
    display: none;
    position: sticky;
    top: 70px;
    z-index: 19;
    margin: 0 auto;
    max-width: 880px;
    padding: 10px 14px;
    background: #fff7ed;
    color: #7c2d12;
    border: 1px solid #fed7aa;
    border-radius: 0 0 12px 12px;
    box-shadow: 0 2px 10px rgba(0,0,0,.08);
    text-align: center;
    font-weight: 600;
}
.new-message-banner.show {
    display: block;
}
.new-message-banner a {
    color: #1d4ed8;
    text-decoration: underline;
}

.media-thumb,
.bubble img.media-thumb {
    width: 150px;
    height: 150px;
    max-width: 46vw;
    object-fit: cover;
    border-radius: 12px;
}
.media-thumb-link {
    width: auto;
    max-width: 100%;
}

.send-box button:disabled {
    opacity: .65;
    cursor: not-allowed;
}

@media (max-width: 640px) {
    .new-message-banner {
        top: 64px;
        border-radius: 0;
        font-size: 14px;
    }
    .media-thumb,
    .bubble img.media-thumb {
        width: 132px;
        height: 132px;
        max-width: 52vw;
    }
}
