/* ── MOBILE MENU BUTTON (hidden by default, shown at ≤768px) ── */
#mobile-menu-btn { display: none; }

/* ── APP SHELL ── */
#app-shell {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ── SIDEBAR ── */
#sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--bg-surface);
  border-right: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width 0.2s ease, min-width 0.2s ease;
  z-index: 100;
}

#sidebar.collapsed {
  width: 60px;
  min-width: 60px;
}

.sidebar-logo {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: var(--header-h);
}

.sidebar-logo .logo-mark {
  font-size: var(--text-lg);
  font-weight: 700;
  color: var(--brand-500);
  white-space: nowrap;
  letter-spacing: -0.5px;
}

.sidebar-logo .logo-sub {
  font-size: var(--text-xs);
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
}

#sidebar.collapsed .logo-sub { display: none; }

.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 8px 0;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 20px;
  color: var(--text-secondary);
  font-size: var(--text-sm);
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
  position: relative;
  border-left: 3px solid transparent;
  user-select: none;
}

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

.nav-item.active {
  background: var(--bg-elevated);
  color: var(--text-primary);
  border-left-color: var(--brand-500);
}

.nav-item svg {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
}

.nav-label {
  overflow: hidden;
  transition: opacity 0.15s;
}

#sidebar.collapsed .nav-label { opacity: 0; width: 0; overflow: hidden; }
#sidebar.collapsed .nav-item { padding: 10px; justify-content: center; }
#sidebar.collapsed .nav-item { border-left-width: 0; border-bottom: 3px solid transparent; }

.sidebar-divider {
  height: 1px;
  background: var(--border-subtle);
  margin: 8px 0;
}

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  gap: 10px;
}

.sidebar-footer .user-avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--brand-500);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
  color: white;
  flex-shrink: 0;
}

.sidebar-footer .user-info {
  flex: 1;
  overflow: hidden;
  min-width: 0;
}

.sidebar-footer .user-name {
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--text-primary);
  truncate: overflow;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-footer .user-role {
  font-size: 10px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#sidebar.collapsed .sidebar-footer { justify-content: center; }
#sidebar.collapsed .sidebar-footer .user-info { display: none; }

/* ── SIDEBAR TOGGLE ── */
#sidebar-toggle {
  background: none;
  border: none;
  color: var(--text-muted);
  padding: 6px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  transition: color 0.15s, background 0.15s;
  flex-shrink: 0;
}

#sidebar-toggle:hover { color: var(--text-primary); background: var(--bg-elevated); }

/* ── MAIN AREA ── */
#main-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

/* ── HEADER ── */
#app-header {
  height: var(--header-h);
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  padding: 0 20px;
  gap: 16px;
  flex-shrink: 0;
  z-index: 50;
}

.header-breadcrumb {
  font-size: var(--text-sm);
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 6px;
}

.header-breadcrumb .current {
  color: var(--text-primary);
  font-weight: 500;
}

.header-search {
  flex: 1;
  max-width: 400px;
  position: relative;
}

.header-search input {
  width: 100%;
  background: var(--bg-elevated);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 7px 12px 7px 36px;
  font-size: var(--text-sm);
  color: var(--text-primary);
  outline: none;
  transition: border-color 0.15s;
}

.header-search input::placeholder { color: var(--text-muted); }
.header-search input:focus { border-color: var(--brand-500); }

.header-search .search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}

.header-search .search-kbd {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 10px;
  color: var(--text-muted);
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: 4px;
  padding: 1px 5px;
  font-family: var(--font-mono);
  pointer-events: none;
}

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

.header-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  padding: 6px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s, background 0.15s;
  position: relative;
}

.header-btn:hover { color: var(--text-primary); background: var(--bg-elevated); }

.header-btn .badge {
  position: absolute;
  top: 2px;
  right: 2px;
  background: var(--danger-fg);
  color: white;
  font-size: 9px;
  font-weight: 700;
  border-radius: 9999px;
  min-width: 14px;
  height: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 3px;
}

.save-indicator {
  font-size: var(--text-xs);
  color: var(--warning-fg);
  display: flex;
  align-items: center;
  gap: 4px;
}

.save-indicator.saved { color: var(--success-fg); }

/* ── APP CONTENT ── */
#app-content {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 24px;
  background: var(--bg-base);
}

/* ── VIEW TRANSITIONS ── */
.view-enter {
  animation: viewFadeIn 0.2s ease-out;
}

@keyframes viewFadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── VIEW HEADERS ── */
.view-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
  flex-wrap: wrap;
  gap: 12px;
}

.view-title {
  font-size: var(--text-xl);
  font-weight: 700;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: 10px;
}

.view-subtitle {
  font-size: var(--text-sm);
  color: var(--text-secondary);
  margin-top: 2px;
}

.view-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

/* ── TOOLBAR ── */
.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
  flex-wrap: wrap;
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 10px 14px;
}

.toolbar-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

.toolbar-divider {
  width: 1px;
  height: 20px;
  background: var(--border-subtle);
  margin: 0 4px;
}

/* ── GRID LAYOUTS ── */
.grid-2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.grid-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; }
.grid-auto { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }

.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.charts-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
  margin-bottom: 24px;
}

/* ── RESPONSIVE ── */
@media (max-width: 1200px) {
  .charts-grid { grid-template-columns: 1fr; }
}

@media (max-width: 900px) {
  .grid-4 { grid-template-columns: repeat(2, 1fr); }
  .kpi-grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
}

@media (max-width: 768px) {
  /* Sidebar: off-canvas overlay */
  #sidebar {
    position: fixed;
    left: -100%;
    top: 0;
    height: 100%;
    transition: left 0.25s ease;
    z-index: 200;
    width: var(--sidebar-w) !important;
    min-width: var(--sidebar-w) !important;
  }
  #sidebar.mobile-open { left: 0; }
  #sidebar.collapsed { width: var(--sidebar-w) !important; min-width: var(--sidebar-w) !important; }
  #sidebar.collapsed .nav-label { opacity: 1; width: auto; overflow: visible; }
  #sidebar.collapsed .nav-item { padding: 10px 20px; justify-content: flex-start; border-left-width: 3px; border-bottom-width: 0; }
  #sidebar.collapsed .logo-sub { display: block; }
  #sidebar.collapsed .sidebar-footer { justify-content: flex-start; }
  #sidebar.collapsed .sidebar-footer .user-info { display: block; }

  .sidebar-overlay {
    position: fixed;
    inset: 0;
    background: var(--bg-overlay);
    z-index: 150;
    display: none;
  }
  .sidebar-overlay.active { display: block; }

  /* Header */
  #mobile-menu-btn { display: flex; }
  #sidebar-toggle  { display: none; }
  #app-header      { padding: 0 12px; gap: 8px; }
  .header-search   { display: none; }
  .header-breadcrumb { font-size: var(--text-xs); }

  /* Content */
  #app-content { padding: 12px; }

  /* Grids collapse */
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
  .kpi-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
  .charts-grid { grid-template-columns: 1fr; }

  /* View header stacks */
  .view-header { flex-direction: column; align-items: flex-start; }
  .view-actions {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    display: flex;
    gap: 6px;
  }
  .view-actions::-webkit-scrollbar { display: none; }

  /* Toolbar scrollable */
  .toolbar {
    overflow-x: auto;
    flex-wrap: nowrap;
    gap: 6px;
    -webkit-overflow-scrolling: touch;
  }
  .toolbar::-webkit-scrollbar { display: none; }
  .filter-bar {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .filter-bar::-webkit-scrollbar { display: none; }

  /* Form rows single column */
  .form-row, .form-row-3 { grid-template-columns: 1fr; }
}

/* ── PRINT ── */
@media print {
  #sidebar, #app-header, .view-actions,
  .toolbar, .header-actions, #toast-container,
  .drawer-backdrop, .drawer, .modal-backdrop { display: none !important; }
  #app-shell, #main-area { display: block !important; overflow: visible; height: auto; }
  #app-content { padding: 0 !important; overflow: visible !important; height: auto !important; }
  body { background: white !important; color: #111 !important; }
  .card, .kpi-card { break-inside: avoid; border: 1px solid #ddd !important; }
  .kpi-value, .view-title { color: #111 !important; }
  a[href]::after { content: none; }
}
