:root {
  --bg: #14181f;
  --surface: #1d232d;
  --surface-hi: #262e3a;
  --text: #f2f4f7;
  --muted: #8a93a3;
  --accent: #4f8cff;
  --accent-active: #3a6fd8;
  --success: #34c77b;
  --error: #ff5c5c;
  --danger: #ff5c5c;
  --radius: 18px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
  overscroll-behavior-y: contain;
}

.screen {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: env(safe-area-inset-top) 20px env(safe-area-inset-bottom);
}

.hidden { display: none !important; }

h1 {
  font-size: 22px;
  font-weight: 700;
  margin: 0;
}

.muted { color: var(--muted); font-size: 14px; }

/* Login screen */
#login-screen { justify-content: center; }
.pin-box {
  width: 100%;
  max-width: 320px;
  text-align: center;
}
.pin-box h1 { margin-bottom: 4px; }
.pin-box p { margin-top: 4px; margin-bottom: 24px; }

.pin-box input {
  width: 100%;
  font-size: 17px;
  text-align: center;
  padding: 14px 0;
  border-radius: var(--radius);
  border: none;
  background: var(--surface);
  color: var(--text);
  margin-bottom: 12px;
}
.pin-box input:focus { outline: 2px solid var(--accent); }

#login-submit {
  width: 100%;
  padding: 14px 0;
  border-radius: var(--radius);
  border: none;
  background: var(--accent);
  color: white;
  font-size: 17px;
  font-weight: 600;
  margin-top: 4px;
}
#login-submit:active { background: var(--accent-active); }

.error { color: var(--error); margin-top: 12px; }

/* Header (main / settings / user form) */
#main-screen, #settings-screen, #user-form-screen { width: 100%; max-width: 480px; margin: 0 auto; }
header {
  width: 100%;
  padding: 24px 4px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
header h1 { flex: 1; }
#settings-screen header h1, #user-form-screen header h1 { text-align: center; }

.header-actions {
  display: flex;
  gap: 8px;
  min-width: 40px;
  justify-content: flex-end;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  border: none;
  background: var(--surface);
  color: var(--text);
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:active { background: var(--surface-hi); }

/* Tabs */
.tabs {
  width: 100%;
  display: flex;
  gap: 8px;
  padding: 4px 4px 16px;
  overflow-x: auto;
}
.tab {
  flex: 0 0 auto;
  padding: 10px 18px;
  border-radius: 999px;
  border: none;
  background: var(--surface);
  color: var(--muted);
  font-size: 14px;
  font-weight: 600;
}
.tab.active {
  background: var(--accent);
  color: white;
}

#gate-list {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding-bottom: 40px;
}

.gate-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px 22px;
  border: none;
  width: 100%;
  color: var(--text);
  text-align: left;
  font-family: inherit;
}

.gate-card:active { background: var(--surface-hi); }

.gate-name {
  font-size: 17px;
  font-weight: 600;
}

.gate-status {
  font-size: 13px;
  color: var(--muted);
  margin-top: 2px;
  min-height: 16px;
}

.gate-status.ok { color: var(--success); }
.gate-status.error { color: var(--error); }
.gate-status.live { color: var(--accent); font-weight: 600; }

.gate-divider {
  width: 100%;
  border: none;
  border-top: 1px solid var(--surface-hi);
  margin: 10px 0 4px;
}

.camera-block {
  width: 100%;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface);
  position: relative;
}

.camera-feed {
  width: 100%;
  height: auto;
  display: block;
  background: #000;
}

.camera-feed.camera-error {
  opacity: 0.3;
}

.camera-label {
  position: absolute;
  left: 10px;
  bottom: 8px;
  font-size: 12px;
  font-weight: 600;
  color: white;
  background: rgba(0, 0, 0, 0.45);
  padding: 3px 9px;
  border-radius: 999px;
}

#app-footer {
  width: 100%;
  text-align: center;
  color: var(--muted);
  opacity: 0.45;
  font-size: 12px;
  padding: 24px 0 16px;
}

.gate-icon {
  width: 46px;
  height: 46px;
  border-radius: 14px;
  background: var(--surface-hi);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-left: 16px;
  transition: background 0.2s, transform 0.15s;
}

.gate-icon svg { width: 24px; height: 24px; stroke: var(--accent); }

.gate-icon.pulsing {
  background: var(--accent);
  transform: scale(0.92);
}
.gate-icon.pulsing svg { stroke: white; }

#loading-screen { justify-content: center; align-items: center; }

/* Settings: user list */
#user-list {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-bottom: 40px;
}

.user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px 20px;
  border: none;
  width: 100%;
  color: var(--text);
  text-align: left;
  font-family: inherit;
}
.user-row:active { background: var(--surface-hi); }

.user-name { font-size: 16px; font-weight: 600; }
.user-scope { margin-top: 2px; }

/* User form */
.form-main {
  width: 100%;
  display: flex;
  flex-direction: column;
  padding-bottom: 40px;
}

.field-label {
  font-size: 13px;
  color: var(--muted);
  margin: 16px 0 6px;
}

.form-main input[type="text"],
.form-main input[type="password"] {
  width: 100%;
  font-size: 16px;
  padding: 12px 14px;
  border-radius: 12px;
  border: none;
  background: var(--surface);
  color: var(--text);
}
.form-main input:focus { outline: 2px solid var(--accent); }

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 0;
  font-size: 15px;
}
.checkbox-row input[type="checkbox"] {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

.gates-checklist {
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-radius: 14px;
  padding: 4px 14px;
  margin-top: 6px;
}
.gates-checklist .gate-check { border-bottom: 1px solid var(--surface-hi); }
.gates-checklist .gate-check:last-child { border-bottom: none; }

.form-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 28px;
}

.btn-primary {
  padding: 14px 0;
  border-radius: var(--radius);
  border: none;
  background: var(--accent);
  color: white;
  font-size: 17px;
  font-weight: 600;
}
.btn-primary:active { background: var(--accent-active); }

.btn-danger {
  padding: 14px 0;
  border-radius: var(--radius);
  border: none;
  background: transparent;
  color: var(--danger);
  font-size: 15px;
  font-weight: 600;
}

/* Invito utente (link + QR) */
.invite-section {
  margin-top: 32px;
  padding-top: 24px;
  border-top: 1px solid var(--surface-hi);
}

.btn-secondary {
  width: 100%;
  padding: 12px 0;
  border-radius: 14px;
  border: 1px solid var(--surface-hi);
  background: var(--surface);
  color: var(--text);
  font-size: 15px;
  font-weight: 600;
  margin-top: 10px;
}
.btn-secondary:active { background: var(--surface-hi); }

.invite-result {
  margin-top: 18px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

#invite-qr {
  width: 180px;
  height: 180px;
  border-radius: 12px;
  background: white;
  padding: 10px;
}

.invite-link-row {
  width: 100%;
  display: flex;
  gap: 8px;
}

.invite-link-row input {
  flex: 1;
  min-width: 0;
  font-size: 13px;
  padding: 10px 12px;
  border-radius: 10px;
  border: none;
  background: var(--surface);
  color: var(--muted);
}

/* Toggle mostra/nascondi password */
.password-field {
  position: relative;
  width: 100%;
}
.password-field input {
  width: 100%;
  padding-right: 46px !important;
}
.toggle-password {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 17px;
  border-radius: 10px;
}
.toggle-password:active { background: var(--surface-hi); }
.toggle-password.showing { color: var(--accent); }

/* Lista dispositivi Face ID/Touch ID */
.biometric-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid var(--surface-hi);
}
.biometric-row:last-child { border-bottom: none; }
.biometric-row button {
  border: none;
  background: transparent;
  color: var(--danger);
  font-size: 13px;
  font-weight: 600;
}
