:root {
  color-scheme: dark;
  --bg: #101114;
  --panel: #17191e;
  --panel-2: #20232a;
  --line: #2d3038;
  --text: #f5f2eb;
  --muted: #999da8;
  --accent: #e7ff55;
  --accent-ink: #171900;
  --telegram: #4fa8e8;
  --field: #0f1013;
  --topbar: #17191ef2;
  --danger: #ff7979;
  --pattern: #ffffff08;
  --app-height: 100dvh;
  --viewport-offset-top: 0px;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
[data-theme="light"] {
  color-scheme: light;
  --bg: #f1eee5;
  --panel: #fffdf7;
  --panel-2: #ece8dd;
  --line: #d2ccbd;
  --text: #24231f;
  --muted: #706d64;
  --accent: #6d39ff;
  --accent-ink: #fff;
  --telegram: #0878b9;
  --field: #fff;
  --topbar: #fffdf7eb;
  --danger: #c73030;
  --pattern: #00000009;
}
[data-theme="terminal"] {
  color-scheme: dark;
  --bg: #001408;
  --panel: #001d0c;
  --panel-2: #052a14;
  --line: #0b6331;
  --text: #a8ffbd;
  --muted: #5cb276;
  --accent: #3cff6f;
  --accent-ink: #001408;
  --telegram: #59ffa9;
  --field: #001006;
  --topbar: #001d0cf2;
  --danger: #ff6580;
  --pattern: #3cff6f0b;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}
* { box-sizing: border-box; }
html, body { margin: 0; width: 100%; min-height: 100%; overscroll-behavior: none; }
body { background: var(--bg); color: var(--text); min-height: 100svh; }
button, input, textarea, select { font: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); line-height: 1.5; }
.eyebrow { color: var(--accent); font-weight: 800; font-size: 11px; letter-spacing: .16em; margin: 0 0 8px; }
.error { min-height: 1.2em; color: var(--danger); font-size: 13px; }
.login-view { min-height: var(--app-height, 100dvh); display: grid; place-items: center; padding: max(24px, env(safe-area-inset-top)) max(24px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(24px, env(safe-area-inset-left)); background: radial-gradient(circle at 50% 0, color-mix(in srgb, var(--accent) 14%, var(--bg)) 0, transparent 42%); }
.login-card { width: min(100%, 390px); background: rgba(23,25,30,.94); border: 1px solid var(--line); padding: 36px; border-radius: 24px; box-shadow: 0 26px 70px #0008; }
.login-card h1 { font-size: 42px; margin: 8px 0 4px; letter-spacing: -.05em; }
.brand-mark { position: relative; width: 54px; height: 54px; display: grid; place-items: center; background: var(--accent); color: var(--accent-ink); border-radius: 16px; font-weight: 950; transform: rotate(-4deg); }
.brand-mark.small { width: 38px; height: 38px; border-radius: 11px; font-size: 14px; }
.unread-badge { position: absolute; z-index: 2; right: -9px; top: -8px; min-width: 20px; height: 20px; display: grid; place-items: center; padding: 0 5px; border: 2px solid var(--panel); border-radius: 999px; color: #fff; background: #f04455; box-shadow: 0 3px 10px #0007; font: 900 10px/1 ui-sans-serif, system-ui, sans-serif; transform: rotate(4deg); }
label { display: grid; gap: 7px; color: #cacdd4; font-size: 13px; margin-top: 17px; }
input, textarea, select { width: 100%; color: var(--text); background: var(--field); border: 1px solid var(--line); border-radius: 11px; padding: 13px; outline: none; }
input:focus, textarea:focus, select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px #e7ff5519; }
.login-card > button, .user-form > button { width: 100%; border: 0; background: var(--accent); color: var(--accent-ink); padding: 14px; border-radius: 11px; font-weight: 800; margin-top: 20px; }
.chat-view { height: var(--app-height, 100dvh); display: grid; grid-template-rows: 64px minmax(0, 1fr) auto auto auto; max-width: 1000px; margin: auto; border-inline: 1px solid var(--line); background: var(--panel); overflow: hidden; transform: translateY(var(--viewport-offset-top, 0px)); }
.topbar { display: flex; align-items: center; justify-content: space-between; padding: 0 18px; border-bottom: 1px solid var(--line); background: var(--topbar); backdrop-filter: blur(14px); }
.brand, .top-actions { display: flex; align-items: center; gap: 12px; }
.brand > div:last-child { display: grid; }
.brand small, #connection-state { color: var(--muted); font-size: 11px; margin-top: 2px; }
#connection-state.online { color: var(--accent); }
#current-user { font-size: 13px; color: #c5c8cf; }
.icon-button { border: 1px solid var(--line); color: var(--text); background: var(--panel-2); border-radius: 9px; width: 36px; height: 36px; font-size: 20px; }
.busted-gear { position: relative; transform: rotate(-14deg); color: var(--accent); text-shadow: 2px 1px 0 color-mix(in srgb, var(--danger) 65%, transparent); }
.busted-gear::after { content: "╱"; position: absolute; right: 4px; top: -3px; color: var(--danger); font-size: 12px; font-weight: 900; transform: rotate(25deg); }
.busted-gear:hover { transform: rotate(18deg) scale(1.06); transition: transform .18s ease; }
.logout-button { border: 1px solid #ff797955; color: #ff9a9a; background: #ff797910; border-radius: 9px; height: 36px; padding: 0 12px; font-size: 12px; font-weight: 800; white-space: nowrap; }
.logout-button:hover { background: #ff797920; border-color: #ff797988; }
.messages { min-height: 0; overflow-y: auto; overscroll-behavior-y: contain; -webkit-overflow-scrolling: touch; padding: 24px clamp(12px, 4vw, 40px); display: flex; flex-direction: column; gap: 15px; background-image: radial-gradient(var(--pattern) 1px, transparent 1px); background-size: 22px 22px; }
.load-more { align-self: center; color: var(--muted); background: var(--panel-2); border: 1px solid var(--line); padding: 8px 14px; border-radius: 999px; }
.message { display: grid; grid-template-columns: 36px minmax(0, 1fr); gap: 10px; max-width: 78%; min-width: 0; align-self: flex-start; }
.message.web { align-self: flex-end; grid-template-columns: minmax(0, 1fr); }
.message.web .avatar { display: none; }
.avatar { width: 36px; height: 36px; border-radius: 12px; display: grid; place-items: center; background: #2c3038; color: var(--telegram); font-weight: 800; font-size: 12px; }
.bubble { min-width: 0; background: var(--panel-2); border: 1px solid var(--line); border-radius: 4px 16px 16px 16px; padding: 10px 12px 8px; box-shadow: 0 5px 18px #0002; overflow: hidden; }
.web .bubble { background: color-mix(in srgb, var(--accent) 14%, var(--panel-2)); border-color: color-mix(in srgb, var(--accent) 35%, var(--line)); border-radius: 16px 4px 16px 16px; }
.message.mentioned .bubble { border: 2px solid var(--accent); box-shadow: 0 0 0 4px #e7ff5520, 0 8px 28px #0006; }
.message.mentioned { animation: mention-in .35s ease-out; }
@keyframes mention-in { from { transform: translateY(5px); opacity: .7; } }
.message-meta { display: flex; align-items: baseline; justify-content: space-between; gap: 14px; }
.sender { color: var(--telegram); font-size: 13px; }
.web .sender { color: var(--accent); }
.mention-badge { color: var(--accent-ink); background: var(--accent); border-radius: 999px; padding: 2px 6px; font-size: 8px; font-weight: 900; letter-spacing: .06em; }
time { color: var(--muted); font-size: 10px; white-space: nowrap; }
.body { white-space: pre-wrap; overflow-wrap: anywhere; word-break: break-word; margin: 6px 0 3px; line-height: 1.42; }
.body:empty { display: none; }
.reply-button { float: right; opacity: 0; border: 0; background: transparent; color: var(--muted); padding: 3px 0 0 10px; font-size: 11px; }
.message:hover .reply-button { opacity: 1; }
.quoted { display: block; width: 100%; text-align: left; margin-top: 7px; padding: 6px 8px; border: 0; border-left: 3px solid var(--accent); border-radius: 4px; color: #b9bdc7; background: #0002; font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.attachment { margin-top: 8px; }
.attachment img, .attachment video { display: block; max-width: min(520px, 100%); max-height: 480px; border-radius: 10px; }
.attachment audio { width: min(360px, 100%); }
.file-link { color: var(--text); display: flex; gap: 10px; align-items: center; padding: 10px; border: 1px solid #ffffff18; border-radius: 10px; text-decoration: none; }
.reply-bar { display: flex; justify-content: space-between; align-items: center; background: var(--panel-2); border-top: 1px solid var(--line); border-left: 3px solid var(--accent); padding: 8px 16px; }
.reply-bar > div { display: grid; min-width: 0; }
.reply-bar small { color: var(--muted); }
#reply-preview { color: #bbbfc8; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; max-width: 70vw; }
.composer { min-height: 72px; display: flex; align-items: flex-end; gap: 10px; padding: 12px 16px; border-top: 1px solid var(--line); background: var(--panel); padding-bottom: max(12px, env(safe-area-inset-bottom)); }
.attach-button { display: grid; place-items: center; flex: 0 0 44px; height: 44px; margin: 0; border-radius: 12px; background: var(--panel-2); border: 1px solid var(--line); font-size: 24px; cursor: pointer; }
.attach-button input { display: none; }
.compose-field { flex: 1; min-width: 0; }
textarea { resize: none; max-height: 140px; min-height: 44px; padding: 11px 13px; }
.file-name { display: block; color: var(--accent); font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.compose-field { position: relative; }
.typing-state { min-height: 0; height: 0; overflow: hidden; padding: 0 16px; color: var(--muted); background: var(--panel); font-size: 11px; font-style: italic; transition: height .18s ease, padding .18s ease; }
.typing-state.active { height: 25px; padding-top: 6px; }
.mention-menu { position: absolute; z-index: 8; left: 0; right: 0; bottom: calc(100% + 8px); max-height: 280px; overflow-y: auto; padding: 6px; border: 1px solid var(--line); border-radius: 14px; background: color-mix(in srgb, var(--panel) 94%, transparent); box-shadow: 0 18px 50px #0008; backdrop-filter: blur(16px); }
.mention-menu button { width: 100%; display: flex; align-items: center; gap: 10px; padding: 9px; border: 0; border-radius: 9px; color: var(--text); background: transparent; text-align: left; }
.mention-menu button:hover, .mention-menu button.active { color: var(--accent-ink); background: var(--accent); }
.mention-menu button > span:first-child { flex: 0 0 32px; height: 32px; display: grid; place-items: center; border-radius: 9px; color: var(--telegram); background: var(--panel-2); font-size: 10px; font-weight: 900; }
.mention-menu button > span:last-child { display: grid; min-width: 0; }
.mention-menu small { color: var(--muted); }
.mention-menu button:hover small, .mention-menu button.active small { color: inherit; opacity: .7; }
.send-button { flex: 0 0 44px; height: 44px; border: 0; border-radius: 12px; background: var(--accent); color: var(--accent-ink); font-size: 19px; font-weight: 900; }
.send-button:disabled { opacity: .5; cursor: wait; }
dialog { width: min(680px, calc(100% - 24px)); max-height: calc(100dvh - 32px); color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: 20px; padding: 24px; }
dialog::backdrop { background: #000b; backdrop-filter: blur(4px); }
.dialog-head { display: flex; justify-content: space-between; align-items: start; }
.dialog-head h2 { margin: 0; }
.user-form { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; border-bottom: 1px solid var(--line); padding-bottom: 22px; }
.user-form .checkbox { display: flex; align-items: center; gap: 8px; }
.checkbox input { width: auto; }
.users-list { display: grid; gap: 8px; margin-top: 18px; }
.user-row { display: flex; justify-content: space-between; gap: 10px; align-items: center; padding: 10px; background: var(--panel-2); border-radius: 10px; }
.user-row-actions { display: flex; align-items: center; gap: 8px; }
.user-row-actions select { width: min(230px, 38vw); padding: 7px 9px; font-size: 12px; }
.user-row small { color: var(--muted); display: block; }
.user-row button { border: 1px solid var(--line); background: transparent; color: var(--text); border-radius: 8px; padding: 6px 9px; }
.settings-dialog { width: min(560px, calc(100% - 24px)); }
.settings-section { padding: 18px 0; border-bottom: 1px solid var(--line); }
.settings-section:last-child { border-bottom: 0; padding-bottom: 0; }
.settings-section h3 { margin: 0 0 9px; }
.settings-section p { margin: 6px 0 12px; }
.theme-picker { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.theme-picker button, .action-button { min-height: 44px; border: 1px solid var(--line); border-radius: 11px; padding: 9px 12px; color: var(--text); background: var(--panel-2); font-weight: 750; }
.theme-picker button.active { color: var(--accent-ink); background: var(--accent); border-color: var(--accent); }
.action-button { color: var(--accent-ink); background: var(--accent); border-color: var(--accent); }
.action-button:disabled { opacity: .45; cursor: not-allowed; }
.install-help { font-size: 12px; }
.toast { position: fixed; z-index: 100; left: 50%; bottom: max(22px, env(safe-area-inset-bottom)); transform: translateX(-50%); width: max-content; max-width: calc(100% - 28px); padding: 11px 16px; border: 1px solid var(--accent); border-radius: 999px; color: var(--accent-ink); background: var(--accent); box-shadow: 0 14px 40px #0008; font-size: 13px; font-weight: 800; text-align: center; animation: toast-in .2s ease-out; }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } }
[data-theme="terminal"] .brand-mark { border-radius: 2px; transform: none; box-shadow: 0 0 18px color-mix(in srgb, var(--accent) 45%, transparent); }
[data-theme="terminal"] .bubble, [data-theme="terminal"] input, [data-theme="terminal"] textarea, [data-theme="terminal"] select, [data-theme="terminal"] button { border-radius: 2px; }
[data-theme="terminal"] .brand strong::after { content: "_"; animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }
[data-theme="light"] .login-card { box-shadow: 0 26px 70px #5c574733; }
[data-theme="light"] .reply-bar { background: #f2eee4; }
@media (max-width: 620px) {
  button, .attach-button { touch-action: manipulation; }
  input, textarea, select { font-size: 16px; }
  #current-user { display: none; }
  .chat-view { width: 100%; max-width: none; border: 0; grid-template-rows: calc(58px + env(safe-area-inset-top)) minmax(0, 1fr) auto auto auto; }
  .topbar { min-width: 0; padding: env(safe-area-inset-top) max(10px, env(safe-area-inset-right)) 0 max(10px, env(safe-area-inset-left)); }
  .brand { min-width: 0; gap: 9px; }
  .brand > div:last-child { min-width: 0; }
  .brand strong { font-size: 14px; }
  .brand small { max-width: 130px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .brand-mark.small { width: 34px; height: 34px; border-radius: 10px; font-size: 12px; }
  .top-actions { gap: 7px; }
  .logout-button { height: 34px; padding: 0 10px; font-size: 10px; }
  .icon-button { width: 34px; height: 34px; }
  .messages { padding: 14px max(9px, env(safe-area-inset-right)) 18px max(9px, env(safe-area-inset-left)); gap: 12px; }
  .message { grid-template-columns: 32px minmax(0, 1fr); gap: 7px; max-width: 96%; }
  .message.web { max-width: 89%; }
  .avatar { width: 32px; height: 32px; border-radius: 10px; font-size: 10px; }
  .bubble { min-width: 0; padding: 9px 10px 7px; border-radius: 3px 14px 14px 14px; }
  .web .bubble { border-radius: 14px 3px 14px 14px; }
  .message-meta { display: grid; grid-template-columns: minmax(0, 1fr) auto; align-items: center; gap: 3px 8px; }
  .sender { min-width: 0; overflow-wrap: anywhere; line-height: 1.15; }
  .mention-badge { grid-column: 1 / -1; grid-row: 2; justify-self: start; }
  time { grid-column: 2; grid-row: 1; }
  .body { font-size: 15px; line-height: 1.4; }
  .quoted { max-width: 100%; }
  .attachment img, .attachment video { max-height: 55dvh; }
  .reply-button { opacity: .72; min-height: 28px; padding-top: 7px; }
  .reply-bar { padding: 7px max(10px, env(safe-area-inset-right)) 7px max(12px, env(safe-area-inset-left)); }
  .composer { min-height: 60px; gap: 8px; padding: 8px max(10px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left)); }
  .attach-button, .send-button { flex-basis: 44px; width: 44px; height: 44px; border-radius: 12px; }
  textarea { min-height: 44px; padding: 10px 11px; line-height: 1.35; }
  dialog { width: 100%; max-width: none; height: var(--app-height, 100dvh); max-height: none; margin: 0; border: 0; border-radius: 0; padding: max(18px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) max(18px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left)); overflow-y: auto; }
  .dialog-head { position: sticky; top: calc(-1 * max(18px, env(safe-area-inset-top))); z-index: 2; margin: calc(-1 * max(18px, env(safe-area-inset-top))) calc(-1 * max(14px, env(safe-area-inset-right))) 0 calc(-1 * max(14px, env(safe-area-inset-left))); padding: max(18px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) 12px max(14px, env(safe-area-inset-left)); background: var(--topbar); backdrop-filter: blur(12px); }
  .dialog-head h2 { font-size: 28px; }
  .user-form { grid-template-columns: 1fr; }
  .user-row { flex-direction: column; align-items: stretch; }
  .user-row-actions { display: grid; grid-template-columns: minmax(0, 1fr) auto; }
  .user-row-actions select { width: 100%; min-width: 0; }
  .login-view { padding: max(18px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) max(18px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left)); place-items: center; }
  .login-card { width: 100%; padding: 26px 22px; border-radius: 20px; }
  .login-card h1 { font-size: clamp(36px, 12vw, 44px); }
  .theme-picker { grid-template-columns: 1fr; }
}

@media (max-width: 350px) {
  .logout-button { max-width: none; padding-inline: 7px; font-size: 9px; }
  .brand small { max-width: 108px; }
  .message { max-width: 98%; }
  .message.web { max-width: 92%; }
  .messages { padding-inline: 6px; }
  .top-actions { gap: 4px; }
  .brand { gap: 6px; }
  .brand-mark.small { width: 30px; height: 30px; }
  .icon-button { width: 32px; height: 32px; }
}

@media (max-height: 500px) and (orientation: landscape) {
  .chat-view { grid-template-rows: 50px minmax(0, 1fr) auto auto auto; }
  .topbar { padding-top: 0; }
  .brand-mark.small { width: 32px; height: 32px; }
  .messages { padding-block: 8px; }
  .composer { min-height: 52px; padding-block: 4px; }
  .attach-button, .send-button { height: 42px; }
}
