/*
 * Discord-like look for the Stoat web client (dark, near-black, blurple accent).
 * Served as /custom.css by the web container (compose.override.yml bind mount),
 * linked from index.html by stoat/patches/0001-baseline.patch.
 * The dc_* classes and --dc-speaking used below come from the client commit
 * "feat(ui): stable dc_* class hooks" (stoat/patches/); without it those rules
 * simply match nothing.
 *
 * Theme variables are set as inline styles on <body> by LoadTheme.tsx, so they
 * cannot be overridden on body itself. Redefining them on #root works because
 * every app element inherits from #root, not directly from body.
 * Portals/menus mounted outside #root are covered by the body > * selector.
 *
 * Apply: copy to /opt/stoat/web/custom.css on LXC 106, then
 *   docker compose restart web     (inject.js copies dist/ at container start)
 * and hard-reload the page (Ctrl+F5).
 */

#root,
body > *:not(script) {
  /* surfaces: Discord dark (AMOLED-ish) */
  --md-sys-color-background: #000000;
  --md-sys-color-surface: #000000;
  --md-sys-color-surface-dim: #000000;
  --md-sys-color-surface-bright: #232428;
  --md-sys-color-surface-container-lowest: #000000;
  --md-sys-color-surface-container-low: #0b0b0d;
  --md-sys-color-surface-container: #111214;
  --md-sys-color-surface-container-high: #16171a;
  --md-sys-color-surface-container-highest: #1e1f22;
  --md-sys-color-surface-variant: #1e1f22;

  /* text */
  --md-sys-color-on-surface: #dbdee1;
  --md-sys-color-on-surface-variant: #949ba4;
  --md-sys-color-on-surface-container: #dbdee1;

  /* hairlines between panels */
  --md-sys-color-outline: #3f4147;
  --md-sys-color-outline-variant: #26272b;

  /* accent: Discord blurple */
  --md-sys-color-primary: #5865f2;
  --md-sys-color-on-primary: #ffffff;
  /* selected channel etc.: Discord uses a neutral grey highlight, not blurple */
  --md-sys-color-primary-container: #2e3035;
  --md-sys-color-on-primary-container: #ffffff;
  --md-sys-color-secondary: #949ba4;
  --md-sys-color-on-secondary: #000000;
  --md-sys-color-secondary-container: #2b2d31;
  --md-sys-color-on-secondary-container: #f2f3f5;
  --mdui-color-secondary-container: 43, 45, 49;
  --mdui-color-on-secondary-container: 242, 243, 245;

  /* status colours as in Discord */
  --brand-presence-online: #23a55a;
  --brand-presence-idle: #f0b232;
  --brand-presence-busy: #f23f43;

  /* speaking ring in the voice sidebar and call tiles (client falls back to primary) */
  --dc-speaking: #23a55a;

  /* tighter corners, closer to Discord */
  --borderRadius-md: 8px;
  --borderRadius-lg: 8px;
  --borderRadius-li: 8px;
  --borderRadius-xl: 12px;
  --borderRadius-xli: 12px;
}

html,
body {
  background: #000000;
}

/*
 * Layout: flat panels separated by 1px lines instead of floating rounded cards.
 * Hooks are the stable class names / tags from the client source:
 *   .app_root  root column        .main_bar  server rail + channel list
 *   .channel_bar  channel list    .app_body  page (header + chat + members)
 *   main       chat column        .dc_member_sidebar  member list / pins / search
 */
:root {
  --dc-line: #26272b;
}

.app_root,
.app_root > div {
  background: #000000 !important;
}

/* channel list: framed panel with a rounded top-left corner, like Discord */
.main_bar .channel_bar {
  background: #000000 !important;
  border-top: 1px solid var(--dc-line);
  border-left: 1px solid var(--dc-line);
  border-top-left-radius: 8px !important;
  border-bottom-left-radius: 0 !important;
}

/* server name bar at the top of the channel list: flush, with a bottom line */
.channel_bar > :first-child {
  margin: 0 !important;
  border-radius: 0 !important;
  border-bottom: 1px solid var(--dc-line);
}
.channel_bar > :first-child:not([style*="url("]) {
  background: transparent !important;
}
/* home sidebar: the first child is the whole scroller, its line would float above the user panel */
.channel_bar.home > :first-child {
  border-bottom: 0;
}
/* server name: inset from the rail like Discord (the flush override above dropped the client's margin) */
.channel_bar.server > :first-child {
  padding-inline: 16px 8px !important;
}

/* page area: separated from the channel list by a line */
.app_body {
  background: #000000 !important;
  border-radius: 0 !important;
  border-top: 1px solid var(--dc-line);
  border-left: 1px solid var(--dc-line);
}

/* channel header bar (sibling right before the chat row) */
.app_body :has(+ div > main) {
  margin: 0 !important;
  border-radius: 0 !important;
  border-bottom: 1px solid var(--dc-line);
}

/* chat column: no floating card */
.app_body main {
  margin: 0 !important;
  border-radius: 0 !important;
  background: #000000 !important;
}

/* member list / pins / search column */
.dc_member_sidebar {
  border-left: 1px solid var(--dc-line);
  border-radius: 0 !important;
  background: #000000;
}

/* thin neutral scrollbars instead of blurple ones.
   Chromium: style via ::-webkit-scrollbar - setting scrollbar-color/width there switches to the
   standard scrollbar with arrow buttons and overrides the client's hidden (scrollbar-width: none)
   sidebar scrollers. Hidden scrollers keep working: their ::-webkit-scrollbar { display: none }
   is a different property. */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
::-webkit-scrollbar-button {
  display: none;
}
::-webkit-scrollbar-track,
::-webkit-scrollbar-corner {
  background: transparent;
}
::-webkit-scrollbar-thumb {
  background: #2b2d31;
  border-radius: 4px;
  border: 2px solid transparent;
  background-clip: padding-box;
}
/* Firefox has no ::-webkit-scrollbar; colour only, width stays with the client */
@supports not selector(::-webkit-scrollbar) {
  * {
    scrollbar-color: #2b2d31 transparent;
  }
}

/*
 * Discord details. Every selector below starts from a dc_* hook added to the
 * client (or .Toolbar inside .dc_msg), never from a bare element or `*`:
 * these rules are unlayered and beat panda's layered utilities everywhere
 * they match. Panda's own `!important` values cannot be overridden from
 * here (layered !important wins), so such spots are redrawn instead.
 */

/* --- mentions: yellow tint + 2px yellow bar on the left --- */
.dc_mention {
  background: rgba(250, 168, 26, 0.1);
  box-shadow: inset 2px 0 0 #f0b232;
  border-radius: 0 var(--borderRadius-md) var(--borderRadius-md) 0;
}
.dc_mention:hover {
  background: rgba(250, 168, 26, 0.08);
}

/* --- message hover toolbar: framed pill with shadow over the message top.
   Never set `display` here: the client toggles it on hover. --- */
.dc_msg > .Toolbar {
  top: -16px;
  right: 14px;
  border: 1px solid #2b2d31;
  border-radius: 8px;
  background: #111214;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.45);
  fill: #b5bac1;
}
.dc_msg > .Toolbar > div {
  padding: 6px;
}
.dc_msg > .Toolbar > div:hover {
  background: rgba(78, 80, 88, 0.3);
  fill: #dbdee1;
}

/* --- server rail: 48px icons, circle -> rounded square on hover/selected --- */
.dc_server > svg,
.dc_server > a > svg {
  /* Avatar sets its size inline */
  width: 48px !important;
  height: 48px !important;
}
.dc_server foreignObject > div {
  border-radius: 50%;
  transition: border-radius 0.15s ease-out;
}
.dc_server:hover foreignObject > div,
.dc_server_selected foreignObject > div {
  border-radius: 33%;
}
/* text/icon fallback (home, add, discover, servers without an icon) */
.dc_server foreignObject > div > div {
  background: #1e1f22;
  color: #dbdee1;
  fill: #dbdee1;
  transition:
    background-color 0.15s ease-out,
    color 0.15s ease-out;
}
.dc_server:hover foreignObject > div > div,
.dc_server_selected foreignObject > div > div {
  background: #5865f2;
  color: #ffffff;
  fill: #ffffff;
}
/* left pill: the client's ::before has `height: 32px !important` when selected,
   which cannot be beaten from an unlayered sheet, so draw our own on ::after */
.dc_server::before {
  display: none;
}
.dc_server::after {
  content: "";
  position: absolute;
  left: -8px;
  width: 12px;
  height: 0;
  border-radius: 4px;
  background: #f2f3f5;
  transition: height 0.15s ease-out;
}
.dc_server_alert::after {
  height: 8px;
}
.dc_server:hover::after {
  height: 20px;
}
.dc_server_selected::after {
  height: 40px;
}

/* --- channel list: flat sections, uppercase categories, Discord row colours --- */
.dc_category_section {
  background: transparent;
}
.dc_category {
  --color: #949ba4;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
}
.dc_category:hover {
  --color: #dbdee1;
}
/* chevron before the title, as in Discord */
.dc_category > svg {
  order: -1;
}
.dc_channel > a {
  --color: #80848e;
  height: 34px;
  border-radius: 4px;
  font-size: 16px;
  font-weight: 500;
}
.dc_channel > a:hover {
  --color: #dbdee1;
  background: rgba(78, 80, 88, 0.3);
}
.dc_channel_muted > a {
  --color: #4e5058;
}
.dc_channel_active > a {
  --color: #f2f3f5;
}
.dc_channel_unread > a {
  --color: #f2f3f5;
  font-weight: 600;
}
.dc_channel_selected > a,
.dc_channel_selected > a:hover {
  --color: #ffffff;
  background: rgba(78, 80, 88, 0.6);
}
/* unread pill at the left edge of the channel list (row margin = --gap-md) */
.dc_channel_unread > a::before {
  content: "";
  position: absolute;
  left: calc(-1 * var(--gap-md));
  top: 50%;
  width: 4px;
  height: 8px;
  margin-top: -4px;
  border-radius: 0 4px 4px 0;
  background: #f2f3f5;
}

/* --- reactions: pills, own reaction in blurple --- */
.dc_reaction {
  align-items: center;
  gap: 6px;
  padding: 2px 6px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: #16171a;
  color: #b5bac1;
  font-weight: 500;
}
.dc_reaction img {
  width: 16px;
  height: 16px;
}
.dc_reaction:hover {
  border-color: #3f4147;
  background: #1e1f22;
}
.dc_reaction_me,
.dc_reaction_me:hover {
  border-color: #5865f2;
  background: rgba(88, 101, 242, 0.15);
  color: #dee0fc;
}
.dc_reaction_add {
  height: 26px;
  padding: 3px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: #16171a;
  fill: #b5bac1;
}
.dc_reaction_add:hover {
  border-color: #3f4147;
  fill: #dbdee1;
}

/* --- dividers: centred date, red "NEW" line with the badge on the right --- */
.dc_divider {
  position: relative;
}
.dc_divider time {
  margin-inline: auto;
  padding-inline: 4px;
  font-size: 12px;
  line-height: 13px;
  color: #949ba4;
  background: #000000;
}
.dc_divider_unread {
  border-top-color: #f23f43;
}
.dc_new {
  position: absolute;
  right: 0;
  top: 0;
  margin-top: -7px;
  padding: 0 4px;
  border-radius: 0 8px 8px 0;
  background: #f23f43;
  color: #ffffff;
  font-size: 10px;
  line-height: 13px;
  font-weight: 700;
  text-transform: uppercase;
}
/* "new messages since ..." bar at the top of the chat: solid blurple */
.dc_newbar {
  background-color: #5865f2;
  backdrop-filter: none;
  font-weight: 500;
}

/* --- phone / tablet: member list, pins and search slide over the chat instead of squeezing it.
   Upstream already turns the server+channel list into a drawer at <= 600px; the right column
   stays a flex sibling of <main> (pins/search even set width: 360px inline). --- */
@media (max-width: 840px), (max-height: 600px) {
  .app_body div:has(> main) {
    position: relative;
  }
  .dc_member_sidebar {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    width: min(320px, 85vw) !important;
    max-width: 100%;
    overflow-x: hidden;
    background: #000000;
    border-left: 1px solid var(--dc-line);
  }
  /* dims the chat behind the drawer; tap closes it (TextChannel.tsx renders it off desktop) */
  .dc_sidebar_scrim {
    position: absolute;
    inset: 0;
    z-index: 19;
    background: rgba(0, 0, 0, 0.6);
    -webkit-tap-highlight-color: transparent;
  }
  /* the floating call card sits above .app_body; hide it while the drawer is open
     (only the card: modals opened from the drawer live in #floating too) */
  body:has(.dc_sidebar_scrim) .dc_call_float {
    visibility: hidden;
  }
}

/* Context menus can contain long labels; keep the floating menu inside the phone viewport. */
.dc_context_menu {
  max-width: calc(100vw - 16px);
}
.dc_status_menu {
  box-sizing: border-box;
  max-width: calc(100vw - 16px);
}
.dc_status_menu > .dc_context_menu {
  box-sizing: border-box;
  max-width: 100%;
  max-height: calc(100dvh - 24px);
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
}
.dc_context_menu span:not([aria-hidden='true']) {
  min-width: 0;
  overflow-wrap: anywhere;
}
.dc_user_card {
  width: min(340px, calc(100vw - 16px)) !important;
  max-width: calc(100vw - 16px);
  box-sizing: border-box;
}
.dc_user_card_grid {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}
.dc_search_sort {
  width: 100%;
  min-width: 0;
}
.dc_search_sort > button {
  flex: 1 1 0;
  min-width: 0;
  padding-inline: 4px;
  white-space: normal;
  text-align: center;
  overflow-wrap: anywhere;
}
@media (max-width: 480px) {
  .dc_search_sort > button {
    font-size: 12px;
    line-height: 1.2;
    white-space: nowrap;
  }
}
@media (max-width: 600px) {
  .dc_profile_grid {
    grid-template-columns: minmax(0, 1fr);
  }
  .dc_profile_grid > * {
    grid-column: 1 / -1;
    min-width: 0;
  }
}

/* touch: user panel buttons and reactions at least 40px for a finger */
@media (pointer: coarse) {
  .dc_user_row button {
    min-width: 40px;
    min-height: 40px;
  }
  .dc_reaction,
  .dc_reaction_add {
    min-height: 40px;
  }
  .dc_reaction {
    min-width: 48px;
    justify-content: center;
    padding: 2px 10px;
  }
  .dc_reaction_add {
    min-width: 40px;
    justify-content: center;
  }
}
@media (max-width: 600px) {
  .app_body {
    border-left: 0;
  }
}

/* user panel: keep mic / deafen / settings readable over a busy nameplate */
.dc_user_plate ~ * .dc_panel_btn,
.dc_user_row:has(.dc_user_plate) .dc_panel_btn {
  background: rgba(17, 18, 20, 0.72);
  backdrop-filter: blur(2px);
}
.dc_user_row:has(.dc_user_plate) .dc_panel_btn:hover {
  background: rgba(46, 48, 53, 0.9);
}

/* mdui web components (checkboxes in dialogs) keep mdui's light-theme text token; dark text on dark */
mdui-checkbox {
  --mdui-color-on-surface: 219, 222, 225;
}

/* phone, compact message mode: time + name on their own line, text below and smaller */
@media (max-width: 600px) {
  .dc_compact > .dc_msg_row {
    flex-direction: column;
  }
  .dc_compact > .dc_msg_row > :first-child {
    justify-content: flex-start;
    padding-inline: 12px 0;
  }
  .dc_compact > .dc_msg_row > :first-child > * {
    padding-inline: 0;
  }
  .dc_compact > .dc_msg_row > :last-child {
    padding-inline-start: 12px;
  }
  .dc_msg_content {
    font-size: min(var(--message-size), 15px);
  }
}
