/* =============================================================================
   App Shell — Control Tower Layout
   Top Nav (72px sticky/glass) + Left Rail (256/88) + Main Grid + Right Rail (360)
   ============================================================================= */

/* ---------- ROOT GRID ---------- */
.app-shell {
  display: grid;
  grid-template-columns: var(--shell-rail-w) 1fr var(--shell-rightrail-w);
  grid-template-rows: var(--shell-topnav-h) 1fr;
  grid-template-areas:
    "rail topnav topnav"
    "rail main   rightrail";
  min-height: 100vh;
  background: var(--bg-canvas);
  transition: grid-template-columns var(--motion-drawer) var(--motion-spring);
}
.app-shell[data-rail-collapsed="true"] {
  grid-template-columns: var(--shell-rail-w-collapsed) 1fr var(--shell-rightrail-w);
}
.app-shell[data-rightrail-hidden="true"] {
  grid-template-columns: var(--shell-rail-w) 1fr 0;
}
.app-shell[data-rail-collapsed="true"][data-rightrail-hidden="true"] {
  grid-template-columns: var(--shell-rail-w-collapsed) 1fr 0;
}

/* ---------- TOP NAV ---------- */
.top-nav {
  grid-area: topnav;
  position: sticky;
  top: 0;
  z-index: var(--z-topnav);
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: var(--space-4);
  height: var(--shell-topnav-h);
  padding: 0 var(--space-6);
  background: var(--bg-glass);
  -webkit-backdrop-filter: saturate(140%) blur(var(--blur-glass));
  backdrop-filter: saturate(140%) blur(var(--blur-glass));
  border-bottom: 1px solid var(--border-subtle);
}
.top-nav__left, .top-nav__center, .top-nav__right {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}
.top-nav__center {
  justify-self: center;
  width: 100%;
  max-width: 560px;
}

/* Yard switcher pill */
.yard-switcher {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-pill);
  font-weight: 600;
  font-size: 14px;
  color: var(--text-primary);
  cursor: pointer;
  transition: all var(--motion-hover) var(--motion-ease);
}
.yard-switcher:hover {
  border-color: var(--brand-primary);
  background: var(--brand-primary-soft);
}
.yard-switcher__dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--state-success);
  box-shadow: 0 0 0 3px rgba(21, 127, 59, 0.18);
}

/* Global search */
.global-search {
  position: relative;
  width: 100%;
}
.global-search__input {
  width: 100%;
  height: 40px;
  padding: 0 var(--space-4) 0 var(--space-10);
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-control);
  font-family: var(--font-primary);
  font-size: 14px;
  color: var(--text-primary);
  transition: all var(--motion-hover) var(--motion-ease);
}
.global-search__input:hover { border-color: var(--border-default); }
.global-search__input:focus {
  outline: none;
  border-color: var(--brand-primary);
  box-shadow: var(--shadow-focus);
}
.global-search__input::placeholder { color: var(--text-tertiary); }
.global-search__icon {
  position: absolute;
  left: var(--space-3);
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-tertiary);
  pointer-events: none;
}
.global-search__shortcut {
  position: absolute;
  right: var(--space-3);
  top: 50%;
  transform: translateY(-50%);
  font-size: 11px;
  font-family: var(--font-mono);
  color: var(--text-tertiary);
  background: var(--bg-canvas);
  padding: 2px 6px;
  border-radius: 4px;
  border: 1px solid var(--border-subtle);
}

/* Live pill */
.live-pill {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-1) var(--space-3);
  font-size: 12px;
  font-weight: 600;
  color: var(--state-success);
  background: var(--state-success-soft);
  border-radius: var(--radius-pill);
  border: 1px solid rgba(21, 127, 59, 0.18);
}
.live-pill__dot {
  width: 6px; height: 6px;
  border-radius: 50%;
  background: var(--state-success);
  animation: pulse 2s infinite;
}
.live-pill[data-state="reconnecting"] {
  color: var(--state-warning);
  background: var(--state-warning-soft);
  border-color: rgba(179, 90, 0, 0.18);
}
.live-pill[data-state="reconnecting"] .live-pill__dot { background: var(--state-warning); }
.live-pill[data-state="stale"] {
  color: var(--text-tertiary);
  background: var(--state-neutral-soft);
  border-color: var(--border-subtle);
}
.live-pill[data-state="stale"] .live-pill__dot {
  background: var(--text-tertiary);
  animation: none;
}
.live-pill[data-state="paused"] {
  color: var(--state-info);
  background: var(--state-info-soft);
  border-color: rgba(3, 105, 161, 0.18);
}

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.6; transform: scale(1.2); }
}

/* Top nav icon button */
.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px; height: 40px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-control);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all var(--motion-hover) var(--motion-ease);
  position: relative;
}
.icon-button:hover {
  background: var(--bg-surface);
  border-color: var(--border-subtle);
  color: var(--text-primary);
}
.icon-button[aria-pressed="true"] {
  background: var(--brand-primary-soft);
  color: var(--brand-primary);
}
.icon-button__badge {
  position: absolute;
  top: 6px; right: 6px;
  min-width: 16px; height: 16px;
  padding: 0 4px;
  font-size: 10px;
  font-weight: 700;
  color: white;
  background: var(--state-danger);
  border-radius: var(--radius-pill);
  display: inline-flex; align-items: center; justify-content: center;
  border: 2px solid var(--bg-glass);
}

/* User chip */
.user-chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-1) var(--space-3) var(--space-1) var(--space-1);
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-pill);
  cursor: pointer;
  transition: all var(--motion-hover) var(--motion-ease);
}
.user-chip:hover { border-color: var(--brand-primary); }
.user-chip__avatar {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: var(--gradient-brand);
  color: white;
  font-size: 12px;
  font-weight: 700;
  display: inline-flex; align-items: center; justify-content: center;
}
.user-chip__name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
}

/* ---------- LEFT RAIL ---------- */
.left-rail {
  grid-area: rail;
  position: sticky;
  top: 0;
  align-self: start;
  height: 100vh;
  background: var(--bg-surface);
  border-right: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  overflow-x: hidden;
  z-index: var(--z-rail);
  transition: width var(--motion-drawer) var(--motion-spring);
}

.left-rail__brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-5);
  border-bottom: 1px solid var(--border-subtle);
  height: var(--shell-topnav-h);
  flex-shrink: 0;
}
.left-rail__logo {
  width: 40px; height: 40px;
  border-radius: var(--radius-control);
  background: var(--gradient-brand);
  color: white;
  font-weight: 800;
  font-size: 16px;
  display: inline-flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.left-rail__brand-text {
  font-weight: 700;
  font-size: 16px;
  color: var(--text-primary);
  letter-spacing: -0.01em;
  white-space: nowrap;
  transition: opacity var(--motion-fast) var(--motion-ease);
}

.left-rail__nav {
  flex: 1;
  padding: var(--space-3) var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.rail-section-title {
  padding: var(--space-4) var(--space-3) var(--space-2);
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-tertiary);
  white-space: nowrap;
  transition: opacity var(--motion-fast) var(--motion-ease);
}

.rail-item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-3);
  border-radius: var(--radius-control);
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 500;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
  transition: all var(--motion-hover) var(--motion-ease);
  position: relative;
}
.rail-item:hover {
  background: var(--brand-primary-soft);
  color: var(--brand-primary);
}
.rail-item.active {
  background: var(--brand-primary-soft);
  color: var(--brand-primary);
  font-weight: 600;
}
.rail-item.active::before {
  content: '';
  position: absolute;
  left: -12px;
  top: 50%;
  transform: translateY(-50%);
  width: 3px; height: 24px;
  background: var(--brand-primary);
  border-radius: 0 4px 4px 0;
}
.rail-item__icon {
  width: 20px; height: 20px;
  flex-shrink: 0;
}
.rail-item__text {
  flex: 1;
  transition: opacity var(--motion-fast) var(--motion-ease);
}
.rail-item__badge {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 22px; height: 18px;
  padding: 0 6px;
  font-size: 11px;
  font-weight: 700;
  border-radius: var(--radius-pill);
  background: var(--state-danger);
  color: white;
}

.left-rail__footer {
  padding: var(--space-3);
  border-top: 1px solid var(--border-subtle);
  flex-shrink: 0;
}
.left-rail__toggle {
  width: 100%;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background: transparent;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-control);
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--motion-hover) var(--motion-ease);
}
.left-rail__toggle:hover {
  border-color: var(--brand-primary);
  color: var(--brand-primary);
}

/* COLLAPSED STATE */
.app-shell[data-rail-collapsed="true"] .left-rail__brand-text,
.app-shell[data-rail-collapsed="true"] .rail-item__text,
.app-shell[data-rail-collapsed="true"] .rail-item__badge,
.app-shell[data-rail-collapsed="true"] .rail-section-title,
.app-shell[data-rail-collapsed="true"] .left-rail__toggle span {
  opacity: 0;
  visibility: hidden;
  width: 0;
  pointer-events: none;
}
.app-shell[data-rail-collapsed="true"] .left-rail__brand,
.app-shell[data-rail-collapsed="true"] .rail-item,
.app-shell[data-rail-collapsed="true"] .left-rail__toggle {
  justify-content: center;
}
.app-shell[data-rail-collapsed="true"] .rail-section-title {
  padding: var(--space-2) 0;
  height: 1px;
  margin: var(--space-2) auto;
  background: var(--border-subtle);
  width: 24px;
  overflow: hidden;
  visibility: visible;
  opacity: 1;
  color: transparent;
}

/* Tooltip on collapsed rail items (CSS-only) */
.app-shell[data-rail-collapsed="true"] .rail-item:hover::after {
  content: attr(data-label);
  position: absolute;
  left: calc(100% + 12px);
  top: 50%;
  transform: translateY(-50%);
  padding: 6px 12px;
  background: var(--text-primary);
  color: var(--text-inverse);
  font-size: 12px;
  font-weight: 600;
  border-radius: var(--radius-control);
  box-shadow: var(--shadow-md);
  white-space: nowrap;
  z-index: var(--z-dropdown);
  pointer-events: none;
}

/* ---------- MAIN CONTENT ---------- */
.main-area {
  grid-area: main;
  min-width: 0;          /* prevent grid blowout */
  display: flex;
  flex-direction: column;
}

.filter-bar {
  position: sticky;
  top: var(--shell-topnav-h);
  z-index: var(--z-sticky);
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-6);
  height: var(--shell-filterbar-h);
  background: var(--bg-glass-strong);
  -webkit-backdrop-filter: blur(var(--blur-glass-soft));
  backdrop-filter: blur(var(--blur-glass-soft));
  border-bottom: 1px solid var(--border-subtle);
}

.page-content {
  flex: 1;
  padding: var(--space-6);
  display: block;             /* default: legacy pages render in flow */
}
.page-content.page-content--grid {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  gap: var(--shell-grid-gap);
  align-content: start;
}
/* When a child page declares an inner grid wrapper */
.page-grid {
  display: grid;
  grid-template-columns: repeat(12, 1fr);
  gap: var(--shell-grid-gap);
  align-content: start;
}

/* Span helpers */
.col-3  { grid-column: span 3; }
.col-4  { grid-column: span 4; }
.col-5  { grid-column: span 5; }
.col-6  { grid-column: span 6; }
.col-7  { grid-column: span 7; }
.col-8  { grid-column: span 8; }
.col-12 { grid-column: span 12; }

/* Premium card surface (replaces older .card cosmetic) */
.surface {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-sm);
  overflow: hidden;
  transition: box-shadow var(--motion-hover) var(--motion-ease),
              transform var(--motion-hover) var(--motion-ease);
}
.surface:hover { box-shadow: var(--shadow-md); }

.surface--panel { border-radius: var(--radius-panel); }

.surface__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-3);
  padding: var(--space-5) var(--space-5) var(--space-3);
}
.surface__title {
  font-size: var(--type-panel-size);
  line-height: calc(var(--type-panel-lh) / var(--type-panel-size));
  font-weight: var(--type-panel-weight);
  color: var(--text-primary);
}
.surface__body {
  padding: 0 var(--space-5) var(--space-5);
}
.surface__body--flush { padding: 0; }

/* ---------- RIGHT RAIL ---------- */
.right-rail {
  grid-area: rightrail;
  position: sticky;
  top: 0;
  align-self: start;
  height: 100vh;
  background: var(--bg-surface);
  border-left: 1px solid var(--border-subtle);
  overflow-y: auto;
  overflow-x: hidden;
  z-index: var(--z-rail);
  display: flex;
  flex-direction: column;
}
.right-rail__header {
  display: flex; align-items: center; justify-content: space-between;
  padding: var(--space-4) var(--space-5);
  height: var(--shell-topnav-h);
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}
.right-rail__tabs {
  display: flex; gap: var(--space-1);
  padding: var(--space-3) var(--space-4) 0;
}
.right-rail__tab {
  flex: 1;
  padding: var(--space-2) var(--space-3);
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-tertiary);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--motion-hover) var(--motion-ease);
}
.right-rail__tab:hover { color: var(--text-secondary); }
.right-rail__tab[aria-selected="true"] {
  color: var(--brand-primary);
  border-bottom-color: var(--brand-primary);
}
.right-rail__body {
  flex: 1;
  padding: var(--space-4);
  overflow-y: auto;
}

/* Notification stack item */
.notif {
  display: flex;
  gap: var(--space-3);
  padding: var(--space-3);
  border-radius: var(--radius-control);
  margin-bottom: var(--space-2);
  border: 1px solid var(--border-subtle);
  background: var(--bg-surface);
  transition: all var(--motion-hover) var(--motion-ease);
}
.notif:hover { box-shadow: var(--shadow-sm); }
.notif__icon {
  width: 32px; height: 32px;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: var(--radius-control);
  flex-shrink: 0;
}
.notif--info    .notif__icon { background: var(--state-info-soft);    color: var(--state-info); }
.notif--success .notif__icon { background: var(--state-success-soft); color: var(--state-success); }
.notif--warning .notif__icon { background: var(--state-warning-soft); color: var(--state-warning); }
.notif--critical .notif__icon { background: var(--state-danger-soft); color: var(--state-danger); }
.notif--critical { border-color: rgba(200, 30, 30, 0.20); }
.notif__title { font-size: 13px; font-weight: 600; color: var(--text-primary); margin-bottom: 2px; }
.notif__body  { font-size: 12px; color: var(--text-secondary); line-height: 1.5; }
.notif__meta  { font-size: 11px; color: var(--text-tertiary); margin-top: var(--space-1); display: flex; gap: var(--space-2); }

/* ---------- RESPONSIVE ---------- */
@media (max-width: 1599px) {
  /* On smaller screens, take the right rail OUT of the grid and show it as a
     floating drawer on top of the content when the user opens it. */
  .app-shell {
    grid-template-columns: var(--shell-rail-w) 1fr;
    grid-template-areas: "rail topnav" "rail main";
  }
  .app-shell[data-rail-collapsed="true"] {
    grid-template-columns: var(--shell-rail-w-collapsed) 1fr;
  }
  .right-rail {
    position: fixed;
    top: 0;
    right: 0;
    height: 100vh;
    width: var(--shell-rightrail-w);
    max-width: 90vw;
    z-index: var(--z-drawer);
    box-shadow: var(--shadow-lg);
    transform: translateX(0);
    transition: transform var(--motion-drawer) var(--motion-spring);
  }
  /* Hidden by default on small screens — slides out to the right */
  .app-shell[data-rightrail-hidden="true"] .right-rail { transform: translateX(100%); }
  /* Backdrop only when open */
  .app-shell:not([data-rightrail-hidden="true"]) .right-rail::before {
    content: '';
    position: fixed;
    top: 0; left: 0; right: var(--shell-rightrail-w); bottom: 0;
    background: var(--bg-overlay);
    z-index: -1;
    animation: backdropIn 200ms ease;
  }
  @keyframes backdropIn { from { opacity: 0; } to { opacity: 1; } }
}

@media (max-width: 1279px) {
  .top-nav__center { max-width: 360px; }
}

@media (max-width: 839px) {
  .app-shell {
    grid-template-columns: var(--shell-rail-w-collapsed) 1fr;
  }
  .left-rail__brand-text,
  .rail-item__text,
  .rail-item__badge,
  .rail-section-title,
  .left-rail__toggle span { display: none; }
  .left-rail__brand, .rail-item, .left-rail__toggle { justify-content: center; }
  .top-nav { padding: 0 var(--space-3); }
  .top-nav__center { display: none; }
  .page-content { padding: var(--space-4); gap: var(--space-4); }
  .col-3, .col-4, .col-5, .col-6, .col-7, .col-8 { grid-column: span 12; }
}

@media (max-width: 599px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: var(--shell-topnav-h) 1fr;
    grid-template-areas: "topnav" "main";
  }
  .left-rail {
    position: fixed;
    left: 0; top: 0;
    width: var(--shell-rail-w);
    transform: translateX(-100%);
    transition: transform var(--motion-drawer) var(--motion-spring);
    box-shadow: var(--shadow-lg);
  }
  .left-rail__brand-text,
  .rail-item__text,
  .rail-item__badge,
  .rail-section-title { display: revert; }
  .left-rail__brand, .rail-item, .left-rail__toggle { justify-content: flex-start; }
  .app-shell[data-rail-mobile-open="true"] .left-rail { transform: translateX(0); }
  .app-shell[data-rail-mobile-open="true"]::before {
    content: '';
    position: fixed; inset: 0;
    background: var(--bg-overlay);
    z-index: calc(var(--z-rail) - 1);
  }
  .top-nav { padding: 0 var(--space-3); }
}

/* Hide legacy sidebar/header from layout.css when shell is active */
.app-shell .sidebar,
.app-shell .header {
  display: none !important;
}

/* Page header (inside .page-content) */
.page-header {
  grid-column: span 12;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-4);
  flex-wrap: wrap;
  padding-bottom: var(--space-2);
}
.page-header__title { margin: 0; }
.page-header__subtitle {
  font-size: var(--type-page-subtitle-size);
  line-height: calc(var(--type-page-subtitle-lh) / var(--type-page-subtitle-size));
  color: var(--text-secondary);
  margin-top: var(--space-1);
}
