/*
 * Hush Web UI polish r54
 *
 * This layer is intentionally kept separate from the feature styles.  It
 * contains the production interaction contract: predictable focus states,
 * touch targets, high-contrast fallbacks, resilient viewport sizing and a
 * restrained motion system.  It is loaded last so the same rules are applied
 * to the workspace, attachment and feature panels as well as the shell.
 */

:root {
  color-scheme: light;
  --focus-ring: #1769e0;
  --focus-ring-soft: rgba(23,105,224,.24);
  --surface-elevated: color-mix(in srgb, var(--bg-panel) 96%, var(--brand) 4%);
  --text-readable: color-mix(in srgb, var(--ink-1) 94%, #000 6%);
  --motion-fast: 140ms;
  --motion-base: 180ms;
}

[data-theme="dark"] {
  color-scheme: dark;
  --focus-ring: #8ab4ff;
  --focus-ring-soft: rgba(138,180,255,.34);
  --surface-elevated: color-mix(in srgb, var(--bg-panel) 92%, #fff 8%);
  --text-readable: #f1f3f5;
}

/* Make every keyboard stop visible, including semantic div buttons created by
 * the renderer.  The offset prevents the ring from being hidden by clipped
 * avatar/list rows. */
:where(button, a, input, textarea, select, summary,
  [role="button"], [role="tab"], [tabindex], [contenteditable="true"]):focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 3px;
  box-shadow: 0 0 0 5px var(--focus-ring-soft);
}

:where(.conv, .nav-row, .res-row, .thread-item, .bookmark-item):focus-visible {
  background: var(--brand-soft);
}

:where(button, a, [role="button"], [tabindex]) {
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* Keep status and body copy readable when a user's display uses a larger
 * system font.  Flex children must be allowed to shrink or they overlap the
 * timestamp and badges. */
:where(.conv-name, .conv-preview, .chat-head .title, .chat-head .sub,
  .msg-body, .bubble, .file-name, .res-name, .res-sub, .thread-copy,
  .upload-title, .upload-detail) {
  min-width: 0;
  overflow-wrap: anywhere;
}

/* Raised surfaces have a small, consistent lift instead of a collection of
 * one-off shadows. */
:where(.modal, .context-menu, .mention-pop, .emoji-panel, .update-bar,
  .upload-tray, .bookmark-list, .thread-list) {
  background: var(--surface-elevated);
}

/* ---------- shell and responsive density ---------- */
.app {
  min-width: 0;
  min-height: 100svh;
  height: 100dvh;
  isolation: isolate;
}

.rail,
.list-panel,
.main,
.chat-head,
.composer {
  contain: layout style;
}

.list-panel {
  width: clamp(264px, 23vw, 340px);
}

.chat-head {
  padding-inline: clamp(12px, 1.8vw, 24px);
}

.chat-head .info {
  min-width: 0;
}

.msgs {
  scroll-behavior: smooth;
  scrollbar-gutter: stable both-edges;
  overscroll-behavior-y: contain;
  padding-inline: clamp(12px, 2.2vw, 32px);
}

.msg-body {
  max-width: min(70%, 640px);
}

.bubble {
  line-height: 1.58;
  text-wrap: pretty;
}

/* Smaller laptop windows keep the list useful without starving the message
 * column.  The rail remains available for one-click navigation. */
@media (min-width: 681px) and (max-width: 980px) {
  :root { --rail-w: 56px; }
  .list-panel { width: clamp(248px, 31vw, 292px); }
  .msg-body { max-width: min(76%, 560px); }
  .chat-head .sub { max-width: 34vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
}

@media (min-width: 1600px) {
  .list-panel { width: 340px; }
  .msgs { padding-inline: clamp(24px, 4vw, 64px); }
  .msg-body { max-width: min(64%, 720px); }
}

@media (orientation: landscape) and (max-width: 900px) and (min-width: 681px) {
  .composer-input textarea { min-height: 36px; max-height: 96px; }
  .msgs { padding-block: 10px; }
  .call-layer { padding-block: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-bottom)); }
}

/* Mobile browser chrome changes the visual viewport.  svh gives the stable
 * minimum, while dvh expands after the keyboard/browser bar settles. */
@media (max-width: 680px) {
  .app { min-height: 100svh; height: 100dvh; }
  .list-panel, .main { bottom: calc(var(--tab-h) + env(safe-area-inset-bottom, 0px)); }
  body.in-chat .main { bottom: 0; }
  .chat-head { padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }
  .msgs { padding-left: max(10px, env(safe-area-inset-left)); padding-right: max(10px, env(safe-area-inset-right)); }
  .msg-body { max-width: min(84%, 520px); }
  .composer {
    padding-bottom: max(4px, env(safe-area-inset-bottom, 0px));
    background: var(--bg-panel);
  }
  .composer-tools { padding-left: max(8px, env(safe-area-inset-left)); padding-right: max(8px, env(safe-area-inset-right)); }
  .composer-input { padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }
  .composer-bar { align-items: flex-end; }
  .composer-hint { max-width: 50vw; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .emoji-panel.open { height: min(42dvh, 290px); }
  .emoji-grid { height: calc(min(42dvh, 290px) - 44px); grid-template-columns: repeat(auto-fill, minmax(42px, 1fr)); }
  .modal-mask { padding-left: max(10px, env(safe-area-inset-left)); padding-right: max(10px, env(safe-area-inset-right)); }
}

/* Coarse pointers need forgiving controls and no hover-only meaning. */
@media (pointer: coarse) {
  .tool-btn, .icon-btn, .rail-btn, .tab, .viewer .close,
  .emoji-tab, .emoji-cell, .emoji-choice, .reaction-choice,
  .ctx-item, .mention-item, .idx-bar button, .search-filter {
    min-width: 44px;
    min-height: 44px;
  }
  .conv, .nav-row, .res-row { min-height: 56px; }
  .tool-btn:hover, .icon-btn:hover, .conv:hover, .nav-row:hover, .res-row:hover,
  .ctx-item:hover, .mention-item:hover { background: inherit; }
  .msgs { -webkit-overflow-scrolling: touch; }
}

/* ---------- loading, empty and failure states ---------- */
.empty-state {
  text-align: center;
  color: var(--ink-3);
}

.empty-state .glyph {
  display: grid;
  place-items: center;
  background: linear-gradient(145deg, var(--brand-soft), var(--bg-hover));
  color: var(--brand-ink);
  box-shadow: inset 0 0 0 1px var(--line);
}

.empty-state .txt {
  color: var(--text-readable);
  font-weight: 550;
  line-height: 1.55;
}

.empty-state .modal-desc {
  max-width: 34rem;
  margin-inline: auto;
}

.is-loading,
[aria-busy="true"] {
  position: relative;
  pointer-events: none;
}

.is-loading::after,
[aria-busy="true"]::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: linear-gradient(100deg, transparent 18%, color-mix(in srgb, var(--bg-panel) 70%, transparent) 48%, transparent 78%);
  background-size: 220% 100%;
  animation: hush-shimmer 1.15s linear infinite;
}

@keyframes hush-shimmer { to { background-position: -220% 0; } }

.conn-bar,
.toast {
  text-wrap: balance;
}

.conn-bar {
  min-height: 32px;
  padding-inline: max(12px, env(safe-area-inset-left)) max(12px, env(safe-area-inset-right));
}

/* ---------- composer and message ergonomics ---------- */
.composer:focus-within {
  border-top-color: color-mix(in srgb, var(--brand) 48%, var(--line-strong));
  box-shadow: 0 -2px 12px rgba(16,24,40,.04);
}

.composer-input textarea {
  scrollbar-width: thin;
  overflow-wrap: anywhere;
}

.composer-tools {
  scrollbar-width: none;
  overflow-x: auto;
  overscroll-behavior-x: contain;
}

.composer-tools::-webkit-scrollbar { display: none; }

.tool-btn[aria-pressed="true"],
.tool-btn.on {
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--brand) 55%, transparent);
}

.send-btn:not(:disabled) {
  min-width: 68px;
}

.send-btn:disabled {
  opacity: .72;
}

/* The emoji panel is a true popover on desktop.  Explicit visibility and
 * pointer rules keep it clickable in embedded desktop shells as well as tabs. */
.emoji-panel { visibility: hidden; pointer-events: none; }
.emoji-panel.open { display: flex; visibility: visible; pointer-events: auto; }
/* A fixed emoji popover must escape the composer's layout containment.
 * Keeping style containment preserves paint isolation without creating a
 * nested fixed-positioning viewport in desktop shells. */
.composer { contain: style; }
.main { contain: style; }

.modal-head { display: flex; align-items: center; gap: 10px; }
.modal-title { min-width: 0; flex: 1; }
.modal-back {
  display: inline-flex; align-items: center; justify-content: center;
  width: 34px; height: 34px; flex: none; border-radius: 8px;
  color: var(--ink-2); background: transparent;
}
.modal-back:hover { background: var(--bg-hover); color: var(--ink-1); }
.modal-back svg { width: 18px; height: 18px; }

.file-card { display: flex; align-items: stretch; gap: 0; }
.file-main {
  display: flex; align-items: center; gap: var(--s3); min-width: 0; flex: 1;
  padding: 2px 0; color: inherit; text-align: left; background: transparent;
}
.file-main:hover { background: var(--bg-hover); border-radius: var(--r-sm); }
.file-dl {
  display: inline-flex; align-items: center; justify-content: center;
  width: 38px; min-width: 38px; min-height: 38px; border-radius: var(--r-full);
  color: var(--ink-3); background: transparent;
}
.file-dl:hover { color: var(--brand-ink); background: var(--brand-soft); }
.file-assistant .conv-name::before {
  content: '↗'; display: inline-grid; place-items: center; width: 17px; height: 17px;
  margin-right: 5px; border-radius: 5px; color: var(--brand-ink);
  background: var(--brand-soft); font-size: 12px; font-weight: 700;
}
.link-preview {
  display: inline; padding: 0; color: var(--brand-ink); text-decoration: underline;
  text-decoration-thickness: 1px; text-underline-offset: 2px;
}
.link-preview:hover { color: var(--brand); }
.file-preview-text {
  margin: 0; max-height: 58vh; overflow: auto; padding: 14px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--bg-sunken);
  white-space: pre-wrap; overflow-wrap: anywhere; font: 13px/1.65 ui-monospace, SFMono-Regular, Consolas, monospace;
}
.file-preview-frame, .web-preview-frame {
  display: block; width: 100%; min-height: min(58vh, 560px);
  border: 1px solid var(--line); border-radius: 10px; background: #fff;
}
.file-preview-audio { width: 100%; }
.file-preview-video { display: block; width: 100%; max-height: 58vh; border-radius: 10px; background: #111; }
.file-preview-generic { display: grid; place-items: center; gap: 10px; padding: 42px 20px; color: var(--ink-2); text-align: center; }
.file-preview-generic svg { width: 48px; height: 48px; color: var(--brand-ink); }
.file-preview-generic strong { color: var(--ink-1); overflow-wrap: anywhere; }
.file-preview-generic span { font-size: 12px; color: var(--ink-3); }
.web-preview-card { display: flex; align-items: center; gap: 12px; padding: 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg-sunken); }
.web-preview-icon { display: grid; place-items: center; width: 42px; height: 42px; border-radius: 10px; background: var(--brand-soft); color: var(--brand-ink); }
.web-preview-icon svg { width: 22px; height: 22px; }
.web-preview-copy { min-width: 0; display: grid; gap: 3px; }
.web-preview-copy strong, .web-preview-copy span { min-width: 0; overflow-wrap: anywhere; }
.web-preview-copy span { color: var(--ink-3); font-size: 12px; }

.msg-foot {
  font-variant-numeric: tabular-nums;
}

.msg.me .bubble {
  box-shadow: 0 1px 1px rgba(16,24,40,.04);
}

/* ---------- dark/high-contrast/forced-colors contracts ---------- */
@media (prefers-contrast: more) {
  :root {
    --line: rgba(16,24,40,.24);
    --line-strong: rgba(16,24,40,.42);
    --ink-2: #2f3640;
    --ink-3: #4f5967;
    --ink-4: #697586;
  }
  [data-theme="dark"] {
    --line: rgba(255,255,255,.28);
    --line-strong: rgba(255,255,255,.5);
    --ink-2: #d8dde5;
    --ink-3: #b3bdc9;
    --ink-4: #9aa5b3;
  }
  .bubble, .conv.active, .nav-row:focus-visible { border: 1px solid var(--line-strong); }
  :where(button, a, input, textarea, select, [role="button"]):focus-visible { outline-width: 4px; }
}

@media (forced-colors: active) {
  *, *::before, *::after { forced-color-adjust: auto; }
  .btn, .tool-btn, .icon-btn, .tab, .rail-btn, .conv, .nav-row,
  .ctx-item, .emoji-cell, .reaction-choice { border: 1px solid ButtonText; }
  .btn-primary, .send-btn, .rail-btn.active, .tab.on { background: Highlight; color: HighlightText; }
  .empty-state .glyph { border: 1px solid ButtonText; background: Canvas; }
  .conn-bar, .toast { border: 1px solid CanvasText; }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  .is-loading::after, [aria-busy="true"]::after { animation: none; }
  .boot-splash .boot-logo, .dot-live, .call-avatar.ringing::after { animation: none; }
  .bubble, .tool-btn, .send-btn, .conv, .nav-row, .tab { transition-duration: 0ms; }
}

.crop-stage { position: relative; max-height: min(62dvh, 560px); overflow: hidden; background: #111; border-radius: 12px; display: grid; place-items: center; touch-action: none; cursor: crosshair; }
.crop-stage img { display: block; max-width: 100%; max-height: min(62dvh, 560px); user-select: none; pointer-events: none; }
.crop-selection { position: absolute; display: block; width: 0; height: 0; border: 2px solid #fff; box-shadow: 0 0 0 9999px rgba(0,0,0,.42); pointer-events: none; }

/* Conversation printouts remain useful for an incident review without the
 * application chrome or transient controls. */
@media print {
  html, body { overflow: visible; background: #fff; color: #111; }
  .rail, .list-panel, .chat-head, .composer, .tabbar, .toast-wrap,
  .conn-bar, .update-bar, .call-layer, .context-menu { display: none !important; }
  .app, .main, .msgs { display: block; height: auto; min-height: 0; }
  .msgs { padding: 0; overflow: visible; }
  .msg { break-inside: avoid; }
}

/* Desktop app shell: keep the conversation column calm while giving the
 * active header and composer a clear visual hierarchy. These rules also make
 * the installed PWA/Electron-style window behave consistently with a browser
 * tab when its viewport is short or its system scaling is high. */
@media (min-width: 681px) {
  .app { background: var(--bg-app); }
  .rail { background: color-mix(in srgb, var(--bg-sunken) 88%, var(--brand) 12%); border-right-color: color-mix(in srgb, var(--line) 82%, var(--brand) 18%); }
  .list-panel { background: color-mix(in srgb, var(--bg-panel) 94%, var(--brand) 6%); border-right-color: var(--line-strong); }
  .chat-head { background: color-mix(in srgb, var(--bg-panel) 96%, var(--brand) 4%); box-shadow: 0 1px 0 color-mix(in srgb, var(--line) 80%, transparent); }
  .chat-head .title { font-weight: 650; letter-spacing: -.01em; }
  .msgs { background: radial-gradient(circle at 50% -20%, color-mix(in srgb, var(--brand-soft) 55%, transparent), transparent 48%), var(--bg-app); }
  .composer { background: color-mix(in srgb, var(--bg-panel) 96%, var(--brand) 4%); border-color: color-mix(in srgb, var(--line-strong) 78%, var(--brand) 22%); box-shadow: 0 -8px 24px rgba(16,24,40,.045), inset 0 1px 0 rgba(255,255,255,.55); }
  .composer-input textarea { font-size: 15px; }
  .update-bar { border: 1px solid color-mix(in srgb, var(--brand) 22%, var(--line-strong)); box-shadow: 0 12px 36px rgba(16,24,40,.13); }
}

@media (min-width: 681px) and (max-height: 720px) {
  .chat-head { min-height: 52px; }
  .msgs { padding-block: 12px; }
  .composer { max-height: 176px; }
}
