:root {
  --bg-page: #f7f8fa;
  --bg-card: #ffffff;
  --bg-sidebar: #f7f8fa;
  --bg-header: #ffffff;
  --bg-hover: #f0f2f5;
  --bg-active: #e8ebef;
  --bg-input: #ffffff;
  --bg-table-header: #f7f8fa;

  --text-primary: #1a1a1a;
  --text-secondary: #5e6577;
  --text-muted: #8792a2;
  --text-inverse: #ffffff;

  --border: #e3e8ee;
  --border-subtle: #eef0f3;

  --accent: #635bff;
  --accent-hover: #5046e5;
  --accent-subtle: rgba(99, 91, 255, 0.08);
  --accent-text: #635bff;

  --success: #0d9e6e;
  --success-subtle: rgba(13, 158, 110, 0.08);
  --warning: #d97706;
  --warning-subtle: rgba(217, 119, 6, 0.08);
  --danger: #dc2626;
  --danger-subtle: rgba(220, 38, 38, 0.06);
  --info: #2563eb;
  --info-subtle: rgba(37, 99, 235, 0.08);

  --sidebar-width: 240px;
  --header-height: 52px;
  --radius: 8px;
  --radius-sm: 6px;
  --radius-lg: 10px;

  --shadow-xs: 0 1px 2px rgba(0,0,0,0.04);
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.08);
  --shadow-lg: 0 12px 32px rgba(0,0,0,0.12);

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: "SF Mono", SFMono-Regular, Consolas, "Liberation Mono", Menlo, monospace;
}

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

html, body {
  height: 100%;
  overflow: hidden;
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text-primary);
  background: var(--bg-page);
  -webkit-font-smoothing: antialiased;
}

::selection {
  background: var(--accent);
  color: white;
}

/* ===== LAYOUT ===== */
.app-wrapper {
  display: none;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

.app-wrapper.active {
  display: flex;
  flex-direction: column;
}

/* ===== HEADER ===== */
.app-header {
  height: var(--header-height);
  background: var(--bg-header);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 20px;
  gap: 12px;
  flex-shrink: 0;
  z-index: 100;
}

.app-header .logo {
  font-size: 17px;
  font-weight: 700;
  color: var(--text-primary);
  letter-spacing: -0.02em;
}

.app-header .tenant-name {
  font-size: 13px;
  color: var(--text-muted);
  padding-left: 12px;
  border-left: 1px solid var(--border);
}

.app-header .header-spacer { flex: 1; }

.app-header .user-menu {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: var(--radius-sm);
  transition: background 120ms;
}

.app-header .user-menu:hover {
  background: var(--bg-hover);
}

.app-header .user-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
}

.app-header .user-name {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
}

.app-body {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ===== SIDEBAR ===== */
.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow-y: auto;
}

.sidebar::-webkit-scrollbar { width: 0; }

.sidebar-nav {
  padding: 12px 8px;
  flex: 1;
}

.nav-section {
  margin-bottom: 4px;
}

.nav-section-title {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  padding: 8px 12px 4px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 120ms;
  user-select: none;
}

.nav-item:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.nav-item.active {
  background: var(--accent-subtle);
  color: var(--accent);
}

.nav-item svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  opacity: 0.6;
}

.nav-item:hover svg,
.nav-item.active svg {
  opacity: 1;
}

/* ===== NAV FLYOUT ===== */
.nav-flyout-parent {
  position: relative;
}

.nav-flyout-trigger {
  cursor: pointer;
}

.nav-flyout-parent.active > .nav-flyout-trigger,
.nav-flyout-parent:has(.nav-item.active) > .nav-flyout-trigger {
  background: var(--accent-subtle);
  color: var(--accent);
}

.nav-flyout-parent:has(.nav-item.active) > .nav-flyout-trigger svg {
  opacity: 1;
}

.nav-flyout {
  display: none;
  position: fixed;
  min-width: 160px;
  max-height: calc(100vh - 80px);
  overflow-y: auto;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0,0,0,0.2);
  padding: 6px;
  z-index: 1000;
}

.nav-flyout-parent.open > .nav-flyout {
  display: block;
}

/* Mobile: render the flyout inline under its trigger rather than floating.
   Avoids the stacking-context + portal mess that caused the submenu to
   render behind page content or overlap adjacent menu rows. */
@media (max-width: 768px) {
  .nav-flyout-parent .nav-flyout {
    position: static !important;
    box-shadow: none !important;
    border: none !important;
    background: transparent !important;
    padding: 0 0 4px 20px !important;
    margin: 0 !important;
    min-width: 0 !important;
    max-height: none !important;
    left: auto !important;
    top: auto !important;
  }
  .nav-flyout-parent.open > .nav-flyout {
    display: block !important;
  }
}

.nav-flyout-item {
  display: block;
  padding: 7px 14px;
  font-size: 13px;
  border-radius: var(--radius-sm);
  color: var(--text-secondary);
  text-decoration: none;
  white-space: nowrap;
}

.nav-flyout-item:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.nav-flyout-item.active {
  background: var(--accent-subtle);
  color: var(--accent);
}

/* Nested collapsible section inside a flyout (e.g. Admin → Designers) */
.nav-flyout-section { display: flex; flex-direction: column; }
.nav-flyout-section-trigger {
  display: flex; align-items: center; justify-content: space-between;
  width: 100%;
  padding: 7px 14px;
  font-size: 13px;
  font-family: inherit;
  background: transparent;
  border: 0;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: var(--radius-sm);
  text-align: left;
}
.nav-flyout-section-trigger:hover { background: var(--bg-hover); color: var(--text-primary); }
.nav-flyout-section-chevron { transition: transform 150ms ease; flex-shrink: 0; opacity: 0.6; }
.nav-flyout-section.open .nav-flyout-section-chevron { transform: rotate(90deg); }
.nav-flyout-section-body {
  display: none;
  padding-left: 12px;
  margin: 2px 0 4px;
  border-left: 2px solid var(--border);
  margin-left: 14px;
}
.nav-flyout-section.open .nav-flyout-section-body { display: flex; flex-direction: column; gap: 1px; }

/* ===== CONTENT ===== */
.content-area {
  flex: 1;
  overflow-y: auto;
  background: var(--bg-page);
}

.content-area::-webkit-scrollbar { width: 6px; }
.content-area::-webkit-scrollbar-track { background: transparent; }
.content-area::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 24px 32px 16px;
  gap: 16px;
}

.page-title {
  font-size: 20px;
  font-weight: 600;
  color: var(--text-primary);
  letter-spacing: -0.02em;
}

.page-subtitle {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 1px;
}

.page-actions {
  display: flex;
  gap: 8px;
}

.page-content {
  padding: 0 32px 32px;
}

/* Nav badge — small red pill showing unread count, sits inline with nav text */
.nav-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  margin-left: 8px;
  border-radius: 9px;
  background: #ef4444;
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  line-height: 1;
}
.nav-badge.alert {
  background: #f59e0b;
}
.nav-badge.alert::before {
  content: '!';
}

/* ===== LOGIN ===== */
.login-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: var(--bg-page);
}

.login-card {
  width: 100%;
  max-width: 380px;
  padding: 36px 32px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
}

.login-logo {
  text-align: center;
  font-size: 20px;
  font-weight: 700;
  color: var(--text-primary);
  margin-bottom: 4px;
  letter-spacing: -0.02em;
}

.login-subtitle {
  text-align: center;
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 28px;
}

.login-error {
  display: none;
  background: var(--danger-subtle);
  color: var(--danger);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  margin-bottom: 16px;
}

.login-error.visible { display: block; }

/* ===== LOCK SCREEN ===== */
.lock-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(4px);
  z-index: 9999;
  align-items: center;
  justify-content: center;
}

.lock-overlay.active { display: flex; }

.lock-card {
  width: 100%;
  max-width: 340px;
  padding: 32px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  text-align: center;
  box-shadow: var(--shadow-md);
}

.lock-avatar {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--accent);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 600;
  margin: 0 auto 12px;
}

.lock-name {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 2px;
}

.lock-message {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 20px;
}

/* ===== RESPONSIVE ===== */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: -240px;
    top: var(--header-height);
    bottom: 0;
    z-index: 200;
    transition: left 200ms ease;
    background: var(--bg-card);
    box-shadow: var(--shadow-lg);
  }

  .sidebar.open { left: 0; }

  .mobile-overlay {
    display: none;
    position: fixed;
    inset: 0;
    top: var(--header-height);
    background: rgba(0,0,0,0.2);
    z-index: 199;
  }

  .mobile-overlay.active { display: block; }
  .menu-toggle { display: flex !important; }
  .page-header { padding: 16px; }
  /* Extra bottom padding for iOS so the last item isn't hidden behind
     the home indicator / Safari URL bar. max() keeps the base 16px on
     non-notched devices and adds the inset on devices that expose one. */
  .page-content { padding: 0 16px max(16px, env(safe-area-inset-bottom)); }
  .content-area { padding-bottom: env(safe-area-inset-bottom); }
}

.menu-toggle {
  display: none;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: var(--radius-sm);
}

.menu-toggle:hover {
  background: var(--bg-hover);
}
