:root {
  --bg: #f3f5f9; --panel: #ffffff; --line: #e2e8f0; --text: #0f172a; --muted: #64748b;
  --primary: #2563eb; --primary-600: #1d4ed8; --danger: #dc2626; --ok: #16a34a; --warn: #d97706;
  --navy: #0f1e3a; --navy-700: #16294b;
  --rail: 60px; --panelw: 320px;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px; color: var(--text); background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; cursor: pointer; }
input, select { font-family: inherit; font-size: 14px; }
input:focus, select:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(37,99,235,.15); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ---------------- Login ---------------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px;
  background: radial-gradient(1200px 500px at 50% -10%, #1b3160 0%, var(--navy) 60%); }
.login-card { width: 100%; max-width: 380px; background: var(--panel); border-radius: 16px;
  box-shadow: 0 20px 60px rgba(0,0,0,.35); padding: 28px; }
.login-brand { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; }
.brand-logo { width: 42px; height: 42px; border-radius: 12px; background: var(--primary);
  color: #fff; display: grid; place-items: center; }
.brand-name { font-weight: 800; font-size: 17px; line-height: 1.1; }
.brand-sub { color: var(--muted); font-size: 12px; }
.login-card label { font-size: 12px; font-weight: 600; color: var(--muted); display: block; margin: 12px 0 5px; }
.login-card input { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 9px; }
.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 9px; padding: 10px 14px; font-weight: 600; color: var(--text); }
.btn:hover { background: #f8fafc; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; width: 100%; margin-top: 18px; }
.btn-primary:hover { background: var(--primary-600); }
.btn-danger { color: var(--danger); border-color: #fecaca; background: #fff; }
.btn-sm { padding: 6px 10px; font-size: 12px; }
.login-err { color: var(--danger); font-size: 13px; margin-top: 12px; min-height: 18px; }
.login-hint { color: var(--muted); font-size: 12px; margin-top: 16px; text-align: center; }

/* ---------------- App layout ---------------- */
#app { display: flex; height: 100vh; height: 100dvh; overflow: hidden; }
.rail { width: var(--rail); background: var(--navy); color: #cbd5e1; display: flex;
  flex-direction: column; align-items: center; padding: 10px 0; gap: 4px; flex-shrink: 0; z-index: 1200; }
.rail .logo { width: 38px; height: 38px; border-radius: 11px; background: var(--primary);
  color: #fff; display: grid; place-items: center; margin-bottom: 10px; }
.rail-btn { width: 46px; height: 46px; border: none; background: transparent; color: #cbd5e1;
  border-radius: 11px; display: grid; place-items: center; position: relative; }
.rail-btn:hover { background: rgba(255,255,255,.08); color: #fff; }
.rail-btn.active { background: var(--primary); color: #fff; }
.rail-btn svg { width: 22px; height: 22px; }
.rail-btn .tip { position: absolute; left: 54px; top: 50%; transform: translateY(-50%);
  background: #0b1220; color: #fff; padding: 4px 9px; border-radius: 7px; font-size: 12px;
  white-space: nowrap; opacity: 0; pointer-events: none; transition: opacity .12s; z-index: 5; }
.rail-btn:hover .tip { opacity: 1; }
.rail-spacer { flex: 1; }

.panel { width: var(--panelw); background: var(--panel); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; flex-shrink: 0; z-index: 1100; }
.panel-head { padding: 14px 16px; border-bottom: 1px solid var(--line); display: flex;
  align-items: center; justify-content: space-between; }
.panel-head h2 { margin: 0; font-size: 15px; }
.panel-body { padding: 14px 16px; overflow: auto; flex: 1; }
.panel-close { border: none; background: transparent; color: var(--muted); font-size: 20px; line-height: 1; padding: 2px 6px; border-radius: 6px; }
.panel-close:hover { background: #f1f5f9; }

.map-wrap { flex: 1; position: relative; }
#map { position: absolute; inset: 0; }

/* field */
.field { margin-bottom: 12px; }
.field label { font-size: 12px; font-weight: 600; color: var(--muted); display: block; margin-bottom: 5px; }
.field input, .field select { width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 9px; }
.row { display: flex; gap: 8px; }
.row > * { flex: 1; }
.switch { display: flex; align-items: center; gap: 9px; cursor: pointer; font-size: 13px; font-weight: 600; color: var(--text); }
.switch input { width: 18px; height: 18px; flex: 0 0 auto; accent-color: var(--primary); cursor: pointer; }

/* search results */
.res { padding: 9px 11px; border: 1px solid var(--line); border-radius: 9px; margin-bottom: 7px; cursor: pointer; }
.res:hover { border-color: var(--primary); background: #f8fbff; }
.res .code { font-weight: 700; font-size: 13px; }
.res .name { color: var(--muted); font-size: 12px; }

/* ficha */
.ficha { position: absolute; top: 12px; right: 12px; width: 330px; max-width: calc(100% - 24px);
  max-height: calc(100% - 24px); background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; box-shadow: 0 14px 40px rgba(15,23,42,.22); display: flex; flex-direction: column; z-index: 1000; }
.ficha-head { padding: 13px 15px; border-bottom: 1px solid var(--line); display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
.ficha-head .t { font-weight: 700; font-size: 15px; }
.ficha-head .s { color: var(--muted); font-size: 12px; margin-top: 2px; }
.ficha-body { padding: 0; overflow: auto; }
.attr { display: grid; grid-template-columns: 42% 58%; border-bottom: 1px solid #f1f5f9; }
.attr dt { padding: 8px 15px; font-size: 11px; color: var(--muted); font-weight: 600;
  text-transform: uppercase; letter-spacing: .02em; background: #f8fafc; word-break: break-word; }
.attr dd { padding: 8px 15px; margin: 0; font-size: 13px; word-break: break-word; }
.ficha-actions { padding: 11px 15px; border-top: 1px solid var(--line); display: flex; gap: 8px; }

.badge { display: inline-block; font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 20px; }
.badge.admin { background: #ede9fe; color: #6d28d9; }
.badge.viewer { background: #e0f2fe; color: #0369a1; }

.user-row { display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 9px 0; border-bottom: 1px solid #f1f5f9; }
.user-row .em { font-size: 13px; word-break: break-all; }

.notice { padding: 10px 12px; border-radius: 9px; font-size: 13px; margin-bottom: 12px; }
.notice.info { background: #eff6ff; color: #1e40af; }
.notice.warn { background: #fffbeb; color: #92400e; }
.notice.err { background: #fef2f2; color: #991b1b; }
.notice.ok { background: #f0fdf4; color: #166534; }

.measure-out { font-size: 22px; font-weight: 800; margin: 6px 0; }
.weather-now { display: flex; align-items: center; gap: 12px; margin: 10px 0 14px; }
.weather-now .temp { font-size: 34px; font-weight: 800; }
.wk { display: grid; grid-template-columns: repeat(3,1fr); gap: 6px; }
.wk .d { text-align: center; border: 1px solid var(--line); border-radius: 9px; padding: 7px 4px; }
.wk .d .dd { font-size: 11px; color: var(--muted); }
.wk .d .tt { font-weight: 700; font-size: 13px; }

.rail-user { font-size: 10px; text-align: center; color: #94a3b8; max-width: 56px; word-break: break-all; line-height: 1.2; }

/* floating home button */
.home-fab { position: absolute; right: 14px; bottom: 14px; width: 46px; height: 46px; border-radius: 50%;
  background: var(--primary); color: #fff; border: none; display: grid; place-items: center;
  box-shadow: 0 8px 22px rgba(37,99,235,.45); z-index: 1000; }
.home-fab:hover { background: var(--primary-600); }

.spinner { width: 18px; height: 18px; border: 3px solid #cbd5e1; border-top-color: var(--primary);
  border-radius: 50%; animation: spin .8s linear infinite; display: inline-block; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }

.coord-box { position: absolute; left: 12px; bottom: 12px; background: rgba(255,255,255,.92);
  border: 1px solid var(--line); border-radius: 8px; padding: 4px 9px; font-size: 11px; color: var(--muted);
  z-index: 900; font-variant-numeric: tabular-nums; }

/* ---------------- Responsive ---------------- */
@media (max-width: 760px) {
  :root { --panelw: 100%; }
  /* Barra de herramientas fija abajo; el mapa ocupa el resto. Nada de
     column-reverse + 100vh (la barra se iría bajo la UI del navegador). */
  #app { display: block; }
  .map-wrap { position: fixed; top: 0; left: 0; right: 0; bottom: 58px; }
  .rail { position: fixed; left: 0; right: 0; bottom: 0; height: 58px; width: auto;
    flex-direction: row; align-items: center; padding: 0 6px; gap: 2px;
    overflow-x: auto; z-index: 1200; }
  .rail .logo { display: none; }
  .rail-btn { flex: 0 0 auto; }
  .rail-spacer { flex: 0 0 8px; }
  .rail-btn .tip { display: none; }
  .rail-user { display: none; }
  .panel { position: fixed; left: 0; right: 0; bottom: 58px; top: auto; max-height: 60dvh;
    border-right: none; border-top: 1px solid var(--line); border-radius: 16px 16px 0 0;
    box-shadow: 0 -10px 30px rgba(0,0,0,.18); z-index: 1100; }
  .ficha { top: 10px; left: 10px; right: 10px; width: auto; max-height: 48dvh; }
  .home-fab { bottom: 72px; }
  .coord-box { display: none; }
}

@media print {
  .rail, .panel, .home-fab, .coord-box, .leaflet-control-container { display: none !important; }
  #app { height: auto; }
  .map-wrap { position: static; }
  #map { position: relative; height: 70vh; }
  .ficha { position: static; width: 100%; box-shadow: none; margin-top: 10px; max-height: none; }
}
