/* ============================================================
 * Shared page nav + global design tokens.
 * Claude Desktop 風 persistent sidebar + モダン UI primitives.
 *
 * 状態は <html> の class で管理. inline script (各 HTML head) で
 * paint 前に状態を確定するため FOUC なし.
 *
 * - 全 viewport で hamburger 常時表示 (画面左上端)
 * - .nav-menu は左側 fixed sidebar
 * - toggle button で開閉, 状態は localStorage で永続化 (auto-close なし)
 * - PC / iPad横 (>=768px): sidebar open 時に body を 280px 右に push (双方向 transition)
 * - スマホ (<768px): sidebar は content の上に overlay (push しない) + scrim + scroll lock
 * - html.no-transitions で初回 paint まで transition を抑止 (flicker 防止)
 * - prefers-reduced-motion 対応で transition は無効化
 * ============================================================ */

/* CRITICAL-START
 * このコメント markers (CRITICAL-START / CRITICAL-END) 間の CSS は
 * server.js の HTML 注入 middleware が起動時に抽出 → minify → 各 HTML
 * response の <head> に <style> inline 注入する. nav.css 自体は preload
 * + async stylesheet 化されるため, critical CSS が同 packet で到達し
 * FOUC を完全に解消する (sidebar / font-size / tokens 等が paint 時に確定).
 *
 * 編集規律: critical 範囲を変えたい場合 markers の位置を移動する.
 * markers を消したら server 起動時に fail-fast (extract 空で throw).
 * ================================================================ */
/* ----------------------------------------------------------------
 * Design tokens (global).
 * nav.css が全 page で読み込まれるため, ここで定義したものは
 * 全 page から参照可能.
 * ---------------------------------------------------------------- */
:root {
  /* Easing curves */
  --ease-standard: cubic-bezier(0.4, 0, 0.2, 1);     /* material standard */
  --ease-decelerate: cubic-bezier(0, 0, 0.2, 1);     /* enter */

  /* Duration */
  --duration-fast: 150ms;
  --duration-normal: 250ms;
  --duration-medium: 280ms;

  /* Radius scale */
  --radius-xs: 4px;
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --radius-xl: 20px;
  --radius-pill: 999px;

  /* Shadow elevation */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.22), 0 1px 1px rgba(0, 0, 0, 0.14);
  --shadow-md: 0 4px 8px rgba(0, 0, 0, 0.26), 0 2px 4px rgba(0, 0, 0, 0.16);
  --shadow-lg: 0 12px 24px rgba(0, 0, 0, 0.36), 0 4px 10px rgba(0, 0, 0, 0.22);
  --shadow-xl: 0 24px 48px rgba(0, 0, 0, 0.45), 0 8px 16px rgba(0, 0, 0, 0.28);

  /* Color tokens (拡張) — 既存 --bg --fg --accent --panel --line を維持しつつ追加 */
  --bg-elevated: rgba(255, 255, 255, 0.04);
  --accent-hover: #84b8ff;
  --line-strong: rgba(255, 255, 255, 0.18);

  /* Control sizing (button / input min-height). iOS HIG tap target 44px を base に. */
  --btn-h-sm: 32px;    /* small action (button.small) */
  --btn-h-base: 44px;  /* default — iOS HIG / WCAG 2.5.5 タップ最小 */
  --btn-h-lg: 48px;    /* form input 同等 */
  --btn-h-xl: 56px;    /* login primary CTA */
  --btn-h-2xl: 60px;   /* inventory.big tap-first */

  /* ----------------------------------------------------------------
   * z-index scale (= 縦軸 stacking order の単一情報源).
   * page 側で hardcode していた 9999 / 1000 / 100 等を駆逐し,
   * 後発 modal/toast を追加する時にどの層に積むか迷わなくする.
   * 値の間隔を空けて将来層追加に耐える設計.
   *   page sticky header  : 10
   *   sticky panels       : 50-80
   *   nav scrim           : 999  (sidebar の直下)
   *   nav sidebar         : 1000
   *   nav toggle button   : 1001
   *   modal backdrop      : 1100
   *   modal dialog        : 1110
   *   toast / SW banner   : 1200
   *   skip-link (focus)   : 9999 (= 常に最前)
   * ---------------------------------------------------------------- */
  --z-sticky: 10;
  --z-sticky-panel: 50;
  --z-fab: 80;
  --z-nav-scrim: 999;
  --z-nav: 1000;
  --z-nav-toggle: 1001;
  --z-modal-backdrop: 1100;
  --z-modal: 1110;
  --z-toast: 1200;
  --z-skip-link: 9999;

  /* Base color tokens (全 page 共通). 各 HTML page <style> で重複定義していた
   * 10 tokens を critical 化. page 固有 token (--critical / --warning / --refrig /
   * --p-color 等) は各 page の <style> 内に残す.
   * 同値で上書きされても visually 不変 (= 削除は段階的に進めても安全). */
  --bg: #0f1116;
  --panel: #181b22;
  --panel2: #1f232c;
  --line: #2a2f3a;
  --fg: #e6e8ee;
  --fg2: #b3b9c3;
  --accent: #6ea8ff;
  --warn: #ffb454;
  --danger: #ff6e6e;
  --ok: #6ee7a7;
}

/* a11y / battery: motion 設定を尊重. 各 HTML page <style> で 14 重複していた rule.
 * critical inline 化することで navigation の paint 直後から無効化が効く. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* High contrast 対応の focus 視覚化. 各 HTML page <style> で 14 重複していた rule. */
:focus-visible {
  outline: 3px solid #4a9eff;
  outline-offset: 2px;
}

/* Skip link (a11y) は visible focus 時のみ表示. 各 HTML page <style> で 14 重複. */
.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  z-index: var(--z-skip-link, 9999);
  background: #1a1408;
  color: #fff;
  padding: 8px 16px;
  text-decoration: none;
}
.skip-link:focus {
  left: 0;
}

/* iOS の青い tap highlight を全 page で抑止 + auto text-size up を suppress
 * (orientation 変更 / page 遷移 / standalone PWA 起動 時の text-size 揺らぎ防止) */
html {
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  /* base font-size を ここで lock (各 page の inline <style> が後で同値を再設定するが,
   * nav.css が cache 待ち中の brief moment でも 15px paint で済むよう保険として). */
  font-size: 15px;
}
body {
  font-size: 15px;
}

/* ----------------------------------------------------------------
 * Page nav
 * ---------------------------------------------------------------- */
.page-nav {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  /* header 内で flex-wrap=wrap でも先頭固定にして hamburger を左上に保つ */
  order: -1;
}

.nav-toggle-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 1px solid var(--line, var(--border, #4a4a4a));
  color: var(--fg, #e6e8ee);
  font-size: 1.4em;
  line-height: 1;
  padding: 6px 12px;
  border-radius: var(--radius-md);
  cursor: pointer;
  min-width: 44px;
  min-height: 44px;
  font-family: inherit;
  /* Sidebar open 時に z-index で常に sidebar より上, ボタン位置は body push に追従 */
  position: relative;
  z-index: var(--z-nav-toggle, 1001);
  transition: background var(--duration-fast) var(--ease-standard),
              border-color var(--duration-fast) var(--ease-standard),
              transform var(--duration-fast) var(--ease-standard);
}
.nav-toggle-btn:focus-visible {
  outline: 3px solid var(--accent, #6ea8ff);
  outline-offset: 2px;
}
.nav-toggle-btn:hover {
  background: var(--bg-elevated, rgba(255,255,255,0.08));
  border-color: var(--line-strong, rgba(255,255,255,0.18));
}
.nav-toggle-btn:active {
  transform: scale(0.96);
}

/* ----------------------------------------------------------------
 * Sidebar body: 左側 fixed column. 閉じている時は左にスライドアウト.
 * ---------------------------------------------------------------- */
.nav-menu {
  list-style: none;
  margin: 0;
  padding: 60px 8px 16px;
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  gap: 4px;

  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 280px;
  z-index: var(--z-nav, 1000);
  overflow-y: auto;

  /* モダン: 半透明 + backdrop blur で背景うっすら透過. fallback は solid panel. */
  background: var(--panel, #181b22);
  border-right: 1px solid var(--line, #2a2f3a);
  box-shadow: var(--shadow-lg);

  /* default: 閉じた状態 (translateX(-100%)) */
  transform: translateX(-100%);
  transition: transform var(--duration-medium) var(--ease-standard);
  will-change: transform;
}

/* backdrop-filter 対応 browser のみ blur + 半透明 (Safari iOS 9+, Chrome 76+) */
@supports ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
  .nav-menu {
    background: rgba(24, 27, 34, 0.78);
    backdrop-filter: blur(18px) saturate(150%);
    -webkit-backdrop-filter: blur(18px) saturate(150%);
  }
}

/* <html> 要素に nav-sidebar-open が付いていれば sidebar を表示.
 * inline script (各 HTML head) で paint 前に class を付与するため FOUC なし. */
html.nav-sidebar-open .nav-menu {
  transform: translateX(0);
}

.nav-menu li { margin: 0; padding: 0; }
.nav-menu li + li {
  border-top: 1px solid var(--line, rgba(255,255,255,0.06));
}
.nav-menu a {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  text-decoration: none;
  color: var(--fg, #e6e8ee);
  background: transparent;
  border-radius: var(--radius-md);
  white-space: nowrap;
  min-height: 44px;
  /* color / background は initial paint で確定させたいので transition は背景のみ.
   * (color transition があると page 遷移時に default link 色 → final color の
   * フェード が見えて FOUC の原因になる.) */
  transition: background var(--duration-fast) var(--ease-standard),
              transform var(--duration-fast) var(--ease-standard);
}
.nav-menu a:hover {
  background: var(--bg-elevated, rgba(255,255,255,0.08));
}
.nav-menu a:active {
  transform: scale(0.98);
}
.nav-menu a:focus-visible {
  outline: 3px solid var(--accent, #6ea8ff);
  outline-offset: 2px;
}
.nav-menu a[aria-current="page"] {
  background: var(--accent, #6ea8ff);
  color: #0b1120;
  font-weight: 600;
  box-shadow: 0 2px 6px rgba(110, 168, 255, 0.35);
}

/* ----------------------------------------------------------------
 * PC / iPad横 (>=768px): sidebar open 時に body content を 280px 右に push.
 * 重要: transition は selector の外 (body 直接) に置く. 旧版は
 * `html.nav-sidebar-open body` のみに transition を当てていたため
 * close 時 (class 削除時) transition が消えて jump cut していた.
 * 新版は body の transition を常時保持し, padding-left の変化を双方向に animate.
 * ---------------------------------------------------------------- */
@media (min-width: 768px) {
  body {
    transition: padding-left var(--duration-medium) var(--ease-standard);
  }
  html.nav-sidebar-open body {
    padding-left: 280px;
  }
}

/* ----------------------------------------------------------------
 * No-transitions: 初回 paint で transition + animation を完全に抑止
 * (jitter / ぴょこぴょこ 防止). inline script が html.no-transitions を付与
 * → window.load + setTimeout 50ms 後に解除.
 * ---------------------------------------------------------------- */
html.no-transitions,
html.no-transitions *,
html.no-transitions *::before,
html.no-transitions *::after {
  transition: none !important;
  animation-duration: 0.001s !important;
  animation-delay: 0s !important;
  scroll-behavior: auto !important;
}

/* ----------------------------------------------------------------
 * Mobile (<768px): overlay モード時の scrim + body scroll lock.
 * scrim は backdrop-filter で blur. tap で close (nav.js).
 * ---------------------------------------------------------------- */
.nav-scrim {
  display: none;
}
@media (max-width: 767.98px) {
  /* iOS Home Bar が sidebar 末尾リンクに被らないよう bottom padding に safe-area を加算 */
  .nav-menu {
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }
  html.nav-sidebar-open .nav-scrim {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: var(--z-nav-scrim, 999); /* nav-menu (--z-nav: 1000) の下, page content の上 */
    animation: nav-scrim-fade-in var(--duration-normal) var(--ease-standard);
  }
  @supports ((backdrop-filter: blur(1px)) or (-webkit-backdrop-filter: blur(1px))) {
    html.nav-sidebar-open .nav-scrim {
      backdrop-filter: blur(5px);
      -webkit-backdrop-filter: blur(5px);
    }
  }
  html.nav-sidebar-open body {
    overflow: hidden;
  }
}

@keyframes nav-scrim-fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* ----------------------------------------------------------------
 * prefers-reduced-motion: 全アニメ無効化.
 * ---------------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  .nav-menu { transition: none; }
  body { transition: none; }
  html.nav-sidebar-open body { transition: none; }
  .nav-scrim { animation: none; }
  .nav-toggle-btn,
  .nav-menu a {
    transition: none;
  }
}

/* ----------------------------------------------------------------
 * admin-only リンク / 要素は default 非表示. JS (applyAdminUI) が
 * html.user-is-admin を付与した時のみ visible にする.
 * .admin-only は <li> / <a> / <td> / <tr> / <th> / <div> / <section> など
 * 多様な要素で使われるため revert で元の display 値に戻す.
 * ---------------------------------------------------------------- */
.admin-only {
  display: none;
}
html.user-is-admin .admin-only {
  display: revert;
}
/* ----------------------------------------------------------------
 * Global buy chip: 全 page 右下に常駐する 🛒 chip.
 * localStorage.kitchen_shop_prefill_v1 の items 数を badge 表示, 0 件 / shopping
 * page では nav.js が display:none 制御. z-index は drawer(1000)/scrim(999) 未満.
 * ---------------------------------------------------------------- */
.global-cart-chip {
  position: fixed;
  bottom: max(16px, env(safe-area-inset-bottom));
  right: max(16px, env(safe-area-inset-right));
  z-index: 990; /* nav scrim (999) の下に位置. sidebar 開放時にも見える方が UX 良い */
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: var(--btn-h-base);
  padding: 0 16px 0 14px;
  background: var(--accent, #6ea8ff);
  color: #0b1120;
  text-decoration: none;
  font-weight: 600;
  font-family: inherit;
  font-size: 0.95em;
  line-height: 1;
  border-radius: var(--radius-pill);
  box-shadow: var(--shadow-lg);
  transition: background var(--duration-fast) var(--ease-standard),
              transform var(--duration-fast) var(--ease-standard),
              box-shadow var(--duration-fast) var(--ease-standard);
}
.global-cart-chip .cart-icon {
  font-size: 1.25em;
  line-height: 1;
}
.global-cart-chip .cart-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 22px;
  height: 22px;
  padding: 0 7px;
  background: rgba(11, 17, 32, 0.92);
  color: var(--accent, #6ea8ff);
  border-radius: var(--radius-pill);
  font-size: 0.85em;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.global-cart-chip:hover {
  background: var(--accent-hover, #84b8ff);
  box-shadow: var(--shadow-xl);
}
.global-cart-chip:active {
  transform: scale(0.96);
}
.global-cart-chip:focus-visible {
  outline: 3px solid var(--accent, #6ea8ff);
  outline-offset: 3px;
}
@media (prefers-reduced-motion: reduce) {
  .global-cart-chip { transition: none; }
}

/* CRITICAL-END
 * ここまでが server.js が抽出して各 HTML <head> に inline 注入する範囲.
 * 以下の rule は async load される nav.css 本体でのみ提供されるため,
 * 初回 paint 直後の brief moment では適用されない (hover / focus / modal
 * entrance 等, 初期 paint に効かなくても問題ない rule のみ).
 * ================================================================ */

/* AI recipe generation: v68 で floating chip 廃止. sidebar menu link を直接
 * 書き換える方式に変更. nav.js が a[href^="/recipe-builder.html"] の textContent
 * + href を state に応じて更新する. CSS 不要. 既存 chip DOM が残っていたら
 * 非表示にして deploy 過渡期の二重表示を防ぐ. */
.ai-chip { display: none !important; }

/* ----------------------------------------------------------------
 * Universal element refresh (モダン UI primitives).
 * page-level <style> を上書きしない控えめな base styling.
 * ---------------------------------------------------------------- */

/* Button: focus / press feedback の base. page 個別の background は維持. */
button {
  transition: background var(--duration-fast) var(--ease-standard),
              border-color var(--duration-fast) var(--ease-standard),
              transform var(--duration-fast) var(--ease-standard),
              box-shadow var(--duration-fast) var(--ease-standard);
}
button:focus-visible {
  outline: 3px solid var(--accent, #6ea8ff);
  outline-offset: 2px;
}
button:active:not([disabled]) {
  transform: scale(0.97);
}
/* button[disabled]: opacity:0.55 では文字 contrast が WCAG 4.5:1 を満たさないため,
 * 明示的に disabled background + 暗めの fg を使用. opacity は 1 に固定して
 * テキストの可読性を確保. */
button[disabled],
button[aria-disabled="true"] {
  cursor: not-allowed;
  opacity: 1;
  background: var(--done, #3b4150);
  color: var(--fg2, #b3b9c3);
  border-color: var(--line, #2a2f3a);
}

/* input/select/textarea: focus ring 統一. background / border は page 個別を尊重. */
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 3px solid var(--accent, #6ea8ff);
  outline-offset: 1px;
}

/* iOS Safari は input の computed font-size が 16px 未満だと focus 時に
 * 自動で zoom する. UX 上の trap なので, 全ての入力要素に min 16px を強制.
 * max() を使い既に > 16px の page 個別 rule を尊重 (例 1em ≒ 15px). */
input,
select,
textarea {
  font-size: max(16px, 1em);
}

/* Link focus ring */
a:focus-visible {
  outline: 3px solid var(--accent, #6ea8ff);
  outline-offset: 2px;
  border-radius: var(--radius-xs);
}

/* Modal entrance animation (ui.js の dialog backdrop で使用) */
@keyframes modal-fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes modal-scale-in {
  from { opacity: 0; transform: scale(0.96); }
  to { opacity: 1; transform: scale(1); }
}

/* Toast slide+fade entrance (ui.js Toast / undo-toast 等). */
@keyframes toast-slide-in {
  from { opacity: 0; transform: translateY(16px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* (旧 main page-fade-in animation は削除. opacity:0 → 1 + translateY のせいで
 *  iOS で reflow + auto text-size-adjust が走り「文字が一瞬大きく→落ち着く」
 *  という FOUC 体験を生んでいた. 静かに paint する方が UX 良好.) */

/* R19.2 Team 7 audit fix B3: SW update banner CSS を nav.css に 移動 (= 全 page 共通).
 * 元 library.css のみ → inventory.html / settings.html 等 で 効かなかった問題 解消.
 * library.css にも 重複定義残置 (= backward compat). */
.sw-update-banner {
  position: fixed;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  z-index: var(--z-toast, 1200);
  background: var(--accent, #f59e0b);
  color: #0b1120;
  padding: 10px 14px;
  border-radius: 10px;
  box-shadow: 0 6px 18px rgba(0,0,0,0.4);
  display: none;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  font-size: 14px;
  max-width: 90vw;
}
.sw-update-banner:not([hidden]) {
  display: flex;
}
.sw-update-banner-btn {
  background: #0b1120;
  color: var(--accent, #f59e0b);
  border: none;
  border-radius: 6px;
  padding: 6px 14px;
  cursor: pointer;
  font-weight: 700;
  min-height: 36px;
}
.sw-update-banner-btn:hover { opacity: 0.9; }
.sw-update-banner-close {
  background: transparent;
  border: none;
  color: #0b1120;
  cursor: pointer;
  font-size: 16px;
  font-weight: 700;
  padding: 4px 6px;
  min-height: 32px;
}
.sw-update-banner-close:focus-visible,
.sw-update-banner-btn:focus-visible {
  outline: 3px solid #0b1120;
  outline-offset: 2px;
}
