:root {
  color-scheme: light;
  --bg: #f4f6f8; --surface: #fff; --text: #1f2933; --muted: #52606d; --border: #d9e2ec;
  --accent: #2563eb; --accent-text: #fff; --error: #b42318; --error-bg: #fef3f2; --ok: #067647; --ok-bg: #ecfdf3;
  --badge-bg: #d92d20; --badge-text: #fff;
  --warn-bg: #fffaeb; --warn-border: #fedf89;
  --accent-soft: #e8efff; --shadow: 0 10px 30px rgb(15 23 42 / .14); --hover: #eef2f6;
}
/* Dark colors: used when the system prefers dark (unless the user picked light),
   or whenever the user picked dark with the toggle. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111827; --surface: #1f2937; --text: #f3f4f6; --muted: #9ca3af; --border: #374151;
    --accent: #60a5fa; --accent-text: #0b1220; --error: #fda29b; --error-bg: #3b1715; --ok: #75e0a7; --ok-bg: #0f2e1f;
    --badge-bg: #f97066; --badge-text: #1b0a08;
    --warn-bg: #2e2410; --warn-border: #7a5c12;
    --accent-soft: #1c2f4d; --shadow: 0 10px 30px rgb(0 0 0 / .5); --hover: #273449;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111827; --surface: #1f2937; --text: #f3f4f6; --muted: #9ca3af; --border: #374151;
  --accent: #60a5fa; --accent-text: #0b1220; --error: #fda29b; --error-bg: #3b1715; --ok: #75e0a7; --ok-bg: #0f2e1f;
  --badge-bg: #f97066; --badge-text: #1b0a08;
  --warn-bg: #2e2410; --warn-border: #7a5c12;
  --accent-soft: #1c2f4d; --shadow: 0 10px 30px rgb(0 0 0 / .5); --hover: #273449;
}
* { box-sizing: border-box; }
body { margin: 0; font: 16px/1.5 system-ui, -apple-system, Segoe UI, sans-serif; background: var(--bg); color: var(--text); }
h1 { margin: 0 0 .25rem; font-size: 1.6rem; }
h2 { font-size: 1.15rem; margin: 2rem 0 .75rem; }
.sub { color: var(--muted); margin: 0 0 1.5rem; }
.card { max-width: 26rem; margin: 3rem auto; padding: 2rem; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; }
.wide { max-width: 60rem; margin: 2rem auto; padding: 0 1rem; }
form { display: grid; gap: 1rem; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
label { display: grid; gap: .3rem; font-weight: 600; font-size: .9rem; }
small { font-weight: 400; color: var(--muted); }
input { font: inherit; padding: .6rem .75rem; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); color: var(--text); width: 100%; }
input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
button { font: inherit; font-weight: 600; padding: .65rem 1rem; border: 0; border-radius: 8px; background: var(--accent); color: var(--accent-text); cursor: pointer; }
button:disabled { opacity: .6; cursor: progress; }
button.secondary { background: transparent; color: var(--text); border: 1px solid var(--border); }
button.small { padding: .35rem .75rem; font-size: .9rem; }
button.link { background: none; color: var(--accent); padding: 0; }
a { color: var(--accent); }
.links { display: flex; justify-content: space-between; flex-wrap: wrap; gap: .5rem; margin-top: 1.25rem; font-size: .95rem; }
.error, .notice { margin: 0; padding: .6rem .75rem; border-radius: 8px; font-size: .95rem; }
.error { color: var(--error); background: var(--error-bg); }
.notice { color: var(--ok); background: var(--ok-bg); }
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .6rem .9rem; border-bottom: 1px solid var(--border); }
tr:last-child td { border-bottom: 0; }
th { font-size: .85rem; color: var(--muted); }
.list { list-style: none; padding: 0; margin: 0; display: grid; gap: .5rem; }
.list li { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; padding: .75rem 1rem; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; }
.actions { display: flex; gap: .5rem; justify-content: flex-end; }
.empty { color: var(--muted); }
dialog { border: 1px solid var(--border); border-radius: 12px; background: var(--surface); color: var(--text); width: min(26rem, calc(100% - 2rem)); padding: 1.5rem; }
dialog h2 { margin-top: 0; }
dialog::backdrop { background: rgb(0 0 0 / .4); }
@media (max-width: 480px) { .card { margin: 3.75rem 1rem 1rem; padding: 1.5rem; } .row { grid-template-columns: 1fr; } }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 1.25rem; }
.panel h2 { margin-top: 0; }
fieldset { border: 1px solid var(--border); border-radius: 8px; padding: .75rem; margin: 0; display: grid; gap: .5rem; min-width: 0; }
legend { font-weight: 600; font-size: .9rem; padding: 0 .25rem; }
select { font: inherit; padding: .6rem .75rem; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); color: var(--text); }
input[type=file] { padding: .5rem; }
label.check { display: flex; align-items: center; gap: .5rem; font-weight: 400; }
label.check input { width: auto; }
.people { display: grid; grid-template-columns: repeat(auto-fill, minmax(14rem, 1fr)); gap: .25rem .75rem; max-height: 12rem; overflow-y: auto; }
.grow { display: grid; gap: .15rem; flex: 1 1 18rem; min-width: 0; overflow-wrap: anywhere; }
.meta { color: var(--muted); }
a.button { display: inline-block; font-weight: 600; text-decoration: none; padding: .35rem .75rem; font-size: .9rem; border-radius: 8px; background: var(--accent); color: var(--accent-text); }
button.danger { background: transparent; color: var(--error); border: 1px solid var(--error); }
.events { padding-left: 1.2rem; display: grid; gap: .35rem; font-size: .95rem; }

/* Chat */
.chat-page .main { height: 100dvh; display: flex; flex-direction: column; }
.chat { flex: 1; min-height: 0; display: grid; grid-template-columns: 15rem 1fr; }
.rooms { border-right: 1px solid var(--border); background: var(--surface); padding: 1rem; overflow-y: auto; }
.rooms h2 { margin: 0 0 .5rem; font-size: .85rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.rooms ul { list-style: none; margin: 0 0 1rem; padding: 0; display: grid; gap: 2px; }
.rooms a { display: block; padding: .4rem .6rem; border-radius: 6px; color: var(--text); text-decoration: none; overflow-wrap: anywhere; }
.rooms a:hover { background: var(--bg); }
.rooms a .ttl { float: right; color: var(--muted); font-size: .8rem; font-weight: 400; margin-left: .5rem; }
.rooms a[aria-current] .ttl { color: inherit; opacity: .85; }
.rooms a[aria-current] { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.rooms summary { cursor: pointer; color: var(--accent); font-weight: 600; margin-bottom: .75rem; }
.conversation { display: flex; flex-direction: column; min-height: 0; padding: 1rem 1.25rem; gap: .75rem; }
.room-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; }
.room-head h1 { font-size: 1.3rem; overflow-wrap: anywhere; }
.room-head .meta { margin: 0; }
.room-actions { display: flex; gap: .5rem; flex-shrink: 0; }
.members-panel { border: 1px solid var(--border); border-radius: 10px; background: var(--surface); padding: .75rem 1rem; display: grid; gap: .75rem; }
.members-panel[hidden] { display: none; }
.members-panel h3 { margin: 0 0 .5rem; font-size: .95rem; }
.members-panel ul { list-style: none; margin: 0; padding: 0; display: grid; gap: .25rem; }
.members-panel li { display: flex; justify-content: space-between; align-items: center; gap: .5rem; }
.invite-people { max-height: 10rem; margin: .5rem 0; }
.rooms .lock { margin-right: .3rem; }
.status { margin: 0; color: var(--muted); font-size: .9rem; }
.log { flex: 1; overflow-y: auto; min-height: 0; display: flex; flex-direction: column; gap: .5rem; }
.log #older { align-self: center; }
.alert-settings { margin-top: 2rem; padding-top: 1.25rem; border-top: 1px solid var(--border); }
.composer { display: flex; gap: .5rem; align-items: flex-end; }
.composer[hidden] { display: none; }
textarea { font: inherit; flex: 1; resize: none; padding: .6rem .75rem; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--text); }
textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
@media (max-width: 760px) {
  .chat { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .rooms { border-right: 0; border-bottom: 1px solid var(--border); max-height: 30vh; }
  .rooms ul { display: flex; flex-wrap: wrap; gap: .25rem; }
}

/* Online list */
.online { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: .5rem; }
.online li { display: flex; align-items: center; gap: .5rem; padding: .3rem .8rem .3rem .35rem; background: var(--surface); border: 1px solid var(--border); border-radius: 999px; font-size: .95rem; }

.online li .avatar { box-shadow: 0 0 0 2px var(--surface), 0 0 0 3.5px var(--ok); }
.online a { display: inline-flex; align-items: center; gap: .45rem; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr)); gap: 1rem; margin-top: 1rem; }
.cards a { display: block; padding: 1.1rem 1.25rem; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; text-decoration: none; color: var(--text); transition: transform .15s ease, box-shadow .15s ease, border-color .15s ease; }
.cards a:hover { transform: translateY(-2px); box-shadow: var(--shadow); border-color: var(--accent); }
.card-icon { display: grid; place-items: center; width: 2.5rem; height: 2.5rem; border-radius: 10px; background: var(--accent-soft); color: var(--accent); margin-bottom: .6rem; }
.card-icon svg { width: 1.35rem; height: 1.35rem; }
.cards a strong { color: var(--accent); }
.cards a span { display: block; color: var(--muted); font-size: .95rem; }

/* Private messages */
.badge { display: inline-block; min-width: 1.3rem; padding: 0 .35rem; margin-left: .35rem; border-radius: 999px; background: var(--badge-bg); color: var(--badge-text); font-size: .75rem; font-weight: 700; line-height: 1.3rem; text-align: center; vertical-align: middle; }
.badge[hidden] { display: none; }
.rooms a[aria-current] .badge { background: var(--accent-text); color: var(--accent); }
.people-list { max-height: 16rem; overflow-y: auto; margin-top: .5rem !important; }
#new-dm input { padding: .45rem .6rem; }
.online a { color: var(--text); text-decoration: none; }
.online a:hover { color: var(--accent); text-decoration: underline; }

.hint { margin: 0; padding: .6rem .75rem; border-radius: 8px; background: var(--bg); color: var(--muted); font-size: .9rem; }
.hint strong { color: var(--text); }
.username-big { font-size: 1.6rem; font-weight: 700; margin: 0 0 1.5rem; padding: .75rem 1rem; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; text-align: center; letter-spacing: .02em; }
a.button { text-align: center; }
#done a.button { display: block; padding: .65rem 1rem; font-size: 1rem; }

/* Light/dark toggle */
.theme-toggle { display: inline-flex; align-items: center; justify-content: center; width: 2.25rem; height: 2.25rem; padding: 0; border-radius: 999px; background: transparent; color: var(--text); border: 1px solid var(--border); cursor: pointer; }
.theme-toggle:hover { border-color: var(--accent); color: var(--accent); }
.theme-toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.theme-toggle svg { width: 1.1rem; height: 1.1rem; }
.theme-toggle.floating { position: fixed; top: .9rem; right: 1rem; background: var(--surface); }
.theme-slot { display: contents; }
.notice.warn { color: var(--text); background: var(--warn-bg); border: 1px solid var(--warn-border); }
.card form h2 { margin: .5rem 0 0; }
.tag { display: inline-block; margin-left: .4rem; padding: 0 .45rem; border-radius: 999px; font-size: .75rem; font-weight: 600; background: var(--error-bg); color: var(--error); vertical-align: middle; }
.tag.muted { background: var(--bg); color: var(--muted); border: 1px solid var(--border); }
tr.flag td:nth-child(2) { color: var(--error); font-weight: 600; }
label.inline { display: inline-flex; align-items: center; gap: .5rem; margin-bottom: .75rem; font-weight: 600; }
label.inline select { width: auto; padding: .35rem .6rem; }
#activity-more { margin-top: .75rem; }

/* Attachments in private messages */
.attach-button { display: inline-flex; align-items: center; justify-content: center; width: 2.6rem; height: 2.6rem; border: 1px solid var(--border); border-radius: 8px; cursor: pointer; font-size: 1.1rem; background: var(--surface); flex: none; }
.attach-button:hover { border-color: var(--accent); }
.attach-button:focus-within { outline: 2px solid var(--accent); outline-offset: 1px; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.attachment-pending { display: flex; justify-content: space-between; align-items: center; gap: 1rem; padding: .45rem .75rem; border: 1px dashed var(--border); border-radius: 8px; font-size: .95rem; }
.attachment-pending[hidden] { display: none; }
.file-chip { display: inline-block; color: var(--accent) !important; margin-top: .35rem; padding: .35rem .6rem; border-radius: 6px; background: var(--bg); border: 1px solid var(--border); text-decoration: none; color: var(--accent); font-weight: 600; overflow-wrap: anywhere; }
.file-chip.gone { color: var(--muted); font-weight: 400; }

/* App layout: sidebar on the left, page on the right. On phones the sidebar
   becomes a tab bar along the bottom. */
body.app { display: grid; grid-template-columns: 15.5rem minmax(0, 1fr); min-height: 100dvh; }
.main { min-width: 0; }
.sidebar { position: sticky; top: 0; height: 100dvh; display: flex; flex-direction: column; gap: 1.25rem; padding: 1rem .75rem; background: var(--surface); border-right: 1px solid var(--border); z-index: 30; }
.brand { display: flex; align-items: center; gap: .65rem; padding: .25rem .5rem; color: var(--text); text-decoration: none; }
.brand-mark { display: grid; place-items: center; width: 2.3rem; height: 2.3rem; border-radius: 10px; background: linear-gradient(135deg, #1d4ed8, #4f46e5); color: #fff; flex: none; }
.brand-mark svg { width: 1.35rem; height: 1.35rem; }
.brand-name { display: grid; font-weight: 800; font-size: 1.1rem; line-height: 1.15; letter-spacing: .04em; }
.brand-name small { font-size: .72rem; font-weight: 600; letter-spacing: .02em; color: var(--muted); }
.side-nav { display: grid; gap: 2px; }
.side-nav[hidden] { display: none; }
.side-nav a { display: flex; align-items: center; gap: .75rem; padding: .55rem .75rem; border-radius: 8px; color: var(--muted); text-decoration: none; font-weight: 500; transition: background .15s ease, color .15s ease; }
.side-nav a[hidden] { display: none; }
.side-nav a:hover { background: var(--hover); color: var(--text); }
.side-nav a[aria-current] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.side-nav .icon { width: 1.3rem; height: 1.3rem; flex: none; }
.side-nav .badge { margin-left: auto; }
.side-foot { margin-top: auto; display: flex; align-items: center; gap: .5rem; }
.user-menu { position: relative; flex: 1; min-width: 0; }
.user-menu summary { list-style: none; display: flex; align-items: center; gap: .6rem; padding: .35rem .5rem; border-radius: 8px; cursor: pointer; }
.user-menu summary::-webkit-details-marker { display: none; }
.user-menu summary:hover, .user-menu[open] summary { background: var(--hover); }
.user-menu summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.me-name { font-weight: 600; font-size: .9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.user-menu .menu { position: absolute; bottom: calc(100% + .4rem); left: 0; min-width: 12rem; display: grid; padding: .35rem; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow); animation: pop-in .12s ease-out; }
.user-menu .menu a, .user-menu .menu button { display: block; width: 100%; text-align: left; padding: .5rem .75rem; border-radius: 6px; color: var(--text); text-decoration: none; font-weight: 500; font-size: .95rem; }
.user-menu .menu a:hover, .user-menu .menu button:hover { background: var(--hover); }
.user-menu .menu a[aria-current] { color: var(--accent); }

/* Avatars */
.avatar { --av: #6b7280; display: inline-grid; place-items: center; width: 2.25rem; height: 2.25rem; border-radius: 50%; background: var(--av); color: #fff; font-size: .8rem; font-weight: 700; letter-spacing: .02em; flex: none; user-select: none; }
.avatar.sm { width: 1.75rem; height: 1.75rem; font-size: .68rem; }
.person-line { display: flex; align-items: center; gap: .5rem; min-width: 0; }
.grow-name { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.rooms a.person-line { display: flex; }
.rooms a[aria-current] .avatar { box-shadow: 0 0 0 2px var(--accent-text); }

/* Messages: bubbles, grouping, date dividers */
#messages { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; margin-top: auto; }
#messages li.msg { display: flex; align-items: flex-start; gap: .6rem; max-width: min(44rem, 88%); margin-top: .75rem; }
#messages li.msg.cont { margin-top: .2rem; }
#messages li.msg.cont .avatar { visibility: hidden; height: 0; }
#messages li.msg.cont .who { display: none; }
.msg-main { display: flex; flex-direction: column; align-items: flex-start; min-width: 0; }
.msg .who { font-size: .85rem; margin-bottom: .15rem; }
.msg .who small { font-size: .78rem; }
.msg .body { white-space: pre-wrap; overflow-wrap: anywhere; background: var(--surface); border: 1px solid var(--border); border-radius: 4px 16px 16px 16px; padding: .5rem .85rem; max-width: 100%; }
.msg.cont .body { border-radius: 16px; }
#messages li.msg.mine { align-self: flex-end; flex-direction: row-reverse; }
.msg.mine .avatar { display: none; }
.msg.mine .msg-main { align-items: flex-end; }
.msg.mine .who strong { display: none; }
.msg.mine .body { background: var(--accent); color: var(--accent-text); border-color: transparent; border-radius: 16px 4px 16px 16px; }
.msg.mine.cont .body { border-radius: 16px; }
.msg.mine .file-chip { background: var(--surface); border-color: transparent; }
.msg .seen { margin-top: .2rem; font-size: .78rem; }
.day-divider { display: flex; align-items: center; gap: .75rem; margin: 1.25rem 0 .25rem; color: var(--muted); font-size: .78rem; font-weight: 600; letter-spacing: .02em; }
.day-divider::before, .day-divider::after { content: ''; flex: 1; height: 1px; background: var(--border); }
.empty-log { margin: auto; text-align: center; color: var(--muted); }
.typing { margin: 0; min-height: 1.3rem; font-size: .85rem; color: var(--muted); font-style: italic; }
.typing.active::before { content: ''; display: inline-block; width: .45rem; height: .45rem; margin-right: .45rem; border-radius: 50%; background: var(--accent); vertical-align: middle; animation: pulse 1s ease-in-out infinite; }
.chat-page .conversation { gap: .5rem; }

/* Files */
.list li .file-icon { display: grid; place-items: center; width: 2.6rem; height: 2.6rem; border-radius: 10px; color: #fff; font-size: .68rem; font-weight: 800; letter-spacing: .04em; flex: none; background: #6b7280; }
.list li .file-icon.pdf { background: #c0362c; }
.list li .file-icon.doc { background: #2b5cb8; }
.list li .file-icon.xls { background: #1d7a46; }
.list li .file-icon.img { background: #7c3aed; }
.list li { transition: border-color .15s ease, box-shadow .15s ease; }
.list li:hover { border-color: var(--accent); }

/* Empty states */
.empty-state { display: flex; align-items: center; gap: .75rem; margin: 0 0 .5rem; padding: 1rem 1.25rem; border: 1px dashed var(--border); border-radius: 12px; color: var(--muted); }
.empty-state span { font-size: 1.4rem; }
.empty-state[hidden], .empty-hint[hidden], .empty-log[hidden] { display: none; }
.empty-hint { margin: 0 0 1rem; color: var(--muted); font-size: .9rem; }

/* Sign-in pages */
.auth-brand { display: flex; align-items: center; gap: .65rem; margin-bottom: 1.5rem; font-weight: 800; font-size: 1.05rem; letter-spacing: .02em; }

/* Motion */
#messages li.msg.new { animation: msg-in .22s ease-out; }
@keyframes msg-in { from { opacity: 0; transform: translateY(8px); } }
@keyframes pop-in { from { opacity: 0; transform: translateY(4px) scale(.98); } }
@keyframes pulse { 50% { opacity: .3; } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

@media (max-width: 760px) {
  body.app { grid-template-columns: minmax(0, 1fr); padding-bottom: calc(3.9rem + env(safe-area-inset-bottom)); }
  .sidebar { position: fixed; top: auto; bottom: 0; left: 0; right: 0; height: auto; flex-direction: row; align-items: center; gap: .25rem; padding: .3rem .5rem calc(.3rem + env(safe-area-inset-bottom)); border-right: 0; border-top: 1px solid var(--border); }
  .brand { display: none; }
  .side-nav { display: flex; flex: 1; justify-content: space-around; gap: 0; }
  .side-nav a { position: relative; flex-direction: column; gap: .1rem; padding: .3rem .35rem; font-size: .68rem; min-width: 3.2rem; }
  .side-nav .badge { position: absolute; top: 0; right: .3rem; margin: 0; }
  .side-foot { margin-top: 0; flex: none; gap: .25rem; }
  .user-menu { flex: none; }
  .me-name { display: none; }
  .user-menu .menu { left: auto; right: 0; }
  .chat-page .main { height: calc(100dvh - 3.9rem - env(safe-area-inset-bottom)); }
  #messages li.msg { max-width: 92%; }
}
.cards a .card-icon { display: grid; color: var(--accent); }
@media (max-width: 760px) {
  .cards { gap: .6rem; }
  .cards a { display: grid; grid-template-columns: auto 1fr; column-gap: .85rem; align-items: center; padding: .85rem 1rem; }
  .cards a .card-icon { grid-row: span 2; margin: 0; }
}
