/**
 * KPI Portal – Layout
 * Dark sidebar + topbar + main content area.
 */

/* ── App Shell ────────────────────────────────────────────────────── */
.app-shell {
  display: flex;
  min-height: 100vh;
}

/* ── Sidebar ──────────────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-width);
  min-height: 100vh;
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  z-index: var(--z-overlay);
  transition: width var(--ease-std);
  border-right: 1px solid var(--sidebar-border);
  overflow: hidden;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 16px;
  height: var(--topbar-height);
  border-bottom: 1px solid var(--sidebar-border);
  flex-shrink: 0;
  text-decoration: none;
}

.sidebar-brand-icon {
  width: 32px;
  height: 32px;
  background: var(--accent);
  border-radius: var(--r-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: white;
}

.sidebar-brand-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

.sidebar-brand-name {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-on-dark);
  letter-spacing: 0.3px;
  white-space: nowrap;
}

.sidebar-brand-sub {
  font-size: 10px;
  color: var(--sidebar-text);
  white-space: nowrap;
  text-transform: uppercase;
  letter-spacing: 0.6px;
}

.sidebar-nav {
  flex: 1;
  padding: 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow-y: auto;
  overflow-x: hidden;
}

.sidebar-section-label {
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--sidebar-text);
  padding: 8px 8px 4px;
  white-space: nowrap;
  overflow: hidden;
  margin-top: 4px;
}

.sidebar-nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: var(--r-sm);
  color: var(--sidebar-text);
  font-size: 13.5px;
  font-weight: 500;
  cursor: pointer;
  transition: background var(--ease-fast), color var(--ease-fast);
  white-space: nowrap;
  overflow: hidden;
  border: none;
  background: transparent;
  width: 100%;
  text-align: left;
  position: relative;
  text-decoration: none;
}

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

.sidebar-nav-item.active {
  background: var(--sidebar-bg-active);
  color: var(--sidebar-text-active);
}

.sidebar-nav-item.active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 4px;
  bottom: 4px;
  width: 3px;
  background: var(--sidebar-accent-line);
  border-radius: 0 2px 2px 0;
}

.sidebar-nav-item .nav-icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  opacity: 0.8;
}

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

.sidebar-nav-item .nav-badge {
  margin-left: auto;
  background: var(--danger-chart);
  color: white;
  font-size: 10px;
  font-weight: 700;
  padding: 1px 5px;
  border-radius: var(--r-pill);
  min-width: 16px;
  text-align: center;
  flex-shrink: 0;
}

.sidebar-footer {
  padding: 8px;
  border-top: 1px solid var(--sidebar-border);
  flex-shrink: 0;
}

.sidebar-user {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: var(--r-sm);
  cursor: pointer;
  transition: background var(--ease-fast);
  overflow: hidden;
}

.sidebar-user:hover {
  background: var(--sidebar-bg-hover);
}

.sidebar-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  color: white;
  flex-shrink: 0;
}

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

.sidebar-user-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-on-dark);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-user-role {
  font-size: 10px;
  color: var(--sidebar-text);
  text-transform: capitalize;
  white-space: nowrap;
}

/* ── Main Area ───────────────────────────────────────────────────── */
.main-area {
  flex: 1;
  margin-left: var(--sidebar-width);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  transition: margin-left var(--ease-std);
}

/* ── Topbar ──────────────────────────────────────────────────────── */
.topbar {
  height: var(--topbar-height);
  background: var(--topbar-bg);
  border-bottom: 1px solid var(--topbar-border);
  display: flex;
  align-items: center;
  padding: 0 24px;
  position: sticky;
  top: 0;
  z-index: calc(var(--z-overlay) - 1);
  gap: 12px;
}

.topbar-title {
  font-size: 15px;
  font-weight: 700;
  color: var(--text-primary);
  flex: 1;
}

.topbar-period-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
}

.topbar-period-label {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 500;
}

.topbar-select {
  background: var(--page-bg);
  border: 1px solid var(--border-color);
  border-radius: var(--r-sm);
  color: var(--text-primary);
  padding: 7px 10px;
  font-size: 13px;
  font-family: var(--font-ui);
  cursor: pointer;
  transition: border-color var(--ease-fast);
}

.topbar-select:focus {
  outline: none;
  border-color: var(--accent);
}

.topbar-divider {
  width: 1px;
  height: 20px;
  background: var(--border-color);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* ── Loading bar (top) ───────────────────────────────────────────── */
.topbar-progress {
  position: absolute;
  bottom: -2px;
  left: 0;
  right: 0;
  height: 2px;
  overflow: hidden;
  opacity: 0;
  transition: opacity var(--ease-fast);
}

.topbar-progress.active {
  opacity: 1;
}

.topbar-progress-bar {
  height: 100%;
  background: linear-gradient(90deg, var(--accent) 0%, var(--purple-chart) 100%);
  width: 30%;
  animation: progress-slide 1s ease-in-out infinite;
}

@keyframes progress-slide {
  0%   { transform: translateX(-100%); }
  100% { transform: translateX(450%); }
}

/* ── Page Content Area ───────────────────────────────────────────── */
.page-area {
  flex: 1;
  padding: 24px;
  max-width: 1600px;
  width: 100%;
  margin: 0 auto;
  box-sizing: border-box;
  position: relative;
}

/* PHASE 2: Page containers — persistent, visibility-toggled */
.page-container {
  width: 100%;
}

/* Smooth entrance animation on first render — defined in base.css */

/* Transient error pages */
.page-error {
  padding: 40px 0;
}

/* ── Page Header ─────────────────────────────────────────────────── */
.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 20px;
  gap: 16px;
}

.page-header-left {}

.page-header-title {
  font-size: 22px;
  font-weight: 800;
  color: var(--text-primary);
  line-height: 1.2;
  margin-bottom: 3px;
}

.page-header-sub {
  font-size: 13px;
  color: var(--text-muted);
}

.page-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

/* ── Breadcrumb ──────────────────────────────────────────────────── */
.breadcrumb {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 4px;
  font-size: 12px;
}

.breadcrumb-item {
  color: var(--text-muted);
  cursor: pointer;
  transition: color var(--ease-fast);
}

.breadcrumb-item:hover { color: var(--accent); }
.breadcrumb-item.active { color: var(--text-secondary); cursor: default; }
.breadcrumb-sep { color: var(--text-faint); }

/* ── Stats Grid ──────────────────────────────────────────────────── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 14px;
  margin-bottom: 20px;
}

/* ── Section card wrapper ────────────────────────────────────────── */
.section-card {
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: var(--card-radius);
  box-shadow: var(--shadow-xs);
  overflow: hidden;
}

.section-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border-light);
  background: var(--page-bg);
}

.section-card-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  gap: 8px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.section-card-count {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 400;
}

.section-card-actions {
  display: flex;
  gap: 8px;
  align-items: center;
}

/* ── Login screen ────────────────────────────────────────────────── */
.login-screen {
  display: none;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(145deg, var(--sidebar-bg) 0%, #162235 60%, #0D2444 100%);
  padding: 24px;
}

.login-screen.active {
  display: flex;
}

.login-card {
  background: var(--card-bg);
  border-radius: var(--r-xl);
  padding: 40px;
  width: 100%;
  max-width: 420px;
  box-shadow: var(--shadow-lg);
}

.login-header {
  text-align: center;
  margin-bottom: 32px;
}

.login-logo {
  width: 56px;
  height: 56px;
  background: var(--accent);
  border-radius: var(--r-md);
  display: flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 16px;
  color: white;
}

.login-title {
  font-size: 22px;
  font-weight: 800;
  margin-bottom: 4px;
}

.login-subtitle {
  font-size: 13px;
  color: var(--text-muted);
}

/* ── Responsive ──────────────────────────────────────────────────── */
@media (max-width: 1280px) {
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 1024px) {
  .sidebar { width: var(--sidebar-width-sm); }
  .sidebar-brand-text,
  .sidebar-nav-item span:not(.nav-badge),
  .sidebar-section-label,
  .sidebar-user-info { display: none; }
  .sidebar-nav-item { justify-content: center; padding: 10px; }
  .sidebar-nav-item.active::before { top: 0; bottom: 0; }
  .main-area { margin-left: var(--sidebar-width-sm); }
  .sidebar-user { justify-content: center; }
}

@media (max-width: 768px) {
  .stats-grid { grid-template-columns: 1fr; }
  .page-area { padding: 16px; }
  .topbar { padding: 0 16px; }
  .page-header { flex-direction: column; }
  .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
}
