:root {
  --brand: #1f3a5f; --brand-2: #2d5a8c; --bg: #ffffff; --panel: #f4f6f9; --surface: #ffffff; --line: #dde3ea;
  --text: #1c2430; --muted: #66727f; --danger: #b42318; --unread: #d1495b; --side: #1f3a5f; --link: #2d5a8c;
  --backdrop: rgba(0,0,0,.35);
  color-scheme: light;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--text);
}
/* Dark theme: chosen manually, or following the device when set to "System". */
:root[data-theme="dark"] {
  --brand: #3b6ea8; --brand-2: #8ab8f0; --bg: #0f141a; --panel: #18202a; --surface: #141b23; --line: #2a3542;
  --text: #e4eaf1; --muted: #8e9bab; --danger: #ff7b72; --unread: #ff6b81; --side: #111c2b; --link: #8ab8f0;
  --backdrop: rgba(0,0,0,.6);
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --brand: #3b6ea8; --brand-2: #8ab8f0; --bg: #0f141a; --panel: #18202a; --surface: #141b23; --line: #2a3542;
    --text: #e4eaf1; --muted: #8e9bab; --danger: #ff7b72; --unread: #ff6b81; --side: #111c2b; --link: #8ab8f0;
    --backdrop: rgba(0,0,0,.6);
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
button, input, textarea, select { font: inherit; }
button { cursor: pointer; border: 1px solid var(--line); background: var(--surface); color: var(--text); border-radius: 6px; padding: 6px 12px; }
button.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
button.link { border: 0; background: none; padding: 2px 4px; color: var(--link); }
a { color: var(--link); }
button.danger { color: var(--danger); }
input, textarea, select { border: 1px solid var(--line); border-radius: 6px; padding: 8px 10px; width: 100%; background: var(--surface); color: var(--text); }
input::placeholder, textarea::placeholder { color: var(--muted); }
.boot { padding: 2rem; color: var(--muted); }

/* Sign-in / setup */
.auth { max-width: 360px; margin: 10vh auto; padding: 24px; border: 1px solid var(--line); border-radius: 12px; }
.auth h1 { margin-top: 0; font-size: 1.4rem; }
.auth form { display: grid; gap: 10px; }
.error { color: var(--danger); min-height: 1.2em; font-size: .9rem; }

/* Shell */
.shell { display: grid; grid-template-columns: 240px 1fr; height: 100dvh; }
.side { background: var(--side); color: #fff; display: flex; flex-direction: column; overflow-y: auto; }
.side header { padding: 14px 16px; font-weight: 600; border-bottom: 1px solid rgba(255,255,255,.12); }
.side header small { display: block; font-weight: 400; opacity: .75; }
.side h3 { font-size: .75rem; text-transform: uppercase; letter-spacing: .05em; opacity: .7; margin: 16px 16px 4px; display: flex; justify-content: space-between; }
.side h3 button { background: none; border: 0; color: #fff; padding: 0 4px; font-size: 1rem; opacity: .9; }
.side a { display: flex; justify-content: space-between; padding: 5px 16px; color: #fff; text-decoration: none; opacity: .85; }
.side a:hover, .side a.active { background: rgba(255,255,255,.12); opacity: 1; }
.side a.unread { font-weight: 700; opacity: 1; }
.side a.unread::after { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--unread); align-self: center; }
.side a.notjoined { opacity: .55; font-style: italic; }
.side footer { margin-top: auto; padding: 12px 16px; font-size: .85rem; display: grid; gap: 6px; }
.side footer label { display: flex; gap: 6px; align-items: center; }
.side footer input { width: auto; }
.side footer select { width: auto; flex: 1; background: rgba(255,255,255,.1); color: #fff; border-color: rgba(255,255,255,.3); padding: 4px 6px; }
.side footer select option { color: #1c2430; background: #fff; }
.side footer button { background: none; color: #fff; border-color: rgba(255,255,255,.3); }
.conn { font-size: .75rem; opacity: .7; }

.main { display: flex; flex-direction: column; min-width: 0; height: 100dvh; }
.main > header { padding: 12px 16px; border-bottom: 1px solid var(--line); display: flex; gap: 10px; align-items: center; }
.main > header h2 { margin: 0; font-size: 1.1rem; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.menu-btn { display: none; }

/* Chat */
.messages { flex: 1; overflow-y: auto; padding: 8px 16px; }
.msg { padding: 4px 0; }
.msg .who { font-weight: 600; margin-right: 6px; }
.msg .when { color: var(--muted); font-size: .75rem; }
.msg .text { white-space: pre-wrap; word-wrap: break-word; }
.attach { margin-top: 4px; display: inline-flex; gap: 8px; align-items: center; border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; background: var(--panel); }
.attach img { display: block; max-width: min(320px, 100%); max-height: 240px; border-radius: 6px; }
.older { display: block; margin: 8px auto; }
.composer { border-top: 1px solid var(--line); padding: 10px 16px; display: flex; gap: 8px; align-items: flex-end; }
.composer textarea { resize: none; height: 42px; max-height: 160px; }
.progress { font-size: .8rem; color: var(--muted); padding: 0 16px; }

/* Files + admin */
.pane { flex: 1; overflow-y: auto; padding: 12px 16px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; align-items: center; }
.crumbs { flex: 1; }
.crumbs button { border: 0; background: none; color: var(--link); padding: 2px; }
table { width: 100%; border-collapse: collapse; font-size: .92rem; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); }
th { color: var(--muted); font-weight: 500; font-size: .8rem; }
td.name { max-width: 0; width: 50%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 20px; }
.stat { background: var(--panel); border-radius: 8px; padding: 12px; }
.stat b { display: block; font-size: 1.3rem; }
.stat span { color: var(--muted); font-size: .8rem; }
.inline-form { display: grid; grid-template-columns: 1fr 1fr 1fr auto auto; gap: 8px; margin: 10px 0 20px; }
.muted { color: var(--muted); }

/* Dialog */
dialog { border: 1px solid var(--line); border-radius: 12px; padding: 20px; width: min(420px, 92vw); background: var(--bg); color: var(--text); }
dialog::backdrop { background: var(--backdrop); }
dialog form { display: grid; gap: 10px; }
dialog .row { display: flex; gap: 8px; justify-content: flex-end; }
dialog label { display: flex; gap: 6px; align-items: center; }
dialog label input { width: auto; }

@media (max-width: 720px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: fixed; inset: 0 25% 0 0; z-index: 5; transform: translateX(-102%); transition: transform .2s; }
  .shell.nav-open .side { transform: none; box-shadow: 0 0 0 100vmax var(--backdrop); }
  .menu-btn { display: inline-block; }
  .inline-form { grid-template-columns: 1fr; }
  td.hide-sm, th.hide-sm { display: none; }
}
