/* ==========================================================================
   App shell — topbar, sidebar, layout, and shared primitives.
   Every colour here resolves through a semantic alias from tokens.css.
   Surfaces are border-led, never shadow-led (TGF convention).
   ========================================================================== */

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  font-family: var(--font-primary);
  font-weight: var(--fw-regular);
  font-size: var(--ds-body);
  line-height: var(--body-line-height);
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

h1,
h2,
h3,
h4,
h5,
h6 {
  font-weight: var(--fw-medium);
  margin: 0;
}

a {
  color: var(--accent);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}

button {
  font-family: inherit;
  font-size: inherit;
  cursor: pointer;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius);
}

/* Wide content must scroll inside its own container — the body never scrolls
   horizontally. */
.scroll-x {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* ---------- Topbar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--gap-button);
  height: var(--ds-topbar-h);
  padding: 0 var(--ds-pad-section);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  border: 0;
  background: none;
  padding: 4px;
  margin-left: -4px;
  border-radius: var(--radius);
  color: var(--text);
  min-width: 0; /* lets the name truncate instead of pushing the theme/refresh buttons off-screen */
  flex: 1 1 auto;
}
.brand:hover {
  color: var(--accent);
}

.brand__logo {
  width: 26px;
  height: 26px;
  flex: none;
}

.brand__name {
  font-size: 1.15rem;
  font-weight: var(--fw-medium);
  letter-spacing: var(--heading-letter-spacing-tight);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: var(--gap-sm);
  min-width: 0;
  flex: none; /* buttons keep their size; .brand truncates first */
}

.meta-info {
  font-size: var(--ds-small);
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 0.4em 0.9em;
  font-size: var(--ds-small);
  font-weight: var(--fw-regular);
  color: var(--text);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  transition: border-color var(--transition), color var(--transition),
    background-color var(--transition);
  white-space: nowrap;
}
.btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}
.btn[aria-pressed='true'],
.btn--active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--white);
  font-weight: var(--fw-medium);
}

.btn--icon {
  padding: 0.4em 0.55em;
}

/* Hamburger — only shown once the sidebar collapses off-canvas. */
.nav-toggle {
  display: none;
}

/* ---------- Layout ---------- */

.layout {
  display: grid;
  grid-template-columns: var(--ds-sidebar-w) minmax(0, 1fr);
  align-items: start;
  max-width: var(--ds-max-w);
  margin: 0 auto;
}

.sidebar {
  position: sticky;
  top: var(--ds-topbar-h);
  height: calc(100vh - var(--ds-topbar-h));
  overflow-y: auto;
  padding: var(--ds-pad-section) 0.75rem var(--gap-md);
  border-right: 1px solid var(--border);
  background: var(--bg);
}

.main {
  min-width: 0;
  padding: var(--ds-pad-section);
  display: flow-root;
}

/* ---------- Sidebar navigation ---------- */

.nav-group {
  margin-bottom: var(--gap-md);
}

.nav-group__label {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 0.6rem;
  margin-bottom: var(--gap-sm);
  font-size: var(--eyebrow-size);
  font-weight: var(--fw-medium);
  letter-spacing: var(--eyebrow-letter-spacing);
  text-transform: uppercase;
  color: var(--text-muted);
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 0.5rem 0.6rem;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  color: var(--text);
  font-size: var(--ds-body);
  text-align: left;
  transition: background-color var(--transition), color var(--transition);
}
.nav-item:hover {
  background: var(--bg-sunken);
  text-decoration: none;
}
.nav-item[aria-current='page'] {
  background: var(--accent-bg);
  color: var(--accent-text-on-bg);
  font-weight: var(--fw-medium);
}

.nav-item__icon {
  width: 17px;
  height: 17px;
  flex: none;
  opacity: 0.75;
}
.nav-item[aria-current='page'] .nav-item__icon {
  opacity: 1;
}

.nav-item__label {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.nav-item__count {
  font-size: var(--ds-micro);
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  flex: none;
}
.nav-item[aria-current='page'] .nav-item__count {
  color: inherit;
}

/* Taxonomy leaves sit one level in from their tier heading. */
.nav-item--leaf {
  padding-left: 1.5rem;
  font-size: var(--ds-small);
  color: var(--text-secondary);
}

/* ---------- Shared primitives ---------- */

.section {
  margin-bottom: var(--gap-md);
}

.section__head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: var(--gap-button);
  flex-wrap: wrap;
  margin-bottom: var(--gap-button);
}

.section__title {
  font-size: var(--h4-size);
  font-weight: var(--fw-medium);
  letter-spacing: var(--heading-letter-spacing-tight);
}

/* ---------- Page header (TGF-style eyebrow + headline) ---------- */

.page-header {
  padding-bottom: var(--gap-md);
  margin-bottom: var(--gap-md);
  border-bottom: 1px solid var(--border);
}

.page-header__title {
  font-size: var(--h3-size);
  font-weight: var(--fw-medium);
  letter-spacing: var(--heading-letter-spacing-tight);
  color: var(--text);
  margin: 0.4rem 0 0.6rem;
}

.page-header__subhead {
  font-size: var(--body-size);
  color: var(--text-secondary);
  line-height: var(--body-line-height);
  max-width: 640px;
}

.hint {
  font-size: var(--ds-small);
  font-weight: var(--fw-regular);
  color: var(--text-muted);
}

.card {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: var(--ds-pad-card);
}

.card--flush {
  padding: 0;
  overflow: hidden;
}

.eyebrow {
  display: block;
  margin-bottom: 4px;
  font-size: var(--eyebrow-size);
  font-weight: var(--fw-medium);
  letter-spacing: var(--eyebrow-letter-spacing);
  text-transform: uppercase;
  color: var(--text-secondary);
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.2em 0.6em;
  font-size: var(--ds-micro);
  font-weight: var(--fw-medium);
  border-radius: var(--radius);
  background: var(--chip-bg);
  color: var(--text-secondary);
  white-space: nowrap;
}
.badge--accent {
  background: var(--accent-bg);
  color: var(--accent-text-on-bg);
}
.badge--up {
  background: rgba(var(--up-rgb), 0.14);
  color: var(--up);
}
.badge--down {
  background: rgba(var(--down-rgb), 0.14);
  color: var(--down);
}
.badge--warn {
  background: rgba(var(--yellow-rgb), 0.28);
  color: var(--yellow-text);
}

/* Directional numbers. Tabular figures stop columns jittering. */
.pct {
  font-variant-numeric: tabular-nums;
}
.pct--up {
  color: var(--up);
}
.pct--down {
  color: var(--down);
}
.pct--flat {
  color: var(--text-muted);
}

.num {
  font-variant-numeric: tabular-nums;
}

.inset {
  background: var(--bg-inset);
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
}

.empty {
  padding: 1.25rem;
  font-size: var(--ds-small);
  color: var(--text-muted);
  text-align: center;
}

.back-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-bottom: var(--gap-sm);
  font-size: var(--ds-small);
  color: var(--text-secondary);
  background: none;
  border: 0;
  padding: 0;
}
.back-link:hover {
  color: var(--accent);
}

/* ---------- Footer ---------- */

.footer {
  max-width: var(--ds-max-w);
  margin: 0 auto;
  padding: var(--gap-md) var(--ds-pad-section);
  border-top: 1px solid var(--border);
  font-size: var(--ds-small);
  color: var(--text-muted);
  display: flex;
  gap: var(--gap-button);
  flex-wrap: wrap;
  justify-content: space-between;
}

/* ---------- Loading / error states ---------- */

.skeleton {
  border-radius: var(--radius);
  background: var(--bg-sunken);
  animation: pulse 1.4s ease-in-out infinite;
}
@keyframes pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.55;
  }
}
@media (prefers-reduced-motion: reduce) {
  .skeleton {
    animation: none;
  }
  * {
    transition-duration: 0ms !important;
  }
}

.error-banner {
  border: 1px solid var(--down);
  background: rgba(var(--down-rgb), 0.08);
  color: var(--text);
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
  font-size: var(--ds-small);
  margin-bottom: var(--gap-md);
}

/* ---------- Responsive ---------- */

@media (max-width: 1100px) {
  :root {
    --ds-sidebar-w: 210px;
  }
}

@media (max-width: 860px) {
  .nav-toggle {
    display: inline-flex;
  }

  .layout {
    grid-template-columns: minmax(0, 1fr);
  }

  /* Sidebar goes off-canvas and slides over the content. */
  .sidebar {
    position: fixed;
    top: var(--ds-topbar-h);
    left: 0;
    z-index: 15;
    width: min(82vw, 300px);
    border-right: 1px solid var(--border);
    transform: translateX(-100%);
    transition: transform var(--transition);
    box-shadow: var(--hover-shadow);
  }
  body[data-nav-open='true'] .sidebar {
    transform: translateX(0);
  }

  body[data-nav-open='true']::after {
    content: '';
    position: fixed;
    inset: var(--ds-topbar-h) 0 0 0;
    z-index: 14;
    background: var(--overlay-backdrop);
  }

  .meta-info {
    display: none;
  }
}

@media (max-width: 560px) {
  :root {
    --ds-pad-section: 0.875rem;
  }
}
