/* ============================================================
   ★ 技術解説: CSS Anchor Positioning + Popover API
   ------------------------------------------------------------
   このデモの主役。アンカー要素と対象要素を CSS だけで連結し、
   popover API と組み合わせて JS ゼロでメニュー位置を決める。
   ============================================================ */

/* (1) アンカー元の宣言
   anchor-name で名前を付けると、その要素を別要素から参照できる
    popoverの場合は省略可能

    */
.theme-button {
  anchor-name: --theme-button-anchor;
}

/* (2) アンカー先の配置
   - position-anchor: 参照するアンカー名
   - inset の anchor(): アンカーの bottom / right を座標として参照
   - position-try-fallbacks: 画面端で衝突したら自動で反転 */
.theme-menu {
  position-anchor: --theme-button-anchor;
  top: calc(anchor(bottom) + 8px);
  right: anchor(right);
  /* popover の UA スタイルは inset: 0 を持つ。使わない辺を auto に戻さないと
     left: 0 / bottom: 0 が残り、over-constrained で right / top が無視される */
  bottom: auto;
  left: auto;
  position-try-fallbacks: flip-block;
}

/* (3) popover の開閉アニメーション
   display / overlay は離散プロパティだが、allow-discrete + @starting-style
   で開いた瞬間のフェードイン、閉じる際のフェードアウトを実現 */
.theme-menu {
  opacity: 0;
  transform: translateY(-40px);
  transition: opacity 0.15s var(--ease-liquid),
  transform 0.15s var(--ease-liquid),
  display 0.15s var(--ease-liquid) allow-discrete,
  overlay 0.15s var(--ease-liquid) allow-discrete;
}

.theme-menu:popover-open {
  opacity: 1;
  transform: translateY(0);

  @starting-style {
    opacity: 0;
    transform: translateY(-40px);
  }
}

/* ============================================================
   以下、装飾スタイル
   （リセット / デザイントークン / Liquid Glass の質感など、
   　Anchor Positioning とは直接関係のない見た目の調整）
   ============================================================ */

/* ---------- Design Tokens (Light Theme) ---------- */
:root {
  --liquid-primary: oklch(0.787 0.158 79);

  --text-primary: oklch(0.267 0.029 256);
  --text-secondary: oklch(0.406 0.013 255);
  --text-tertiary: oklch(0.461 0.014 254);

  --bg-primary: oklch(0.954 0.008 240);
  --bg-gradient-1: oklch(0.93 0.04 240 / 0.6);
  --bg-gradient-2: oklch(0.95 0.055 160 / 0.3);
  --bg-orb-1: oklch(0.787 0.158 79 / 0.14);

  --header-bg: oklch(0.97 0.004 240 / 0.62);
  --header-hairline: rgba(0, 0, 0, 0.08);

  --nav-pill-bg: rgba(255, 255, 255, 0.32);
  --nav-pill-border: rgba(255, 255, 255, 0.5);
  --active-pill-bg: rgba(255, 255, 255, 0.95);

  --glass-bg-strong: rgba(255, 255, 255, 0.85);
  --glass-border: rgba(255, 255, 255, 0.8);
  --glass-card-bg: rgba(255, 255, 255, 0.5);

  --shadow-rest: 0 2px 8px rgba(0, 0, 0, 0.06), 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow-elevated: 0 10px 30px -5px rgba(0, 0, 0, 0.1),
  0 4px 8px -2px rgba(0, 0, 0, 0.04);

  --radius-sm: 12px;
  --radius-md: 20px;
  --radius-full: 9999px;

  --space-2xs: 0.375rem;
  --space-xs: 0.5rem;
  --space-sm: 1rem;
  --space-md: 1.5rem;
  --space-lg: 2rem;

  --ease-liquid: cubic-bezier(0.4, 0, 0.2, 1);
}

/* ---------- Reset ---------- */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  min-height: 100dvh;
  background: var(--bg-primary);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
}

/* ---------- Focus Ring ---------- */
:focus-visible {
  outline: 2px solid var(--liquid-primary);
  outline-offset: 4px;
  border-radius: var(--radius-sm);
}

/* Ambient Mesh Background */
body::before {
  content: "";
  position: fixed;
  z-index: -1;
  inset: 0;
  background: radial-gradient(
    ellipse at 20% 20%,
    var(--bg-gradient-1) 0,
    transparent 50%
  ),
  radial-gradient(ellipse at 80% 10%, var(--bg-orb-1) 0, transparent 45%),
  radial-gradient(
    ellipse at 90% 80%,
    var(--bg-gradient-2) 0,
    transparent 50%
  ),
  var(--bg-primary);
}

/* ---------- Header (Liquid Glass) ---------- */
.header {
  position: fixed;
  z-index: 100;
  inset: 0 0 auto 0;
  border-bottom: 1px solid var(--header-hairline);
  backdrop-filter: blur(24px) saturate(180%);
  background: var(--header-bg);
}

.header-inner {
  display: flex;
  max-width: 1400px;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-sm) var(--space-lg);
  margin: 0 auto;
  gap: var(--space-lg);
}

/* ---------- Logo ---------- */
.logo {
  display: flex;
  align-items: center;
  gap: var(--space-xs);
  color: inherit;
  text-decoration: none;
}

.avatar {
  display: block;
  overflow: hidden;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--liquid-primary);
  box-shadow: var(--shadow-rest);
}

.avatar img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* ---------- Nav Pill ---------- */
.nav {
  display: flex;
  align-items: center;
  padding: 4px;
  border: 1px solid var(--nav-pill-border);
  border-radius: var(--radius-full);
  background: var(--nav-pill-bg);
  box-shadow: var(--shadow-rest);
}

.nav-link {
  padding: var(--space-xs) var(--space-md);
  border-radius: var(--radius-full);
  color: var(--text-tertiary);
  font-size: 0.875rem;
  font-weight: 500;
  text-decoration: none;
  transition: background 0.2s var(--ease-liquid),
  color 0.2s var(--ease-liquid);
}

.nav-link:hover {
  background: var(--glass-card-bg);
  color: var(--text-primary);
}

.nav-link.active {
  background: var(--active-pill-bg);
  box-shadow: var(--shadow-rest);
  color: var(--text-primary);
  font-weight: 700;
}

/* ---------- Actions ---------- */
.actions {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
}

/* ---------- Theme Toggle Button (見た目部分) ---------- */
.theme-button {
  display: grid;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 1px solid var(--nav-pill-border);
  border-radius: var(--radius-full);
  backdrop-filter: blur(16px) saturate(180%);
  background: var(--nav-pill-bg);
  box-shadow: var(--shadow-rest);
  cursor: pointer;
  place-items: center;
  transition: background 0.2s var(--ease-liquid);
}

.theme-button:hover {
  background: var(--active-pill-bg);
}

/* ---------- Theme Menu (見た目部分) ---------- */
.theme-menu {
  width: max-content;
  min-width: 180px;
  padding: 4px;
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-md);
  margin: 0;
  backdrop-filter: blur(12px);
  background: var(--glass-bg-strong);
  box-shadow: var(--shadow-elevated);
  list-style: none;
}

.menu-item {
  display: flex;
  width: 100%;
  align-items: center;
  padding: 10px 12px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 500;
  gap: 10px;
  transition: background 0.15s var(--ease-liquid),
  color 0.15s var(--ease-liquid);
}

.menu-item:hover {
  background: var(--active-pill-bg);
  color: var(--text-primary);
}

.menu-icon {
  flex-shrink: 0;
  opacity: 0.7;
}

.menu-label {
  flex: 1;
  text-align: left;
}

.check-icon {
  flex-shrink: 0;
}
