/* ── Reset & base ─────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:       #0f172a;
  --surface:  #1e293b;
  --border:   #334155;
  --text:     #e2e8f0;
  --muted:    #94a3b8;
  --accent:   #2563eb;
  --accent-h: #3b82f6;
  --danger:   #ef4444;
  --radius:   8px;
  --sidebar-w: 240px;
}

html, body { height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

/* Eliminate 300ms tap delay and prevent iOS auto-zoom on input focus */
button, input, select, textarea, [role="button"] {
  touch-action: manipulation;
}
input, select, textarea {
  font-size: 1rem;
}

/* ── Login screen ─────────────────────────────────────── */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
}
#login-screen.hidden { display: none; }

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 2.5rem 2rem;
  width: 100%;
  max-width: 380px;
}

.login-card h1 {
  font-size: 1.5rem;
  margin-bottom: 1.5rem;
  text-align: center;
}

.login-card label {
  display: block;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 0.25rem;
}

.login-card input {
  width: 100%;
  padding: 0.6rem 0.75rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
  margin-bottom: 1rem;
  outline: none;
  transition: border-color 0.15s;
}
.login-card input:focus { border-color: var(--accent); }

.login-card button {
  width: 100%;
  padding: 0.65rem;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
.login-card button:hover { background: var(--accent-h); }

#login-error {
  color: var(--danger);
  font-size: 0.85rem;
  text-align: center;
  margin-top: 0.75rem;
  min-height: 1.3em;
}

/* ── App shell ────────────────────────────────────────── */
#app-shell { display: none; height: 100vh; }
#app-shell.active { display: flex; }

/* Sidebar */
#sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1rem 0;
  overflow-y: auto;
}

.sidebar-brand {
  font-size: 1.25rem;
  font-weight: 700;
  padding: 0 1.25rem 1rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 0.5rem;
}

.sidebar-nav { flex: 1; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.6rem 1.25rem;
  color: var(--muted);
  text-decoration: none;
  font-size: 0.95rem;
  cursor: pointer;
  transition: background 0.1s, color 0.1s;
  border: none;
  background: none;
  width: 100%;
  text-align: left;
}
.nav-item:hover { background: var(--bg); color: var(--text); }
.nav-item.active { color: var(--accent-h); background: var(--bg); }

.nav-icon {
  width: 20px;
  height: 20px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
}

.sidebar-footer {
  padding: 0.75rem 1.25rem;
  border-top: 1px solid var(--border);
  margin-top: 0.5rem;
}

.sidebar-footer .username {
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 0.35rem;
}

#logout-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--muted);
  padding: 0.35rem 0.75rem;
  font-size: 0.8rem;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}
#logout-btn:hover { border-color: var(--danger); color: var(--danger); }

/* Viewport */
#app-viewport {
  flex: 1;
  padding: 1.5rem 2rem;
  padding-bottom: max(1.5rem, env(safe-area-inset-bottom));
  overflow-y: auto;
}
#app-viewport h2 { margin-bottom: 1rem; }

/* ── Install banner ───────────────────────────────────── */
.install-banner {
  position: fixed;
  bottom: 1rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem 1rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  z-index: 1000;
  font-size: 0.9rem;
  animation: slide-up 0.3s ease-out;
  max-width: calc(100vw - 2rem);
}
.install-banner.hidden { display: none; }

.install-actions { display: flex; align-items: center; gap: 0.5rem; flex-shrink: 0; }

.install-accept {
  padding: 0.4rem 1rem;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
  white-space: nowrap;
}
.install-accept:hover { background: var(--accent-h); }

.install-dismiss {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 0 0.25rem;
  line-height: 1;
}
.install-dismiss:hover { color: var(--text); }

@keyframes slide-up {
  from { transform: translateX(-50%) translateY(1rem); opacity: 0; }
  to   { transform: translateX(-50%) translateY(0); opacity: 1; }
}

/* ── Offline indicator ───────────────────────────────── */
.offline-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background: var(--danger);
  color: #fff;
  text-align: center;
  padding: 0.35rem;
  font-size: 0.8rem;
  font-weight: 600;
  z-index: 1001;
  animation: slide-down 0.3s ease-out;
}
.offline-bar.hidden { display: none; }

@keyframes slide-down {
  from { transform: translateY(-100%); }
  to   { transform: translateY(0); }
}

/* ── Hamburger toggle (hidden on desktop) ─────────────── */
.hamburger-btn {
  display: none;
  position: fixed;
  top: 0.65rem;
  left: 0.65rem;
  z-index: 1100;
  width: 42px;
  height: 42px;
  border: none;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,0,0,0.3);
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}

/* Sidebar backdrop (mobile overlay) */
.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 999;
  opacity: 0;
  transition: opacity 0.25s;
}
.sidebar-backdrop.visible {
  display: block;
  opacity: 1;
}

/* ── Responsive ───────────────────────────────────────── */

/* Tablet */
@media (max-width: 900px) {
  #app-viewport { padding: 1.25rem 1.25rem; }
}

/* Mobile */
@media (max-width: 768px) {
  .hamburger-btn { display: flex; }

  #app-shell.active { display: block; position: relative; }

  #sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: 280px;
    min-width: 280px;
    z-index: 1000;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
  }
  #sidebar.open { transform: translateX(0); }

  .sidebar-brand span,
  .nav-item span,
  .sidebar-footer .username { display: initial; }

  .nav-item {
    padding: 0.75rem 1.25rem;
    font-size: 1rem;
    min-height: 48px;
  }

  #app-viewport {
    padding: calc(3.5rem + env(safe-area-inset-top, 0px)) 1rem max(1rem, env(safe-area-inset-bottom, 0px));
    width: 100%;
    min-height: 100vh;
    min-height: 100dvh;
  }

  .login-card {
    margin: 0 1rem;
    padding: 2rem 1.5rem;
  }

  .install-banner {
    bottom: max(0.5rem, env(safe-area-inset-bottom, 0px));
    font-size: 0.82rem;
    padding: 0.6rem 0.75rem;
    gap: 0.6rem;
  }
}

/* Small phones */
@media (max-width: 400px) {
  #app-viewport { padding-left: 0.75rem; padding-right: 0.75rem; }
  .login-card { padding: 1.5rem 1rem; }
}

/* ── Module-level responsive helpers ──────────────────── */
@media (max-width: 768px) {
  .module-container { font-size: 0.92rem; }

  .module-container table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Module action buttons — full touch target */
  .btn-action {
    min-width: 44px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
}

/* Responsive grid utility for module forms */
.responsive-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}
@media (max-width: 480px) {
  .responsive-grid { grid-template-columns: 1fr; }
}

/* Responsive flex-wrap for stat bars */
.stat-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1.5rem;
}
