:root {
  --bg: #f6f7fb;
  --panel: #ffffff;
  --border: #e6e8ef;
  --text: #171a23;
  --muted: #6b7184;
  --accent: #4f46e5;
  --accent-soft: #eef0ff;
  --out: #4f46e5;
  --out-text: #ffffff;
  --in: #eff1f6;
  --danger: #d92d20;
  --ok: #12805c;
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1117;
    --panel: #171a23;
    --border: #262a36;
    --text: #e8eaf1;
    --muted: #9298ab;
    --accent: #7c74ff;
    --accent-soft: #23234a;
    --out: #5b52f0;
    --in: #232733;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; background: var(--bg); color: var(--text); }
.hidden { display: none !important; }
input, textarea, select, button { font: inherit; color: inherit; }
input, textarea, select {
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 9px 11px; width: 100%;
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label { display: block; font-weight: 500; margin: 0 0 12px; }
label input, label select, label textarea { margin-top: 4px; font-weight: 400; }

.btn { border: 1px solid var(--border); background: var(--panel); border-radius: 8px; padding: 8px 12px; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn.icon { padding: 8px 10px; display: inline-flex; align-items: center; }
.btn.wide { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }

.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; margin-bottom: 14px; }
.brand.big { font-size: 22px; justify-content: center; margin-bottom: 20px; }
.logo { width: 26px; height: 26px; border-radius: 7px; background: var(--accent); display: inline-block; position: relative; }
.logo::after { content: ""; position: absolute; inset: 7px 6px 8px; background: #fff; border-radius: 3px; }

.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 28px; width: 100%; max-width: 380px; }
.card .btn { width: 100%; margin-top: 6px; padding: 10px; }
.card.wide-card { max-width: 460px; }
.form-h { margin: 18px 0 8px; font-size: 15px; }
.error { color: var(--danger); min-height: 1em; margin: 10px 0 0; }

.app { display: grid; grid-template-columns: 230px 340px 1fr; height: 100%; }
.sidebar { background: var(--panel); border-right: 1px solid var(--border); padding: 16px; display: flex; flex-direction: column; gap: 10px; overflow-y: auto; }
.sidebar nav { flex: 1; }
.nav-label { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 14px 8px 4px; }
.nav-item { display: flex; justify-content: space-between; padding: 7px 8px; border-radius: 7px; color: inherit; text-decoration: none; }
.nav-item:hover { background: var(--bg); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.badge { background: var(--accent); color: #fff; border-radius: 999px; font-size: 11px; padding: 0 7px; line-height: 18px; font-weight: 600; }
.me { display: flex; flex-direction: column; gap: 6px; border-top: 1px solid var(--border); padding-top: 12px; }
.switch { display: flex; align-items: center; gap: 8px; font-weight: 500; margin: 0 0 4px; cursor: pointer; }
.switch input { display: none; }
.switch span { width: 34px; height: 20px; border-radius: 999px; background: var(--border); position: relative; flex: none; transition: .15s; }
.switch span::after { content: ""; position: absolute; width: 16px; height: 16px; border-radius: 50%; background: #fff; top: 2px; left: 2px; transition: .15s; }
.switch input:checked + span { background: var(--ok); }
.switch input:checked + span::after { left: 16px; }

.list { border-right: 1px solid var(--border); display: flex; flex-direction: column; min-width: 0; background: var(--panel); }
.list-head { padding: 12px; border-bottom: 1px solid var(--border); display: flex; gap: 8px; }
.notice { margin: 12px; padding: 10px 12px; border-radius: 8px; background: #fff7e6; color: #8a5a00; font-size: 13px; }
@media (prefers-color-scheme: dark) { .notice { background: #3a2c0d; color: #f5c56b; } }
.convs { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conv { padding: 12px 14px; border-bottom: 1px solid var(--border); cursor: pointer; display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; }
.conv:hover { background: var(--bg); }
.conv.active { background: var(--accent-soft); }
.conv .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv.unread .name::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); margin-right: 6px; vertical-align: middle; }
.conv .time { color: var(--muted); font-size: 12px; }
.conv .preview { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; grid-column: 1 / -1; }
.conv .line-tag { font-size: 11px; color: var(--muted); grid-column: 1 / -1; }
.convs .none { padding: 30px 16px; color: var(--muted); text-align: center; }

.thread { display: flex; flex-direction: column; min-width: 0; height: 100%; position: relative; }
.empty { margin: auto; color: var(--muted); padding: 20px; text-align: center; }
.thread-inner { display: flex; flex-direction: column; height: 100%; min-height: 0; }
.thread-head { display: flex; align-items: center; gap: 8px; padding: 12px 16px; border-bottom: 1px solid var(--border); background: var(--panel); flex-wrap: wrap; }
.who { flex: 1; min-width: 140px; }
.who-name { font-weight: 700; font-size: 16px; }
.who-sub { color: var(--muted); font-size: 12px; }
.items { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 6px; }
.day { text-align: center; color: var(--muted); font-size: 12px; margin: 10px 0 4px; }
.bubble { max-width: min(75%, 520px); padding: 8px 12px; border-radius: 16px; white-space: pre-wrap; word-wrap: break-word; }
.bubble.in { background: var(--in); align-self: flex-start; border-bottom-left-radius: 4px; }
.bubble.out { background: var(--out); color: var(--out-text); align-self: flex-end; border-bottom-right-radius: 4px; }
.bubble a { color: inherit; text-decoration: underline; }
.media { max-width: min(75%, 360px); display: flex; flex-direction: column; gap: 4px; }
.media.in { align-self: flex-start; align-items: flex-start; }
.media.out { align-self: flex-end; align-items: flex-end; }
.media video { max-width: 100%; max-height: 320px; border-radius: 14px; display: block; border: 1px solid var(--border); background: #000; }
.media .file { background: var(--in); padding: 8px 12px; border-radius: 12px; color: inherit; text-decoration: none; }
.media .file.small { background: none; padding: 0 4px; font-size: 12px; color: var(--muted); text-decoration: underline; }
.photos { display: grid; gap: 3px; border-radius: 16px; overflow: hidden; width: 100%; }
.photos.n1 { width: auto; }
.photos.n2, .photos.n4 { grid-template-columns: 1fr 1fr; width: 300px; max-width: 100%; }
.photos.n3 { grid-template-columns: 1fr 1fr 1fr; width: 330px; max-width: 100%; }
.ph { display: block; padding: 0; border: 0; background: var(--in); cursor: zoom-in; margin: 0; min-width: 0; }
.photos.n1 .ph img { max-width: 100%; max-height: 340px; width: auto; height: auto; display: block; }
.photos:not(.n1) .ph { aspect-ratio: 1; }
.photos:not(.n1) .ph img { width: 100%; height: 100%; object-fit: cover; display: block; aspect-ratio: auto !important; }
.ph.loading { width: 220px; height: 160px; display: grid; place-items: center; color: var(--muted); font-size: 12px; cursor: default; animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .55; } }
.pending { display: flex; flex-direction: column; gap: 6px; }
.pending .ph { cursor: default; }
.pending:not(.has-error) .photos, .pending:not(.has-error) .bubble { opacity: .7; }
.bar { display: inline-block; width: 70px; height: 4px; border-radius: 4px; background: var(--border); margin-left: 8px; vertical-align: middle; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .2s; }
.link-btn { border: 0; background: none; padding: 0; color: var(--accent); cursor: pointer; font-size: 11px; font-weight: 600; text-decoration: underline; }
.link-btn:disabled { color: var(--muted); text-decoration: none; }
.st.delivered, .st.read { color: var(--ok); }
.meta { font-size: 11px; color: var(--muted); }
.meta.in { align-self: flex-start; margin: 0 4px 6px; }
.meta.out { align-self: flex-end; margin: 0 4px 6px; }
.meta .failed { color: var(--danger); }
.callrow { align-self: center; background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 8px 14px; font-size: 13px; text-align: center; max-width: 90%; }
.callrow audio { display: block; margin-top: 6px; width: 260px; max-width: 100%; }

.composer { border-top: 1px solid var(--border); padding: 10px 12px; background: var(--panel); }
.composer-row { display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { resize: none; max-height: 160px; }
.attach-list { display: flex; flex-wrap: wrap; gap: 6px; }
.attach-list:not(:empty) { margin-bottom: 8px; }
.chip { background: var(--bg); border: 1px solid var(--border); border-radius: 999px; padding: 3px 10px; font-size: 12px; display: inline-flex; gap: 6px; align-items: center; }
.chip button { border: 0; background: none; cursor: pointer; padding: 0; color: var(--muted); }
.thumb { position: relative; width: 64px; height: 64px; border-radius: 10px; overflow: hidden; border: 1px solid var(--border); background: var(--bg); display: grid; place-items: center; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb-label { font-size: 11px; color: var(--muted); }
.thumb button { position: absolute; top: 3px; right: 3px; width: 20px; height: 20px; border-radius: 50%; border: 0; background: rgba(0,0,0,.6); color: #fff; font-size: 11px; line-height: 20px; padding: 0; cursor: pointer; }
.composer-foot { font-size: 11px; color: var(--muted); margin-top: 6px; }
.composer-foot:empty { display: none; }
.drop-zone { position: absolute; inset: 0; background: rgba(79, 70, 229, .12); border: 2px dashed var(--accent); border-radius: 12px; display: grid; place-items: center; z-index: 4; pointer-events: none; }
.drop-zone div { background: var(--panel); padding: 12px 18px; border-radius: 10px; font-weight: 600; color: var(--accent); }

.lightbox { position: fixed; inset: 0; background: rgba(8, 9, 14, .92); z-index: 30; display: grid; place-items: center; }
.lightbox img { max-width: calc(100vw - 120px); max-height: calc(100dvh - 110px); object-fit: contain; border-radius: 6px; }
.lb-bar { position: absolute; top: 12px; right: 12px; left: 16px; display: flex; gap: 8px; align-items: center; justify-content: flex-end; color: #fff; }
.lb-bar span { margin-right: auto; font-size: 13px; opacity: .8; }
.lb-bar .btn { background: rgba(255,255,255,.12); border-color: transparent; color: #fff; text-decoration: none; }
.lb-nav { position: absolute; top: 50%; transform: translateY(-50%); width: 46px; height: 46px; border-radius: 50%; border: 0; background: rgba(255,255,255,.14); color: #fff; font-size: 30px; line-height: 1; cursor: pointer; }
.lb-nav.prev { left: 14px; }
.lb-nav.next { right: 14px; }
@media (max-width: 900px) { .lightbox img { max-width: 100vw; max-height: calc(100dvh - 140px); border-radius: 0; } .lb-nav { top: auto; bottom: 18px; transform: none; } }

.modal { position: fixed; inset: 0; background: rgba(10, 12, 20, .5); display: grid; place-items: center; padding: 16px; z-index: 10; }
.modal-card { background: var(--panel); border-radius: var(--radius); padding: 24px; width: 100%; max-width: 640px; max-height: 90vh; overflow-y: auto; position: relative; }
.modal-card h2 { margin: 0 0 16px; font-size: 18px; }
.modal-card h3 { margin: 22px 0 10px; font-size: 15px; }
.modal-card .close { position: absolute; top: 14px; right: 14px; }
.row { display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap; }
.row > * { flex: 1; min-width: 140px; }
.row > .btn { flex: none; }
.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table td, .table th { text-align: left; padding: 7px 6px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.code { font-family: ui-monospace, Menlo, monospace; font-size: 12px; background: var(--bg); padding: 8px; border-radius: 6px; word-break: break-all; display: block; margin-top: 4px; }
.help { color: var(--muted); font-size: 13px; }

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--panel); padding: 10px 16px; border-radius: 10px; z-index: 20; max-width: calc(100% - 32px); }

.mobile-only { display: none; }
@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: 260px; z-index: 5; transform: translateX(-100%); transition: transform .2s; box-shadow: 0 0 30px rgba(0,0,0,.2); }
  .app.menu-open .sidebar { transform: none; }
  .mobile-only { display: inline-flex; }
  .thread { display: none; }
  .app.show-thread .list { display: none; }
  .app.show-thread .thread { display: flex; height: 100dvh; }
  .thread-head .btn.ghost { padding: 6px 8px; font-size: 13px; }
  .bubble { max-width: 85%; }
}
