:root {
  color-scheme: light;

  --bg: #f7f7f7;
  --surface: #ffffff;
  --surface-2: #f0f0f0;
  --surface-3: #e8e8e8;

  --text: #111111;
  --text-2: #555555;
  --text-3: #888888;

  --border: #dedede;
  --border-strong: #c8c8c8;

  --accent: #111111;
  --accent-hover: #000000;
  --accent-fg: #ffffff;

  --danger: #b3261e;

  --radius-sm: 10px;
  --radius-md: 14px;
  --radius-lg: 20px;

  --shadow-sm:
    0 1px 2px rgba(0,0,0,0.05),
    0 4px 12px rgba(0,0,0,0.04);

  --shadow-md:
    0 10px 30px rgba(0,0,0,0.10);

  --ease: cubic-bezier(.22,1,.36,1);
  --press: scale(.97);
}

[data-theme="dark"] {
  color-scheme: dark;

  --bg: #080808;
  --surface: #111111;
  --surface-2: #181818;
  --surface-3: #202020;

  --text: #f5f5f5;
  --text-2: #a0a0a0;
  --text-3: #707070;

  --border: #282828;
  --border-strong: #383838;

  --accent: #f5f5f5;
  --accent-hover: #ffffff;
  --accent-fg: #080808;

  --shadow-sm:
    0 1px 2px rgba(0,0,0,0.30),
    0 4px 14px rgba(0,0,0,0.25);

  --shadow-md:
    0 14px 40px rgba(0,0,0,0.45);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;

    --bg: #080808;
    --surface: #111111;
    --surface-2: #181818;
    --surface-3: #202020;

    --text: #f5f5f5;
    --text-2: #a0a0a0;
    --text-3: #707070;

    --border: #282828;
    --border-strong: #383838;

    --accent: #f5f5f5;
    --accent-hover: #ffffff;
    --accent-fg: #080808;
  }
}

/* ---------- Reset ---------- */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;

  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
}

html,
body,
button,
a,
img,
svg,
.avatar,
.room-item,
.icon-btn,
.btn {
  -webkit-user-select: none;
  user-select: none;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;

  background: var(--bg);
  color: var(--text);

  font-family:
    Inter,
    ui-sans-serif,
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    sans-serif;

  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* Only actual text-entry areas may be selected */
input,
textarea,
[contenteditable="true"] {
  -webkit-user-select: text;
  user-select: text;
}

/* Prevent browser selection visuals */
::selection {
  background: transparent;
  color: inherit;
}
::-moz-selection {
  background: transparent;
  color: inherit;
}

button,
a,
input,
textarea,
select {
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
}

img {
  -webkit-user-drag: none;
  user-drag: none;
}

/* ---------- Layout ---------- */
#app {
  width: 100%;
  height: 100%;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.hidden {
  display: none !important;
}

/* ---------- Scrollbars ---------- */
* {
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}
::-webkit-scrollbar-track {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 999px;
}
::-webkit-scrollbar-thumb:hover {
  background: var(--text-3);
}

/* ---------- Animations ---------- */
@keyframes appear {
  from {
    opacity: 0;
    transform: translateY(6px) scale(.985);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}
@keyframes message-in {
  from {
    opacity: 0;
    transform: translateY(5px) scale(.985);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}
@keyframes modal-in {
  from {
    opacity: 0;
    transform: translateY(12px) scale(.97);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.room-item {
  transition:
    background-color .18s var(--ease),
    transform .18s var(--ease);
}
.room-item:active {
  transform: scale(.985);
}
.btn,
.icon-btn {
  transition:
    background-color .18s var(--ease),
    color .18s var(--ease),
    transform .16s var(--ease),
    opacity .18s var(--ease);
}
.btn:active,
.icon-btn:active {
  transform: var(--press);
}
.msg {
  animation: message-in .22s var(--ease);
}
.modal {
  animation: modal-in .25s var(--ease);
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .5rem;
  min-height: 44px;
  padding: 0 1rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  font-size: .9rem;
  cursor: pointer;
}
.btn:hover {
  background: var(--surface-2);
  border-color: var(--border-strong);
}
.btn:disabled {
  opacity: .5;
  pointer-events: none;
}
.btn.primary {
  background: var(--accent);
  color: var(--accent-fg);
  border-color: var(--accent);
}
.btn.primary:hover {
  background: var(--accent-hover);
  border-color: var(--accent-hover);
}
.btn.ghost {
  background: transparent;
  border-color: transparent;
}
.btn.ghost:hover {
  background: var(--surface-2);
}
.icon-btn {
  width: 44px;
  height: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text-3);
  cursor: pointer;
}
.icon-btn:hover {
  color: var(--text);
  background: var(--surface-2);
}

/* ---------- Login ---------- */
.login-view {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background: var(--bg);
}
.login-card {
  width: 100%;
  max-width: 380px;
  padding: 2rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
}
.login-card h1 {
  font-size: 1.8rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}
.login-card p {
  color: var(--text-2);
  margin-top: .25rem;
  margin-bottom: 1.5rem;
}
.login-card label {
  display: block;
  margin-bottom: 1rem;
}
.login-card label span {
  display: block;
  font-size: .75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-2);
  margin-bottom: .3rem;
}
.login-card input {
  width: 100%;
  padding: .7rem .8rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: .95rem;
}
.login-card input:focus {
  border-color: var(--border-strong);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--text) 10%, transparent);
}
.error {
  color: var(--danger);
  font-size: .85rem;
  margin-bottom: .75rem;
}

/* ---------- Sidebar ---------- */
#sidebar {
  width: 320px;
  max-width: 100%;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  min-height: 0;
}
.sidebar-header {
  display: flex;
  align-items: center;
  gap: .3rem;
  padding: .5rem .8rem;
  border-bottom: 1px solid var(--border);
}
.sidebar-title {
  flex: 1;
  font-weight: 600;
  font-size: 1.1rem;
  padding-left: .2rem;
}
.search-wrap {
  padding: .5rem .8rem;
}
#room-search {
  width: 100%;
  padding: .6rem .8rem;
  background: var(--bg);
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: .9rem;
}
#room-search:focus {
  border-color: var(--border-strong);
  background: var(--surface);
}
.room-list {
  flex: 1;
  overflow-y: auto;
  list-style: none;
}
.room-item {
  display: flex;
  align-items: center;
  gap: .7rem;
  padding: .6rem .8rem;
  cursor: pointer;
}
.room-item:hover {
  background: var(--surface-2);
}
.room-item.active {
  background: var(--surface-2);
  border-left: 3px solid var(--accent);
}
.room-item .avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--surface-3);
}
.room-info {
  flex: 1;
  min-width: 0;
}
.room-info .name {
  font-weight: 600;
  font-size: .95rem;
}
.room-info .preview {
  font-size: .8rem;
  color: var(--text-2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.room-meta-right {
  text-align: right;
  flex-shrink: 0;
}
.room-meta-right .time {
  font-size: .7rem;
  color: var(--text-3);
}
.badge {
  display: inline-block;
  background: var(--accent);
  color: var(--accent-fg);
  font-size: .7rem;
  font-weight: 700;
  padding: 0 .4rem;
  border-radius: 999px;
  min-width: 1.2rem;
  text-align: center;
  margin-top: .2rem;
}
.lock {
  font-size: .7rem;
  opacity: .7;
}

/* ---------- Chat panel ---------- */
#chat-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  min-width: 0;
  min-height: 0;
}
.empty-state {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-3);
}
#chat-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.chat-header {
  display: flex;
  align-items: center;
  gap: .7rem;
  padding: .5rem .8rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.chat-header .avatar {
  width: 40px;
  height: 40px;
}
.room-meta {
  flex: 1;
  min-width: 0;
}
.room-meta h2 {
  font-size: 1rem;
  font-weight: 600;
}
.room-meta .muted {
  font-size: .75rem;
  color: var(--text-2);
}

.message-list {
  flex: 1;
  overflow-y: auto;
  padding: .8rem;
  display: flex;
  flex-direction: column;
  gap: .2rem;
}
.msg {
  max-width: 75%;
  padding: .6rem .8rem;
  border-radius: 15px;
  line-height: 1.4;
  word-break: break-word;
}
.msg.mine {
  align-self: flex-end;
  background: var(--accent);
  color: var(--accent-fg);
  border-bottom-right-radius: 5px;
}
.msg.theirs {
  align-self: flex-start;
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 5px;
}
.msg .sender {
  font-size: .7rem;
  font-weight: 600;
  color: var(--text-2);
  margin-bottom: .2rem;
}
.msg .time {
  font-size: .6rem;
  opacity: .6;
  text-align: right;
  margin-top: .2rem;
}
.msg .body a {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
}
.msg.redacted {
  opacity: .5;
  font-style: italic;
}
.msg .reply-preview {
  font-size: .8rem;
  opacity: .7;
  border-left: 2px solid currentColor;
  padding-left: .4rem;
  margin-bottom: .2rem;
}
.msg .enc-badge {
  font-size: .6rem;
  opacity: .6;
  margin-top: .2rem;
}
.typing {
  padding: .2rem 1rem;
  color: var(--text-3);
  font-size: .8rem;
  animation: pulse 1.5s infinite;
}

.composer {
  display: flex;
  gap: .5rem;
  padding: .5rem .8rem;
  background: var(--surface);
  border-top: 1px solid var(--border);
}
#message-input {
  flex: 1;
  padding: .7rem 1rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg);
  color: var(--text);
  font-size: .95rem;
}
#message-input:focus {
  border-color: var(--border-strong);
}

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: rgba(0,0,0,0.4);
  backdrop-filter: blur(6px);
}
.modal {
  width: 100%;
  max-width: 420px;
  max-height: 85vh;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-md);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.2rem;
  border-bottom: 1px solid var(--border);
}
.modal-header h3 {
  font-size: 1.1rem;
  font-weight: 600;
}
.modal-body {
  padding: 1.2rem;
  overflow-y: auto;
}
.modal-body label {
  display: block;
  margin-bottom: 1rem;
}
.modal-body label span {
  display: block;
  font-size: .75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-2);
  margin-bottom: .3rem;
}
.modal-body input {
  width: 100%;
  padding: .7rem .8rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: .95rem;
}
.modal-body input:focus {
  border-color: var(--border-strong);
}
.actions {
  display: flex;
  justify-content: flex-end;
  gap: .5rem;
  margin-top: 1rem;
}
.dm-result {
  padding: .5rem .6rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background .15s;
}
.dm-result:hover {
  background: var(--surface-2);
}
.dm-result .dm-id {
  font-size: .75rem;
  color: var(--text-3);
}

/* ---------- Mobile ---------- */
.mobile-only {
  display: none;
}
@media (max-width: 720px) {
  #sidebar {
    width: 100%;
  }
  #main-view.chat-open #sidebar {
    display: none;
  }
  #main-view:not(.chat-open) #chat-panel {
    display: none;
  }
  .mobile-only {
    display: flex;
  }
  .msg {
    max-width: 88%;
  }
}