/* ============================================================================
   Layout — app shell, sidebar, top bar, view containers, mobile bottom-tab
   ============================================================================ */

.app {
  min-height: 100vh;
  /* dvh tracks the visible viewport, so the page doesn't sit "below the fold"
     when iOS Safari's chrome retracts. Browsers without dvh keep the 100vh
     fallback above. */
  min-height: 100dvh;
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  grid-template-rows: auto 1fr;
  grid-template-areas:
    "sidebar topbar"
    "sidebar main";
  background: var(--bg-primary);
}

/* ---------- Sidebar (desktop) ---------- */
.sidebar {
  grid-area: sidebar;
  background: var(--bg-primary);
  border-right: var(--border-width) solid var(--border);
  padding: var(--space-5) var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
  position: sticky;
  top: 0;
  height: 100vh;
  height: 100dvh;
}

.brand {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: var(--space-1) var(--space-2) var(--space-3);
}
/* Combined symbol + wordmark stacked vertically (viewBox 200 × 137.07).
   White-fill SVG → invert under data-theme=light. */
.brand__logo {
  width: 120px;
  height: auto;
  display: block;
}
html[data-theme="light"] .brand__logo { filter: invert(1); }

.nav { display: flex; flex-direction: column; gap: 2px; }
.nav__item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-3);
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  font-size: var(--fs-body);
  transition: background-color 140ms var(--ease-out), color 140ms var(--ease-out);
}
.nav__item:hover { background: var(--bg-tertiary); color: var(--text-primary); }
.nav__item.is-active { background: var(--bg-tertiary); color: var(--text-primary); font-weight: var(--fw-medium); }
/* Accent-colour the icon on the active item so the current view is obvious at a glance */
.nav__item.is-active .icon { color: var(--accent); }
.nav__item .icon { width: 18px; height: 18px; flex-shrink: 0; }
.nav__shortcut {
  margin-left: auto;
  font-size: 11px;
  color: var(--text-tertiary);
  font-feature-settings: "tnum";
}

.sidebar__footer {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding-top: var(--space-4);
  border-top: var(--border-width) solid var(--border);
}

/* ---------- Top bar ---------- */
.topbar {
  grid-area: topbar;
  height: 56px;
  border-bottom: var(--border-width) solid var(--border);
  background: var(--bg-primary);
  padding: 0 var(--page-margin-desktop);
  display: flex;
  align-items: center;
  gap: var(--space-3);
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
}
.topbar__search {
  flex: 1;
  max-width: 480px;
  display: flex;
  align-items: center;
  gap: var(--space-2);
  background: var(--bg-secondary);
  border: var(--border-width) solid var(--border);
  border-radius: var(--radius-md);
  padding: 8px var(--space-3);
  color: var(--text-tertiary);
  cursor: pointer;
  transition: border-color 140ms var(--ease-out);
}
.topbar__search:hover { border-color: var(--border-strong); }
.topbar__search .icon { width: 16px; height: 16px; }
.topbar__search-text { flex: 1; font-size: var(--fs-body); }
.topbar__search-kbd {
  font-size: 11px;
  color: var(--text-tertiary);
  border: var(--border-width) solid var(--border);
  border-radius: var(--radius-sm);
  padding: 2px 6px;
  font-family: var(--font-sans);
}

.topbar__spacer { flex: 1; }
.topbar__actions { display: flex; gap: var(--space-2); align-items: center; }

/* ---------- Main view ---------- */
.main {
  grid-area: main;
  padding: var(--space-6) var(--page-margin-desktop) var(--space-7);
  min-width: 0;
}

.view {
  display: flex;
  flex-direction: column;
  gap: var(--section-gap);
  max-width: 1280px;
  margin: 0 auto;
}

.view__header {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--space-4);
  flex-wrap: wrap;
}
.view__title {
  font-family: var(--font-display);
  font-size: var(--fs-h1);
  line-height: var(--lh-h1);
  font-weight: var(--fw-display-strong);
  letter-spacing: -0.015em;
}
.view__subtitle {
  color: var(--text-secondary);
  margin-top: var(--space-1);
}

.section { display: flex; flex-direction: column; gap: var(--space-4); }
.section__head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: var(--space-3);
}
.section__title {
  font-size: var(--fs-h2);
  line-height: var(--lh-h2);
  font-weight: var(--fw-strong);
  letter-spacing: -0.01em;
}

/* Stat grid */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--card-gap);
}
@media (max-width: 1024px) { .stat-grid { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 560px)  { .stat-grid { grid-template-columns: 1fr; } }

/* Two-up split */
.split { display: grid; grid-template-columns: 2fr 1fr; gap: var(--card-gap); }
@media (max-width: 1024px) { .split { grid-template-columns: 1fr; } }

/* ---------- Mobile: bottom tab bar replaces sidebar ---------- */
.bottom-tab { display: none; }

@media (max-width: 768px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-areas:
      "topbar"
      "main";
    /* Reserve space for the bottom-tab AND the iOS home indicator so content
       isn't hidden behind either. env() falls back to 0 on browsers/devices
       without a safe area. */
    padding-bottom: calc(var(--bottom-tab-height) + env(safe-area-inset-bottom, 0px));
  }
  .sidebar { display: none; }
  .topbar { padding: 0 var(--page-margin-mobile); }
  .main { padding: var(--space-5) var(--page-margin-mobile) var(--space-7); }

  .bottom-tab {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    position: fixed;
    bottom: 0; left: 0; right: 0;
    /* Extend the bar's height by the safe-area inset and pad it down so the
       icons sit above the home indicator on iPhone. */
    height: calc(var(--bottom-tab-height) + env(safe-area-inset-bottom, 0px));
    padding-bottom: env(safe-area-inset-bottom, 0px);
    background: var(--bg-secondary);
    border-top: var(--border-width) solid var(--border);
    z-index: var(--z-sticky);
  }
  .bottom-tab__item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    color: var(--text-tertiary);
    font-size: 11px;
    min-height: 44px;
  }
  .bottom-tab__item.is-active { color: var(--accent); }
  .bottom-tab__item .icon { width: 20px; height: 20px; }
  /* 5 tabs at narrow phone widths — shrink the label slightly so it fits */
  @media (max-width: 380px) {
    .bottom-tab__item { font-size: 10px; }
  }

  /* Compact the topbar so the search button + theme + New all fit on a phone */
  .topbar__search-text { display: none; }
  .topbar__search-kbd { display: none; }
  .topbar__search {
    flex: 0 0 auto;
    width: 40px;
    padding: 8px;
    justify-content: center;
  }
  /* "+ New" becomes icon-only — strip the text span, keep the icon + 44px tap target */
  .topbar__actions .btn--primary span { display: none; }
  .topbar__actions .btn--primary {
    width: 44px;
    padding: 0;
    justify-content: center;
  }
  .topbar__actions .btn--primary .icon { margin: 0; }

  /* View header: smaller H1 + allow subtitle to wrap below comfortably */
  .view__title {
    font-size: 26px;
    line-height: 32px;
    /* Brand mark sits to the left of the title once the sidebar is hidden,
       so the logo stays visible on tablet and phone. */
    display: flex;
    align-items: center;
    gap: var(--space-3);
  }
  .view__title::before {
    content: "";
    display: block;
    width: 28px;
    height: 28px;
    flex-shrink: 0;
    background: url("../Images/Entropy Symbol.svg") center/contain no-repeat;
  }
  html[data-theme="light"] .view__title::before { filter: invert(1); }
  .view__subtitle { font-size: var(--fs-small); }
  .view__header { gap: var(--space-2); }
}

/* ---------- Auth (login) screen ---------- */
.auth-screen {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: var(--space-5);
  background: var(--bg-primary);
}
.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--bg-secondary);
  border: var(--border-width) solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-7);
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
  box-shadow: var(--shadow-panel);
}
.auth-card__brand {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  align-items: center;
  text-align: center;
}
/* Combined symbol + wordmark logo (200 × 137.07 viewBox, ~1.46:1). */
.auth-card__logo {
  width: 160px;
  height: 110px;
  display: block;
}
html[data-theme="light"] .auth-card__logo { filter: invert(1); }
/* Retained for the fatal "Configuration needed" screen in app.js — the
   normal sign-in path uses the logo image instead of a text title. */
.auth-card__title {
  font-family: var(--font-display);
  font-size: 28px;
  line-height: 32px;
  font-weight: var(--fw-display-strong);
  letter-spacing: -0.015em;
}
.auth-card__subtitle { color: var(--text-secondary); font-size: var(--fs-body); }

/* ---------- Loading state for app shell ---------- */
.app-loading {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  background: var(--bg-primary);
  color: var(--text-tertiary);
  font-size: var(--fs-small);
}
