:root {
  --brand: #0d6efd;
  --brand-muted: #6c757d;
  --bg: #f4f6f9;
  --card: #fff;
  --text: #212529;
  --border: #dee2e6;
  --sidebar-w: 240px;
  --nav-break: 900px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --touch-min: 44px;
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
}

body.nav-open {
  touch-action: none;
}

a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }

.skip-link {
  position: absolute;
  left: -9999px;
  top: auto;
  width: 1px;
  height: 1px;
  overflow: hidden;
}
.skip-link:focus {
  position: fixed;
  left: var(--safe-left);
  top: var(--safe-top);
  z-index: 10000;
  width: auto;
  height: auto;
  padding: 0.75rem 1rem;
  background: #fff;
  border: 2px solid var(--brand);
  border-radius: 8px;
  clip: auto;
}

.layout {
  display: flex;
  min-height: 100vh;
  min-height: 100dvh;
  position: relative;
}

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.5);
  z-index: 998;
  -webkit-tap-highlight-color: transparent;
}

.layout.sidebar-open .sidebar-backdrop {
  display: block;
}

.sidebar {
  width: var(--sidebar-w);
  background: #1e293b;
  color: #e2e8f0;
  padding: 1rem 0;
  padding-top: calc(1rem + var(--safe-top));
  flex-shrink: 0;
  -webkit-overflow-scrolling: touch;
}

.sidebar a {
  color: #cbd5e1;
  display: flex;
  align-items: center;
  padding: 0.65rem 1.25rem;
  font-size: 0.9rem;
  min-height: var(--touch-min);
  -webkit-tap-highlight-color: rgba(255, 255, 255, 0.08);
}
.sidebar a:hover {
  background: #334155;
  color: #fff;
  text-decoration: none;
}

.sidebar .brand {
  padding: 0 1.25rem 1rem;
  padding-left: calc(1.25rem + var(--safe-left));
  font-weight: 700;
  font-size: 1.05rem;
  border-bottom: 1px solid #334155;
  margin-bottom: 0.5rem;
}
.sidebar .brand small {
  display: block;
  font-weight: 400;
  opacity: 0.75;
  font-size: 0.75rem;
}

.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  width: 100%;
}

.topbar {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding: 0.65rem 1rem;
  padding-left: calc(1rem + var(--safe-left));
  padding-right: calc(1rem + var(--safe-right));
  padding-top: calc(0.65rem + var(--safe-top));
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 0.75rem;
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  z-index: 50;
}

.topbar-start {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
}

.topbar-center {
  flex: 1 1 200px;
  min-width: 0;
  display: flex;
  justify-content: stretch;
}

.topbar-end {
  flex: 0 1 auto;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
  justify-content: flex-end;
  margin-left: auto;
}

.topbar-user {
  font-size: 0.9rem;
  max-width: 42vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-search {
  display: flex;
  gap: 0.5rem;
  width: 100%;
  max-width: 100%;
  align-items: stretch;
}

.topbar-search input[type="search"] {
  flex: 1;
  min-width: 0;
  padding: 0.55rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 16px;
  margin: 0;
}

.topbar-logout {
  display: inline-flex;
  align-items: center;
  margin: 0;
}

.sidebar-toggle {
  display: none;
  width: var(--touch-min);
  height: var(--touch-min);
  padding: 0;
  border: none;
  border-radius: 8px;
  background: #e2e8f0;
  color: #1e293b;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
}

.sidebar-toggle-bars,
.sidebar-toggle-bars::before,
.sidebar-toggle-bars::after {
  display: block;
  width: 1.25rem;
  height: 2px;
  background: currentColor;
  border-radius: 1px;
  position: relative;
}
.sidebar-toggle-bars::before,
.sidebar-toggle-bars::after {
  content: "";
  position: absolute;
  left: 0;
}
.sidebar-toggle-bars::before { top: -6px; }
.sidebar-toggle-bars::after { top: 6px; }

.content {
  padding: 1rem;
  padding-bottom: calc(1.5rem + var(--safe-bottom));
  flex: 1;
  max-width: 100vw;
}

@media (min-width: 640px) {
  .content {
    padding: 1.5rem;
    padding-bottom: calc(1.5rem + var(--safe-bottom));
  }
}

.card {
  background: var(--card);
  border-radius: 10px;
  border: 1px solid var(--border);
  padding: 1rem;
  margin-bottom: 1rem;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
  max-width: 100%;
}

@media (min-width: 640px) {
  .card {
    padding: 1.25rem;
  }
}

h1 { font-size: clamp(1.2rem, 4vw, 1.5rem); margin: 0 0 1rem; line-height: 1.25; }
h2 { font-size: clamp(1rem, 3vw, 1.15rem); margin: 0 0 0.75rem; line-height: 1.3; }

table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

table.data th,
table.data td {
  text-align: left;
  padding: 0.55rem 0.5rem;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

table.data th {
  font-weight: 600;
  background: #f8f9fa;
}

/* Horizontal scroll for wide tables on small screens */
@media (max-width: 768px) {
  .card:has(table.data) {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
  }
  .card:has(table.data) table.data {
    min-width: min(100%, 520px);
  }
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: var(--touch-min);
  padding: 0.5rem 1rem;
  border-radius: 8px;
  border: none;
  background: var(--brand);
  color: #fff;
  cursor: pointer;
  font-size: 0.9rem;
  line-height: 1.2;
  -webkit-tap-highlight-color: transparent;
}

.btn:hover {
  filter: brightness(0.92);
  text-decoration: none;
  color: #fff;
}

.btn.secondary { background: var(--brand-muted); }
.btn.danger { background: #dc3545; }
.btn.link {
  background: transparent;
  color: var(--brand);
  min-height: var(--touch-min);
  padding: 0.35rem 0.65rem;
}

label {
  display: block;
  margin-top: 0.65rem;
  font-weight: 600;
  font-size: 0.85rem;
}

input,
select,
textarea {
  width: 100%;
  max-width: 100%;
  padding: 0.55rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-top: 0.2rem;
  font-size: 16px;
}

@media (min-width: 640px) {
  input,
  select,
  textarea {
    max-width: 480px;
    font-size: 0.95rem;
  }
  textarea {
    max-width: 640px;
  }
}

textarea {
  min-height: 100px;
}

.form-actions {
  margin-top: 1rem;
  display: flex;
  gap: 0.5rem;
  align-items: center;
  flex-wrap: wrap;
}

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 160px), 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.stat {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem;
}

.stat .num {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--brand);
}

.stat .lbl {
  font-size: 0.8rem;
  color: var(--brand-muted);
}

.alert {
  padding: 0.75rem 0.9rem;
  border-radius: 8px;
  margin-bottom: 1rem;
  font-size: 0.95rem;
}

.alert.error { background: #f8d7da; color: #842029; }
.alert.ok { background: #d1e7dd; color: #0f5132; }

.auth-body {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(1rem + var(--safe-top)) calc(1rem + var(--safe-right)) calc(1rem + var(--safe-bottom)) calc(1rem + var(--safe-left));
  background: linear-gradient(135deg, #1e293b, #334155);
}

.auth-card {
  width: 100%;
  max-width: 400px;
  background: #fff;
  border-radius: 12px;
  padding: 1.5rem;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.2);
}

@media (min-width: 480px) {
  .auth-card {
    padding: 1.75rem;
  }
}

.auth-card h1 { font-size: 1.35rem; }

.badge {
  display: inline-block;
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  font-size: 0.75rem;
  background: #e9ecef;
}

/* ——— Mobile navigation (drawer) ——— */
@media (max-width: 899px) {
  .sidebar-toggle {
    display: inline-flex;
  }

  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: min(280px, 88vw);
    max-width: calc(100vw - var(--safe-left) - var(--safe-right));
    z-index: 999;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.15);
    padding-left: 0;
  }

  .sidebar .brand {
    padding-left: calc(1.25rem + var(--safe-left));
  }

  .layout.sidebar-open .sidebar {
    transform: translateX(0);
  }

  .main {
    width: 100%;
  }

  .topbar-end {
    margin-left: auto;
    flex: 1 1 auto;
    justify-content: flex-end;
  }
}

@media (min-width: 900px) {
  .sidebar-toggle {
    display: none !important;
  }

  .topbar-start {
    display: none;
  }

  .sidebar-backdrop {
    display: none !important;
  }

  .sidebar {
    position: relative;
    transform: none !important;
    box-shadow: none;
    transition: none;
  }

  .layout.sidebar-open .sidebar {
    transform: none;
  }

  .topbar {
    flex-wrap: nowrap;
  }

  .topbar-center {
    max-width: 420px;
  }

  .topbar-end {
    margin-left: auto;
    width: auto;
  }

  .topbar-user {
    max-width: none;
  }
}
