/* UI 2.0 (WP50): base, icons, buttons, shell, menus. Loaded after kit.css; values come from tokens.css. */

/* ---------- base ---------- */
html { font-family: var(--font-ui); font-feature-settings: "cv11", "ss01"; }
body {
  font-family: var(--font-ui);
  font-size: var(--fs-body);
  line-height: var(--lh-body);
  color: var(--text);
  background: var(--surface-page);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
::selection { background: var(--selection); }
* { scrollbar-width: thin; scrollbar-color: var(--scrollbar) transparent; }
:focus-visible { outline: none; box-shadow: var(--focus-ring); border-radius: var(--r-ctrl); }
/* Windows high contrast drops box-shadow, so the ring comes back as an outline there */
@media (forced-colors: active) { :focus-visible, :is(input, select, textarea):focus { outline: 2px solid Highlight !important; outline-offset: 1px; } }
a { color: var(--accent-text); text-underline-offset: 2px; }
h1, h2, h3 { color: var(--text); letter-spacing: var(--tracking-tight); line-height: var(--lh-tight); }
h1 { font-size: var(--fs-2xl); font-weight: 650; margin: 0 0 var(--sp-4); }
h2 { font-size: var(--fs-lg); font-weight: 600; }
kbd {
  font: 500 var(--fs-xs) / 1 var(--font-ui);
  padding: 2px 5px;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
  color: var(--text-secondary);
  background: var(--surface);
  white-space: nowrap;
}
.num, .kit-amount, td.num, th.num { font-variant-numeric: tabular-nums; }

/* ---------- icons (one Phosphor sprite) ---------- */
.icon { width: 16px; height: 16px; fill: currentColor; flex: none; vertical-align: -3px; }
.icon-lg { width: 20px; height: 20px; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: var(--h-control); padding: 0 var(--sp-3);
  font: 550 var(--fs-body) / 1 var(--font-ui);
  border-radius: var(--r-ctrl); border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text);
  text-decoration: none; white-space: nowrap; cursor: pointer;
  transition: background var(--dur-1) var(--ease-out), border-color var(--dur-1) var(--ease-out), transform var(--dur-1) var(--ease-out);
}
.btn:hover { background: var(--surface-hover); }
.btn:active { transform: translateY(1px); }
.btn-sm { height: 30px; padding: 0 10px; font-size: var(--fs-sm); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--text-on-accent); }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-secondary); }
.btn-ghost:hover { background: var(--surface-hover); color: var(--text); }
/* Loading: a button that started a request shows a spinner in place of its label until the answer comes back.
   htmx marks the requesting element or form (.htmx-request); plain form posts get aria-busy from shell.ts. */
button[aria-busy="true"], button.htmx-request, form.htmx-request button[type="submit"]:not(.menu-item), form.htmx-request [data-kit-save] {
  position: relative; color: transparent !important; pointer-events: none;
}
button[aria-busy="true"]::after, button.htmx-request::after, form.htmx-request button[type="submit"]:not(.menu-item)::after, form.htmx-request [data-kit-save]::after {
  content: ""; position: absolute; inset: 0; margin: auto; width: 14px; height: 14px;
  border: 2px solid var(--spin, var(--text-muted)); border-right-color: transparent; border-bottom-color: transparent;
  border-radius: 50%; animation: spin 0.7s linear infinite;
}
.btn-primary, button[type="submit"]:not(.btn):not(.menu-item) { --spin: var(--text-on-accent); }
.icon-btn {
  display: inline-grid; place-items: center; width: 32px; height: 32px; padding: 0;
  border: 0; border-radius: var(--r-ctrl); background: transparent; color: var(--text-muted); cursor: pointer;
  transition: background var(--dur-1) var(--ease-out), color var(--dur-1) var(--ease-out);
}
.icon-btn:hover { background: var(--surface-hover); color: var(--text); }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- progress bar (htmx requests) ---------- */
.progress {
  position: fixed; inset: 0 0 auto 0; height: 2px; z-index: var(--z-progress); pointer-events: none;
  background: var(--accent); transform-origin: left; transform: scaleX(0); opacity: 0;
}
.progress.is-loading { opacity: 1; animation: progress 1.4s var(--ease-out) forwards; }
@keyframes progress { 0% { transform: scaleX(0); } 60% { transform: scaleX(0.7); } 100% { transform: scaleX(0.92); } }

/* ---------- shell ---------- */
.shell { display: grid; grid-template-columns: var(--w-sidebar) minmax(0, 1fr); min-height: 100dvh; background: var(--surface-page); }
.shell.sidebar-collapsed { grid-template-columns: var(--w-rail) minmax(0, 1fr); }
.shell-main { display: flex; flex-direction: column; min-width: 0; }
.cb-main { flex: 1; min-width: 0; padding: var(--sp-6) var(--sp-8) var(--sp-10); background: transparent; overflow: visible; }

.sidebar {
  position: sticky; top: 0; height: 100dvh; z-index: var(--z-sidebar);
  display: flex; flex-direction: column; min-width: 0; width: auto;
  background: var(--surface-chrome);
  -webkit-backdrop-filter: saturate(160%) blur(18px); backdrop-filter: saturate(160%) blur(18px);
  border-right: 1px solid var(--border);
  overflow: hidden; user-select: none;
}
.sidebar-head { display: flex; align-items: center; gap: var(--sp-2); height: var(--h-topbar); padding: 0 var(--sp-3) 0 var(--sp-4); flex: none; }
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--text); min-width: 0; flex: 1; }
.brand-mark { width: 26px; height: 26px; border-radius: 7px; flex: none; }
.brand-name { font-size: var(--fs-md); font-weight: 650; letter-spacing: -0.02em; white-space: nowrap; }
.sidebar-scroll { flex: 1; overflow-y: auto; padding: var(--sp-1) var(--sp-2) var(--sp-4); }

.nav-list, .nav-sub { list-style: none; margin: 0; padding: 0; }
.nav-link {
  display: flex; align-items: center; gap: 10px; width: 100%;
  min-height: 32px; padding: 0 10px; border: 0; border-radius: var(--r-ctrl);
  background: transparent; color: var(--text-secondary);
  font: 500 var(--fs-body) / 1.2 var(--font-ui); text-align: left; text-decoration: none; cursor: pointer;
  transition: background var(--dur-1) var(--ease-out), color var(--dur-1) var(--ease-out);
}
.nav-link:hover { background: var(--surface-hover); color: var(--text); }
.nav-link[aria-current="page"] { background: var(--surface-selected); color: var(--accent-text); font-weight: 600; }
.nav-icon { width: 18px; height: 18px; color: var(--text-muted); }
.nav-link:hover .nav-icon, .nav-link[aria-current="page"] .nav-icon, .is-open > .nav-link .nav-icon { color: var(--text); }
.nav-text { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nav-caret { width: 12px; height: 12px; color: var(--text-faint); transition: transform var(--dur-2) var(--ease-out); }
.is-open > .nav-link .nav-caret { transform: rotate(90deg); }
.nav-item[data-level="0"] + .nav-item[data-level="0"] { margin-top: 1px; }
.nav-item:not(.is-open) > .nav-sub { display: none; }
.nav-item.is-open > .nav-sub { display: block; padding: 2px 0 4px; }
.nav-item[data-level="1"] > .nav-link { padding-left: 38px; min-height: 30px; font-size: var(--fs-sm); }
.nav-item[data-level="2"] > .nav-link { padding-left: 52px; min-height: 28px; font-size: var(--fs-sm); color: var(--text-muted); }
.nav-item[data-level="2"] > .nav-link:hover { color: var(--text); }
.nav-item[data-level="1"] > .nav-sub { position: relative; }
.nav-item[data-level="1"] > .nav-sub::before {
  content: ""; position: absolute; left: 41px; top: 2px; bottom: 6px; width: 1px; background: var(--border);
}

/* rail: icons only, tooltips from data-tip */
.shell.sidebar-collapsed .brand-name,
.shell.sidebar-collapsed .nav-text,
.shell.sidebar-collapsed .nav-caret,
.shell.sidebar-collapsed .nav-sub { display: none !important; }
.shell.sidebar-collapsed .sidebar-head { flex-direction: column; justify-content: center; gap: 2px; height: auto; padding: var(--sp-2) 0; }
.shell.sidebar-collapsed .brand { flex: none; }
.shell.sidebar-collapsed .nav-link { justify-content: center; padding: 0; height: 36px; position: relative; }
.shell.sidebar-collapsed .nav-link:hover::after {
  content: attr(data-tip); position: absolute; left: calc(100% + 10px); top: 50%; transform: translateY(-50%);
  padding: 5px 8px; border-radius: var(--r-ctrl); background: var(--text); color: var(--surface);
  font-size: var(--fs-sm); font-weight: 500; white-space: nowrap; pointer-events: none; z-index: var(--z-popover);
  box-shadow: var(--shadow-float);
}
.shell.sidebar-collapsed .sidebar { overflow: visible; }

/* topbar */
.topbar {
  position: sticky; top: 0; z-index: var(--z-topbar);
  display: flex; align-items: center; gap: var(--sp-2);
  height: var(--h-topbar); padding: 0 var(--sp-6) 0 var(--sp-4);
  background: var(--surface-chrome);
  -webkit-backdrop-filter: saturate(160%) blur(18px); backdrop-filter: saturate(160%) blur(18px);
  border-bottom: 1px solid var(--border);
  color: var(--text);
}
.topbar-menu { display: none; }
.topbar-spacer { flex: 1; }
.search-trigger {
  display: inline-flex; align-items: center; gap: var(--sp-2);
  width: min(360px, 40vw); height: 32px; padding: 0 6px 0 10px;
  border: 1px solid var(--border); border-radius: var(--r-ctrl);
  background: var(--surface-sunken); color: var(--text-muted);
  font: 450 var(--fs-body) / 1 var(--font-ui); cursor: pointer;
  transition: border-color var(--dur-1) var(--ease-out), background var(--dur-1) var(--ease-out);
}
.search-trigger:hover { border-color: var(--border-strong); background: var(--surface); }
.search-trigger span { flex: 1; min-width: 0; text-align: left; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.date-chip {
  display: inline-flex; align-items: center; gap: 6px; height: 30px; padding: 0 10px;
  border-radius: var(--r-pill); background: var(--surface-sunken); border: 1px solid var(--border);
  color: var(--text-secondary); font-size: var(--fs-sm); font-weight: 500; white-space: nowrap;
}
.date-chip .icon { width: 14px; height: 14px; color: var(--text-muted); }
.date-chip-sep { width: 1px; height: 12px; background: var(--border-strong); }
.avatar-btn { border: 0; padding: 0; background: transparent; border-radius: var(--r-pill); cursor: pointer; }
.avatar {
  display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: var(--r-pill);
  background: var(--accent-subtle); color: var(--accent-text); font-size: var(--fs-xs); font-weight: 650; letter-spacing: 0.02em;
}
.avatar-lg { width: 38px; height: 38px; font-size: var(--fs-body); }

/* popover menus (native popover: Esc and outside click close them) */
.menu {
  position: fixed; inset: auto; top: calc(var(--h-topbar) + 6px); margin: 0;
  min-width: 220px; padding: 6px; border: 1px solid var(--border); border-radius: var(--r-panel);
  background: var(--surface-raised); color: var(--text); box-shadow: var(--shadow-float);
  opacity: 0; transform: translateY(-4px) scale(0.98); transform-origin: top right;
  transition: opacity var(--dur-2) var(--ease-out), transform var(--dur-2) var(--ease-out), display var(--dur-2) allow-discrete, overlay var(--dur-2) allow-discrete;
}
.menu:popover-open { opacity: 1; transform: none; }
@starting-style { .menu:popover-open { opacity: 0; transform: translateY(-4px) scale(0.98); } }
.menu-new { right: 112px; }
.menu-profile { right: 16px; width: 260px; }
.menu-item {
  display: flex; align-items: center; gap: 10px; width: 100%; min-height: 32px; padding: 0 10px;
  border: 0; border-radius: var(--r-ctrl); background: transparent; color: var(--text);
  font: 450 var(--fs-body) / 1.2 var(--font-ui); text-align: left; text-decoration: none; cursor: pointer;
}
.menu button.menu-item { background: transparent; border: 0; box-shadow: none; padding: 0 10px; font-size: var(--fs-body); font-weight: 450; border-radius: var(--r-ctrl); color: var(--text); }
.menu-item .icon { color: var(--text-muted); }
.menu-item:hover, .menu-item:focus-visible { background: var(--surface-hover); box-shadow: none; }
.menu .menu-item:focus-visible { box-shadow: inset 2px 0 0 var(--accent); }   /* the hover fill alone is too faint to show focus */
.menu-item-danger, .menu-item-danger .icon, .menu button.menu-item-danger { color: var(--danger); }
.menu-sep { height: 1px; margin: 6px -6px; background: var(--border); }
.menu-label { padding: 4px 10px 6px; font-size: var(--fs-xs); font-weight: 600; color: var(--text-muted); }
.menu-head { display: flex; align-items: center; gap: 10px; padding: 6px 8px 4px; }
.menu-head-name { font-weight: 600; }
.menu-head-meta { font-size: var(--fs-sm); color: var(--text-muted); }
.menu-form { margin: 0; }
.segmented { display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px; margin: 0 4px 4px; padding: 2px; border-radius: var(--r-ctrl); background: var(--surface-sunken); }
.segmented button {
  display: inline-flex; align-items: center; justify-content: center; gap: 4px; height: 28px;
  border: 0; border-radius: 5px; background: transparent; color: var(--text-secondary);
  font: 500 var(--fs-sm) / 1 var(--font-ui); cursor: pointer;
}
.segmented button .icon { width: 14px; height: 14px; }
.segmented button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow-low); }

/* sheets (keyboard shortcuts) */
.sheet { width: min(520px, calc(100vw - 32px)); padding: 0; border: 1px solid var(--border); border-radius: 14px; background: var(--surface-raised); color: var(--text); box-shadow: var(--shadow-float); }
.sheet::backdrop { background: rgb(10 12 16 / 0.42); -webkit-backdrop-filter: blur(2px); backdrop-filter: blur(2px); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: var(--sp-4) var(--sp-4) var(--sp-2) var(--sp-6); }
.sheet-title { margin: 0; font-size: var(--fs-lg); font-weight: 600; letter-spacing: var(--tracking-tight); }
.shortcut-list { margin: 0; padding: var(--sp-2) var(--sp-6) var(--sp-6); display: grid; gap: 2px; }
.shortcut-list > div { display: grid; grid-template-columns: 170px 1fr; align-items: center; gap: var(--sp-3); padding: 6px 0; border-bottom: 1px solid var(--border); }
.shortcut-list > div:last-child { border-bottom: 0; }
.shortcut-list dt { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; color: var(--text-muted); }
.shortcut-list dd { margin: 0; color: var(--text-secondary); }

/* sign-in and other anonymous pages: no shell */
.auth-main { min-height: 100dvh; display: grid; place-items: center; padding: var(--sp-8) var(--sp-4); }

/* F2 quick-add opens a screen in an iframe without the shell */
.kit-quick .sidebar, .kit-quick .topbar { display: none !important; }
.kit-quick .shell { grid-template-columns: minmax(0, 1fr); }

/* phones and tablets: the menu is a drawer */
@media (max-width: 900px) {
  .shell, .shell.sidebar-collapsed { grid-template-columns: minmax(0, 1fr); }
  /* closed, the drawer is hidden as well as slid away, so screen readers and Tab skip it */
  .sidebar { position: fixed; inset: 0 auto 0 0; width: min(var(--w-sidebar), 86vw); transform: translateX(-100%); visibility: hidden; transition: transform var(--dur-3) var(--ease-out), visibility 0s var(--dur-3); box-shadow: var(--shadow-float);
    background: var(--surface); -webkit-backdrop-filter: none; backdrop-filter: none; }   /* over the page, glass makes the menu hard to read */
  .shell.sidebar-open .sidebar { transform: none; visibility: visible; transition-delay: 0s; }
  .shell.sidebar-collapsed .brand-name, .shell.sidebar-collapsed .nav-text, .shell.sidebar-collapsed .nav-caret { display: revert !important; }
  .sidebar-collapse { display: none; }
  .topbar { padding: 0 var(--sp-3); }
  .topbar-menu { display: inline-grid; }
  .search-trigger { width: auto; flex: 1; min-width: 0; }
  .search-trigger kbd, .date-chip, .topbar .btn-ghost span { display: none; }
  .cb-main { padding: var(--sp-4); }
}

@media (prefers-reduced-transparency: reduce) {
  .sidebar, .topbar { background: var(--surface); -webkit-backdrop-filter: none; backdrop-filter: none; }
}

/* No cross-page transition: clerks type the moment a screen opens, and keys must never land mid-animation. */
@media (prefers-reduced-motion: reduce) {
  .progress.is-loading { animation: none; transform: scaleX(0.6); }
}

/* ---------- toasts ---------- */
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
#toast-host {
  position: fixed; right: var(--sp-5); bottom: var(--sp-5); z-index: var(--z-toast);
  display: flex; flex-direction: column; align-items: flex-end; gap: var(--sp-2); pointer-events: none;
}
.toast {
  pointer-events: auto; display: flex; align-items: flex-start; gap: 10px;
  width: min(380px, calc(100vw - 32px)); padding: 11px 8px 11px 12px;
  border: 1px solid var(--border); border-radius: var(--r-panel);
  background: var(--surface-raised); color: var(--text); box-shadow: var(--shadow-float);
  font-size: var(--fs-body); line-height: 1.4;
  transition: opacity var(--dur-2) var(--ease-out), transform var(--dur-2) var(--ease-out);
}
@starting-style { .toast { opacity: 0; transform: translateY(8px) scale(0.98); } }
.toast.is-leaving { opacity: 0; transform: translateX(16px); }
.toast-icon { width: 18px; height: 18px; margin-top: 1px; }
.toast-ok .toast-icon { color: var(--success); }
.toast-warn .toast-icon { color: var(--warning); }
.toast-error { border-color: color-mix(in srgb, var(--danger) 35%, var(--border)); }
.toast-error .toast-icon { color: var(--danger); }
.toast-text { flex: 1; min-width: 0; overflow-wrap: anywhere; }
.toast-close {
  display: inline-grid; place-items: center; width: 22px; height: 22px; margin: -2px 0 -2px 4px; padding: 0;
  border: 0; border-radius: 5px; background: transparent; color: var(--text-faint); cursor: pointer;
}
.toast-close:hover { background: var(--surface-hover); color: var(--text); }
.toast-close .icon { width: 14px; height: 14px; }

/* ---------- confirm (confirm.ts) ---------- */
.confirm {
  width: min(400px, calc(100vw - 32px)); padding: var(--sp-6) var(--sp-6) var(--sp-5);
  border: 1px solid var(--border); border-radius: 14px; background: var(--surface-raised); color: var(--text);
  box-shadow: var(--shadow-float);
}
.confirm::backdrop { background: rgb(10 12 16 / 0.42); }
.confirm[open] { animation: pop var(--dur-2) var(--ease-out); }
.confirm-icon { display: inline-grid; place-items: center; width: 36px; height: 36px; border-radius: var(--r-pill); background: var(--accent-subtle); color: var(--accent-text); }
.confirm.is-danger .confirm-icon { background: var(--danger-subtle); color: var(--danger); }
.confirm-icon .icon { width: 20px; height: 20px; }
.confirm-text { margin: var(--sp-3) 0 var(--sp-5); font-size: var(--fs-md); font-weight: 550; line-height: 1.45; }
.confirm-actions { display: flex; justify-content: flex-end; gap: var(--sp-2); }
.confirm-actions kbd { margin-left: 2px; border-color: currentColor; color: inherit; background: transparent; opacity: 0.7; }
@keyframes pop { from { opacity: 0; transform: translateY(6px) scale(0.98); } }

@media print {
  :root { color-scheme: light; }
  html, body { background: #fff !important; color: #000 !important; }
  .progress, #toast-host { display: none !important; }
  .shell { display: block; }
}
