:root {
  --bg: #FBF6EE;
  --panel: #FFFFFF;
  --panel-2: #F4ECE0;
  --ink: #2B2420;
  --ink-2: #6F6358;
  --line: #E9DFD1;
  --accent: #C8553D;
  --accent-ink: #FFFFFF;
  --accent-soft: #F8E2D9;
  --mine: #F6DDD1;
  --mine-ink: #2B2420;
  --theirs: #FFFFFF;
  --green: #2E8B62;
  --red: #D23F31;
  --shadow: 0 1px 2px rgba(60, 40, 20, .12);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1D1815;
    --panel: #29221E;
    --panel-2: #342B26;
    --ink: #F4ECE3;
    --ink-2: #B5A79A;
    --line: #3C322C;
    --accent: #E2714F;
    --accent-soft: #48302A;
    --mine: #5A3529;
    --mine-ink: #FBEFE8;
    --theirs: #2F2723;
    --shadow: none;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 18px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
button:disabled { opacity: .5; cursor: default; }
a { color: inherit; }
h1, h2, p { margin: 0; }
svg { display: block; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
#root { height: 100%; }

.icon { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ---- Login and install screens ---- */
.center-screen {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: calc(24px + var(--safe-top)) 24px calc(24px + var(--safe-bottom));
  overflow-y: auto;
  text-align: center;
}
.center-card { width: 100%; max-width: 420px; display: flex; flex-direction: column; gap: 16px; align-items: stretch; }
.logo { width: 96px; height: 96px; border-radius: 24px; align-self: center; box-shadow: 0 6px 18px rgba(150, 60, 30, .25); }
.brand { font-size: 36px; font-weight: 700; letter-spacing: -.01em; }
.lead { color: var(--ink-2); }
.field {
  width: 100%;
  padding: 16px;
  border-radius: 14px;
  border: 2px solid var(--line);
  background: var(--panel);
  font-size: 20px;
  text-align: center;
}
.field:focus { outline: none; border-color: var(--accent); }
.btn {
  min-height: 56px;
  padding: 12px 20px;
  border-radius: 14px;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 20px;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  text-decoration: none;
}
.btn.secondary { background: var(--panel-2); color: var(--ink); }
.error { color: var(--red); font-weight: 600; min-height: 1.35em; }
.link { color: var(--accent); text-decoration: underline; font-size: 17px; background: none; }
.steps { text-align: left; display: flex; flex-direction: column; gap: 14px; background: var(--panel); border-radius: 16px; padding: 18px; box-shadow: var(--shadow); list-style: none; margin: 0; counter-reset: s; }
.steps li { display: flex; gap: 12px; align-items: flex-start; counter-increment: s; }
.steps li::before { content: counter(s); flex: none; width: 32px; height: 32px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); font-weight: 700; display: grid; place-items: center; }

/* ---- Layout ---- */
.app { height: 100%; display: flex; }
.sidebar { width: 100%; height: 100%; display: flex; flex-direction: column; background: var(--bg); }
.chat-pane { display: none; flex: 1; min-width: 0; height: 100%; flex-direction: column; background: var(--bg); }
.app.in-chat .sidebar { display: none; }
.app.in-chat .chat-pane { display: flex; }
.empty-pane { margin: auto; color: var(--ink-2); padding: 24px; text-align: center; }
@media (min-width: 820px) {
  .sidebar { width: 360px; flex: none; border-right: 1px solid var(--line); display: flex !important; }
  .chat-pane { display: flex; }
  .back-btn { display: none !important; }
}

.topbar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(10px + var(--safe-top)) 12px 10px;
  min-height: calc(64px + var(--safe-top));
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 24px; font-weight: 700; flex: 1; min-width: 0; }
.topbar-info { flex: 1; min-width: 0; }
.topbar-name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-status { font-size: 15px; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-status.online { color: var(--green); }
.icon-btn { width: 48px; height: 48px; border-radius: 50%; display: grid; place-items: center; flex: none; color: var(--ink); }
.icon-btn:hover { background: var(--panel-2); }
.icon-btn.accent { color: var(--accent); }
.net-bar { flex: none; background: #8A6D1F; color: #fff; text-align: center; font-size: 15px; padding: 4px 8px; }
.banner { display: flex; gap: 12px; align-items: center; background: var(--accent-soft); padding: 12px 14px; font-size: 16px; }
.banner > span { flex: 1; }
.banner .btn { min-height: 44px; font-size: 16px; padding: 8px 14px; }

/* ---- Chat list ---- */
.chat-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding-bottom: var(--safe-bottom); }
.chat-row { display: flex; align-items: center; border-bottom: 1px solid var(--line); padding-right: 6px; }
.chat-row:hover { background: var(--panel-2); }
.chat-row.active { background: var(--accent-soft); }
.chat-row-open { flex: 1; min-width: 0; display: flex; align-items: center; gap: 14px; padding: 12px 8px 12px 14px; text-align: left; }
.mute-btn { color: var(--ink-2); }
.mute-btn.muted { color: var(--red); }
.chat-row-main { flex: 1; min-width: 0; }
.chat-row-top { display: flex; align-items: baseline; gap: 8px; }
.chat-row-name { font-weight: 700; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 20px; }
.chat-row-time { font-size: 14px; color: var(--ink-2); flex: none; }
.chat-row-bottom { display: flex; align-items: center; gap: 8px; margin-top: 2px; }
.chat-row-preview { flex: 1; min-width: 0; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 17px; }
.unread { flex: none; min-width: 26px; height: 26px; padding: 0 7px; border-radius: 13px; background: var(--accent); color: #fff; font-size: 15px; font-weight: 700; display: grid; place-items: center; }
.unread.muted { background: var(--ink-2); }
.avatar { position: relative; width: 56px; height: 56px; border-radius: 50%; display: grid; place-items: center; color: #fff; font-weight: 700; font-size: 24px; flex: none; user-select: none; }
.avatar.small { width: 44px; height: 44px; font-size: 19px; }
.avatar.huge { width: 136px; height: 136px; font-size: 58px; }
.avatar.online::after { content: ""; position: absolute; right: 1px; bottom: 1px; width: 15px; height: 15px; border-radius: 50%; background: #3DBE7B; border: 3px solid var(--panel); }
.c0 { background: #C8553D; } .c1 { background: #2E7D8C; } .c2 { background: #6B7F3A; } .c3 { background: #8E5AA8; }
.c4 { background: #B8862C; } .c5 { background: #3D6FB4; } .c6 { background: #A84B72; } .c7 { background: #4F8A6B; } .cg { background: #7A6555; }
.empty-list { padding: 32px 24px; text-align: center; color: var(--ink-2); }

/* ---- Messages ---- */
.messages { flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 12px 10px 8px; display: flex; flex-direction: column; gap: 4px; overscroll-behavior: contain; }
.day { align-self: center; font-size: 14px; color: var(--ink-2); background: var(--panel-2); border-radius: 10px; padding: 3px 12px; margin: 10px 0 6px; }
.msg { max-width: min(82%, 560px); display: flex; flex-direction: column; align-self: flex-start; }
.msg.mine { align-self: flex-end; }
.msg-sender { font-size: 14px; font-weight: 700; color: var(--accent); margin: 6px 0 2px 12px; }
.bubble { position: relative; background: var(--theirs); border-radius: 16px 16px 16px 5px; padding: 8px 12px; box-shadow: var(--shadow); overflow-wrap: anywhere; white-space: pre-wrap; user-select: text; }
.msg.mine .bubble { background: var(--mine); color: var(--mine-ink); border-radius: 16px 16px 5px 16px; }
.bubble.media { padding: 4px; }
.bubble img, .bubble video { display: block; max-width: 100%; max-height: 62vh; width: auto; border-radius: 12px; background: var(--panel-2); }
.bubble .caption { padding: 6px 8px 2px; }
.msg-meta { display: flex; justify-content: flex-end; align-items: center; gap: 4px; font-size: 13px; color: var(--ink-2); margin-top: 2px; padding: 0 4px; }
.bubble.media .msg-meta { padding: 2px 8px 2px; }
.tick { width: 18px; height: 14px; fill: none; stroke: var(--ink-2); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.tick.read { stroke: var(--accent); }
.deleted { font-style: italic; color: var(--ink-2); }
.file-card { display: flex; align-items: center; gap: 12px; text-decoration: none; min-width: 200px; padding: 4px 0; }
.file-icon { width: 48px; height: 48px; border-radius: 12px; background: var(--accent); color: #fff; display: grid; place-items: center; flex: none; }
.file-name { font-weight: 600; word-break: break-word; }
.file-size { font-size: 14px; color: var(--ink-2); }
.call-note { align-self: center; display: flex; align-items: center; gap: 8px; font-size: 16px; color: var(--ink-2); background: var(--panel-2); border-radius: 14px; padding: 8px 14px; margin: 6px 0; }
.call-note.missed { color: var(--red); }
.progress { height: 6px; border-radius: 3px; background: var(--line); overflow: hidden; margin-top: 8px; }
.progress > i { display: block; height: 100%; background: var(--accent); width: 0; transition: width .15s; }
.retry { color: var(--red); font-weight: 600; font-size: 15px; text-decoration: underline; }
.load-more { align-self: center; padding: 8px 16px; color: var(--accent); }

/* ---- Composer ---- */
.composer { flex: none; display: flex; align-items: flex-end; gap: 6px; padding: 8px 8px calc(8px + var(--safe-bottom)); background: var(--panel); border-top: 1px solid var(--line); }
.composer textarea {
  flex: 1;
  min-width: 0;
  resize: none;
  border: 0;
  border-radius: 22px;
  background: var(--panel-2);
  padding: 11px 16px;
  max-height: 150px;
  line-height: 1.3;
  font-size: 18px;
}
.composer textarea:focus { outline: none; }
.send-btn { background: var(--accent); color: #fff; }
.send-btn:hover { background: var(--accent); }

/* ---- Sheets, menus, lightbox ---- */
.overlay { position: fixed; inset: 0; background: rgba(20, 14, 10, .55); z-index: 20; display: flex; align-items: flex-end; justify-content: center; }
.sheet { width: 100%; max-width: 480px; background: var(--panel); border-radius: 20px 20px 0 0; padding: 14px 14px calc(14px + var(--safe-bottom)); display: flex; flex-direction: column; gap: 8px; }
@media (min-width: 820px) { .overlay { align-items: center; } .sheet { border-radius: 20px; } }
.sheet-title { font-weight: 700; padding: 4px 6px 8px; }
.sheet-note { color: var(--ink-2); font-size: 16px; padding: 0 6px 6px; }
.sheet-btn { display: flex; align-items: center; gap: 16px; min-height: 60px; padding: 8px 12px; border-radius: 14px; font-size: 20px; text-align: left; width: 100%; text-decoration: none; }
.sheet-btn:hover { background: var(--panel-2); }
.sheet-btn .icon-wrap { width: 46px; height: 46px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; flex: none; }
.sheet-btn.danger { color: var(--red); }
.sheet-btn.danger .icon-wrap { background: rgba(210, 63, 49, .13); color: var(--red); }

.lightbox { position: fixed; inset: 0; background: #000; z-index: 30; display: flex; flex-direction: column; }
.lightbox-top { display: flex; justify-content: space-between; padding: calc(8px + var(--safe-top)) 8px 8px; color: #fff; }
.lightbox-top .icon-btn { color: #fff; }
.lightbox-top .icon-btn:hover { background: rgba(255, 255, 255, .15); }
.lightbox-body { flex: 1; min-height: 0; overflow: auto; display: flex; touch-action: pinch-zoom; }
.lightbox-body img { margin: auto; max-width: 100%; max-height: 100%; object-fit: contain; }

.toast { position: fixed; left: 50%; bottom: calc(90px + var(--safe-bottom)); transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 12px; z-index: 60; max-width: 90vw; text-align: center; font-size: 16px; }

/* ---- Call screen ---- */
.call-screen { position: fixed; inset: 0; z-index: 40; background: #1A1512; color: #fff; display: flex; flex-direction: column; overflow: hidden; }
.call-remote { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #000; }
.call-remote.contain { object-fit: contain; }
.call-local { position: absolute; right: 14px; top: calc(14px + var(--safe-top)); width: 112px; height: 160px; object-fit: cover; border-radius: 16px; background: #000; box-shadow: 0 4px 16px rgba(0, 0, 0, .5); z-index: 2; }
.call-local.mirror { transform: scaleX(-1); }
.call-info { position: relative; z-index: 1; flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; padding: calc(24px + var(--safe-top)) 24px 24px; text-align: center; }
.call-info.over-video { justify-content: flex-start; align-items: flex-start; padding-top: calc(16px + var(--safe-top)); text-shadow: 0 1px 4px rgba(0, 0, 0, .8); text-align: left; padding-right: 140px; }
.call-info.over-video .avatar { display: none; }
.call-info.over-video .call-name { font-size: 22px; }
.call-info.over-video .call-state { font-size: 16px; }
.call-name { font-size: 32px; font-weight: 700; }
.call-state { font-size: 19px; opacity: .85; }
.call-controls { position: relative; z-index: 3; display: flex; justify-content: center; align-items: flex-start; gap: 18px; flex-wrap: wrap; padding: 16px 16px calc(30px + var(--safe-bottom)); }
.call-screen.has-video .call-controls { background: linear-gradient(transparent, rgba(0, 0, 0, .6)); }
.call-btn-wrap { display: flex; flex-direction: column; align-items: center; gap: 7px; font-size: 14px; width: 76px; text-align: center; }
.call-btn { width: 68px; height: 68px; border-radius: 50%; display: grid; place-items: center; background: rgba(255, 255, 255, .17); color: #fff; }
.call-btn .icon { width: 30px; height: 30px; }
.call-btn.off { background: #fff; color: #1A1512; }
.call-btn.hangup { background: var(--red); }
.call-btn.accept { background: #2FA76E; animation: pulse 1.4s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(47, 167, 110, .6); } 100% { box-shadow: 0 0 0 22px rgba(47, 167, 110, 0); } }
@media (prefers-reduced-motion: reduce) { .call-btn.accept { animation: none; } }
.hidden { display: none !important; }
