/* =============================================================================
   Chief's Den — Layout
   File: public/assets/css/layout.css
   App shell, sidebar, content grid, responsive breakpoints, iOS safe areas.
   ============================================================================= */

/* ---------------------------------------------------------------------------
   App Shell
--------------------------------------------------------------------------- */

.app-shell {
  /* Deliberately NOT flex. The sidebar is position:fixed, so it is out of
     flow and there is nothing to lay out beside .main-content — but as a flex
     item .main-content was sized to the full viewport width and THEN pushed
     right by its sidebar margin, overflowing the page by exactly the sidebar's
     width. As a block, its auto width is the container minus its own margin,
     which is right at every breakpoint. */
  display: block;
  min-height: 100vh;
  min-height: 100dvh;
}

/* ---------------------------------------------------------------------------
   Sidebar (desktop)
--------------------------------------------------------------------------- */

.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  width: var(--sidebar-width);
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  z-index: var(--z-sticky);
  transition: width var(--transition-slow);
  overflow: hidden;
}

.sidebar.collapsed {
  width: var(--sidebar-collapsed);
}

/* Sidebar header — logo + app name */
.sidebar-header {
  padding: var(--space-6) var(--space-5);
  border-bottom: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-shrink: 0;
  min-height: 72px;
}

/* .sidebar-logo used to sit on an <svg>, which sized itself. It is now the
   wrapper the seal component renders, so the image inside has to be told to
   fill it — otherwise the picture keeps its intrinsic 128px and blows the
   sidebar header apart. */
.sidebar-logo {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  display: block;
}
/* <picture> is display:inline by default, so an img sized at 100% inside one
   is resolving against a box that is itself shrink-to-fit. Give the picture the
   36px explicitly rather than relying on that chain. */
.sidebar-logo picture {
  display: block;
  width: 100%;
  height: 100%;
}
.sidebar-logo img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
}

.sidebar-title {
  font-family: var(--font-display);
  font-size: var(--text-md);
  font-weight: var(--weight-medium);
  color: var(--color-text);
  white-space: nowrap;
  overflow: hidden;
}

.sidebar-tagline {
  font-size: var(--text-xs);
  /* --color-accent-dim measured 3.10:1 here — the worst text on the page. That
     token earns its keep as a border and disabled-state colour, and this was
     its ONLY use as text, so the tagline moves to the brighter amber rather
     than lifting the token and dragging every border along with it. */
  color: var(--color-accent);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  white-space: nowrap;
}

/* Sidebar navigation */
.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-4) 0;
}

.nav-section {
  margin-bottom: var(--space-4);
}

.nav-section-label {
  font-size: var(--text-xs);
  font-weight: var(--weight-semi);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--color-text-subtle);
  padding: var(--space-2) var(--space-5);
  margin-bottom: var(--space-1);
  white-space: nowrap;
  overflow: hidden;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-5);
  color: var(--color-text-subtle);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  text-decoration: none;
  border-radius: 0;
  transition: color var(--transition-fast), background var(--transition-fast);
  white-space: nowrap;
  overflow: hidden;
  min-height: 44px;
  position: relative;
}

.nav-item:hover {
  color: var(--color-text);
  background: var(--color-surface-2);
}

.nav-item.active {
  color: var(--color-accent);
  background: var(--color-accent-bg);
}

.nav-item.active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  background: var(--color-accent);
  border-radius: 0 2px 2px 0;
}

.nav-item-icon {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  opacity: 0.7;
}

.nav-item.active .nav-item-icon,
.nav-item:hover .nav-item-icon {
  opacity: 1;
}

.nav-item-badge {
  margin-left: auto;
  background: var(--color-danger);
  color: var(--color-danger-text);
  font-size: 12px;
  font-weight: var(--weight-semi);
  padding: 1px 6px;
  border-radius: var(--radius-full);
  flex-shrink: 0;
}

/* Sidebar footer — user info */
.sidebar-footer {
  padding: var(--space-4) var(--space-5);
  border-top: 1px solid var(--color-border);
  flex-shrink: 0;
}

.user-row {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-height: 44px;
}

.user-avatar {
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  background: var(--color-accent-bg);
  border: 1px solid var(--color-accent-dim);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-display);
  font-size: var(--text-sm);
  color: var(--color-accent);
  flex-shrink: 0;
}

.user-info {
  flex: 1;
  overflow: hidden;
}

.user-name {
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  color: var(--color-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.user-role {
  font-size: var(--text-xs);
  color: var(--color-text-subtle);
  text-transform: capitalize;
}

/* ---------------------------------------------------------------------------
   Main Content Area
--------------------------------------------------------------------------- */

.main-content {
  flex: 1;
  margin-left: var(--sidebar-width);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  transition: margin-left var(--transition-slow);
}

.sidebar.collapsed ~ .main-content {
  margin-left: var(--sidebar-collapsed);
}

/* Page header */
.page-header {
  padding: var(--space-6) var(--content-padding);
  border-bottom: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-4);
  flex-shrink: 0;
  background: var(--color-bg);
  position: sticky;
  top: 0;
  z-index: var(--z-raised);
}

.page-title {
  font-family: var(--font-display);
  font-size: var(--text-2xl);
  font-weight: var(--weight-medium);
  color: var(--color-text);
}

.page-subtitle {
  font-size: var(--text-sm);
  color: var(--color-text-subtle);
  margin-top: var(--space-1);
}

.page-actions {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-shrink: 0;
}

/* Page body */
.page-body {
  flex: 1;
  padding: var(--space-6) var(--content-padding);
  max-width: var(--content-max-width);
  width: 100%;
}

/* ---------------------------------------------------------------------------
   Floating Scan Button
--------------------------------------------------------------------------- */

.scan-fab {
  position: fixed;
  bottom: calc(var(--space-6) + var(--safe-bottom));
  right: var(--space-6);
  width: 56px;
  height: 56px;
  border-radius: var(--radius-full);
  background: var(--color-accent);
  color: var(--color-text-inverse);
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-lg), var(--shadow-amber-md);
  z-index: var(--z-raised);
  transition: transform var(--transition-fast), box-shadow var(--transition-fast);
}

.scan-fab:hover {
  transform: scale(1.05);
  box-shadow: var(--shadow-xl), var(--shadow-amber-lg);
}

.scan-fab:active {
  transform: scale(0.97);
}

.scan-fab svg {
  width: 24px;
  height: 24px;
}

/* ---------------------------------------------------------------------------
   Content Grid
--------------------------------------------------------------------------- */

.grid {
  display: grid;
  gap: var(--space-5);
}

.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }

.grid-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: var(--space-5);
}

.grid-cards-sm {
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
}

/* ---------------------------------------------------------------------------
   Toast Notifications
--------------------------------------------------------------------------- */

.toast-container {
  position: fixed;
  bottom: calc(var(--space-6) + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  z-index: var(--z-toast);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  align-items: center;
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-5);
  border-radius: var(--radius-lg);
  font-size: var(--text-sm);
  font-weight: var(--weight-medium);
  max-width: 400px;
  box-shadow: var(--shadow-lg);
  pointer-events: auto;
  animation: toast-in 200ms ease forwards;
}

.toast.success { background: var(--color-success-bg); color: var(--color-success-text); border: 1px solid var(--color-success); }
.toast.error   { background: var(--color-danger-bg);  color: var(--color-danger-text);  border: 1px solid var(--color-danger); }
.toast.warning { background: var(--color-warning-bg); color: var(--color-warning-text); border: 1px solid var(--color-warning); }
.toast.info    { background: var(--color-info-bg);    color: var(--color-info-text);    border: 1px solid var(--color-info); }
.toast.amber   { background: var(--color-accent-bg);  color: var(--color-accent-hi);    border: 1px solid var(--color-accent); }

@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ---------------------------------------------------------------------------
   Responsive — Tablet (≤1024px)
--------------------------------------------------------------------------- */

@media (max-width: 1024px) {
  :root {
    --sidebar-width: var(--sidebar-collapsed);
    --content-padding: var(--space-5);
  }

  .sidebar {
    width: var(--sidebar-collapsed);
  }

  .sidebar-title,
  .sidebar-tagline,
  .nav-section-label,
  .nav-item-text,
  .user-info {
    display: none;
  }

  .nav-item {
    justify-content: center;
    padding: var(--space-3);
  }

  .nav-item.active::before {
    left: 0;
    right: auto;
  }

  .sidebar-header {
    justify-content: center;
    padding: var(--space-4);
  }

  .sidebar-footer {
    padding: var(--space-4);
  }

  .user-row {
    justify-content: center;
  }

  .main-content {
    margin-left: var(--sidebar-collapsed);
  }
}

/* ---------------------------------------------------------------------------
   Responsive — Mobile (≤768px)
--------------------------------------------------------------------------- */

@media (max-width: 768px) {
  :root {
    --content-padding: var(--space-4);
  }

  /* Hide sidebar completely on mobile */
  .sidebar {
    display: none;
  }

  .main-content {
    margin-left: 0;
    /* Account for bottom tab bar */
    padding-bottom: calc(var(--tabbar-height) + var(--safe-bottom));
  }

  .page-header {
    padding: var(--space-4) var(--content-padding);
  }

  .page-title {
    font-size: var(--text-xl);
  }

  .grid-2, .grid-3, .grid-4 {
    grid-template-columns: 1fr;
  }

  /* The card grid asks for a 280px minimum, which on a 375px phone means one
     column — and with a 3:4 image that is a 455px-tall card, so exactly one
     bottle fits on the screen and the collection cannot be browsed. Two
     narrower columns show eight. */
  .grid-cards {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: var(--space-3);
  }
  .grid-cards .item-card-image { aspect-ratio: 1 / 1; }
  .grid-cards .item-card-name  { font-size: var(--text-sm); }

  /* The tab bar already carries Scan, so the floating button is a second
     control for the same thing, sitting on top of the content. */
  .scan-fab {
    display: none;
  }

  /* The tab bar is turned on at the END of this file — putting the override
     here would place it BEFORE the base `.tab-bar { display: none }` rule,
     which is exactly the bug that kept it hidden on every phone. */
}

/* ---------------------------------------------------------------------------
   Mobile Tab Bar (iOS-style bottom navigation)
--------------------------------------------------------------------------- */

.tab-bar {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(var(--tabbar-height) + var(--safe-bottom));
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
  z-index: var(--z-sticky);
  padding-bottom: var(--safe-bottom);
}

.tab-bar-items {
  display: flex;
  height: var(--tabbar-height);
  /* .tab-bar is itself a flex container, so this row was a flex ITEM sized to
     its own content — 258px of a 375px screen — and the labels ran into each
     other instead of spreading out. It has to be told to take the full width. */
  flex: 1;
  width: 100%;
}

.tab-item {
  flex: 1 1 0;
  /* Without this a flex item refuses to shrink below its text, which is how
     six labels overflow a narrow phone. */
  min-width: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-1);
  color: var(--color-text-subtle);
  text-decoration: none;
  font-size: 12px;
  font-weight: var(--weight-medium);
  letter-spacing: 0.03em;
  padding: 0 2px;
  transition: color var(--transition-fast);
  min-height: 44px;
}

.tab-item span {
  max-width: 100%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tab-item.active {
  color: var(--color-accent);
}

.tab-item svg {
  width: 22px;
  height: 22px;
}

/* ---------------------------------------------------------------------------
   Auth layout (login, MFA, invite pages)
--------------------------------------------------------------------------- */

.auth-shell {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--color-bg);
  padding: var(--space-6);
  position: relative;
  overflow: hidden;
}

/* Film grain texture overlay */
.auth-shell::before {
  content: '';
  position: fixed;
  inset: 0;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='300' height='300'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3C/filter%3E%3Crect width='300' height='300' filter='url(%23noise)' opacity='0.04'/%3E%3C/svg%3E");
  pointer-events: none;
  z-index: 0;
}

.auth-card {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 400px;
  background: var(--color-surface);
  border: 1px solid var(--color-border-light);
  border-radius: var(--radius-2xl);
  padding: var(--space-10) var(--space-8);
  box-shadow: var(--shadow-xl), var(--shadow-amber-sm);
}

.auth-logo-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-bottom: var(--space-8);
}

.auth-logo {
  width: 80px;
  height: 80px;
  margin-bottom: var(--space-4);
}

.auth-title {
  font-family: var(--font-display);
  font-size: var(--text-2xl);
  font-weight: var(--weight-medium);
  color: var(--color-text);
  text-align: center;
  margin-bottom: var(--space-1);
}

.auth-subtitle {
  font-size: var(--text-sm);
  color: var(--color-text-subtle);
  text-align: center;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
}

.auth-field {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

/* The seal at the head of every screen on layouts/auth.php — MFA, forgot,
   reset, invite, backup codes. One declaration, because until 2026-09-04 those
   five pages carried four different answers between them. */
.auth-seal {
  display: flex;
  justify-content: center;
  margin-bottom: var(--space-4);
}
.auth-seal img {
  width: 96px;
  height: 96px;
  filter: drop-shadow(0 6px 18px rgba(0,0,0,0.5))
          drop-shadow(0 0 20px rgba(200,134,10,0.14));
}

.auth-divider {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  color: var(--color-text-dim);
  font-size: var(--text-xs);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.auth-divider::before,
.auth-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--color-border);
}

/* ---------------------------------------------------------------------------
   Dashboard / Kiosk layout
--------------------------------------------------------------------------- */

.dashboard-shell {
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--color-bg);
  overflow: hidden;
  position: relative;
}

.dashboard-panel {
  position: absolute;
  inset: var(--tv-safe-zone);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

/* ---------------------------------------------------------------------------
   Admin layout
--------------------------------------------------------------------------- */

.admin-layout {
  display: grid;
  grid-template-columns: 220px 1fr;
  gap: var(--space-6);
  min-height: calc(100vh - var(--topbar-height));
}

.admin-sidebar {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  padding: var(--space-4) 0;
  height: fit-content;
  position: sticky;
  top: calc(var(--topbar-height) + var(--space-6));
}

@media (max-width: 1024px) {
  .admin-layout {
    grid-template-columns: 1fr;
  }
  .admin-sidebar {
    position: static;
  }
}


/* ---------------------------------------------------------------------------
   Mobile navigation — top bar, drawer, and turning on the tab bar

   Everything here is deliberately at the END of the file. `.tab-bar`,
   `.mobile-bar` and `.drawer` are all `display: none` by default further up,
   and these overrides only win because they come after. Move this block and
   the phone loses its navigation again.
--------------------------------------------------------------------------- */

.mobile-bar { display: none; }
.drawer      { display: none; }

@media (max-width: 768px) {

  /* ---- the bar that was never visible ---- */
  .tab-bar { display: flex; }

  /* ---- top bar ---- */
  .mobile-bar {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    position: fixed;
    top: 0; left: 0; right: 0;
    height: calc(52px + var(--safe-top));
    padding: var(--safe-top) var(--space-2) 0;
    background: color-mix(in srgb, var(--color-surface) 92%, transparent);
    backdrop-filter: blur(12px);
    border-bottom: 1px solid var(--color-border);
    z-index: calc(var(--z-sticky) + 1);
  }

  .mobile-bar-title {
    flex: 1;
    text-align: center;
    font-family: var(--font-display);
    font-size: var(--text-base);
    color: var(--color-accent-hi);
    letter-spacing: 0.02em;
  }

  /* 44px is the smallest target iOS treats as reliably tappable. */
  .mobile-bar-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px; height: 44px;
    flex-shrink: 0;
    background: none;
    border: none;
    padding: 0;
    color: var(--color-text-muted);
    cursor: pointer;
  }
  .mobile-bar-btn svg { width: 22px; height: 22px; }

  /* The fixed top bar sits over the page, so the page has to start below it. */
  .main-content { padding-top: calc(52px + var(--safe-top)); }

  /* ---- drawer ---- */
  .drawer {
    display: flex;
    flex-direction: column;
    position: fixed;
    top: 0; bottom: 0; left: 0;
    width: min(84vw, 320px);
    background: var(--color-surface);
    border-right: 1px solid var(--color-border);
    z-index: var(--z-modal, 400);
    padding-top: var(--safe-top);
    padding-bottom: var(--safe-bottom);
    /* Off-canvas rather than hidden, so it can slide. visibility keeps it out
       of the tab order while closed — a screen reader should not walk a menu
       that is not on screen. */
    transform: translateX(-102%);
    visibility: hidden;
    transition: transform 220ms ease, visibility 0s linear 220ms;
  }
  .drawer.open {
    transform: translateX(0);
    visibility: visible;
    transition: transform 220ms ease, visibility 0s;
  }

  .drawer-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-3);
    padding: var(--space-4);
    border-bottom: 1px solid var(--color-border);
  }
  .drawer-title {
    font-family: var(--font-display);
    font-size: var(--text-lg);
    color: var(--color-accent-hi);
  }
  .drawer-tagline {
    font-size: 12px;
    letter-spacing: 0.16em;
    text-transform: uppercase;
    color: var(--color-text-subtle);
    margin-top: 2px;
  }

  .drawer-nav {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: var(--space-3) 0;
  }

  /* The drawer reuses .nav-item, which the 1024px query above strips down to
     an icon for the collapsed sidebar. Put the labels back. */
  .drawer .nav-item {
    justify-content: flex-start;
    padding: var(--space-3) var(--space-5);
    min-height: 44px;
  }
  .drawer .nav-item-text { display: block; }
  .drawer .nav-section-label { display: block; }

  .drawer-foot {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    padding: var(--space-4);
    border-top: 1px solid var(--color-border);
  }
  .drawer-foot .user-info { display: block; }

  .drawer-scrim {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    z-index: calc(var(--z-modal, 400) - 1);
    opacity: 0;
    transition: opacity 220ms ease;
  }
  .drawer-scrim.open { opacity: 1; }

  /* A <button> in the tab bar needs the anchor's reset to match its siblings. */
  .tab-item { background: none; border: none; cursor: pointer; font-family: inherit; }
}

/* Nothing above 768px should ever show the phone chrome, whatever else the
   page does. */
@media (min-width: 769px) {
  .mobile-bar, .drawer, .drawer-scrim, .tab-bar { display: none !important; }
}
