/* 1. Tokens */
:root {
  color-scheme: light;
  --brand-primary: #6256e8;
  --brand-primary-hover: #5145d0;
  --brand-secondary: #347cf6;
  --brand-secondary-hover: #2866d2;
  --brand-teal: #0d9488;
  --gradient-primary: linear-gradient(132deg, #725cf3 0%, #4b83f5 100%);
  --gradient-story: linear-gradient(135deg, #f59e0b 0%, #ec4899 38%, #7c3aed 70%, #4f46e5 100%);
  --gradient-soft: linear-gradient(145deg, rgb(114 92 243 / 10%), rgb(75 131 245 / 6%));
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --text-xs: 11px;
  --text-sm: 13px;
  --text-md: 15px;
  --text-lg: 20px;
  --text-xl: 28px;
  --text-2xl: 34px;
  --surface-hover: var(--hover);
  --touch-target: 44px;
  --control-height: 44px;
  --icon-size: 20px;
  --app-viewport-height: 100vh;
  --bg: #f6f7fc;
  --surface: #ffffff;
  --surface-secondary: #f0f1f8;
  --surface-tertiary: #e9ebf5;
  --surface-elevated: #ffffff;
  --text: #1b1a2b;
  --text-secondary: #6c6b81;
  --text-tertiary: #9291a4;
  --text-inverse: #ffffff;
  --border: #e3e4ee;
  --border-subtle: #ececf3;
  --border-strong: #cbcbd9;
  --hover: #efeff8;
  --accent: var(--brand-primary);
  --accent-contrast: #ffffff;
  --danger: #d83e5d;
  --success: #059669;
  --warning: #b45309;
  --error: #dc2626;
  --info: #2563eb;
  --bubble-incoming-bg: var(--surface-tertiary);
  --bubble-incoming-text: var(--text);
  --bubble-outgoing-bg: var(--brand-primary);
  --bubble-outgoing-text: var(--text-inverse);
  --radius-sm: 9px;
  --radius-md: 14px;
  --radius: var(--radius-md);
  --radius-lg: 18px;
  --radius-xl: 26px;
  --radius-full: 9999px;
  --media-radius: 10px;
  --sidebar-collapsed: 76px;
  --sidebar-expanded: 248px;
  --sidebar-width: 248px;
  --inbox-width: 360px;
  --page-content-max: 1440px;
  --shadow: 0 10px 32px rgb(35 34 76 / 8%);
  --shadow-raised: 0 18px 54px rgb(28 31 66 / 16%);
  --z-sticky: 10;
  --z-popover: 30;
  --z-dialog: 60;
  --z-navigation: 70;
  --status-online: #10b981;
  --status-away: #f59e0b;
  --status-busy: #ef4444;
  --status-offline: #9ca3af;
  --motion-fast: 150ms cubic-bezier(.4, 0, .2, 1);
  --motion-normal: 250ms cubic-bezier(.4, 0, .2, 1);
  --focus-ring: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent);
  --scrollbar-track: #f0f1f8;
  --scrollbar-thumb: #c7cad7;
  --scrollbar-thumb-hover: #a5a9ba;
  font: 15px/1.5 Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text);
  background: var(--bg);
}

@supports (height: 100dvh) {
  :root { --app-viewport-height: 100dvh; }
}

html.dark {
  color-scheme: dark;
  --brand-primary: #6256e8;
  --brand-primary-hover: #5145d0;
  --brand-secondary: #72a7ff;
  --brand-secondary-hover: #91b8ff;
  --brand-teal: #2dd4bf;
  --gradient-primary: linear-gradient(132deg, #6256e8 0%, #3b62d0 100%);
  --gradient-story: linear-gradient(135deg, #fbbf24 0%, #f43f5e 38%, #d946ef 70%, #6366f1 100%);
  --gradient-soft: linear-gradient(145deg, rgb(138 104 246 / 12%), rgb(79 141 246 / 8%));
  --bg: #050505;
  --surface: #0b0b0b;
  --surface-secondary: #121212;
  --surface-tertiary: #1b1b1b;
  --surface-elevated: #181818;
  --text: #f4f4f5;
  --text-secondary: #a3a3a3;
  --text-tertiary: #737373;
  --text-inverse: #ffffff;
  --border: #262626;
  --border-subtle: #1d1d1d;
  --border-strong: #414141;
  --hover: #191919;
  --accent: var(--brand-primary);
  --accent-contrast: #ffffff;
  --danger: #ff607a;
  --success: #34d399;
  --warning: #fbbf24;
  --error: #fb7185;
  --info: #60a5fa;
  --bubble-incoming-bg: #202020;
  --bubble-incoming-text: #f4f5ff;
  --bubble-outgoing-bg: #6256e8;
  --bubble-outgoing-text: #ffffff;
  --scrollbar-track: #080808;
  --scrollbar-thumb: #3f3f46;
  --scrollbar-thumb-hover: #5a5a63;
  --shadow: 0 12px 36px rgb(0 0 0 / 38%);
  --shadow-raised: 0 20px 64px rgb(0 0 0 / 52%);
}

/* 2. Reset and shared primitives */
* { box-sizing: border-box; }

* {
  scrollbar-width: thin;
  scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);
}

::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

::-webkit-scrollbar-track { background: var(--scrollbar-track); }

::-webkit-scrollbar-thumb {
  border-radius: 3px;
  background: var(--scrollbar-thumb);
}

::-webkit-scrollbar-thumb:hover { background: var(--scrollbar-thumb-hover); }

html { background: var(--bg); }

/* Keep the signed-in app inside the viewport; active pages own their scrolling. */
html:has(#chat-page:not(.hidden)),
body:has(#chat-page:not(.hidden)) {
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  min-width: 320px;
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

h1, h2, h3, h4, h5, h6 {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

button,
input,
textarea,
select { font: inherit; }

button {
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

button:disabled {
  cursor: not-allowed;
  opacity: .55;
}

button:focus-visible,
input:focus-visible,
textarea:focus-visible,
select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

button,
input,
textarea,
select { color: var(--text); }

svg { display: block; }

.icon {
  width: var(--icon-size);
  height: var(--icon-size);
  flex: 0 0 auto;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 1.8;
}

.icon-sprite {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
}

.hidden { display: none !important; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.eyebrow {
  margin: 0;
  color: var(--text-secondary);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
}

.state,
.empty {
  color: var(--text-secondary);
  font-size: 14px;
  text-align: center;
}

.inline-state {
  min-height: 0;
  margin: 0;
  padding: 8px 0;
}

.avatar {
  position: relative;
  display: grid;
  width: 44px;
  height: 44px;
  flex: 0 0 auto;
  overflow: hidden;
  place-items: center;
  border-radius: 50%;
  background: var(--surface-secondary);
  color: var(--text);
  font-size: 14px;
  font-weight: 700;
}

.presence-dot {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 13px;
  height: 13px;
  border: 2px solid var(--surface);
  border-radius: 50%;
  background: #2fbf71;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.primary,
.secondary,
.icon-button,
.text-button {
  border: 0;
  border-radius: var(--radius-sm);
  font-weight: 650;
}

.primary {
  display: inline-flex;
  min-height: var(--control-height);
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 9px 14px;
  background: var(--gradient-primary);
  color: var(--accent-contrast);
  box-shadow: 0 6px 18px color-mix(in srgb, var(--accent) 22%, transparent);
  transition: filter var(--motion-fast), box-shadow var(--motion-fast), transform var(--motion-fast);
}

.primary:hover:not(:disabled) {
  filter: brightness(1.06);
  box-shadow: 0 9px 24px color-mix(in srgb, var(--accent) 28%, transparent);
  transform: translateY(-1px);
}

.secondary {
  min-height: var(--control-height);
  padding: 9px 14px;
  border: 1px solid var(--border);
  background: var(--surface);
  transition: background-color var(--motion-fast), border-color var(--motion-fast), color var(--motion-fast);
}

.icon-button {
  display: inline-grid;
  width: var(--control-height);
  height: var(--control-height);
  place-items: center;
  padding: 0;
  background: transparent;
  color: var(--text);
  transition: background-color var(--motion-fast), color var(--motion-fast), transform var(--motion-fast);
}

.icon-button:hover:not(:disabled),
.secondary:hover:not(:disabled),
.text-button:hover { background: var(--hover); }

.secondary:hover:not(:disabled) { border-color: var(--border-strong); }

.text-button {
  width: fit-content;
  padding: 6px 0;
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px;
}

.form-status {
  min-height: 20px;
  margin: 0;
  color: var(--text-secondary);
  font-size: 13px;
}

input,
textarea,
select {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  transition: border-color var(--motion-fast), background-color var(--motion-fast), box-shadow var(--motion-fast);
}

input:focus-visible,
textarea:focus-visible,
select:focus-visible { box-shadow: var(--focus-ring); }

input,
textarea { padding: 10px 12px; }

textarea {
  resize: vertical;
}

.search-input {
  width: 100%;
  min-height: 42px;
  background: var(--surface-secondary);
}

/* Auth */
.auth-page {
  display: grid;
  min-height: 100vh;
  place-items: center;
  padding: 24px;
  background: radial-gradient(ellipse at 50% 28%, rgba(98, 86, 232, 0.16), transparent 55%), radial-gradient(circle at 50% 80%, rgba(52, 124, 246, 0.08), transparent 45%), var(--bg);
  scroll-padding-block: 24px 112px;
}

.auth-card {
  display: grid;
  width: min(100%, 420px);
  gap: 14px;
  padding: 32px 28px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

html.dark .auth-card {
  border-color: rgba(255, 255, 255, 0.1);
  background: linear-gradient(175deg, #101015 0%, #08080b 100%);
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.65), 0 0 0 1px rgba(255, 255, 255, 0.06);
}

.auth-controls {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
}

.auth-controls select {
  min-height: 34px;
  padding: 4px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-secondary);
  color: var(--text-secondary);
  font-size: 12.5px;
  font-weight: 550;
  cursor: pointer;
  transition: all var(--motion-fast);
}

html.dark .auth-controls select {
  border-color: rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.04);
}

.auth-controls select:hover,
.auth-controls select:focus {
  color: var(--text);
  border-color: var(--accent);
  outline: none;
}

.auth-controls .icon-button {
  width: 34px;
  height: 34px;
  min-height: 34px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-secondary);
  color: var(--text-secondary);
  transition: all var(--motion-fast);
}

html.dark .auth-controls .icon-button {
  border-color: rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.04);
}

.auth-controls .icon-button:hover {
  color: var(--text);
  border-color: var(--accent);
}

.signup-methods {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-secondary);
}

html.dark .signup-methods {
  border-color: rgba(255, 255, 255, 0.08);
  background: rgba(255, 255, 255, 0.04);
}

.signup-methods button {
  min-height: 38px;
  padding: 8px 10px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.18s cubic-bezier(0.4, 0, 0.2, 1);
}

.signup-methods button:hover:not(.selected) {
  color: var(--text);
  background: rgba(255, 255, 255, 0.04);
}

.signup-methods button.selected {
  border-color: transparent;
  background: var(--accent);
  color: var(--accent-contrast);
  box-shadow: 0 2px 8px rgba(98, 86, 232, 0.35);
}

#auth-status:empty,
#loading-text:empty { display: none; }

.auth-password-input {
  position: relative;
  display: block;
  width: 100%;
}

.auth-password-input input {
  width: 100%;
  min-height: 44px;
  padding-right: 54px;
}

.auth-card input {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  transition: all var(--motion-fast);
}

html.dark .auth-card input {
  border-color: rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.03);
}

.auth-card input:hover {
  border-color: rgba(255, 255, 255, 0.22);
}

.auth-card input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 25%, transparent);
  outline: none;
}

.auth-card .auth-password-input .password-toggle.icon-button {
  position: absolute;
  top: 50%;
  right: 4px;
  width: 40px;
  min-height: 40px;
  height: 40px;
  margin: 0;
  padding: 0;
  transform: translateY(-50%);
  color: var(--text-secondary);
}

.auth-card .auth-password-input .password-toggle.icon-button:hover {
  color: var(--text);
  background: var(--hover);
}

.optional-field {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
}

.optional-field summary {
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
}

.optional-field[open] label { margin-top: 12px; }
.optional-field summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

.auth-panel {
  display: grid;
  gap: 12px;
}

.password-toggle { justify-self: end; }

.field-feedback {
  min-height: 16px;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 450;
}

#username[aria-invalid="true"] { border-color: var(--danger, #d44); }
#username-availability:not(:empty) { color: var(--text-secondary); }
#username[aria-invalid="true"] + #username-help + #username-availability { color: var(--danger, #d44); }
#username[aria-invalid="false"] + #username-help + #username-availability { color: var(--success, #17834a); }

.auth-card .form-status[role="status"]:not(:empty) {
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  background: var(--surface-secondary);
  line-height: 1.45;
}

.recovery-code-dialog {
  width: min(100% - 24px, 420px);
}

.recovery-code-dialog form {
  display: grid;
  gap: 14px;
}

.recovery-code-dialog pre {
  margin: 0;
  padding: 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  color: var(--text);
  font: 700 16px/1.5 ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.auth-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 4px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 20px;
  font-weight: 750;
  letter-spacing: -0.02em;
  color: var(--text);
}

.mark {
  display: inline-grid;
  width: 32px;
  height: 32px;
  place-items: center;
  overflow: hidden;
  border-radius: 9px;
  background: transparent;
}

.mark img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
}

.auth-card .eyebrow {
  margin: 0;
  font-size: 11px;
  font-weight: 750;
  letter-spacing: 0.1em;
  color: var(--brand-secondary, #72a7ff);
  text-transform: uppercase;
}

.auth-card h1 {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 26px;
  font-weight: 750;
  letter-spacing: -0.03em;
  color: var(--text);
}

.auth-card label {
  display: grid;
  gap: 6px;
  font-size: 13px;
  font-weight: 650;
}

.auth-card .primary,
.auth-card .secondary { width: 100%; }

.auth-card #reset-password-open {
  align-self: center;
  justify-self: end;
  margin-top: -4px;
  margin-bottom: 2px;
  font-size: 12.5px;
  font-weight: 550;
  color: var(--brand-secondary, #72a7ff);
  text-decoration: none;
  cursor: pointer;
  transition: color var(--motion-fast);
}

.auth-card #reset-password-open:hover {
  color: #a0c4ff;
  text-decoration: underline;
  background: transparent;
}

.auth-card #login-button {
  min-height: 44px;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 650;
  background: var(--gradient-primary, linear-gradient(132deg, #6256e8 0%, #3b62d0 100%));
  border: 0;
  color: #ffffff;
  box-shadow: 0 4px 16px rgba(98, 86, 232, 0.35);
  cursor: pointer;
  transition: transform var(--motion-fast), filter var(--motion-fast), box-shadow var(--motion-fast);
}

.auth-card #login-button:hover:not(:disabled) {
  transform: translateY(-1px);
  filter: brightness(1.08);
  box-shadow: 0 6px 20px rgba(98, 86, 232, 0.45);
}

.auth-card #signup-button {
  min-height: 42px;
  border-radius: 12px;
  font-size: 13.5px;
  font-weight: 600;
  border: 1px solid var(--border);
  background: var(--surface-secondary);
  color: var(--text);
  cursor: pointer;
  transition: all var(--motion-fast);
}

html.dark .auth-card #signup-button {
  border-color: rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.03);
}

.auth-card #signup-button:hover:not(:disabled) {
  border-color: rgba(255, 255, 255, 0.22);
  background: rgba(255, 255, 255, 0.07);
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: .01ms !important;
    animation-duration: .01ms !important;
  }
}

@media (max-width: 600px) {
  .auth-page { min-height: 100svh; min-height: 100dvh; padding: max(14px, env(safe-area-inset-top)) 14px max(20px, env(safe-area-inset-bottom)); place-items: center; }
  .auth-card { gap: 14px; padding: clamp(20px, 6vw, 28px); }
  .auth-controls select { min-height: 44px; }
  input, textarea, select { font-size: 16px; }
  .primary, .secondary, .icon-button { min-height: 44px; }
  .icon-button { width: 44px; height: 44px; }
  .signup-methods button { min-height: 48px; padding: 8px 6px; }
  .auth-card .auth-password-input .password-toggle.icon-button { width: 44px; min-height: 44px; height: 44px; }
  .auth-card .auth-password-input input { padding-right: 56px; scroll-margin-block: 20px 112px; }
}
