:root {
    --bg: #f3f6f4;
    --surface: #ffffff;
    --surface-2: #e9efec;
    --text: #131c1a;
    --muted: #5c6b66;
    --border: #d7e0db;
    --accent: #0e6e5b;
    --accent-soft: #ddebe5;
    --warn: #8a5a07;
    --crit: #9b2a24;
    --radius: 10px;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 15px;
    line-height: 1.7;
}

body.centered {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 20px;
}

/* ---------------------------------------------------------------- topbar */
.topbar {
    display: flex;
    align-items: center;
    gap: 20px;
    padding: 12px 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 10;
}

.brand { font-weight: 700; }

.topbar .stats {
    display: flex;
    gap: 14px;
    font-size: .82rem;
    color: var(--muted);
    flex-wrap: wrap;
}

.topbar nav {
    margin-inline-start: auto;
    display: flex;
    align-items: center;
    gap: 14px;
}

.topbar nav a { color: var(--accent); text-decoration: none; }
.topbar nav a:hover { text-decoration: underline; }

.inline { display: inline; margin: 0; }

.link-btn {
    background: none;
    border: none;
    color: var(--accent);
    cursor: pointer;
    font: inherit;
    padding: 0;
}

/* ---------------------------------------------------------------- layout */
.layout {
    display: grid;
    grid-template-columns: minmax(260px, 340px) 1fr;
    height: calc(100vh - 53px);
}

.page {
    max-width: 720px;
    margin: 0 auto;
    padding: 28px 20px 60px;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

/* ------------------------------------------------------------- list pane */
.list-pane {
    background: var(--surface);
    border-inline-start: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.search-row { padding: 12px; border-bottom: 1px solid var(--border); }

.search-row input {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font: inherit;
    background: var(--bg);
}

.conv-list {
    list-style: none;
    margin: 0;
    padding: 0;
    overflow-y: auto;
    flex: 1;
}

.conv-item {
    padding: 12px 14px;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
    display: grid;
    gap: 3px;
}

.conv-item:hover { background: var(--surface-2); }
.conv-item.active { background: var(--accent-soft); }

.conv-item .row1 {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    align-items: baseline;
}

.conv-item .name { font-weight: 600; }

.conv-item .time {
    font-size: .72rem;
    color: var(--muted);
    white-space: nowrap;
}

.conv-item .preview {
    font-size: .84rem;
    color: var(--muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.empty {
    padding: 24px 16px;
    color: var(--muted);
    text-align: center;
    font-size: .9rem;
}

/* ----------------------------------------------------------------- badge */
.badge {
    display: inline-block;
    font-size: .7rem;
    padding: 1px 8px;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--muted);
    white-space: nowrap;
}

.badge.granted { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
.badge.revoked { background: #f6e0de; color: var(--crit); border-color: var(--crit); }
.badge.pending { background: #f5e9d2; color: var(--warn); border-color: var(--warn); }
.badge.open    { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
.badge.closed  { background: var(--surface-2); }

/* ----------------------------------------------------------- thread pane */
.thread-pane { display: flex; flex-direction: column; overflow: hidden; }

.thread-empty {
    display: grid;
    place-items: center;
    height: 100%;
    color: var(--muted);
}

.thread {
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
}

.thread-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 16px;
    padding: 14px 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.thread-head h2 { margin: 0; font-size: 1.05rem; }
.thread-head .phone { font-size: .8rem; color: var(--muted); }
.badges { display: flex; gap: 6px; flex-wrap: wrap; }

.messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.msg { max-width: 68%; padding: 9px 14px; border-radius: 14px; }

.msg.in {
    background: var(--surface);
    border: 1px solid var(--border);
    align-self: flex-start;
    border-start-start-radius: 3px;
}

.msg.out {
    background: var(--accent-soft);
    border: 1px solid var(--accent);
    align-self: flex-end;
    border-start-end-radius: 3px;
}

.msg .body { white-space: pre-wrap; word-break: break-word; }

.msg .meta {
    font-size: .68rem;
    color: var(--muted);
    margin-top: 3px;
    display: flex;
    gap: 8px;
}

.msg.failed { border-color: var(--crit); }
.msg.failed .meta { color: var(--crit); }

/* -------------------------------------------------------------- composer */
.composer {
    display: flex;
    gap: 10px;
    padding: 12px 20px;
    background: var(--surface);
    border-top: 1px solid var(--border);
}

.composer textarea {
    flex: 1;
    resize: vertical;
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font: inherit;
    background: var(--bg);
}

.composer-note {
    margin: 0;
    padding: 0 20px 12px;
    font-size: .8rem;
    color: var(--warn);
    background: var(--surface);
}

/* ----------------------------------------------------------------- forms */
button {
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius);
    padding: 9px 20px;
    font: inherit;
    cursor: pointer;
}

button:hover { filter: brightness(1.08); }
button:disabled { opacity: .5; cursor: not-allowed; }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 24px;
}

.card h2 { margin: 0 0 6px; font-size: 1.05rem; }

.hint { color: var(--muted); font-size: .86rem; margin: 0 0 16px; }

.stack { display: flex; flex-direction: column; gap: 6px; }

.stack label { font-size: .84rem; font-weight: 600; margin-top: 10px; }

.stack input {
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font: inherit;
    background: var(--bg);
}

.stack button { margin-top: 18px; align-self: flex-start; }

.login { width: min(380px, 100%); display: flex; flex-direction: column; gap: 6px; }
.login h1 { font-size: 1.2rem; margin: 0 0 14px; }
.login label { font-size: .84rem; font-weight: 600; margin-top: 10px; }

.login input {
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font: inherit;
    background: var(--bg);
}

.login button { margin-top: 20px; }

.error  { color: var(--crit); font-size: .86rem; margin: 0; }
.notice { color: var(--accent); font-size: .86rem; margin: 0; }
.result { font-size: .86rem; margin: 12px 0 0; }

.status-line {
    background: var(--surface-2);
    border-radius: var(--radius);
    padding: 10px 14px;
    font-size: .86rem;
    margin-bottom: 16px;
}

.code {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 12px 14px;
    overflow-x: auto;
    direction: ltr;
    text-align: left;
    font-size: .82rem;
    margin: 0;
}

@media (max-width: 760px) {
    .layout { grid-template-columns: 1fr; }
    .list-pane { max-height: 42vh; }
    .msg { max-width: 88%; }
}
