/* App shell: header, navigation, banners, toasts, footer, error card, layout.  (owner: APP-SHELL, prefix app-)
   Spec: docs/spec/50-app.md "10. シェルと共通 UI", docs/spec/00-overview.md "4.7", "6. デザイン方針", "7. a11y 契約（一般）".
   Loaded statically by index.html after tokens.css and base.css.  No hex / rgb colours: tokens only.  No global element selectors
   (those live in base.css); everything here hangs off an app- class.
   Sections: 1 shell + skip link  2 header  3 navigation  4 stats chips  5 banners  6 main / skeleton / error  7 toasts  8 footer
             9 forced colors  10 print */

/* ---------------------------------------------------------------- 1. shell + skip link */
.app-shell { min-height: 100vh; min-height: 100dvh; display: flex; flex-direction: column; }

.app-skip {
  position: absolute; z-index: 100; top: var(--space-3); left: var(--space-3);
  min-height: var(--tap); padding: 0 var(--space-4); border-radius: var(--radius-m);
  border: 2px solid var(--primary-strong); background: var(--primary); color: var(--primary-ink);
  font-weight: 700; box-shadow: var(--shadow-2);
  transform: translateY(-300%);
}
.app-skip:focus { transform: none; }

/* ---------------------------------------------------------------- 2. header */
.app-header {
  position: sticky; top: 0; z-index: 30;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.app-header-inner {
  position: relative; display: flex; align-items: center; gap: var(--space-3);
  min-height: var(--header-h); max-width: 1480px; margin: 0 auto; padding: 0 var(--space-4);
}

.app-brand {
  order: 0; flex: none; display: inline-flex; align-items: center; gap: 10px; min-height: var(--tap); min-width: var(--tap);
  color: var(--text); text-decoration: none; border-radius: var(--radius-s);
}
.app-brand:hover { color: var(--text); }
.app-brand-mark {
  display: grid; place-items: center; flex: none; width: 36px; height: 36px; border-radius: 10px;
  background: linear-gradient(135deg, var(--primary), var(--primary-strong)); color: var(--primary-ink);
  box-shadow: var(--shadow-1);
}
.app-brand-text { display: flex; flex-direction: column; line-height: 1.15; }
.app-brand-a { font-size: 1rem; font-weight: 800; letter-spacing: .01em; }
.app-brand-b { font-size: .75rem; font-weight: 700; color: var(--text-2); }
@media (min-width: 520px) {
  .app-brand-text { flex-direction: row; align-items: baseline; gap: .4em; }
  .app-brand-a { font-size: 1.0625rem; }
  .app-brand-b { font-size: 1rem; font-weight: 600; }
}

/* menu button (narrow screens only) */
.app-menu-btn {
  order: 2; display: inline-flex; align-items: center; gap: var(--space-2); flex: none;
  min-height: var(--tap); min-width: var(--tap); padding: 0 var(--space-3); border-radius: var(--radius-m);
  border: 2px solid var(--border-ui); background: var(--surface); color: var(--text); font-weight: 700; font-size: .875rem;
}
.app-menu-btn:hover { background: var(--surface-2); }
.app-burger { display: grid; gap: 4px; width: 18px; }
.app-burger i { display: block; height: 2px; border-radius: 2px; background: currentColor; transition: transform .15s ease, opacity .15s ease; }
.app-header[data-menu="open"] .app-burger i:nth-child(1) { transform: translateY(6px) rotate(45deg); }
.app-header[data-menu="open"] .app-burger i:nth-child(2) { opacity: 0; }
.app-header[data-menu="open"] .app-burger i:nth-child(3) { transform: translateY(-6px) rotate(-45deg); }
@media (max-width: 400px) {
  .app-menu-label { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
  .app-menu-btn { padding: 0; justify-content: center; }
}

/* ---------------------------------------------------------------- 3. navigation
   Narrow (default): a dropdown panel under the header, opened by the menu button (aria-expanded).
   Wide (>= 1240px): inline in the header as text links (the icons stay in the dropdown: 7 labels + brand + chips do not fit with
   icons); the roomier sizes join from 1440px. */
.app-nav {
  display: none; position: absolute; top: 100%; left: 0; right: 0; z-index: 31;
  max-height: calc(100vh - var(--header-h)); max-height: calc(100dvh - var(--header-h)); overflow-y: auto;
  padding: var(--space-2) var(--space-4) var(--space-4);
  background: var(--surface); border-bottom: 1px solid var(--border); box-shadow: var(--shadow-2);
}
.app-header[data-menu="open"] .app-nav { display: block; }
.app-nav-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: var(--space-2); }   /* >= 8px between targets (00-overview 7 (2)) */
.app-nav-item { margin: 0; }
.app-nav-item + .app-nav-item { margin-top: 0; }
.app-nav-item:not(.app-nav-util) + .app-nav-util { margin-top: var(--space-1); padding-top: var(--space-3); border-top: 1px solid var(--border); }
.app-nav-link {
  position: relative; display: flex; align-items: center; gap: 12px; min-height: 48px; padding: 0 var(--space-3);
  border-radius: var(--radius-m); color: var(--text-2); font-weight: 700; text-decoration: none;
}
.app-nav-link:hover { background: var(--surface-2); color: var(--text); }
.app-nav-icon { flex: none; }
.app-nav-text { min-width: 0; }
/* the current page / section: tinted pill + a bar (never colour alone) */
.app-nav-link[aria-current] { background: var(--primary-soft); color: var(--primary-strong); }
.app-nav-link[aria-current]::after {
  content: ""; position: absolute; left: 0; top: 10px; bottom: 10px; width: 4px; border-radius: 0 4px 4px 0; background: var(--primary);
}

.app-tools { order: 1; display: flex; align-items: center; gap: var(--space-2); flex: none; margin-left: auto; }
.app-level, .app-icon-link { display: none; }

@media (min-width: 1240px) {
  .app-menu-btn { display: none; }
  .app-nav {
    display: block; position: static; flex: 1 1 auto; min-width: 0; max-height: none; overflow: visible; padding: 0;
    background: none; border: 0; box-shadow: none;
  }
  .app-header[data-menu="open"] .app-nav { display: block; }
  .app-nav-list { flex-direction: row; justify-content: center; gap: var(--space-2); }
  .app-nav-util { display: none; }
  .app-header-inner { gap: var(--space-2); }
  .app-nav-link { min-height: var(--tap); min-width: var(--tap); justify-content: center; padding: 0 6px; gap: 6px; font-size: .875rem; white-space: nowrap; }   /* short labels such as 復習 still reach 44 x 44 */
  .app-nav-link[aria-current]::after { left: 12px; right: 12px; top: auto; bottom: 3px; width: auto; height: 3px; border-radius: 3px; }
  .app-brand { order: 0; }
  .app-nav { order: 1; }
  .app-tools { order: 2; margin-left: 0; }
  .app-level, .app-icon-link { display: inline-flex; }
  .app-nav-icon { display: none; }
}
@media (min-width: 1440px) {
  .app-header-inner { gap: var(--space-3); }
  .app-nav-link { padding: 0 8px; font-size: .9375rem; }
}

.app-level {
  align-items: center; gap: 6px; min-height: var(--tap); padding: 0 12px; border-radius: 999px;
  border: 1px solid var(--border-ui); background: var(--surface); color: var(--text); font-size: .875rem; font-weight: 700; text-decoration: none;
  white-space: nowrap;
}
.app-level:hover { background: var(--surface-2); color: var(--text); }
.app-level[aria-current] { background: var(--primary-soft); color: var(--primary-strong); border-color: var(--primary); }
.app-level-icon { color: var(--primary-strong); }
.app-level-value { font-variant-numeric: tabular-nums; }
.app-icon-link {
  align-items: center; justify-content: center; width: var(--tap); height: var(--tap); border-radius: 50%;
  color: var(--text-2); text-decoration: none;
}
.app-icon-link:hover { background: var(--surface-2); color: var(--text); }
.app-icon-link[aria-current] { background: var(--primary-soft); color: var(--primary-strong); }

/* ---------------------------------------------------------------- 4. stats chips (today's XP, streak) */
.app-stats { list-style: none; display: flex; gap: 6px; margin: 0; padding: 0; }
.app-chip {
  position: relative; display: inline-flex; align-items: center; gap: 4px; margin: 0; min-height: 34px; padding: 0 10px 3px;
  border-radius: 999px; font-size: .8125rem; font-weight: 700; line-height: 1.2; white-space: nowrap; overflow: hidden;
}
.app-stats .app-chip + .app-chip { margin-top: 0; }
.app-chip-value { font-size: .9375rem; font-variant-numeric: tabular-nums; }
.app-chip--xp { background: var(--primary-soft); color: var(--primary-strong); }
.app-chip--streak { background: var(--accent-soft); color: var(--text); }
.app-chip-icon { flex: none; }
.app-chip--xp .app-chip-icon { color: var(--primary-strong); }
.app-chip-check { color: var(--ok); }
.app-chip-bar {
  position: absolute; left: 10px; right: 10px; bottom: 3px; height: 3px; border-radius: 3px; background: var(--surface); overflow: hidden;
}
.app-chip-bar > i { display: block; height: 100%; width: var(--pct, 0%); background: var(--primary); border-radius: inherit; transition: width .4s ease; }
.app-chip.is-done .app-chip-bar > i { background: var(--ok); }
/* "sr" = visually hidden but still read: the chips keep their full text ("今日 44 / 40 XP", "連続 8 日") for screen readers */
@media (max-width: 640px) {
  .app-chip-label, .app-chip-goal {
    position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
  }
  .app-chip { padding: 0 8px 3px; }
  .app-chip-bar { left: 8px; right: 8px; }
}
@media (max-width: 480px) {
  .app-header-inner { gap: var(--space-2); padding: 0 var(--space-3); }
  .app-chip-unit {
    position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
  }
  .app-stats { gap: 4px; }
  .app-chip.is-done .app-chip-icon { display: none; }      /* the check replaces the bolt: same width */
}
@media (max-width: 339px) {
  .app-brand-text { display: none; }                           /* the aria-label still names the site; the footer spells it out */
}
@media (min-width: 1240px) and (max-width: 1439px) {
  .app-chip-label { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
}

/* ---------------------------------------------------------------- 5. banners (below the header; at most two) */
.app-banners {
  width: 100%; max-width: var(--content-w); margin: 0 auto; padding: var(--space-3) var(--space-4) 0;
  display: grid; gap: var(--space-2);
}
.app-banners[hidden] { display: none; }
.app-banner {
  display: flex; align-items: flex-start; gap: var(--space-3); padding: var(--space-3) var(--space-4);
  border: 1px solid var(--border); border-inline-start-width: 6px; border-radius: var(--radius-m);
  background: var(--surface-2); color: var(--text);
}
.app-banner--warn { background: var(--warn-soft); border-color: var(--warn); }
.app-banner--info { background: var(--primary-soft); border-color: var(--primary); }
.app-banner--ok { background: var(--ok-soft); border-color: var(--ok); }
.app-banner-icon { flex: none; display: grid; margin-top: 2px; }
.app-banner--warn .app-banner-icon { color: var(--warn); }
.app-banner--info .app-banner-icon { color: var(--primary-strong); }
.app-banner--ok .app-banner-icon { color: var(--ok); }
.app-banner-body { flex: 1 1 auto; min-width: 0; }
.app-banner-text { margin: 0 0 var(--space-1); font-weight: 700; line-height: 1.6; }
.app-banner-note { margin: 0; font-size: .875rem; line-height: 1.7; color: var(--text-2); }
.app-banner-actions { margin-top: var(--space-2); }
.app-banner-close {
  flex: none; display: inline-flex; align-items: center; justify-content: center; width: var(--tap); height: var(--tap);
  margin: -6px -8px -6px 0; border: 0; border-radius: 50%; background: transparent; color: var(--text-2);
}
.app-banner-close:hover { background: var(--surface-3); color: var(--text); }

/* ---------------------------------------------------------------- 6. main area, skeleton, error card */
.app-main {
  flex: 1 0 auto; width: 100%; max-width: var(--content-w); margin: 0 auto;
  padding: var(--space-5) var(--space-4) var(--space-7);
}
.app-main:focus { outline: none; }
/* Every route change moves focus to the first <h1> (so a screen reader starts at the page title). A heading is not an operable
   control and the page scrolls to the top anyway, so the focus ring would only be noise there. */
.app-main h1[tabindex="-1"]:focus, .app-error h1:focus { outline: none; }
@media (min-width: 900px) { .app-main { padding-top: var(--space-6); } }

.app-boot, .app-noscript { margin: var(--space-6) 0; color: var(--text-2); }
.app-noscript { padding: var(--space-4); border: 2px solid var(--warn); border-radius: var(--radius-m); background: var(--warn-soft); color: var(--text); font-weight: 700; }

/* shown while a view is being imported; fades in after a short delay so a fast load never flashes it */
.app-skeleton { display: grid; gap: var(--space-3); max-width: 720px; animation: app-fade-in .25s ease .2s both; }
.app-skeleton-line { height: 16px; border-radius: 8px; background: var(--surface-3); animation: app-pulse 1.4s ease-in-out infinite; }
.app-skeleton-title { width: 45%; height: 30px; }
.app-skeleton-short { width: 70%; }
.app-skeleton-block { height: 160px; border-radius: var(--radius-m); background: var(--surface-2); border: 1px solid var(--border); animation: app-pulse 1.4s ease-in-out .2s infinite; }
@keyframes app-fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes app-pulse { 50% { opacity: .5; } }

.app-error { max-width: 560px; margin: var(--space-7) auto 0; display: grid; justify-items: center; gap: var(--space-2); text-align: center; }
.app-error-icon { display: grid; place-items: center; width: 56px; height: 56px; border-radius: 50%; background: var(--warn-soft); color: var(--warn); }
.app-error h1 { margin: var(--space-2) 0 0; font-size: 1.375rem; }
.app-error p { margin: 0; color: var(--text-2); }
.app-error-actions { justify-content: center; margin-top: var(--space-3); }
.app-error-detail { width: 100%; margin-top: var(--space-3); text-align: left; color: var(--text-2); font-size: .875rem; }
.app-error-detail pre { margin-top: var(--space-2); white-space: pre-wrap; overflow-wrap: anywhere; }

/* ---------------------------------------------------------------- 7. toasts (not a live region: announce() reads each once) */
.app-toasts {
  position: fixed; z-index: 60; right: var(--space-4); bottom: var(--space-4);
  display: flex; flex-direction: column; align-items: flex-end; gap: var(--space-2);
  max-width: min(380px, calc(100vw - 32px)); pointer-events: none;
}
.app-toast {
  --toast-accent: var(--primary); --toast-ink: var(--primary-strong);
  pointer-events: auto; display: flex; align-items: center; gap: 10px; padding: 10px 16px;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); border-inline-start: 6px solid var(--toast-accent);
  border-radius: var(--radius-m); box-shadow: var(--shadow-2); font-weight: 700; font-variant-numeric: tabular-nums;
  animation: app-toast-in .22s ease-out both;
}
.app-toast--xp, .app-toast--streak, .app-toast--badge { --toast-accent: var(--accent); --toast-ink: var(--accent-ink); }
.app-toast--field, .app-toast--freeze { --toast-accent: var(--ok); --toast-ink: var(--ok); }
.app-toast-icon { display: grid; flex: none; color: var(--toast-ink); }
.app-toast-text { min-width: 0; }
.app-toast.is-leaving { animation: app-toast-out .2s ease-in both; }
@keyframes app-toast-in { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }
@keyframes app-toast-out { to { opacity: 0; transform: translateY(6px); } }
@media (max-width: 640px) {
  /* the player has a fixed bottom bar on phones, so toasts sit under the header instead */
  .app-toasts { top: calc(var(--header-h) + var(--space-2)); bottom: auto; left: var(--space-3); right: var(--space-3); align-items: stretch; max-width: none; }
  @keyframes app-toast-in { from { opacity: 0; transform: translateY(-10px); } to { opacity: 1; transform: none; } }
  /* compact on phones: only the newest toast is shown, slim, and it never intercepts taps on the lesson below */
  .app-toasts { align-items: flex-end; left: auto; max-width: min(70vw, 280px); }
  .app-toast { pointer-events: none; padding: 6px 12px; font-size: .875rem; }
  .app-toast:not(:last-child) { display: none; }
}

/* ---------------------------------------------------------------- 8. footer */
.app-footer { background: var(--surface); border-top: 1px solid var(--border); }
.app-footer-inner {
  max-width: var(--content-w); margin: 0 auto; padding: var(--space-5) var(--space-4) calc(var(--space-5) + env(safe-area-inset-bottom, 0px));
  display: grid; gap: var(--space-1); font-size: .875rem; line-height: 1.7;
}
.app-footer-inner p { margin: 0; }
.app-footer-notice { color: var(--text); font-weight: 700; }
.app-footer-note, .app-footer-brand { color: var(--text-2); }
.app-footer-links { display: flex; flex-wrap: wrap; gap: var(--space-2) var(--space-5); }
.app-footer-links a { display: inline-flex; align-items: center; min-height: var(--tap); }

/* ---------------------------------------------------------------- 9. forced colors */
@media (forced-colors: active) {
  .app-header, .app-footer, .app-nav { border-color: CanvasText; }
  .app-skip { background: ButtonFace; color: ButtonText; border-color: ButtonText; }
  .app-brand-mark { background: Canvas; color: CanvasText; border: 2px solid CanvasText; box-shadow: none; }
  .app-menu-btn, .app-level, .app-chip, .app-banner, .app-toast, .app-banner-close { border: 1px solid CanvasText; background: Canvas; color: CanvasText; }
  .app-banner, .app-toast { border-inline-start-width: 6px; }
  .app-burger i { background: ButtonText; }
  .app-nav-link[aria-current] { background: Canvas; color: LinkText; border: 2px solid Highlight; }
  .app-nav-link[aria-current]::after { background: Highlight; }
  .app-nav-link:hover, .app-icon-link:hover { background: Canvas; color: Highlight; }
  .app-icon-link[aria-current], .app-level[aria-current] { border: 2px solid Highlight; }
  .app-chip-bar { border: 1px solid CanvasText; background: Canvas; }
  .app-chip-bar > i, .app-chip.is-done .app-chip-bar > i { background: Highlight; }
  .app-toast { box-shadow: none; }
  .app-error-icon { border: 2px solid CanvasText; background: Canvas; color: CanvasText; }
}

/* ---------------------------------------------------------------- 10. print (css/print.css, owned by the player, adds the rest) */
@media print {
  .app-skip, .app-header, .app-banners, .app-toasts, .app-footer-links { display: none !important; }
  .app-main { max-width: none; padding: 0; }
  .app-footer { border: 0; background: none; }
}
