:root {
	--bg: #f4f4f5;
	--panel: #ffffff;
	--border: #e4e4e7;
	--text: #18181b;
	--muted: #71717a;
	--accent: #2f6fed;
	--accent-soft: #e8f0fe;
	--bubble-in: #ffffff;
	--bubble-out: #d7e9ff;
	--danger: #dc2626;
	--radius: 10px;
	--sidebar: 340px;
}

* { box-sizing: border-box; }

html, body {
	margin: 0;
	height: 100%;
	background: var(--bg);
	color: var(--text);
	font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

button, input, textarea { font: inherit; }

/* ---------- Вход ---------- */

.login-wrap {
	min-height: 100%;
	display: grid;
	place-items: center;
	padding: 24px;
}

.login-card {
	width: 100%;
	max-width: 360px;
	background: var(--panel);
	border: 1px solid var(--border);
	border-radius: 14px;
	padding: 28px 24px;
}

.login-card h1 { margin: 0 0 4px; font-size: 19px; }

.login-card p.sub {
	margin: 0 0 20px;
	color: var(--muted);
	font-size: 14px;
}

.field { margin-bottom: 14px; }

.field label {
	display: block;
	margin-bottom: 5px;
	font-size: 13px;
	color: var(--muted);
}

.field input {
	width: 100%;
	padding: 10px 12px;
	border: 1px solid var(--border);
	border-radius: var(--radius);
	background: #fff;
}

.field input:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }

.checkbox {
	display: flex;
	align-items: center;
	gap: 8px;
	margin: 16px 0;
	font-size: 14px;
	color: var(--muted);
}

.btn {
	width: 100%;
	padding: 11px 14px;
	border: 0;
	border-radius: var(--radius);
	background: var(--accent);
	color: #fff;
	cursor: pointer;
	font-weight: 500;
}

.btn:hover { filter: brightness(1.05); }
.btn:disabled { opacity: .6; cursor: default; }

.error {
	margin: 0 0 14px;
	padding: 10px 12px;
	border-radius: var(--radius);
	background: #fef2f2;
	color: var(--danger);
	font-size: 14px;
}

.error[hidden] { display: none; }

/* ---------- Каркас панели ---------- */

.app {
	display: flex;
	height: 100vh;
	overflow: hidden;
}

.sidebar {
	width: var(--sidebar);
	flex: 0 0 var(--sidebar);
	display: flex;
	flex-direction: column;
	background: var(--panel);
	border-right: 1px solid var(--border);
}

.sidebar-head { padding: 12px 14px; border-bottom: 1px solid var(--border); }

.sidebar-head .title { font-weight: 600; font-size: 15px; }

.sidebar-head .who {
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 8px;
	margin-top: 4px;
	font-size: 13px;
	color: var(--muted);
}

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

.link-btn:hover { text-decoration: underline; }

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

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

.search input:focus { outline: none; border-color: var(--accent); background: #fff; }

.tabs {
	display: flex;
	gap: 4px;
	padding: 8px 10px;
	border-bottom: 1px solid var(--border);
}

.tab {
	flex: 1;
	padding: 6px 4px;
	border: 0;
	border-radius: 8px;
	background: none;
	color: var(--muted);
	cursor: pointer;
	font-size: 13px;
	white-space: nowrap;
}

.tab:hover { background: var(--bg); }

.tab[aria-selected="true"] {
	background: var(--accent-soft);
	color: var(--accent);
	font-weight: 500;
}

.dialog-list {
	flex: 1;
	overflow-y: auto;
	overscroll-behavior: contain;
}

.dialog {
	display: block;
	width: 100%;
	text-align: left;
	border: 0;
	border-bottom: 1px solid var(--border);
	background: none;
	padding: 10px 14px;
	cursor: pointer;
	color: var(--text);
}

.dialog:hover { background: var(--bg); }
.dialog[aria-current="true"] { background: var(--accent-soft); }
.dialog.inactive, .dialog.inactive .dialog-name { color: var(--muted); }

.dialog-row, .dialog-preview-row {
	display: flex;
	align-items: baseline;
	justify-content: space-between;
	gap: 8px;
}

.dialog-name {
	font-weight: 500;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}

.dialog-time {
	flex: 0 0 auto;
	font-size: 12px;
	color: var(--muted);
}

.dialog-sub, .dialog-preview {
	font-size: 13px;
	color: var(--muted);
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}

.badge {
	flex: 0 0 auto;
	min-width: 20px;
	height: 20px;
	padding: 0 6px;
	border-radius: 10px;
	background: var(--accent);
	color: #fff;
	font-size: 12px;
	line-height: 20px;
	text-align: center;
}

.badge[hidden] { display: none; }

.tag-inactive { font-size: 12px; color: var(--muted); font-style: italic; }

/* ---------- Переписка ---------- */

.chat {
	flex: 1;
	min-width: 0;
	display: flex;
	flex-direction: column;
	background: var(--bg);
}

.chat-empty {
	flex: 1;
	display: grid;
	place-items: center;
	color: var(--muted);
}

/* Правило для hidden обязательно: иначе display выше перебивает атрибут. */
.chat-empty[hidden] { display: none; }

.chat-view {
	display: flex;
	flex-direction: column;
	flex: 1;
	min-height: 0;
}

.chat-view[hidden] { display: none; }

.chat-head {
	padding: 10px 16px;
	background: var(--panel);
	border-bottom: 1px solid var(--border);
	display: flex;
	align-items: center;
	gap: 12px;
}

.back-btn { display: none; }

.chat-head .who-name { font-weight: 600; }

.chat-head .who-meta {
	font-size: 13px;
	color: var(--muted);
	display: flex;
	flex-wrap: wrap;
	gap: 10px;
}

.chat-head .who-meta a { color: var(--accent); text-decoration: none; }
.chat-head .who-meta a:hover { text-decoration: underline; }

.status-dot::before { content: "\25CF"; margin-right: 4px; color: #16a34a; }
.status-dot.off::before { color: #a1a1aa; }

.note-box {
	padding: 10px 16px;
	background: #fffbeb;
	border-bottom: 1px solid var(--border);
}

.note-box[hidden] { display: none; }

.note-box textarea {
	width: 100%;
	min-height: 60px;
	padding: 8px 10px;
	border: 1px solid var(--border);
	border-radius: var(--radius);
	resize: vertical;
}

.note-actions { margin-top: 8px; display: flex; gap: 8px; align-items: center; }

.btn-small {
	padding: 6px 12px;
	border: 0;
	border-radius: 8px;
	background: var(--accent);
	color: #fff;
	cursor: pointer;
	font-size: 13px;
}

.btn-ghost {
	padding: 6px 12px;
	border: 1px solid var(--border);
	border-radius: 8px;
	background: #fff;
	cursor: pointer;
	font-size: 13px;
}

.feed {
	flex: 1;
	overflow-y: auto;
	overscroll-behavior: contain;
	padding: 16px;
	display: flex;
	flex-direction: column;
	gap: 4px;
}

.day-sep {
	align-self: center;
	margin: 12px 0 8px;
	padding: 3px 12px;
	border-radius: 12px;
	background: rgba(0, 0, 0, .06);
	font-size: 12px;
	color: var(--muted);
}

.msg {
	max-width: min(560px, 78%);
	padding: 7px 11px 5px;
	border-radius: 12px;
	background: var(--bubble-in);
	border: 1px solid var(--border);
	align-self: flex-start;
	overflow-wrap: anywhere;
}

.msg.out {
	align-self: flex-end;
	background: var(--bubble-out);
	border-color: #c3dcff;
}

.msg-author {
	font-size: 12px;
	color: var(--accent);
	font-weight: 500;
	margin-bottom: 2px;
}

.msg-quote {
	margin-bottom: 4px;
	padding: 4px 8px;
	border-left: 3px solid var(--accent);
	background: rgba(0, 0, 0, .04);
	border-radius: 4px;
	font-size: 13px;
	color: var(--muted);
	white-space: pre-wrap;
}

.msg-text { white-space: pre-wrap; }
.msg-text a { color: var(--accent); }

.msg-meta {
	display: flex;
	align-items: center;
	justify-content: flex-end;
	gap: 5px;
	margin-top: 2px;
	font-size: 11px;
	color: var(--muted);
	white-space: nowrap;
}

.msg-meta .edited { font-style: italic; }
.msg-meta .failed { color: var(--danger); }

.retry-btn {
	border: 0;
	background: none;
	color: var(--danger);
	cursor: pointer;
	font-size: 11px;
	text-decoration: underline;
	padding: 0;
}

.composer {
	display: flex;
	gap: 8px;
	align-items: flex-end;
	padding: 10px 16px;
	background: var(--panel);
	border-top: 1px solid var(--border);
}

.composer textarea {
	flex: 1;
	min-height: 40px;
	max-height: 180px;
	padding: 9px 12px;
	border: 1px solid var(--border);
	border-radius: var(--radius);
	resize: none;
	overflow-y: auto;
}

.composer textarea:focus { outline: none; border-color: var(--accent); }
.composer textarea:disabled { background: var(--bg); color: var(--muted); }

.composer .btn-send {
	flex: 0 0 auto;
	padding: 9px 18px;
	border: 0;
	border-radius: var(--radius);
	background: var(--accent);
	color: #fff;
	cursor: pointer;
}

.composer .btn-send:disabled { opacity: .5; cursor: default; }

.toast {
	position: fixed;
	left: 50%;
	bottom: 24px;
	transform: translateX(-50%);
	padding: 10px 16px;
	border-radius: var(--radius);
	background: #18181b;
	color: #fff;
	font-size: 14px;
	z-index: 50;
}

.toast[hidden] { display: none; }

.loading {
	padding: 12px;
	text-align: center;
	color: var(--muted);
	font-size: 13px;
}

.loading[hidden] { display: none; }

/* ---------- Телефон ---------- */

@media (max-width: 760px) {
	:root { --sidebar: 100%; }

	.sidebar { border-right: 0; }
	.chat { display: none; }

	.app.chat-open .sidebar { display: none; }
	.app.chat-open .chat { display: flex; }

	.back-btn {
		display: block;
		border: 0;
		background: none;
		font-size: 20px;
		line-height: 1;
		cursor: pointer;
		padding: 0 4px 0 0;
		color: var(--accent);
	}

	.msg { max-width: 86%; }
}

/* ---------- Связь и новые сообщения (этап 5) ---------- */

.who-actions { display: flex; gap: 12px; }

.offline-bar {
	position: fixed;
	top: 0;
	left: 50%;
	transform: translateX(-50%);
	z-index: 60;
	padding: 6px 18px;
	border-radius: 0 0 10px 10px;
	background: #b45309;
	color: #fff;
	font-size: 13px;
}

.offline-bar[hidden] { display: none; }

.feed-wrap {
	position: relative;
	flex: 1;
	min-height: 0;
	display: flex;
}

.feed-wrap .feed { flex: 1; }

.new-msgs {
	position: absolute;
	right: 20px;
	bottom: 14px;
	padding: 8px 14px;
	border: 1px solid var(--border);
	border-radius: 20px;
	background: var(--panel);
	color: var(--accent);
	cursor: pointer;
	font-size: 13px;
	box-shadow: 0 2px 10px rgba(0, 0, 0, .12);
}

.new-msgs[hidden] { display: none; }

.sound-off { color: var(--muted) !important; }

/* ---------- Вложения и просмотрщик фото (этап 6) ---------- */

.btn-attach {
	flex: 0 0 auto;
	width: 40px;
	height: 40px;
	border: 1px solid var(--border);
	border-radius: var(--radius);
	background: #fff;
	cursor: pointer;
	font-size: 18px;
	line-height: 1;
}

.btn-attach:hover { background: var(--bg); }
.btn-attach:disabled { opacity: .5; cursor: default; }

.attach-preview {
	display: flex;
	align-items: center;
	gap: 12px;
	padding: 10px 16px;
	background: var(--accent-soft);
	border-top: 1px solid var(--border);
}

.attach-preview[hidden] { display: none; }

.attach-preview img {
	width: 56px;
	height: 56px;
	object-fit: cover;
	border-radius: 8px;
	flex: 0 0 auto;
}

.attach-preview img[hidden] { display: none; }

.attach-info { flex: 1; min-width: 0; }

.attach-info > div {
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}

.viewer {
	position: fixed;
	inset: 0;
	z-index: 100;
	display: flex;
	align-items: center;
	justify-content: center;
	background: rgba(0, 0, 0, .9);
	cursor: zoom-out;
}

.viewer[hidden] { display: none; }

.viewer img {
	max-width: 94vw;
	max-height: 90vh;
	object-fit: contain;
}

.viewer-close {
	position: absolute;
	top: 14px;
	right: 18px;
	border: 0;
	background: none;
	color: #fff;
	font-size: 28px;
	line-height: 1;
	cursor: pointer;
}

.viewer-download {
	position: absolute;
	bottom: 20px;
	left: 50%;
	transform: translateX(-50%);
	padding: 8px 18px;
	border-radius: 20px;
	background: rgba(255, 255, 255, .15);
	color: #fff;
	text-decoration: none;
	font-size: 14px;
}

.viewer-download:hover { background: rgba(255, 255, 255, .28); }
