/* MailStack UI Foundation — MAILSTACK-UI-FOUNDATION-001 */
:root {
  --ui-canvas: #f7f9fc;
  --ui-surface: #ffffff;
  --ui-surface-soft: #f8fafc;
  --ui-primary: #0b4ff5;
  --ui-primary-hover: #073fd0;
  --ui-primary-active: #0637b5;
  --ui-primary-tint: #eef4ff;
  --ui-text: #0b1733;
  --ui-text-muted: #667085;
  --ui-border: #d9e1ee;
  --ui-border-strong: #b8c4d6;
  --ui-success: #12a66a;
  --ui-warning: #f59e0b;
  --ui-danger: #dc2626;
  --ui-secondary: #7c3aed;
  --ui-focus: #70a1ff;
  --ui-font-sans: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --ui-font-size-xs: 0.75rem;
  --ui-font-size-sm: 0.875rem;
  --ui-font-size-base: 1rem;
  --ui-line-height: 1.5;
  --ui-space-1: 4px;
  --ui-space-2: 8px;
  --ui-space-3: 12px;
  --ui-space-4: 16px;
  --ui-space-5: 24px;
  --ui-space-6: 32px;
  --ui-space-7: 48px;
  --ui-control-height: 44px;
  --ui-radius-sm: 8px;
  --ui-radius-md: 12px;
  --ui-radius-lg: 16px;
  --ui-shadow-sm: 0 1px 3px rgba(11, 23, 51, 0.08);
  --ui-shadow-md: 0 12px 32px rgba(11, 23, 51, 0.1);
  --ui-sidebar-width: 256px;
  --ui-sidebar-collapsed-width: 80px;
  --ui-topbar-height: 72px;
  --ui-content-max: 1440px;
  --ui-transition: 180ms ease;

  /* Backward-compatible aliases consumed by the existing page styles. */
  --bg: var(--ui-canvas);
  --surface: var(--ui-surface);
  --surface-soft: var(--ui-surface-soft);
  --text: var(--ui-text);
  --muted: var(--ui-text-muted);
  --line: var(--ui-border);
  --brand: var(--ui-primary);
  --brand-dark: var(--ui-primary-hover);
  --danger: #b42318;
  --success: #087443;
  --warning: #a15c00;
  --radius: var(--ui-radius-lg);
  --shadow: var(--ui-shadow-md);
}

html {
  background: var(--ui-canvas);
}

body {
  color: var(--ui-text);
  background: var(--ui-canvas);
  font-family: var(--ui-font-sans);
  font-size: var(--ui-font-size-base);
  line-height: var(--ui-line-height);
}

body.shell-open {
  overflow: hidden;
}

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

button,
a,
input,
select,
textarea,
summary {
  outline-offset: 3px;
}

:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--ui-focus) 72%, transparent);
  outline-offset: 3px;
}

@supports not (color: color-mix(in srgb, white, black)) {
  :focus-visible {
    outline-color: rgba(11, 79, 245, 0.45);
  }
}

.ui-icon {
  display: block;
  width: 20px;
  height: 20px;
  flex: 0 0 auto;
  fill: none;
  color: currentColor;
}

.brand-lockup {
  display: inline-flex;
  align-items: center;
  min-width: 0;
  gap: 10px;
  color: var(--ui-text);
  font-weight: 800;
  letter-spacing: -0.025em;
  text-decoration: none;
}

.brand-lockup:hover {
  text-decoration: none;
}

.brand-mark {
  display: block;
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
}

.brand-name {
  overflow: hidden;
  font-size: 1.25rem;
  line-height: 1;
  white-space: nowrap;
}

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--ui-control-height);
  height: var(--ui-control-height);
  padding: 0;
  border: 1px solid var(--ui-border);
  border-radius: 10px;
  background: var(--ui-surface);
  color: #344054;
  cursor: pointer;
  transition: background var(--ui-transition), border-color var(--ui-transition), color var(--ui-transition);
}

.icon-button:hover {
  border-color: var(--ui-border-strong);
  background: var(--ui-surface-soft);
  color: var(--ui-primary);
}

/* Authenticated application shell */
.app-shell {
  display: grid;
  grid-template-columns: var(--ui-sidebar-width) minmax(0, 1fr);
  min-height: 100vh;
  background: var(--ui-canvas);
  transition: grid-template-columns var(--ui-transition);
}

.app-sidebar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  width: var(--ui-sidebar-width);
  height: 100vh;
  min-height: 560px;
  overflow: hidden;
  border-right: 1px solid var(--ui-border);
  background: var(--ui-surface);
  transition: width var(--ui-transition), transform var(--ui-transition), box-shadow var(--ui-transition);
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: var(--ui-topbar-height);
  padding: 14px 16px 14px 20px;
  border-bottom: 1px solid var(--ui-border);
  gap: 12px;
}

.sidebar-collapse {
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
}

.sidebar-nav {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 18px 12px 20px;
  scrollbar-width: thin;
}

.nav-section + .nav-section {
  margin-top: 24px;
}

.nav-section-label {
  margin: 0 10px 8px;
  color: #8090aa;
  font-size: 0.6875rem;
  font-weight: 800;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  white-space: nowrap;
}

.nav-item {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
  min-height: 44px;
  margin: 2px 0;
  padding: 10px 12px;
  border: 0;
  border-radius: 10px;
  gap: 12px;
  background: transparent;
  color: #40506a;
  font-size: 0.925rem;
  font-weight: 650;
  text-align: left;
  text-decoration: none;
  cursor: pointer;
  transition: background var(--ui-transition), color var(--ui-transition);
}

.nav-item:hover {
  background: var(--ui-surface-soft);
  color: var(--ui-primary);
  text-decoration: none;
}

.nav-item.is-active {
  background: var(--ui-primary-tint);
  color: var(--ui-primary);
  font-weight: 750;
}

.nav-item.is-active::before {
  position: absolute;
  top: 8px;
  bottom: 8px;
  left: -12px;
  width: 3px;
  border-radius: 0 3px 3px 0;
  background: var(--ui-primary);
  content: "";
}

.nav-item[hidden] {
  display: none;
}

.nav-label {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-footer {
  padding: 16px;
  border-top: 1px solid var(--ui-border);
}

.mode-card {
  display: flex;
  align-items: flex-start;
  padding: 12px;
  border: 1px solid #cfe0ff;
  border-radius: 12px;
  gap: 10px;
  background: #f4f8ff;
}

.mode-indicator {
  width: 9px;
  height: 9px;
  margin-top: 5px;
  flex: 0 0 auto;
  border-radius: 999px;
  background: var(--ui-primary);
  box-shadow: 0 0 0 4px rgba(11, 79, 245, 0.1);
}

.mode-copy {
  min-width: 0;
}

.mode-copy strong,
.mode-copy small {
  display: block;
}

.mode-copy strong {
  font-size: 0.8125rem;
}

.mode-copy small {
  margin-top: 2px;
  color: var(--ui-text-muted);
  font-size: 0.72rem;
  line-height: 1.35;
}

.app-workspace {
  display: flex;
  min-width: 0;
  min-height: 100vh;
  flex-direction: column;
}

.app-topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: var(--ui-topbar-height);
  padding: 10px 28px;
  border-bottom: 1px solid var(--ui-border);
  gap: 20px;
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(14px);
}

.topbar-leading,
.topbar-actions {
  display: flex;
  align-items: center;
  min-width: 0;
  gap: 12px;
}

.shell-menu-button {
  display: none;
}

.topbar-context {
  display: flex;
  min-width: 0;
  flex-direction: column;
}

.topbar-product {
  color: var(--ui-text);
  font-size: 0.82rem;
  font-weight: 800;
}

.topbar-domain {
  overflow: hidden;
  color: var(--ui-text-muted);
  font-size: 0.76rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-menu {
  position: relative;
}

.user-menu > summary {
  display: flex;
  align-items: center;
  min-width: 190px;
  min-height: 48px;
  padding: 5px 8px;
  border: 1px solid transparent;
  border-radius: 12px;
  gap: 10px;
  cursor: pointer;
  list-style: none;
  transition: background var(--ui-transition), border-color var(--ui-transition);
}

.user-menu > summary::-webkit-details-marker {
  display: none;
}

.user-menu > summary:hover,
.user-menu[open] > summary {
  border-color: var(--ui-border);
  background: var(--ui-surface-soft);
}

.user-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  flex: 0 0 auto;
  border-radius: 999px;
  background: var(--ui-primary-tint);
  color: var(--ui-primary);
  font-size: 0.82rem;
  font-weight: 850;
}

.user-summary-copy {
  display: flex;
  min-width: 0;
  flex: 1;
  flex-direction: column;
}

.user-summary-copy strong,
.user-summary-copy small {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-summary-copy strong {
  color: var(--ui-text);
  font-size: 0.82rem;
}

.user-summary-copy small {
  color: var(--ui-text-muted);
  font-size: 0.72rem;
}

.user-menu-chevron {
  width: 16px;
  height: 16px;
  transition: transform var(--ui-transition);
}

.user-menu[open] .user-menu-chevron {
  transform: rotate(180deg);
}

.user-menu-panel {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 70;
  width: min(260px, calc(100vw - 24px));
  overflow: hidden;
  border: 1px solid var(--ui-border);
  border-radius: 12px;
  background: var(--ui-surface);
  box-shadow: var(--ui-shadow-md);
}

.user-menu-meta {
  display: flex;
  padding: 14px 16px;
  border-bottom: 1px solid var(--ui-border);
  flex-direction: column;
}

.user-menu-meta strong {
  overflow-wrap: anywhere;
  font-size: 0.875rem;
}

.user-menu-meta span {
  margin-top: 2px;
  color: var(--ui-text-muted);
  font-size: 0.78rem;
  overflow-wrap: anywhere;
}

.user-menu-action {
  display: flex;
  align-items: center;
  width: 100%;
  min-height: 44px;
  padding: 10px 16px;
  border: 0;
  gap: 10px;
  background: transparent;
  color: #344054;
  font-weight: 700;
  text-align: left;
  cursor: pointer;
}

.user-menu-action:hover {
  background: var(--ui-surface-soft);
  color: var(--ui-danger);
}

.app-main.page-shell {
  display: block;
  min-height: 0;
  padding: 32px 28px 48px;
  flex: 1;
}

.content-container {
  width: min(100%, var(--ui-content-max));
  margin-inline: auto;
}

.app-footer {
  padding: 18px 28px;
  border-top: 1px solid var(--ui-border);
  color: var(--ui-text-muted);
  background: var(--ui-surface);
  font-size: 0.78rem;
}

.app-footer-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: min(100%, var(--ui-content-max));
  margin-inline: auto;
  gap: 20px;
}

.footer-links {
  display: inline-flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 16px;
}

.shell-backdrop {
  display: none;
}

/* Shared component normalization. Page-specific redesign remains outside this phase. */
.page-header {
  margin-bottom: 24px;
}

.page-header h1 {
  color: var(--ui-text);
  font-size: clamp(1.85rem, 3vw, 2.5rem);
}

.eyebrow {
  color: var(--ui-primary);
}

.panel,
.stat-card {
  border-color: var(--ui-border);
  box-shadow: var(--ui-shadow-sm);
}

.btn {
  min-height: 44px;
  border-color: var(--ui-border-strong);
  border-radius: 10px;
  transition: background var(--ui-transition), border-color var(--ui-transition), color var(--ui-transition), box-shadow var(--ui-transition);
}

.btn-small {
  min-height: 36px;
}

.btn-primary {
  border-color: var(--ui-primary);
  background: var(--ui-primary);
}

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

.btn-primary:active {
  border-color: var(--ui-primary-active);
  background: var(--ui-primary-active);
}

.filter-bar input,
.filter-bar select,
.form-control,
.form-group input,
.form-group select,
.form-group textarea,
select {
  min-height: 44px;
  border-color: var(--ui-border-strong);
  border-radius: 10px;
}

.filter-bar input:focus,
.filter-bar select:focus,
.form-control:focus,
.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus,
select:focus {
  border-color: var(--ui-primary);
  box-shadow: 0 0 0 3px rgba(11, 79, 245, 0.12);
}

.alert {
  border-radius: 10px;
}

/* Unauthenticated shell */
.auth-shell {
  display: flex;
  min-height: 100vh;
  flex-direction: column;
  background:
    radial-gradient(circle at 15% 15%, rgba(11, 79, 245, 0.08), transparent 36%),
    var(--ui-canvas);
}

.auth-brand-header {
  display: flex;
  align-items: center;
  min-height: 72px;
  padding: 14px clamp(18px, 4vw, 48px);
}

.auth-main {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 18px 48px;
  flex: 1;
}

.auth-container {
  width: min(100%, 520px);
}

.auth-card {
  width: 100%;
  margin: 0;
  border-color: var(--ui-border);
  box-shadow: var(--ui-shadow-md);
}

.auth-footer {
  padding: 18px;
  border-top: 1px solid var(--ui-border);
  color: var(--ui-text-muted);
  background: rgba(255, 255, 255, 0.72);
  font-size: 0.78rem;
  text-align: center;
}

/* Desktop collapsed navigation */
@media (min-width: 1200px) {
  body.sidebar-collapsed .app-shell {
    grid-template-columns: var(--ui-sidebar-collapsed-width) minmax(0, 1fr);
  }

  body.sidebar-collapsed .app-sidebar {
    width: var(--ui-sidebar-collapsed-width);
  }

  body.sidebar-collapsed .sidebar-header {
    justify-content: center;
    padding-inline: 12px;
  }

  body.sidebar-collapsed .brand-name,
  body.sidebar-collapsed .nav-section-label,
  body.sidebar-collapsed .nav-label,
  body.sidebar-collapsed .mode-copy {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }

  body.sidebar-collapsed .sidebar-collapse {
    position: absolute;
    top: 50px;
    right: -19px;
    z-index: 2;
    width: 38px;
    height: 38px;
    box-shadow: var(--ui-shadow-sm);
    transform: rotate(180deg);
  }

  body.sidebar-collapsed .nav-item {
    justify-content: center;
    padding-inline: 10px;
  }

  body.sidebar-collapsed .nav-item.is-active::before {
    left: -12px;
  }

  body.sidebar-collapsed .mode-card {
    justify-content: center;
    padding-inline: 8px;
  }
}

/* Tablet and mobile navigation drawer */
@media (max-width: 1199px) {
  .app-shell {
    display: block;
  }

  .app-sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(var(--ui-sidebar-width), calc(100vw - 48px));
    max-width: 320px;
    transform: translateX(-105%);
    box-shadow: none;
  }

  body.shell-open .app-sidebar {
    transform: translateX(0);
    box-shadow: 18px 0 50px rgba(11, 23, 51, 0.18);
  }

  .sidebar-collapse {
    display: none;
  }

  .shell-menu-button {
    display: inline-flex;
  }

  .shell-backdrop {
    position: fixed;
    inset: 0;
    z-index: 45;
    display: block;
    width: 100%;
    height: 100%;
    padding: 0;
    border: 0;
    background: rgba(11, 23, 51, 0.42);
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--ui-transition);
  }

  body.shell-open .shell-backdrop {
    opacity: 1;
    pointer-events: auto;
  }

  .app-topbar {
    padding-inline: 20px;
  }

  .app-main.page-shell {
    padding-inline: 20px;
  }

  .app-footer {
    padding-inline: 20px;
  }
}

@media (max-width: 767px) {
  :root {
    --ui-topbar-height: 64px;
  }

  .app-topbar {
    padding-inline: 12px;
  }

  .topbar-product {
    display: none;
  }

  .user-menu > summary {
    min-width: 0;
    padding-inline: 4px;
  }

  .user-summary-copy,
  .user-menu-chevron {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
  }

  .user-menu-panel {
    right: -4px;
  }

  .app-main.page-shell {
    padding: 22px 12px 36px;
  }

  .app-footer {
    padding: 16px 12px;
  }

  .app-footer-inner {
    align-items: flex-start;
    flex-direction: column;
    gap: 10px;
  }

  .footer-links {
    justify-content: flex-start;
    gap: 12px;
  }

  .page-header {
    gap: 16px;
  }

  .page-header > .btn {
    width: 100%;
  }
}

@media (prefers-reduced-motion: reduce) {
  :root {
    --ui-transition: 0ms;
  }
}
