:root {
  --bg: #f1f5f9;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #0f766e;
  --primary-hover: #0d5f59;
  --in: #15803d;
  --in-bg: #dcfce7;
  --out: #b91c1c;
  --out-bg: #fee2e2;
  --warn: #b45309;
  --warn-bg: #fef3c7;
  --radius: 12px;
}

* { box-sizing: border-box; }
html, body {
  width: 100%;
  max-width: 100%;
  overflow-x: hidden;
}
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
  -webkit-tap-highlight-color: transparent;
}

.hidden { display: none !important; }

/* LOGIN */
.login-wrap {
  width: 100%;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.login-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 24px 20px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 10px 28px rgba(15, 23, 42, 0.08);
}
.login-card h1 { margin: 0 0 4px; font-size: 1.4rem; color: var(--primary); }
.login-card p { margin: 0 0 18px; color: var(--muted); font-size: 0.9rem; }
.login-err {
  background: var(--out-bg);
  color: var(--out);
  padding: 9px 12px;
  border-radius: 8px;
  font-size: 0.86rem;
  margin-bottom: 12px;
}

/* TOPBAR */
.topbar {
  width: 100%;
  position: sticky;
  top: 0;
  z-index: 30;
  background: #0f172a;
  color: #fff;
  padding: 12px 16px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.brand { min-width: 0; }
.brand h1 { margin: 0; font-size: 1.12rem; }
.brand small { color: #94a3b8; font-size: 0.78rem; display: block; }

.top-right { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; max-width: 100%; }
.user-chip {
  font-size: 0.8rem;
  background: #1e293b;
  padding: 5px 10px;
  border-radius: 999px;
  color: #e2e8f0;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.tabs { display: flex; gap: 6px; flex-wrap: wrap; }
.tab-btn {
  background: transparent;
  color: #cbd5e1;
  border: 1px solid #334155;
  padding: 7px 13px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.88rem;
}
.tab-btn.active { background: var(--primary); border-color: var(--primary); color: #fff; }

.container {
  width: 100%;
  max-width: 100%;
  margin: 14px 0;
  padding: 0 16px 48px;
}
.panel { display: none; width: 100%; }
.panel.active { display: block; }

.cards {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}
.card {
  width: 100%;
  min-width: 0;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 13px 14px;
}
.card-label { font-size: 0.78rem; color: var(--muted); margin-bottom: 3px; }
.card-value { font-size: 1.25rem; font-weight: 700; word-break: break-word; }
.text-in { color: var(--in); }
.text-out { color: var(--out); }
.text-primary { color: var(--primary); }

.box {
  width: 100%;
  min-width: 0;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 14px;
}
.box-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
}
.box-header h2, .box-header h3 { margin: 0; font-size: 1rem; }

.row {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: end;
}
.grid-2 {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}
.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1 1 180px;
  min-width: 0;
  max-width: 100%;
}
.field label { font-size: 0.8rem; font-weight: 600; color: var(--muted); }
input, select, textarea {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  padding: 9px 11px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  font: inherit;
  font-size: 0.95rem;
  background: #fff;
  min-height: 40px;
}
input[type="checkbox"] { width: auto; min-height: auto; }
input:focus, select:focus, textarea:focus { outline: 2px solid #99f6e4; border-color: var(--primary); }

.btn {
  padding: 9px 14px;
  border: 1px solid transparent;
  border-radius: 8px;
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 40px;
  text-decoration: none;
  white-space: nowrap;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-outline { background: #fff; border-color: #cbd5e1; color: var(--text); }
.btn-outline:hover { background: #f8fafc; }
.btn-danger { background: #dc2626; color: #fff; }
.btn-sm { padding: 5px 10px; font-size: 0.8rem; min-height: 32px; }
.btn-block { width: 100%; }

.table-wrap { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
th, td { padding: 9px 8px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { background: #f8fafc; color: var(--muted); font-size: 0.76rem; text-transform: uppercase; letter-spacing: 0.03em; white-space: nowrap; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.nowrap { white-space: nowrap; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 700;
  white-space: nowrap;
}
.badge-in { background: var(--in-bg); color: var(--in); }
.badge-out { background: var(--out-bg); color: var(--out); }
.badge-warn { background: var(--warn-bg); color: var(--warn); }
.badge-link { background: #ccfbf1; color: #0f766e; cursor: pointer; border: none; }

.scanner-box {
  width: 100%;
  background: #0f172a;
  border-radius: 12px;
  overflow: hidden;
  position: relative;
  min-height: 240px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #94a3b8;
}
.scanner-box video { width: 100%; max-height: 360px; object-fit: cover; display: block; }
.scan-status {
  width: 100%;
  margin-top: 10px;
  padding: 11px 12px;
  border-radius: 9px;
  font-size: 0.9rem;
  display: none;
  word-break: break-word;
}
.scan-status.ok { display: block; background: var(--in-bg); color: var(--in); }
.scan-status.err { display: block; background: var(--out-bg); color: var(--out); }
.scan-status.warn { display: block; background: var(--warn-bg); color: var(--warn); }

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 14px;
  z-index: 60;
}
.modal-backdrop.open { display: flex; }
.modal {
  background: #fff;
  border-radius: 14px;
  max-width: 520px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  padding: 18px;
}
.modal-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; gap: 8px; }
.modal-head h3 { margin: 0; }
.proof-preview { max-width: 100%; max-height: 65vh; border-radius: 8px; display: block; margin: 10px auto; }

.qr-print-sheet { display: none; width: 100%; }
.qr-grid { width: 100%; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
.qr-card {
  border: 1.5px dashed #64748b;
  border-radius: 8px;
  padding: 12px 10px;
  text-align: center;
  break-inside: avoid;
  background: #fff;
}
.qr-card .qr-org { font-size: 0.72rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.qr-card .qr-house { font-size: 1.05rem; font-weight: 800; margin: 4px 0 2px; }
.qr-card .qr-name { font-size: 0.92rem; font-weight: 600; margin-bottom: 6px; }
.qr-card .qr-code { font-size: 0.75rem; color: var(--muted); margin-top: 4px; }

.link-btn {
  background: none;
  border: none;
  color: var(--primary);
  font-weight: 600;
  cursor: pointer;
  padding: 0;
  font: inherit;
  text-decoration: underline;
}

/* BOTTOM NAV MOBILE */
.bottom-nav { display: none; }

@media (max-width: 900px) {
  .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .grid-2 { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 720px) {
  .topbar { padding: 10px 12px; }
  .topbar .tabs { display: none; }
  .brand h1 { font-size: 1.02rem; }
  .container { padding: 0 10px 92px; margin: 10px 0; }
  body.role-user .container { padding-bottom: 28px; }
  .card { padding: 11px; }
  .card-value { font-size: 1.02rem; }
  .field { flex: 1 1 100%; width: 100%; }
  .box-header .row { width: 100%; }
  .box-header .row .btn { flex: 1 1 auto; }
  input, select, textarea { font-size: 16px; min-height: 44px; }
  .btn { min-height: 44px; }
  .btn-sm { min-height: 36px; }
  .scanner-box { min-height: 260px; }

  body.role-admin .bottom-nav {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 40;
    background: #ffffff;
    border-top: 1px solid var(--border);
    padding: 4px 4px calc(4px + env(safe-area-inset-bottom));
  }
  .bnav-btn {
    background: none;
    border: none;
    color: var(--muted);
    font: inherit;
    font-size: 0.72rem;
    font-weight: 600;
    padding: 6px 2px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    cursor: pointer;
    min-width: 0;
  }
  .bnav-btn.active { color: var(--primary); }
  .bnav-btn svg { width: 20px; height: 20px; stroke: currentColor; fill: none; stroke-width: 2; }
}

@media print {
  body { background: #fff; }
  .topbar, .no-print, .bottom-nav, .login-wrap { display: none !important; }
  .container { width: 100%; max-width: 100%; margin: 0; padding: 0; }
  body.printing-qr .panel,
  body.printing-qr .modal-backdrop { display: none !important; }
  body.printing-qr .qr-print-sheet { display: block !important; }
  body:not(.printing-qr) .qr-print-sheet { display: none !important; }
  .box, .card { border: none; padding: 0; margin-bottom: 10px; }
}
