:root {
    --color-bg: #f0f2f5;
    --color-surface: #ffffff;
    --color-primary: #008069;
    --color-primary-dark: #005c4b;
    --color-read-tick: #53bdeb;
    --color-chat-bg: #efeae2;
    --color-text: #1b1f23;
    --color-muted: #667781;
    --color-border: #e2e5e9;
    --color-error: #b42318;
    --color-error-bg: #fef3f2;
    --color-info-bg: #eef6ff;
    --color-info-text: #1e4e8c;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: Segoe UI, Helvetica Neue, Helvetica, Arial, sans-serif;
    background: var(--color-bg);
    color: var(--color-text);
}

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.75rem 1.5rem;
    background: var(--color-primary-dark);
}

.topbar .brand {
    color: #fff;
    font-weight: 600;
    text-decoration: none;
}

.topbar-nav {
    display: flex;
    align-items: center;
    gap: 1.25rem;
}

.topbar-nav a {
    color: #d9f2ee;
    text-decoration: none;
    font-size: 0.9rem;
}

.container {
    max-width: 1100px;
    margin: 0 auto;
    padding: 1.5rem;
}

h1 {
    font-size: 1.4rem;
}

.login-box {
    max-width: 320px;
    margin: 3rem auto 0;
    background: var(--color-surface);
    padding: 1.5rem;
    border-radius: 8px;
    border: 1px solid var(--color-border);
}

form label {
    display: block;
    margin-top: 0.75rem;
    font-size: 0.85rem;
    color: var(--color-muted);
}

form input,
form textarea {
    width: 100%;
    padding: 0.5rem;
    margin-top: 0.25rem;
    border: 1px solid var(--color-border);
    border-radius: 6px;
    font-size: 1rem;
}

form textarea {
    min-height: 4rem;
    resize: vertical;
}

form button {
    margin-top: 1rem;
    width: 100%;
    padding: 0.6rem;
    background: var(--color-primary);
    color: #fff;
    border: none;
    border-radius: 6px;
    font-size: 1rem;
    cursor: pointer;
}

form button:hover {
    background: var(--color-primary-dark);
}

.alert {
    padding: 0.6rem 0.9rem;
    border-radius: 6px;
    font-size: 0.9rem;
}

.alert-error {
    background: var(--color-error-bg);
    color: var(--color-error);
}

.alert-info {
    background: var(--color-info-bg);
    color: var(--color-info-text);
}

.empty {
    color: var(--color-muted);
}

.contact-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

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

.contact-item a {
    display: grid;
    grid-template-columns: auto 1fr auto auto;
    grid-template-areas:
        "avatar name phone badge"
        "avatar preview preview badge";
    column-gap: 0.75rem;
    padding: 0.75rem 0.5rem;
    text-decoration: none;
    color: inherit;
    border-radius: 6px;
}

.contact-item a:hover {
    background: var(--color-bg);
}

.contact-item.unread a {
    background: #f0faf8;
}

.contact-item.active a {
    background: #e9edef;
}

.avatar {
    grid-area: avatar;
    align-self: center;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 0.9rem;
    color: #3c3c3c;
}

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

.contact-phone {
    grid-area: phone;
    color: var(--color-muted);
    font-size: 0.85rem;
}

.contact-preview {
    grid-area: preview;
    color: var(--color-muted);
    font-size: 0.85rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.badge {
    grid-area: badge;
    background: var(--color-primary);
    color: #fff;
    border-radius: 999px;
    padding: 0.1rem 0.5rem;
    font-size: 0.75rem;
    align-self: center;
}

.back-link {
    display: inline-block;
    margin-bottom: 0.75rem;
    color: var(--color-muted);
    text-decoration: none;
}

.app-shell {
    display: flex;
    height: calc(100vh - 150px);
    min-height: 420px;
    border: 1px solid var(--color-border);
    border-radius: 8px;
    overflow: hidden;
    background: var(--color-surface);
}

.sidebar {
    width: 320px;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--color-border);
    padding: 1rem;
    overflow: hidden;
}

.sidebar h1 {
    margin: 0 0 0.75rem;
    flex-shrink: 0;
}

.new-conversation {
    flex-shrink: 0;
    margin-bottom: 0.75rem;
}

.new-conversation summary {
    cursor: pointer;
    padding: 0.5rem 0.75rem;
    border-radius: 6px;
    background: var(--color-bg);
    color: var(--color-primary);
    font-weight: 600;
    font-size: 0.9rem;
    list-style: none;
}

.new-conversation summary::-webkit-details-marker {
    display: none;
}

.new-conversation form {
    padding: 0.75rem;
}

#contacts-container {
    overflow-y: auto;
    flex: 1;
}

.chat-pane {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
}

.empty-pane {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--color-muted);
    text-align: center;
    padding: 1rem;
}

.chat-pane-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 1rem;
    border-bottom: 1px solid var(--color-border);
    flex-shrink: 0;
}

.chat-pane-header .avatar {
    width: 44px;
    height: 44px;
    flex-shrink: 0;
}

.chat-pane-header h1 {
    margin: 0;
    font-size: 1.1rem;
}

.chat-pane-header .contact-phone {
    margin: 0.1rem 0 0;
}

.chat-pane .alert {
    margin: 0.75rem 1rem 0;
    flex-shrink: 0;
}

.chat-pane .thread {
    flex: 1;
    overflow-y: auto;
    margin: 0;
    border-radius: 0;
}

.chat-pane .reply-form,
.chat-pane .template-form {
    flex-shrink: 0;
    margin-top: 0;
    padding: 0.75rem 1rem;
    border-top: 1px solid var(--color-border);
}

.thread {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    padding: 1rem;
    background: var(--color-chat-bg);
}

.bubble {
    max-width: 75%;
    min-width: 0;
    padding: 0.4rem 0.6rem;
    border-radius: 7.5px;
    background: var(--color-surface);
    box-shadow: 0 1px 0.5px rgba(0, 0, 0, 0.13);
}

.bubble p {
    margin: 0;
}

.bubble-media {
    max-width: 100%;
    border-radius: 6px;
}

.bubble-in {
    align-self: flex-start;
}

.bubble-out {
    align-self: flex-end;
    background: #d9fdd3;
}

.bubble-meta {
    display: block;
    margin-top: 0.25rem;
    font-size: 0.7rem;
    color: var(--color-muted);
    text-align: right;
}

.ticks {
    display: inline-block;
    margin-left: 0.15rem;
}

.ticks-sent,
.ticks-delivered {
    color: var(--color-muted);
}

.ticks-read {
    color: var(--color-read-tick);
}

.ticks-failed {
    color: var(--color-error);
}

.reply-form,
.template-form {
    margin-top: 1rem;
}

.template-register,
.template-list {
    margin-top: 1.5rem;
    padding: 1rem;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: 8px;
}

.template-register h2,
.template-list h2 {
    margin: 0 0 0.5rem;
    font-size: 1rem;
}

.template-items {
    list-style: none;
    padding: 0;
    margin: 0;
}

.template-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.6rem 0;
}

.template-item + .template-item {
    border-top: 1px solid var(--color-border);
}

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

.template-meta {
    color: var(--color-muted);
    font-size: 0.85rem;
    margin-left: auto;
}

.template-status {
    padding: 0.1rem 0.5rem;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: lowercase;
}

.status-approved {
    background: #d9fdd3;
    color: #1f7a1f;
}

.status-pending {
    background: #fff3cd;
    color: #8a6500;
}

.status-rejected {
    background: var(--color-error-bg);
    color: var(--color-error);
}
