/* ==========================================================================
   App-wide chat: docked button, toast, and slide-over / bottom-sheet drawer.
   Warm charcoal surfaces (#222 / #2a2a2a / #292826), sand accent (#afa38c),
   light text (#eceae5), bold wide-tracked uppercase type.
   ========================================================================== */

.app-chat-safe-probe {
  position: fixed;
  top: 0;
  left: 0;
  width: 0;
  height: 0;
  padding-top: var(--pwa-safe-top, env(safe-area-inset-top, 0px));
  padding-bottom: var(--pwa-safe-bottom, env(safe-area-inset-bottom, 0px));
  visibility: hidden;
  pointer-events: none;
}

/* ---------- Docked button ---------- */
.app-chat-fab {
  position: fixed;
  right: calc(10px + var(--pwa-safe-right, 0px));
  top: 58vh;
  z-index: 9990;
  width: 44px;
  height: 44px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-pill, 999px);
  border: 1px solid rgba(175, 163, 140, 0.45);
  background: rgba(34, 34, 34, 0.92);
  color: #eceae5;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  cursor: pointer;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  opacity: 0.86;
  transition: opacity 0.2s ease, border-color 0.2s ease, transform 0.15s ease;
}

.app-chat-fab[hidden] {
  display: none;
}

.app-chat-fab:hover,
.app-chat-fab:focus-visible,
.app-chat-fab.has-unread {
  opacity: 1;
  border-color: #afa38c;
}

.app-chat-fab:active {
  transform: scale(0.94);
}

.app-chat-fab.is-dragging {
  opacity: 1;
  transition: none;
  cursor: grabbing;
}

.app-chat-fab-badge {
  position: absolute;
  top: -5px;
  right: -4px;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  box-sizing: border-box;
  border-radius: var(--radius-pill, 999px);
  background: #e2a045;
  color: #1a1a1a;
  font-size: 0.66rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  line-height: 18px;
  text-align: center;
  box-shadow: 0 0 0 2px #222222;
}

.app-chat-fab-badge[hidden] {
  display: none;
}

/* Phones: a slim tab docked to the right edge so it covers as little as possible */
@media (max-width: 899px), (pointer: coarse) {
  .app-chat-fab {
    right: var(--pwa-safe-right, 0px);
    width: 38px;
    padding-left: 3px;
    border-right: 0;
    border-radius: 22px 0 0 22px;
  }

  .app-chat-fab-badge {
    right: auto;
    left: -7px;
  }
}

/* ---------- Toast ---------- */
.app-chat-toast {
  position: fixed;
  top: calc(10px + var(--pwa-safe-top, 0px));
  left: 50%;
  z-index: 10070;
  width: min(360px, calc(100vw - 24px));
  box-sizing: border-box;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-radius: var(--radius-md, 10px);
  border: 1px solid rgba(175, 163, 140, 0.35);
  background: rgba(34, 34, 34, 0.97);
  color: #eceae5;
  box-shadow: var(--shadow-modal, 0 12px 40px rgba(0, 0, 0, 0.55));
  transform: translate(-50%, -16px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  text-transform: uppercase;
}

.app-chat-toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
  pointer-events: auto;
}

.app-chat-toast.is-clickable {
  cursor: pointer;
}

.app-chat-toast-avatar {
  flex: 0 0 auto;
  font-size: 1.35rem;
  line-height: 1;
}

.app-chat-toast-text {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.app-chat-toast-title {
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: #afa38c;
}

.app-chat-toast-msg {
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------- Drawer shell ---------- */
.app-chat-backdrop {
  position: fixed;
  inset: 0;
  z-index: 10055;
  background: rgba(0, 0, 0, 0.42);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

.app-chat-backdrop.is-open {
  opacity: 1;
  pointer-events: auto;
}

.app-chat-drawer {
  --app-chat-kb: 0px;
  position: fixed;
  z-index: 10060;
  left: 0;
  right: 0;
  top: calc(var(--pwa-safe-top, 0px) + 48px);
  bottom: var(--app-chat-kb);
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  background: #222222;
  color: #eceae5;
  border: 1px solid var(--surface-border, rgba(175, 163, 140, 0.25));
  border-bottom: 0;
  border-radius: var(--radius-lg, 16px) var(--radius-lg, 16px) 0 0;
  box-shadow: var(--shadow-modal, 0 12px 40px rgba(0, 0, 0, 0.55));
  font-family: var(--app-font-family);
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  transform: translateY(104%);
  visibility: hidden;
  transition: transform 0.24s ease, visibility 0s linear 0.24s;
}

.app-chat-drawer.is-open {
  transform: translateY(0);
  visibility: visible;
  transition: transform 0.24s ease, visibility 0s;
}

@media (min-width: 900px) {
  .app-chat-drawer {
    left: auto;
    right: calc(16px + var(--pwa-safe-right, 0px));
    top: calc(16px + var(--pwa-safe-top, 0px));
    bottom: calc(16px + var(--pwa-safe-bottom, 0px));
    width: 384px;
    border-bottom: 1px solid var(--surface-border, rgba(175, 163, 140, 0.25));
    border-radius: var(--radius-lg, 16px);
    transform: translateX(calc(100% + 32px));
  }

  .app-chat-drawer.is-open {
    transform: translateX(0);
  }

  .app-chat-backdrop {
    background: rgba(0, 0, 0, 0.18);
  }
}

/* ---------- Header ---------- */
.app-chat-head {
  position: relative;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 52px;
  padding: 6px 8px 6px 12px;
  box-sizing: border-box;
  border-bottom: 1px solid var(--surface-border-subtle, rgba(175, 163, 140, 0.18));
  background: #292826;
}

.app-chat-head-main {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}

.app-chat-drawer[data-view="inbox"] .app-chat-head-main {
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
}

.app-chat-head-text {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.app-chat-title {
  font-size: 0.95rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: #eceae5;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.app-chat-subtitle {
  font-size: 0.66rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  color: #afa38c;
}

.app-chat-icon-btn {
  flex: 0 0 auto;
  width: 36px;
  height: 36px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm, 6px);
  border: 1px solid transparent;
  background: transparent;
  color: #eceae5;
  cursor: pointer;
}

.app-chat-icon-btn:hover,
.app-chat-icon-btn:focus-visible {
  border-color: rgba(175, 163, 140, 0.35);
  background: rgba(175, 163, 140, 0.08);
}

.app-chat-drawer[data-view="inbox"] .app-chat-back {
  display: none;
}

/* ---------- Search (inbox only) ---------- */
.app-chat-search-wrap {
  flex-shrink: 0;
  padding: 8px 12px;
  border-bottom: 1px solid var(--surface-border-subtle, rgba(175, 163, 140, 0.18));
}

.app-chat-drawer[data-view="thread"] .app-chat-search-wrap {
  display: none;
}

.app-chat-search {
  width: 100%;
  box-sizing: border-box;
  height: 36px;
  padding: 0 12px;
  border-radius: var(--radius-input, 8px);
  border: 1px solid rgba(175, 163, 140, 0.25);
  background: #2a2a2a;
  color: #eceae5;
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}

.app-chat-search:focus {
  border-color: #afa38c;
}

.app-chat-search::placeholder,
.app-chat-input::placeholder {
  color: rgba(236, 234, 229, 0.42);
}

/* ---------- Single scroll pane ---------- */
.app-chat-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  overscroll-behavior: contain;
  padding: 6px 10px 12px;
  box-sizing: border-box;
}

.app-chat-section {
  padding: 12px 4px 6px;
  font-size: 0.64rem;
  font-weight: 700;
  letter-spacing: 0.14em;
  color: #afa38c;
}

.app-chat-row {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  margin: 0 0 4px;
  box-sizing: border-box;
  border-radius: var(--radius-md, 10px);
  border: 1px solid transparent;
  background: rgba(41, 40, 38, 0.55);
  color: #eceae5;
  text-align: left;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.app-chat-row:hover,
.app-chat-row:focus-visible {
  background: rgba(41, 40, 38, 0.95);
  border-color: rgba(175, 163, 140, 0.3);
}

.app-chat-row.is-unread {
  border-color: rgba(226, 160, 69, 0.45);
}

.app-chat-avatar {
  position: relative;
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: #2f2d2a;
  border: 1px solid rgba(175, 163, 140, 0.25);
  font-size: 1.25rem;
  line-height: 1;
}

.app-chat-avatar.sm {
  width: 32px;
  height: 32px;
  font-size: 1.05rem;
}

.app-chat-avatar.is-team {
  color: #afa38c;
  font-size: 1.1rem;
  font-weight: 700;
}

.app-chat-dot {
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #6bcf63;
  box-shadow: 0 0 0 2px #222222;
}

.app-chat-row-main {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.app-chat-row-top {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.app-chat-row-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.86rem;
  font-weight: 700;
  letter-spacing: 0.07em;
}

.app-chat-role {
  flex: 0 0 auto;
  display: inline-block;
  padding: 1px 6px;
  border-radius: var(--radius-pill, 999px);
  border: 1px solid rgba(175, 163, 140, 0.6);
  color: #afa38c;
  font-size: 0.56rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  line-height: 1.5;
  vertical-align: middle;
}

.app-chat-row-sub {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  color: rgba(236, 234, 229, 0.55);
}

.app-chat-row.is-unread .app-chat-row-sub {
  color: #eceae5;
}

.app-chat-row-meta {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 4px;
}

.app-chat-row-time {
  font-size: 0.62rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: rgba(236, 234, 229, 0.45);
}

.app-chat-count {
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  box-sizing: border-box;
  border-radius: var(--radius-pill, 999px);
  background: #e2a045;
  color: #1a1a1a;
  font-size: 0.64rem;
  font-weight: 700;
  line-height: 18px;
  text-align: center;
}

.app-chat-empty {
  margin: 28px 12px;
  text-align: center;
  font-size: 0.74rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  color: rgba(236, 234, 229, 0.5);
}

/* ---------- Thread ---------- */
.app-chat-group {
  display: flex;
  flex-direction: column;
  gap: 3px;
  margin: 10px 0 2px;
  max-width: 82%;
}

.app-chat-group.is-mine {
  margin-left: auto;
  align-items: flex-end;
}

.app-chat-group.is-theirs {
  margin-right: auto;
  align-items: flex-start;
}

.app-chat-group-name {
  padding: 0 4px 2px;
  font-size: 0.6rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  color: #afa38c;
}

.app-chat-bubble {
  max-width: 100%;
  box-sizing: border-box;
  padding: 8px 12px;
  border-radius: 14px;
  font-size: 0.86rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  line-height: 1.35;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.app-chat-group.is-mine .app-chat-bubble {
  background: #afa38c;
  color: #1a1a1a;
  border-bottom-right-radius: 5px;
}

.app-chat-group.is-mine .app-chat-bubble + .app-chat-bubble {
  border-top-right-radius: 5px;
}

.app-chat-group.is-theirs .app-chat-bubble {
  background: #34322f;
  color: #eceae5;
  border: 1px solid rgba(175, 163, 140, 0.18);
  border-bottom-left-radius: 5px;
}

.app-chat-group.is-theirs .app-chat-bubble + .app-chat-bubble {
  border-top-left-radius: 5px;
}

.app-chat-meta {
  padding: 1px 4px 0;
  font-size: 0.58rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  color: rgba(236, 234, 229, 0.42);
}

.app-chat-typing {
  flex-shrink: 0;
  min-height: 0;
  padding: 0 14px;
  font-size: 0.62rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  color: #afa38c;
}

.app-chat-typing:not(:empty) {
  padding: 2px 14px 4px;
}

.app-chat-quick {
  flex-shrink: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px 12px 0;
  min-width: 0;
  overflow: hidden;
  border-top: 1px solid var(--surface-border-subtle, rgba(175, 163, 140, 0.18));
}

/* Keep the thread readable while the iOS keyboard takes half the screen */
.app-chat-drawer.has-keyboard .app-chat-quick {
  display: none;
}

.app-chat-chip {
  flex: 0 1 auto;
  max-width: 100%;
  height: 28px;
  padding: 0 10px;
  border-radius: var(--radius-pill, 999px);
  border: 1px solid rgba(175, 163, 140, 0.4);
  background: transparent;
  color: #eceae5;
  font-size: 0.66rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  cursor: pointer;
  white-space: nowrap;
}

.app-chat-chip:hover,
.app-chat-chip:focus-visible {
  background: rgba(175, 163, 140, 0.14);
  border-color: #afa38c;
}

.app-chat-compose {
  flex-shrink: 0;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px 6px;
  margin: 0;
}

.app-chat-input {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 40px;
  max-height: 112px;
  box-sizing: border-box;
  padding: 10px 12px;
  border-radius: var(--radius-md, 10px);
  border: 1px solid rgba(175, 163, 140, 0.3);
  background: #2a2a2a;
  color: #eceae5;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 0.05em;
  line-height: 1.25;
  text-transform: uppercase;
  resize: none;
  outline: none;
  overflow-y: auto;
}

.app-chat-input:focus {
  border-color: #afa38c;
}

.app-chat-send {
  flex: 0 0 auto;
  height: 40px;
  padding: 0 14px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border-radius: var(--radius-md, 10px);
  border: none;
  background: #afa38c;
  color: #1a1a1a;
  font-size: 0.74rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  cursor: pointer;
}

.app-chat-send:hover,
.app-chat-send:focus-visible {
  background: #c2b69e;
}

.app-chat-foot {
  flex-shrink: 0;
  padding: 2px 12px calc(8px + var(--pwa-safe-bottom, 0px));
  text-align: center;
  font-size: 0.56rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  color: rgba(236, 234, 229, 0.38);
}

.app-chat-drawer.has-keyboard .app-chat-foot {
  padding-bottom: 6px;
}

.app-chat-drawer[data-view="inbox"] .app-chat-typing,
.app-chat-drawer[data-view="inbox"] .app-chat-quick,
.app-chat-drawer[data-view="inbox"] .app-chat-compose {
  display: none;
}

@media (min-width: 900px) {
  .app-chat-foot {
    padding-bottom: 10px;
  }
}

/* ---------- Online Players popup: chat-only rows ---------- */
.online-user-item.is-chat-target {
  cursor: pointer;
}

.online-user-item .online-user-role {
  margin-left: 6px;
}

/* ---------- Online players who are mid-game (shown, not challengeable) ---------- */
.online-user-status {
  display: inline-block;
  margin-left: auto;
  padding: 3px 8px;
  border-radius: 999px;
  border: 1px solid rgba(214, 196, 160, 0.45);
  color: #d6c4a0;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  white-space: nowrap;
}

.online-user-item.is-busy {
  cursor: default;
}
