/* Field comms: a drawer over the right rail, not a card in the plate. The
   closed state is a narrow edge tab; opening it covers HUD glass, never the
   centre of the map. */
.chat {
  position: absolute;
  top: calc(var(--rail-inset) + 58px);
  right: 0;
  left: auto;
  z-index: 11;
  /* Open comms covers the right rail plus its outer inset. Its left edge
     lands exactly on the rail, so it never covers the plate. */
  width: calc(var(--rail-right) + var(--rail-inset));
  max-width: calc(var(--rail-right) + var(--rail-inset));
  padding: 0;
  overflow: hidden;
  border-radius: 14px 0 0 14px;
  text-transform: none;
  letter-spacing: 0;
  pointer-events: auto;
  --channel: 142, 232, 255;
  --channel-ink: #c9f6ff;
  transition:
    width 220ms cubic-bezier(0.22, 1, 0.36, 1),
    top 220ms cubic-bezier(0.22, 1, 0.36, 1),
    opacity 180ms ease,
    border-color 180ms ease;
}

.chat[data-channel='suggestions'] {
  --channel: 255, 201, 125;
  --channel-ink: #ffe4b9;
}

.chat[data-channel='sightings'] {
  --channel: 154, 241, 195;
  --channel-ink: #d5ffe8;
}

.chat.closed {
  top: calc(var(--rail-inset) + 74px);
  width: 42px;
  max-width: 42px;
  border-radius: 12px 0 0 12px;
}

/* The tab is still a visible channel: a status pip, the name running vertically,
   and an unread count. The log itself stays out of the plate until opened. */
.chat.closed .chat-head {
  position: relative;
  flex-direction: column;
  justify-content: flex-start;
  gap: 8px;
  min-height: 174px;
  padding: 11px 0 10px;
  background: linear-gradient(180deg, rgba(var(--channel), 0.16), rgba(var(--channel), 0.035));
}

.chat.closed .chat-head b {
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  font-size: 8px;
  letter-spacing: 0.22em;
}

.chat.closed .chat-online,
.chat.closed .chat-chevron,
.chat.closed .chat-peek {
  display: none;
}

.chat.closed .chat-unread {
  position: absolute;
  top: 7px;
  right: 7px;
}

.chat.closed .chat-key {
  order: 3;
}

/* Reading or death owns the glass; the room still exists, it just stops
   crowding the page. The command view keeps the map clean as well. */
body.journal-open .chat,
body.colony-lost .chat,
body.command-view .chat {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.chat-head {
  position: relative;
  display: flex;
  align-items: center;
  gap: 7px;
  width: 100%;
  min-height: 38px;
  padding: 9px 12px;
  border: 0;
  background:
    linear-gradient(90deg, rgba(var(--channel), 0.08), transparent 66%),
    transparent;
  font: inherit;
  font-size: 9px;
  text-align: left;
  text-transform: uppercase;
  letter-spacing: 0.18em;
  color: var(--dim);
  cursor: pointer;
  pointer-events: auto;
  transition: background 160ms ease, color 160ms ease;
}

.chat-head:hover {
  background:
    linear-gradient(90deg, rgba(var(--channel), 0.14), transparent 70%),
    rgba(127, 232, 255, 0.04);
  color: rgba(223, 249, 255, 0.92);
}

.chat-head:focus-visible {
  outline: 1px solid rgba(var(--channel), 0.55);
  outline-offset: -2px;
}

.chat-pip {
  flex: 0 0 auto;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: rgba(128, 156, 164, 0.6);
  box-shadow: 0 0 0 2px rgba(128, 156, 164, 0.1);
}

.chat[data-status='connected'] .chat-pip {
  background: #8ef0c0;
  box-shadow: 0 0 8px rgba(142, 240, 192, 0.7);
}

.chat[data-status='connecting'] .chat-pip,
.chat[data-status='disconnected'] .chat-pip {
  background: #ffd28a;
  box-shadow: 0 0 8px rgba(255, 210, 138, 0.55);
  animation: chat-pulse 1.4s ease-in-out infinite;
}

@keyframes chat-pulse {
  50% {
    opacity: 0.35;
  }
}

.chat-head b {
  font-size: 9px;
  font-weight: 500;
  letter-spacing: 0.18em;
  color: rgba(213, 246, 255, 0.92);
  white-space: nowrap;
}

.chat-key {
  display: grid;
  place-items: center;
  min-width: 15px;
  height: 15px;
  border: 1px solid rgba(127, 232, 255, 0.18);
  border-radius: 4px;
  background: rgba(127, 232, 255, 0.06);
  font-size: 7px;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: rgba(178, 232, 244, 0.78);
}

.chat-online {
  margin-left: auto;
  font-size: 7.5px;
  letter-spacing: 0.14em;
  color: rgba(131, 177, 188, 0.9);
  white-space: nowrap;
}

.chat-unread {
  display: grid;
  place-items: center;
  min-width: 16px;
  height: 16px;
  padding: 0 4px;
  border: 1px solid rgba(255, 178, 92, 0.34);
  border-radius: 999px;
  background: rgba(255, 178, 92, 0.18);
  font-size: 7px;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: #ffd8a6;
}

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

.chat-chevron {
  width: 5px;
  height: 5px;
  margin-left: 1px;
  border-right: 1px solid rgba(178, 230, 242, 0.72);
  border-bottom: 1px solid rgba(178, 230, 242, 0.72);
  transform: rotate(45deg) translateY(-1px);
  transition: transform 200ms ease;
}

.chat:not(.closed) .chat-chevron {
  transform: rotate(225deg) translateY(-1px);
}

/* Closed is still a channel: the header stays and the newest line remains
   readable in a one-line preview. */
.chat:not(.closed) .chat-peek,
.chat.closed .chat-peek {
  display: none;
}

.chat-peek {
  display: grid;
  gap: 2px;
  padding: 0 12px 10px;
  border-top: 1px solid rgba(127, 232, 255, 0.07);
}

.chat-peek-channel {
  padding-top: 7px;
  font-size: 6.5px;
  font-weight: 600;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: rgba(var(--channel), 0.78);
}

.chat-peek-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 9px;
  line-height: 1.4;
  color: rgba(190, 227, 235, 0.7);
}

.chat-body {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto auto;
  gap: 8px;
  padding: 0 12px 11px;
}

.chat.closed .chat-body {
  display: none;
}

.chat-channels {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 3px;
  padding: 3px;
  border: 1px solid rgba(127, 232, 255, 0.1);
  border-radius: 9px;
  background: rgba(1, 8, 11, 0.35);
}

.chat-channel {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-width: 0;
  padding: 6px 3px 5px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  font: inherit;
  font-size: 7px;
  font-weight: 500;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: rgba(132, 170, 180, 0.8);
  cursor: pointer;
  transition: color 150ms ease, background 150ms ease, border-color 150ms ease;
}

.chat-channel:hover {
  color: rgba(221, 249, 255, 0.92);
  background: rgba(127, 232, 255, 0.06);
}

.chat-channel:focus-visible {
  outline: 1px solid rgba(var(--channel), 0.48);
  outline-offset: -2px;
}

.chat-channel[data-chat-channel='general'] {
  --channel: 142, 232, 255;
}

.chat-channel[data-chat-channel='suggestions'] {
  --channel: 255, 201, 125;
}

.chat-channel[data-chat-channel='sightings'] {
  --channel: 154, 241, 195;
}

.chat-channel[aria-selected='true'] {
  border-color: rgba(var(--channel), 0.26);
  background: linear-gradient(180deg, rgba(var(--channel), 0.16), rgba(var(--channel), 0.045));
  color: var(--channel-ink);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08);
}

.chat-channel:active {
  transform: translateY(1px);
}

.chat-channel-count {
  display: grid;
  place-items: center;
  min-width: 12px;
  height: 12px;
  padding: 0 3px;
  border-radius: 999px;
  background: rgba(255, 178, 92, 0.2);
  font-size: 6px;
  font-weight: 700;
  letter-spacing: 0;
  color: #ffd8a6;
}

.chat-channel-count[hidden] {
  display: none;
}

.chat-channel-copy {
  min-height: 10px;
  margin: -1px 1px 0;
  font-size: 6.5px;
  line-height: 1.35;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: rgba(118, 156, 167, 0.72);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-log {
  min-height: 92px;
  max-height: 176px;
  display: flex;
  flex-direction: column;
  gap: 7px;
  padding: 2px 3px 2px 1px;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: rgba(127, 232, 255, 0.25) transparent;
}

.chat-log:empty::after {
  content: attr(data-empty);
  display: block;
  padding: 22px 4px;
  font-size: 7.5px;
  line-height: 1.5;
  letter-spacing: 0.12em;
  text-align: center;
  text-transform: uppercase;
  color: rgba(117, 153, 164, 0.66);
}

.chat-line {
  padding-left: 8px;
  border-left: 1px solid var(--tone, #8ee8ff);
  box-shadow: -6px 0 14px -8px var(--tone, #8ee8ff);
  animation: chat-in 220ms ease both;
}

@keyframes chat-in {
  from {
    opacity: 0;
    transform: translateY(3px);
  }
}

.chat-meta {
  display: flex;
  align-items: baseline;
  gap: 6px;
  margin-bottom: 1px;
}

.chat-meta b {
  max-width: 62%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 7.5px;
  font-weight: 600;
  letter-spacing: 0.13em;
  text-transform: uppercase;
  color: var(--tone, #8ee8ff);
}

.chat-line.self .chat-meta b {
  color: #bdf4ff;
}

.chat-meta time {
  margin-left: auto;
  font-size: 6.5px;
  letter-spacing: 0.08em;
  color: rgba(133, 169, 179, 0.62);
  font-variant-numeric: tabular-nums;
}

.chat-text {
  display: block;
  font-size: 10.5px;
  line-height: 1.48;
  letter-spacing: 0.01em;
  color: rgba(214, 240, 246, 0.9);
  overflow-wrap: anywhere;
  user-select: text;
}

.chat-line.self .chat-text {
  color: rgba(229, 253, 255, 0.96);
}

.chat-line.developer {
  margin: 2px -4px;
  padding: 5px 7px 6px 10px;
  border-left-color: #a5ffe9;
  border-radius: 0 7px 7px 0;
  background: linear-gradient(90deg, rgba(124, 255, 230, 0.11), rgba(157, 135, 255, 0.06) 56%, transparent);
  box-shadow:
    -7px 0 19px -9px rgba(129, 255, 235, 0.9),
    inset 0 1px 0 rgba(226, 255, 255, 0.05);
  animation: chat-in 220ms ease both, chat-developer-pulse 4.8s ease-in-out infinite;
}

@keyframes chat-developer-pulse {
  0%, 100% { box-shadow: -7px 0 15px -10px rgba(129, 255, 235, 0.72), inset 0 1px 0 rgba(226, 255, 255, 0.05); }
  50% { box-shadow: -7px 0 23px -7px rgba(151, 255, 239, 1), inset 0 1px 0 rgba(226, 255, 255, 0.08); }
}

.chat-line.developer .chat-meta b {
  color: #e9ffff;
  text-shadow: 0 0 10px rgba(153, 255, 240, 0.52);
}

.chat-line.tester {
  margin: 2px -4px;
  padding: 5px 7px 6px 10px;
  border-left-color: #ffdc91;
  border-radius: 0 7px 7px 0;
  background: linear-gradient(90deg, rgba(255, 218, 143, 0.13), rgba(140, 255, 224, 0.06) 56%, transparent);
  box-shadow:
    -7px 0 19px -9px rgba(255, 215, 130, 0.85),
    inset 0 1px 0 rgba(255, 251, 226, 0.06);
  animation: chat-in 220ms ease both, chat-tester-pulse 4.8s ease-in-out infinite;
}

@keyframes chat-tester-pulse {
  0%, 100% { box-shadow: -7px 0 15px -10px rgba(255, 215, 130, 0.65), inset 0 1px 0 rgba(255, 251, 226, 0.05); }
  50% { box-shadow: -7px 0 23px -7px rgba(255, 231, 160, 0.96), inset 0 1px 0 rgba(255, 251, 226, 0.09); }
}

.chat-line.tester .chat-meta b {
  color: #fff6d7;
  text-shadow: 0 0 10px rgba(255, 221, 143, 0.55);
}

.chat-role-title {
  margin: 2px 0 4px;
  font-size: 7.2px;
  letter-spacing: 0.2em;
}

.chat-line.tester .chat-role-title {
  font-size: 7.6px;
  letter-spacing: 0.18em;
}

@media (prefers-reduced-motion: reduce) {
  .chat-line.developer,
  .chat-line.tester { animation: chat-in 220ms ease both; }
}

.chat-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 6px;
  align-items: stretch;
  padding-top: 8px;
  border-top: 1px solid rgba(127, 232, 255, 0.09);
}

.chat-input {
  width: 100%;
  min-width: 0;
  padding: 8px 9px;
  border: 1px solid rgba(127, 232, 255, 0.16);
  border-radius: 8px;
  background: rgba(3, 12, 16, 0.55);
  color: #e6fbff;
  font: inherit;
  font-size: 10.5px;
  letter-spacing: 0.02em;
  text-transform: none;
  outline: none;
  pointer-events: auto;
  user-select: text;
  transition: border-color 150ms ease, box-shadow 150ms ease;
}

.chat-input::placeholder {
  color: rgba(123, 160, 171, 0.62);
}

.chat-input:focus {
  border-color: rgba(var(--channel), 0.48);
  box-shadow: 0 0 0 2px rgba(var(--channel), 0.08);
}

.chat-input:disabled {
  opacity: 0.45;
}

.chat-send {
  padding: 0 10px;
  border: 1px solid rgba(var(--channel), 0.25);
  border-radius: 8px;
  background: linear-gradient(180deg, rgba(var(--channel), 0.15), rgba(var(--channel), 0.04));
  color: var(--channel-ink);
  font: inherit;
  font-size: 7.5px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  cursor: pointer;
  pointer-events: auto;
  transition: background 150ms ease, border-color 150ms ease, opacity 150ms ease;
}

.chat-send:hover:not(:disabled) {
  border-color: rgba(var(--channel), 0.44);
  background: linear-gradient(180deg, rgba(var(--channel), 0.25), rgba(var(--channel), 0.07));
}

.chat-send:disabled {
  opacity: 0.4;
  cursor: default;
}

.chat-note {
  min-height: 10px;
  margin: 0;
  font-size: 6.5px;
  line-height: 1.4;
  letter-spacing: 0.13em;
  text-transform: uppercase;
  color: rgba(121, 158, 169, 0.7);
}

.chat-note.warn {
  color: rgba(255, 196, 120, 0.92);
}

.chat-note.muted {
  color: rgba(113, 145, 155, 0.62);
}

@media (max-width: 1180px) {
  .chat {
    top: calc(var(--rail-inset) + 52px);
    width: calc(var(--rail-right) + var(--rail-inset));
    max-width: calc(var(--rail-right) + var(--rail-inset));
  }

  .chat.closed {
    top: calc(var(--rail-inset) + 66px);
    width: 40px;
    max-width: 40px;
  }

  .chat-log {
    max-height: 150px;
  }
}

@media (max-width: 940px) {
  .chat {
    top: calc(var(--rail-inset) + 46px);
    width: calc(var(--rail-right) + var(--rail-inset));
    max-width: calc(var(--rail-right) + var(--rail-inset));
  }

  .chat.closed {
    top: calc(var(--rail-inset) + 60px);
    width: 38px;
    max-width: 38px;
  }

  .chat-channel {
    font-size: 6.5px;
    letter-spacing: 0.06em;
  }

  .chat-channel-copy {
    font-size: 6px;
  }

  .chat-log {
    max-height: 128px;
  }
}

@media (max-width: 760px) {
  .chat {
    top: calc(var(--rail-inset) + 52px);
    right: 0;
    left: auto;
    width: min(300px, calc(100vw - 12px));
    max-width: calc(100vw - 12px);
  }

  .chat.closed {
    top: calc(var(--rail-inset) + 62px);
    width: 38px;
    max-width: 38px;
  }

  .chat-log {
    max-height: 140px;
  }
}

@media (max-height: 700px) {
  .chat-log {
    max-height: 112px;
  }

  .chat-peek-text {
    font-size: 8.5px;
  }
}

@media (max-height: 560px) {
  .chat-log {
    min-height: 52px;
    max-height: 86px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .chat,
  .chat-line,
  .chat-chevron,
  .chat-channel {
    animation: none;
    transition: none;
  }
}
