:root {
  color-scheme: light;
  --bg: #f8fafb;
  --bg-secondary: #ffffff;
  --ink: #203341;
  --muted: #788792;
  --line: #e5ebee;
  --accent: #157d78;
  --soft: #e9f4f2;
  --hover: #f0f3f5;
  --selected: #edf6e5;
  --error-bg: #fff0eb;
  --error-border: #efcebd;
  --error-text: #984c31;
  --warning-bg: #fff2d9;
  --warning-text: #9a6925;
  --unavailable: #a67840;
  --code-bg: #edf1f3;
  --blockquote-border: #9ac7c0;
  --blockquote-text: #637985;
  --table-border: #dbe3e8;
  --reaction-bg: #ffffff;
  --thread-bg: #ffffff;
  --sidebar-bg: #ffffff;
  --search-bg: #ffffff;
  --topbar-bg: #ffffff;
  --avatar-bg: #ecf0f3;
  --avatar-text: #637985;
  --avatar-active-bg: #cce7e2;
  --lightbox-bg: #12212bed;
  --highlight-bg: #fff3b0;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #1a1d21;
  --bg-secondary: #23262a;
  --ink: #d9e1e8;
  --muted: #8b959d;
  --line: #363b41;
  --accent: #5ebfb5;
  --soft: #2a3338;
  --hover: #2a2e33;
  --selected: #2a3a2e;
  --error-bg: #3a2520;
  --error-border: #5a3530;
  --error-text: #e8a090;
  --warning-bg: #3a3020;
  --warning-text: #e8c080;
  --unavailable: #c09060;
  --code-bg: #2a2e33;
  --blockquote-border: #5ebfb5;
  --blockquote-text: #8b959d;
  --table-border: #363b41;
  --reaction-bg: #2a2e33;
  --thread-bg: #23262a;
  --sidebar-bg: #23262a;
  --search-bg: #23262a;
  --topbar-bg: #23262a;
  --avatar-bg: #363b41;
  --avatar-text: #8b959d;
  --avatar-active-bg: #3a5050;
  --lightbox-bg: #0a0d11ee;
  --highlight-bg: #6a5a20;
}

*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); }

a { color: var(--accent); }
button, input, select { font: inherit; }
button { cursor: pointer; border: 0; border-radius: 8px; padding: 10px 16px; background: var(--accent); color: white; font-weight: 600; }
button:hover { filter: brightness(.95); }
button:disabled { opacity: .5; cursor: wait; }
input, select { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; background: var(--bg-secondary); color: var(--ink); min-width: 0; }
input:focus, select:focus, button:focus-visible, a:focus-visible { outline: 2px solid #77bdb7; outline-offset: 2px; }

.topbar { height: 76px; display: flex; align-items: center; justify-content: space-between; padding: 0 28px; border-bottom: 1px solid var(--line); background: var(--topbar-bg); }
.brand { display: flex; gap: 13px; align-items: center; font-weight: 700; font-size: 18px; letter-spacing: -.3px; }
.brand-icon { font-size: 33px; color: var(--accent); }
.brand small { display: block; font-size: 9px; letter-spacing: 1.8px; color: var(--muted); margin-top: 3px; }
.offline { color: var(--muted); font-size: 12px; display: flex; align-items: center; gap: 8px; }
.offline i { width: 7px; height: 7px; background: #4a9c83; border-radius: 50%; }

/* Theme toggle button */
.theme-toggle { background: none; border: 1px solid var(--line); border-radius: 8px; width: 36px; height: 36px; padding: 0; display: grid; place-items: center; font-size: 18px; color: var(--muted); cursor: pointer; margin-left: 12px; }
.theme-toggle:hover { background: var(--soft); color: var(--ink); }
.topbar-right { display: flex; align-items: center; gap: 8px; }

.workspace { display: flex; height: calc(100dvh - 76px); }
.sidebar { width: 280px; flex-shrink: 0; border-right: 1px solid var(--line); background: var(--sidebar-bg); display: flex; flex-direction: column; padding: 24px 16px 16px; }
.sidebar-heading { display: flex; align-items: center; justify-content: space-between; padding: 0 8px 16px; }
.sidebar-heading h2 { font-size: 15px; margin: 0; }
.count { background: var(--soft); color: var(--muted); border-radius: 6px; padding: 3px 7px; font-size: 11px; }
#channel-filter { width: 100%; background: var(--bg); margin-bottom: 16px; }
#channels { overflow: auto; flex: 1; }
.channel { width: 100%; display: flex; gap: 10px; text-align: left; background: none; color: var(--ink); font-weight: 400; padding: 13px 10px; margin-bottom: 3px; }
.channel.active { background: var(--soft); }
.avatar { width: 34px; height: 34px; border-radius: 10px; background: var(--avatar-bg); color: var(--avatar-text); display: grid; place-items: center; font-size: 12px; font-weight: 700; flex-shrink: 0; }
.avatar img { width: 100%; height: 100%; object-fit: cover; border-radius: inherit; cursor: zoom-in; }
.channel.active .avatar { background: var(--avatar-active-bg); color: var(--accent); }
.channel-text { min-width: 0; flex: 1; }
.channel-name { font-weight: 600; display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.channel-meta { display: block; font-size: 11px; color: var(--muted); margin-top: 5px; }
.badge { font-size: 10px; color: var(--accent); background: var(--soft); border-radius: 4px; padding: 2px 5px; display: inline-block; margin: 4px 4px 0 0; }
.badge.warning { color: var(--warning-text); background: var(--warning-bg); }
.sidebar-footer { font-size: 10px; line-height: 1.6; color: var(--muted); padding: 18px 8px 0; border-top: 1px solid var(--line); margin-top: 12px; }

main { flex: 1; min-width: 0; display: flex; flex-direction: column; overflow: auto; }
.search-bar { padding: 24px 32px 20px; border-bottom: 1px solid var(--line); background: var(--search-bg); }
.search-line { display: flex; gap: 10px; }
.search-line input { flex: 1; }
.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 10px; }
.filters select { max-width: 200px; font-size: 12px; padding: 7px 9px; }
.filters label { font-size: 11px; color: var(--muted); display: flex; gap: 5px; align-items: center; }
.filters input { padding: 5px 7px; font-size: 11px; max-width: 135px; }
.date-note { font-size: 10px; color: var(--muted); }

.conversation-heading { display: flex; align-items: center; justify-content: space-between; padding: 26px 32px 22px; border-bottom: 1px solid var(--line); }
.eyebrow { font-size: 9px; letter-spacing: 1.5px; color: var(--accent); font-weight: 700; }
#view-title { font-size: 23px; letter-spacing: -.6px; line-height: 1.3; margin: 8px 0; overflow-wrap: anywhere; }
#view-meta { color: var(--muted); font-size: 12px; margin: 0; }
.quiet { color: var(--accent); background: var(--soft); font-size: 12px; white-space: nowrap; }

.timeline { padding: 16px 32px 40px; }
.welcome { text-align: center; padding: 80px 0; color: var(--muted); }
.welcome > span { color: #8cbdb6; font-size: 56px; }
.welcome h2 { font-size: 20px; color: var(--ink); margin: 16px 0 10px; }
.welcome p { line-height: 1.8; }

/* Day divider with collapse */
.day-divider { display: flex; align-items: center; gap: 12px; color: var(--muted); font-size: 11px; margin: 22px 0; cursor: pointer; user-select: none; }
.day-divider:hover { color: var(--ink); }
.day-divider:before, .day-divider:after { content: ""; height: 1px; background: var(--line); flex: 1; }
.day-divider.collapsed { margin-bottom: 0; }
.day-group { overflow: hidden; }
.day-group.collapsed { max-height: 0 !important; }

.message { display: flex; gap: 12px; padding: 16px 0; border-bottom: 1px solid var(--line); scroll-margin: 24px; overflow-wrap: anywhere; }
.message.selected { background: var(--selected); border-radius: 10px; padding: 16px 12px; margin-left: -12px; margin-right: -12px; }
.message-body { min-width: 0; flex: 1; }
.message-header { display: flex; gap: 9px; align-items: center; flex-wrap: wrap; }
.author { font-size: 13px; font-weight: 650; }
.timestamp { color: var(--muted); font-size: 11px; text-decoration: none; }
.deleted { font-size: 10px; color: var(--muted); }
.message-content { font-size: 14px; line-height: 1.7; margin-top: 6px; }
.message-content p { margin: 5px 0 10px; }
.message-content pre { overflow: auto; background: var(--code-bg); border: 1px solid var(--line); border-radius: 8px; padding: 14px; line-height: 1.5; font-size: 12px; }
.message-content code { background: var(--code-bg); padding: 1px 4px; border-radius: 3px; font-size: .9em; }
.message-content pre code { padding: 0; background: none; }
.message-content blockquote { border-left: 3px solid var(--blockquote-border); margin: 8px 0; padding: 3px 12px; color: var(--blockquote-text); }
.message-content table { display: block; overflow: auto; border-collapse: collapse; }
.message-content td, .message-content th { border: 1px solid var(--table-border); padding: 6px 10px; }
.message-content h1, .message-content h2, .message-content h3 { font-size: 17px; margin: 12px 0 8px; }

/* Search highlight */
.search-highlight { background: var(--highlight-bg); border-radius: 2px; padding: 0 2px; }

.message-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.text-button { background: none; color: var(--accent); font-size: 11px; padding: 3px 0; text-align: left; }
.text-button.copied { color: var(--muted); }

.attachments { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 12px; }
.attachment { border: 1px solid var(--line); border-radius: 8px; background: var(--bg-secondary); padding: 9px; max-width: 100%; display: flex; flex-direction: column; gap: 8px; min-width: 170px; }
.attachment img { max-width: 260px; max-height: 190px; object-fit: contain; border-radius: 5px; cursor: zoom-in; }
.attachment audio, .attachment video { max-width: 100%; width: 290px; max-height: 250px; }
.attachment a { font-size: 12px; overflow-wrap: anywhere; }
.unavailable { font-size: 11px; color: var(--unavailable); }

/* Reactions with popup */
.reactions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.reaction { font-size: 11px; border: 1px solid var(--line); border-radius: 6px; padding: 3px 6px; background: var(--reaction-bg); cursor: pointer; position: relative; }
.reaction:hover { background: var(--soft); }
.reaction-popup { position: absolute; bottom: calc(100% + 6px); left: 0; background: var(--bg-secondary); border: 1px solid var(--line); border-radius: 8px; padding: 8px 12px; font-size: 12px; color: var(--ink); box-shadow: 0 4px 12px rgba(0,0,0,0.12); z-index: 5; white-space: nowrap; min-width: 140px; }
.reaction-popup::after { content: ""; position: absolute; top: 100%; left: 16px; border: 6px solid transparent; border-top-color: var(--line); }
.reaction-popup::before { content: ""; position: absolute; top: 100%; left: 16px; border: 5px solid transparent; border-top-color: var(--bg-secondary); z-index: 1; }
.reaction-popup-title { font-size: 11px; color: var(--muted); margin-bottom: 4px; }
.reaction-popup-user { display: flex; align-items: center; gap: 6px; padding: 2px 0; }
.reaction-popup-user .avatar { width: 22px; height: 22px; font-size: 9px; border-radius: 6px; }

.load-row { text-align: center; padding: 15px; }
.load-row button { background: var(--bg-secondary); border: 1px solid var(--line); color: var(--accent); font-size: 12px; }
.empty { color: var(--muted); text-align: center; padding: 40px 20px; }
.error { margin: 16px 32px; background: var(--error-bg); border: 1px solid var(--error-border); border-radius: 8px; padding: 12px; color: var(--error-text); font-size: 12px; }

#search-results { padding: 16px 32px; }
.search-result { width: 100%; text-align: left; background: var(--bg-secondary); color: var(--ink); border: 1px solid var(--line); padding: 18px; margin-bottom: 10px; font-weight: 400; }
.result-header { display: flex; gap: 8px; align-items: center; }
.result-meta { font-size: 11px; color: var(--muted); }
.result-text { font-size: 14px; line-height: 1.6; white-space: pre-wrap; overflow-wrap: anywhere; }

.thread-panel { width: 390px; flex-shrink: 0; overflow: auto; background: var(--thread-bg); border-left: 1px solid var(--line); padding: 24px; }
.thread-heading { display: flex; align-items: center; justify-content: space-between; padding-bottom: 20px; border-bottom: 1px solid var(--line); }
.thread-heading h2 { margin-top: 8px; }
.thread-panel .message .avatar { display: none; }
.thread-panel .message { padding: 18px 0; }
.thread-panel .attachment img { max-width: 240px; }
.thread-panel .message-content { font-size: 13px; }

.lightbox { position: fixed; inset: 0; z-index: 10; background: var(--lightbox-bg); display: flex; align-items: center; justify-content: center; padding: 50px; }
.lightbox[hidden] { display: none; }
.lightbox img { max-width: 100%; max-height: 100%; object-fit: contain; }
.lightbox button { position: absolute; right: 24px; top: 20px; background: #ffffff20; font-size: 20px; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }

/* Print styles */
@media print {
  .topbar, .sidebar, .search-bar, .thread-panel, .lightbox, .message-actions, .load-row, .day-divider, #latest, .sidebar-footer { display: none !important; }
  .workspace { height: auto; display: block; }
  main { overflow: visible; }
  .timeline { padding: 0; }
  .message { break-inside: avoid; padding: 8px 0; }
  .conversation-heading { padding: 0 0 16px; }
  body { background: white; color: black; }
  .message-content pre { border: 1px solid #ccc; }
}

/* Responsive */
@media (min-width: 1500px) { .timeline { padding-left: 56px; padding-right: 56px; } }
@media (max-width: 1100px) {
  .thread-panel { position: fixed; right: 0; top: 76px; bottom: 0; width: 380px; box-shadow: -8px 0 25px #20334118; z-index: 4; }
  .sidebar { width: 235px; }
  .search-bar, .conversation-heading, .timeline, #search-results { padding-left: 22px; padding-right: 22px; }
}
@media (max-width: 700px) {
  .topbar { padding: 0 16px; height: 64px; }
  .offline { font-size: 0; }
  .workspace { height: calc(100dvh - 64px); flex-direction: column; }
  .sidebar { width: 100%; max-height: 180px; padding: 10px 14px; border-right: 0; border-bottom: 1px solid var(--line); }
  .sidebar-heading, .sidebar-footer { display: none; }
  .channel { min-width: 200px; width: 200px; padding: 8px; margin: 0; }
  #channels { display: flex; overflow-x: auto; flex-shrink: 0; }
  .channel .avatar { display: none; }
  .search-bar { padding: 15px; }
  .filters select { max-width: 145px; }
  .date-note { display: none; }
  .conversation-heading { padding: 18px 16px; }
  .timeline, #search-results { padding: 10px 16px 25px; }
  .thread-panel { top: 64px; width: 100%; }
  .welcome { padding: 35px 0; }
  .message { gap: 8px; }
  .message > .avatar { width: 28px; height: 28px; font-size: 10px; }
  .attachment img { max-width: 210px; }
  .error { margin: 10px 15px; }
}

/* Attachment blocks */
.attachment-block { background: var(--soft); border-left: 4px solid var(--accent); border-radius: 6px; padding: 12px 14px; margin: 8px 0; }
.attachment-title { font-weight: 600; font-size: 14px; margin-bottom: 6px; }
.attachment-title a { color: var(--accent); }
.attachment-author { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.attachment-author a { color: var(--muted); }
.attachment-text { margin: 6px 0; }
.attachment-fields { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 6px 16px; margin: 8px 0; }
.attachment-field { font-size: 13px; }
.attachment-field-title { font-weight: 600; font-size: 11px; color: var(--muted); margin-bottom: 2px; }
.attachment-field-value { word-break: break-word; }
.attachment-footer { font-size: 11px; color: var(--muted); margin-top: 8px; border-top: 1px solid var(--line); padding-top: 6px; }

#archive-select { width: 100%; margin: 8px 0 16px; }
.login-page { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login-card { flex: none; width: min(100%, 400px); padding: 32px; border: 1px solid var(--line); border-radius: 12px; }
.login-card form { display: grid; gap: 16px; }
.login-card input { width: 100%; }
#login-error { color: var(--ink); }
