/*
 * App shell: reset, typography, chrome layout, keyframes.
 *
 * Every colour here is a token from tokens.css. Nothing in this file may
 * hardcode a palette value — that indirection is the entire dark-mode
 * mechanism. The two exceptions are the scrollbar thumb and the selection
 * tint, which the design specifies as fixed translucent greys that read
 * correctly over both themes.
 */

/* ---- reset ------------------------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }

/* Alpine has not booted yet — hide what it is about to conditionally show. */
[x-cloak] { display: none !important; }

/* A removed formset row is hidden, never detached — its inputs must keep
   posting or every later row's index shifts. Grid and flex rows would ignore
   the plain `hidden` attribute, hence the override. */
[hidden] { display: none !important; }

html, body, h1, h2, h3, h4, h5, h6, p, figure, blockquote, dl, dd, ol, ul {
  margin: 0;
  padding: 0;
}

ol, ul { list-style: none; }

html, body { height: 100%; }

body {
  font-family: var(--font-sans);
  font-size: 13.5px;
  line-height: 1.5;
  letter-spacing: -.005em;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

img, svg, video, canvas { display: block; max-width: 100%; }

button, input, select, textarea {
  font: inherit;
  letter-spacing: inherit;
  color: inherit;
}

h1, h2, h3, h4, h5, h6 { font-weight: 500; line-height: 1.25; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 3px; }

table { border-collapse: collapse; width: 100%; }

/* ---- scrollbars -------------------------------------------------------- */
* { scrollbar-width: thin; scrollbar-color: rgba(120, 130, 145, .32) transparent; }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: rgba(120, 130, 145, .32);
  border-radius: 8px;
  border: 3px solid transparent;
  background-clip: content-box;
}
::-webkit-scrollbar-thumb:hover { background: rgba(120, 130, 145, .5); background-clip: content-box; }

::selection { background: rgba(31, 79, 216, .18); }

/* ---- shell ------------------------------------------------------------- */
.app {
  display: flex;
  /* `dvh` first where it is understood: on a phone `100vh` is the viewport
     with the browser chrome *retracted*, so the bottom of every page sits
     under the URL bar until you scroll. The `vh` line stays as the fallback. */
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.sidebar {
  width: 252px;
  flex: 0 0 auto;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width .26s cubic-bezier(.4, 0, .2, 1);
}

.sidebar[data-collapsed="true"] { width: 64px; }

/*
 * The collapsed rail is 64px wide and its content box is ~40px — room for one
 * 28px control per line and nothing beside it. Everything hidden here is
 * hidden with `display: none`, never `opacity: 0`: an invisible box still
 * takes its width, and that is exactly what used to push the collapse toggle
 * past the rail's edge and under `overflow: hidden`, leaving no way to expand
 * the sidebar again.
 *
 * The two brand marks go with them. The head must stay 56px tall to keep its
 * rule level with the topbar's, so a 28px mark and a 28px toggle cannot be
 * stacked inside it, and of the two only the toggle does anything. The rail
 * keeps what is operable — the toggle, the nav icons, theme and sign-out.
 */
.sidebar[data-collapsed="true"] .nav-label,
.sidebar[data-collapsed="true"] .sidebar-identity,
.sidebar[data-collapsed="true"] .sidebar-mark,
.sidebar[data-collapsed="true"] .spacer,
.sidebar[data-collapsed="true"] .kbd {
  display: none;
}

.sidebar[data-collapsed="true"] .sidebar-head { justify-content: center; padding: 0; }
.sidebar[data-collapsed="true"] .nav-item { justify-content: center; padding: 0; }

/* Two controls, one above the other — side by side they do not fit. */
.sidebar[data-collapsed="true"] .sidebar-foot {
  flex-direction: column;
  gap: 6px;
  padding: 10px 0;
}

/* A group heading with no room for its words becomes the divider it was
   drawing in text: zero-height, so only the rule shows. */
.sidebar[data-collapsed="true"] .nav-group-label {
  height: 0;
  margin: 12px 6px;
  padding: 0;
  overflow: hidden;
  border-top: 1px solid var(--border);
}
/* …except the first, which would sit a second line right under the head's. */
.sidebar[data-collapsed="true"] .sidebar-nav > .nav-group-label:first-child { display: none; }

.sidebar-head {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 56px;
  flex: 0 0 auto;
  padding: 0 12px 0 14px;
  border-bottom: 1px solid var(--border);
}

.sidebar-mark {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: var(--accent);
  color: var(--on-accent);
  font-family: var(--font-mono);
  font-size: 13px;
  font-weight: 600;
}

.sidebar-identity { min-width: 0; }
.sidebar-name {
  font-size: 12.5px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.sidebar-meta {
  font-size: 11px;
  color: var(--text-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-nav {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 12px 10px;
}

.sidebar-foot {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 10px;
  border-top: 1px solid var(--border);
}

.nav-group-label {
  padding: 0 8px;
  margin: 14px 0 6px;
  font-size: 10.5px;
  font-weight: 500;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--text-3);
  white-space: nowrap;
}
.sidebar-nav > .nav-group-label:first-child { margin-top: 0; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 32px;
  padding: 0 8px;
  margin-bottom: 1px;
  border-radius: var(--radius);
  color: var(--text-2);
  font-size: 12.5px;
  white-space: nowrap;
  transition: background .16s, color .16s;
}
.nav-item:hover { background: var(--panel-3); color: var(--text); text-decoration: none; }
.nav-item:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.nav-item svg { flex: 0 0 auto; }
.nav-label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* A destination that does not exist yet. Rendered as a <span>, so it is not
   focusable and not a link — it reads as the honest absence it is rather than
   a control that does nothing. */
.nav-item.is-disabled { color: var(--text-3); cursor: not-allowed; }
.nav-item.is-disabled:hover { background: transparent; color: var(--text-3); }

.nav-item[aria-current="page"] {
  background: var(--accent-soft);
  color: var(--accent);
  font-weight: 500;
}

.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.topbar {
  height: 56px;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 18px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  z-index: 20;
}

.breadcrumb {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  font-size: 12.5px;
  color: var(--text-2);
}
/* Only ever drawn on a narrow viewport — see the media query at the foot of
   this file. It shares the topbar with the breadcrumb, so it is a control the
   layout must account for at every width, hidden rather than absent.

   Scoped to .topbar for the specificity, not for the scope: the button is a
   .btn, components.css loads after this file, and a bare `.nav-toggle` ties
   with `.btn`'s own `display: inline-flex` and loses on source order — which
   is exactly how the hamburger showed up on a 1440px desktop. */
.topbar .nav-toggle { display: none; flex: 0 0 auto; }

/* Closes the drawer by tapping the page it covers. A sibling of .sidebar so it
   can sit under it and over everything else. */
.nav-scrim {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: var(--scrim);
  animation: fadeIn .18s ease both;
}

.breadcrumb a { color: var(--text-2); }
.breadcrumb a:hover { color: var(--text); }
.breadcrumb strong { font-weight: 500; color: var(--text); }
.breadcrumb-sep { opacity: .5; }

.viewport {
  flex: 1;
  overflow-y: auto;
  position: relative;
}

/* The scroll container is .viewport, so that is where anchor jumps happen. */
@media (prefers-reduced-motion: no-preference) {
  .viewport { scroll-behavior: smooth; }
}

.page {
  padding: 26px 26px 90px;
  max-width: 1560px;
  animation: fadeUp .28s cubic-bezier(.2, .8, .2, 1) both;
}

.page-title { font-size: 19px; font-weight: 500; letter-spacing: -.02em; }
.page-sub { font-size: 12.5px; color: var(--text-2); }

/* ---- auth (login, via base_bare.html) ---------------------------------- */
.auth-shell {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 24px;
  background:
    radial-gradient(680px 380px at 50% -8%, var(--accent-soft), transparent 70%),
    var(--bg);
}

.auth-card {
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: 100%;
  max-width: 372px;
  padding: 26px;
  animation: fadeUp .34s cubic-bezier(.2, .8, .2, 1) both;
}

.auth-brand { display: flex; align-items: center; gap: 11px; margin-bottom: 4px; }

.auth-mark {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--on-accent);
  font-family: var(--font-mono);
  font-size: 15px;
  font-weight: 600;
}

.auth-title { font-size: 15px; font-weight: 500; letter-spacing: -.01em; }
.auth-sub { font-size: 12px; color: var(--text-3); }

/* ---- narrow viewports --------------------------------------------------- */
/*
 * 860px is where the 252px rail stops paying for itself: on a 390px phone it
 * leaves 138px for the work. Below it the sidebar becomes a drawer over the
 * page — opened from the topbar, dismissed by the scrim — and the desktop
 * collapse state is dropped on the way in (app.js), because a 64px rail inside
 * a drawer is a rail nobody asked for.
 *
 * The same 860 lives in `NARROW` in static/js/app.js, which decides what the
 * one toggle button does. Change one, change the other. It is duplicated
 * rather than derived because a media query cannot read a custom property.
 */
@media (max-width: 860px) {
  .sidebar {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 60;
    width: 252px;
    transform: translateX(-100%);
    transition: transform .24s cubic-bezier(.4, 0, .2, 1);
    box-shadow: var(--shadow-pop);
  }

  .sidebar[data-open="true"] { transform: none; }

  .topbar .nav-toggle { display: inline-flex; }

  /* Fingers, not a mouse: the 32px row is under the 44px touch target every
     platform's guidance asks for, and this is the one control on the page
     that is nothing but rows. */
  .nav-item { height: 40px; }

  /* Height, not min-height, is what let a detail page's five actions paint
     over the page below: they wrap at this width, and a 56px box with three
     rows of buttons in it overflows in silence. The bar grows instead. */
  .topbar {
    height: auto;
    min-height: 56px;
    flex-wrap: wrap;
    gap: 8px;
    padding: 8px 10px;
  }

  /* A breadcrumb is the one thing here that can be arbitrarily long — a
     quotation number and a customer name — and it shares the row with the
     drawer toggle and the page actions. It gives way first: each crumb is its
     own flex item, so the ellipsis has to go on the crumbs, not on the nav
     around them. */
  .breadcrumb { overflow: hidden; }
  .breadcrumb > * { min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }

  /* Page actions sit beside it and must not lose their labels to a line
     break: "Save quotation" over two lines makes the topbar 70px tall and
     still reads worse than a shortened breadcrumb. */
  .topbar .btn { white-space: nowrap; }

  .page { padding: 16px 14px 72px; }
}

/* ---- keyframes --------------------------------------------------------- */
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: none; }
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes popIn {
  from { opacity: 0; transform: translateY(-4px) scale(.97); }
  to { opacity: 1; transform: none; }
}

@keyframes rowIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: none; }
}

@keyframes barUp {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: none; }
}

@keyframes shimmer {
  from { background-position: -420px 0; }
  to { background-position: 420px 0; }
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
  }
}
