:root {
  --bg: #f3f6fb;
  --surface: #ffffff;
  --surface-muted: #f7f9fc;
  --border: #e3eaf2;
  --text: #1f2a37;
  --text-muted: #5b6b7c;
  --primary: #2f6fed;
  --primary-hover: #245bd1;
  --primary-soft: #eef4ff;
  --danger: #b42318;
  --success: #1f9d63;
  --warning: #c47b00;
  --shadow: 0 10px 30px rgba(31, 61, 110, 0.08);
  --shadow-soft: 0 4px 16px rgba(31, 61, 110, 0.06);
  --radius: 16px;
  --radius-sm: 12px;
  --nav-width: 68px;
  --sidebar-width: 280px;
  --composer-max: 780px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
}

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

html,
body {
  margin: 0;
  height: 100%;
  color: var(--text);
  background: var(--bg);
}

body {
  overflow: hidden;
}

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

.hidden,
[hidden] {
  display: none !important;
}

/* ========== App shell ========== */
.app-shell {
  display: flex;
  height: 100dvh;
  width: 100%;
  max-width: 100vw;
  overflow: hidden;
  background:
    radial-gradient(circle at top right, #e8f0ff 0, transparent 42%),
    linear-gradient(180deg, #f8fbff 0%, var(--bg) 100%);
}

/* ========== Primary nav ========== */
.primary-nav {
  width: var(--nav-width);
  flex: 0 0 var(--nav-width);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
  padding: 14px 0 calc(14px + var(--safe-bottom));
  background: var(--surface);
  border-right: 1px solid var(--border);
  z-index: 40;
}

.nav-top {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  width: 100%;
}

.nav-brand {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 4px 0 8px;
}

.nav-brand-mark {
  width: 36px;
  height: 36px;
  border-radius: 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(145deg, #4a86ff, #2f6fed);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.02em;
  box-shadow: 0 6px 14px rgba(47, 111, 237, 0.28);
}

.nav-brand-text {
  display: none;
  font-size: 10px;
  color: var(--text-muted);
  font-weight: 600;
}

.nav-actions {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  width: 100%;
  padding: 0 8px;
}

.nav-btn {
  width: 48px;
  height: 48px;
  border: none;
  border-radius: 14px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 0;
  transition: background 0.15s ease, color 0.15s ease;
}

.nav-btn:hover {
  background: var(--surface-muted);
  color: var(--text);
}

.nav-btn:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.nav-btn.is-active {
  background: var(--primary-soft);
  color: var(--primary);
}

.nav-label {
  font-size: 10px;
  line-height: 1.1;
  font-weight: 600;
}

.nav-bottom {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.nav-user-btn {
  border: none;
  background: transparent;
  padding: 0;
  cursor: pointer;
  border-radius: 50%;
}

.nav-user-btn:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 3px;
}

.user-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(145deg, #6ea0ff, #2f6fed);
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  border: 2px solid #fff;
  box-shadow: 0 2px 8px rgba(47, 111, 237, 0.25);
}

.user-avatar-lg {
  width: 40px;
  height: 40px;
  font-size: 16px;
}

/* ========== Conversation sidebar ========== */
.conversation-sidebar {
  width: var(--sidebar-width);
  flex: 0 0 var(--sidebar-width);
  display: flex;
  flex-direction: column;
  background: rgba(255, 255, 255, 0.82);
  border-right: 1px solid var(--border);
  backdrop-filter: blur(8px);
  min-width: 0;
  z-index: 30;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 16px 8px;
}

.sidebar-title {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
}

.sidebar-close-btn {
  display: none;
  border: none;
  background: var(--surface-muted);
  color: var(--text-muted);
  width: 32px;
  height: 32px;
  border-radius: 10px;
  cursor: pointer;
  align-items: center;
  justify-content: center;
}

.sidebar-close-btn:hover {
  color: var(--text);
}

.sidebar-search {
  padding: 8px 12px 12px;
}

.search-input {
  width: 100%;
  height: 38px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  padding: 0 12px;
  font-size: 13px;
  color: var(--text);
  outline: none;
}

.search-input:focus {
  border-color: #b9cef8;
  box-shadow: 0 0 0 3px rgba(47, 111, 237, 0.12);
}

.search-input:disabled {
  background: var(--surface-muted);
  color: var(--text-muted);
  cursor: not-allowed;
}

.conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 10px 16px;
  min-height: 0;
}

.conversation-empty {
  margin: 28px 8px;
  padding: 20px 16px;
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  text-align: center;
  background: rgba(255, 255, 255, 0.6);
}

.empty-title {
  margin: 0 0 6px;
  font-size: 14px;
  font-weight: 600;
}

.empty-desc {
  margin: 0;
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.5;
}

.conversation-groups {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* ========== Main workspace ========== */
.main-workspace {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  position: relative;
  background: transparent;
}

.workspace-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 18px;
  border-bottom: 1px solid transparent;
  min-height: 58px;
}

.header-menu-btn {
  display: none;
  border: none;
  background: var(--surface);
  border: 1px solid var(--border);
  width: 38px;
  height: 38px;
  border-radius: 12px;
  cursor: pointer;
  color: var(--text);
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.workspace-title-wrap {
  flex: 1;
  min-width: 0;
}

.workspace-kicker {
  margin: 0 0 2px;
  font-size: 12px;
  color: var(--text-muted);
}

.workspace-title {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.workspace-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.header-link {
  font-size: 13px;
  color: var(--primary);
  text-decoration: none;
  font-weight: 600;
  padding: 8px 10px;
  border-radius: 10px;
}

.header-link:hover {
  background: var(--primary-soft);
}

.alert-banner {
  margin: 0 18px 8px;
  padding: 12px 14px;
  border-radius: 12px;
  background: #fff5f4;
  border: 1px solid #f3c4bf;
  color: var(--danger);
  font-size: 13px;
}

/* Guest */
.guest-view {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 16px calc(24px + var(--safe-bottom));
}

.guest-card {
  width: 100%;
  max-width: 440px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 32px 28px;
  text-align: center;
}

.guest-mark {
  width: 48px;
  height: 48px;
  margin: 0 auto 16px;
  border-radius: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(145deg, #4a86ff, #2f6fed);
  color: #fff;
  font-weight: 700;
  box-shadow: 0 8px 18px rgba(47, 111, 237, 0.28);
}

.guest-card h2 {
  margin: 0 0 10px;
  font-size: 18px;
  line-height: 1.45;
}

.guest-desc {
  margin: 0 0 20px;
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1.7;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 42px;
  padding: 0 18px;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 600;
  border: none;
  cursor: pointer;
  text-decoration: none;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

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

/* Auth workspace */
.auth-workspace {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.workspace-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.welcome-view {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 20px 12px;
}

.welcome-inner {
  text-align: center;
  max-width: 640px;
}

.welcome-hello {
  margin: 0 0 8px;
  font-size: 15px;
  color: var(--text-muted);
  font-weight: 500;
}

.welcome-title {
  margin: 0 0 10px;
  font-size: clamp(24px, 3.2vw, 32px);
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.3;
}

.welcome-sub {
  margin: 0;
  font-size: 14px;
  color: var(--text-muted);
}

.chat-view {
  flex: 1;
  padding: 12px 16px;
}

.message-list {
  max-width: var(--composer-max);
  margin: 0 auto;
}

/* Composer */
.composer-dock {
  flex-shrink: 0;
  padding: 8px 16px calc(14px + var(--safe-bottom));
  display: flex;
  justify-content: center;
}

.composer {
  width: 100%;
  max-width: var(--composer-max);
}

.composer-shell {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  box-shadow: var(--shadow-soft);
  padding: 12px 12px 10px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.composer-shell:focus-within {
  border-color: #b9cef8;
  box-shadow: 0 0 0 3px rgba(47, 111, 237, 0.1), var(--shadow-soft);
}

.composer-input {
  width: 100%;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 15px;
  line-height: 1.55;
  max-height: 160px;
  min-height: 28px;
  padding: 4px 6px 8px;
  overflow-y: auto;
}

.composer-input:disabled {
  color: var(--text-muted);
  cursor: not-allowed;
}

.composer-input::placeholder {
  color: #93a0b0;
}

.composer-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding-top: 2px;
}

.composer-toolbar-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 1;
}

.composer-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 34px;
  padding: 0 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-muted);
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  flex-shrink: 0;
}

.composer-chip:hover:not(:disabled) {
  color: var(--text);
  background: #eef2f7;
}

.composer-chip:disabled {
  opacity: 0.65;
  cursor: not-allowed;
}

.chip-label {
  display: inline;
}

.model-select-wrap {
  min-width: 0;
}

.model-select {
  max-width: 180px;
  height: 34px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface-muted);
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  padding: 0 28px 0 12px;
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, #8a97a8 50%),
    linear-gradient(135deg, #8a97a8 50%, transparent 50%);
  background-position: calc(100% - 14px) 14px, calc(100% - 9px) 14px;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  cursor: not-allowed;
}

.send-btn {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s ease, opacity 0.15s ease;
}

.send-btn:hover:not(:disabled) {
  background: var(--primary-hover);
}

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

.send-btn:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

.composer-hint {
  margin: 8px 4px 0;
  text-align: center;
  font-size: 11px;
  color: #8a97a8;
  line-height: 1.4;
}

/* User menu */
.user-menu {
  position: fixed;
  left: 76px;
  bottom: 18px;
  width: 260px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  z-index: 60;
  overflow: hidden;
}

.user-menu-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 14px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-muted);
}

.user-menu-meta {
  min-width: 0;
}

.user-menu-name {
  margin: 0 0 2px;
  font-size: 14px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-menu-role {
  margin: 0;
  font-size: 12px;
  color: var(--text-muted);
  text-transform: none;
}

.user-menu-body {
  padding: 6px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.user-menu-item {
  display: block;
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 10px 12px;
  border-radius: 10px;
  cursor: pointer;
  text-decoration: none;
}

.user-menu-item:hover {
  background: var(--surface-muted);
}

.user-menu-danger {
  color: var(--danger);
}

/* Settings modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(20, 32, 51, 0.38);
}

.modal-panel {
  position: relative;
  z-index: 1;
  background: var(--surface);
  border-radius: 18px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  width: min(760px, 100%);
  max-height: min(640px, calc(100dvh - 40px));
  overflow: hidden;
}

.settings-layout {
  display: flex;
  min-height: 420px;
  max-height: min(640px, calc(100dvh - 40px));
}

.settings-nav {
  width: 180px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  background: var(--surface-muted);
  padding: 18px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.settings-heading {
  margin: 0 8px 12px;
  font-size: 16px;
}

.settings-tab {
  text-align: left;
  border: none;
  background: transparent;
  color: var(--text-muted);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 10px 12px;
  border-radius: 10px;
  cursor: pointer;
}

.settings-tab:hover {
  background: rgba(255, 255, 255, 0.7);
  color: var(--text);
}

.settings-tab.is-active {
  background: var(--surface);
  color: var(--primary);
  box-shadow: 0 1px 3px rgba(31, 61, 110, 0.06);
}

.settings-content {
  flex: 1;
  min-width: 0;
  padding: 22px 24px;
  position: relative;
  overflow-y: auto;
}

.modal-close {
  position: absolute;
  top: 14px;
  right: 14px;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 10px;
  background: var(--surface-muted);
  color: var(--text-muted);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.modal-close:hover {
  color: var(--text);
}

.settings-pane h3 {
  margin: 0 0 16px;
  font-size: 18px;
  padding-right: 36px;
}

.account-meta {
  margin: 0;
  display: grid;
  gap: 10px;
}

.account-meta > div {
  display: grid;
  grid-template-columns: 88px 1fr;
  gap: 8px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-muted);
}

.account-meta dt {
  margin: 0;
  color: var(--text-muted);
  font-size: 13px;
}

.account-meta dd {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  word-break: break-all;
}

.placeholder-text {
  margin: 0;
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1.7;
}

/* Mobile overlay */
.mobile-overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 32, 51, 0.35);
  z-index: 25;
}

/* Toast */
.toast-host {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 100;
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: min(420px, calc(100vw - 24px));
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  padding: 12px 14px;
  border-radius: 12px;
  font-size: 13px;
  font-weight: 600;
  line-height: 1.45;
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  animation: toast-in 0.18s ease;
}

.toast-info {
  border-color: #c9daf8;
  background: #f4f8ff;
  color: #1d4fad;
}

.toast-success {
  border-color: #cdeedc;
  background: #f0faf5;
  color: #146c43;
}

.toast-warning {
  border-color: #f0dfb5;
  background: #fff8eb;
  color: #8a5a00;
}

.toast-error {
  border-color: #f3c4bf;
  background: #fff5f4;
  color: var(--danger);
}

@keyframes toast-in {
  from {
    opacity: 0;
    transform: translateY(-6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ========== Responsive ========== */
@media (max-width: 1023px) {
  .conversation-sidebar {
    position: fixed;
    top: 0;
    left: var(--nav-width);
    bottom: 0;
    height: 100dvh;
    transform: translateX(-110%);
    transition: transform 0.2s ease;
    box-shadow: var(--shadow);
    background: var(--surface);
  }

  .conversation-sidebar.is-open {
    transform: translateX(0);
  }

  .sidebar-close-btn {
    display: inline-flex;
  }

  .header-menu-btn {
    display: inline-flex;
  }

  .user-menu {
    left: 12px;
    right: 12px;
    width: auto;
    bottom: calc(12px + var(--safe-bottom));
  }
}

@media (max-width: 767px) {
  .app-shell {
    flex-direction: column;
  }

  .primary-nav {
    width: 100%;
    flex: 0 0 auto;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    padding: 8px 10px;
    border-right: none;
    border-bottom: 1px solid var(--border);
    height: 56px;
  }

  .nav-top {
    flex-direction: row;
    gap: 8px;
    width: auto;
  }

  .nav-brand {
    flex-direction: row;
    padding: 0 4px 0 0;
    gap: 8px;
  }

  .nav-brand-mark {
    width: 32px;
    height: 32px;
    border-radius: 10px;
    font-size: 11px;
  }

  .nav-actions {
    flex-direction: row;
    width: auto;
    padding: 0;
    gap: 2px;
  }

  .nav-btn {
    width: 44px;
    height: 44px;
    border-radius: 12px;
  }

  .nav-label {
    display: none;
  }

  .nav-bottom {
    flex-direction: row;
  }

  .conversation-sidebar {
    left: 0;
    top: 56px;
    width: min(320px, 88vw);
    height: calc(100dvh - 56px);
  }

  .workspace-header {
    padding: 10px 12px;
  }

  .workspace-kicker {
    display: none;
  }

  .composer-dock {
    padding: 6px 10px calc(10px + var(--safe-bottom));
  }

  .chip-label {
    display: none;
  }

  .model-select {
    max-width: 140px;
  }

  .composer-hint {
    font-size: 10px;
  }

  .settings-layout {
    flex-direction: column;
    min-height: 0;
    max-height: calc(100dvh - 24px);
  }

  .modal {
    padding: 12px;
    align-items: stretch;
  }

  .modal-panel {
    width: 100%;
    max-height: calc(100dvh - 24px);
    border-radius: 16px;
  }

  .settings-nav {
    width: 100%;
    flex-direction: row;
    flex-wrap: wrap;
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 12px;
    gap: 6px;
  }

  .settings-heading {
    width: 100%;
    margin: 0 4px 4px;
  }

  .settings-tab {
    padding: 8px 10px;
    font-size: 12px;
  }

  .settings-content {
    padding: 16px;
  }

  .account-meta > div {
    grid-template-columns: 1fr;
  }

  .user-menu {
    top: 64px;
    bottom: auto;
    left: 12px;
    right: 12px;
  }

  .welcome-title {
    font-size: 24px;
  }

  .guest-card {
    padding: 24px 18px;
  }
}

@media (min-width: 768px) and (max-width: 1023px) {
  .composer-hint {
    font-size: 11px;
  }
}

@media (min-width: 1440px) {
  .welcome-title {
    font-size: 34px;
  }
}
