/* ==========================================================================
   Layout — the app shell: sidebar, topbar, main view, mobile drawer + tabbar,
   plus the generic grid/stack primitives pages compose with.
   ========================================================================== */

.app-shell {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
}

/* ---------- Sidebar ------------------------------------------------------- */
.sidebar {
  grid-column: 1;
  display: flex;
  flex-direction: column;
  background: var(--surface-2);
  border-right: 1px solid var(--border-subtle);
  position: sticky;
  top: 0;
  height: 100vh;
  height: 100dvh;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: 0 var(--sp-5);
  height: var(--topbar-h);
  flex: 0 0 auto;
  border-bottom: 1px solid var(--border-subtle);
}
.sidebar-avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid var(--border-strong);
  box-shadow: 0 0 0 1px var(--accent-ring), 0 4px 16px rgba(255, 90, 31, 0.10);
  flex: 0 0 auto;
}
.brand-text { display: flex; flex-direction: column; line-height: 1.15; min-width: 0; }
.brand-name { font-size: var(--fs-md); font-weight: 700; letter-spacing: -0.015em; color: var(--accent); }
.brand-sub {
  font-size: var(--fs-2xs);
  color: var(--text);
  text-transform: uppercase;
  letter-spacing: var(--tracking-caps);
  font-weight: 600;
}
.drawer-close { display: none; margin-left: auto; }

.sidebar-nav {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: var(--sp-4) var(--sp-3);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nav-section {
  font-size: var(--fs-2xs);
  text-transform: uppercase;
  letter-spacing: var(--tracking-caps);
  color: var(--text-4);
  font-weight: 600;
  padding: var(--sp-4) var(--sp-3) var(--sp-2);
}
.nav-section:first-child { padding-top: var(--sp-1); }

.nav-item {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  padding: 9px var(--sp-3);
  border-radius: var(--r-sm);
  color: var(--text-2);
  font-size: var(--fs-base);
  font-weight: 500;
  text-decoration: none;
  position: relative;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.nav-item:hover { background: var(--surface-3); color: var(--text); text-decoration: none; }
.nav-item svg { width: 17px; height: 17px; flex: 0 0 auto; opacity: 0.75; }
.nav-item.is-active { background: var(--surface-4); color: var(--text); }
.nav-item.is-active svg { opacity: 1; color: var(--accent); }
.nav-item.is-active::before {
  content: "";
  position: absolute;
  left: 0; top: 50%;
  transform: translateY(-50%);
  width: 3px; height: 16px;
  background: var(--accent);
  border-radius: 0 2px 2px 0;
}
.nav-count {
  margin-left: auto;
  font-family: var(--font-mono);
  font-size: var(--fs-2xs);
  color: var(--text-3);
  background: var(--surface-1);
  border: 1px solid var(--border-subtle);
  border-radius: var(--r-pill);
  padding: 1px 7px;
  font-variant-numeric: tabular-nums;
}

.sidebar-foot {
  flex: 0 0 auto;
  padding: var(--sp-3);
  border-top: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  gap: var(--sp-3);
}
.sidebar-health { display: flex; flex-direction: column; gap: 5px; }
.health-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-2);
  font-size: var(--fs-xs);
  color: var(--text-3);
}
.health-row .v { font-family: var(--font-mono); color: var(--text-2); }

/* ---------- Main column -------------------------------------------------- */
.main-col {
  grid-column: 2;
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: var(--z-topbar);
  height: var(--topbar-h);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: var(--sp-4);
  padding: 0 var(--sp-6);
  background: color-mix(in srgb, var(--surface-0) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border-subtle);
}
.topbar-burger { display: grid; }
.topbar-left {
  display: flex;
  align-items: center;
  gap: var(--sp-3);
  flex: 0 0 auto;
}
.topbar-brand {
  display: flex;
  flex-direction: column;
  justify-content: center;
  line-height: 1.15;
  min-width: 0;
}
.topbar-brand .brand-name { font-size: var(--fs-md); white-space: nowrap; }
.topbar-heading { min-width: 0; flex: 1 1 auto; }
.topbar-heading h1 { font-size: var(--fs-xl); letter-spacing: -0.02em; }
.topbar-sub {
  font-size: var(--fs-sm);
  color: var(--text-3);
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.topbar-actions { display: flex; align-items: center; gap: var(--sp-3); flex: 0 0 auto; }
.page-actions { display: flex; align-items: center; gap: var(--sp-2); }

/* ---------- Profile / operator identity ---------------------------------- */
.profile { position: relative; flex: 0 0 auto; }
.profile-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  padding: 4px 10px 4px 5px;
  border-radius: var(--r-pill);
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-2);
  font: inherit;
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.profile-btn:hover { background: var(--surface-3); color: var(--text); border-color: var(--border-subtle); }
.profile-btn[aria-expanded="true"] { background: var(--surface-4); color: var(--text); border-color: var(--border); }
.profile-avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  object-fit: cover;
  border: 1.5px solid var(--border-strong);
  box-shadow: 0 0 0 1px var(--accent-ring), 0 4px 16px rgba(255, 90, 31, 0.16);
  flex: 0 0 auto;
  background: var(--surface-1);
}
.profile-name {
  font-size: var(--fs-sm);
  font-weight: 600;
  letter-spacing: var(--tracking-wide);
  white-space: nowrap;
}
.profile-caret {
  color: var(--text-3);
  transition: transform var(--dur) var(--ease);
}
.profile-btn[aria-expanded="true"] .profile-caret { transform: rotate(180deg); }

.profile-menu {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  min-width: 196px;
  background: var(--surface-4);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: var(--sp-2);
  box-shadow: var(--shadow-lg);
  z-index: var(--z-modal);
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.profile-menu[hidden] { display: none; }
.profile-head {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-2);
  border-bottom: 1px solid var(--border-subtle);
  margin-bottom: var(--sp-1);
}
.profile-head-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  object-fit: cover;
  border: 1px solid var(--border-strong);
  box-shadow: 0 0 0 1px var(--accent-ring);
  flex: 0 0 auto;
  background: var(--surface-1);
}
.profile-head-name { font-size: var(--fs-sm); font-weight: 600; letter-spacing: var(--tracking-wide); }
.profile-head-sub {
  font-size: var(--fs-2xs);
  color: var(--text-4);
  text-transform: uppercase;
  letter-spacing: var(--tracking-caps);
  font-weight: 600;
}
.profile-item {
  display: block;
  width: 100%;
  text-align: left;
  padding: 8px var(--sp-2);
  border-radius: var(--r-sm);
  color: var(--text-2);
  font-size: var(--fs-sm);
  font: inherit;
  background: transparent;
  border: none;
  cursor: pointer;
  text-decoration: none;
  transition: background var(--dur-fast) var(--ease), color var(--dur-fast) var(--ease);
}
.profile-item:hover { background: var(--surface-3); color: var(--text); text-decoration: none; }
.profile-item-danger:hover { background: var(--danger-soft); color: var(--danger); }

/* Desktop: hamburger collapses the sidebar column so the terminal can run
   full-width. Only applies on wide screens; mobile keeps the drawer. */
@media (min-width: 861px) {
  .app-shell { transition: grid-template-columns var(--dur) var(--ease); }
  .app-shell.nav-collapsed { grid-template-columns: 0 minmax(0, 1fr); }
  .app-shell.nav-collapsed .sidebar { display: none; }
}

.view {
  flex: 1 1 auto;
  padding: var(--sp-6);
  max-width: var(--content-max);
  width: 100%;
  outline: none;
}

/* ---------- Mobile drawer + tab bar -------------------------------------- */
.nav-scrim {
  position: fixed;
  inset: 0;
  background: rgba(4, 5, 7, 0.68);
  backdrop-filter: blur(2px);
  z-index: calc(var(--z-drawer) - 1);
}
.tabbar { display: none; }

@media (max-width: 860px) {
  .app-shell { grid-template-columns: minmax(0, 1fr); }

  .sidebar {
    position: fixed;
    top: 0; left: 0;
    z-index: var(--z-drawer);
    width: 268px;
    max-width: 84vw;
    transform: translateX(-100%);
    transition: transform var(--dur) var(--ease);
    border-right: 1px solid var(--border);
    box-shadow: var(--shadow-lg);
  }
  .sidebar.is-open { transform: translateX(0); }
  .drawer-close { display: grid; }

  .main-col { grid-column: 1; }
  .topbar-burger { display: grid; }
  /* ---- Mobile topbar: two-row layout ------------------------------------ */
  /* Row 1: burger + branding …… profile avatar.
     Row 2: page title + per-page actions/status badge. */
  .topbar {
    padding: var(--sp-2) var(--sp-4);
    gap: var(--sp-3);
    row-gap: 0;
    height: auto;
    min-height: var(--topbar-h);
    flex-wrap: wrap;
    justify-content: space-between;
  }
  .topbar-left {
    order: 1;
    gap: var(--sp-2);
  }
  .topbar-brand {
    flex-direction: row;
    align-items: baseline;
    white-space: nowrap;
    gap: 5px;
    min-width: 0;
  }
  .topbar-brand .brand-name { font-size: var(--fs-sm); }
  .topbar-brand .brand-sub {
    font-size: var(--fs-2xs);
    /* Override the sidebar brand-sub's letter-spacing so the
       single-line label stays compact on a 320px screen. */
    letter-spacing: 0.04em;
  }
  .profile { order: 1; }

  /* Row 2 — full-width page title row. */
  .topbar-heading {
    order: 2;
    flex: 1 1 100%;
    min-width: 0;
    border-top: 1px solid var(--border-subtle);
    padding-top: var(--sp-2);
    margin-top: var(--sp-1);
  }
  .topbar-heading h1 {
    font-size: var(--fs-lg);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .topbar-sub { display: none; }
  .topbar-actions { order: 3; flex: 1 1 100%; }
  .page-actions { flex-wrap: wrap; row-gap: var(--sp-2); }

  /* Slightly smaller profile elements so Row 1 stays uncluttered
     on 320-380px screens. */
  .profile-btn { padding: 3px 6px 3px 4px; gap: 5px; }
  .profile-name { font-size: var(--fs-xs); }
  .profile-avatar { width: 30px; height: 30px; }

  .view {
    padding: var(--sp-4);
    padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + var(--sp-6));
  }

  .tabbar {
    position: fixed;
    bottom: 0; left: 0; right: 0;
    z-index: var(--z-tabbar);
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: 1fr;
    height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
    padding-bottom: env(safe-area-inset-bottom);
    background: color-mix(in srgb, var(--surface-2) 94%, transparent);
    backdrop-filter: blur(16px);
    border-top: 1px solid var(--border);
  }
  .tab-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    color: var(--text-3);
    font-size: var(--fs-2xs);
    font-weight: 600;
    text-decoration: none;
    letter-spacing: 0.01em;
    -webkit-tap-highlight-color: transparent;
  }
  .tab-item svg { width: 20px; height: 20px; }
  .tab-item:hover { text-decoration: none; }
  .tab-item.is-active { color: var(--accent); }

  /* Unread bubble on a bottom-bar tab. Sits over the icon's corner, hugging
     the label on the same baseline — no extra row, no layout shift. */
  .tab-item { position: relative; }
  .tab-count {
    position: absolute;
    top: 3px;
    right: calc(50% - 18px);
    min-width: 15px;
    height: 15px;
    padding: 0 4px;
    border-radius: var(--r-pill);
    font-family: var(--font-mono);
    font-size: var(--fs-2xs);
    line-height: 15px;
    text-align: center;
    font-variant-numeric: tabular-nums;
    background: var(--surface-3);
    color: var(--text-2);
    border: 1px solid var(--border-subtle);
  }
  .tab-count-alert { background: var(--accent); color: var(--text); border: none; }
}

/* Desktop sidebar: Alerts unread uses the accent so it reads as a signal,
   not just another counter. */
.nav-count.is-alert { background: var(--accent); color: var(--text); border: none; }

/* ---------- Page composition primitives ---------------------------------- */
.stack { display: flex; flex-direction: column; gap: var(--sp-4); }
.stack-sm { display: flex; flex-direction: column; gap: var(--sp-2); }
.stack-lg { display: flex; flex-direction: column; gap: var(--sp-6); }
.row { display: flex; align-items: center; gap: var(--sp-3); }
.row-wrap { display: flex; align-items: center; gap: var(--sp-3); flex-wrap: wrap; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: var(--sp-3); }
.spacer { flex: 1 1 auto; }

.grid { display: grid; gap: var(--sp-4); }
.grid-stats { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.grid-cards { grid-template-columns: repeat(auto-fill, minmax(268px, 1fr)); }
/* Wallet cards: min() keeps narrow phones at one full-width column instead of
   forcing a 320px+ track that would overflow the viewport. */
.grid-wallets { grid-template-columns: repeat(auto-fill, minmax(min(100%, 340px), 1fr)); }

/* Dashboard: main column + rail, collapsing to one column on tablet. */
.grid-dash { grid-template-columns: minmax(0, 1.65fr) minmax(0, 1fr); align-items: start; }
@media (max-width: 1100px) {
  .grid-dash { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 520px) {
  .grid-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--sp-3); }
  .grid-cards { grid-template-columns: minmax(0, 1fr); }
}

.section-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--sp-3);
  margin-bottom: var(--sp-3);
}
.section-head h2 { font-size: var(--fs-md); letter-spacing: -0.01em; }
.section-head .link { font-size: var(--fs-sm); color: var(--text-3); font-weight: 500; }
.section-head .link:hover { color: var(--cyan); text-decoration: none; }
