/* ==========================================================================
   Base: reset, shell, sidebar, topbar, command palette, primitives
   ========================================================================== */

*, *::before, *::after { box-sizing: border-box; }

/* The hidden attribute must win over any display rule below. */
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font-family: var(--sans);
  font-size: var(--fs-base);
  line-height: var(--lh-body);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

h1, h2, h3, h4 { margin: 0; font-weight: 600; line-height: var(--lh-tight); }
p { margin: 0 0 var(--s4); }
p:last-child { margin-bottom: 0; }
ul, ol { margin: 0; padding: 0; }
a { color: inherit; }
button { font: inherit; color: inherit; }
input, select, textarea { font: inherit; color: inherit; }
svg { display: block; }

:focus-visible {
  outline: 2px solid var(--emerald);
  outline-offset: 2px;
  border-radius: 3px;
}
.sidebar :focus-visible, .topbar :focus-visible { outline-color: var(--warm-yellow); }

.skip-link {
  position: absolute; left: -9999px; top: 0; z-index: 200;
  background: var(--deep); color: var(--ink-invert);
  padding: var(--s3) var(--s4); border-radius: 0 0 var(--radius) 0;
  text-decoration: none;
}
.skip-link:focus { left: 0; }

/* ------------------------------------------------------------------ shell */
.shell {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  grid-template-rows: var(--topbar-h) minmax(0, 1fr);
  grid-template-areas: "brand top" "side main";
  min-height: 100vh;
  min-height: 100dvh;
}

/* ----------------------------------------------------------------- topbar */
.topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: var(--s4);
  padding: 0 var(--s5) 0 var(--s5);
  background: var(--warm-white);
  border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 40;
}

.brand {
  grid-area: brand;
  display: flex; align-items: center; gap: 10px;
  background: var(--deep);
  color: var(--ink-invert);
  height: var(--topbar-h);
  padding: 0 var(--s4);
  text-decoration: none;
  position: sticky; top: 0; z-index: 41;
}
.brand-mark svg { overflow: visible; }
.brand-mark .m1 { fill: rgba(243,240,230,0.32); }
.brand-mark .m2 { fill: rgba(243,240,230,0.55); }
.brand-mark .m3 { fill: var(--emerald-lo); }
.brand-mark .m4 { fill: var(--solar); }
.brand:hover .m4 { fill: var(--warm-yellow); }
.brand-mark rect, .brand-mark circle { transition: fill var(--t) var(--ease); }

.brand-words { display: flex; flex-direction: column; line-height: 1.06; }
.brand-1 {
  font-family: var(--sans); font-size: 0.625rem; letter-spacing: 0.19em;
  color: var(--ink-invert-muted); font-weight: 600;
}
.brand-2 {
  font-family: var(--serif); font-size: 0.9375rem; letter-spacing: 0.035em;
  font-weight: 600;
}

.search-trigger {
  flex: 1 1 auto;
  min-width: 0;
  max-width: 520px;
  display: flex; align-items: center; gap: var(--s3);
  height: 36px; padding: 0 var(--s3);
  background: var(--panel-alt);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--ink-muted);
  cursor: pointer;
  text-align: left;
  transition: border-color var(--t) var(--ease), background var(--t) var(--ease);
}
.search-trigger:hover { border-color: var(--line-strong); background: var(--sunk); }
.search-trigger-text {
  flex: 1; font-size: var(--fs-sm); white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.search-icon { fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; }

.kbd {
  font-family: var(--sans); font-size: 0.6875rem; font-weight: 600;
  color: var(--ink-muted);
  background: var(--panel);
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: var(--radius-sm);
  padding: 1px 5px;
  white-space: nowrap;
}

.topbar-right { margin-left: auto; display: flex; align-items: center; gap: var(--s4); }

.mode-toggle {
  display: inline-flex;
  background: var(--panel-alt);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 2px;
}
.mode-btn {
  border: 0; background: transparent; cursor: pointer;
  font-size: var(--fs-xs); font-weight: 600; letter-spacing: 0.04em;
  padding: 5px 12px; border-radius: 999px;
  color: var(--ink-muted);
  transition: background var(--t) var(--ease), color var(--t) var(--ease);
}
.mode-btn.is-active { background: var(--forest); color: var(--warm-yellow); }
.mode-btn:not(.is-active):hover { color: var(--ink); }

.user-slot { display: flex; align-items: center; gap: var(--s3); font-size: var(--fs-sm); }
.user-chip {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 4px 10px 4px 5px; border-radius: 999px;
  background: var(--panel-alt); border: 1px solid var(--line);
}
.avatar {
  width: 22px; height: 22px; border-radius: 50%;
  background: var(--forest); color: var(--warm-yellow);
  display: grid; place-items: center;
  font-size: 0.625rem; font-weight: 700; letter-spacing: 0.02em;
}
.role-tag {
  font-size: 0.625rem; letter-spacing: 0.09em; text-transform: uppercase;
  color: var(--ink-muted); font-weight: 700;
}

.icon-btn {
  display: none;
  border: 1px solid var(--line); background: var(--panel);
  border-radius: var(--radius); width: 36px; height: 36px;
  cursor: pointer; place-items: center;
}
.burger, .burger::before, .burger::after {
  display: block; width: 16px; height: 1.6px; background: var(--ink); border-radius: 2px;
  transition: transform var(--t) var(--ease), opacity var(--t) var(--ease);
}
.burger { position: relative; }
.burger::before, .burger::after { content: ""; position: absolute; left: 0; }
.burger::before { top: -5px; }
.burger::after  { top: 5px; }
[aria-expanded="true"] .burger { background: transparent; }
[aria-expanded="true"] .burger::before { transform: translateY(5px) rotate(45deg); }
[aria-expanded="true"] .burger::after  { transform: translateY(-5px) rotate(-45deg); }

/* ---------------------------------------------------------------- sidebar */
.sidebar {
  grid-area: side;
  background: var(--deep);
  color: var(--ink-invert);
  position: sticky; top: var(--topbar-h);
  height: calc(100vh - var(--topbar-h));
  height: calc(100dvh - var(--topbar-h));
  overflow-y: auto;
  z-index: 39;
}
.sidebar-inner {
  display: flex; flex-direction: column; justify-content: space-between;
  min-height: 100%;
  padding: var(--s5) 0 var(--s5);
}
.nav { list-style: none; }
.nav a {
  display: flex; align-items: baseline; gap: 10px;
  padding: 9px var(--s5);
  text-decoration: none;
  color: rgba(243, 240, 230, 0.76);
  font-size: var(--fs-sm);
  border-left: 2px solid transparent;
  transition: color var(--t) var(--ease), background var(--t) var(--ease), border-color var(--t) var(--ease);
}
.nav a:hover { color: var(--ink-invert); background: rgba(255, 255, 255, 0.045); }
.nav a.is-active {
  color: var(--warm-yellow);
  background: rgba(255, 255, 255, 0.06);
  border-left-color: var(--solar);
}
.nav-num {
  font-family: var(--mono); font-size: 0.625rem; letter-spacing: 0.06em;
  color: rgba(243, 240, 230, 0.36);
  min-width: 16px;
}
.nav a.is-active .nav-num { color: var(--solar); }

.nav-foot { padding: 0 var(--s5); }
.eco {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 11px; margin-bottom: var(--s3);
  border: 1px solid var(--line-dark); border-radius: var(--radius);
  text-decoration: none; color: var(--ink-invert);
  font-size: var(--fs-sm); font-weight: 500;
  transition: border-color var(--t) var(--ease), background var(--t) var(--ease), transform var(--t) var(--ease);
}
.eco:hover { border-color: rgba(255,255,255,0.3); background: rgba(255,255,255,0.05); transform: translateY(-1px); }
.eco-dot { width: 7px; height: 7px; border-radius: 50%; background: #5B9BD5; flex: none; }
.eco-label { flex: 1; }
.eco-arrow { color: var(--ink-invert-muted); font-size: var(--fs-sm); }

.nav-minor {
  display: block; padding: 5px 0;
  font-size: var(--fs-xs); color: var(--ink-invert-muted); text-decoration: none;
  transition: color var(--t) var(--ease);
}
.nav-minor:hover { color: var(--ink-invert); }

.ecosystem {
  margin: var(--s5) 0 0; padding-top: var(--s4);
  border-top: 1px solid var(--line-dark);
  display: flex; flex-direction: column; gap: 4px;
}
.ecosystem-kicker {
  font-size: 0.5938rem; letter-spacing: 0.16em; text-transform: uppercase;
  color: var(--solar); font-weight: 700;
}
.ecosystem-pair { font-size: var(--fs-xs); color: var(--ink-invert-muted); line-height: 1.5; }
.ecosystem-pair b { color: rgba(243,240,230,0.9); font-weight: 600; }

.sidebar-scrim {
  position: fixed; inset: 0; background: rgba(9, 43, 36, 0.42);
  z-index: 38; border: 0;
}

/* ------------------------------------------------------------------- main */
.main { grid-area: main; min-width: 0; }
.main:focus { outline: none; }
.view { padding: var(--s7) var(--s7) var(--s9); max-width: 1180px; }

/* -------------------------------------------------------------- primitives */
.kicker {
  font-size: 0.6875rem; letter-spacing: 0.14em; text-transform: uppercase;
  font-weight: 700; color: var(--emerald);
}
.page-title {
  font-family: var(--serif); font-size: var(--fs-2xl); letter-spacing: -0.012em;
  margin: var(--s3) 0 var(--s4); font-weight: 600;
}
.page-lede {
  font-size: var(--fs-md); color: var(--ink-soft); max-width: var(--measure);
  line-height: 1.55;
}
.section-title {
  font-family: var(--serif); font-size: var(--fs-lg); font-weight: 600;
  margin: 0 0 var(--s4);
}
.rule { border: 0; border-top: 1px solid var(--line); margin: var(--s7) 0; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: var(--s5);
}
a.card, button.card {
  display: block; text-align: left; width: 100%; cursor: pointer;
  text-decoration: none;
  transition: transform var(--t) var(--ease), box-shadow var(--t) var(--ease), border-color var(--t) var(--ease);
}
a.card:hover, button.card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow);
  border-color: var(--line-strong);
}
.card-title { font-family: var(--serif); font-size: var(--fs-md); font-weight: 600; margin-bottom: 6px; }
.card-sub { font-size: var(--fs-sm); color: var(--ink-muted); line-height: 1.5; }

.grid { display: grid; gap: var(--s4); }
.grid-2 { grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fill, minmax(258px, 1fr)); }

.tag {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: var(--fs-xs); font-weight: 600; letter-spacing: 0.02em;
  padding: 2px 8px; border-radius: 999px;
  background: var(--sunk); color: var(--ink-soft);
  border: 1px solid var(--line);
}
.tag-row { display: flex; flex-wrap: wrap; gap: 6px; }

.fit {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: var(--fs-xs); font-weight: 700; letter-spacing: 0.03em;
  padding: 2px 9px; border-radius: 999px;
  color: var(--fit-ink, var(--unknown));
  background: var(--fit-wash, var(--unknown-wash));
  border: 1px solid color-mix(in srgb, var(--fit-ink, var(--unknown)) 26%, transparent);
  white-space: nowrap;
}
.fit::before {
  content: ""; width: 6px; height: 6px; border-radius: 50%;
  background: currentColor; flex: none;
}

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 9px 16px; border-radius: var(--radius);
  border: 1px solid var(--forest); background: var(--forest); color: var(--ink-invert);
  font-size: var(--fs-sm); font-weight: 600; cursor: pointer; text-decoration: none;
  transition: background var(--t) var(--ease), transform var(--t) var(--ease), box-shadow var(--t) var(--ease);
}
.btn:hover { background: var(--emerald); border-color: var(--emerald); transform: translateY(-1px); box-shadow: var(--shadow-sm); }
.btn-accent { background: var(--solar); border-color: var(--solar); color: var(--accent-ink); }
.btn-accent:hover { background: var(--warm-yellow); border-color: var(--warm-yellow); color: var(--accent-ink); }
.btn-ghost { background: transparent; color: var(--ink); border-color: var(--line-strong); }
.btn-ghost:hover { background: var(--panel-alt); border-color: var(--ink-muted); color: var(--ink); }
.btn-sm { padding: 6px 12px; font-size: var(--fs-xs); }
.btn[disabled] { opacity: 0.5; cursor: not-allowed; transform: none; }

.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: var(--s4); }
.field label { font-size: var(--fs-xs); font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; color: var(--ink-muted); }
.field input, .field select, .field textarea {
  padding: 9px 11px; border: 1px solid var(--line-strong); border-radius: var(--radius);
  background: var(--panel); font-size: var(--fs-base);
  transition: border-color var(--t) var(--ease), box-shadow var(--t) var(--ease);
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none; border-color: var(--emerald);
  box-shadow: 0 0 0 3px rgba(23, 118, 93, 0.14);
}

.notice {
  border-left: 3px solid var(--accent);
  background: var(--accent-wash);
  padding: var(--s4) var(--s5);
  border-radius: 0 var(--radius) var(--radius) 0;
  font-size: var(--fs-sm); line-height: 1.55;
}
.notice-warn { border-left-color: var(--warn); background: var(--warn-wash); }
.notice-quiet { border-left-color: var(--line-strong); background: var(--panel-alt); color: var(--ink-soft); }

.empty { color: var(--ink-muted); font-size: var(--fs-sm); padding: var(--s5) 0; }

.loading {
  display: inline-block; width: 15px; height: 15px;
  border: 2px solid var(--line-strong); border-top-color: var(--emerald);
  border-radius: 50%; animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .loading { animation-duration: 2s; } }

/* ------------------------------------------------------ command palette */
.palette { position: fixed; inset: 0; z-index: 100; display: grid; place-items: start center; }
.palette-backdrop { position: absolute; inset: 0; background: rgba(9, 43, 36, 0.34); backdrop-filter: blur(2px); }
.palette-box {
  position: relative; margin-top: 9vh; width: min(660px, calc(100vw - 32px));
  background: var(--panel); border: 1px solid var(--line-strong);
  border-radius: var(--radius-lg); box-shadow: var(--shadow-lg);
  overflow: hidden;
  animation: palette-in var(--t-slow) var(--ease);
}
@keyframes palette-in { from { opacity: 0; transform: translateY(-8px) scale(0.99); } }
@media (prefers-reduced-motion: reduce) { .palette-box { animation: none; } }

.palette-input-row { display: flex; align-items: center; gap: var(--s3); padding: var(--s4) var(--s5); border-bottom: 1px solid var(--line); color: var(--ink-muted); }
#paletteInput { flex: 1; border: 0; outline: none; background: transparent; font-size: var(--fs-md); color: var(--ink); }
#paletteInput::-webkit-search-cancel-button { display: none; }
.palette-esc {
  border: 1px solid var(--line); background: var(--panel-alt); border-radius: var(--radius-sm);
  font-size: var(--fs-xs); padding: 3px 8px; cursor: pointer; color: var(--ink-muted);
}
.palette-results { max-height: min(58vh, 460px); overflow-y: auto; padding: var(--s2) 0; }
.palette-group-label {
  font-size: 0.625rem; letter-spacing: 0.13em; text-transform: uppercase; font-weight: 700;
  color: var(--ink-muted); padding: var(--s3) var(--s5) 4px;
}
.palette-item {
  display: flex; align-items: baseline; gap: var(--s3);
  width: 100%; text-align: left; border: 0; background: transparent; cursor: pointer;
  padding: 8px var(--s5); text-decoration: none;
}
.palette-item:hover, .palette-item.is-cursor { background: var(--panel-alt); }
.palette-item.is-cursor { box-shadow: inset 2px 0 0 var(--solar); }
.palette-item-title { font-weight: 600; font-size: var(--fs-sm); }
.palette-item-sub {
  font-size: var(--fs-xs); color: var(--ink-muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; min-width: 0;
}
.palette-foot {
  display: flex; gap: var(--s5); padding: var(--s3) var(--s5);
  border-top: 1px solid var(--line); background: var(--panel-alt);
  font-size: var(--fs-xs); color: var(--ink-muted);
}
.palette-foot span { display: inline-flex; align-items: center; gap: 5px; }

/* ------------------------------------------------------ simple/expert mode */
body[data-mode="simple"] [data-level="expert"] { display: none; }

/* ------------------------------------------------------------- responsive */
@media (max-width: 1080px) {
  .view { padding: var(--s6) var(--s5) var(--s8); }
}

@media (max-width: 880px) {
  .shell { grid-template-columns: minmax(0, 1fr); grid-template-areas: "top" "main"; }
  .icon-btn { display: grid; }
  .brand { position: static; background: transparent; color: var(--ink); height: auto; padding: 0; }
  .brand-1 { color: var(--ink-muted); }
  .brand-mark .m1 { fill: rgba(18, 60, 50, 0.28); }
  .brand-mark .m2 { fill: rgba(18, 60, 50, 0.5); }
  .brand-mark .m3 { fill: var(--emerald); }
  .topbar { padding: 0 var(--s4); gap: var(--s3); }
  .search-trigger-text { display: none; }
  .search-trigger { max-width: none; flex: 0 0 auto; width: 36px; justify-content: center; padding: 0; }
  #searchKbd { display: none; }
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; width: min(300px, 84vw);
    height: 100%; transform: translateX(-102%);
    transition: transform var(--t-slow) var(--ease);
    box-shadow: var(--shadow-lg);
  }
  .shell.nav-open .sidebar { transform: translateX(0); }
  .sidebar-inner { padding-top: var(--s6); }
  .view { padding: var(--s5) var(--s4) var(--s8); }
  .page-title { font-size: var(--fs-xl); }
}

/* The detail-level toggle stays reachable on tablets; it only leaves the
   top bar on phones, where Settings still exposes it. */
@media (max-width: 620px) {
  .topbar .mode-toggle { display: none; }
}

@media (max-width: 420px) {
  .view { padding: var(--s4) var(--s4) var(--s8); }
  .brand-2 { font-size: 0.8125rem; }
  .user-slot .role-tag { display: none; }
}
