/* Thundera Shell v2 — top bar, apps launcher, user menu, mobile drawer, footer, banner, toasts.
 * Requires tokens.css. Every selector is .th-* scoped; nothing here styles the host page.
 * Guide: https://thundera.eu/docs/shell */

/* ── Slot: reserves the bar height before JS runs (no layout shift) ── */
[data-th-bar] {
  display: block; position: sticky; top: 0; z-index: var(--th-z-bar);
  height: calc(var(--th-bar-h) + env(safe-area-inset-top, 0px));
  background: var(--th-glass);
  border-bottom: 1px solid transparent;
}
[data-th-bar][data-th-static] { position: relative; }
[data-th-footer]:empty { display: block; min-height: 320px; }

.th-root, .th-root *, .th-root *::before, .th-root *::after { box-sizing: border-box; }
.th-root {
  font-family: var(--th-font-sans); font-size: var(--th-fs-sm); line-height: var(--th-lh-normal);
  color: var(--th-text); -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale;
}
:where(.th-root) a { color: inherit; text-decoration: none; }
:where(.th-root) button { font: inherit; color: inherit; background: none; border: 0; margin: 0; padding: 0; cursor: pointer; }
:where(.th-root) svg { display: block; flex: none; }
.th-root :focus-visible { outline: 2px solid var(--brand-accent); outline-offset: 2px; }
.th-root [hidden] { display: none !important; }

/* ── Bar ── */
.th-bar {
  position: absolute; inset: 0; display: flex; align-items: center; gap: var(--th-space-2);
  padding: env(safe-area-inset-top, 0px) max(var(--th-gutter), env(safe-area-inset-right, 0px)) 0 max(var(--th-gutter), env(safe-area-inset-left, 0px));
  background: var(--th-glass);
  -webkit-backdrop-filter: saturate(170%) blur(18px); backdrop-filter: saturate(170%) blur(18px);
  border-bottom: 1px solid var(--th-border);
  transition: background-color var(--th-dur-2) var(--th-ease), box-shadow var(--th-dur-2) var(--th-ease);
}
[data-th-bar][data-scrolled] .th-bar { box-shadow: 0 6px 24px rgba(0, 0, 0, .18); border-bottom-color: var(--th-border-2); }
@supports not ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  .th-bar, [data-th-bar] { background: var(--th-bg); }
}
.th-skip {
  position: absolute; left: var(--th-space-3); top: -60px; z-index: 2; padding: 8px 14px;
  border-radius: var(--th-radius-sm); background: var(--brand-accent); color: var(--brand-on) !important;
  font-weight: var(--th-fw-semibold); transition: top var(--th-dur-1) var(--th-ease);
}
.th-skip:focus { top: calc(env(safe-area-inset-top, 0px) + 10px); }

.th-brand { display: flex; align-items: center; gap: var(--th-space-2); min-width: 0; flex: none; }
.th-home { display: flex; align-items: center; gap: 9px; padding: 6px; margin-left: -6px; border-radius: var(--th-radius-sm); color: var(--th-text); }
.th-home:hover { background: var(--th-hover); }
.th-home svg { width: 26px; height: 23px; }
.th-home-word { font-weight: var(--th-fw-bold); font-size: 15px; letter-spacing: -.01em; }
.th-is-group .th-home svg path { fill: url(#th-g-mark); }
.th-sep { width: 1px; height: 20px; background: var(--th-border-3); transform: rotate(18deg); margin: 0 2px; }
.th-app-link { display: flex; align-items: center; gap: 9px; padding: 5px 8px 5px 5px; border-radius: var(--th-radius-sm); min-width: 0; }
.th-app-link:hover { background: var(--th-hover); }
.th-app-mark { width: 28px; height: 28px; border-radius: 8px; object-fit: contain; flex: none; }
.th-app-mono { display: grid; place-items: center; width: 28px; height: 28px; border-radius: 8px; background: var(--brand-grad); color: #fff; font-weight: var(--th-fw-bold); font-size: 13px; flex: none; }
.th-app-name { font-weight: var(--th-fw-semibold); font-size: 15px; letter-spacing: -.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.th-nav { display: flex; align-items: center; gap: 2px; margin-left: var(--th-space-4); min-width: 0; overflow: hidden; }
.th-nav a {
  position: relative; padding: 7px 11px; border-radius: var(--th-radius-sm); color: var(--th-text-2);
  font-weight: var(--th-fw-medium); white-space: nowrap; transition: color var(--th-dur-1), background-color var(--th-dur-1);
}
.th-nav a:hover { color: var(--th-text); background: var(--th-hover); }
.th-nav a[aria-current] { color: var(--th-text); }
.th-nav a[aria-current]::after { content: ""; position: absolute; left: 11px; right: 11px; bottom: -11px; height: 2px; border-radius: 2px; background: var(--brand-grad); }
.th-spacer { flex: 1 1 auto; min-width: var(--th-space-2); }
.th-actions { display: flex; align-items: center; gap: var(--th-space-1-5); flex: none; }

.th-icon-btn {
  display: inline-grid; place-items: center; width: 38px; height: 38px; border-radius: var(--th-radius-sm);
  color: var(--th-text-2); transition: color var(--th-dur-1), background-color var(--th-dur-1);
}
.th-icon-btn:hover, .th-icon-btn[aria-expanded="true"] { color: var(--th-text); background: var(--th-hover); }
.th-icon-btn svg { width: 20px; height: 20px; }

.th-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: 36px; padding: 0 14px;
  border-radius: var(--th-radius-sm); font-weight: var(--th-fw-semibold); font-size: var(--th-fs-sm); white-space: nowrap;
  border: 1px solid transparent; transition: background-color var(--th-dur-1), border-color var(--th-dur-1), transform var(--th-dur-1), box-shadow var(--th-dur-2);
}
.th-root .th-btn-primary { background: var(--brand-accent); color: var(--brand-on); }
.th-root .th-btn-primary:hover { box-shadow: var(--brand-glow); transform: translateY(-1px); }
.th-root .th-btn-ghost { color: var(--th-text); border-color: var(--th-border-2); }
.th-root .th-btn-ghost:hover { background: var(--th-hover); border-color: var(--th-border-3); }
.th-btn-block { width: 100%; height: 44px; font-size: var(--th-fs-base); }

.th-avatar-btn { display: grid; place-items: center; width: 38px; height: 38px; border-radius: 50%; }
.th-avatar-btn:hover .th-avatar, .th-avatar-btn[aria-expanded="true"] .th-avatar { box-shadow: 0 0 0 2px var(--th-bg), 0 0 0 4px var(--brand-line); }
.th-avatar {
  width: 32px; height: 32px; border-radius: 50%; object-fit: cover; flex: none;
  display: grid; place-items: center; background: var(--brand-grad); color: #fff; font-weight: var(--th-fw-bold); font-size: 13px;
  transition: box-shadow var(--th-dur-1);
}
.th-avatar-lg { width: 40px; height: 40px; font-size: 15px; }
.th-burger, .th-signin-m { display: none !important; }
.th-defs { position: absolute; width: 0; height: 0; overflow: hidden; }

/* ── Popovers (launcher + user menu) ── */
.th-pop {
  position: fixed; z-index: var(--th-z-popover);
  top: calc(var(--th-bar-h) + env(safe-area-inset-top, 0px) + 8px);
  right: max(var(--th-gutter), env(safe-area-inset-right, 0px));
  width: 360px; max-width: calc(100vw - 16px);
  max-height: calc(100dvh - var(--th-bar-h) - env(safe-area-inset-top, 0px) - 16px); overflow: auto; overscroll-behavior: contain;
  background: var(--th-surface-1); border: 1px solid var(--th-border-2); border-radius: var(--th-radius-lg);
  box-shadow: var(--th-shadow-4); padding: var(--th-space-2);
  opacity: 0; visibility: hidden; transform: translateY(-6px) scale(.98); transform-origin: top right;
  transition: opacity var(--th-dur-2) var(--th-ease), transform var(--th-dur-2) var(--th-ease), visibility 0s linear var(--th-dur-2);
}
.th-pop[data-open="true"] { opacity: 1; visibility: visible; transform: none; transition-delay: 0s; }
.th-user { width: 300px; }
.th-pop-title { padding: 8px 10px 6px; font-size: var(--th-fs-xs); font-weight: var(--th-fw-semibold); letter-spacing: var(--th-tracking-wide); text-transform: uppercase; color: var(--th-text-3); }

.th-search { position: relative; margin: 2px 2px var(--th-space-2); }
.th-search svg { position: absolute; left: 11px; top: 50%; width: 16px; height: 16px; transform: translateY(-50%); color: var(--th-text-3); pointer-events: none; }
.th-search input {
  width: 100%; height: 38px; padding: 0 12px 0 34px; border-radius: var(--th-radius-sm);
  border: 1px solid var(--th-border-2); background: var(--th-surface-2); color: var(--th-text);
  font: inherit; font-size: var(--th-fs-sm); outline: none; -webkit-appearance: none; appearance: none;
}
.th-search input:focus { border-color: var(--brand-accent); box-shadow: 0 0 0 3px var(--brand-soft); outline: none; }
.th-search input::placeholder { color: var(--th-text-3); }

.th-grid { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 4px; }
.th-app {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: 7px; padding: 12px 4px 10px;
  border-radius: var(--th-radius-md); text-align: center; border: 1px solid transparent;
  transition: background-color var(--th-dur-1), border-color var(--th-dur-1);
}
.th-app:hover, .th-app:focus-visible { background: var(--th-hover); }
.th-root .th-app:focus-visible { outline: none; border-color: var(--brand-accent); }
.th-app[aria-current] { background: var(--brand-soft); border-color: var(--brand-line); }
.th-app-icon { display: grid; place-items: center; width: 44px; height: 44px; border-radius: 12px; background: var(--th-surface-2); border: 1px solid var(--th-border); overflow: hidden; }
.th-app-icon img { width: 34px; height: 34px; object-fit: contain; }
.th-app-icon .th-app-mono { width: 100%; height: 100%; border-radius: 0; }
.th-app-label { font-size: 12.5px; font-weight: var(--th-fw-medium); line-height: 1.25; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.th-app-badge { position: absolute; top: 8px; left: calc(50% + 15px); width: 8px; height: 8px; border-radius: 50%; background: var(--th-amber); box-shadow: 0 0 0 2px var(--th-surface-1); }
.th-app-badge[data-status="disabled"] { background: var(--th-text-3); }
.th-empty { padding: 18px 8px; text-align: center; color: var(--th-text-3); }
.th-pop-foot { display: flex; justify-content: space-between; gap: 8px; margin-top: var(--th-space-2); padding: 8px 6px 2px; border-top: 1px solid var(--th-border); }
.th-pop-foot a { padding: 6px 8px; border-radius: var(--th-radius-sm); color: var(--th-text-2); font-weight: var(--th-fw-medium); }
.th-pop-foot a:hover { color: var(--th-text); background: var(--th-hover); }

.th-user-head { display: flex; align-items: center; gap: 12px; padding: 10px; }
.th-user-meta { min-width: 0; flex: 1; }
.th-user-name { font-weight: var(--th-fw-semibold); font-size: var(--th-fs-base); line-height: 1.25; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.th-user-email { color: var(--th-text-2); font-size: var(--th-fs-xs); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.th-user-plan { display: inline-block; margin-top: 4px; padding: 1px 8px; border-radius: var(--th-radius-full); font-size: 11px; font-weight: var(--th-fw-semibold); background: var(--brand-soft); color: var(--brand-accent); border: 1px solid var(--brand-line); }
.th-hr { height: 1px; margin: 6px 4px; background: var(--th-border); border: 0; }
.th-item {
  display: flex; align-items: center; gap: 10px; width: 100%; padding: 9px 10px; border-radius: var(--th-radius-sm);
  color: var(--th-text-2); font-weight: var(--th-fw-medium); text-align: left; transition: background-color var(--th-dur-1), color var(--th-dur-1);
}
.th-item:hover, .th-item:focus-visible { background: var(--th-hover); color: var(--th-text); }
.th-item svg { width: 17px; height: 17px; color: var(--th-text-3); }
.th-item:hover svg { color: var(--th-text-2); }
.th-item-danger:hover { color: var(--th-danger-text); }
.th-theme-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 6px 10px; color: var(--th-text-2); font-weight: var(--th-fw-medium); }
.th-seg { display: inline-flex; padding: 2px; border-radius: var(--th-radius-sm); background: var(--th-surface-2); border: 1px solid var(--th-border); }
.th-seg button { display: grid; place-items: center; width: 32px; height: 26px; border-radius: 6px; color: var(--th-text-3); }
.th-seg button svg { width: 15px; height: 15px; }
.th-seg button:hover { color: var(--th-text); }
.th-seg button[aria-pressed="true"] { background: var(--th-surface-4); color: var(--th-text); box-shadow: var(--th-shadow-1); }

/* ── Mobile drawer ── */
.th-drawer { position: fixed; inset: 0; z-index: var(--th-z-drawer); visibility: hidden; transition: visibility 0s linear var(--th-dur-3); }
.th-drawer[data-open="true"] { visibility: visible; transition-delay: 0s; }
.th-drawer-scrim { position: absolute; inset: 0; background: var(--th-overlay); opacity: 0; transition: opacity var(--th-dur-3) var(--th-ease); }
.th-drawer[data-open="true"] .th-drawer-scrim { opacity: 1; }
.th-drawer-panel {
  position: absolute; top: 0; right: 0; bottom: 0; width: min(380px, 100%); display: flex; flex-direction: column;
  background: var(--th-bg); border-left: 1px solid var(--th-border-2); box-shadow: var(--th-shadow-4);
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) 0;
  transform: translateX(100%); transition: transform var(--th-dur-3) var(--th-ease);
}
.th-drawer[data-open="true"] .th-drawer-panel { transform: none; }
@media (max-width: 479.98px) { .th-drawer-panel { width: 100%; border-left: 0; } }
.th-drawer-head { display: flex; align-items: center; justify-content: space-between; height: var(--th-bar-h); padding: 0 12px 0 20px; border-bottom: 1px solid var(--th-border); flex: none; }
.th-drawer-body { flex: 1; overflow: auto; overscroll-behavior: contain; padding: 12px 12px 24px; }
.th-drawer-nav a { display: flex; align-items: center; justify-content: space-between; padding: 13px 12px; border-radius: var(--th-radius-md); font-size: var(--th-fs-lg); font-weight: var(--th-fw-semibold); letter-spacing: -.01em; }
.th-drawer-nav a:hover { background: var(--th-hover); }
.th-drawer-nav a[aria-current] { background: var(--brand-soft); color: var(--brand-accent); }
.th-drawer-nav a svg { width: 18px; height: 18px; color: var(--th-text-3); }
.th-drawer-section { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--th-border); }
.th-drawer-cta { display: grid; gap: 10px; padding: 4px; }
html.th-lock, html.th-lock body { overflow: hidden !important; }

@media (max-width: 899.98px) {
  .th-nav, .th-hide-mobile { display: none !important; }
  .th-burger { display: inline-grid !important; }
  .th-signin-m { display: inline-flex !important; }
  .th-bar { gap: 4px; }
  .th-home-word.th-optional { display: none; }
  .th-app-name { font-size: 14.5px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .th-brand { flex: 0 1 auto; } /* a long app name + Sign in must not push the bar past a 390px viewport */
  .th-pop { left: 8px; right: 8px; width: auto; max-width: none; transform-origin: top center; }
  .th-actions .th-btn { height: 34px; padding: 0 12px; }
}
@media (max-width: 380px) {
  .th-sep, .th-is-app .th-home { display: none; }
}

/* ── Banner (maintenance + announcements) ── */
.th-banner { display: flex; align-items: flex-start; gap: 12px; padding: 10px max(var(--th-gutter), env(safe-area-inset-left, 0px)); font-size: var(--th-fs-sm); border-bottom: 1px solid var(--th-border); background: var(--th-info-soft); color: var(--th-text); }
.th-banner[data-severity="warning"] { background: var(--th-warning-soft); }
.th-banner[data-severity="critical"], .th-banner[data-severity="error"] { background: var(--th-danger-soft); }
.th-banner[data-severity="success"] { background: var(--th-success-soft); }
.th-banner-text { flex: 1; min-width: 0; }
.th-banner strong { font-weight: var(--th-fw-semibold); margin-right: 6px; }
.th-banner .th-icon-btn { width: 28px; height: 28px; margin: -4px -6px -4px 0; }

/* ── Toasts ── */
.th-toasts { position: fixed; z-index: var(--th-z-toast); right: max(16px, env(safe-area-inset-right, 0px)); bottom: max(16px, env(safe-area-inset-bottom, 0px)); display: grid; gap: 8px; width: min(380px, calc(100vw - 32px)); pointer-events: none; }
.th-toast {
  display: flex; align-items: flex-start; gap: 10px; padding: 12px 14px; border-radius: var(--th-radius-md); pointer-events: auto;
  background: var(--th-surface-2); border: 1px solid var(--th-border-2); box-shadow: var(--th-shadow-3); color: var(--th-text);
  animation: th-toast-in var(--th-dur-3) var(--th-ease);
}
.th-toast::before { content: ""; width: 8px; height: 8px; margin-top: 6px; border-radius: 50%; background: var(--th-sky); flex: none; }
.th-toast[data-kind="success"]::before { background: var(--th-green); }
.th-toast[data-kind="warning"]::before { background: var(--th-amber); }
.th-toast[data-kind="error"]::before { background: var(--th-red); }
.th-toast[data-leaving] { opacity: 0; transform: translateY(6px); transition: opacity var(--th-dur-2), transform var(--th-dur-2); }
@keyframes th-toast-in { from { opacity: 0; transform: translateY(10px) scale(.98); } }
@media (max-width: 899.98px) { .th-toasts { left: 16px; right: 16px; width: auto; margin: 0 auto; max-width: 420px; } }

/* ── Footer ── */
.th-footer { position: relative; margin-top: auto; background: var(--th-bg-2); border-top: 1px solid var(--th-border); color: var(--th-text-2); }
.th-footer-in { max-width: var(--th-container-wide); margin: 0 auto; padding: clamp(48px, 7vw, 80px) max(var(--th-gutter), env(safe-area-inset-left, 0px)) max(28px, env(safe-area-inset-bottom, 0px)); }
.th-footer-grid { display: grid; grid-template-columns: minmax(220px, 1.4fr) repeat(var(--th-footer-cols, 4), minmax(0, 1fr)); gap: 40px 32px; }
.th-footer-grid[data-cols="3"] { --th-footer-cols: 3; }
.th-footer-brand p { margin: 14px 0 0; max-width: 34ch; line-height: var(--th-lh-relaxed); }
.th-footer-brand .th-home { margin-left: -6px; }
.th-footer-brand .th-heart { display: inline-block; width: 1em; height: 1em; vertical-align: -0.125em; }
.th-footer h2 { margin: 2px 0 14px; font-size: var(--th-fs-xs); font-weight: var(--th-fw-semibold); letter-spacing: var(--th-tracking-wide); text-transform: uppercase; color: var(--th-text-3); }
.th-footer ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.th-footer li a, .th-footer li button { display: inline-flex; align-items: center; gap: 8px; padding: 5px 0; color: var(--th-text-2); transition: color var(--th-dur-1); text-align: left; }
.th-footer li a:hover, .th-footer li button:hover { color: var(--th-text); }
.th-footer li img { width: 18px; height: 18px; border-radius: 5px; }
.th-footer-bottom { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 16px; margin-top: 48px; padding-top: 24px; border-top: 1px solid var(--th-border); font-size: var(--th-fs-xs); color: var(--th-text-3); }
.th-footer-bottom nav { display: flex; align-items: center; gap: 4px; }
.th-footer-bottom .th-icon-btn { width: 34px; height: 34px; }
.th-status-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--th-green); box-shadow: 0 0 0 3px var(--th-success-soft); }
@media (max-width: 1023.98px) { .th-footer-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); } .th-footer-brand { grid-column: 1 / -1; } }
@media (max-width: 599.98px) { .th-footer-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 32px 20px; } }

@media (prefers-reduced-motion: reduce) {
  .th-root *, .th-root *::before, .th-root *::after { animation-duration: 1ms !important; transition-duration: 1ms !important; }
}
@media print { [data-th-bar], .th-pop, .th-drawer, .th-toasts { display: none !important; } }
