/* Sidebar */
:root {
  --sidebar-inline-space: max(calc(var(--inline-space) * 1.5), 1vw);
  --sidebar-tools-height: calc(120px + env(safe-area-inset-bottom, 0px));
  --sidebar-radius: 0.25em;
}

/* Prevent iOS native long-press drag preview (green +) and share sheet
   on sortable items. Must be set before the long press, not after. */
[data-sortable-list-target="item"] {
  -webkit-touch-callout: none;
  -webkit-user-drag: none;
}

/* Jiggle mode – iOS home-screen style wobble */
@keyframes jiggle {
  0%, 100% { transform: rotate(-3deg); }
  50% { transform: rotate(3deg); }
}

.is-jiggling {
  animation: jiggle 0.2s ease-in-out infinite;
  touch-action: none;
  user-select: none;
}

.is-jiggling:nth-child(2n)    { animation-delay: -0.1s; }
.is-jiggling:nth-child(3n+1)  { animation-delay: -0.05s; }
.is-jiggling:nth-child(5n+2)  { animation-delay: -0.15s; }

.is-jiggling,
.is-dragging {
  outline: none;
}

.is-dragging {
  scale: 1.12;
}


.sidebar__container {
  min-block-size: 0; /* allow shrink in flex context so overflow-y scroll works */
  flex: 1 1 0;
  padding-block-start: var(--block-space);
  padding-block-end: 0;
  overflow-x: hidden;
  overflow-y: auto;
  scroll-snap-type: none; /* override utility so list scrolls freely */
  -webkit-overflow-scrolling: touch;

  /* Mobile home: flex column, each section scrolls independently */
  @media (max-width: 67.4375rem) {
    display: flex;
    flex-direction: column;
    gap: calc(var(--block-space) / 4);
    overflow: hidden;
    padding-block-start: var(--block-space);

    /* Let turbo-frame wrappers participate in flex layout */
    > turbo-frame {
      display: flex;
      flex-direction: column;
      flex-shrink: 1;
      min-block-size: 0;
    }
  }
}

.sidebar__tools {
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  inset: auto 0 0 0;
  padding-block-start: calc(var(--block-space) * 1.25);
  padding-block-end: calc(var(--block-space) * 1.25 + env(safe-area-inset-bottom, 0px));
  padding-inline: var(--sidebar-inline-space);
  position: fixed;

  @media (min-width: 67.5rem) {
    inline-size: calc(var(--sidebar-width));
    inset-inline-start: auto;
  }
}

.sidebar__tools .avatar img,
.sidebar__tools .btn img {
  block-size: 40px;
  inline-size: 40px;
}

.sidebar__tools .btn:not(.avatar) img {
  block-size: 18px;
  inline-size: 18px;
}

.sidebar__tools-row {
  flex-shrink: 0;
}

.sidebar__tool-form {
  display: flex;
  flex-shrink: 0;
}

.sidebar__revision {
  font-variant-numeric: tabular-nums;
}

/* Direct messsages */
.directs {
  --btn-border-color: var(--color-border-darker);
  --column-gap: 0ch;

  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
  box-shadow: 0 8px 16px -8px oklch(0% 0 0 / 0.4);
  column-gap: var(--column-gap);
  display: grid;
  grid-auto-rows: minmax(auto, max-content);
  grid-template-columns: repeat(auto-fill, minmax(64px, max-content));
  inset: 0 0 auto 0;
  justify-items: start;
  max-block-size: 22ch; /* ~3–4 rows visible so scroll affordance is clear, cut off less */
  overscroll-behavior: auto;
  margin-block-start: 0.6em;
  margin-block-end: calc(var(--block-space) / 0.7);
  padding-block: calc(var(--block-space) / 3);
  row-gap: calc(var(--block-space) / 2);
  padding-inline: calc(-0.5ch + var(--sidebar-inline-space));
  position: sticky;
  scroll-snap-type: none; /* override utility so scrolling works freely */
  z-index: 3;

  @media (max-width: 67.4375rem) {
    box-shadow: none;
    flex: 0 1 auto;
    grid-template-columns: repeat(auto-fill, minmax(64px, max-content));
    inset: auto;
    justify-items: start;
    margin-block-end: 0;
    -webkit-mask-image: none;
    mask-image: none;
    max-block-size: 28dvh;
    min-block-size: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding-block: 0;
    padding-inline: var(--inline-space-double);
    position: relative;

    /* Small spacer so text clears the edge on mobile */
    &::after {
      content: "";
      block-size: 0.25em;
      grid-column: 1 / -1;
    }
  }

  -webkit-mask-image: linear-gradient(to bottom, oklch(0% 0 0 / 1) calc(100% - 0.75em), oklch(0% 0 0 / 0) 100%);
  mask-image: linear-gradient(to bottom, oklch(0% 0 0 / 1) calc(100% - 0.75em), oklch(0% 0 0 / 0) 100%);

  @media (min-width: 67.5rem) {
    position: relative;
    inset: auto;
    max-block-size: none;
    overflow: visible;
    overflow-y: visible;
    -webkit-mask-image: none;
    mask-image: none;
  }

  &.directs--pinned {
    -webkit-mask-image: none;
    mask-image: none;
    gap: var(--inline-space);

    &::after {
      content: "";
      block-size: 0.1em;
    }

    grid-template-columns: repeat(auto-fill, minmax(80px, max-content));
    margin-block-end: 0;
    max-block-size: none;
    padding-block: calc(var(--block-space) / 2);
    position: relative;

    .direct {
      --avatar-size: 7ch;
      --group-size: 7ch;
    }

    @media (min-width: 67.5rem) {
      overflow: visible;
      overflow-y: visible;
    }

    @media (max-width: 67.4375rem) {
      flex: 0 0 auto;
      gap: var(--inline-space);
      grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
      max-block-size: 30dvh;
      min-block-size: 0;
      overflow-y: auto;
      padding-block: 0.25em;
      -webkit-overflow-scrolling: touch;

      .direct {
        --avatar-size: 7ch;
        --group-size: 7ch;
      }
    }
  }

  &.directs--pinned.directs--pinned-empty {
    align-content: center;
    justify-items: center;
    min-block-size: 0;
    padding-block: 0.25em;
    gap: 0;
    row-gap: 0;

    &::after {
      content: none;
    }

    .directs__empty {
      margin: 0;
      padding: 0;
      line-height: 1;
    }

    @media (max-width: 67.4375rem) {
      min-block-size: 0;
      padding-block: 0.25em;
      gap: 0;
      row-gap: 0;
    }
  }

  &.directs--new {
    --column-gap: var(--inline-space);
    --row-gap: 2px;

    -webkit-mask-image: none;
    mask-image: none;
    padding-inline-end: var(--sidebar-inline-space);
  }

}

.directs__empty {
  color: var(--color-muted);
  font-size: 0.75rem;
  font-style: italic;
  grid-column: 1 / -1;
  letter-spacing: 0.02em;
  text-align: center;
}

.rooms--pinned {
  gap: calc(var(--inline-space) * 1.25);
  margin-block-end: 0;
  padding-block-end: var(--block-space);

  @media (max-width: 67.4375rem) {
    flex: 0 1 auto;
    gap: calc(var(--inline-space) * 1.25);
    max-block-size: 20dvh;
    min-block-size: 0;
    overflow-y: auto;
    padding-block-end: var(--block-space);
    -webkit-overflow-scrolling: touch;
  }
}

/* Section headings for sidebar tiers */
.sidebar__heading {
  align-items: center;
  color: var(--color-muted);
  display: flex;
  flex-shrink: 0;
  font-size: 0.65rem;
  font-weight: 600;
  gap: 0.5em;
  letter-spacing: 0.08em;
  padding-inline: var(--sidebar-inline-space);
  text-transform: uppercase;

  &::after {
    border-block-start: 1px solid var(--color-border-darker);
    content: "";
    flex: 1;
  }

  @media (max-width: 67.4375rem) {
    padding-inline: var(--inline-space-double);
  }
}

.directs--edit  {
  display: grid;
  gap: var(--inline-space);
  grid-template-columns: repeat(auto-fit, minmax(33%, 1fr));
  grid-template-rows: min-content;
  place-items: center;

  .member {
    aspect-ratio: 1;
    block-size: auto;
    inline-size: 100%;
    margin-inline: auto;
    place-content: center;
  }
}

.direct {
  --avatar-size: 5ch;

  border-radius: var(--sidebar-radius);
  box-shadow: none;
  color: var(--color-text);
  display: grid;
  inline-size: var(--avatar-size);
  justify-items: center;
  position: relative;
  text-decoration: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;

  .avatar,
  .avatar__group {
    margin-inline: 0;
  }

  @media (any-hover: hover) {
    &:where(:not(:active):hover) .avatar:not(.avatar--icon) {
      filter: brightness(0.7);
    }
  }

  &:focus-within,
  &:where(:not(:active)):focus-visible {
    outline: 0;
  }

  &.current {
    > .avatar {
      box-shadow: 0 0 0 3px var(--color-link);
    }

    .direct__author {
      font-weight: 600;
    }
  }

  &.is-drag-ready {
    box-shadow: 0 0 0 2px var(--color-link);
    touch-action: none;
  }

  &.is-dragging {
    box-shadow: 0 0 0 2px var(--color-link), 0 6px 18px oklch(0% 0 0 / 0.3);
    opacity: 0.9;
    z-index: 10;
    touch-action: none;
  }
}

.directs__placeholder .direct {
  cursor: pointer;
}

.direct__author {
  --column-gap: 0.3ch;

  font-size: 0.75rem;
  line-height: 1.1;
  margin-block-start: 0.05em;

  .unread & {
    font-weight: 800;
    position: relative;

    &::after {
      --size: 0.6em;

      aspect-ratio: 1;
      background-color: var(--color-text);
      block-size: var(--size);
      border-radius: calc(var(--size) * 2);
      content: "";
      flex-shrink: 0;
      inline-size: var(--size);
      position: absolute;
      inset: 1.3em auto auto 50%;
      transform: translate(-50%);
    }
  }
}

/* Rooms */
.rooms {
  display: grid;
  gap: var(--inline-space-half);
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  padding-inline: var(--sidebar-inline-space);
  padding-block-start: 0;
  padding-block-end: var(--sidebar-tools-height);
  margin: 0;
  margin-block-start: 0.6em;

  /* Mobile home: independent scroll, take remaining space */
  @media (max-width: 67.4375rem) {
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    gap: var(--inline-space);
    padding-inline: var(--inline-space-double);
    flex: 1 1 0;
    min-block-size: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    align-content: start;
  }
}

html[data-theme="mideval"] .rooms {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  padding-inline: var(--sidebar-inline-space);

  @media (max-width: 67.4375rem) {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    padding-inline: var(--inline-space-double);
  }
}

.rooms__new-btn {
  --btn-border-radius: var(--sidebar-radius);

  border-radius: var(--sidebar-radius);
  border: 0;
  margin: 0;
}

.room {
  --btn-border-radius: var(--sidebar-radius);
  --btn-border-size: 1px;

  background-color: var(--color-text-reversed);
  border-radius: var(--sidebar-radius);
  border: 1px solid var(--color-border-darker);
  color: var(--color-text);
  font-weight: normal;
  justify-content: center;
  padding-block: 0.65em;
  padding-inline: 0.5em;
  font-size: 1rem;
  margin: 0;
  box-shadow: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;

  @media (any-hover: hover) {
    &:where(:not(.current):not(:active):hover) {
      filter: brightness(0.9);
    }
  }

  &.is-drag-ready {
    /* Visual feedback when long-press triggers drag mode (before moving) */
    box-shadow: 0 0 0 2px var(--color-link);
    touch-action: none;
  }

  &.is-dragging {
    box-shadow: 0 0 0 2px var(--color-link), 0 6px 18px oklch(0% 0 0 / 0.3);
    opacity: 0.9;
    z-index: 10;
    touch-action: none; /* prevent scroll/zoom during touch drag */
  }

  .searches-list & {
    border-radius: var(--sidebar-radius);
  }

  &.unread {
    --hover-color: var(--color-text);

    font-weight: 600;

    &:not(:hover) {
      border-color: var(--color-text);
    }
  }

  &.current {
    background-color: var(--color-selected);
    color: var(--color-text);
    font-weight: 600;

    &:not(:hover) {
      border-color: var(--color-selected);
    }

    @media (any-hover: hover) {
      &:where(:not(:active):hover) {
        background-color: var(--color-selected);
        color: var(--color-text);
      }
    }
  }
}

@media (hover: hover) and (pointer: fine) {
  .room[data-sortable-list-target="item"] {
    -webkit-user-drag: element;
  }
}

/* Only apply to current room pill in sidebar list, not the nav room title (.room--current) */
#sidebar .room.current {
  --btn-border-radius: var(--sidebar-radius);
  --hover-filter: none;
  --num-buttons: 1;

  min-block-size: var(--btn-size);

  .room__contents {
    .account-has-logo & {
      --num-buttons: 2;
    }

    max-inline-size: calc(100dvw - (var(--btn-size) * var(--num-buttons)) - (var(--btn-size) + max(var(--inline-space), 1vw)) - ((var(--inline-space) + 1.8em) * 2) - var(--sidebar-width) - var(--inline-space) * var(--num-buttons));

    @media (max-width: 67.4375rem) {
      --num-buttons: 2;

      .account-has-logo & {
        --num-buttons: 3;
      }
    }
  }
}

/* Missed calls page */
.missed-calls-page__title {
  margin: 0;
}

.missed-calls-page__content {
  max-inline-size: 40rem;
  margin-inline: auto;
}

.missed-calls-list {
  list-style: none;
  margin: 0;
  padding: 0;
}

.missed-calls-list__item {
  --border-radius: var(--sidebar-radius);

  background: var(--color-bg);
  border: 1px solid var(--color-border);
}

.missed-calls-list__link {
  color: inherit;
  text-decoration: none;
}

.missed-calls-list__link:hover {
  color: inherit;
}

.missed-calls-list__info {
  gap: 0.15em;
}

.missed-calls-list__name {
  font-weight: 600;
}

.missed-calls-list__delete {
  background: none;
  border: none;
  color: var(--color-muted);
}

.missed-calls-list__delete:hover {
  color: var(--color-negative);
}

.missed-calls-page__empty figure img {
  opacity: 0.5;
}

/* Voice calls index */
.voice-calls-index {
  list-style: none;
  margin: 0;
  padding: 0;
  max-inline-size: 40rem;
  margin-inline: auto;
}

.voice-calls-index__item {
  --border-radius: var(--sidebar-radius);

  background: var(--color-bg);
  border: 1px solid var(--color-border);
  transition: border-color 0.15s ease, box-shadow 0.15s ease, transform 0.15s ease;
}

.voice-calls-index__link {
  color: inherit;
  text-decoration: none;
  width: 100%;
}

.voice-calls-index__link:hover {
  color: inherit;
}

.voice-calls-index__item:hover {
  border-color: var(--color-border-dark);
  box-shadow: 0 0.3rem 0.9rem rgba(0, 0, 0, 0.2);
  transform: translateY(-1px);
}

/* Searches */
.searches {
  #nav {
    align-items: start;
    column-gap: 0;
    padding-inline-end: 0;
  }

  .rooms {
    padding-block-start: var(--block-space);
  }

  .message--formatted .message__room {
    display: block;
    font-size: 0.8rem;
    font-weight: 400;
    overflow: hidden;
    text-overflow: ellipsis;
    visibility: visible;
    white-space: nowrap;

    &::before {
      content: "→";
    }

    .message--me & {
      &::after {
        content: "←";
      }

      &::before {
        content: "";
      }
    }
  }

  .message__actions {
    display: none !important;
    visibility: hidden !important;
  }
}

.searches__recents {
  --mask: linear-gradient(to left, oklch(0% 0 0 / 1) 97%, oklch(0% 0 0 / 0) 99%);

  -webkit-mask-image: var(--mask);
  display: none;
  mask-image: var(--mask);
  position: relative;

  @media (max-width: 67.4375rem) {
    display: flex;
  }

  .room {
    max-inline-size: 20ch;

    &:first-child {
      margin-inline-start: var(--inline-space);
    }
  }

  .searches__btn {
    margin-inline-end: var(--inline-space-double);
  }
}

.searches__query {
  --btn-border-radius: var(--sidebar-radius);

  min-block-size: var(--btn-size);
}

.searches__results {
  padding-block-start: var(--navbar-height);
}

.searches__input:required:invalid {
  ~ .searches__reset {
    display: none;
    visibility: hidden;
  }
}
