/* The app shell (plan 13): header, tab bar, main, shared bits. Scoped under .app-shell;
   colours come from lib/ui/tokens.css only. Views bring their own CSS (views/<name>/*.css). */

.app-shell,
.app-shell * {
  box-sizing: border-box;
}
html:has(body.app-shell) {
  background: var(--bg-color);
}
body.app-shell {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  font-family: var(--font-ui);
  background: var(--bg-color);
  color: var(--text-color);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}
.app-shell [lang="ja"] {
  font-family: var(--font-ja);
}
:where(.app-shell) a {
  color: var(--primary);
}
:where(.app-shell) button {
  font: inherit;
  color: inherit;
}
.app-shell :focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}
.app-shell h1:focus,
.app-shell #view:focus {
  outline: none;
}
.app-shell .icon {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}
.app-shell .muted {
  color: var(--text-muted);
}

.app-shell .skip-link {
  position: absolute;
  left: 8px;
  top: -48px;
  z-index: 50;
  padding: 8px 12px;
  border-radius: 8px;
  background: var(--surface-color);
  color: var(--text-color);
}
.app-shell .skip-link:focus {
  top: 8px;
}

/* --- Header --- */
.app-shell .app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  height: 60px;
  padding: 0 var(--gutter);
  padding-top: env(safe-area-inset-top);
  box-sizing: content-box;
  display: flex;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid var(--border-color);
}
/* the blur sits on a pseudo-element: a backdrop-filter on the header itself would
   become the containing block of the fixed bottom tab bar inside it */
.app-shell .app-header::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: var(--surface-overlay);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
}
.app-shell .app-back {
  margin-left: -8px;
}
.app-shell .app-back[hidden] {
  display: none;
}
.app-shell .brand {
  color: var(--text-color);
  text-decoration: none;
  font-weight: 600;
  font-size: 1.05rem;
  letter-spacing: -0.2px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  white-space: nowrap;
  flex-shrink: 0;
}
/* CSS-generated so the Iyaku Reader extension never rewrites it */
.app-shell .brand-mark::before {
  content: "いやく";
}
.app-shell .brand-mark {
  font-family: var(--font-ja);
  font-size: 0.8rem;
  padding: 3px 7px;
  border-radius: 6px;
  background: var(--primary-gradient);
  color: #fff;
}
.app-shell .app-header-slot {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}
.app-shell .header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}
.app-shell .header-link {
  background: transparent;
  border: 1px solid var(--border-color);
  color: var(--text-color);
  padding: 7px 14px;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  max-width: 160px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.app-shell .header-link:hover {
  background: var(--surface-hover);
}
.app-shell .account-btn.signed-in {
  color: var(--text-muted);
}
.app-shell .account-btn.is-plus {
  border-color: rgba(245, 158, 11, 0.45);
}

/* --- Sync pill (store.pending() / offline) --- */
.app-shell .sync-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid var(--border-color);
  background: transparent;
  color: var(--text-muted);
  font-size: 0.75rem;
  white-space: nowrap;
  cursor: pointer;
}
.app-shell .sync-pill[hidden] {
  display: none;
}
.app-shell .sync-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--success);
}
.app-shell .sync-pill[data-tone="offline"] {
  color: var(--warning);
  border-color: rgba(245, 158, 11, 0.35);
}
.app-shell .sync-pill[data-tone="offline"] .sync-dot {
  background: var(--warning);
}
.app-shell .sync-pill[data-tone="busy"] .sync-dot {
  background: var(--primary);
  animation: app-pulse 1.2s ease-in-out infinite;
}
@keyframes app-pulse {
  50% {
    opacity: 0.3;
  }
}

/* --- Tabs: in the header on wide screens --- */
.app-shell .tabbar {
  display: flex;
  align-items: center;
  gap: 2px;
  margin-left: 12px;
}
.app-shell .tab {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 8px 12px;
  border-radius: 10px;
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.9rem;
  font-weight: 500;
  transition:
    color 0.15s ease,
    background 0.15s ease;
}
.app-shell .tab .icon {
  width: 18px;
  height: 18px;
}
.app-shell .tab:hover {
  color: var(--text-color);
  background: var(--surface-hover);
}
.app-shell .tab.active {
  color: var(--text-color);
  background: var(--surface-hover);
}
.app-shell .tab.active .icon {
  color: var(--primary);
}
body.app-shell[data-chrome="immersive"] .tabbar {
  display: none;
}

/* --- Main --- */
.app-shell .app-main {
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
  padding: 28px var(--gutter) 64px;
  display: block;
}
/* booting / a slow view chunk: a quiet spinner, only if it takes a moment */
.app-shell .app-main:empty::after {
  content: "";
  display: block;
  width: 26px;
  height: 26px;
  margin: 18vh auto 0;
  border-radius: 50%;
  border: 2.5px solid var(--border-color);
  border-top-color: var(--primary);
  animation:
    app-spin 0.8s linear infinite,
    app-fade-in 0.2s ease 0.3s both;
}
@keyframes app-spin {
  to {
    transform: rotate(360deg);
  }
}
@keyframes app-fade-in {
  from {
    opacity: 0;
  }
}

/* --- A view that failed to load (router.js) --- */
.app-shell .view-error {
  margin: 10vh auto 0;
  max-width: 440px;
  text-align: center;
  padding: 28px 24px;
  background: var(--surface-color);
  border: 1px solid var(--border-color);
  border-radius: 20px;
}
.app-shell .view-error h1 {
  margin: 0 0 8px;
  font-size: 1.3rem;
}
.app-shell .view-error-msg {
  margin: 0 0 18px;
  color: var(--text-muted);
  overflow-wrap: anywhere;
}
.app-shell .view-error-actions {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px;
}

/* --- Narrow screens: the tab bar moves to the bottom --- */
@media (max-width: 720px) {
  .app-shell .app-header {
    height: 56px;
    gap: 6px;
  }
  .app-shell .tabbar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    margin: 0;
    gap: 0;
    justify-content: space-around;
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    background: var(--surface-overlay);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-top: 1px solid var(--border-color);
  }
  .app-shell .tab {
    flex: 1;
    flex-direction: column;
    gap: 3px;
    padding: 6px 4px;
    font-size: 0.7rem;
    border-radius: 12px;
  }
  .app-shell .tab .icon {
    width: 22px;
    height: 22px;
  }
  .app-shell .tab:hover,
  .app-shell .tab.active {
    background: transparent;
  }
  .app-shell .tab.active {
    color: var(--primary);
  }
  .app-shell .app-main {
    padding: 20px var(--gutter) calc(96px + env(safe-area-inset-bottom));
  }
  body.app-shell[data-chrome="immersive"] .app-main {
    padding-bottom: calc(32px + env(safe-area-inset-bottom));
  }
  .app-shell #toast {
    bottom: calc(84px + env(safe-area-inset-bottom));
  }
}

/* --- Toast (lib/ui/toast.js) and the error banner (study/errors.js) --- */
.app-shell #toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translate(-50%, 20px);
  max-width: calc(100vw - 32px);
  background: var(--surface-hover);
  border: 1px solid var(--border-color);
  color: var(--text-color);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 0.9rem;
  opacity: 0;
  pointer-events: none;
  transition: all 0.25s ease;
  z-index: 2000;
}
.app-shell #toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}
.app-shell #errorBanner {
  position: fixed;
  left: 50%;
  bottom: 16px;
  transform: translate(-50%, 24px);
  width: min(680px, calc(100% - 32px));
  background: var(--surface-color);
  border: 1px solid var(--danger);
  color: var(--text-color);
  border-radius: var(--radius);
  padding: 12px 14px;
  z-index: 3000;
  opacity: 0;
  pointer-events: none;
  transition: all 0.25s ease;
}
.app-shell #errorBanner.show {
  opacity: 1;
  pointer-events: auto;
  transform: translate(-50%, 0);
}
.app-shell .error-banner-inner {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
}
.app-shell .error-banner-text {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: 0.9rem;
  min-width: 0;
  overflow-wrap: anywhere;
}
.app-shell .error-banner-details {
  font-size: 0.75rem;
  color: var(--text-muted);
}
.app-shell .error-banner-actions {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}
.app-shell .error-banner-actions button {
  background: transparent;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 0.85rem;
}
.app-shell .auth-modal .btn-secondary,
.app-shell .auth-modal .btn-primary {
  background: var(--primary-gradient) !important;
  color: #fff !important;
  border: none !important;
  border-radius: 12px;
  font-weight: 600;
}
.app-shell .noscript {
  padding: 24px;
  text-align: center;
}

@media (prefers-reduced-motion: reduce) {
  .app-shell *,
  .app-shell *::before,
  .app-shell *::after {
    animation-duration: 0.01ms !important;
    transition: none !important;
  }
}
