.chat-shell {
  --chat-safe-area-bottom: env(safe-area-inset-bottom, 0px);
  --chat-viewport-height: 100vh;
  display: grid;
  gap: 18px;
  grid-template-columns: minmax(240px, 300px) minmax(0, 1fr);
  align-items: start;
  margin: 16px auto 32px;
}

.chat-shell--compact {
  grid-template-columns: 1fr;
}

.chat-shell__sidebar,
.chat-shell__main,
.chat-schema-warning {
  min-width: 0;
}

.chat-shell__sidebar {
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 16px;
}

.chat-shell__section-head,
.chat-main__header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.chat-shell__section-head h2,
.chat-main__title,
.chat-schema-warning h2 {
  margin: 0;
  color: var(--c-accent, #ffc932);
  font-size: 18px;
}

.chat-shell__section-meta,
.chat-runtime__mode,
.chat-status-pill {
  flex-shrink: 0;
  white-space: nowrap;
}

.chat-room-list {
  display: grid;
  gap: 10px;
}

.chat-room-list__item {
  display: inline-flex;
  gap: 6px;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 8px 13px;
  border: 1px solid rgba(255,255,255,0.10);
  border-radius: 999px;
  background: rgba(255,255,255,0.04);
  color: var(--c-text, #eaeaea);
  cursor: pointer;
  transition: border-color 0.18s ease, transform 0.18s ease, background 0.18s ease;
}

.chat-room-list__item:hover,
.chat-room-list__item:focus-visible,
.chat-room-list__item.is-active {
  border-color: rgba(255, 201, 50, 0.45);
  background: rgba(255, 201, 50, 0.08);
  transform: translateY(-1px);
}

.chat-room-list__name {
  font-size: 14px;
  font-weight: 700;
}

.chat-room-list__meta,
.chat-composer__hint,
.chat-message__meta,
.chat-empty-state p,
.chat-main__subtitle {
  color: rgba(234, 234, 234, 0.72);
  font-size: 13px;
}

.chat-room-list__badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  min-height: 18px;
  padding: 0 6px;
  font-size: 10px;
  font-weight: 700;
  margin-left: auto;
  line-height: 1;
}

.chat-runtime__grid {
  display: grid;
  gap: 10px;
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.chat-runtime__card {
  display: grid;
  gap: 6px;
  padding: 12px;
  border-radius: 14px;
  background: rgba(255,255,255,0.04);
}

.chat-runtime__label {
  color: rgba(234, 234, 234, 0.68);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.chat-runtime__value {
  color: var(--c-text, #eaeaea);
  font-size: 14px;
}

.chat-shell__main {
  display: grid;
  grid-template-rows: auto minmax(360px, 1fr) auto;
  min-height: 70vh;
  padding: 18px;
  gap: 16px;
}

.chat-shell--compact .chat-shell__main {
  width: min(100%, 680px);
  margin: 0 auto;
}

.chat-shell--panel {
  margin: 0;
  gap: 0;
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
  min-height: 0;
}

.chat-shell--panel .chat-shell__main {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  width: 100%;
  min-height: 0;
  height: 100%;
  padding: 0;
  gap: 0;
  background: transparent;
  border: 0;
  border-radius: 0;
}

.chat-shell--panel .chat-main__header {
  flex: 0 0 auto;
  padding: 8px 10px 6px;
}

.chat-shell--panel .chat-main__title {
  font-size: 14px;
}

.chat-shell--panel .chat-main__subtitle {
  margin-top: 1px;
  font-size: 10px;
}

.chat-room-tabs {
  padding: 8px 10px 6px;
  flex: 0 0 auto;
}

.chat-room-tabs__list {
  display: flex;
  gap: 10px;
  overflow-x: auto;
  padding-bottom: 2px;
  scrollbar-width: none;
}

.chat-room-tabs__list::-webkit-scrollbar {
  display: none;
}

.chat-room-tabs .chat-room-list__item {
  flex: 0 0 auto;
  min-width: 0;
  width: auto;
  padding: 6px 11px;
  gap: 4px;
  min-height: 30px;
}

.chat-shell--panel .chat-room-tabs__list {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  overflow: visible;
}

.chat-shell--compact:not(.chat-shell--panel) .chat-room-tabs__list {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  overflow: visible;
}

.chat-shell--compact:not(.chat-shell--panel) .chat-room-tabs .chat-room-list__item {
  width: 100%;
  justify-content: center;
}

.chat-shell--panel .chat-room-list__item {
  width: 100%;
  min-width: 0;
  text-align: center;
  min-height: 30px;
}

.chat-shell--panel .chat-room-list__name {
  font-size: 10px;
}

.chat-shell--panel .chat-room-list__badge {
  margin-left: 0;
  min-width: 16px;
  min-height: 16px;
  padding: 0 5px;
  font-size: 9px;
}

.chat-main__subtitle {
  margin: 4px 0 0;
  max-width: 60ch;
}

.chat-main__stream-wrap {
  position: relative;
  min-height: 360px;
  border-radius: 16px;
  background: rgba(7, 12, 16, 0.45);
  overflow: hidden;
}

.chat-shell--compact:not(.chat-shell--panel) .chat-shell__main {
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-height: 450px;
  height: 450px;
  max-height: 450px;
  padding: 12px;
  gap: 10px;
}

.chat-shell--compact:not(.chat-shell--panel) .chat-room-tabs {
  padding: 4px 6px 2px;
}

.chat-shell--compact:not(.chat-shell--panel) .chat-room-tabs .chat-room-list__item {
  min-height: 28px;
  padding: 4px 10px;
}

.chat-shell--compact:not(.chat-shell--panel) .chat-room-list__name {
  font-size: 12px;
}

.chat-shell--compact:not(.chat-shell--panel) .chat-main__stream-wrap {
  min-height: 0;
  max-height: none;
}

.chat-shell--compact:not(.chat-shell--panel) .chat-stream {
  min-height: 0;
  height: 100%;
  max-height: none;
  gap: 7px;
  padding: 12px;
}

.chat-shell--compact:not(.chat-shell--panel) .chat-message__bubble {
  gap: 4px;
  max-width: min(76%, 430px);
  padding: 8px 10px;
  border-radius: 14px;
}

.chat-shell--compact:not(.chat-shell--panel) .chat-message__author {
  font-size: 11px;
}

.chat-shell--compact:not(.chat-shell--panel) .chat-message__time {
  font-size: 10px;
}

.chat-shell--compact:not(.chat-shell--panel) .chat-message__text {
  font-size: 12px;
  line-height: 1.35;
}

.chat-shell--compact:not(.chat-shell--panel) .chat-composer {
  padding: 8px 10px;
  gap: 6px;
}

.chat-shell--compact:not(.chat-shell--panel) .chat-composer__row {
  gap: 8px;
}

.chat-shell--compact:not(.chat-shell--panel) .chat-composer__input {
  min-height: 42px;
  height: 42px;
  max-height: 96px;
  padding: 10px 12px;
  line-height: 20px;
}

.chat-shell--compact:not(.chat-shell--panel) .chat-composer__submit {
  width: 36px;
  min-width: 36px;
  height: 36px;
  flex-basis: 36px;
}

.chat-main__stream-wrap--panel {
  display: flex;
  flex: 1 1 350px;
  flex-direction: column;
  width: 100%;
  min-height: 0;
  max-height: min(346px, calc(50vh - 136px));
  border-radius: 0;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(7, 12, 16, 0.4);
}

.chat-shell--panel .chat-main__stream-wrap--panel {
  min-height: 0;
}

.chat-stream {
  display: flex;
  flex-direction: column;
  gap: 10px;
  height: 100%;
  min-height: 360px;
  max-height: calc(70vh - 160px);
  padding: 18px;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 201, 50, 1) rgba(255, 255, 255, 0.03);
  scrollbar-gutter: stable;
  scroll-padding-bottom: calc(120px + var(--chat-keyboard-offset, 0px) + var(--chat-safe-area-bottom));
}

.chat-shell--panel .chat-stream {
  flex: 1 1 auto;
  height: auto;
  min-height: 0;
  max-height: inherit;
  gap: 7px;
  padding: 10px;
}

.chat-shell--panel .chat-empty-state {
  min-height: 0;
  flex: 1 1 auto;
}

.chat-empty-state {
  display: grid;
  place-items: center;
  min-height: 360px;
  padding: 24px;
  text-align: center;
}

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

.chat-empty-state h3 {
  margin: 0 0 8px;
  color: var(--c-text, #eaeaea);
  font-size: 20px;
}

.chat-message {
  display: flex;
  width: 100%;
}

.chat-message--own {
  justify-content: flex-end;
}

.chat-message__bubble {
  display: grid;
  gap: 6px;
  max-width: min(74%, 540px);
  padding: 10px 12px;
  border-radius: 18px;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.10);
  text-align: left;
}

.chat-message--own .chat-message__bubble {
  background: linear-gradient(135deg, rgba(255, 201, 50, 0.22), rgba(255, 201, 50, 0.10));
  border-color: rgba(255, 201, 50, 0.32);
}

.chat-message--system .chat-message__bubble {
  max-width: 100%;
  background: rgba(255,255,255,0.05);
}

.chat-message__head {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 6px;
  min-width: 0;
  flex-wrap: nowrap;
}

.chat-message__avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  overflow: hidden;
  border-radius: 999px;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.10);
  flex: 0 0 auto;
}

.chat-message__avatar img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.chat-message__author {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--c-text, #eaeaea);
  font-size: 12px;
  font-weight: 700;
}

.chat-message__source {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 18px;
  padding: 1px 7px;
  border-radius: 999px;
  background: rgba(97, 154, 232, 0.18);
  color: #9cc4ff;
  font-size: 9px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  line-height: 1;
}

.chat-message__text {
  color: var(--c-text, #eaeaea);
  font-size: 14px;
  line-height: 1.4;
  text-align: left;
  white-space: pre-wrap;
  word-break: break-word;
}

.chat-message__link {
  color: #9cc4ff;
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
}

.chat-message__link:hover {
  color: #c6dcff;
}

.chat-message__time,
.chat-message__meta {
  margin-left: auto;
  color: rgba(234, 234, 234, 0.68);
  font-size: 11px;
  line-height: 1.2;
  white-space: nowrap;
  flex-shrink: 0;
}

.chat-shell--panel .chat-message__bubble {
  gap: 4px;
  max-width: min(72%, 254px);
  padding: 8px 10px;
  border-radius: 14px;
}

.chat-shell--panel .chat-message__head {
  gap: 6px;
}

.chat-shell--panel .chat-message__author {
  font-size: 10px;
}

.chat-shell--panel .chat-message__avatar {
  width: 16px;
  height: 16px;
}

.chat-shell--panel .chat-message__source {
  min-height: 16px;
  padding: 1px 6px;
  font-size: 8px;
}

.chat-shell--panel .chat-message__time {
  font-size: 9px;
}

.chat-shell--panel .chat-message__text {
  font-size: 11px;
  line-height: 1.32;
}

.chat-new-messages {
  position: absolute;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  font-size: 13px;
  z-index: 3;
}

.chat-new-messages:hover {
  transform: translateX(-50%) translateY(-1px);
}

.chat-composer {
  display: grid;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 14px;
  background: rgba(255,255,255,0.05);
}

.chat-composer--panel {
  position: relative;
  flex: 0 0 auto;
  border-radius: 0;
  margin-top: 0;
  padding: 8px 10px calc(8px + var(--chat-safe-area-bottom));
  background: linear-gradient(180deg, rgba(16, 24, 32, 0.96), rgba(10, 16, 22, 0.98));
}

.chat-composer__row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.chat-composer__label {
  color: rgba(234, 234, 234, 0.76);
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.chat-composer__input {
  flex: 1 1 auto;
  min-width: 0;
  width: 100%;
  min-height: 44px;
  max-height: 120px;
  padding: 12px 14px 10px;
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 20px;
  background: rgba(8, 14, 18, 0.54);
  color: var(--c-text, #eaeaea);
  font: inherit;
  line-height: 20px;
  resize: none;
  box-sizing: border-box;
  overflow-y: auto;
  scrollbar-width: auto;
  scrollbar-color: rgba(255, 201, 50, 1) rgba(255, 255, 255, 0.03);
}

.chat-stream::-webkit-scrollbar,
.chat-composer__input::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

.chat-stream::-webkit-scrollbar-track,
.chat-composer__input::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.025);
  border-radius: 999px;
}

.chat-stream::-webkit-scrollbar-thumb,
.chat-composer__input::-webkit-scrollbar-thumb {
  background: rgba(255, 201, 50, 1);
  border-radius: 999px;
  border: 0;
}

.chat-stream::-webkit-scrollbar-thumb:hover,
.chat-composer__input::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 214, 99, 1);
}

.chat-composer__input::placeholder {
  line-height: 20px;
}

.chat-composer__input:focus-visible {
  outline: 2px solid var(--ring, rgba(255, 201, 50, 0.30));
  outline-offset: 2px;
  border-color: rgba(255, 201, 50, 0.4);
}

.chat-composer__footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.chat-composer__feedback {
  margin: 6px 2px 0;
  min-height: 16px;
  font-size: 12px;
  line-height: 1.3;
}

.chat-composer__feedback.is-error {
  color: #ff9a9a;
}

.chat-composer__feedback.is-success {
  color: #92e0a6;
}

.chat-composer__hint {
  flex: 1 1 220px;
  min-width: 0;
}

.chat-composer__submit {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 38px;
  width: 38px;
  min-width: 38px;
  height: 38px;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: linear-gradient(180deg, #ffd45b, #ffc932);
  color: #18222b;
  cursor: pointer;
  box-shadow: 0 10px 18px rgba(0, 0, 0, 0.22);
  transition: transform 0.18s ease, box-shadow 0.18s ease, filter 0.18s ease;
}

.chat-composer__submit:hover,
.chat-composer__submit:focus-visible {
  transform: translateY(-1px);
  filter: brightness(1.02);
  box-shadow: 0 12px 20px rgba(0, 0, 0, 0.28);
}

.chat-composer__submit:disabled {
  cursor: wait;
  opacity: 0.78;
}

.chat-composer__submit-icon {
  font-size: 14px;
  line-height: 1;
}

.chat-shell--panel .chat-composer__label {
  font-size: 11px;
}

.chat-shell--panel .chat-composer__row {
  align-items: flex-end;
  gap: 6px;
}

.chat-shell--panel .chat-composer__input {
  min-height: 40px;
  height: 40px;
  max-height: 96px;
  padding: 10px 12px 8px;
  font-size: 12px;
  line-height: 20px;
}

.chat-shell--panel .chat-composer__hint {
  font-size: 11px;
}

.chat-shell--panel .chat-composer__feedback {
  margin-top: 4px;
  font-size: 11px;
}

.chat-shell--panel .chat-composer__submit {
  flex: 0 0 34px;
  width: 34px;
  min-width: 34px;
  height: 34px;
}

.chat-shell--panel .chat-composer__submit-icon {
  font-size: 13px;
}

.chat-schema-warning {
  margin: 16px auto 32px;
  padding: 20px;
}

.chat-schema-warning__list {
  margin: 14px 0 0;
  padding-left: 18px;
  color: var(--c-text, #eaeaea);
}

.chat-shell:not(.chat-shell--panel).chat-shell--keyboard-open .chat-stream {
  padding-bottom: calc(120px + var(--chat-keyboard-offset, 0px) + var(--chat-safe-area-bottom));
}

.chat-shell--panel.chat-shell--keyboard-open .chat-stream {
  padding-bottom: calc(18px + var(--chat-safe-area-bottom));
}

.chat-shell--keyboard-open .chat-composer {
  box-shadow: 0 -14px 28px rgba(0, 0, 0, 0.24);
}

@media (max-width: 899px) {
  .chat-shell {
    grid-template-columns: 1fr;
  }

  .chat-runtime__grid {
    grid-template-columns: 1fr 1fr;
  }

  .chat-message__bubble {
    max-width: min(82%, 420px);
  }
}

@media (max-width: 699px) {
  .chat-shell__main {
    min-height: calc(100vh - 220px);
    padding: 14px;
  }

  .chat-shell--panel .chat-shell__main {
    min-height: 0;
    padding: 0;
  }

  .chat-stream {
    max-height: none;
    min-height: calc(100vh - 430px);
    padding-bottom: calc(24px + var(--chat-keyboard-offset, 0px) + var(--chat-safe-area-bottom));
  }

  .chat-shell--panel .chat-stream {
    min-height: 0;
    max-height: inherit;
  }

  .chat-shell--panel .chat-main__stream-wrap--panel {
    max-height: min(346px, calc(50dvh - 136px));
  }

  .chat-room-tabs {
    padding-inline: 14px;
  }

  .chat-shell:not(.chat-shell--panel) .chat-composer {
    position: sticky;
    bottom: 0;
    margin-bottom: calc(var(--chat-keyboard-offset, 0px) + var(--chat-safe-area-bottom));
    z-index: 5;
  }

  .chat-composer__footer {
    flex-wrap: wrap;
    align-items: flex-end;
  }

  .chat-composer__hint {
    flex-basis: 100%;
    font-size: 12px;
  }

  .chat-shell--panel .chat-composer__submit {
    flex: 0 0 34px;
    width: 34px;
    min-width: 34px;
    margin-left: 0;
    min-height: 34px;
    height: 34px;
  }

  .chat-shell--panel .chat-composer__input {
    min-height: 38px;
    height: 38px;
    padding-top: 9px;
    padding-bottom: 7px;
  }

  .chat-composer--panel {
    position: relative;
    bottom: auto;
    margin-bottom: 0;
  }

  .chat-shell--panel .chat-composer__footer {
    display: grid;
    gap: 8px;
    align-items: stretch;
  }

  .chat-shell--panel .chat-composer__hint {
    margin: 0;
    text-align: left;
  }

  .chat-shell--panel.chat-shell--keyboard-open .chat-stream {
    min-height: 0;
    max-height: none;
  }

  .chat-message__bubble {
    max-width: 88%;
  }

  .chat-message__head {
    align-items: center;
  }

  .chat-message__time {
    font-size: 10px;
  }
}

/* ==========================================================================
   Dock / FAB chat integration
   Regole specifiche della chat dock/FAB, separate dal dock generico.
   ========================================================================= */

.game-dock-panel--chat {
  bottom: 66px;
}

.game-dock-panel--chat.is-open {
  height: auto;
  max-height: min(calc(var(--chat-viewport-height, 100vh) - 122px), 510px);
}

.game-dock-panel__body--chat {
  display: flex;
  flex: 1 1 auto;
  min-height: 0;
  padding: 0;
  overflow: hidden;
  background: linear-gradient(180deg, rgba(26, 40, 54, 0.98), rgba(16, 24, 32, 1));
}

.game-dock-panel__body--chat > .chat-shell {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  height: 100%;
}

.game-chat-fab {
  display: none;
}

@media (max-width: 799px) {
  .game-dock-panel--chat.is-open {
    height: auto;
    max-height: min(calc(var(--chat-viewport-height, 100dvh) - 122px), 510px);
  }
}

@media (max-width: 699px) {
  .game-dock-panel--chat .game-dock-panel__footer {
    display: none;
  }
}

@media (min-width: 800px) {
  .game-chat-fab {
    position: fixed;
    right: max(20px, calc((100vw - 1280px) / 2 + 20px));
    bottom: 70px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 56px;
    height: 56px;
    border: 1px solid rgba(255, 201, 50, 0.28);
    border-radius: 18px;
    background: linear-gradient(180deg, rgba(31, 47, 64, 0.96), rgba(19, 31, 43, 0.98));
    box-shadow: 0 14px 28px rgba(0, 0, 0, 0.32);
    color: var(--c-accent);
    cursor: pointer;
    pointer-events: auto;
    z-index: 9005;
    transition: transform 0.18s ease, box-shadow 0.18s ease, border-color 0.18s ease, background 0.18s ease;
  }

  .game-chat-fab:hover,
  .game-chat-fab.is-active {
    transform: translateY(-1px);
    border-color: rgba(255, 201, 50, 0.52);
    box-shadow: 0 18px 32px rgba(0, 0, 0, 0.38);
    background: linear-gradient(180deg, rgba(39, 58, 78, 0.98), rgba(23, 37, 51, 1));
  }

  .game-chat-fab__icon {
    font-size: 24px;
    line-height: 1;
    pointer-events: none;
  }

  .game-chat-fab__badge {
    position: absolute;
    top: 4px;
    right: 4px;
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    background: #e53935;
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    border-radius: 999px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 0 0 2px rgba(20, 29, 39, 0.92);
    pointer-events: none;
  }

  .game-dock-panel--chat {
    top: auto;
    bottom: 138px;
    left: auto;
    right: max(20px, calc((100vw - 1280px) / 2 + 20px));
    width: min(92vw, 390px);
    max-width: 390px;
    max-height: min(76vh, 760px);
    transform: translateY(12px);
    border-radius: 18px;
    opacity: 0;
    transition: max-height 0.3s ease-out, opacity 0.2s ease-out, transform 0.2s ease-out;
  }

  .game-dock-panel--chat.is-open {
    height: auto;
    max-height: min(calc(var(--chat-viewport-height, 100vh) - 164px), 560px);
    opacity: 1;
    transform: translateY(0);
  }
}
