:root {
  color-scheme: light dark;
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #eef1f8;
  --border: #e2e6f0;
  --text: #14161c;
  --text-muted: #5b6172;
  --accent: #4c6fff;
  --accent-contrast: #ffffff;
  --bubble-user: #4c6fff;
  --bubble-user-text: #ffffff;
  --bubble-ai: #ffffff;
  --bubble-ai-text: #1b1e27;
  --ok: #1faa59;
  --warn: #e0a800;
  --danger: #e23d5b;
  --shadow: 0 1px 2px rgba(20, 22, 28, 0.06), 0 8px 24px rgba(20, 22, 28, 0.05);
  --radius: 18px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1016;
    --surface: #171a22;
    --surface-2: #1e222c;
    --border: #262b37;
    --text: #eef1f8;
    --text-muted: #9aa1b4;
    --accent: #6b86ff;
    --accent-contrast: #0e1016;
    --bubble-user: #4c6fff;
    --bubble-user-text: #ffffff;
    --bubble-ai: #1e222c;
    --bubble-ai-text: #eef1f8;
    --ok: #35c46f;
    --warn: #f0c04a;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; min-height: 100%; }

body {
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", "Noto Naskh Arabic", Tahoma, system-ui, -apple-system, sans-serif;
  font-size: 15px;
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

button, input, textarea { font: inherit; }
button, .secondary-button {
  appearance: none;
  border: 1px solid transparent;
  border-radius: 14px;
  padding: 12px 18px;
  background: var(--accent);
  color: var(--accent-contrast);
  font-weight: 600;
  cursor: pointer;
  text-align: center;
  transition: transform 0.08s ease, opacity 0.15s ease, background 0.15s ease;
}
button:hover, .secondary-button:hover { filter: brightness(1.04); }
button:active, .secondary-button:active { transform: scale(0.97); }
button:focus-visible, .secondary-button:focus-visible,
input:focus-visible, textarea:focus-visible {
  outline: 3px solid rgba(76, 111, 255, 0.22);
  outline-offset: 2px;
}
button.danger {
  background: transparent;
  border-color: var(--danger);
  color: var(--danger);
}

input, textarea {
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface-2);
  color: var(--text);
  padding: 12px 14px;
  outline: none;
}
input:focus, textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(76, 111, 255, 0.18);
}
textarea { resize: none; max-height: 140px; }

.wrap {
  width: min(100%, 1180px);
  min-height: 100dvh;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
}

/* The same compact brand header used by the Android WebView. */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(env(safe-area-inset-top, 0px) + 12px) 16px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand__mark, .nova-orb {
  display: grid;
  place-items: center;
  color: var(--accent-contrast);
  background: linear-gradient(135deg, var(--accent), #7a5cff);
  box-shadow: var(--shadow);
}
.brand__mark { width: 40px; height: 40px; border-radius: 12px; }
.brand__text { display: flex; flex-direction: column; }
.brand__title { font-size: 1.15rem; line-height: 1.25; letter-spacing: 0.3px; }
.brand__subtitle { color: var(--text-muted); font-size: 0.78rem; }

.connection {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 6px 11px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: 0.76rem;
  white-space: nowrap;
}
.status__dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--warn);
}
.connection.online .status__dot { background: var(--ok); }

/* Pairing and unlock use the app's centered security-sheet language. */
.gate {
  width: min(100%, 460px);
  margin: auto;
  padding: 28px 22px;
}
.hero { text-align: center; margin-bottom: 18px; }
.hero h1 { margin: 4px 0; font-size: 1.45rem; }
.hero p { margin: 0; color: var(--text-muted); }
.hero .eyebrow {
  margin-top: 14px;
  color: var(--accent);
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.04em;
}
.nova-orb { width: 66px; height: 66px; margin: 0 auto; border-radius: 20px; }
.nova-orb.small { width: 42px; height: 42px; margin: 0; border-radius: 13px; }
.card {
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.card h2 { margin: 0 0 6px; font-size: 1.15rem; }
.card button { width: 100%; margin-top: 14px; }
.muted { color: var(--text-muted); }
.warn { color: var(--warn); font-weight: 600; }
.mono {
  direction: ltr;
  text-align: left;
  color: var(--text-muted);
  font: 12px/1.5 ui-monospace, SFMono-Regular, Consolas, monospace;
  overflow-wrap: anywhere;
}
.qr-wrap { display: grid; justify-items: center; margin-top: 18px; }
canvas#pair-qr, canvas#matrix-qr {
  max-width: 100%;
  height: auto;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: #fff;
  box-shadow: var(--shadow);
}

/* Desktop adds a quiet rail; every content surface keeps the app proportions. */
.app-shell {
  flex: 1;
  display: grid;
  grid-template-columns: 220px minmax(0, 820px);
  justify-content: center;
  align-items: start;
  gap: 18px;
  padding: 18px;
}
.side-nav {
  position: sticky;
  top: 83px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 11px;
  width: 100%;
  padding: 10px;
  border: 1px solid transparent;
  border-radius: 13px;
  background: transparent;
  color: var(--text-muted);
  text-align: start;
}
.nav-item:hover { background: var(--surface-2); filter: none; }
.nav-item.active {
  border-color: rgba(76, 111, 255, 0.24);
  background: rgba(76, 111, 255, 0.12);
  color: var(--accent);
}
.nav-icon {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  flex: 0 0 36px;
  border-radius: 11px;
  background: var(--surface-2);
}
.nav-item.active .nav-icon { background: var(--accent); color: var(--accent-contrast); }
.nav-icon svg { width: 20px; height: 20px; }
.nav-label { flex: 1; }
.workspace { min-width: 0; width: 100%; }

.panel {
  min-height: calc(100dvh - 120px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--bg);
  box-shadow: var(--shadow);
  animation: rise 0.18s ease-out;
}
@keyframes rise {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}
.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 65px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.panel-head h2 { margin: 0; font-size: 1.08rem; }
.panel-head p { margin: 2px 0 0; color: var(--text-muted); font-size: 0.78rem; }
#ai-panel .panel-head { display: none; }

/* Message list and bubbles mirror Android exactly. */
.messages {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin: 0;
  padding: 18px 16px 10px;
  list-style: none;
  scroll-behavior: smooth;
}
.ai-messages, .chat-messages { min-height: 360px; }
.message {
  max-width: 84%;
  padding: 11px 14px;
  border-radius: var(--radius);
  line-height: 1.6;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  box-shadow: var(--shadow);
  animation: rise 0.18s ease-out;
}
.message.ai, .message.in {
  align-self: flex-start;
  border: 1px solid var(--border);
  border-bottom-right-radius: 6px;
  background: var(--bubble-ai);
  color: var(--bubble-ai-text);
}
.message.user, .message.out {
  align-self: flex-end;
  border-bottom-left-radius: 6px;
  background: var(--bubble-user);
  color: var(--bubble-user-text);
}
.message-body { margin: 0; }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  margin-top: auto;
  padding: 10px 12px calc(env(safe-area-inset-bottom, 0px) + 12px);
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.composer input, .composer textarea { flex: 1; min-height: 46px; }
.send-button {
  display: grid;
  place-items: center;
  width: 46px;
  height: 46px;
  flex: 0 0 46px;
  padding: 0;
  border-radius: 14px;
}
.send-button svg { transform: scaleX(-1); }

.contact-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 0;
  padding: 16px 14px;
  list-style: none;
}
.contact {
  display: grid;
  grid-template-columns: 12px minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
}
.contact button {
  padding: 7px 12px;
  border-color: var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.82rem;
}
.presence-dot {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: var(--danger);
  box-shadow: 0 0 0 3px rgba(226, 61, 91, 0.12);
}
.presence-dot.online {
  background: var(--ok);
  box-shadow: 0 0 0 3px rgba(31, 170, 89, 0.14);
}
.contact-name { font-size: 0.92rem; font-weight: 700; }
.contact-meta { color: var(--text-muted); font-size: 0.78rem; }
.lock-mark {
  padding: 4px 9px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--ok);
  font-size: 0.72rem;
  white-space: nowrap;
}
.typing { margin: 10px 16px 0; color: var(--accent); font-size: 0.8rem; }
#conversation-line { margin: 10px 16px -4px; }

.attachment-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 10px 12px 12px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}
.secondary-button {
  padding: 8px 11px;
  border-color: var(--border);
  border-radius: 11px;
  background: var(--surface-2);
  color: var(--text);
  font-size: 0.8rem;
}
.attachment-card { min-width: min(320px, 70vw); display: grid; gap: 9px; }
.message-body + .attachment-card { margin-top: 9px; }
.attachment-details { display: grid; gap: 2px; }
.attachment-details strong { overflow-wrap: anywhere; }
.attachment-details small { color: inherit; font-size: 0.75rem; opacity: 0.75; }
.attachment-open, .attachment-download {
  display: block;
  width: 100%;
  padding: 8px 11px;
  border: 1px solid color-mix(in srgb, currentColor 28%, transparent);
  border-radius: 10px;
  background: color-mix(in srgb, var(--surface) 28%, transparent);
  color: inherit;
  text-align: center;
  text-decoration: none;
}
.attachment-content:empty { display: none; }
.attachment-media {
  display: block;
  width: 100%;
  max-width: 440px;
  max-height: 360px;
  border-radius: 12px;
  background: #05070b;
}
.attachment-image { object-fit: contain; }

.setting-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin: 16px 14px 24px;
}
#settings-panel > h3, #settings-panel > .warn, #settings-panel > details { margin-inline: 14px; }
.device-list { display: flex; flex-direction: column; gap: 8px; margin: 0 14px 18px; }
.device-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
}
.device-row small { display: block; color: var(--text-muted); }
details { margin-block: 16px; color: var(--text-muted); }

.status-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  width: fit-content;
  max-width: calc(100% - 28px);
  margin: 0 auto 12px;
  padding: 7px 11px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text-muted);
  font-size: 0.74rem;
}
.status-bar > span::before { content: "●"; margin-inline-end: 6px; color: var(--accent); }
.status { margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.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;
}

@media (max-width: 760px) {
  .wrap { width: 100%; }
  .connection { padding: 8px; }
  .connection__label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .gate { padding: 24px 18px 96px; }
  .app-shell { display: block; padding: 0 0 calc(72px + env(safe-area-inset-bottom, 0px)); }
  .side-nav {
    position: fixed;
    inset: auto 0 0;
    z-index: 30;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 2px;
    padding: 7px 6px calc(env(safe-area-inset-bottom, 0px) + 7px);
    border: 0;
    border-top: 1px solid var(--border);
    border-radius: 0;
    box-shadow: 0 -4px 18px rgba(20, 22, 28, 0.06);
  }
  .nav-item { flex-direction: column; justify-content: center; gap: 3px; padding: 4px 2px; border: 0; font-size: 0.68rem; }
  .nav-icon { width: 32px; height: 30px; flex-basis: 30px; background: transparent; }
  .nav-item.active { border-color: transparent; background: transparent; }
  .nav-item.active .nav-icon { border-radius: 999px; }
  .nav-label { flex: none; }
  .panel {
    min-height: calc(100dvh - 137px);
    border: 0;
    border-radius: 0;
    box-shadow: none;
  }
  .panel-head { min-height: 61px; }
  .message { max-width: 88%; }
  .ai-messages, .chat-messages { min-height: 280px; }
  .status-bar { margin-top: 8px; margin-bottom: 84px; }
  .device-row { grid-template-columns: 1fr; }
}

@media (max-width: 420px) {
  .brand__title { font-size: 1rem; }
  .brand__subtitle { font-size: 0.72rem; }
  .panel-head { padding-inline: 14px; }
  .attachment-bar { align-items: stretch; flex-direction: column; }
  .attachment-bar .secondary-button { width: 100%; }
}

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