
:root { --kb-inset: 0px; --chat-floor: var(--kb-inset); } 

@media (max-width: 600px) {
  body:has(.shell-bar) { --chat-floor: max(var(--kb-inset), calc(var(--rail-h) + env(safe-area-inset-bottom, 0px))); }
}
.chat { display: flex; flex-direction: column; gap: var(--step-2); min-height: 0; flex: 1 1 auto; }
.chat-head { display: flex; align-items: center; justify-content: space-between; gap: var(--step-2); }
.chat-title { margin: 0; font-size: var(--type-body); font-weight: 600; color: var(--ink-2); }

.ui-sheet .chat-title, .chat-table .chat-title { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }


.chat-scroll { flex: 1 1 auto; min-height: 0; overflow-y: auto; overscroll-behavior: contain; display: flex; flex-direction: column-reverse; }
.chat-lines { list-style: none; margin: 0; padding: var(--step-2) var(--step-1); display: flex; flex-direction: column; gap: var(--step-3); }
.chat-empty { margin: auto; padding: var(--step-5) var(--step-4); text-align: center; color: var(--ink-3); font-size: var(--type-read); }

.chat-day { display: flex; align-items: center; gap: var(--step-3); color: var(--ink-3); font-size: var(--type-note); }
.chat-day::before, .chat-day::after { content: ""; flex: 1 1 auto; height: 1px; background: var(--line-1); }
.chat-sys { margin: 0; text-align: center; color: var(--ink-3); font-size: var(--type-note); }

.chat-group { display: flex; flex-direction: column; gap: 2px; }
.chat-group.is-mine { align-items: flex-end; }
.chat-group.is-theirs { align-items: flex-start; }
.chat-group-head { display: flex; align-items: center; gap: var(--step-1); padding: 0 var(--step-2) 2px; font-size: var(--type-note); color: var(--ink-3); }
.chat-name { color: var(--ink-2); font-weight: 600; }
.chat-tag { color: var(--ink-3); }
.chat-at { color: var(--ink-3); font-variant-numeric: tabular-nums; }
.chat-ring { width: 8px; height: 8px; border-radius: 50%; box-shadow: 0 0 0 2px var(--you); }
.chat-ring-far { box-shadow: 0 0 0 2px var(--opponent); }

.chat-bubbles { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; width: 100%; }
.chat-group.is-mine .chat-bubbles { align-items: flex-end; }
.chat-line { display: flex; align-items: center; gap: var(--step-1); width: 100%; }
.chat-group.is-mine .chat-line { justify-content: flex-end; }
.chat-bubble { max-width: min(80%, 34em); padding: var(--step-2) var(--step-3); border-radius: 18px 18px 18px 18px; font-size: var(--type-read); line-height: 1.4; color: var(--ink-1); }
:root[data-input="touch"] .chat-bubble { font-size: 15px; }
.chat-group.is-theirs .chat-bubble { background: var(--surface-2); border-radius: 6px 18px 18px 6px; cursor: pointer; }
.chat-group.is-theirs .chat-line:first-child .chat-bubble { border-top-left-radius: 18px; }
.chat-group.is-theirs .chat-line:last-child .chat-bubble { border-bottom-left-radius: 4px; }
.chat-group.is-mine .chat-bubble { background: var(--decide-wash); border-radius: 18px 6px 6px 18px; }
.chat-group.is-mine .chat-line:first-child .chat-bubble { border-top-right-radius: 18px; }
.chat-group.is-mine .chat-line:last-child .chat-bubble { border-bottom-right-radius: 4px; }

.chat-table .chat-group.is-mine .chat-bubble, .ui-sheet[data-sheet="chat"] .chat-group.is-mine .chat-bubble { background: var(--you-wash); }
.chat-text { display: block; white-space: pre-wrap; overflow-wrap: anywhere; }

.chat-first, .chat-more { display: block; }
.chat-more { min-height: 1.4em; }
.chat-link { color: var(--focus); text-decoration: underline; text-underline-offset: 2px; }
.chat-group.is-mine .chat-link { color: var(--ink-1); }
.chat-link:focus-visible { outline: var(--ring-w) solid var(--focus); outline-offset: var(--ring-off); border-radius: 2px; }


.chat-more-btn { flex: 0 0 auto; display: grid; place-items: center; width: var(--touch); height: var(--touch); padding: 0; border: 0; border-radius: 50%; background: none; color: var(--ink-3); cursor: pointer; opacity: 0; transition: opacity var(--motion-1) var(--ease-inout); }
.chat-more-btn:focus-visible, .chat-more-btn[aria-expanded="true"] { opacity: 1; }
@media (hover: hover) { .chat-line:hover .chat-more-btn { opacity: 1; } }
@media (hover: hover) { .chat-more-btn:hover { color: var(--ink-1); } }
.chat-more-btn:focus-visible { outline: var(--ring-w) solid var(--focus); outline-offset: var(--ring-in); }
.chat-more-glyph { width: 18px; height: 18px; }
:root[data-input="touch"] .chat-more-btn { display: none; }
.chat-menu-time { margin: 0; padding: var(--step-1) var(--step-3) var(--step-2); font-size: var(--type-note); color: var(--ink-3); border-bottom: 1px solid var(--line-1); }


.chat-composer { flex: 0 0 auto; display: grid; gap: var(--step-1); padding-top: var(--step-2); border-top: 1px solid var(--line-1); }
.chat-compose-row { display: flex; align-items: flex-end; gap: var(--step-2); }
.chat-field { flex: 1 1 auto; min-width: 0; min-height: var(--touch); max-height: calc(4 * 1.4em + 22px); field-sizing: content; resize: none; overflow-y: auto; margin: 0; padding: 10px var(--step-4); border: 1px solid var(--line-2); border-radius: 22px; background: var(--surface-well); color: var(--ink-1); font: inherit; font-size: var(--type-read); line-height: 1.4; }
.chat-field::placeholder { color: var(--ink-3); }
.chat-field:focus-visible { outline: var(--ring-w) solid var(--focus); outline-offset: var(--ring-off); }
:root[data-input="touch"] .chat-field { font-size: 16px; }
.chat-send { flex: 0 0 auto; display: grid; place-items: center; width: var(--touch); height: var(--touch); padding: 0; border: 0; border-radius: 50%; background: var(--surface-2); color: var(--ink-3); cursor: default; transition: background-color var(--motion-1) var(--ease-inout), color var(--motion-1) var(--ease-inout); }
.chat-send.is-live { background: var(--decide); color: var(--surface-ground); cursor: pointer; }
@media (hover: hover) { .chat-send.is-live:hover { background: var(--decide-ink); } }
.chat-send.is-sending { opacity: 0.7; }
.chat-send:focus-visible { outline: var(--ring-w) solid var(--focus); outline-offset: var(--ring-off); }
.chat-send-glyph { width: 20px; height: 20px; }
.chat-compose-foot { display: flex; align-items: baseline; gap: var(--step-2); padding: 0 var(--step-4); }
.chat-said { flex: 1 1 auto; margin: 0; font-size: var(--type-note); line-height: 1.4; color: var(--ink-2); }
.chat-count { font-size: var(--type-note); font-variant-numeric: tabular-nums; color: var(--ink-3); }
.chat-count.is-over { color: var(--bad); }
.chat-closed { padding: var(--step-3) var(--step-2); border-top: 1px solid var(--line-1); }
@media (prefers-reduced-motion: reduce) {
  :root:not([data-motion="full"]) :is(.chat-send, .chat-more-btn) { transition: none; }
}
:root[data-motion="reduce"] :is(.chat-send, .chat-more-btn) { transition: none; }


.chat-label { font-size: var(--type-note); color: var(--ink-2); }
.chat-quote { margin: 0; padding: var(--step-2); border-radius: var(--round-1); background: var(--surface-well); display: grid; gap: var(--step-1); }
.chat-reasons { margin: 0; padding: 0; border: 0; display: grid; gap: var(--step-2); }
.chat-reason { display: flex; align-items: center; gap: var(--step-2); min-height: 44px; }


.room-page.has-chat { display: grid; grid-template-columns: minmax(0, 1fr) minmax(280px, 360px); gap: var(--step-5); align-items: start; }
.room-page.has-chat > .chat-room { grid-column: 2; grid-row: 1 / span 6; position: sticky; top: var(--step-4); display: flex; flex-direction: column; height: min(640px, calc(100vh - var(--shell-h) - var(--footer-h) - var(--step-6))); padding: var(--step-3); border: 1px solid var(--line-1); border-radius: var(--round-2); background: var(--surface-1); }
.room-page.has-chat > :not(.chat-room) { grid-column: 1; }

.results.has-chat { grid-template-columns: minmax(0, 1fr) minmax(280px, 360px); column-gap: var(--step-5); }
.results.has-chat > .chat-room { grid-column: 2; grid-row: 1 / span 6; position: sticky; top: var(--step-4); align-self: start; justify-self: stretch; text-align: left; display: flex; flex-direction: column; height: min(640px, calc(100vh - var(--shell-h) - var(--footer-h) - var(--step-6))); padding: var(--step-3); border: 1px solid var(--line-1); border-radius: var(--round-2); background: var(--surface-1); }
.results.has-chat > :not(.chat-room) { grid-column: 1; }
@media (max-width: 899px) {
  :is(.results, .sb-page).has-chat { grid-template-columns: minmax(0, 1fr); }
  :is(.results, .sb-page).has-chat > .chat-room { grid-column: 1; grid-row: auto; position: static; width: min(560px, 100%); height: 420px; justify-self: center; }
}

.chat-open, .turn-menu { position: relative; }
.chat-open .shell-badge, .turn-menu .shell-badge { position: absolute; top: -4px; right: -6px; }

.ui-sheet:is([data-sheet="room-chat"], [data-sheet="chat"], [data-sheet="dm"], [data-sheet="log"]:has(> .ui-sheet-body > .chat-table:not([hidden]))) { bottom: var(--chat-floor); }
.ui-sheet:is([data-sheet="room-chat"], [data-sheet="chat"], [data-sheet="dm"], [data-sheet="log"]:has(> .ui-sheet-body > .chat-table:not([hidden])))[data-height="full"] { height: calc(100dvh - 24px - var(--chat-floor)); max-height: calc(100dvh - 24px - var(--chat-floor)); }
.ui-sheet:is([data-sheet="room-chat"], [data-sheet="chat"], [data-sheet="dm"], [data-sheet="log"]:has(> .ui-sheet-body > .chat-table:not([hidden]))) > .ui-sheet-body { display: flex; flex-direction: column; flex: 1 1 auto; min-height: 0; gap: 0; padding: 0 var(--step-3) var(--step-3); overflow: hidden; }
.ui-sheet[data-sheet="dm"] > .ui-sheet-body { padding: 0; }

.chat-fold { display: flex; align-items: center; gap: var(--step-2); }
.chat-table { max-height: 40vh; display: flex; flex-direction: column; }
.chat-table .chat { max-height: calc(40vh - var(--touch)); }


.dm-toggle { position: fixed; right: var(--step-4); bottom: var(--step-4); z-index: 40; display: grid; place-items: center; width: 52px; height: 52px; padding: 0; border: 1px solid var(--line-2); border-radius: 50%; background: var(--surface-1); color: var(--ink-1); box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45); cursor: pointer; }
@media (hover: hover) { .dm-toggle:hover { background: var(--surface-2); } }
.dm-toggle .shell-badge { position: absolute; top: -2px; right: -2px; }
.dm-glyph { width: 24px; height: 24px; }
.dm-toggle:focus-visible, .dm-thread-btn:focus-visible, .dm-back:focus-visible { outline: var(--ring-w) solid var(--focus); outline-offset: var(--ring-off); }
.dm-panel { position: fixed; top: var(--shell-h); right: 0; bottom: 0; z-index: var(--z-sheet); width: min(360px, 100vw); display: flex; flex-direction: column; border-left: 1px solid var(--line-2); background: var(--surface-overlay); box-shadow: var(--win-shadow); }
.dm-panel[hidden] { display: none; }
.dm-head { display: flex; align-items: center; justify-content: space-between; gap: var(--step-2); min-height: 52px; padding: var(--step-1) var(--step-2) var(--step-1) var(--step-4); border-bottom: 1px solid var(--line-1); }
.dm-title { margin: 0; font-size: var(--type-head); font-weight: 600; color: var(--ink-1); }


.dm-screens { position: relative; flex: 1 1 auto; min-height: 0; overflow: hidden; }
.dm-screen { position: absolute; inset: 0; display: flex; flex-direction: column; min-height: 0; background: var(--surface-overlay); transition: transform var(--motion-3) var(--ease-standard), opacity var(--motion-3) var(--ease-standard), visibility 0s linear 0s; }
.dm-screen-list { overflow-y: auto; }
.dm-screens[data-dm-screen="thread"] .dm-screen-list { transform: translateX(-30%); opacity: 0; visibility: hidden; transition-delay: 0s, 0s, var(--motion-3); }
.dm-screens[data-dm-screen="list"] .dm-screen-thread { transform: translateX(100%); visibility: hidden; transition-delay: 0s, 0s, var(--motion-3); }
@media (prefers-reduced-motion: reduce) {
  :root:not([data-motion="full"]) .dm-screen { transform: none; transition: opacity var(--motion-2) linear, visibility 0s linear 0s; }
  :root:not([data-motion="full"]) .dm-screens[data-dm-screen="list"] .dm-screen-thread { opacity: 0; transition-delay: 0s, var(--motion-2); }
  :root:not([data-motion="full"]) .dm-screens[data-dm-screen="thread"] .dm-screen-list { transition-delay: 0s, var(--motion-2); }
}
:root[data-motion="reduce"] .dm-screen { transform: none; transition: opacity var(--motion-2) linear, visibility 0s linear 0s; }
:root[data-motion="reduce"] .dm-screens[data-dm-screen="list"] .dm-screen-thread { opacity: 0; transition-delay: 0s, var(--motion-2); }
:root[data-motion="reduce"] .dm-screens[data-dm-screen="thread"] .dm-screen-list { transition-delay: 0s, var(--motion-2); }


.dm-threads { list-style: none; margin: 0; padding: var(--step-1) 0; }
.dm-thread-btn { display: flex; align-items: center; gap: var(--step-3); width: 100%; min-height: 64px; padding: var(--step-2) var(--step-4); border: 0; background: none; color: inherit; font: inherit; text-align: left; cursor: pointer; }
@media (hover: hover) { .dm-thread-btn:hover { background: var(--surface-2); } }
.dm-avatar-wrap { position: relative; flex: 0 0 auto; width: 40px; height: 40px; }
.dm-avatar { display: grid; place-items: center; width: 40px; height: 40px; border-radius: 50%; background: var(--surface-2); box-shadow: inset 0 0 0 1px var(--line-2); color: var(--decide-ink); font-size: var(--type-head); font-weight: 600; line-height: 1; }
.dm-presence { position: absolute; right: -1px; bottom: -1px; width: 12px; height: 12px; border-radius: 50%; border: 2px solid var(--surface-overlay); background: var(--ink-disabled); }
.dm-presence.is-online, .dm-presence.is-in_game { background: var(--good); }
.dm-presence.is-away { background: var(--surface-overlay); box-shadow: inset 0 0 0 2px var(--ink-2); }
.dm-thread-main { display: grid; gap: 2px; flex: 1 1 auto; min-width: 0; }
.dm-thread-top, .dm-thread-bottom { display: flex; align-items: center; justify-content: space-between; gap: var(--step-2); min-width: 0; }
.dm-thread-name { min-width: 0; text-transform: none; letter-spacing: normal; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: var(--type-read); font-weight: 600; color: var(--ink-1); }
.dm-thread-time { flex: 0 0 auto; font-size: var(--type-note); color: var(--ink-3); font-variant-numeric: tabular-nums; }
.dm-thread-preview { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: var(--type-body); color: var(--ink-3); }
.dm-thread.is-unread .dm-thread-preview { color: var(--ink-1); }
.dm-thread.is-unread .dm-thread-time { color: var(--ink-1); }
.dm-empty { margin: var(--step-6) auto; max-width: 26em; }


.dm-thread-body { display: flex; flex-direction: column; flex: 1 1 auto; min-height: 0; }
.dm-thread-head { flex: 0 0 auto; display: flex; align-items: center; gap: var(--step-2); min-height: 56px; padding: var(--step-1) var(--step-3) var(--step-1) var(--step-1); border-bottom: 1px solid var(--line-1); }
.dm-back { display: grid; place-items: center; flex: 0 0 auto; width: var(--touch); height: var(--touch); padding: 0; border: 0; border-radius: 50%; background: none; color: var(--ink-1); cursor: pointer; }
@media (hover: hover) { .dm-back:hover { background: var(--surface-2); } }
.dm-back-glyph { width: 22px; height: 22px; }
.dm-thread-head .dm-avatar-wrap, .dm-thread-head .dm-avatar { width: 32px; height: 32px; font-size: var(--type-read); }
.dm-thread-who { display: grid; min-width: 0; }
.dm-thread-who .dm-thread-name { margin: 0; }
.dm-thread-presence { font-size: var(--type-note); color: var(--ink-3); }
.dm-thread-presence.is-online, .dm-thread-presence.is-in_game { color: var(--good); }
.dm-thread-body > .chat { padding: 0 var(--step-3) var(--step-3); }
.dm-thread-body > .chat-empty { margin: var(--step-5) var(--step-4); }


.friends-pane.is-chat { display: flex; flex-direction: column; overflow: hidden; padding: 0; }
.friends-pane.is-chat > .dm-thread-body { flex: 1 1 auto; min-height: 0; }
.friends-pane.is-chat > .dm-thread-body > .chat { padding: 0 var(--step-4) var(--step-4); }
