/* Clarix Health palette: Midnight Navy #0B2545, Deep Teal #0E7C86,
   Clarity Mint #19C3A6, Mist #DDEEEC. Dark mode follows the device setting. */
:root {
  color-scheme: light dark;

  /* Chrome (header, sidebar, bottom nav) is navy in both themes */
  --navy-900: #0b2545;
  --navy-800: #0f2e57;
  --navy-700: #164074;
  --mint-400: #4fd8c0;
  --mint-300: #6fe3cb;

  --page-bg: #ffffff;
  --bg: #f3f8f8;
  --surface: #ffffff;
  --surface-card: #f3f8f8;
  --border: #dce8e8;
  --text: #14243a;
  --text-muted: #4a5b6b;
  --heading: #0b2545;
  --accent: #0e7c86;
  --accent-soft: rgba(14, 124, 134, 0.12);
  --focus-ring: rgba(25, 195, 166, 0.4);
  --hover: #eef5f5;
  --thumb-bg: #eaf4f3;
  --chip-active-bg: #0b2545;
  --chip-active-text: #ffffff;
  --mention-bg: rgba(25, 195, 166, 0.22);
  --danger: #c62828;
  --radius-lg: 16px;
  --radius-md: 10px;
  --shadow-card: 0 12px 32px rgba(11, 37, 69, 0.08);
  --shadow-panel: 0 4px 16px rgba(11, 37, 69, 0.06);
  --backdrop: rgba(11, 37, 69, 0.6);
}

@media (prefers-color-scheme: dark) {
  :root {
    --page-bg: #091524;
    --bg: #0d1c30;
    --surface: #112338;
    --surface-card: #112338;
    --border: #22385a;
    --text: #e6eef5;
    --text-muted: #9db1c4;
    --heading: #f1f6fa;
    --accent: #4fd8c0;
    --accent-soft: rgba(79, 216, 192, 0.16);
    --focus-ring: rgba(79, 216, 192, 0.45);
    --hover: #152a42;
    --thumb-bg: #0f2236;
    --chip-active-bg: #4fd8c0;
    --chip-active-text: #0b2545;
    --mention-bg: rgba(79, 216, 192, 0.25);
    --danger: #ff6b6b;
    --shadow-card: 0 12px 32px rgba(0, 0, 0, 0.4);
    --shadow-panel: 0 4px 16px rgba(0, 0, 0, 0.3);
    --backdrop: rgba(0, 0, 0, 0.65);
  }
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html, body {
  height: 100%;
}

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  margin: 0;
  color: var(--text);
  background: var(--bg);
  padding-left: env(safe-area-inset-left, 0px);
  padding-right: env(safe-area-inset-right, 0px);
}

button {
  font: inherit;
}

/* ---------- Auth screen ---------- */

.auth-screen {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--page-bg);
}

/* Soft mist card with a hairline border so it reads against the white page. */
.auth-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  padding: 36px 32px 28px;
}

.brand {
  text-align: center;
  margin-bottom: 28px;
}

.brand-mark {
  display: block;
  width: 64px;
  height: 64px;
  margin: 0 auto 12px;
}

.brand h1 {
  margin: 0;
  font-size: 1.6rem;
  letter-spacing: -0.02em;
  color: var(--heading);
}

.tagline {
  margin: 4px 0 0;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.auth-form h2 {
  margin: 0;
  font-size: 1.15rem;
  color: var(--heading);
}

.auth-hint {
  margin: -8px 0 0;
  color: var(--text-muted);
  font-size: 0.85rem;
  line-height: 1.4;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
}

.field input,
.field select,
#message-input {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 11px 13px;
  /* 16px minimum stops iOS Safari zooming the page on focus. */
  font-size: 1rem;
  -webkit-appearance: none;
  appearance: none;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
  background: var(--surface);
  color: var(--text);
}

.field input:focus,
.field select:focus,
#message-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--focus-ring);
}

.btn-primary {
  border: none;
  border-radius: var(--radius-md);
  padding: 12px 16px;
  background: linear-gradient(135deg, #0e7c86, #0b5f73);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
  transition: filter 0.15s ease, transform 0.05s ease;
}

.btn-primary:hover {
  filter: brightness(1.08);
}

.btn-primary:active {
  transform: translateY(1px);
}

.btn-ghost {
  border: 1px solid rgba(255, 255, 255, 0.35);
  background: transparent;
  color: #fff;
  border-radius: var(--radius-md);
  padding: 8px 14px;
  cursor: pointer;
  transition: background 0.15s ease;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
}

.btn-ghost:hover {
  background: rgba(255, 255, 255, 0.12);
}

.btn-link {
  align-self: center;
  border: none;
  background: none;
  color: var(--accent);
  cursor: pointer;
  font-size: 0.85rem;
  padding: 6px;
}

.btn-link:hover {
  text-decoration: underline;
}

.error {
  color: var(--danger);
  font-size: 0.85rem;
  min-height: 1.2em;
  margin: 4px 0 0;
  text-align: center;
}

.error.status-success {
  color: var(--accent);
}

.field-row {
  display: flex;
  gap: 12px;
}

.field-row .field {
  flex: 1;
  min-width: 0;
}

/* ---------- Chat screen ---------- */

.chat-screen {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  height: var(--app-height, 100dvh);
}

.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 24px;
  background: linear-gradient(120deg, var(--navy-900), var(--navy-800));
  border-bottom: 2px solid #19c3a6;
  flex: 0 0 auto;
}

.app-brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-mark-sm {
  width: 32px;
  height: 32px;
  margin: 0;
}

.app-title {
  color: #fff;
  font-weight: 700;
  font-size: 1.15rem;
  letter-spacing: -0.02em;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* ---------- Primary nav ---------- */

.main-nav {
  display: flex;
  gap: 4px;
  flex: 0 0 auto;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: var(--radius-md);
  color: rgba(255, 255, 255, 0.7);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 600;
  transition: background 0.15s ease, color 0.15s ease;
}

.nav-link:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
}

.nav-link-active,
.nav-link-active:hover {
  background: rgba(25, 195, 166, 0.18);
  color: var(--mint-300);
}

.nav-icon {
  display: inline-flex;
  line-height: 1;
}

/* Line icons from assets/icons.svg; they take the surrounding text color. */
.icon {
  width: 1.25em;
  height: 1.25em;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.nav-icon .icon {
  width: 20px;
  height: 20px;
}

/* Desktop/tablet: left sidebar spanning full height, header to its right */
@media (min-width: 601px) {
  .chat-screen {
    display: grid;
    grid-template-columns: 216px minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr) auto;
    grid-template-areas:
      "header header"
      "nav main"
      "nav error";
    height: 100vh;
    height: 100dvh;
    height: var(--app-height, 100dvh);
  }

  .chat-screen > .app-header {
    grid-area: header;
  }

  .chat-screen > .main-nav {
    grid-area: nav;
  }

  .chat-screen > main {
    grid-area: main;
    min-height: 0;
  }

  /* Status line sits under main, so the sidebar runs the full height. */
  .chat-screen > .error {
    grid-area: error;
    margin: 0;
  }

  .main-nav {
    flex-direction: column;
    gap: 4px;
    padding: 20px 12px;
    background: var(--navy-800);
    border-right: 1px solid var(--navy-700);
  }

  .nav-link {
    padding: 10px 14px;
  }
}

.members-layout {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  max-width: 760px;
  margin: 0 auto;
  padding: 24px 16px;
  width: 100%;
}

.members-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-panel);
}

.members-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 12px;
}

.members-panel h1 {
  margin: 0;
  font-size: 1.4rem;
  color: var(--heading);
}

.members-summary {
  margin: 4px 0 0;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.btn-create {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  white-space: nowrap;
}

.btn-create .icon {
  width: 18px;
  height: 18px;
}

.member-row {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 4px;
  border-bottom: 1px solid var(--border);
}

.member-row:last-child {
  border-bottom: none;
}

.avatar-wrap {
  position: relative;
  flex: none;
}

.avatar-wrap .avatar {
  width: 42px;
  height: 42px;
  font-size: 0.9rem;
}

.avatar-wrap .presence-dot {
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 13px;
  height: 13px;
  border: 2px solid var(--surface);
}

.presence-dot-offline,
.avatar-wrap .presence-dot-offline {
  background: #94a3b8;
}

.member-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.member-name-line {
  display: flex;
  align-items: center;
  gap: 8px;
}

.member-name {
  font-weight: 600;
  color: var(--heading);
}

.member-you {
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--accent);
  font-size: 0.7rem;
  font-weight: 700;
}

.member-sub {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 10px;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.member-status-online {
  color: var(--accent);
  font-weight: 600;
}

.member-badges {
  display: flex;
  gap: 6px;
}

.member-badge {
  padding: 2px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--text-muted);
  font-size: 0.75rem;
  font-weight: 600;
}

.member-badge-admin {
  border-color: transparent;
  background: var(--accent-soft);
  color: var(--accent);
}

.member-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-left: 8px;
}

.btn-link-danger {
  color: var(--danger);
}

@media (max-width: 600px) {
  .members-layout {
    padding: 12px 10px;
  }

  .members-panel {
    padding: 14px;
  }

  .member-row {
    flex-wrap: wrap;
    row-gap: 4px;
  }

  .member-actions {
    flex-basis: 100%;
    margin: 0 0 0 56px;
  }
}

.chat-layout {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  padding: 16px;
}

.chat-panel {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px;
  box-shadow: var(--shadow-panel);
}

.messages-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  margin-bottom: 12px;
}

#messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 4px 0;
}

/* Flat rows (no bubbles): avatar on the left, content on the right. */
.message {
  position: relative;
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-top: 14px;
  padding: 6px 12px 6px 8px;
  border-radius: var(--radius-md);
  transition: background 0.12s ease;
}

.message:first-child {
  margin-top: 0;
}

.message:hover,
.message:focus-within {
  background: var(--hover);
}

/* Consecutive messages from the same person within a few minutes. */
.message-grouped {
  margin-top: 0;
  padding-top: 2px;
}

.message-grouped .avatar {
  visibility: hidden;
  height: 0;
}

.message-grouped .sender-name,
.message-grouped .timestamp {
  display: none;
}

.message-grouped .message-body {
  margin-top: 0;
}

@media (hover: hover) {
  /* Collapse the (empty) header; its actions float over the row on hover. */
  .message-grouped .message-header {
    height: 0;
  }

  .message-grouped .message-actions {
    position: absolute;
    top: 0;
    right: 12px;
  }
}

.message-content {
  flex: 1 1 auto;
  min-width: 0;
}

.message-body,
.reply-quote,
.file-card,
.reaction-chips {
  max-width: 760px;
}

.avatar {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  color: #fff;
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  user-select: none;
}

.message strong {
  color: var(--heading);
}

.timestamp {
  color: var(--text-muted);
  font-size: 0.75em;
  margin-left: 6px;
}

.message-header {
  display: flex;
  align-items: baseline;
  gap: 4px;
}

.message-actions {
  margin-left: auto;
  display: flex;
  gap: 4px;
}

.message-actions .btn-link {
  padding: 2px 4px;
  font-size: 0.75rem;
}

.reaction-picker-wrapper {
  position: relative;
  display: inline-flex;
}

.reaction-picker-trigger {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 0.85rem;
  line-height: 1;
  padding: 2px 4px;
  color: var(--text-muted);
  border-radius: var(--radius-md);
}

.reaction-picker-trigger:hover {
  color: var(--accent);
}

.reaction-picker-trigger .icon {
  width: 18px;
  height: 18px;
  display: block;
}

.reaction-picker-trigger:hover {
  background: var(--bg);
}

.reaction-picker {
  display: none;
  position: absolute;
  bottom: 100%;
  left: 0;
  margin-bottom: 4px;
  gap: 2px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 4px;
  box-shadow: 0 4px 16px rgba(7, 23, 38, 0.12);
  z-index: 1;
}

.reaction-picker-wrapper:hover .reaction-picker,
.reaction-picker-wrapper:focus-within .reaction-picker {
  display: flex;
}

.reaction-picker-btn {
  border: none;
  background: none;
  cursor: pointer;
  font-size: 0.85rem;
  padding: 2px 3px;
  border-radius: var(--radius-md);
}

.reaction-picker-btn:hover {
  background: var(--bg);
}

.reaction-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
}

.reaction-chip {
  border: 1px solid var(--border);
  background: var(--bg);
  border-radius: 999px;
  padding: 2px 8px;
  font-size: 0.78rem;
  cursor: pointer;
}

.reaction-chip-active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.message-body a {
  color: var(--accent);
}

.message-body code {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 5px;
  font-family: "Consolas", "SFMono-Regular", monospace;
  font-size: 0.9em;
}

.mention {
  background: var(--mention-bg);
  color: var(--heading);
  border-radius: 4px;
  padding: 0 4px;
  font-weight: 600;
}

.reply-quote {
  border-left: 3px solid #19c3a6;
  padding: 4px 8px;
  margin-top: 4px;
  font-size: 0.8rem;
  color: var(--text-muted);
  background: var(--bg);
  border-radius: 0 var(--radius-md) var(--radius-md) 0;
  cursor: pointer;
}

.reply-label {
  font-weight: 600;
  color: var(--accent);
}

.reply-snippet {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#reply-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 6px 10px;
  margin-bottom: 8px;
  font-size: 0.82rem;
  color: var(--text-muted);
}

.edited-tag {
  color: var(--text-muted);
  font-size: 0.75em;
  font-style: italic;
}

.message-body {
  margin-top: 4px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.code-block {
  margin: 8px 0 4px;
  padding: 14px 16px;
  background: #0b2545;
  border: 1px solid var(--border);
  border-radius: 12px;
  color: #e6eef5;
  overflow-x: auto;
  white-space: pre;
  font-family: "Cascadia Mono", "SFMono-Regular", Consolas, monospace;
  font-size: 0.85rem;
  line-height: 1.5;
}

.message-body .code-block code {
  background: none;
  border: none;
  padding: 0;
  color: inherit;
  font: inherit;
}

.edit-textarea {
  width: 100%;
  min-height: 60px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 8px;
  font: inherit;
  background: var(--surface);
  color: var(--text);
  resize: vertical;
}

.edit-actions {
  display: flex;
  gap: 8px;
  margin-top: 6px;
}

.btn-small {
  padding: 6px 10px;
  font-size: 0.8rem;
}

.file-card {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: fit-content;
  max-width: 100%;
  min-width: min(100%, 280px);
  margin-top: 4px;
  padding: 10px 14px 10px 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.file-card:has(.file-preview-pdf) {
  width: 100%;
}

.file-row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.file-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.file-badge {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 10px;
  color: #fff;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.file-meta {
  color: var(--text-muted);
  font-size: 0.78rem;
}

.file-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 12px;
}

.file-actions a,
.file-actions button {
  font-size: 0.8rem;
  color: var(--accent);
  text-decoration: none;
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
}

.file-actions a:hover,
.file-actions button:hover {
  text-decoration: underline;
}

#search-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

#search-input {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 8px 10px;
  font-size: 1rem;
  background: var(--surface);
  color: var(--text);
}

.search-empty {
  color: var(--text-muted);
  text-align: center;
  font-size: 0.85rem;
}

#presence-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 8px;
}

.presence-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.presence-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
  display: inline-block;
}

#typing-indicator {
  min-height: 1.2em;
  margin: 0 0 8px;
  color: var(--text-muted);
  font-size: 0.8rem;
  font-style: italic;
}

.composer {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 0 0 auto;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.composer:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--focus-ring);
}

.composer #message-input,
.composer #message-input:focus {
  border: none;
  box-shadow: none;
  background: transparent;
  padding: 4px 2px;
  min-height: 28px;
  max-height: 160px;
  line-height: 1.45;
  font-family: inherit;
  resize: none;
}

.composer-bar {
  display: flex;
  align-items: center;
  gap: 12px;
}

.composer-hint {
  flex: 1;
  min-width: 0;
  font-size: 0.78rem;
  color: var(--text-muted);
}

.composer .btn-primary {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 18px;
}

.composer .btn-primary .icon {
  width: 18px;
  height: 18px;
}

.btn-primary:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  filter: none;
  transform: none;
}

@media (pointer: coarse) {
  .composer-hint {
    display: none;
  }
}

.file-button {
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 600;
}

.file-button:hover {
  color: var(--accent);
  border-color: var(--accent);
}

.file-button .icon {
  width: 20px;
  height: 20px;
}

@media (max-width: 600px) {
  .file-button {
    min-width: 44px;
    justify-content: center;
    padding: 0 10px;
  }

  .file-button-label {
    display: none;
  }
}

.files-layout {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  max-width: 1200px;
  margin: 0 auto;
  padding: 16px;
  width: 100%;
}

.files-page-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-panel);
}

.files-heading h1 {
  margin: 0;
  font-size: 1.4rem;
  color: var(--heading);
}

.files-subtitle {
  margin: 4px 0 16px;
  font-size: 0.9rem;
  color: var(--text-muted);
}

/* Search, sort and type chips stay in view while the grid scrolls under them. */
.files-controls {
  position: sticky;
  top: -16px;
  z-index: 2;
  margin: 0 -20px;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.files-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
}

.files-search {
  position: relative;
  flex: 1 1 260px;
  display: flex;
  align-items: center;
  color: var(--text-muted);
}

.files-search .icon {
  position: absolute;
  left: 12px;
  width: 18px;
  height: 18px;
  pointer-events: none;
}

.files-search input,
.files-sort select {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 1rem;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.files-search input {
  width: 100%;
  padding: 10px 12px 10px 38px;
}

.files-sort {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
}

.files-sort select {
  padding: 10px 12px;
  cursor: pointer;
}

.files-search input:focus,
.files-sort select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--focus-ring);
}

.view-toggle {
  display: flex;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
}

.view-toggle button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.view-toggle button + button {
  border-left: 1px solid var(--border);
}

.view-toggle button[aria-pressed="true"] {
  background: var(--accent-soft);
  color: var(--accent);
}

.type-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.type-chip {
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  padding: 6px 14px;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.type-chip:hover {
  border-color: var(--accent);
}

.type-chip[aria-pressed="true"] {
  background: var(--chip-active-bg);
  border-color: var(--chip-active-bg);
  color: var(--chip-active-text);
}

.type-chip-count {
  margin-left: 6px;
  font-weight: 500;
  opacity: 0.7;
}

.files-count {
  margin: 14px 0 12px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-muted);
}

.files-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 16px;
}

.file-tile {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  transition: box-shadow 0.15s ease, transform 0.15s ease;
}

.file-tile:not(.tile-skeleton):hover {
  box-shadow: 0 8px 24px rgba(11, 37, 69, 0.14);
  transform: translateY(-2px);
}

.file-thumb {
  position: relative;
  flex: none;
  height: 132px;
  overflow: hidden;
  background: var(--thumb-bg);
  cursor: pointer;
}

.tile-skeleton .file-thumb {
  cursor: default;
}

.file-type-pill {
  position: absolute;
  top: 10px;
  right: 10px;
  padding: 2px 10px;
  border-radius: 8px;
  color: #fff;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.thumb-image {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.thumb-snippet {
  margin: 0;
  height: 100%;
  padding: 14px 12px;
  overflow: hidden;
  color: var(--text-muted);
  font-family: "Cascadia Mono", "SFMono-Regular", Consolas, monospace;
  font-size: 0.7rem;
  line-height: 1.5;
  white-space: pre;
}

/* Stand-in for formats we can't preview (PDF, Word, Excel, PowerPoint). */
.thumb-page {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 84px;
  height: 130px;
  background: #fff;
  border-radius: 8px 8px 0 0;
  box-shadow: 0 2px 10px rgba(11, 37, 69, 0.15);
  font-size: 0.95rem;
  font-weight: 700;
}

.file-body {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
  padding: 12px 14px 14px;
}

.file-title {
  align-self: flex-start;
  max-width: 100%;
  padding: 0;
  border: none;
  background: none;
  color: var(--heading);
  font: inherit;
  font-weight: 700;
  text-align: left;
  word-break: break-word;
  cursor: pointer;
}

.file-title:hover {
  color: var(--accent);
}

.file-by {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.85rem;
}

.file-by .avatar {
  width: 22px;
  height: 22px;
  font-size: 0.6rem;
}

.file-sub {
  font-size: 0.78rem;
  color: var(--text-muted);
}

.files-grid.is-list {
  grid-template-columns: 1fr;
  gap: 10px;
}

.is-list .file-tile {
  flex-direction: row;
  align-items: center;
}

.is-list .file-thumb {
  width: 96px;
  height: 96px;
}

.is-list .file-type-pill {
  top: 6px;
  right: 6px;
  padding: 1px 7px;
  font-size: 0.62rem;
}

.is-list .thumb-snippet {
  padding: 8px;
  font-size: 0.5rem;
}

.is-list .thumb-page {
  top: 10px;
  width: 48px;
  height: 86px;
  font-size: 0.7rem;
}

.is-list .file-body {
  flex: 1;
}

.files-sentinel {
  height: 1px;
}

.files-more {
  margin: 12px 0 0;
  text-align: center;
  font-size: 0.85rem;
  color: var(--text-muted);
}

/* Inline viewer for images and PDFs */
.viewer {
  display: flex;
  flex-direction: column;
  width: min(1000px, 100%);
  height: min(90vh, 820px);
  height: min(90dvh, 820px);
  overflow: hidden;
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  animation: dialog-in 0.18s ease-out;
}

.viewer-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.viewer-title {
  flex: 1;
  min-width: 0;
  margin: 0;
  overflow: hidden;
  color: var(--heading);
  font-size: 1rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.viewer-download {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
}

.viewer-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}

.viewer-close:hover {
  background: var(--hover);
  color: var(--text);
}

.viewer-image,
.viewer-pdf {
  flex: 1;
  min-height: 0;
  width: 100%;
  border: none;
  background: var(--bg);
}

.viewer-image {
  object-fit: contain;
}

@media (max-width: 600px) {
  .files-layout {
    padding: 10px;
  }

  .files-page-panel {
    padding: 14px;
  }

  /* Too tall to pin on a phone: scroll with the page, chips in one swipeable row. */
  .files-controls {
    position: static;
    margin: 0;
    padding: 0 0 4px;
    border-bottom: none;
  }

  .type-chips {
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 6px;
    scrollbar-width: none;
  }

  .type-chips::-webkit-scrollbar {
    display: none;
  }

  .type-chip {
    flex: none;
  }

  .files-grid {
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 12px;
  }

  .files-grid.is-list {
    grid-template-columns: 1fr;
  }

  .file-thumb {
    height: 110px;
  }
}

.btn-link:disabled {
  color: var(--text-muted);
  cursor: not-allowed;
}

.btn-link:disabled:hover {
  text-decoration: none;
}

.file-preview {
  max-width: 100%;
  max-height: 200px;
  display: block;
  border-radius: var(--radius-md);
}

.file-preview-pdf {
  width: 100%;
  height: 300px;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

.file-name {
  font-size: 0.92rem;
  font-weight: 600;
  color: var(--heading);
  word-break: break-word;
}

#chat-error {
  padding: 0 16px 12px;
}

.chat-screen > .error:empty {
  display: none;
}

/* ---------- Responsive ---------- */

@media (max-width: 600px) {
  .main-nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 10;
    justify-content: space-around;
    gap: 0;
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom, 0px));
    background: var(--navy-900);
    border-top: 2px solid #19c3a6;
    border-bottom: none;
  }

  .nav-link {
    flex: 1 1 0;
    flex-direction: column;
    gap: 2px;
    padding: 6px 4px;
    border-radius: var(--radius-md);
    font-size: 0.7rem;
    text-align: center;
  }

  .chat-screen {
    padding-bottom: 64px;
  }
}

@media (max-width: 480px) {
  .auth-card {
    padding: 28px 20px 22px;
  }

  .app-header {
    padding: 12px 16px;
  }

  .chat-layout {
    padding: 10px;
  }

  .chat-panel {
    padding: 12px;
  }

}

/* ---------- Dialogs ---------- */

.dialog-backdrop {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: var(--backdrop);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}

.dialog {
  width: 100%;
  max-width: 420px;
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-card);
  padding: 24px;
  border-top: 4px solid #19c3a6;
}

.dialog-title {
  margin: 0 0 8px;
  font-size: 1.15rem;
  color: var(--heading);
}

.dialog-message,
.dialog-note {
  margin: 0 0 16px;
  font-size: 0.92rem;
  line-height: 1.45;
  color: var(--text-muted);
}

.dialog-note {
  margin: 12px 0 0;
  font-size: 0.85rem;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 20px;
}

.dialog-actions button {
  width: auto;
  min-width: 96px;
  padding: 10px 18px;
  cursor: pointer;
}

.btn-secondary {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
}

.btn-secondary:hover {
  background: var(--bg);
}

.btn-danger {
  border: none;
  border-radius: var(--radius-md);
  background: var(--danger);
  color: #fff;
  font-weight: 600;
}

.btn-danger:hover {
  filter: brightness(1.08);
}

.btn-secondary:focus-visible,
.btn-danger:focus-visible,
.btn-primary:focus-visible {
  outline: 3px solid var(--focus-ring);
  outline-offset: 2px;
}

.dialog-secret {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 12px 14px;
  background: var(--bg);
  border: 1px dashed var(--accent);
  border-radius: var(--radius-md);
}

.dialog-secret-label {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

.dialog-secret-value {
  width: 100%;
  padding: 10px 4px;
  border: none;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--heading);
  font-family: "Cascadia Mono", "SFMono-Regular", Consolas, monospace;
  font-size: 1.5rem;
  letter-spacing: 0.08em;
  text-align: center;
  cursor: pointer;
  /* Plain selectable text: iOS Safari needs the prefixed form. */
  -webkit-user-select: text;
  user-select: text;
}

.dialog-secret-value:focus {
  outline: none;
  background: var(--accent-soft);
}

.dialog-secret-hint {
  color: var(--text-muted);
  font-size: 0.78rem;
  text-align: center;
}

/* Phones and tablets: big full-width buttons, comfortable to hit. */
@media (max-width: 820px) {
  .dialog-actions-stacked {
    flex-direction: column;
    align-items: stretch;
  }

  .dialog-actions-stacked button {
    width: 100%;
    min-height: 48px;
    font-size: 1rem;
  }
}

/* ---------- Password rules checklist ---------- */

.password-rules {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 4px;
  font-size: 0.82rem;
  color: var(--text-muted);
}

.password-rules li::before {
  content: "○";
  display: inline-block;
  width: 1.4em;
}

.password-rules li.rule-met {
  color: var(--accent);
}

.password-rules li.rule-met::before {
  content: "✓";
}

/* ---------- Smoothness: loading, motion, transitions ---------- */

/* The chat shell is only drawn once a session exists (set early by an inline
   script in <head>), so logged-in users see it immediately instead of a flash. */
html:not([data-session]) #chat-view {
  display: none;
}

.new-messages-pill {
  position: absolute;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  border: none;
  border-radius: 999px;
  padding: 7px 16px;
  background: #0e7c86;
  color: #fff;
  font-weight: 600;
  font-size: 0.85rem;
  box-shadow: 0 6px 18px rgba(11, 37, 69, 0.28);
  cursor: pointer;
  animation: pill-in 0.18s ease-out;
}

.message-enter {
  animation: message-in 0.22s ease-out;
}

.message-pending {
  opacity: 0.7;
}

.pending-note {
  margin-left: 8px;
  font-size: 0.72em;
  font-style: italic;
  color: var(--text-muted);
}

.message-failed {
  opacity: 1;
  background: rgba(198, 40, 40, 0.1);
}

.message-failed .pending-note {
  color: var(--danger);
  font-style: normal;
  font-weight: 600;
}

.skeleton {
  width: 70%;
  pointer-events: none;
}

.skeleton-block {
  background: linear-gradient(90deg, var(--border) 25%, var(--bg) 50%, var(--border) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.3s linear infinite;
}

.skeleton-line {
  height: 12px;
  border-radius: 6px;
  margin: 6px 0 8px;
}

.dialog-backdrop {
  animation: fade-in 0.15s ease-out;
}

.dialog {
  animation: dialog-in 0.18s ease-out;
}

.btn-link {
  transition: color 0.15s ease, opacity 0.15s ease;
}

.reaction-chip,
.reaction-picker-btn,
.reaction-picker-trigger,
.file-button {
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

/* Cross-fade between Chats / Files / Members while the header and sidebar stay put. */
@view-transition {
  navigation: auto;
}

.app-header {
  view-transition-name: app-header;
}

.main-nav {
  view-transition-name: app-nav;
}

::view-transition-old(root),
::view-transition-new(root) {
  animation-duration: 0.15s;
}

@keyframes message-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

@keyframes pill-in {
  from { opacity: 0; transform: translate(-50%, 8px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes dialog-in {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to { opacity: 1; transform: none; }
}

@keyframes shimmer {
  from { background-position: 200% 0; }
  to { background-position: -200% 0; }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }

  @view-transition {
    navigation: none;
  }
}

/* Message actions appear on hover/focus; touch devices always show them. */
.message-actions {
  opacity: 0;
  transition: opacity 0.12s ease;
}

.message:hover .message-actions,
.message:focus-within .message-actions {
  opacity: 1;
}

@media (hover: none) {
  .message-actions {
    opacity: 1;
  }
}

/* ---------- Installed app / on-screen keyboard ---------- */

/* Standalone iOS draws under the status bar; keep the header clear of the notch. */
.app-header {
  padding-top: calc(14px + env(safe-area-inset-top, 0px));
}

@media (max-width: 480px) {
  .app-header {
    padding-top: calc(12px + env(safe-area-inset-top, 0px));
  }
}

/* The keyboard covers the bottom nav, so drop it and its reserved space. */
@media (max-width: 600px) {
  .keyboard-open .main-nav {
    display: none;
  }

  .keyboard-open .chat-screen {
    padding-bottom: 0;
  }
}

/* ---------- Form dialog (e.g. Create user) ---------- */

.dialog {
  max-height: calc(100vh - 32px);
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
}

.dialog-form {
  gap: 14px;
  margin-top: 8px;
}

.dialog-form .dialog-actions {
  margin-top: 6px;
}

.required-star {
  color: var(--danger);
}

.field-error {
  margin: 0;
  color: var(--danger);
  font-size: 0.8rem;
}

.field-hint {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.78rem;
}

.field input[aria-invalid="true"],
.field select[aria-invalid="true"] {
  border-color: var(--danger);
}

.field input[aria-invalid="true"]:focus,
.field select[aria-invalid="true"]:focus {
  border-color: var(--danger);
  box-shadow: 0 0 0 3px rgba(198, 40, 40, 0.18);
}

/* An unchosen required select shows its placeholder in the muted color. */
.field select:required:invalid {
  color: var(--text-muted);
}

.field select option {
  color: var(--text);
}

/* ---------- Install prompt (login page) ---------- */

.install-prompt {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 18px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
}

.install-prompt img {
  flex: none;
  border-radius: 10px;
}

.install-text {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
  color: var(--text-muted);
  font-size: 0.8rem;
}

.install-text strong {
  color: var(--heading);
  font-size: 0.9rem;
}

.install-prompt .btn-primary {
  flex: none;
  padding: 8px 16px;
  cursor: pointer;
}

.dialog-steps {
  margin: 0;
  padding-left: 22px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  color: var(--text);
  font-size: 0.92rem;
  line-height: 1.4;
}

.dialog-steps .icon {
  width: 20px;
  height: 20px;
  margin-left: 6px;
  vertical-align: -5px;
  color: var(--accent);
}
