/* chat - one stylesheet, no framework, light and dark from the same tokens.
 *
 * The CSP is style-src 'self', so nothing here may depend on inline style
 * attributes; the only style the script sets is through the CSSOM (textarea
 * height, menu position, the --h avatar hue), which a CSP does not govern. */

:root {
  --bg: #ffffff;
  --bg-soft: #f6f6f4;
  --bg-hover: #efefec;
  --side: #1d2430;
  --side-text: #c9d1dc;
  --side-strong: #ffffff;
  --side-hover: #2a3342;
  --side-active: #2c5bd9;
  --text: #1c1f24;
  --muted: #646b75;
  --line: #e2e3e0;
  --accent: #2f62e6;
  --accent-text: #ffffff;
  --accent-soft: #e8eefe;
  --mention-bg: #fff3c4;
  --mention-text: #6b4f00;
  --danger: #c4312b;
  --ok: #2e9d5b;
  --focus: #2f62e6;
  --radius: 8px;
  --shadow: 0 8px 28px rgba(15, 20, 30, 0.18);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17191d;
    --bg-soft: #1e2126;
    --bg-hover: #23262c;
    --side: #101318;
    --side-text: #a9b2be;
    --side-strong: #ffffff;
    --side-hover: #1b2028;
    --side-active: #2c5bd9;
    --text: #e6e8eb;
    --muted: #9aa2ad;
    --line: #2c3037;
    --accent: #5b8cff;
    --accent-text: #0b0d10;
    --accent-soft: #1e2a45;
    --mention-bg: #4a3d12;
    --mention-text: #ffe49a;
    --danger: #ff6b63;
    --ok: #4cc27f;
    --focus: #7aa2ff;
    --shadow: 0 8px 28px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.45 var(--font); -webkit-font-smoothing: antialiased; }
button, input, textarea, select { font: inherit; color: inherit; }
a { color: var(--accent); }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
kbd { font: 12px var(--mono); border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; padding: 0 5px; }
code { font: 0.9em var(--mono); background: var(--bg-soft); border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; }
pre { margin: 4px 0; padding: 10px 12px; background: var(--bg-soft); border: 1px solid var(--line); border-radius: var(--radius); overflow-x: auto; }
pre code { border: 0; padding: 0; background: none; white-space: pre; }
blockquote { margin: 2px 0; padding-left: 10px; border-left: 3px solid var(--line); color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); }
.hint { margin: 2px 0 10px; color: var(--muted); font-size: 13px; }
.grow { flex: 1; min-width: 0; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; margin-top: 12px; }
.tag { display: inline-block; margin-left: 6px; padding: 0 6px; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; border-radius: 4px; background: var(--bg-soft); color: var(--muted); border: 1px solid var(--line); vertical-align: middle; }

/* --- buttons ---------------------------------------------------------------- */

.button { display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 34px; padding: 0 14px; border-radius: 6px; border: 1px solid var(--line); background: var(--bg); cursor: pointer; text-decoration: none; color: var(--text); font-weight: 500; }
.button:hover { background: var(--bg-hover); }
.button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.button.primary:hover { filter: brightness(1.08); }
.button.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--line)); }
.button.ghost { border-color: transparent; background: transparent; }
.button.small { min-height: 28px; padding: 0 10px; font-size: 13px; }
.button:disabled { opacity: 0.5; cursor: default; }
.icon { display: inline-flex; align-items: center; justify-content: center; width: 30px; height: 30px; border: 0; border-radius: 6px; background: transparent; cursor: pointer; font-size: 16px; line-height: 1; }
.icon:hover { background: var(--bg-hover); }
.link { border: 0; background: none; color: var(--accent); cursor: pointer; padding: 0; }

/* --- layout ----------------------------------------------------------------- */

.app { display: grid; grid-template-columns: 260px minmax(0, 1fr); height: 100vh; height: 100dvh; }
.app.thread-open { grid-template-columns: 260px minmax(0, 1fr) minmax(320px, 400px); }
.app[data-state="loading"] { visibility: hidden; }

.sidebar { display: flex; flex-direction: column; min-height: 0; background: var(--side); color: var(--side-text); }
.ws { padding: 12px 12px 8px; }
.ws-name { width: 100%; text-align: left; border: 0; background: none; color: var(--side-strong); font-size: 17px; font-weight: 700; padding: 6px 8px; border-radius: 6px; cursor: pointer; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ws-name::after { content: " \25BE"; font-weight: 400; opacity: 0.7; }
.ws-name:hover { background: var(--side-hover); }
.search-open { display: flex; justify-content: space-between; align-items: center; margin: 0 12px 8px; padding: 6px 10px; border-radius: 6px; border: 1px solid #ffffff22; background: #ffffff0d; color: var(--side-text); cursor: pointer; text-align: left; }
.search-open kbd { border-color: #ffffff33; color: var(--side-text); }
.nav { flex: 1; overflow-y: auto; padding: 4px 8px 12px; }
.section { display: flex; align-items: center; justify-content: space-between; margin: 14px 4px 2px 8px; }
.section h2 { margin: 0; font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--side-text); opacity: 0.8; }
.section .icon { color: var(--side-text); width: 26px; height: 26px; }
.section .icon:hover { background: var(--side-hover); }
.nav ul { list-style: none; margin: 0; padding: 0; }
.nav .small { margin: 2px 8px; color: var(--side-text); opacity: 0.8; font-size: 13px; }
.item { display: flex; align-items: center; justify-content: space-between; gap: 6px; padding: 4px 8px; border-radius: 6px; color: var(--side-text); text-decoration: none; min-height: 30px; }
.item:hover { background: var(--side-hover); }
.item .label { display: flex; align-items: center; gap: 6px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .prefix { opacity: 0.7; width: 1.1em; flex: none; }
.item.unread { color: var(--side-strong); font-weight: 700; }
.item.active { background: var(--side-active); color: #fff; }
.item.archived { opacity: 0.6; }
.badge { flex: none; min-width: 20px; padding: 0 6px; border-radius: 10px; background: #e5484d; color: #fff; font-size: 12px; font-weight: 700; text-align: center; }
.dot { flex: none; display: inline-block; width: 9px; height: 9px; border-radius: 50%; border: 2px solid var(--muted); }
.dot.on { background: var(--ok); border-color: var(--ok); }
.dot.group { width: auto; height: auto; border: 0; font-size: 11px; opacity: 0.8; }
.me { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-top: 1px solid #ffffff14; }
.me .who { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.2; }
.me strong { color: var(--side-strong); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.me small { opacity: 0.75; }
.me .icon { color: var(--side-text); }
.me .icon:hover { background: var(--side-hover); }

.avatar { --h: 210; flex: none; display: inline-flex; align-items: center; justify-content: center; width: 36px; height: 36px; border-radius: 8px; background: hsl(var(--h) 50% 30%); color: #fff; font-size: 14px; font-weight: 700; letter-spacing: 0.02em; user-select: none; }
.avatar.sm { width: 24px; height: 24px; border-radius: 6px; font-size: 11px; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.channel-head { display: flex; align-items: center; gap: 10px; min-height: 56px; padding: 6px 16px; border-bottom: 1px solid var(--line); }
.channel-head .title { flex: 1; min-width: 0; }
.channel-head h1 { margin: 0; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.channel-head .prefix { color: var(--muted); font-weight: 400; }
.channel-head .topic { margin: 0; color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.only-narrow { display: none; }

.pane { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.pane.drop { outline: 3px dashed var(--accent); outline-offset: -8px; }
.timeline-wrap { position: relative; flex: 1; min-height: 0; display: flex; }
.timeline { flex: 1; overflow-y: auto; padding: 8px 0 12px; overscroll-behavior: contain; }
.timeline:focus-visible { outline-offset: -2px; }
.start { padding: 24px 20px 8px; }
.start h2 { margin: 0 0 4px; font-size: 22px; }
.more { padding: 12px 20px; text-align: center; }
.day, .new-line { display: flex; align-items: center; gap: 10px; margin: 14px 20px 6px; font-size: 12px; font-weight: 600; color: var(--muted); }
.day::before, .day::after, .new-line::before, .new-line::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.new-line { color: #e5484d; }
.new-line::before, .new-line::after { background: #e5484d66; }
.jump { position: absolute; bottom: 12px; left: 50%; transform: translateX(-50%); border: 0; border-radius: 16px; padding: 6px 14px; background: var(--accent); color: var(--accent-text); font-weight: 600; box-shadow: var(--shadow); cursor: pointer; }

.msg { position: relative; display: flex; gap: 10px; padding: 6px 20px; }
.msg.compact { padding-top: 1px; padding-bottom: 1px; }
.msg:hover, .msg:focus-within { background: var(--bg-soft); }
.msg.flash { animation: flash 2.4s ease-out; }
@keyframes flash { from { background: var(--mention-bg); } to { background: transparent; } }
.msg .gutter { flex: none; display: flex; justify-content: flex-end; width: 36px; padding-top: 3px; font-size: 10px; white-space: nowrap; color: transparent; }
.msg.compact:hover .gutter, .msg.compact:focus-within .gutter { color: var(--muted); }
.msg .body { flex: 1; min-width: 0; }
.msg header { display: flex; align-items: baseline; gap: 8px; }
.msg .author { font-weight: 700; }
.msg header time { font-size: 12px; color: var(--muted); }
.msg .text { overflow-wrap: anywhere; }
.msg .text p { margin: 0; }
.msg .edited { font-size: 12px; color: var(--muted); }
.msg.deleted .text { font-size: 14px; }
.mention { padding: 0 2px; border-radius: 4px; background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.mention.me { background: var(--mention-bg); color: var(--mention-text); }
.channel-ref { font-weight: 600; text-decoration: none; }
.files { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 6px; }
.files .image img { display: block; max-width: min(360px, 100%); max-height: 280px; border-radius: var(--radius); border: 1px solid var(--line); }
.files .file { display: inline-flex; align-items: center; gap: 4px; padding: 6px 10px; border: 1px solid var(--line); border-radius: var(--radius); text-decoration: none; color: var(--text); background: var(--bg); max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reactions { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.reaction { border: 1px solid var(--line); border-radius: 12px; padding: 0 8px; min-height: 24px; background: var(--bg); cursor: pointer; font-size: 13px; }
.reaction[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); }
.replies { margin-top: 4px; padding: 2px 6px; border: 0; border-radius: 6px; background: none; color: var(--accent); font-size: 13px; font-weight: 600; cursor: pointer; }
.replies:hover { background: var(--bg); }
.actions { position: absolute; top: -14px; right: 16px; display: none; gap: 2px; padding: 2px; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.08); }
.msg:hover .actions, .msg:focus-within .actions { display: flex; }
.editing textarea { width: 100%; resize: none; padding: 8px 10px; border: 1px solid var(--accent); border-radius: var(--radius); background: var(--bg); }
.editing .row { margin-top: 4px; }

.typing { min-height: 20px; padding: 0 20px; font-size: 12px; color: var(--muted); }

.composer { position: relative; margin: 0 16px 16px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); }
.composer:focus-within { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.compose-row { display: flex; align-items: flex-end; gap: 6px; padding: 6px; }
.compose-row textarea { flex: 1; min-height: 34px; max-height: 240px; resize: none; border: 0; outline: none; background: transparent; padding: 6px 4px; }
.compose-row .icon { margin-bottom: 2px; }
.send { min-height: 32px; }
.pending { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 8px; }
.pending:not(:empty) { padding-top: 8px; }
.chip { display: inline-flex; align-items: center; gap: 2px; padding: 2px 4px 2px 10px; border-radius: 14px; background: var(--bg-soft); border: 1px solid var(--line); font-size: 13px; }
.chip.busy { opacity: 0.6; }
.chip .icon { width: 22px; height: 22px; }
.suggest { position: absolute; left: 8px; right: 8px; bottom: calc(100% + 4px); margin: 0; padding: 4px; list-style: none; background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); z-index: 5; }
.suggest li { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 6px; cursor: pointer; }
.suggest li.sel { background: var(--accent-soft); }

.join-bar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 0 16px 16px; padding: 12px 16px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg-soft); }
.join-bar p { margin: 0; }

.thread { display: flex; flex-direction: column; min-height: 0; border-left: 1px solid var(--line); background: var(--bg); }
.thread > header { display: flex; align-items: center; justify-content: space-between; min-height: 56px; padding: 6px 12px 6px 16px; border-bottom: 1px solid var(--line); }
.thread > header h2 { margin: 0; font-size: 16px; }
.thread-list { flex: 1; overflow-y: auto; padding: 8px 0; }
.thread .msg { padding-left: 16px; padding-right: 16px; }
.pad { padding: 16px; }

/* --- menus, dialogs, toasts ------------------------------------------------- */

.menu { position: fixed; z-index: 30; min-width: 200px; padding: 4px; background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.menu > button { display: block; width: 100%; padding: 8px 12px; text-align: left; border: 0; border-radius: 6px; background: none; cursor: pointer; }
.menu > button:hover, .menu > button:focus { background: var(--accent-soft); outline: none; }
.menu > button.danger { color: var(--danger); }
.palette { display: grid; grid-template-columns: repeat(8, 36px); gap: 2px; }
.palette button { width: 36px; height: 36px; border: 0; border-radius: 6px; background: none; font-size: 20px; cursor: pointer; }
.palette button:hover, .palette button:focus { background: var(--accent-soft); outline: none; }

dialog { width: min(480px, calc(100vw - 32px)); max-height: calc(100dvh - 64px); padding: 0; border: 1px solid var(--line); border-radius: 12px; background: var(--bg); color: var(--text); box-shadow: var(--shadow); }
dialog.wide { width: min(720px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(10, 12, 16, 0.5); }
dialog > header { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 6px 20px; }
dialog > header h2 { margin: 0; font-size: 18px; }
.dialog-body { padding: 6px 20px 20px; overflow-y: auto; max-height: calc(100dvh - 140px); }
.dialog-body label { display: block; margin: 10px 0 4px; font-weight: 600; font-size: 14px; }
.dialog-body label.check { display: flex; gap: 8px; align-items: center; font-weight: 400; }
.dialog-body input:not([type="checkbox"]), .dialog-body select, .admin input, .admin select { display: block; width: 100%; margin-top: 4px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }
.dialog-body h3 { margin: 18px 0 6px; font-size: 14px; }
.pick-list { list-style: none; margin: 8px 0 0; padding: 0; max-height: 360px; overflow-y: auto; }
.pick-list li { display: flex; align-items: center; gap: 10px; padding: 8px 4px; border-bottom: 1px solid var(--line); }
.pick-list li:last-child { border-bottom: 0; }
.pick-list .person { display: flex; align-items: center; gap: 10px; width: 100%; margin: 0; font-weight: 400; cursor: pointer; }
.switcher li { cursor: pointer; padding: 8px 10px; border-radius: 6px; border: 0; }
.switcher li.sel { background: var(--accent-soft); }
.results { list-style: none; margin: 8px 0 0; padding: 0; }
.results .result { display: block; width: 100%; text-align: left; padding: 10px 8px; border: 0; border-bottom: 1px solid var(--line); background: none; cursor: pointer; }
.results .result:hover { background: var(--bg-soft); }
.results .text p { margin: 0; }
.shortcuts { display: grid; grid-template-columns: auto 1fr; gap: 8px 16px; margin: 0; }
.shortcuts dd { margin: 0; }

.toast { position: fixed; left: 50%; bottom: 24px; z-index: 40; transform: translate(-50%, 20px); opacity: 0; pointer-events: none; max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 8px; background: #1c1f24; color: #fff; box-shadow: var(--shadow); transition: opacity 0.2s, transform 0.2s; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); }
.banner { position: fixed; top: 0; left: 50%; transform: translateX(-50%); z-index: 40; padding: 6px 14px; border-radius: 0 0 8px 8px; background: #b7791f; color: #fff; font-size: 13px; font-weight: 600; }

/* --- admin ------------------------------------------------------------------ */

.admin { flex: 1; overflow-y: auto; padding: 0 20px 32px; }
.tabs { display: flex; gap: 4px; margin: 12px 0 16px; border-bottom: 1px solid var(--line); }
.tabs a { padding: 8px 12px; color: var(--muted); text-decoration: none; border-bottom: 2px solid transparent; margin-bottom: -1px; }
.tabs a.active { color: var(--text); border-color: var(--accent); font-weight: 600; }
.table-wrap { overflow-x: auto; }
.admin table { width: 100%; border-collapse: collapse; }
.admin th, .admin td { padding: 8px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: middle; }
.admin th { font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.admin tr.inactive td { opacity: 0.55; }
.admin .person { display: flex; align-items: center; gap: 8px; }
.admin select { width: auto; margin: 0; }
.actions-cell { display: flex; gap: 6px; justify-content: flex-end; }
.card { margin-bottom: 16px; padding: 16px; border: 1px solid var(--line); border-radius: 10px; }
.card h3 { margin: 0 0 8px; font-size: 15px; }
.card .row input { width: auto; flex: 1; min-width: 160px; margin: 0; }
.card label input { display: inline-block; width: 110px; margin: 0; }
.facts { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; overflow-wrap: anywhere; }
.audit { list-style: none; margin: 0 0 12px; padding: 0; font-size: 14px; }
.audit li { display: flex; flex-wrap: wrap; gap: 4px 10px; padding: 6px 0; border-bottom: 1px solid var(--line); }
.audit time { color: var(--muted); min-width: 150px; }

/* --- sign-in ---------------------------------------------------------------- */

body.signin { display: grid; place-items: center; min-height: 100vh; padding: 16px; background: var(--bg-soft); }
.signin-card { width: min(380px, 100%); padding: 32px 28px; border: 1px solid var(--line); border-radius: 14px; background: var(--bg); box-shadow: var(--shadow); text-align: center; }
.signin-card h1 { margin: 0 0 20px; font-size: 22px; }
.signin-card .button { width: 100%; min-height: 42px; }
.signin-card .error { text-align: left; padding: 10px 12px; border-radius: 8px; background: color-mix(in srgb, var(--danger) 10%, var(--bg)); }
.signin-card .dev { margin-top: 20px; text-align: left; }
.signin-card .dev label { display: block; margin: 10px 0; font-weight: 600; font-size: 14px; }
.signin-card .dev input { display: block; width: 100%; margin-top: 4px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }

/* --- narrow screens --------------------------------------------------------- */

@media (max-width: 759px) {
  .app, .app.thread-open { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: min(300px, 85vw); z-index: 20; transform: translateX(-100%); transition: transform 0.2s; }
  .app.nav-open .sidebar { transform: none; box-shadow: var(--shadow); }
  .scrim { position: fixed; inset: 0; z-index: 15; background: rgba(10, 12, 16, 0.45); }
  .only-narrow { display: inline-flex; }
  .thread { position: fixed; inset: 0; z-index: 18; border-left: 0; }
  .channel-head { padding: 6px 8px; }
  .msg { padding-left: 12px; padding-right: 12px; }
  .msg .gutter { display: none; }
  .msg.compact { padding-left: 58px; }
  .composer, .join-bar { margin: 0 8px 8px; }
  .search-open kbd { display: none; }
  .actions { right: 8px; }
  .admin { padding: 0 12px 24px; }
  .admin td:nth-child(2), .admin th:nth-child(2) { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
