:root {
  --bg: #07080c;
  --bg-elev: #0e1118;
  --bg-soft: #141824;
  --line: rgba(255,255,255,.06);
  --text: #eef1f6;
  --muted: #8b93a7;
  --accent: #3d7eff;
  --accent-2: #22d3ee;
  --danger: #ff4d6d;
  --ok: #34d399;
  --warn: #fbbf24;
  --radius: 16px;
  --shadow: 0 20px 50px rgba(0,0,0,.45);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: "Sora", "Inter", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.login-body { overflow: auto; min-height: 100%; }

a { color: inherit; text-decoration: none; }
button { font-family: inherit; cursor: pointer; border: 0; background: none; color: inherit; }
input, textarea, select { font-family: inherit; color: inherit; font-size: 16px; }

/* LOGIN */
.login-wrap {
  min-height: 100%;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(61,126,255,.18), transparent 50%),
    radial-gradient(800px 400px at 100% 100%, rgba(34,211,238,.12), transparent 45%),
    var(--bg);
  padding: 24px;
  padding: max(24px, env(safe-area-inset-top)) 20px max(24px, env(safe-area-inset-bottom));
}
.login-card {
  width: min(440px, 100%);
  background: linear-gradient(180deg, rgba(255,255,255,.04), rgba(255,255,255,.02));
  border: 1px solid var(--line);
  border-radius: 24px;
  padding: 36px 32px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(18px);
}
.brand {
  display: flex; align-items: center; gap: 12px; margin-bottom: 28px;
}
.logo {
  width: 42px; height: 42px; border-radius: 12px;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  display: grid; place-items: center; font-weight: 700;
}
.brand h1 { font-size: 20px; letter-spacing: -.3px; }
.brand p { color: var(--muted); font-size: 12px; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.field input, .field select {
  width: 100%; background: var(--bg-soft); border: 1px solid var(--line);
  border-radius: 12px; padding: 12px 14px; outline: none;
}
.field input:focus, .field select:focus { border-color: var(--accent); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; padding: 13px 16px; border-radius: 12px; font-weight: 600;
  background: linear-gradient(135deg, var(--accent), #2563eb);
  box-shadow: 0 8px 24px rgba(61,126,255,.28);
}
.btn:hover { filter: brightness(1.06); }
.hint { margin-top: 14px; font-size: 12px; color: var(--muted); text-align: center; }
.form-error { color: var(--danger); font-size: 13px; margin-bottom: 12px; }
.flash {
  margin: 0 0 18px; padding: 12px 14px; border-radius: 12px; font-size: 13px; line-height: 1.45;
  border: 1px solid var(--line);
}
.flash.ok { background: rgba(52,211,153,.12); color: #a7f3d0; }
.flash.err { background: rgba(255,77,109,.12); color: #fda4af; }

.empty-list {
  color: var(--muted); font-size: 13px; padding: 24px 18px; line-height: 1.5;
}
.empty-list a { color: var(--accent); }

/* APP SHELL */
.app {
  display: grid;
  grid-template-columns: 88px 320px 1fr;
  height: 100vh;
  height: 100dvh;
}
.rail {
  background: #0a0c12;
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column; align-items: center;
  padding: 16px 0; gap: 10px;
}
.rail .logo { margin-bottom: 10px; }
.rail-btn {
  width: 48px; height: 48px; border-radius: 14px; color: var(--muted);
  display: grid; place-items: center;
}
.rail-btn.active, .rail-btn:hover { background: var(--bg-soft); color: var(--text); }
.rail-spacer { flex: 1; }
.avatar {
  width: 40px; height: 40px; border-radius: 50%;
  background: linear-gradient(135deg, #6366f1, #22d3ee);
  display: grid; place-items: center; font-size: 13px; font-weight: 700;
}

.sidebar {
  background: var(--bg-elev);
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column;
}
.side-head { padding: 18px 18px 10px; }
.side-head h2 { font-size: 18px; }
.side-head small { color: var(--muted); display: block; margin-top: 4px; line-height: 1.4; }
.search {
  margin: 8px 16px 12px;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  display: flex; gap: 8px; align-items: center; color: var(--muted);
}
.search input { background: none; border: 0; outline: none; width: 100%; color: var(--text); }
.chat-list { overflow: auto; flex: 1; }
.chat-item {
  display: flex; gap: 12px; padding: 12px 16px; cursor: pointer; align-items: center;
}
.chat-item:hover, .chat-item.active { background: rgba(255,255,255,.04); }
.chat-item .meta { flex: 1; min-width: 0; }
.chat-item .name { display: flex; justify-content: space-between; font-size: 14px; }
.chat-item .preview { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); }
.dot.off { background: #4b5563; }
.badge {
  min-width: 18px; height: 18px; border-radius: 99px; background: var(--accent);
  font-size: 10px; display: grid; place-items: center; padding: 0 5px;
}

.stage { display: flex; flex-direction: column; min-width: 0; background: #090b10; }
.topbar {
  height: 68px; border-bottom: 1px solid var(--line);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 20px; background: rgba(14,17,24,.8); backdrop-filter: blur(12px);
  gap: 8px;
}
.back-btn { display: none; font-size: 28px; line-height: 1; flex-shrink: 0; }
.peer { min-width: 0; flex: 1; display: flex; align-items: center; gap: 12px; }
.peer strong, .peer small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.peer small { color: var(--muted); }
.actions { display: flex; gap: 8px; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px; background: var(--bg-soft);
  display: grid; place-items: center; color: var(--text);
}
.icon-btn.danger { background: rgba(255,77,109,.12); color: var(--danger); }
.icon-btn.warn { color: var(--warn); }

.messages {
  flex: 1; overflow: auto; padding: 24px;
  background-image:
    radial-gradient(circle at 1px 1px, rgba(255,255,255,.03) 1px, transparent 0);
  background-size: 22px 22px;
}
.bubble-row { display: flex; margin-bottom: 10px; }
.bubble-row.me { justify-content: flex-end; }
.bubble {
  max-width: min(68%, 520px);
  padding: 10px 12px;
  border-radius: 16px;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  font-size: 14px; line-height: 1.45;
}
.bubble.me {
  background: linear-gradient(180deg, #1e3a8a, #1d4ed8);
  border-color: transparent;
}
.bubble .time { display: block; margin-top: 4px; font-size: 10px; color: rgba(255,255,255,.55); text-align: right; }

.composer-wrap {
  border-top: 1px solid var(--line);
  background: var(--bg-elev);
}
.composer {
  padding: 10px 12px 14px;
  display: flex; gap: 8px; align-items: flex-end;
}
.composer textarea {
  flex: 1; min-height: 44px; max-height: 120px; resize: none;
  background: var(--bg-soft); border: 1px solid var(--line);
  border-radius: 14px; padding: 12px 14px; outline: none;
}
.send { width: 44px; height: 44px; border-radius: 14px; background: var(--accent); display: grid; place-items: center; flex-shrink: 0; }
.gif-chip {
  height: 44px; min-width: 44px; padding: 0 8px; border-radius: 12px;
  background: var(--bg-soft); color: var(--accent-2); font-size: 11px; font-weight: 700;
  letter-spacing: .04em; flex-shrink: 0;
}
.gif-chip.on, .composer .icon-btn.on { background: rgba(61,126,255,.2); color: var(--text); }

.media-tray {
  border-bottom: 1px solid var(--line);
  background: #0b0e14;
  max-height: 280px;
  display: flex; flex-direction: column;
}
.tray-tabs {
  display: flex; gap: 6px; padding: 8px 10px 0;
}
.tray-tab {
  padding: 8px 12px; border-radius: 10px; font-size: 12px; font-weight: 600;
  color: var(--muted);
}
.tray-tab.on { background: var(--bg-soft); color: var(--text); }
.tray-search { padding: 8px 10px 0; }
.tray-search input {
  width: 100%; background: var(--bg-soft); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 12px; outline: none;
}
.tray-body {
  overflow: auto; padding: 8px 10px 10px;
  flex: 1; min-height: 160px;
}
.emoji-grid, .sticker-grid, .gif-grid {
  display: grid; gap: 4px;
}
.emoji-grid { grid-template-columns: repeat(8, 1fr); }
.sticker-grid { grid-template-columns: repeat(5, 1fr); }
.gif-grid { grid-template-columns: repeat(3, 1fr); gap: 6px; }
.emoji-btn, .sticker-btn {
  height: 38px; border-radius: 8px; font-size: 22px;
  display: grid; place-items: center;
}
.sticker-btn { height: 64px; font-size: 36px; background: var(--bg-soft); }
.emoji-btn:hover, .sticker-btn:hover { background: var(--bg-soft); }
.gif-btn {
  border-radius: 10px; overflow: hidden; background: var(--bg-soft); aspect-ratio: 1;
}
.gif-btn img { width: 100%; height: 100%; object-fit: cover; display: block; }
.emoji-cats { display: flex; gap: 4px; overflow: auto; padding-bottom: 6px; }
.emoji-cats button { flex: 0 0 auto; padding: 4px 8px; font-size: 16px; opacity: .7; }
.emoji-cats button.on { opacity: 1; }

.bubble.media { padding: 6px; background: var(--bg-soft); }
.bubble.media.me { background: linear-gradient(180deg, #1e3a8a, #1d4ed8); }
.bubble.media img {
  display: block; max-width: 220px; max-height: 220px; width: 100%;
  border-radius: 12px; object-fit: cover;
}
.bubble.sticker {
  background: transparent; border: 0; font-size: 56px; line-height: 1; padding: 4px;
}
.bubble.sticker .time { font-size: 10px; }

/* CALL OVERLAY */
.call-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.72);
  display: none; place-items: center; z-index: 50;
}
.call-overlay.show { display: grid; }
.call-box {
  width: min(720px, 92vw); background: #0c1018; border: 1px solid var(--line);
  border-radius: 24px; overflow: hidden; box-shadow: var(--shadow);
}
.call-video {
  height: 380px; background: #05070b; display: grid; place-items: center;
  position: relative;
}
.self-cam {
  position: absolute; right: 16px; bottom: 16px; width: 140px; height: 100px;
  border-radius: 12px; background: #1f2937; border: 1px solid var(--line);
}
.call-controls { display: flex; justify-content: center; gap: 12px; padding: 18px; }

/* TOAST / IDLE */
.toast {
  position: fixed; right: 20px; bottom: 20px; background: #111827;
  border: 1px solid var(--line); padding: 12px 14px; border-radius: 12px;
  display: none; z-index: 80; max-width: 280px;
}
.idle-bar {
  position: fixed; top: 0; left: 0; height: 3px; width: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  transform-origin: left; transform: scaleX(0); z-index: 90;
}

.settings-drawer {
  position: fixed; right: 0; top: 0; height: 100%; width: min(380px, 100%);
  background: var(--bg-elev); border-left: 1px solid var(--line);
  transform: translateX(100%); transition: .25s ease; z-index: 40; padding: 22px;
  overflow: auto;
}
.settings-drawer.open { transform: none; }
.settings-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.settings-drawer h3 { margin: 0; }
.row { display: flex; justify-content: space-between; align-items: center; padding: 12px 0; border-bottom: 1px solid var(--line); gap: 12px; }
.row small { color: var(--muted); display: block; }

.dock {
  display: none;
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 70;
  background: #0a0c12;
  border-top: 1px solid var(--line);
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
  grid-template-columns: repeat(auto-fit, minmax(64px, 1fr));
  gap: 4px;
}
.dock a, .dock button {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; min-height: 48px; border-radius: 12px; color: var(--muted);
  font-size: 10px; font-weight: 600; letter-spacing: .02em;
}
.dock span { font-size: 18px; line-height: 1; }
.dock a.on, .dock a:hover, .dock button:hover { color: var(--text); background: var(--bg-soft); }

/* ADMIN */
.admin-body { overflow: auto; }
.admin-app { grid-template-columns: 88px 1fr; height: auto; min-height: 100vh; }
.admin-main { padding: 28px 32px 56px; min-width: 0; }
.admin-head {
  display: flex; justify-content: space-between; align-items: flex-start;
  gap: 16px; margin-bottom: 22px;
}
.admin-head h1 { font-size: 22px; letter-spacing: -.3px; }
.admin-head p { color: var(--muted); font-size: 13px; margin-top: 6px; max-width: 520px; }
.admin-grid {
  display: grid; grid-template-columns: minmax(280px, 340px) 1fr; gap: 20px; align-items: start;
}
.card {
  background: var(--bg-elev); border: 1px solid var(--line);
  border-radius: 20px; padding: 20px;
}
.card h2 { font-size: 15px; margin-bottom: 16px; }
.pill {
  display: inline-flex; align-items: center;
  padding: 4px 10px; border-radius: 99px; font-size: 11px; font-weight: 600;
  background: var(--bg-soft); color: var(--muted); text-transform: uppercase; letter-spacing: .04em;
}
.pill.admin { background: rgba(61,126,255,.16); color: #93c5fd; }
.table-wrap { overflow: auto; }
.user-table { width: 100%; border-collapse: collapse; }
.user-table th {
  text-align: left; font-size: 11px; color: var(--muted); font-weight: 600;
  padding: 0 8px 10px; border-bottom: 1px solid var(--line); text-transform: uppercase; letter-spacing: .04em;
}
.user-table td {
  padding: 12px 8px; border-bottom: 1px solid var(--line); font-size: 13px; vertical-align: top;
}
.user-table td strong { display: block; }
.user-table td small { color: var(--muted); }
.user-table .acts { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.user-table form { display: inline-flex; gap: 6px; align-items: center; }
.inline-reset input {
  width: 120px; background: var(--bg-soft); border: 1px solid var(--line);
  border-radius: 8px; padding: 6px 8px; outline: none; font-size: 12px;
}
.mini {
  padding: 6px 10px; border-radius: 8px; font-size: 11px; font-weight: 600;
  background: var(--accent); color: #fff;
}
.mini.ghost { background: var(--bg-soft); color: var(--text); }
.mini.danger { background: rgba(255,77,109,.16); color: var(--danger); }
.pw-row { display: flex; gap: 8px; align-items: center; }
.pw-row input { flex: 1; }
.muted { color: var(--muted); font-size: 12px; }

@media (max-width: 1100px) {
  .admin-grid { grid-template-columns: 1fr; }
}
@media (max-width: 900px) {
  .app {
    grid-template-columns: 1fr;
    height: 100dvh;
    padding-bottom: calc(62px + env(safe-area-inset-bottom));
  }
  .admin-app { grid-template-columns: 1fr; min-height: 100dvh; }
  .rail { display: none; }
  .sidebar { display: flex; min-height: 0; }
  .stage {
    position: fixed;
    inset: 0;
    z-index: 30;
    transform: translateX(100%);
    transition: transform .22s ease;
  }
  body.chat-open .stage { transform: none; }
  body.chat-open .app { padding-bottom: 0; }
  body.chat-open .dock { display: none; }
  .back-btn { display: grid; }
  .topbar {
    height: auto;
    min-height: 56px;
    padding: calc(8px + env(safe-area-inset-top)) 10px 8px 8px;
  }
  .side-head { padding: calc(14px + env(safe-area-inset-top)) 16px 10px; }
  .chat-item { padding: 14px 16px; min-height: 64px; }
  .messages { padding: 16px 12px; }
  .bubble { max-width: 82%; }
  .composer {
    padding: 8px 10px calc(10px + env(safe-area-inset-bottom));
  }
  .media-tray { max-height: 42vh; }
  .emoji-grid { grid-template-columns: repeat(8, 1fr); }
  .gif-grid { grid-template-columns: repeat(2, 1fr); }
  .bubble.media img { max-width: min(220px, 70vw); }
  .dock { display: grid; }
  .toast { left: 12px; right: 12px; bottom: calc(76px + env(safe-area-inset-bottom)); max-width: none; }
  .call-box { width: 100vw; height: 100dvh; max-width: none; border-radius: 0; display: flex; flex-direction: column; }
  .call-video { flex: 1; height: auto; }
  .settings-drawer { width: 100%; padding-top: calc(18px + env(safe-area-inset-top)); }
  .admin-main { padding: 16px 14px calc(88px + env(safe-area-inset-bottom)); }
  .admin-head { flex-direction: column; }
  .login-card { padding: 28px 20px; border-radius: 20px; }
  .user-table thead { display: none; }
  .user-table, .user-table tbody, .user-table tr, .user-table td { display: block; width: 100%; }
  .user-table tr {
    padding: 12px 0;
    border-bottom: 1px solid var(--line);
  }
  .user-table td { padding: 4px 0; border: 0; }
  .user-table .acts { margin-top: 8px; }
  .inline-reset { flex-wrap: wrap; width: 100%; }
  .inline-reset input { width: 100%; min-height: 44px; }
  .mini { min-height: 40px; padding: 10px 12px; }
}
