:root {
    --bg: #f4f5f7;
    --panel: #ffffff;
    --line: #e3e6ea;
    --text: #1c2024;
    --muted: #6b7280;
    --accent: #2563eb;
    --accent-soft: #eff4ff;
    --danger: #b42318;
    --ok: #087443;
    --radius: 10px;
    --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .04);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #14161a;
        --panel: #1b1e24;
        --line: #2b3038;
        --text: #e8eaed;
        --muted: #9aa3af;
        --accent: #6191ff;
        --accent-soft: #1e2735;
        --danger: #f97066;
        --ok: #4ade80;
        --shadow: none;
    }
}

* { box-sizing: border-box; }

/* display:grid у .login/.app перебивает атрибут hidden — возвращаем ему силу */
[hidden] { display: none !important; }

body {
    margin: 0;
    font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
}

button { font: inherit; cursor: pointer; }
.muted { color: var(--muted); }

/* ── Вход ── */
.login { min-height: 100vh; display: grid; place-items: center; padding: 24px; }

.loginCard {
    width: 100%; max-width: 360px; background: var(--panel); padding: 28px;
    border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow);
    display: flex; flex-direction: column; gap: 14px;
}
.loginCard h1 { margin: 0; font-size: 20px; }
.loginCard p { margin: -8px 0 4px; font-size: 13px; }
.loginCard label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }

input, textarea {
    font: inherit; color: var(--text); background: var(--bg);
    border: 1px solid var(--line); border-radius: 8px; padding: 9px 11px; width: 100%;
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }

.loginCard button {
    background: var(--accent); color: #fff; border: 0; border-radius: 8px;
    padding: 11px; font-weight: 600;
}
.err { color: var(--danger); font-size: 13px; }

/* ── Раскладка кабинета ── */
.app { display: grid; grid-template-columns: 216px 340px 1fr; height: 100vh; }

.sidebar {
    background: var(--panel); border-right: 1px solid var(--line);
    display: flex; flex-direction: column; padding: 16px 12px;
}
.brand { font-weight: 700; padding: 4px 8px 16px; }
.siteNav { display: flex; flex-direction: column; gap: 2px; flex: 1; overflow: auto; }

.siteRow { display: flex; align-items: center; border-radius: 8px; }
.siteRow:hover { background: var(--accent-soft); }
.siteRow.active { background: var(--accent-soft); }
.siteRow.active .siteBtn { font-weight: 600; }
.siteBtn {
    display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; text-align: left;
    background: none; border: 0; border-radius: 8px; padding: 8px 10px; color: var(--text); cursor: pointer;
}
.siteLink {
    display: grid; place-items: center; flex: 0 0 auto; width: 28px; height: 28px; margin-right: 4px;
    color: var(--muted); border-radius: 6px; opacity: .55; transition: opacity .12s, color .12s, background .12s;
}
.siteRow:hover .siteLink { opacity: 1; }
.siteLink:hover { color: var(--accent); background: var(--panel); }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: 0 0 auto; background: var(--muted); }
.siteMeta { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 1px; }
.siteBtn span.name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.siteMail { font-size: 11px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge {
    background: var(--accent); color: #fff; border-radius: 999px;
    font-size: 11px; padding: 1px 7px; font-weight: 600;
}

.sidebarFoot { display: flex; flex-direction: column; gap: 4px; padding-top: 12px; border-top: 1px solid var(--line); }
.ghost { background: none; border: 0; color: var(--muted); text-align: left; padding: 6px 10px; border-radius: 8px; font-size: 13px; }
.ghost:hover { background: var(--accent-soft); color: var(--text); }
.who { font-size: 12px; color: var(--muted); padding: 4px 10px; }

/* ── Список диалогов ── */
.list { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-width: 0; }
.listHead { padding: 12px; border-bottom: 1px solid var(--line); display: flex; flex-direction: column; gap: 10px; }
.tabs { display: flex; gap: 4px; }
.tab { background: none; border: 1px solid var(--line); border-radius: 999px; padding: 4px 12px; font-size: 12px; color: var(--muted); }
.tab.active { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); font-weight: 600; }

.threads { overflow: auto; flex: 1; }
.threadItem { padding: 12px 14px; border-bottom: 1px solid var(--line); cursor: pointer; display: flex; flex-direction: column; gap: 3px; }
.threadItem:hover { background: var(--accent-soft); }
.threadItem.active { background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }
.threadTop { display: flex; align-items: center; gap: 8px; }
.threadTop .who2 { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.threadTop time { font-size: 11px; color: var(--muted); flex: 0 0 auto; }
.threadSub { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.threadSnip { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip { font-size: 10px; padding: 1px 6px; border-radius: 4px; border: 1px solid var(--line); color: var(--muted); flex: 0 0 auto; }
.chip.form { border-color: var(--ok); color: var(--ok); }
.unreadDot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); flex: 0 0 auto; }

/* ── Диалог ── */
.thread { display: flex; flex-direction: column; min-width: 0; }
.empty { margin: auto; color: var(--muted); }

.threadHead { padding: 14px 20px; border-bottom: 1px solid var(--line); background: var(--panel); display: flex; gap: 12px; align-items: flex-start; }
.threadHead h2 { margin: 0 0 2px; font-size: 16px; }
.threadHead .sub { font-size: 12px; color: var(--muted); }
.threadActions { margin-left: auto; display: flex; gap: 6px; }
.threadActions button { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; font-size: 13px; color: var(--text); }
.threadActions button:hover { border-color: var(--accent); color: var(--accent); }

.messages { flex: 1; overflow: auto; padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.msg { max-width: 720px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.msg.out { align-self: flex-end; background: var(--accent-soft); border-color: transparent; }
.msgHead { display: flex; gap: 8px; align-items: baseline; font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.msgHead b { color: var(--text); font-size: 13px; }
.msgBody { white-space: pre-wrap; word-wrap: break-word; font-size: 14px; }
.files { margin-top: 10px; display: flex; flex-wrap: wrap; gap: 6px; }
.fileLink {
    display: inline-flex; align-items: center; gap: 6px; font-size: 12px; text-decoration: none;
    color: var(--text); border: 1px solid var(--line); border-radius: 6px; padding: 4px 9px; background: var(--bg);
}
.fileLink:hover { border-color: var(--accent); color: var(--accent); }
.failed { color: var(--danger); font-size: 12px; margin-top: 6px; }

.reply { border-top: 1px solid var(--line); background: var(--panel); padding: 12px 20px 16px; display: flex; flex-direction: column; gap: 8px; }
.reply textarea { min-height: 84px; resize: vertical; }
.replyBar { display: flex; align-items: center; gap: 10px; }
.replyBar .grow { flex: 1; font-size: 12px; color: var(--muted); }
.reply button.send { background: var(--accent); color: #fff; border: 0; border-radius: 8px; padding: 9px 20px; font-weight: 600; }
.reply button.send:disabled { opacity: .5; cursor: default; }
.attachBtn { border: 1px solid var(--line); background: var(--bg); border-radius: 8px; padding: 8px 12px; font-size: 13px; color: var(--text); }

.modal { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; padding: 20px; z-index: 20; }
.modalCard { background: var(--panel); border-radius: 12px; padding: 20px; max-width: 560px; width: 100%; max-height: 80vh; overflow: auto; }
.modalCard h3 { margin: 0 0 12px; }
.mailRow { display: flex; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--line); font-size: 13px; }

@media (max-width: 980px) {
    .app { grid-template-columns: 1fr; grid-template-rows: auto auto 1fr; height: auto; }
    .sidebar { flex-direction: row; align-items: center; gap: 8px; overflow-x: auto; }
    .siteNav { flex-direction: row; }
    .sidebarFoot { flex-direction: row; border: 0; padding: 0; }
    .list { max-height: 42vh; }
}
