/* Final Chapter — app shell.
   Phone: top bar (title + mini character) · content · bottom nav.
   Desktop (≥900px): left nav · content · character panel always visible on the right. */

.shell { min-height: 100dvh; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.topbar-titles { flex: 1; min-width: 0; }
.topbar-brand { display: block; font-family: var(--font-hand); font-size: 1.05rem; line-height: 1; color: var(--muted); }
.topbar-title { font-size: 1.3rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.buddy-mini {
  display: flex; align-items: center; gap: 8px;
  padding: 4px 10px 4px 4px; border: 1px solid var(--line); border-radius: 999px;
  background: var(--surface); cursor: pointer;
}
.buddy-mini-avatar {
  width: 40px; height: 40px; border-radius: 50%; overflow: hidden; flex: none;
  background: var(--surface-2); display: grid; place-items: center; font-size: 1.2rem;
}
.buddy-mini-avatar img { width: 100%; height: 100%; object-fit: cover; object-position: 50% 8%; }

/* ---------- content ---------- */
.view {
  padding: 18px 16px calc(96px + env(safe-area-inset-bottom));
  max-width: 1100px; width: 100%; margin: 0 auto; outline: none;
}

/* ---------- bottom nav (phone) ---------- */
.bottomnav {
  position: fixed; z-index: 40; left: 0; right: 0; bottom: 0;
  display: grid; grid-template-columns: repeat(5, 1fr);
  padding: 6px 6px calc(6px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}
.nav-link {
  display: grid; justify-items: center; gap: 2px; padding: 6px 2px;
  border-radius: 14px; text-decoration: none; color: var(--muted);
  font-size: .72rem; font-weight: 600;
}
.nav-link svg { width: 23px; height: 23px; }
.nav-link[aria-current="page"] { color: var(--c4); background: var(--c1); }

/* ---------- desktop ---------- */
.sidenav, .buddy-panel, .buddy-fab, .nav-collapse { display: none; }

@media (min-width: 900px) {
  .shell {
    display: grid;
    grid-template-columns: var(--side-w, 220px) minmax(0, 1fr);
    grid-template-rows: auto 1fr;
    grid-template-areas: "side top" "side view";
    transition: grid-template-columns .25s ease;
  }
  .topbar { grid-area: top; padding: 18px 28px 14px; }
  .topbar-brand, .buddy-mini { display: none; }
  .topbar-title { font-size: 1.7rem; }
  .view { grid-area: view; padding: 24px 28px 110px; max-width: 1640px; }
  .bottomnav { display: none; }
  .desk-hide { display: none !important; }

  .sidenav {
    grid-area: side; display: flex; flex-direction: column; gap: 22px;
    position: sticky; top: 0; height: 100dvh; padding: 22px 14px; overflow: hidden;
    background: var(--c1); color: var(--c4);
    --muted: color-mix(in srgb, var(--c4) 68%, transparent); --text: var(--c4); --line: color-mix(in srgb, var(--c4) 14%, transparent);
  }
  .sidenav .nav-link[aria-current="page"] { background: var(--c4); color: var(--c1); }
  .brand { display: grid; gap: 2px; padding: 0 8px; text-decoration: none; }
  .brand-title { font-family: var(--font-head); font-weight: var(--head-weight); font-size: 1.45rem; line-height: 1.05; white-space: nowrap; }
  .brand-sub { font-family: var(--font-hand); font-size: 1.05rem; line-height: 1.1; color: var(--muted); }
  .sidenav-links { display: grid; gap: 4px; }
  .sidenav .nav-link {
    grid-template-columns: 24px 1fr; justify-items: start; align-items: center; gap: 10px;
    padding: 10px 12px; font-size: .95rem; white-space: nowrap;
  }
  .sidenav .nav-link:hover { background: var(--line); color: var(--text); }
  .nav-collapse {
    display: grid; grid-template-columns: 24px 1fr; align-items: center; gap: 10px; margin-top: auto;
    padding: 10px 12px; border: 0; border-radius: 14px; background: none; color: var(--muted);
    font-weight: 600; font-size: .9rem; text-align: left; cursor: pointer; white-space: nowrap;
  }
  .nav-collapse:hover { background: var(--line); color: var(--text); }
  .nav-collapse svg { width: 22px; height: 22px; transition: transform .25s; }

  /* collapsed: icons only */
  .shell.is-collapsed { --side-w: 76px; }
  .is-collapsed .brand-sub, .is-collapsed .sidenav .nav-link span:last-child, .is-collapsed .nav-collapse span { display: none; }
  .is-collapsed .brand { padding: 0; }
  .is-collapsed .brand-title { font-size: 0; text-align: center; }
  .is-collapsed .brand-title::before { content: attr(data-short); font-size: 1.35rem; }
  .is-collapsed .sidenav .nav-link, .is-collapsed .nav-collapse { grid-template-columns: 1fr; justify-items: center; }
  .is-collapsed .nav-collapse svg { transform: rotate(180deg); }
  .is-collapsed .theme-quick-side .tq-ico { display: block; }

  /* character: round button bottom-right, opens a popup card */
  .buddy-fab {
    position: fixed; z-index: 50; right: 24px; bottom: 24px;
    display: flex; align-items: center; gap: 10px; padding: 6px 14px 6px 6px;
    border: 1px solid var(--line); border-radius: 999px; background: var(--surface);
    box-shadow: var(--shadow); cursor: pointer;
  }
  .buddy-fab:hover { transform: translateY(-1px); }
  .buddy-fab-avatar { width: 58px; height: 58px; border-radius: 50%; overflow: hidden; background: var(--surface-2); display: grid; place-items: center; font-size: 1.6rem; }
  .buddy-fab-avatar img { width: 100%; height: 100%; object-fit: cover; object-position: 50% 10%; }
  .buddy-fab .needs--mini { width: 64px; }
  .buddy-panel {
    position: fixed; z-index: 51; right: 24px; bottom: 100px; width: 300px; max-height: calc(100dvh - 130px); overflow: auto;
    padding: 16px; border: 1px solid var(--line); border-radius: 22px;
    background: var(--bg); background-image: var(--grain); box-shadow: 0 18px 50px -12px color-mix(in srgb, var(--c1) 45%, transparent);
    transform-origin: bottom right; animation: pop-in .18s cubic-bezier(.2, .9, .3, 1.2);
  }
  .buddy-panel.is-open { display: block; }
  .buddy-panel .buddy-stage { aspect-ratio: 4 / 4.2; }
}
@keyframes pop-in { from { opacity: 0; transform: scale(.92); } to { opacity: 1; transform: scale(1); } }

/* ---------- character panel (placeholder art until step 6) ---------- */
.buddy-stage {
  position: relative; aspect-ratio: 3 / 4; border-radius: 20px; overflow: hidden;
  background: var(--surface-2); border: 1px solid var(--line);
  display: grid; place-items: center; text-align: center;
}
.buddy-stage .placeholder { font-family: var(--font-hand); font-size: 1.3rem; color: var(--muted); padding: 16px; }
.buddy-name { margin: 14px 0 10px; font-family: var(--font-head); font-weight: var(--head-weight); font-size: 1.15rem; }
.buddy-body { position: absolute; left: 0; right: 0; bottom: 0; width: 100%; height: 100%; object-fit: contain; object-position: 50% 100%; }
.buddy-stage { aspect-ratio: 550 / 640; background: linear-gradient(180deg, var(--surface-2), color-mix(in srgb, var(--c3) 45%, var(--surface))); }
.buddy-stage--sheet { max-height: 52dvh; margin: 0 auto; }
.view-host { display: contents; }

/* 🎨 quick theme switch */
.theme-quick-top { font-size: 1.15rem; flex: none; }
.theme-quick-side .tq-ico { font-size: 1.1rem; text-align: center; }
.theme-quick-side { margin-top: auto; }
.theme-quick-side + .nav-collapse { margin-top: 0; }
@media (min-width: 900px) { .theme-quick-top { display: none; } }
.theme-pop {
  position: fixed; z-index: 120; display: flex; gap: 8px; padding: 10px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--line); box-shadow: 0 12px 30px -10px rgba(0, 0, 0, .35);
  animation: pop-in .15s ease-out;
}
.theme-pop button {
  width: 34px; height: 34px; border-radius: 50%; cursor: pointer; border: 2px solid #fff;
  background: linear-gradient(135deg, var(--a) 0 50%, var(--b) 50% 100%); box-shadow: 0 0 0 1px rgba(0, 0, 0, .15);
}
.theme-pop button[aria-checked="true"] { box-shadow: 0 0 0 3px var(--c1); }
