/* ============================================================================
   App Shell — sidebar + topbar + layout
   ============================================================================ */

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: var(--topbar-h) 1fr;
  grid-template-areas:
    "sidebar topbar"
    "sidebar main";
  min-height: 100vh;
  min-width: 0;
  width: 100%;
  transition: grid-template-columns var(--dur-base) var(--ease-out);
}
.app-shell[data-sidebar="collapsed"] {
  grid-template-columns: 0 1fr;
}
.app-shell[data-sidebar="collapsed"] .app-sidebar {
  transform: translateX(-100%);
  pointer-events: none;
  border-right-color: transparent;
}

.app-sidebar {
  grid-area: sidebar;
  background: var(--surface-1);
  border-right: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow: hidden;
  z-index: var(--z-sticky);
  transition: transform var(--dur-base) var(--ease-out),
              border-color var(--dur-base) var(--ease-out);
}

.sidebar-reveal-zone {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  width: 18px;
  z-index: calc(var(--z-sticky) + 3);
  cursor: e-resize;
}
.sidebar-reveal-zone::after {
  content: '';
  position: absolute;
  left: 4px;
  top: 50%;
  width: 4px;
  height: 72px;
  border-radius: 9999px;
  background: color-mix(in srgb, var(--brand-500) 42%, transparent);
  transform: translateY(-50%);
  opacity: 0.55;
}

.app-topbar {
  grid-area: topbar;
  background: var(--surface-1);
  color: var(--text-primary);
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  padding: 0 var(--space-5);
  gap: var(--space-3);
  position: sticky;
  top: 0;
  z-index: var(--z-sticky);
  height: var(--topbar-h);
  min-width: 0;
}
.app-topbar,
.app-topbar .breadcrumb,
.app-topbar .breadcrumb-current {
  color: var(--text-primary);
}

.app-main {
  grid-area: main;
  min-width: 0;
  padding: var(--space-6);
  max-width: 100%;
}

/* ─── Sidebar internals ───────────────────────────────────────────────── */
.sb-brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  height: var(--topbar-h);
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}
.sb-brand-logo {
  width: 28px;
  height: 28px;
  border-radius: var(--radius-md);
  background: color-mix(in srgb, var(--surface-1) 86%, var(--brand-500));
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
  overflow: hidden;
}
.sb-brand-william {
  border-radius: 9999px;
  border: 1px solid color-mix(in srgb, #fbbf24 55%, var(--border-subtle));
}
.sb-brand-william img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
}
.sb-brand-name {
  font-family: var(--font-display);
  font-weight: 900;
  font-size: 0.9375rem;
  letter-spacing: 0.08em;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
}
.sb-brand .sb-icon-btn {
  width: 2rem;
  height: 2rem;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  background: var(--surface-2);
  color: var(--text-primary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}
.sb-brand .sb-icon-btn:hover {
  background: var(--surface-3);
  border-color: var(--border-default);
}

.sb-close-btn {
  width: 2rem;
  height: 2rem;
  margin-left: auto;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  background: var(--surface-2);
  color: var(--text-primary);
  display: none;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}
.sb-close-btn:hover {
  background: var(--surface-3);
  border-color: var(--border-default);
}

.sb-nav {
  flex: 1;
  padding: var(--space-3);
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}
.sb-section-label {
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-tertiary);
  padding: var(--space-3) var(--space-3) var(--space-1);
  white-space: nowrap;
  overflow: hidden;
}
.sb-link {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: 0.5rem 0.75rem;
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  font-size: 0.875rem;
  font-weight: 500;
  transition: all var(--dur-fast) var(--ease-out);
  white-space: nowrap;
  overflow: hidden;
  position: relative;
}
.sb-link:hover {
  background: var(--surface-2);
  color: var(--text-primary);
}
.sb-link[aria-current="page"] {
  background: var(--brand-50);
  color: var(--brand-700);
  font-weight: 600;
}
[data-theme="dark"] .sb-link[aria-current="page"] {
  background: rgb(99 102 241 / 0.15);
  color: var(--brand-300);
}
.sb-link-icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.9375rem;
}
.sb-link-label { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.sb-link-badge {
  background: var(--danger-500);
  color: #fff;
  font-size: 0.6875rem;
  font-weight: 600;
  padding: 0 0.375rem;
  border-radius: var(--radius-full);
  min-width: 1.125rem;
  height: 1.125rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.sb-footer {
  padding: var(--space-3);
  border-top: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

/* Collapsed state */
.app-shell[data-sidebar="collapsed"] .sb-brand-name,
.app-shell[data-sidebar="collapsed"] .sb-section-label,
.app-shell[data-sidebar="collapsed"] .sb-link-label,
.app-shell[data-sidebar="collapsed"] .sb-link-badge,
.app-shell[data-sidebar="collapsed"] .sb-user-meta {
  display: none;
}
.app-shell[data-sidebar="collapsed"] .sb-link,
.app-shell[data-sidebar="collapsed"] .sb-brand,
.app-shell[data-sidebar="collapsed"] .sb-user {
  justify-content: center;
}

/* User card */
.sb-user {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-2);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: background var(--dur-fast);
}
.sb-user:hover { background: var(--surface-2); }
.sb-user-meta { flex: 1; min-width: 0; }
.sb-user-name { font-size: 0.8125rem; font-weight: 600; color: var(--text-primary); }
.sb-user-sub { font-size: 0.6875rem; color: var(--text-tertiary); display: flex; align-items: center; gap: 0.25rem; }

/* ─── Topbar ────────────────────────────────────────────────────────── */
.tb-search {
  flex: 1;
  max-width: 480px;
  position: relative;
  background: transparent;
  border: none;
  padding: 0;
  cursor: pointer;
  margin-left: auto;
}
.tb-search-input {
  width: 100%;
  padding: 0.375rem 0.75rem 0.375rem 2.25rem;
  background: var(--surface-2);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  font-size: 0.8125rem;
  color: var(--text-secondary);
  text-align: left;
  pointer-events: none;
  transition: border-color var(--dur-fast);
}
.tb-search-input:focus {
  background: var(--surface-1);
  border-color: var(--border-default);
  outline: none;
}
.tb-search-icon {
  position: absolute;
  left: 0.75rem;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-tertiary);
  pointer-events: none;
  font-size: 0.75rem;
}
.tb-search-kbd {
  position: absolute;
  right: 0.5rem;
  top: 50%;
  transform: translateY(-50%);
  pointer-events: none;
}

.tb-actions {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  margin-left: auto;
  min-width: 0;
}

.hud-ring-label span {
  display: block;
  max-width: 28px;
  overflow: hidden;
  text-align: center;
}

.tb-icon-btn {
  width: 2.25rem;
  height: 2.25rem;
  border-radius: var(--radius-md);
  color: var(--text-primary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: all var(--dur-fast);
  cursor: pointer;
  background: var(--surface-2);
  border: 1px solid var(--border-subtle);
  font-size: 0.9375rem;
}
.tb-icon-btn:hover {
  background: var(--surface-3);
  color: var(--brand-600);
  border-color: var(--brand-300);
}

.tb-collapse-btn {
  background: var(--surface-2);
  color: var(--text-primary);
  border-color: var(--border-default);
}
.tb-collapse-btn:hover {
  background: var(--brand-50);
  color: var(--brand-600);
  border-color: var(--brand-300);
}
/* Theme toggle: prominent pill that reads at a glance in both modes */
.tb-theme-toggle {
  background: linear-gradient(135deg, var(--brand-500), var(--accent-500));
  color: #fff;
  border-color: transparent;
  box-shadow: var(--shadow-sm);
}
.tb-theme-toggle:hover {
  background: linear-gradient(135deg, var(--brand-500), var(--accent-500));
  color: #fff;
  border-color: transparent;
  transform: rotate(-18deg);
  box-shadow: var(--shadow-md);
}
.tb-theme-toggle i { color: currentColor; transition: transform var(--dur-fast); }
[data-theme="dark"] .tb-theme-toggle {
  background: linear-gradient(135deg, #f59e0b, #fbbf24);
  color: #1f2937;
  border-color: transparent;
}
[data-theme="dark"] .tb-theme-toggle:hover {
  background: linear-gradient(135deg, #f59e0b, #fbbf24);
  color: #1f2937;
}

/* ─── William coach celebration ─────────────────────────────────────── */
.william-coach-layer {
  position: fixed;
  left: 50%;
  top: 50%;
  translate: -50% -50%;
  z-index: calc(var(--z-modal) + 4);
  pointer-events: none;
  width: min(820px, calc(100vw - 28px));
}
.william-coach-card {
  display: grid;
  grid-template-columns: 158px minmax(0, 1fr);
  align-items: end;
  gap: 0;
  pointer-events: auto;
}
.william-coach-portrait {
  width: 180px;
  height: 180px;
  border-radius: 9999px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(145deg, color-mix(in srgb, var(--surface-1) 88%, transparent), color-mix(in srgb, var(--warning-50, #fffbeb) 78%, var(--surface-1)));
  border: 2px solid color-mix(in srgb, #fbbf24 52%, var(--border-subtle));
  box-shadow: 0 20px 44px -24px rgb(15 23 42 / 0.45), inset 0 1px 0 rgb(255 255 255 / 0.5);
  overflow: hidden;
  position: relative;
  z-index: 2;
}
.william-coach-portrait img {
  width: 166px;
  height: 166px;
  object-fit: contain;
  display: block;
  transform: translateY(6px);
}
.william-coach-bubble {
  position: relative;
  min-width: 0;
  margin-left: -12px;
  padding: 22px 58px 22px 30px;
  border-radius: 8px;
  border: 2px solid color-mix(in srgb, #fbbf24 38%, var(--border-subtle));
  background: linear-gradient(180deg, color-mix(in srgb, #fff7ed 90%, var(--surface-1)), var(--surface-1));
  box-shadow: 0 24px 70px -34px rgb(15 23 42 / 0.54), inset 0 1px 0 rgb(255 255 255 / 0.68);
  color: var(--text-primary);
}
[data-theme="dark"] .william-coach-bubble {
  background: linear-gradient(180deg, color-mix(in srgb, var(--surface-1) 94%, #fbbf24 6%), var(--surface-1));
}
.william-coach-bubble::before {
  content: '';
  position: absolute;
  left: -12px;
  bottom: 38px;
  width: 22px;
  height: 22px;
  transform: rotate(45deg);
  background: inherit;
  border-left: 2px solid color-mix(in srgb, #fbbf24 38%, var(--border-subtle));
  border-bottom: 2px solid color-mix(in srgb, #fbbf24 38%, var(--border-subtle));
}
.william-coach-close {
  position: absolute;
  right: 10px;
  top: 10px;
  width: 28px;
  height: 28px;
  border: 1px solid var(--border-subtle);
  border-radius: 9999px;
  background: var(--surface-2);
  color: var(--text-tertiary);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
.william-coach-close:hover { color: var(--text-primary); background: var(--surface-3); }
.william-coach-eyebrow {
  color: var(--brand-600);
  font-size: 0.68rem;
  font-weight: 950;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}
.william-coach-title {
  margin-top: 4px;
  color: var(--text-primary);
  font-size: clamp(1.35rem, 2.6vw, 2rem);
  font-weight: 950;
  line-height: 1.08;
}
.william-coach-copy {
  margin: 8px 0 0;
  color: var(--text-secondary);
  font-size: 1rem;
  line-height: 1.4;
  max-width: 44rem;
}
.william-coach-reward {
  margin-top: 10px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 9999px;
  background: color-mix(in srgb, #fbbf24 16%, var(--surface-2));
  border: 1px solid color-mix(in srgb, #fbbf24 36%, var(--border-subtle));
  color: var(--text-primary);
  font-size: 0.75rem;
  font-weight: 900;
}
.william-coach-action {
  margin-top: 12px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border: 0;
  border-radius: 9999px;
  padding: 7px 12px;
  background: linear-gradient(135deg, var(--brand-500), var(--accent-500));
  color: #fff;
  font-size: 0.78rem;
  font-weight: 900;
  cursor: pointer;
}
.william-coach-action:hover { filter: brightness(1.04); }

/* ─── Breadcrumb ────────────────────────────────────────────────────── */
.breadcrumb {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  font-size: 0.8125rem;
  color: var(--text-secondary);
}
.breadcrumb-sep { color: var(--text-tertiary); }
.breadcrumb-current { color: var(--text-primary); font-weight: 500; }

/* ─── Page header ───────────────────────────────────────────────────── */
.page-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: var(--space-4);
  margin-bottom: var(--space-6);
  flex-wrap: wrap;
}
.page-title {
  font-family: var(--font-display);
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text-primary);
  letter-spacing: -0.015em;
  line-height: 1.2;
}
.page-subtitle {
  font-size: 0.875rem;
  color: var(--text-secondary);
  margin-top: var(--space-1);
}
.page-actions {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  flex-wrap: wrap;
}

/* ─── Mobile ────────────────────────────────────────────────────────── */
@media (max-width: 1023px) {
  html,
  body {
    overflow-x: clip;
  }
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: var(--topbar-h) 1fr;
    grid-template-areas:
      "topbar"
      "main";
    max-width: 100vw;
    overflow-x: clip;
    padding-bottom: var(--mobile-bottom-offset);
  }
  .app-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    transform: translateX(-100%);
    transition: transform var(--dur-slow) var(--ease-out);
    width: 280px;
    height: 100vh;
    box-shadow: var(--shadow-2xl);
  }
  .sidebar-reveal-zone { display: none !important; }
  .app-shell[data-mobile-nav="open"] .app-sidebar {
    transform: translateX(0);
    pointer-events: auto;
    border-right-color: var(--border-subtle);
  }
  .app-shell[data-mobile-nav="open"]::before {
    content: '';
    position: fixed;
    inset: 0;
    background: rgb(15 23 42 / 0.5);
    z-index: calc(var(--z-sticky) - 1);
    animation: fade-in var(--dur-base) var(--ease-out);
  }
  .app-main {
    width: 100%;
    max-width: 100vw;
    padding: var(--space-4);
    overflow-x: clip;
  }
  .mobile-nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    min-height: var(--mobile-nav-h);
    height: calc(var(--mobile-nav-h) + var(--mobile-safe-bottom));
    background: var(--surface-1);
    border-top: 1px solid var(--border-subtle);
    display: flex;
    align-items: center;
    justify-content: space-around;
    z-index: var(--z-sticky);
    padding: 0 var(--space-2);
    padding-bottom: var(--mobile-safe-bottom);
  }
  .mobile-nav-link {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    color: var(--text-tertiary);
    font-size: 0.625rem;
    font-weight: 500;
    padding: var(--space-2);
    min-height: 48px;
    flex: 1;
    border-radius: var(--radius-md);
  }
  .mobile-nav-link i { font-size: 1.125rem; }
  .mobile-nav-link[aria-current="page"] { color: var(--brand-600); }
  .tb-collapse-btn { display: none !important; }
  .sb-close-btn { display: inline-flex; }
  .william-coach-layer {
    left: 50%;
    right: auto;
    top: 50%;
    bottom: auto;
    translate: -50% -50%;
    width: min(94vw, 520px);
  }
  .william-coach-card {
    grid-template-columns: 92px minmax(0, 1fr);
  }
  .william-coach-portrait {
    width: 108px;
    height: 108px;
  }
  .william-coach-portrait img {
    width: 98px;
    height: 98px;
  }
  .william-coach-bubble {
    padding: 16px 44px 16px 22px;
  }
  .william-coach-copy {
    font-size: 0.8rem;
  }
}

@media (max-width: 760px) {
  .app-topbar {
    padding: 0 var(--space-3);
    gap: var(--space-2);
    overflow: hidden;
  }
  .breadcrumb {
    min-width: 0;
    flex: 1 1 auto;
    overflow: hidden;
  }
  .breadcrumb-current {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .tb-search {
    flex: 0 0 44px;
    width: 44px;
    height: 44px;
    margin-left: 0;
  }
  .tb-search-input {
    width: 44px;
    height: 44px;
    padding: 0;
    color: transparent;
    font-size: 0;
    border-radius: var(--radius-lg);
  }
  .tb-search-icon {
    left: 50%;
    font-size: 1rem;
    transform: translate(-50%, -50%);
  }
  .tb-search-kbd { display: none; }
  .tb-actions { gap: 6px; }
  .tb-icon-btn {
    width: 44px;
    height: 44px;
    border-radius: var(--radius-lg);
  }
  .hud { gap: 6px !important; }
  .hud-ring,
  .hud-level,
  .tb-actions .tb-icon-btn:not(.tb-theme-toggle) {
    display: none !important;
  }
}

@media (max-width: 430px) {
  .app-main { padding: 12px; }
  .app-topbar { padding: 0 10px; }
  .breadcrumb { display: none; }
}

@media (min-width: 1024px) {
  .mobile-nav { display: none; }
  .app-shell[data-sidebar="expanded"] .tb-hamburger { display: none !important; }
}
