/* ============================================================
   Layout: sidebar, topbar, main, app shell
   ============================================================ */

.app {
  display: grid;
  grid-template-areas: "sidebar main";
  grid-template-columns: var(--sidebar-width) 1fr;
  min-height: 100vh;
}

[dir="rtl"] .app {
  grid-template-areas: "main sidebar";
  grid-template-columns: 1fr var(--sidebar-width);
}

.app.sidebar-collapsed {
  grid-template-areas: "sidebar main";
  grid-template-columns: var(--sidebar-collapsed) 1fr;
}
[dir="rtl"] .app.sidebar-collapsed {
  grid-template-areas: "main sidebar";
  grid-template-columns: 1fr var(--sidebar-collapsed);
}

.app > .sidebar { grid-area: sidebar; }
.app > .sidebar-mobile-overlay { display: none; }
.app > .main { grid-area: main; min-width: 0; }

/* ==== Sidebar ==== */
.sidebar {
  background: var(--bg-card);
  border-inline-end: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow: hidden;
  transition: all var(--duration-base) var(--ease-out);
  z-index: var(--z-sticky);
}

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

.brand-icon {
  width: 36px;
  height: 36px;
  background: linear-gradient(135deg, var(--brand-accent) 0%, var(--brand-accent-2) 100%);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  color: var(--brand-on-accent);
  font-size: 18px;
  box-shadow: var(--shadow-accent);
  flex-shrink: 0;
}

.brand-info { display: flex; flex-direction: column; min-width: 0; }
.brand-name { font-size: 15px; font-weight: 700; letter-spacing: -0.01em; color: var(--text-primary); }
.brand-sub { font-size: 10px; color: var(--text-muted); font-family: var(--font-mono); }

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

.nav-section-title {
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-faint);
  padding: 8px 10px 4px;
  font-weight: 700;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--duration-fast) var(--ease-out);
  position: relative;
  margin-bottom: 2px;
}

.nav-item:hover { background: var(--bg-card-2); color: var(--text-primary); }
.nav-item.active {
  background: var(--accent-dim);
  color: var(--accent);
  font-weight: 600;
}
.nav-item.active::before {
  content: '';
  position: absolute;
  inset-inline-start: 0;
  top: 8px; bottom: 8px;
  width: 3px;
  background: var(--accent);
  border-radius: 0 2px 2px 0;
}
[dir="rtl"] .nav-item.active::before {
  border-radius: 2px 0 0 2px;
}

.nav-item .icon { font-size: 16px; width: 20px; text-align: center; flex-shrink: 0; }
.nav-item .label { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.sidebar-collapsed .nav-item .label,
.sidebar-collapsed .nav-section-title,
.sidebar-collapsed .brand-info { display: none; }
.sidebar-collapsed .sidebar-header { justify-content: center; padding: 16px 0; }

.sidebar-footer {
  padding: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 8px;
}

/* ==== Topbar ==== */
.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.topbar {
  height: var(--topbar-height);
  background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 20px;
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  backdrop-filter: blur(8px);
}

.topbar-burger {
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  transition: background var(--duration-fast);
}
.topbar-burger:hover { background: var(--bg-card-2); color: var(--text-primary); }

.topbar-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-spacer { flex: 1; }

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

.topbar-search {
  position: relative;
  width: 280px;
}
.topbar-search input {
  width: 100%;
  padding: 8px 12px 8px 36px;
  background: var(--bg-card-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text-primary);
  font-size: 13px;
  font-family: inherit;
  transition: all var(--duration-fast);
}
.topbar-search input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-dim);
}
.topbar-search .icon {
  position: absolute;
  inset-inline-start: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}
[dir="rtl"] .topbar-search input { padding: 8px 36px 8px 12px; }

.icon-btn {
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  font-size: 16px;
  transition: all var(--duration-fast);
  position: relative;
}
.icon-btn:hover { background: var(--bg-card-2); color: var(--text-primary); }
.icon-btn.active { background: var(--accent-dim); color: var(--accent); }

.icon-btn .dot {
  position: absolute;
  top: 6px; inset-inline-end: 6px;
  width: 8px; height: 8px;
  background: var(--success);
  border-radius: 50%;
  border: 2px solid var(--bg-card);
}

/* ==== Content ==== */
/* P1 #1 (2026-06-17): .sidebar-mobile-overlay must be removed from grid
   flow on desktop, otherwise it occupies column 2 of the .app grid
   and pushes <main> into column 3 (off-grid), collapsing it to ~248px. */
.sidebar-mobile-overlay { display: none; }

.content {
  flex: 1;
  padding: 24px;
  max-width: var(--content-max);
  width: 100%;
  margin: 0 auto;
  animation: fadeIn var(--duration-base) var(--ease-out);
}

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  margin-bottom: 24px;
  flex-wrap: wrap;
}
.page-title { font-size: 22px; font-weight: 800; color: var(--text-primary); letter-spacing: -0.02em; }
.page-subtitle { font-size: 13px; color: var(--text-muted); margin-top: 4px; }

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

/* ==== Mobile ==== */
@media (max-width: 768px) {
  .app, [dir="rtl"] .app {
    grid-template-columns: 1fr;
  }
  .sidebar {
    position: fixed;
    inset-inline-start: 0;
    top: 0;
    bottom: 0;
    width: var(--sidebar-width);
    transform: translateX(-100%);
    z-index: var(--z-drawer);
  }
  [dir="rtl"] .sidebar { transform: translateX(100%); }
  .app.mobile-sidebar-open .sidebar { transform: translateX(0); }
  .topbar-search { display: none; }
  .content { padding: 16px; }
  .page-title { font-size: 18px; }
  .sidebar-mobile-overlay {
    position: fixed;
    inset: 0;
    background: var(--bg-overlay);
    z-index: calc(var(--z-drawer) - 1);
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--duration-base);
  }
  .app.mobile-sidebar-open .sidebar-mobile-overlay {
    opacity: 1;
    pointer-events: auto;
  }
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}
@keyframes spin {
  to { transform: rotate(360deg); }
}
.spinner {
  width: 16px; height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  display: inline-block;
}
