/* Mobile bottom navigation — "green dock" (option B).
   Markup: templates/partials/_bottom_nav.html   Behaviour: static/js/bottom-nav.js
   Loaded on every page; the bar hides itself above 980px (phone-only).

   Every colour is a --bn-* token and comes from the theme (Appearance):
   the dock is the primary colour of the current mode (light primary in
   light mode, dark primary in dark mode); the add button and the current
   page's icon are the DARK secondary colour in both modes. The inks on top
   (--nav-fg-light / --nav-fg-dark / --nav-on-accent) are picked for
   contrast in app/__init__.py and set in base.html :root. Hex values below
   appear only as var() fallbacks. */

/* ── Tokens ─────────────────────────────────────────────────────────────── */
.bottom-nav {
  --bn-bg:           var(--light-primary, var(--brand, #0F3B33));
  --bn-ink:          var(--nav-fg-light, #FFFFFF);
  --bn-fg-muted:     color-mix(in oklab, var(--bn-ink) 72%, var(--bn-bg));
  --bn-accent:       var(--dm-secondary, #C9A45C);
  --bn-fg-active:    var(--bn-accent);       /* the current page's icon */
  --bn-on-accent:    var(--nav-on-accent, var(--bn-bg));
  --bn-ring:         var(--bg-page, #F4F6F5);
  --bn-menu-bg:      var(--bn-bg);
  --bn-menu-fg:      var(--bn-ink);
  --bn-menu-icon-bg: var(--bn-accent);
  --bn-menu-icon-fg: var(--bn-on-accent);
  --bn-scrim:        color-mix(in srgb, var(--text, #13201D) 45%, transparent);
  --bn-shadow:       color-mix(in srgb, var(--bn-bg) 30%, transparent);
  --bn-edge:         transparent;

  --bn-h: 64px;
  --bn-fab: 58px;
  --bn-fab-ring: 5px;
  --bn-fab-rise: 30px;
  --bn-ease: cubic-bezier(0.4, 0, 0.2, 1);
  --bn-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --bn-theme-fade: background-color .25s ease, color .25s ease, border-color .25s ease;
}
/* Dark palette: the dock is the dark primary colour; the edge keeps it apart
   from the page when the two are close. */
html.dark-mode .bottom-nav,
html[data-theme="dark"] .bottom-nav {
  --bn-bg:        var(--dm-primary, #3AA58A);
  --bn-ink:       var(--nav-fg-dark, #FFFFFF);
  --bn-accent:    var(--dm-secondary, #C9A45C);
  --bn-fg-active: var(--bn-accent);
  --bn-fg-muted:  color-mix(in oklab, var(--bn-ink) 72%, var(--bn-bg));
  --bn-ring:      var(--dm-bg, #0E1513);
  --bn-menu-fg:   var(--bn-ink);
  --bn-scrim:     color-mix(in srgb, var(--dm-bg, #0E1513) 60%, transparent);
  --bn-shadow:    color-mix(in srgb, var(--dm-bg, #0E1513) 70%, transparent);
  --bn-edge:      color-mix(in srgb, var(--bn-fg-muted) 25%, transparent);
}

/* ── Bar ────────────────────────────────────────────────────────────────── */
.bn-bar {
  position: fixed; inset-inline: 0; bottom: 0;
  /* above the page-loading overlay (#page-load-overlay, 999999) so the dock
     stays put while the next page loads; the drawer (1000001+) covers it */
  z-index: 1000000;
  height: calc(var(--bn-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  padding-inline: 6px;
  box-sizing: border-box;
  display: flex; align-items: stretch; justify-content: space-around;
  background-color: var(--bn-bg);
  border-top: 1px solid var(--bn-edge);
  border-radius: 26px 26px 0 0;
  box-shadow: 0 -8px 24px var(--bn-shadow);
  transition: var(--bn-theme-fade);
}

.bn-tab {
  position: relative;
  flex: 1 1 0; min-width: 48px; min-height: 48px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  padding-block: 6px 4px;
  color: var(--bn-fg-muted);
  font-size: 12px; font-weight: 400; line-height: 1.15;
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
  transition: var(--bn-theme-fade), transform .1s ease;
}
.bn-tab:hover, .bn-tab:focus { color: var(--bn-fg-muted); text-decoration: none; }
.bn-tab-icon { width: 24px; height: 24px; flex-shrink: 0; display: block; transition: color .25s ease; }
/* current page: only its icon changes colour; the label stays as the others */
.bn-tab[aria-current="page"] .bn-tab-icon { color: var(--bn-fg-active); }
.bn-tab-label { max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bn-tab.is-pressed { transform: scale(0.92); }

:where(.bn-tab, .bn-fab, .bn-menu-item):focus-visible {
  outline: 2px solid var(--bn-accent);
  outline-offset: 2px;
}

/* ── Centre button: a diamond that turns into a square ──────────────────── */
.bn-fab-slot { position: relative; flex: 0 0 76px; }
.bn-fab {
  position: absolute; top: calc(-1 * var(--bn-fab-rise)); left: 50%;
  margin-left: calc(-0.5 * var(--bn-fab) - var(--bn-fab-ring));
  width: var(--bn-fab); height: var(--bn-fab); box-sizing: content-box;
  padding: 0; margin-block: 0;
  border: var(--bn-fab-ring) solid var(--bn-ring);
  border-radius: 18px;
  background-color: var(--bn-accent);
  color: var(--bn-on-accent);
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; -webkit-tap-highlight-color: transparent;
  box-shadow: 0 8px 20px var(--bn-shadow);
  transform: rotate(45deg);
  /* closing: 320ms, standard ease */
  transition: transform .32s var(--bn-ease), border-radius .32s var(--bn-ease), var(--bn-theme-fade);
}
/* the site-wide button:hover / button:focus (style.css) repaints buttons with
   the brand colour; after a tap the button keeps focus, so pin its colours */
.bottom-nav .bn-fab,
.bottom-nav .bn-fab:hover,
.bottom-nav .bn-fab:focus {
  background-color: var(--bn-accent);
  border-color: var(--bn-ring);
  color: var(--bn-on-accent);
}
.bn-fab-icon {
  position: absolute; display: block;
  transform: rotate(-45deg);
  transition: transform .32s var(--bn-ease), opacity .15s ease;
}
.bn-fab-close { opacity: 0; }
.bn-fab[aria-expanded="true"] {
  transform: rotate(180deg);
  border-radius: 14px;
  /* opening: 420ms with a light overshoot */
  transition: transform .42s var(--bn-spring), border-radius .42s var(--bn-spring), var(--bn-theme-fade);
}
.bn-fab[aria-expanded="true"] .bn-fab-plus {
  transform: rotate(-135deg);   /* net 45°: the + reads as × */
  transition: transform .42s var(--bn-spring), opacity .15s ease;
}

/* ── Scrim ──────────────────────────────────────────────────────────────── */
.bn-scrim {
  position: fixed; inset: 0;
  z-index: 999998;              /* under the bar and the menu */
  background-color: var(--bn-scrim);
  -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px);
  opacity: 0; visibility: hidden;
  transition: opacity .2s ease, visibility 0s linear .2s, background-color .25s ease;
}
.bottom-nav.is-open .bn-scrim {
  opacity: 1; visibility: visible;
  transition: opacity .2s ease, visibility 0s, background-color .25s ease;
}

/* ── Speed-dial menu ────────────────────────────────────────────────────── */
.bn-menu {
  position: fixed; inset-inline: 0;
  bottom: calc(var(--bn-h) + var(--bn-fab-rise) + 18px + env(safe-area-inset-bottom));
  z-index: 1000001;
  margin: 0; padding: 0; list-style: none;
  /* first item in the DOM sits nearest the button */
  display: flex; flex-direction: column-reverse; align-items: center; gap: 12px;
  pointer-events: none;
}
.bottom-nav.is-open .bn-menu { pointer-events: auto; }
.bn-menu > li { display: flex; }
.bn-menu-item {
  display: inline-flex; align-items: center; gap: 10px;
  height: 48px; box-sizing: border-box;
  padding-inline: 8px 18px;
  border-radius: 999px;
  background-color: var(--bn-menu-bg);
  color: var(--bn-menu-fg);
  font-size: 14px; font-weight: 600; white-space: nowrap;
  text-decoration: none;
  box-shadow: 0 8px 20px var(--bn-shadow);
  -webkit-tap-highlight-color: transparent;
  opacity: 0; visibility: hidden;
  transform: translateY(16px) scale(0.85);
  /* closing: reverse order, 30ms apart (delays below) */
  transition: opacity .2s var(--bn-ease), transform .2s var(--bn-ease), visibility 0s linear .26s, var(--bn-theme-fade);
}
.bn-menu-item:hover, .bn-menu-item:focus { color: var(--bn-menu-fg); text-decoration: none; }
.bn-menu-icon {
  width: 32px; height: 32px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  background-color: var(--bn-menu-icon-bg);
  color: var(--bn-menu-icon-fg);
  transition: var(--bn-theme-fade);
}
.bn-menu > li:nth-child(2) .bn-menu-item { transition-delay: 0s, 0s, .23s, 0s, 0s, 0s; }
.bn-menu > li:nth-child(1) .bn-menu-item { transition-delay: .03s, .03s, .26s, 0s, 0s, 0s; }

.bottom-nav.is-open .bn-menu-item {
  opacity: 1; visibility: visible; transform: none;
  transition: opacity .28s var(--bn-ease), transform .28s var(--bn-spring), visibility 0s, var(--bn-theme-fade);
}
/* opening: starts 120ms into the button turn, nearest first, 45ms apart */
.bottom-nav.is-open .bn-menu > li:nth-child(1) .bn-menu-item { transition-delay: .12s, .12s, 0s, 0s, 0s, 0s; }
.bottom-nav.is-open .bn-menu > li:nth-child(2) .bn-menu-item { transition-delay: .165s, .165s, 0s, 0s, 0s, 0s; }

/* ── Phone only ─────────────────────────────────────────────────────────── */
@media (min-width: 981px) {
  .bottom-nav { display: none; }
}
@media (max-width: 980px) {
  /* last item of every page clears the bar: bar height + 24px */
  body .nb-page { padding-bottom: calc(64px + 24px + env(safe-area-inset-bottom)); }
}
@media (max-width: 360px) {
  .bn-bar { padding-inline: 2px; }
  .bn-tab { font-size: 11px; }
  .bn-fab-slot { flex-basis: 70px; }
}

/* ── Reduced motion: no turn, no stagger, no overshoot — 150ms fades ───── */
@media (prefers-reduced-motion: reduce) {
  .bn-fab, .bn-fab[aria-expanded="true"] { transform: rotate(45deg); transition: var(--bn-theme-fade); }
  .bn-fab-icon, .bn-fab[aria-expanded="true"] .bn-fab-plus { transform: rotate(-45deg); transition: opacity .15s ease; }
  .bn-fab[aria-expanded="true"] .bn-fab-plus { opacity: 0; }
  .bn-fab[aria-expanded="true"] .bn-fab-close { opacity: 1; }
  .bn-menu-item,
  .bottom-nav.is-open .bn-menu-item,
  .bottom-nav .bn-menu > li .bn-menu-item {
    transform: none;
    transition: opacity .15s ease, visibility 0s linear .15s;
    transition-delay: 0s;
  }
  .bottom-nav.is-open .bn-menu-item { transition: opacity .15s ease, visibility 0s; }
  .bn-scrim { transition-duration: .15s, 0s, 0s; }
  .bn-tab.is-pressed { transform: none; }
}

/* ── Step aside for page windows ─────────────────────────────────────────
   Page forms and dialogs (finance drawers, modal overlays, Bootstrap modals,
   the calendar's day dialog) sit below the dock in the stacking order, so the
   dock would cover their bottom buttons. While one is open the dock slides away. */
body:has(.form-drawer-overlay.open, .modal-overlay.open, .modal-overlay.show, .modal-overlay.active,
         .modal.show, .calx-modal-overlay.open, .calx-modal-overlay.show, .calx-modal-overlay.active) .bn-bar {
  transform: translateY(calc(100% + 40px));
  pointer-events: none;
}
.bn-bar { transition: var(--bn-theme-fade), transform .25s var(--bn-ease); }

/* ── Client portal extras ──────────────────────────────────────────────── */
/* count on a shortcut (e.g. pending requests) */
.bn-menu-count {
  min-width: 22px; height: 22px; padding: 0 6px; box-sizing: border-box;
  border-radius: 11px; display: inline-flex; align-items: center; justify-content: center;
  background-color: var(--bn-accent); color: var(--bn-on-accent);
  font-family: "IBM Plex Mono", ui-monospace, monospace; font-size: 12px; font-weight: 500;
  margin-inline-start: 2px;
}
/* privacy-consent page: the bar is there but can't be used until the client agrees */
.bottom-nav.is-locked .bn-bar { opacity: .45; pointer-events: none; }

/* ── Tab tiles that flip (static/js/bottom-nav.js wraps each icon) ──────────
   Each icon sits on a small tile; the current page's tile shows its back --
   the accent colour with the icon in its ink -- and a soft glow behind it.
   A tap flips the new tile over (with an overshoot) and the old one on to its
   plain front again, turning around the vertical axis with real perspective. */
.bn-tile-wrap { position: relative; width: 44px; height: 32px; perspective: 150px; flex-shrink: 0; }
.bn-tile { position: absolute; inset: 0; transform-style: preserve-3d; transform: rotateY(0deg); }
.bn-face { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; border-radius: 11px;
  backface-visibility: hidden; -webkit-backface-visibility: hidden; box-sizing: border-box; }
.bn-face-front { background: rgba(255,255,255,.05); border: 1.5px solid rgba(255,255,255,.12); color: var(--bn-fg-muted); }
.bn-face-back { transform: rotateY(180deg); color: var(--bn-on-accent); border: 1.5px solid rgba(255,255,255,.3);
  background: linear-gradient(135deg, var(--bn-accent), color-mix(in srgb, var(--bn-accent) 62%, #fff)); overflow: hidden; }
.bn-face-back::after { content: ""; position: absolute; inset: 0 0 50% 0; border-radius: 11px 11px 0 0;
  background: linear-gradient(rgba(255,255,255,.22), transparent); pointer-events: none; }
.bn-face .bn-tab-icon { width: 21px; height: 21px; color: inherit !important; }
.bn-tile-wrap::before { content: ""; position: absolute; left: 50%; top: 50%; width: 76px; height: 76px; margin: -38px 0 0 -38px; border-radius: 50%;
  background: radial-gradient(circle, color-mix(in srgb, var(--bn-accent) 55%, transparent) 5%, transparent 68%); opacity: 0; transition: opacity .3s; pointer-events: none; }
.bn-tab[aria-current="page"] .bn-tile-wrap::before { opacity: .55; }
.bn-tab[aria-current="page"] .bn-tile { transform: rotateY(180deg); }
.bn-tile.is-arriving { animation: bnFlipIn .62s linear both; }
.bn-tile.is-leaving { animation: bnFlipOut .5s linear both; }
/* springF(12, .5): over π, back, settle;  springF(14, .62): on round to 2π */
@keyframes bnFlipIn {
  0% { transform: rotateY(0deg) scale(1); } 18% { transform: rotateY(110deg) scale(1.13); } 34% { transform: rotateY(196deg) scale(1.04); }
  50% { transform: rotateY(207deg) scale(1.07); } 68% { transform: rotateY(183deg) scale(1.01); } 84% { transform: rotateY(177deg); } 100% { transform: rotateY(180deg) scale(1); }
}
@keyframes bnFlipOut {
  0% { transform: rotateY(180deg) scale(1); } 22% { transform: rotateY(285deg) scale(1.12); } 45% { transform: rotateY(358deg) scale(1.02); }
  65% { transform: rotateY(368deg) scale(1.03); } 100% { transform: rotateY(360deg) scale(1); }
}
.bn-tab.is-pressed { transform: none; }
@media (prefers-reduced-motion: reduce) { .bn-tile.is-arriving, .bn-tile.is-leaving { animation: none; } }
