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

:root {
  --ink-strong: hsl(225 32% 9%);
  --ink-muted: hsl(225 12% 44%);
  --line-soft: hsl(225 18% 91%);
  --sidebar-width: 272px;
  --content-max: 1240px;
  --focus-shadow: 0 0 0 3px hsl(var(--ring) / 0.2);
}

html { scroll-behavior: smooth; }

body {
  margin: 0;
  font-family: 'Heebo', sans-serif;
  background: hsl(var(--background));
  color: hsl(var(--foreground));
  -webkit-font-smoothing: antialiased;
}

a { color: hsl(var(--primary)); }
h1, h2, h3 { line-height: 1.2; }
button, input, select, textarea { font: inherit; }

.skip-link {
  position: fixed;
  top: 0.75rem;
  left: 0.75rem;
  z-index: 100;
  padding: 0.6rem 0.85rem;
  border-radius: 0.6rem;
  background: hsl(var(--foreground));
  color: hsl(var(--primary-foreground));
  transform: translateY(-160%);
  transition: transform 160ms ease;
}
.skip-link:focus { transform: translateY(0); }

.error { color: hsl(var(--destructive)); font-weight: 600; }
.inline { display: inline; }

/* Layout */
.appshell {
  display: flex;
  min-height: 100vh;
  padding-inline: env(safe-area-inset-left) env(safe-area-inset-right);
}

.sidebar-backdrop { display: none; }

.sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  padding: 1.4rem 1rem 1.1rem;
  background: hsl(var(--sidebar-background) / 0.95);
  border-inline-end: 1px solid hsl(var(--sidebar-border));
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  min-height: 36px;
  margin: 0 0.5rem 1.7rem;
  color: hsl(var(--foreground));
  font-size: 1.08rem;
  font-weight: 800;
  letter-spacing: -0.01em;
  text-decoration: none;
}

.brand-mark {
  display: inline-grid;
  place-items: center;
  width: 2rem;
  height: 2rem;
  border-radius: 0.65rem;
  background: linear-gradient(145deg, hsl(230 75% 59%), hsl(230 68% 36%));
  color: hsl(var(--primary-foreground));
  font-size: 0.72rem;
  font-weight: 900;
  box-shadow: 0 0.4rem 0.9rem hsl(var(--primary) / 0.2);
}

.sidebar-nav { display: flex; flex-direction: column; gap: 0.25rem; }
.nav-section {
  margin: 0.8rem 0.6rem 0.35rem;
  color: hsl(var(--muted-foreground) / 0.8);
  font-size: 0.68rem;
  font-weight: 800;
  letter-spacing: 0.09em;
  text-transform: uppercase;
}
.nav-section:first-child { margin-top: 0; }
.nav-section-spaced { margin-top: 1.7rem; }

.nav-link {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  min-height: 42px;
  padding: 0 0.75rem;
  border-radius: 0.65rem;
  color: hsl(var(--sidebar-foreground));
  text-decoration: none;
  font-size: 0.92rem;
  font-weight: 500;
  transition: background 150ms ease, color 150ms ease, transform 150ms ease;
}
.nav-link:hover {
  background: hsl(var(--sidebar-accent));
  color: hsl(var(--sidebar-accent-foreground));
}
.nav-link[aria-current="page"] {
  background: hsl(var(--sidebar-accent));
  color: hsl(var(--sidebar-accent-foreground));
  font-weight: 800;
}
.nav-link-disabled { opacity: 0.58; cursor: not-allowed; }
.nav-link-disabled:hover { background: transparent; color: hsl(var(--sidebar-foreground)); }
.nav-icon { width: 1.15rem; color: currentColor; text-align: center; font-size: 1rem; }
.nav-soon {
  margin-inline-start: auto;
  padding: 0.1rem 0.4rem;
  border-radius: 999px;
  background: hsl(var(--muted));
  color: hsl(var(--muted-foreground));
  font-size: 0.64rem;
  font-weight: 700;
}
.nav-form { margin: 0; }
.nav-link-button { width: 100%; text-align: start; background: none; border: 0; cursor: pointer; }
.nav-link-button:focus-visible, .nav-link:focus-visible { outline: none; box-shadow: var(--focus-shadow); }

.sidebar-footer {
  margin-top: auto;
  padding-top: 1rem;
  border-top: 1px solid hsl(var(--sidebar-border));
}
.sidebar-status {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  padding: 0.65rem 0.7rem;
  border-radius: 0.75rem;
  background: hsl(var(--muted) / 0.7);
  color: hsl(var(--foreground));
  font-size: 0.8rem;
}
.sidebar-status strong, .sidebar-status small { display: block; }
.sidebar-status strong { font-size: 0.82rem; }
.sidebar-status small { margin-top: 0.1rem; color: hsl(var(--muted-foreground)); font-size: 0.7rem; }
.status-dot { display: inline-block; width: 0.5rem; height: 0.5rem; flex: 0 0 auto; border-radius: 50%; background: hsl(var(--success)); box-shadow: 0 0 0 0.22rem hsl(var(--success) / 0.14); }

.appshell-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-height: 72px;
  padding: 0 2.25rem;
  padding-top: env(safe-area-inset-top);
  border-bottom: 1px solid hsl(var(--border));
  background: hsl(var(--card) / 0.92);
}
.topbar-title { margin: 0; color: hsl(var(--foreground)); font-size: 1rem; font-weight: 800; }
.topbar-context { padding-inline-start: 0.75rem; border-inline-start: 1px solid hsl(var(--border)); color: hsl(var(--muted-foreground)); font-size: 0.8rem; }
.topbar-meta { display: inline-flex; align-items: center; gap: 0.55rem; margin-inline-start: auto; color: hsl(var(--muted-foreground)); font-size: 0.78rem; }
.topbar-toggle {
  display: none;
  min-width: 44px;
  min-height: 44px;
  border: none;
  background: none;
  color: hsl(var(--foreground));
  font-size: 1.25rem;
  cursor: pointer;
  border-radius: var(--radius);
}
.topbar-toggle:focus-visible { outline: none; box-shadow: var(--focus-shadow); }
.appshell-content { flex: 1; width: 100%; max-width: var(--content-max); margin: 0 auto; padding: 2.1rem 2.6rem 3.5rem; }

/* Page composition */
.page-heading { display: flex; align-items: flex-end; justify-content: space-between; gap: 1.5rem; margin-bottom: 1.7rem; }
.page-heading h1 { margin: 0; color: hsl(var(--foreground)); font-size: clamp(1.65rem, 2.2vw, 2rem); letter-spacing: -0.035em; }
.page-heading p { margin: 0.5rem 0 0; color: hsl(var(--muted-foreground)); }
.eyebrow { margin: 0 0 0.35rem !important; color: hsl(var(--muted-foreground)); font-size: 0.74rem; font-weight: 800; letter-spacing: 0.09em; text-transform: uppercase; }
.group-page-heading { display: flex; align-items: flex-end; justify-content: space-between; gap: 1.5rem; margin-bottom: 1.35rem; }
.group-page-heading h1 { margin: 0; color: hsl(var(--foreground)); font-size: clamp(1.65rem, 2.2vw, 2rem); letter-spacing: -0.035em; }
.group-page-heading p:not(.eyebrow) { margin: 0.5rem 0 0; color: hsl(var(--muted-foreground)); }
.group-title-row { display: flex; align-items: center; gap: 0.7rem; }
.group-tabs { display: flex; gap: 0.25rem; overflow-x: auto; margin-bottom: 1.25rem; padding: 0.3rem; border: 1px solid hsl(var(--border)); border-radius: 0.8rem; background: hsl(var(--muted) / 0.7); scrollbar-width: thin; }
.group-tabs [role="tab"] { min-height: 42px; flex: 0 0 auto; padding: 0 0.9rem; border: 0; border-radius: 0.55rem; background: transparent; color: hsl(var(--muted-foreground)); font-size: 0.86rem; font-weight: 700; cursor: pointer; }
.group-tabs [role="tab"]:hover { color: hsl(var(--foreground)); }
.group-tabs [role="tab"][aria-selected="true"] { background: hsl(var(--card)); color: hsl(var(--primary)); box-shadow: 0 0.2rem 0.6rem hsl(230 30% 18% / 0.08); }
.group-tabs [role="tab"]:focus-visible { outline: none; box-shadow: var(--focus-shadow); }
.tab-panel[hidden] { display: none; }
.tab-panel > .section-heading { margin-bottom: 1.25rem; }
.message-compose { max-width: 56rem; margin-bottom: 1.5rem; }
.primary-md-editor { min-height: 25rem !important; padding: 1rem !important; resize: vertical; font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace !important; line-height: 1.55; }
.form-actions, .settings-form, .checkbox-list { display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap; }
.settings-block { padding: 1.15rem 0; border-top: 1px solid hsl(var(--border)); }
.settings-block:first-of-type { padding-top: 0; border-top: 0; }
.settings-block h3 { margin: 0 0 0.45rem; color: hsl(var(--foreground)); font-size: 0.98rem; }
.settings-help { margin: 0 0 0.85rem; color: hsl(var(--muted-foreground)); font-size: 0.85rem; }
.settings-block > form + form { margin-top: 0.75rem; }
.settings-table { margin-top: 1rem; }
.section-heading { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 1rem; }
.section-heading h2 { margin: 0; color: hsl(var(--foreground)); font-size: 1.08rem; }
.section-heading p { margin: 0.25rem 0 0; color: hsl(var(--muted-foreground)); font-size: 0.85rem; }
.text-link { font-size: 0.8rem; font-weight: 800; text-decoration: none; }
.text-link:hover { text-decoration: underline; }
.stats-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1rem; margin-bottom: 1.4rem; }
.stat-card { padding: 1.15rem 1.25rem; background: hsl(var(--card)); border: 1px solid hsl(var(--border)); border-radius: 0.9rem; box-shadow: var(--shadow-card); }
.stat-label { color: hsl(var(--muted-foreground)); font-size: 0.78rem; font-weight: 700; }
.stat-value { margin-top: 0.4rem; color: hsl(var(--foreground)); font-size: 1.8rem; font-weight: 800; letter-spacing: -0.04em; }
.stat-note { margin-top: 0.15rem; color: hsl(var(--muted-foreground)); font-size: 0.75rem; }
.content-grid { display: grid; grid-template-columns: minmax(0, 1.45fr) minmax(280px, 0.75fr); gap: 1.25rem; align-items: start; }

/* Cards */
.card, .appshell-content > section {
  padding: 1.25rem;
  margin-bottom: 1.25rem;
  background: hsl(var(--card));
  color: hsl(var(--card-foreground));
  border: 1px solid hsl(var(--border));
  border-radius: 0.9rem;
  box-shadow: var(--shadow-card);
}
.card h1, .card h2, .appshell-content > section h2 { color: hsl(var(--foreground)); }
.auth-card { width: 100%; max-width: 440px; margin: 3rem auto; padding: 2rem; }
.auth-card h1 { margin: 0; font-size: 1.8rem; letter-spacing: -0.03em; }
.auth-card .auth-subtitle { margin: 0.55rem 0 1.5rem; color: hsl(var(--muted-foreground)); }
.empty-state { display: flex; align-items: center; gap: 1rem; min-height: 150px; padding: 1.25rem; border: 1px dashed hsl(var(--border)); border-radius: 0.75rem; background: hsl(var(--muted) / 0.35); }
.empty-icon { display: grid; place-items: center; width: 3rem; height: 3rem; flex: 0 0 auto; border-radius: 0.8rem; background: hsl(var(--accent)); color: hsl(var(--accent-foreground)); font-size: 1.2rem; }
.empty-state h3 { margin: 0 0 0.3rem; font-size: 0.98rem; }
.empty-state p { max-width: 34rem; margin: 0; color: hsl(var(--muted-foreground)); font-size: 0.84rem; line-height: 1.5; }
.activity-row { display: flex; gap: 0.75rem; padding: 0.85rem 0; border-bottom: 1px solid hsl(var(--border)); }
.activity-row:last-child { border-bottom: 0; }
.activity-dot { width: 0.55rem; height: 0.55rem; flex: 0 0 auto; margin-top: 0.35rem; border-radius: 50%; background: hsl(var(--primary) / 0.65); box-shadow: 0 0 0 0.25rem hsl(var(--accent)); }
.activity-text { color: hsl(var(--muted-foreground)); font-size: 0.82rem; line-height: 1.45; }
.activity-text strong { color: hsl(var(--foreground)); }

/* Forms */
.field { display: block; margin-bottom: 0.9rem; color: hsl(var(--foreground)); font-size: 0.86rem; font-weight: 600; }
.field input, .field textarea, .field select, input:not([type="checkbox"]):not([type="radio"]), select, textarea {
  display: block;
  width: 100%;
  min-height: 44px;
  margin-top: 0.35rem;
  padding: 0 0.8rem;
  border: 1px solid hsl(var(--input));
  border-radius: 0.65rem;
  background: hsl(var(--card));
  color: hsl(var(--foreground));
  font-size: 1rem;
}
.field textarea, textarea { min-height: 7rem; padding-block: 0.65rem; resize: vertical; }
.field input:focus, .field textarea:focus, .field select:focus, input:focus, select:focus, textarea:focus { outline: none; border-color: hsl(var(--ring)); box-shadow: var(--focus-shadow); }
.stack-form { display: block; }
.inline-form { display: flex; gap: 0.75rem; align-items: flex-end; flex-wrap: wrap; }
.inline-form .field { min-width: min(100%, 18rem); margin-bottom: 0; }
.checkbox-field { display: inline-flex; align-items: center; gap: 0.45rem; min-height: 44px; color: hsl(var(--muted-foreground)); font-size: 0.84rem; }
.checkbox-field input { width: 1rem; height: 1rem; }
input[type="checkbox"], input[type="radio"] { accent-color: hsl(var(--primary)); }

/* Buttons */
.btn { display: inline-flex; align-items: center; justify-content: center; min-height: 44px; padding: 0 1rem; border: 1px solid transparent; border-radius: 0.65rem; font-size: 0.92rem; font-weight: 700; cursor: pointer; text-decoration: none; transition: filter 150ms ease, transform 150ms ease, box-shadow 150ms ease; }
.btn:hover { filter: brightness(0.97); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: none; box-shadow: var(--focus-shadow); }
.btn-sm { min-height: 38px; padding: 0 0.75rem; font-size: 0.82rem; }
.btn-primary { background: hsl(var(--primary)); color: hsl(var(--primary-foreground)); box-shadow: 0 0.45rem 1rem hsl(var(--primary) / 0.18); }
.btn-secondary { background: hsl(var(--secondary)); color: hsl(var(--secondary-foreground)); }
.btn-destructive { background: hsl(var(--destructive)); color: hsl(var(--destructive-foreground)); }
.btn-quiet { border-color: hsl(var(--border)); background: hsl(var(--card)); color: hsl(var(--foreground)); }

/* Badges and tables */
.badge { display: inline-flex; align-items: center; gap: 0.25rem; padding: 0.18rem 0.55rem; border-radius: 999px; font-size: 0.78rem; font-weight: 700; }
.badge-success { background: hsl(var(--success) / 0.15); color: hsl(var(--success)); }
.badge-muted { background: hsl(var(--muted)); color: hsl(var(--muted-foreground)); }
.table-wrap { overflow-x: auto; border: 1px solid hsl(var(--border)); border-radius: 0.7rem; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 0.75rem 0.8rem; text-align: start; border-bottom: 1px solid hsl(var(--border)); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: hsl(var(--muted) / 0.35); }
.table-wrap th, .table-wrap td { white-space: nowrap; }
th { color: hsl(var(--muted-foreground)); font-size: 0.72rem; font-weight: 800; letter-spacing: 0.05em; text-transform: uppercase; }
td { color: hsl(var(--foreground)); font-size: 0.88rem; }
td code { display: inline-block; max-width: 28rem; overflow: hidden; text-overflow: ellipsis; vertical-align: bottom; white-space: nowrap; }
.session-badge { display: flex; align-items: center; gap: 0.375rem; margin-block: 0.125rem; }
.session-dot { display: inline-block; width: 0.5rem; height: 0.5rem; border-radius: 50%; background: hsl(var(--muted-foreground)); flex-shrink: 0; }
.session-dot[data-connected="true"] { background: hsl(var(--success)); }
.session-last-seen, .session-none { color: hsl(var(--muted-foreground)); font-size: 0.75rem; }
.pagination { display: flex; gap: 0.75rem; margin-top: 1rem; }
.list-row { display: flex; align-items: center; justify-content: space-between; gap: 0.75rem; min-height: 3rem; border-bottom: 1px solid hsl(var(--border)); }
.list-row:last-child { border-bottom: 0; }
.empty-compact { padding: 1rem; border-radius: 0.65rem; background: hsl(var(--muted) / 0.45); color: hsl(var(--muted-foreground)); font-size: 0.84rem; }

/* Offline banner and dialogs */
.offline-banner { padding: 0.5rem 1rem; background: hsl(var(--destructive) / 0.92); color: hsl(var(--destructive-foreground)); text-align: center; font-size: 0.84rem; }
.offline-banner[hidden] { display: none; }
.dialog { min-width: min(32rem, calc(100vw - 2rem)); padding: 1.4rem; border: 1px solid hsl(var(--border)); border-radius: 0.9rem; background: hsl(var(--card)); box-shadow: var(--shadow-card); }
.dialog::backdrop { background: hsl(var(--foreground) / 0.35); backdrop-filter: blur(2px); }

@media (max-width: 980px) {
  .appshell-content { padding-inline: 1.5rem; }
  .topbar { padding-inline: 1.5rem; }
  .content-grid { grid-template-columns: 1fr; }
}

@media (max-width: 767px) {
  .topbar-toggle { display: inline-flex; align-items: center; justify-content: center; }
  .topbar-context, .topbar-meta { display: none; }
  .topbar { min-height: 60px; padding-inline: 0.85rem; }
  .appshell-content { padding: 1.35rem 1rem calc(2rem + env(safe-area-inset-bottom)); }
  .page-heading { align-items: flex-start; flex-direction: column; }
  .page-heading .btn { width: 100%; }
  .stats-grid { grid-template-columns: 1fr; gap: 0.75rem; }
  .content-grid { gap: 0; }
  .empty-state { align-items: flex-start; flex-direction: column; }
  .sidebar { position: fixed; inset-block: 0; inset-inline-start: 0; z-index: 50; width: 85%; max-width: 320px; padding-bottom: env(safe-area-inset-bottom); transform: translateX(-100%); transition: transform 200ms ease-out; }
  html[dir="rtl"] .sidebar { transform: translateX(100%); }
  .sidebar.is-open { transform: translateX(0); }
  .sidebar-backdrop { display: block; position: fixed; inset: 0; z-index: 40; background: rgba(10, 18, 38, 0.42); opacity: 0; pointer-events: none; transition: opacity 200ms ease-out; }
  .sidebar-backdrop.is-open { opacity: 1; pointer-events: auto; }
  .dialog { position: fixed; inset-inline: 0; bottom: 0; min-width: 0; border-radius: 0.9rem 0.9rem 0 0; padding-bottom: calc(1.4rem + env(safe-area-inset-bottom)); }
}

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