:root {
  --primary: #0f766e;
  --primary-dark: #115e59;
  --primary-light: #ccfbf1;
  --accent: #14b8a6;
  --bg: #f1f5f9;
  --surface: #ffffff;
  --chat-bg: #e8eef3;
  --bubble-own: #d1fae5;
  --bubble-other: #ffffff;
  --text: #0f172a;
  --text-muted: #64748b;
  --border: #e2e8f0;
  --danger: #dc2626;
  --danger-bg: #fef2f2;
  --success: #16a34a;
  --success-bg: #dcfce7;
  --shadow-sm: 0 1px 3px rgba(15, 23, 42, 0.08);
  --shadow-md: 0 6px 20px rgba(15, 23, 42, 0.12);
  --radius-lg: 18px;
  --radius-md: 12px;
  --nav-height: 68px;
}

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

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  line-height: 1.45;
  overscroll-behavior-y: contain;
}

.hidden {
  display: none !important;
}

/* =========================================================
   PANTALLA DE ACCESO (LOGIN)
   ========================================================= */
.login-screen {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px 16px env(safe-area-inset-bottom);
  background: linear-gradient(160deg, #0f766e 0%, #134e4a 100%);
}

.login-card {
  width: 100%;
  max-width: 440px;
  background: var(--surface);
  border-radius: 24px;
  padding: 24px 20px;
  box-shadow: 0 20px 45px rgba(0, 0, 0, 0.25);
}

.login-brand {
  text-align: center;
  margin-bottom: 20px;
}

.brand-badge {
  width: 60px;
  height: 60px;
  margin: 0 auto 10px;
  border-radius: 18px;
  background: var(--primary-light);
  color: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
}

.login-brand h1 {
  margin: 0;
  font-size: 1.6rem;
  font-weight: 800;
  letter-spacing: -0.02em;
}

.login-brand p {
  margin: 4px 0 0;
  color: var(--text-muted);
  font-size: 0.95rem;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.field-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.field-label {
  font-size: 0.92rem;
  font-weight: 700;
  color: var(--text);
}

.member-presets {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.member-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 48px;
  padding: 8px 12px;
  border: 2px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  color: var(--text);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 0.15s, background-color 0.15s, transform 0.1s;
  text-align: left;
}

.member-chip:active {
  transform: scale(0.98);
}

.member-chip.selected {
  border-color: var(--primary);
  background: #f0fdfa;
}

.avatar-dot {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  color: #ffffff;
  font-weight: 700;
  font-size: 0.9rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  text-transform: uppercase;
}

.text-input {
  width: 100%;
  min-height: 48px;
  padding: 12px 14px;
  border: 2px solid var(--border);
  border-radius: var(--radius-md);
  font-size: 16px;
  color: var(--text);
  background: var(--surface);
  outline: none;
  transition: border-color 0.15s;
}

.text-input:focus {
  border-color: var(--primary);
}

.code-input {
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-weight: 700;
}

.color-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.color-swatch {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 3px solid transparent;
  cursor: pointer;
  transition: transform 0.12s, border-color 0.12s;
}

.color-swatch.selected {
  border-color: var(--text);
  transform: scale(1.1);
}

.error-banner {
  margin: 0;
  padding: 12px 14px;
  border-radius: var(--radius-md);
  background: var(--danger-bg);
  color: var(--danger);
  font-size: 0.92rem;
  font-weight: 600;
}

.btn-primary {
  width: 100%;
  min-height: 52px;
  border: none;
  border-radius: 14px;
  background: var(--primary);
  color: #ffffff;
  font-size: 1.05rem;
  font-weight: 700;
  cursor: pointer;
  transition: background-color 0.15s, transform 0.1s;
}

.btn-primary:hover {
  background: var(--primary-dark);
}

.btn-primary:active {
  transform: scale(0.99);
}

/* =========================================================
   SHELL PRINCIPAL DE LA APP
   ========================================================= */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 640px;
  margin: 0 auto;
  background: var(--surface);
  box-shadow: 0 0 30px rgba(15, 23, 42, 0.08);
  overflow: hidden;
}

/* CABECERA Y CONTROL DE UBICACIÓN */
.app-header {
  flex-shrink: 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding-top: env(safe-area-inset-top);
  z-index: 20;
}

.header-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px 6px;
}

.user-pill {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.user-meta {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.user-name {
  font-weight: 800;
  font-size: 1.05rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.connection-status {
  font-size: 0.78rem;
  color: var(--text-muted);
}

.connection-status.online {
  color: var(--success);
  font-weight: 600;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 2px;
}

.btn-icon {
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 12px;
  background: transparent;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.btn-icon.notify-active {
  color: var(--primary);
  background: var(--primary-light);
}

.btn-icon.notify-denied {
  color: var(--danger);
  opacity: 0.7;
}

.btn-icon:active {
  background: var(--bg);
}

/* INTERRUPTOR PRINCIPAL DE UBICACIÓN */
.location-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 4px 12px 10px;
  padding: 10px 14px;
  border-radius: 14px;
  background: var(--bg);
  border: 1px solid var(--border);
}

.location-label-wrap {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.location-title {
  font-size: 0.92rem;
  font-weight: 700;
  color: var(--text);
}

.state-badge {
  font-size: 0.76rem;
  font-weight: 800;
  padding: 3px 8px;
  border-radius: 999px;
  letter-spacing: 0.03em;
}

.state-off {
  background: #e2e8f0;
  color: #475569;
}

.state-on {
  background: var(--success-bg);
  color: var(--success);
}

.switch-btn {
  position: relative;
  width: 58px;
  height: 34px;
  border-radius: 999px;
  border: none;
  background: #cbd5e1;
  padding: 3px;
  cursor: pointer;
  flex-shrink: 0;
  transition: background-color 0.2s ease;
}

.switch-btn[aria-checked="true"] {
  background: var(--success);
}

.switch-thumb {
  display: block;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: #ffffff;
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
  transition: transform 0.2s ease;
}

.switch-btn[aria-checked="true"] .switch-thumb {
  transform: translateX(24px);
}

.demo-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 14px;
  background: #fef9c3;
  color: #854d0e;
  font-size: 0.82rem;
  border-top: 1px solid #fde047;
}

.demo-banner button {
  border: none;
  background: transparent;
  color: #854d0e;
  font-size: 1rem;
  font-weight: 700;
  padding: 4px 8px;
  cursor: pointer;
}

.gps-error-banner,
.chat-error-banner {
  padding: 8px 14px;
  background: var(--danger-bg);
  color: var(--danger);
  font-size: 0.85rem;
  font-weight: 600;
  border-top: 1px solid #fecaca;
}

/* =========================================================
   CONTENIDO DE PESTAÑAS
   ========================================================= */
.app-main {
  flex: 1;
  min-height: 0;
  position: relative;
}

.tab-panel {
  display: none;
  height: 100%;
  width: 100%;
}

.tab-panel.active {
  display: flex;
  flex-direction: column;
}

/* PESTAÑA 1: CHAT FAMILIAR */
#tab-chat {
  background: var(--chat-bg);
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  scroll-behavior: smooth;
}

.empty-state {
  margin: auto;
  text-align: center;
  color: var(--text-muted);
  background: rgba(255, 255, 255, 0.8);
  padding: 16px 20px;
  border-radius: 16px;
}

.date-divider {
  align-self: center;
  margin: 8px 0 4px;
  padding: 4px 12px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.85);
  color: var(--text-muted);
  font-size: 0.78rem;
  font-weight: 700;
  box-shadow: var(--shadow-sm);
}

.msg-row {
  display: flex;
  flex-direction: column;
  max-width: 82%;
}

.msg-row.own {
  align-self: flex-end;
}

.msg-row.other {
  align-self: flex-start;
}

.msg-bubble {
  padding: 9px 13px 7px;
  border-radius: 16px;
  box-shadow: var(--shadow-sm);
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.msg-row.own .msg-bubble {
  background: var(--bubble-own);
  border-bottom-right-radius: 4px;
}

.msg-row.other .msg-bubble {
  background: var(--bubble-other);
  border-bottom-left-radius: 4px;
}

.msg-author {
  font-size: 0.82rem;
  font-weight: 800;
  margin-bottom: 2px;
}

.msg-text {
  font-size: 0.98rem;
  color: var(--text);
  white-space: pre-wrap;
}

.msg-time {
  display: block;
  text-align: right;
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-top: 3px;
}

/* Barra inferior fija del chat */
.chat-composer {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.chat-input {
  flex: 1;
  min-height: 46px;
  padding: 10px 16px;
  border: 1.5px solid var(--border);
  border-radius: 999px;
  font-size: 16px;
  background: var(--bg);
  color: var(--text);
  outline: none;
}

.chat-input:focus {
  border-color: var(--primary);
  background: var(--surface);
}

.btn-send {
  width: 48px;
  height: 48px;
  border: none;
  border-radius: 50%;
  background: var(--primary);
  color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  cursor: pointer;
  transition: background-color 0.15s, transform 0.1s;
}

.btn-send:active {
  transform: scale(0.94);
  background: var(--primary-dark);
}

/* PESTAÑA 2: MAPA FAMILIAR */
#tab-map {
  position: relative;
}

.family-map {
  width: 100%;
  height: 100%;
  z-index: 1;
}

.map-overlay-top {
  position: absolute;
  top: 10px;
  left: 10px;
  right: 10px;
  z-index: 500;
  pointer-events: none;
}

.active-members-strip {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
  scrollbar-width: none;
}

.active-members-strip::-webkit-scrollbar {
  display: none;
}

.map-member-pill {
  pointer-events: auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid rgba(15, 23, 42, 0.08);
  background: rgba(255, 255, 255, 0.95);
  box-shadow: var(--shadow-md);
  font-size: 0.84rem;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
  cursor: pointer;
}

.pill-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
}

.btn-fab {
  position: absolute;
  right: 14px;
  bottom: 18px;
  z-index: 500;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: none;
  background: var(--surface);
  color: var(--primary);
  box-shadow: var(--shadow-md);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.btn-fab:active {
  transform: scale(0.95);
}

.map-empty-toast {
  position: absolute;
  left: 16px;
  right: 16px;
  bottom: 84px;
  z-index: 500;
  background: rgba(15, 23, 42, 0.88);
  color: #ffffff;
  padding: 12px 16px;
  border-radius: 14px;
  font-size: 0.9rem;
  text-align: center;
  box-shadow: var(--shadow-md);
}

/* Marcadores personalizados de Leaflet */
.family-marker-wrap {
  background: transparent;
  border: none;
}

.family-marker {
  display: flex;
  flex-direction: column;
  align-items: center;
  transform: translate(-50%, -85%);
}

.family-marker-pin {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 3px solid #ffffff;
  color: #ffffff;
  font-weight: 800;
  font-size: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.family-marker-label {
  margin-top: 3px;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.96);
  color: var(--text);
  font-size: 0.76rem;
  font-weight: 800;
  white-space: nowrap;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.2);
}

/* =========================================================
   NAVEGACIÓN INFERIOR (2 PESTAÑAS)
   ========================================================= */
.bottom-nav {
  flex-shrink: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  height: calc(var(--nav-height) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
  z-index: 20;
}

.nav-tab {
  position: relative;
  border: none;
  background: transparent;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  font-size: 0.85rem;
  font-weight: 700;
  cursor: pointer;
}

.nav-tab.active {
  color: var(--primary);
}

.nav-tab.active::before {
  content: "";
  position: absolute;
  top: 0;
  left: 22%;
  right: 22%;
  height: 3px;
  border-radius: 0 0 4px 4px;
  background: var(--primary);
}

.tab-badge {
  position: absolute;
  top: 8px;
  right: calc(50% - 26px);
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--success);
  color: #ffffff;
  font-size: 0.72rem;
  font-weight: 800;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
