:root {
  --bg: #f3f4f6; /* gray-100 */
  --surface: #ffffff;
  --ink: #1f2937; /* gray-800 */
  --ink-strong: #111827; /* gray-900 */
  --muted: #6b7280; /* gray-500 */
  --muted-2: #4b5563; /* gray-600 */
  --line: #e5e7eb; /* gray-200 */
  --line-strong: #d1d5db; /* gray-300 */
  --brand: #4f46e5; /* indigo-600 */
  --brand-hover: #4338ca; /* indigo-700 */
  --brand-focus: #6366f1; /* indigo-500 */
  --primary: #1f2937; /* gray-800 */
  --primary-hover: #374151; /* gray-700 */
  --ok: #059669;
  --ok-bg: #d1fae5;
  --ok-text: #065f46;
  --warn: #d97706;
  --warn-bg: #fffbeb;
  --warn-text: #92400e;
  --danger: #dc2626;
  --danger-bg: #fef2f2;
  --danger-text: #991b1b;
  --info-bg: #eef2ff;
  --info-text: #3730a3;
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.1), 0 1px 2px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  --radius: 0.5rem;
  --radius-lg: 0.5rem;
  --sidebar-w: 16rem;
  --topbar-h: 4rem;
  --font: "Figtree", ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
}

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

html, body { margin: 0; padding: 0; }

body {
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  min-height: 100vh;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body.overflow-hidden { overflow: hidden; }

a { color: var(--brand); text-decoration: none; }
a:hover { color: var(--brand-hover); text-decoration: underline; }

/* ——— App shell (sidebar + topbar) ——— */
.app-frame {
  min-height: 100vh;
  display: flex;
}

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: rgba(17, 24, 39, 0.5);
}

.sidebar {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 40;
  width: var(--sidebar-w);
  height: 100vh;
  height: 100dvh;
  background: var(--surface);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform 0.2s ease-out;
}

.app-frame.sidebar-open .sidebar { transform: translateX(0); }

.sidebar-brand {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: var(--topbar-h);
  padding: 0 1rem;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

.brand-link {
  display: inline-flex;
  align-items: center;
  gap: 0.625rem;
  color: var(--ink);
  text-decoration: none;
  font-weight: 600;
}
.brand-link:hover { color: var(--ink-strong); text-decoration: none; }

.brand-mark {
  display: block;
  width: 2rem;
  height: 2rem;
}
.brand-text { font-size: 0.875rem; }

.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;
}

.sidebar-filter {
  position: relative;
  flex-shrink: 0;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--line);
}

.sidebar-filter-icon {
  position: absolute;
  left: 1.375rem;
  top: 50%;
  width: 1rem;
  height: 1rem;
  color: #9ca3af;
  pointer-events: none;
  transform: translateY(-50%);
}
.sidebar-filter-icon svg { display: block; width: 100%; height: 100%; }

.sidebar-filter input {
  display: block;
  width: 100%;
  margin: 0;
  padding: 0.375rem 0.75rem 0.375rem 2rem;
  border: 1px solid var(--line-strong);
  border-radius: 0.375rem;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-size: 0.875rem;
  box-shadow: var(--shadow-sm);
}
.sidebar-filter input::placeholder { color: #9ca3af; }
.sidebar-filter input:focus {
  outline: none;
  border-color: var(--brand-focus);
  box-shadow: 0 0 0 1px var(--brand-focus);
}

.sidebar-nav {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 1rem 0.75rem calc(1rem + env(safe-area-inset-bottom, 0px));
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
}

.sidebar-nav ul {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.sidebar-nav a {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 0.75rem;
  border-radius: 0.375rem;
  font-size: 0.875rem;
  font-weight: 500;
  color: #374151;
  text-decoration: none;
  transition: background-color 0.15s ease, color 0.15s ease;
  min-height: 2.5rem;
  touch-action: manipulation;
}
.sidebar-nav a:hover {
  background: #f9fafb;
  color: var(--ink-strong);
  text-decoration: none;
}
.sidebar-nav a.active {
  background: #f3f4f6;
  color: var(--ink-strong);
}

.user-avatar {
  width: 2rem;
  height: 2rem;
  border-radius: 9999px;
  background: #e5e7eb;
  color: var(--ink);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  font-weight: 600;
  flex-shrink: 0;
}

.user-name {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--ink-strong);
  white-space: nowrap;
}
.user-email {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 0.125rem;
  overflow: hidden;
  text-overflow: ellipsis;
}

.logout-form { margin: 0; }

.app-main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.sidebar-collapse { display: none; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  height: var(--topbar-h);
  padding: 0 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

.topbar-title {
  flex: 1;
  min-width: 0;
}

.page-title {
  margin: 0;
  font-size: 1.125rem;
  font-weight: 600;
  color: var(--ink);
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.user-dropdown { position: relative; }

.user-menu-trigger {
  display: inline-flex;
  align-items: center;
  padding: 0.375rem;
  border: 1px solid transparent;
  border-radius: 9999px;
  color: var(--muted);
  background: #fff;
  cursor: pointer;
  transition: color 0.15s ease;
}
.user-menu-trigger:hover { color: #374151; }
.user-menu-trigger:focus {
  outline: none;
  box-shadow: 0 0 0 2px var(--brand-focus);
}

.user-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 0.5rem);
  z-index: 50;
  width: 12rem;
  overflow: hidden;
  border-radius: 0.375rem;
  background: #fff;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -4px rgba(0, 0, 0, 0.1);
  border: 1px solid rgba(0, 0, 0, 0.05);
}

.user-menu-info {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--line);
  min-width: 0;
}

.dropdown-link {
  display: block;
  width: 100%;
  padding: 0.5rem 1rem;
  border: 0;
  background: #fff;
  color: #374151;
  text-align: left;
  font-size: 0.875rem;
  cursor: pointer;
}
.dropdown-link:hover { background: #f3f4f6; color: var(--ink-strong); }

.content {
  flex: 1;
  padding: 1rem;
  width: 100%;
}

/* Topbar already shows $title — hide generic page H1 duplicates */
.content > h1:first-of-type:not(.mono):not(.show-title) {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.5rem;
  border: none;
  border-radius: 0.375rem;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  touch-action: manipulation;
}
.icon-btn:hover { background: #f3f4f6; color: var(--ink); }
.icon-btn svg { width: 1.5rem; height: 1.5rem; }
.sidebar-close svg { width: 1.25rem; height: 1.25rem; }

@media (min-width: 640px) {
  .topbar { padding: 0 1.5rem; gap: 0.75rem; }
  .content { padding: 1.5rem; }
}

@media (min-width: 1024px) {
  .sidebar { transform: translateX(0); }
  .sidebar-backdrop { display: none !important; }
  .menu-toggle { display: none; }
  .sidebar-close { display: none; }
  .app-main {
    margin-left: var(--sidebar-w);
    transition: margin-left 0.2s ease-out;
  }
  .sidebar-collapse {
    position: fixed;
    left: var(--sidebar-w);
    bottom: 1rem;
    z-index: 30;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 1.5rem;
    height: 5rem;
    padding: 0;
    border: 1px solid var(--line);
    border-radius: 0 0.375rem 0.375rem 0;
    background: #fff;
    color: var(--muted);
    box-shadow: var(--shadow-sm);
    cursor: pointer;
    transition: left 0.2s ease-out, background-color 0.15s ease;
  }
  .sidebar-collapse:hover { background: #f9fafb; }
  .sidebar-collapse svg { width: 1.25rem; height: 1.25rem; }
  .sidebar-collapse .collapse-right { display: none; }
  .app-frame.sidebar-collapsed .sidebar { transform: translateX(-100%); }
  .app-frame.sidebar-collapsed .app-main { margin-left: 0; }
  .app-frame.sidebar-collapsed .sidebar-collapse { left: 0; }
  .app-frame.sidebar-collapsed .collapse-left { display: none; }
  .app-frame.sidebar-collapsed .collapse-right { display: block; }
  .topbar { padding: 0 2rem; }
  .content { padding: 2rem; }
}

/* ——— Guest / auth ——— */
.guest-body { background: var(--bg); }
.guest-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.guest-brand {
  margin-bottom: 1.5rem;
}
.guest-brand a { display: block; }
.guest-logo {
  display: block;
  width: 5rem;
  height: 5rem;
}
.guest-card {
  width: 100%;
  max-width: 28rem;
  background: var(--surface);
  box-shadow: var(--shadow-md);
  border-radius: var(--radius-lg);
  padding: 1.5rem 1.5rem 1.25rem;
  overflow: hidden;
}
.guest-flash { width: 100%; max-width: 28rem; margin-bottom: 1rem; }
.auth-wrap { margin: 0; }
.auth-copy {
  margin: 0 0 1rem;
  color: #4b5563;
  font-size: 0.875rem;
}
.form-group { margin-top: 1rem; }
.turnstile-wrap {
  margin-top: 1rem;
}
.turnstile-wrap .cf-turnstile {
  width: 100% !important;
}
.auth-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-top: 1rem;
}
.auth-actions .btn { margin-left: 0.75rem; }
.auth-actions-end { justify-content: flex-end; }
.auth-actions-end .btn { margin-left: 0; }
.auth-link {
  color: #4b5563;
  font-size: 0.875rem;
  text-decoration: underline;
  border-radius: 0.375rem;
}
.auth-link:hover { color: var(--ink-strong); }
.auth-link:focus {
  outline: none;
  box-shadow: 0 0 0 2px #fff, 0 0 0 4px var(--brand-focus);
}

/* ——— Typography / content helpers ——— */
h1 { font-size: 1.25rem; font-weight: 600; color: var(--ink); margin: 0 0 0.75rem; }
h2 { font-size: 1rem; font-weight: 600; color: var(--ink); margin: 1.25rem 0 0.5rem; }
.lead { color: var(--muted-2); margin: 0 0 1rem; font-size: 0.875rem; }
.muted { color: var(--muted); font-size: 0.875rem; }
.mono { font-family: var(--mono); font-size: 0.8125rem; }

/* ——— Cards / grids / stats ——— */
.card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: 1rem;
  border: 1px solid transparent;
}
.grid { display: grid; gap: 1rem; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
@media (max-width: 900px) {
  .grid-2, .grid-4 { grid-template-columns: 1fr; }
}

.stat { padding: 1rem; }
.stat .n {
  font-size: 1.5rem;
  font-weight: 600;
  color: var(--ink-strong);
  line-height: 1.2;
}
.stat .l {
  color: var(--muted);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-top: 0.25rem;
}

/* ——— Flash ——— */
.flash { list-style: none; padding: 0; margin: 0 0 1rem; }
.flash li {
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  margin-bottom: 0.5rem;
  border: 1px solid;
  font-size: 0.875rem;
}
.flash .success {
  background: #ecfdf5;
  border-color: #a7f3d0;
  color: var(--ok-text);
}
.flash .error {
  background: var(--danger-bg);
  border-color: #fecaca;
  color: var(--danger-text);
}
.flash .info {
  background: var(--info-bg);
  border-color: #c7d2fe;
  color: var(--info-text);
}

/* ——— Forms ——— */
.wms-forms label,
label {
  display: block;
  font-size: 0.875rem;
  font-weight: 500;
  color: #374151;
  margin-bottom: 0.25rem;
}

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

.wms-forms input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]),
.wms-forms textarea,
.wms-forms select,
.content input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]),
.content textarea,
.content select {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--line-strong);
  border-radius: 0.375rem;
  background: #fff;
  box-shadow: var(--shadow-sm);
  font-size: 0.875rem;
  margin: 0.25rem 0 0;
  appearance: none;
}
.content select,
.wms-forms select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3E%3Cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M6 8l4 4 4-4'/%3E%3C/svg%3E");
  background-position: right 0.5rem center;
  background-repeat: no-repeat;
  background-size: 1.25rem;
  padding-right: 2.5rem;
}

.content input:focus,
.content textarea:focus,
.content select:focus,
.wms-forms input:focus,
.wms-forms textarea:focus,
.wms-forms select:focus {
  outline: none;
  border-color: var(--brand-focus);
  box-shadow: 0 0 0 1px var(--brand-focus);
}

.content input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]),
.content textarea,
.content select {
  margin-bottom: 0.75rem;
}

textarea { min-height: 80px; resize: vertical; }

.row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 0.75rem;
}

@media (max-width: 768px) {
  .content input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]),
  .content textarea,
  .content select,
  .wms-forms input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]),
  .wms-forms textarea,
  .wms-forms select {
    font-size: 16px !important; /* prevent iOS zoom */
  }
}

/* ——— Buttons (WMS primary = charcoal uppercase) ——— */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  padding: 0.5rem 1rem;
  border: 1px solid transparent;
  border-radius: 0.375rem;
  background: var(--primary);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  cursor: pointer;
  text-decoration: none;
  transition: background-color 0.15s ease, border-color 0.15s ease;
  touch-action: manipulation;
}
.btn:hover {
  background: var(--primary-hover);
  color: #fff;
  text-decoration: none;
}
.btn:focus {
  outline: none;
  box-shadow: 0 0 0 2px #fff, 0 0 0 4px var(--brand-focus);
}
.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.btn.secondary {
  background: #fff;
  color: #374151;
  border-color: var(--line-strong);
}
.btn.secondary:hover {
  background: #f9fafb;
  color: var(--ink-strong);
}
.btn.danger {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}
.btn.danger:hover { background: #b91c1c; border-color: #b91c1c; }
.btn.small {
  padding: 0.35rem 0.7rem;
  font-size: 0.7rem;
  min-height: 2rem;
}
.btn-block { width: 100%; }
button.linkish {
  background: none;
  border: none;
  color: var(--brand);
  cursor: pointer;
  padding: 0;
  font: inherit;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 0.75rem 0;
}

/* ——— Tables ——— */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.card.table-wrap { padding: 0; }
.card.table-wrap table { margin: 0; }
.card.table-wrap th:first-child,
.card.table-wrap td:first-child { padding-left: 1rem; }
.card.table-wrap th:last-child,
.card.table-wrap td:last-child { padding-right: 1rem; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}
th, td {
  text-align: left;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
}
th {
  background: #f9fafb;
  color: var(--muted);
  font-weight: 500;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding-top: 0.5rem;
  padding-bottom: 0.5rem;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover td { background: #f9fafb; }

/* ——— Badges ——— */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.125rem 0.5rem;
  border-radius: 9999px;
  font-size: 0.75rem;
  font-weight: 500;
  background: #f3f4f6;
  color: #374151;
  border: none;
}
.badge.ok { background: #d1fae5; color: #065f46; }
.badge.warn { background: #fef3c7; color: #92400e; }
.badge.bad { background: #fee2e2; color: #991b1b; }

/* ——— Sticky footer actions (mobile forms) ——— */
.sticky-actions {
  position: sticky;
  bottom: 0;
  background: rgba(255, 255, 255, 0.96);
  padding: 0.75rem 1rem;
  border-top: 1px solid var(--line);
  margin: 1rem -1rem -1rem;
  border-radius: 0 0 var(--radius-lg) var(--radius-lg);
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  backdrop-filter: blur(6px);
  padding-bottom: calc(0.75rem + env(safe-area-inset-bottom, 0px));
}
.card > .sticky-actions {
  margin-left: -1rem;
  margin-right: -1rem;
  margin-bottom: -1rem;
}
