:root {
  --bg: #f4f5f8;
  --surface: #ffffff;
  --surface-elevated: #eceff5;
  --border: #d7dce6;
  --text: #161a24;
  --text-muted: #5a6478;
  --accent: #4458c7;
  --accent-hover: #3749b2;
  --accent-text: #ffffff;
  --bubble-me: #4458c7;
  --bubble-me-text: #ffffff;
  --bubble-me-meta: rgba(255, 255, 255, 0.8);
  --bubble-me-quote: rgba(255, 255, 255, 0.14);
  --bubble-other: #ffffff;
  --bubble-other-text: #161a24;
  --bubble-other-meta: #646f84;
  --bubble-other-quote: #eef1f7;
  --danger-bg: #fdecec;
  --danger-text: #9e2222;
  --danger-border: #f3c1c1;
  --receipt-read: #7dd3fc;
  --presence-online: #15803d;
  --star-color: #f59e0b;
  --highlight-ring: rgba(104, 125, 242, 0.55);
  --shadow: 0 4px 18px rgba(16, 22, 38, 0.08);
}

@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] {
    --bg: #0c0f16;
    --surface: #141925;
    --surface-elevated: #1c2233;
    --border: #273047;
    --text: #e4e8f2;
    --text-muted: #8e99b2;
    --accent: #687df2;
    --accent-hover: #7a8df7;
    --accent-text: #0b0e15;
    --bubble-me: #28355c;
    --bubble-me-text: #edf1fc;
    --bubble-me-meta: #a8b5d9;
    --bubble-me-quote: rgba(10, 14, 24, 0.34);
    --bubble-other: #171d2c;
    --bubble-other-text: #e2e6f0;
    --bubble-other-meta: #8b96b0;
    --bubble-other-quote: #20283d;
    --danger-bg: #36191e;
    --danger-text: #f3a4ae;
    --danger-border: #5e2a33;
    --receipt-read: #53bdeb;
    --presence-online: #4ade80;
    --star-color: #fbbf24;
    --highlight-ring: rgba(122, 141, 247, 0.65);
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
  }
}

:root[data-theme="dark"] {
  --bg: #0c0f16;
  --surface: #141925;
  --surface-elevated: #1c2233;
  --border: #273047;
  --text: #e4e8f2;
  --text-muted: #8e99b2;
  --accent: #687df2;
  --accent-hover: #7a8df7;
  --accent-text: #0b0e15;
  --bubble-me: #28355c;
  --bubble-me-text: #edf1fc;
  --bubble-me-meta: #a8b5d9;
  --bubble-me-quote: rgba(10, 14, 24, 0.34);
  --bubble-other: #171d2c;
  --bubble-other-text: #e2e6f0;
  --bubble-other-meta: #8b96b0;
  --bubble-other-quote: #20283d;
  --danger-bg: #36191e;
  --danger-text: #f3a4ae;
  --danger-border: #5e2a33;
  --receipt-read: #53bdeb;
  --presence-online: #4ade80;
  --star-color: #fbbf24;
  --highlight-ring: rgba(122, 141, 247, 0.65);
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

.app-shell {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
  background: var(--bg);
}

/* Header */
.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 10px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

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

.brand-icon {
  border-radius: 7px;
  display: block;
  flex-shrink: 0;
}

.brand-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.brand-title-row {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.presence-status {
  font-size: 0.76rem;
  color: var(--text-muted);
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.presence-status.is-online {
  color: var(--presence-online);
  font-weight: 600;
}

.brand-title {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 650;
  letter-spacing: -0.01em;
}

.user-badge {
  font-size: 0.78rem;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--surface-elevated);
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.topbar-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.notif-switch {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  border-radius: 999px;
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  font-size: 0.8rem;
}

.notif-label {
  color: var(--text-muted);
  font-weight: 500;
}

.seg-sep {
  color: var(--text-muted);
  font-size: 0.75rem;
}

.seg-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 999px;
  cursor: pointer;
}

.seg-btn.is-active {
  background: var(--accent);
  color: var(--accent-text);
}

.ctrl-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-elevated);
  color: var(--text);
  font: inherit;
  font-size: 0.8rem;
  font-weight: 550;
  padding: 5px 10px;
  border-radius: 999px;
  cursor: pointer;
}

.ctrl-btn:hover {
  border-color: var(--accent);
}

.btn-starred-filter.is-active {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}

.btn-logout {
  color: var(--danger-text);
}

/* Center / Login */
.center-view {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px 16px;
  overflow-y: auto;
}

.loading-text {
  color: var(--text-muted);
  font-size: 0.95rem;
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 24px 20px;
  box-shadow: var(--shadow);
}

.login-heading {
  margin: 0 0 18px;
  font-size: 1.25rem;
  font-weight: 650;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.user-fieldset {
  border: 0;
  padding: 0;
  margin: 0;
}

.field-label {
  display: block;
  font-size: 0.88rem;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--text);
}

.user-options {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.user-pill {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 11px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-elevated);
  font-weight: 700;
  font-size: 1.05rem;
  cursor: pointer;
  user-select: none;
}

.user-pill input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.user-pill:has(input:checked) {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}

.user-pill:has(input:focus-visible) {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.field-group {
  display: flex;
  flex-direction: column;
}

.text-input {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-elevated);
  color: var(--text);
  font: inherit;
  font-size: 1rem;
}

.text-input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
  border-color: var(--accent);
}

.hint-main {
  margin: 8px 0 2px;
  font-size: 0.88rem;
  color: var(--text);
}

.hint-sub {
  margin: 0;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.error-banner {
  margin: 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--danger-bg);
  color: var(--danger-text);
  border: 1px solid var(--danger-border);
  font-size: 0.88rem;
}

.primary-btn {
  appearance: none;
  border: 0;
  border-radius: 12px;
  padding: 12px 16px;
  background: var(--accent);
  color: var(--accent-text);
  font: inherit;
  font-weight: 650;
  cursor: pointer;
}

.primary-btn:hover {
  background: var(--accent-hover);
}

.primary-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

/* Chat View */
.chat-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat-error {
  margin: 8px 12px 0;
  flex-shrink: 0;
}

.messages-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 14px 12px;
  display: flex;
  flex-direction: column;
  overscroll-behavior-y: contain;
}

.empty-state {
  margin: auto;
  color: var(--text-muted);
  font-size: 0.92rem;
  text-align: center;
}

.load-older-btn {
  align-self: center;
  margin-bottom: 10px;
  padding: 6px 14px;
  border-radius: 999px;
  border: 1px solid var(--border-color);
  background: var(--surface-alt);
  color: var(--text-color);
  font-size: 0.84rem;
  cursor: pointer;
}

.load-older-btn:disabled {
  opacity: 0.6;
  cursor: default;
}

.messages-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.msg-row {
  display: flex;
  flex-direction: column;
  max-width: 86%;
}

.msg-row.is-me {
  align-self: flex-end;
  align-items: flex-end;
}

.msg-row.is-other {
  align-self: flex-start;
  align-items: flex-start;
}

.msg-bubble {
  position: relative;
  padding: 8px 12px 7px;
  border-radius: 15px;
  border: 1px solid var(--border);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
  min-width: 84px;
  transition: box-shadow 0.25s ease;
}

.msg-row.is-highlighted .msg-bubble {
  box-shadow: 0 0 0 3px var(--highlight-ring);
}

.msg-row.is-me .msg-bubble {
  background: var(--bubble-me);
  color: var(--bubble-me-text);
  border-color: transparent;
  border-bottom-right-radius: 5px;
}

.msg-row.is-other .msg-bubble {
  background: var(--bubble-other);
  color: var(--bubble-other-text);
  border-bottom-left-radius: 5px;
}

.msg-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 2px;
}

.msg-author {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--bubble-other-meta);
}

.msg-row.is-me .msg-author {
  display: none;
}

.msg-row.is-me .msg-header {
  justify-content: flex-end;
}

.msg-menu-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: inherit;
  opacity: 0.65;
  font-size: 0.95rem;
  line-height: 1;
  padding: 1px 4px;
  border-radius: 6px;
  cursor: pointer;
}

.msg-menu-btn:hover,
.msg-menu-btn:focus-visible {
  opacity: 1;
}

.msg-reply-quote {
  appearance: none;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  width: 100%;
  text-align: left;
  border: 0;
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  padding: 6px 8px;
  margin: 2px 0 6px;
  font: inherit;
  color: inherit;
  cursor: pointer;
}

.msg-row.is-me .msg-reply-quote {
  background: var(--bubble-me-quote);
  border-left-color: rgba(255, 255, 255, 0.7);
}

.msg-row.is-other .msg-reply-quote {
  background: var(--bubble-other-quote);
}

.msg-reply-author {
  font-size: 0.72rem;
  font-weight: 700;
}

.msg-reply-text {
  font-size: 0.8rem;
  opacity: 0.86;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 100%;
}

.msg-img-btn {
  appearance: none;
  display: block;
  border: 0;
  padding: 0;
  margin: 2px 0 6px;
  background: transparent;
  border-radius: 10px;
  overflow: hidden;
  cursor: zoom-in;
}

.msg-image {
  display: block;
  max-width: 100%;
  max-height: 280px;
  border-radius: 10px;
  object-fit: cover;
}

.msg-audio-player {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 210px;
  max-width: 100%;
  margin: 4px 0 6px;
}

.msg-audio-toggle {
  appearance: none;
  border: 0;
  width: 34px;
  height: 34px;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}

.msg-row.is-me .msg-audio-toggle {
  background: rgba(255, 255, 255, 0.22);
  color: var(--bubble-me-text);
}

.msg-row.is-other .msg-audio-toggle {
  background: var(--accent);
  color: var(--accent-text);
}

.msg-audio-progress {
  position: relative;
  flex: 1;
  min-width: 72px;
  height: 6px;
  border-radius: 999px;
  overflow: hidden;
  cursor: pointer;
}

.msg-row.is-me .msg-audio-progress {
  background: rgba(255, 255, 255, 0.28);
}

.msg-row.is-other .msg-audio-progress {
  background: var(--border);
}

.msg-audio-progress-fill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  transition: width 0.1s linear;
}

.msg-row.is-me .msg-audio-progress-fill {
  background: var(--bubble-me-text);
}

.msg-row.is-other .msg-audio-progress-fill {
  background: var(--accent);
}

.msg-audio-duration {
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  flex-shrink: 0;
}

.msg-body {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
  font-size: 0.96rem;
  line-height: 1.42;
}

.msg-body.is-deleted {
  font-style: italic;
  opacity: 0.72;
}

.msg-meta {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 5px;
  margin-top: 4px;
  font-size: 0.72rem;
  flex-wrap: wrap;
}

.msg-row.is-me .msg-meta {
  color: var(--bubble-me-meta);
}

.msg-row.is-other .msg-meta {
  color: var(--bubble-other-meta);
}

.msg-star {
  color: var(--star-color);
  font-size: 0.78rem;
  line-height: 1;
}

.msg-receipt {
  font-weight: 650;
  font-size: 0.78rem;
  letter-spacing: -0.08em;
  color: var(--bubble-me-meta);
}

.msg-receipt.is-read {
  color: var(--receipt-read);
}

.msg-read-time {
  font-size: 0.68rem;
  opacity: 0.88;
}

/* Previews above composer */
.reply-preview,
.image-preview-bar,
.audio-recording-bar,
.audio-preview-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 12px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.audio-recording-status {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.recording-dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  background: #dc2626;
  flex-shrink: 0;
  animation: rec-pulse 1s ease-in-out infinite;
}

@keyframes rec-pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.45; transform: scale(0.85); }
}

.recording-timer {
  font-size: 0.88rem;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: var(--danger-text);
}

.audio-recording-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.ctrl-btn.is-stop {
  background: var(--accent);
  color: var(--accent-text);
  border-color: transparent;
}

.audio-play-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-elevated);
  color: var(--text);
  width: 34px;
  height: 34px;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  cursor: pointer;
  flex-shrink: 0;
}

.reply-preview-body {
  display: flex;
  flex-direction: column;
  min-width: 0;
  border-left: 3px solid var(--accent);
  padding-left: 8px;
}

.reply-preview-author {
  font-size: 0.76rem;
  font-weight: 700;
  color: var(--accent);
}

.reply-preview-text {
  font-size: 0.84rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.preview-cancel-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-elevated);
  color: var(--text);
  border-radius: 999px;
  width: 28px;
  height: 28px;
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}

.image-preview-thumb {
  width: 48px;
  height: 48px;
  border-radius: 8px;
  object-fit: cover;
  border: 1px solid var(--border);
  flex-shrink: 0;
}

.image-preview-meta {
  flex: 1;
  min-width: 0;
}

.image-preview-info {
  font-size: 0.84rem;
  color: var(--text-muted);
}

/* Composer */
.composer-form {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.attach-btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface-elevated);
  color: var(--text);
  border-radius: 12px;
  padding: 9px 11px;
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}

.attach-btn:hover {
  border-color: var(--accent);
}

.attach-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.record-btn {
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.record-btn.is-recording {
  background: var(--danger-bg);
  color: var(--danger-text);
  border-color: var(--danger-border);
}

.composer-input-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--surface-elevated);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 8px 12px 6px;
}

.composer-input-wrap:focus-within {
  border-color: var(--accent);
}

.composer-textarea {
  width: 100%;
  border: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 0.98rem;
  line-height: 1.4;
  resize: none;
  field-sizing: content;
  min-height: 2.2em;
  max-height: 140px;
  padding: 0;
}

.composer-textarea:focus {
  outline: none;
}

.char-counter {
  align-self: flex-end;
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.char-counter.is-near-limit {
  color: var(--danger-text);
  font-weight: 600;
}

.send-btn {
  appearance: none;
  border: 0;
  border-radius: 14px;
  padding: 11px 16px;
  background: var(--accent);
  color: var(--accent-text);
  font: inherit;
  font-weight: 650;
  cursor: pointer;
  flex-shrink: 0;
}

.send-btn:hover {
  background: var(--accent-hover);
}

.send-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

/* Modals / Action Sheet */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(8, 10, 18, 0.72);
}

.action-sheet {
  width: 100%;
  max-width: 320px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  box-shadow: var(--shadow);
}

.action-item {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-weight: 550;
  text-align: left;
  padding: 12px 14px;
  border-radius: 10px;
  cursor: pointer;
}

.action-item:hover {
  background: var(--surface-elevated);
}

.action-item.is-danger {
  color: var(--danger-text);
}

.action-item.is-cancel {
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  margin-top: 4px;
  text-align: center;
}

.modal-card {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 20px;
  box-shadow: var(--shadow);
}

.modal-title {
  margin: 0 0 14px;
  font-size: 1.05rem;
  font-weight: 650;
}

.info-list {
  margin: 0 0 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.info-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-size: 0.9rem;
  border-bottom: 1px solid var(--border);
  padding-bottom: 8px;
}

.info-row dt {
  color: var(--text-muted);
  font-weight: 600;
}

.info-row dd {
  margin: 0;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.modal-close-btn {
  width: 100%;
}

.image-lightbox {
  flex-direction: column;
  gap: 12px;
  background: rgba(6, 8, 14, 0.92);
}

.lightbox-close-btn {
  align-self: flex-end;
}

.lightbox-img {
  max-width: 100%;
  max-height: calc(100dvh - 88px);
  border-radius: 10px;
  object-fit: contain;
}

@media (max-width: 480px) {
  .topbar {
    padding: calc(8px + env(safe-area-inset-top)) 10px 8px;
  }

  .topbar-controls {
    width: 100%;
    justify-content: space-between;
  }

  .msg-row {
    max-width: 90%;
  }
}
