:root {
  --teal: #0f766e;
  --teal-dark: #115e59;
  --teal-light: #ccfbf1;
  --bg: #f1f5f9;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --danger: #dc2626;
  --ok: #16a34a;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom);
}

.hidden { display: none !important; }
.view { display: none; }
.view.active { display: block; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }

/* ---------- LOGIN ---------- */
#view-login {
  min-height: 100vh;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: linear-gradient(160deg, var(--teal) 0%, var(--teal-dark) 100%);
}
#view-login.active { display: flex; }
.login-card {
  background: var(--card);
  border-radius: 20px;
  padding: 32px 24px;
  width: 100%;
  max-width: 380px;
  box-shadow: 0 20px 40px rgba(0,0,0,.25);
  text-align: center;
}
.login-card .logo { font-size: 48px; }
.login-card h1 { margin: 8px 0 4px; font-size: 1.5rem; }
.login-card input {
  width: 100%; padding: 14px; margin: 16px 0 8px;
  border: 1px solid var(--border); border-radius: 12px; font-size: 1rem;
}
.error-text { color: var(--danger); font-size: .9rem; min-height: 1.2em; margin: 8px 0 0; }

/* ---------- HEADER ---------- */
.app-header {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  background: var(--teal); color: #fff;
  padding: calc(env(safe-area-inset-top) + 12px) 12px 12px;
  font-weight: 600; font-size: 1.1rem;
  box-shadow: var(--shadow);
}
#header-title { flex: 1; text-align: center; }
.icon-btn {
  background: rgba(255,255,255,.15); color: #fff; border: none;
  width: 38px; height: 38px; border-radius: 10px; font-size: 1.4rem;
  line-height: 1; cursor: pointer; display: flex; align-items: center; justify-content: center;
}
.icon-btn:active { background: rgba(255,255,255,.3); }

main { padding: 16px; max-width: 640px; margin: 0 auto; }

/* ---------- MENÚ ---------- */
.menu-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-top: 8px; }
.menu-card {
  background: var(--card); border: 1px solid var(--border); border-radius: 16px;
  padding: 22px 12px; display: flex; flex-direction: column; align-items: center; gap: 10px;
  font-size: .95rem; font-weight: 600; color: var(--text); cursor: pointer;
  box-shadow: var(--shadow); min-height: 120px; justify-content: center; text-align: center;
}
.menu-card:active { transform: scale(.97); }
.mc-ico { font-size: 34px; }
#data-status { margin-top: 20px; }

/* ---------- FORM ---------- */
.form { background: var(--card); border-radius: 16px; padding: 18px; box-shadow: var(--shadow); }
.form label { display: block; font-weight: 600; font-size: .9rem; margin: 14px 0 6px; }
.form label:first-child { margin-top: 0; }
.req { color: var(--danger); }
.opt { color: var(--muted); font-weight: 400; font-size: .8rem; }
.form input[type=text], .form input[type=number], .form input[type=date], .form input[type=password] {
  width: 100%; padding: 13px; border: 1px solid var(--border);
  border-radius: 12px; font-size: 1rem; background: #fff;
}
.form input:focus { outline: none; border-color: var(--teal); box-shadow: 0 0 0 3px var(--teal-light); }
.readonly { background: #f8fafc !important; color: var(--teal-dark); font-weight: 700; }
.select-box {
  width: 100%; padding: 13px; border: 1px solid var(--border); border-radius: 12px;
  font-size: 1rem; background: #fff; color: var(--text); -webkit-appearance: none; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%2364748b' d='M1 1l5 5 5-5'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 14px center;
}
.select-box:focus { outline: none; border-color: var(--teal); box-shadow: 0 0 0 3px var(--teal-light); }
.info-banner {
  background: #fffbeb; border: 1px solid #fde68a; color: #92400e;
  padding: 10px 14px; border-radius: 12px; font-size: .85rem; margin-bottom: 14px;
}

/* ---------- BUTTONS ---------- */
.btn { border: none; border-radius: 12px; font-size: 1rem; font-weight: 600; cursor: pointer; padding: 14px; }
.btn-primary { background: var(--teal); color: #fff; }
.btn-primary:active { background: var(--teal-dark); }
.btn-primary:disabled { opacity: .5; }
.btn-block { width: 100%; margin-top: 20px; }

/* ---------- ITEM ROWS (varios EPIs / materiales) ---------- */
.item-rows { display: flex; flex-direction: column; gap: 8px; }
.item-row { display: flex; align-items: center; gap: 8px; }
.item-grow { flex: 1; min-width: 0; }
.item-qty { width: 64px; padding: 13px 8px; border: 1px solid var(--border); border-radius: 12px; font-size: 1rem; text-align: center; }
.item-remove {
  flex: 0 0 auto; width: 38px; height: 46px; border: 1px solid var(--border); background: #fff;
  border-radius: 12px; color: var(--danger); font-size: 1rem; cursor: pointer;
}
.item-remove:active { background: #fef2f2; }
.btn-add {
  margin-top: 8px; width: 100%; padding: 11px; border: 1px dashed var(--teal);
  background: var(--teal-light); color: var(--teal-dark); border-radius: 12px;
  font-size: .9rem; font-weight: 600; cursor: pointer;
}
.btn-add:active { background: #b9f1e6; }

/* ---------- SEGMENTED ---------- */
.segmented { display: flex; border: 1px solid var(--border); border-radius: 12px; overflow: hidden; }
.segmented button {
  flex: 1; padding: 13px; background: #fff; border: none; font-size: .95rem;
  font-weight: 600; color: var(--muted); cursor: pointer;
}
.segmented button.active { background: var(--teal); color: #fff; }

/* ---------- SEARCHABLE SELECT ---------- */
.ss { position: relative; }
.ss-input {
  width: 100%; padding: 13px; border: 1px solid var(--border); border-radius: 12px;
  font-size: 1rem; background: #fff; cursor: pointer;
}
.ss-input:focus { outline: none; border-color: var(--teal); box-shadow: 0 0 0 3px var(--teal-light); }
.ss-panel {
  position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 30;
  background: #fff; border: 1px solid var(--border); border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0,0,0,.15); max-height: 240px; overflow-y: auto; display: none;
}
.ss-panel.open { display: block; }
.ss-option { padding: 12px 14px; font-size: .95rem; cursor: pointer; border-bottom: 1px solid #f1f5f9; }
.ss-option:last-child { border-bottom: none; }
.ss-option:active, .ss-option.hl { background: var(--teal-light); }
.ss-empty { padding: 12px 14px; color: var(--muted); font-size: .9rem; }

/* ---------- SIGNATURE ---------- */
.sig-trigger {
  width: 100%; padding: 22px 14px; border: 2px dashed var(--teal); border-radius: 12px;
  background: var(--teal-light); color: var(--teal-dark); font-size: 1rem; font-weight: 600;
  cursor: pointer;
}
.sig-trigger.signed { border-style: solid; border-color: var(--ok); background: #f0fdf4; color: var(--ok); }
.sig-wrap { border: 2px dashed var(--border); border-radius: 12px; background: #fff; position: relative; }
.sig-canvas { width: 100%; height: 240px; display: block; border-radius: 12px; touch-action: none; }
.sig-hint { position: absolute; top: 0; right: 0; bottom: 0; left: 0; display: flex; align-items: center; justify-content: center;
  color: #cbd5e1; pointer-events: none; font-size: .9rem; }

/* ---------- MODAL (firma / clave) ---------- */
.modal {
  position: fixed; top: 0; right: 0; bottom: 0; left: 0; z-index: 80; background: rgba(15,23,42,.55);
  display: flex; align-items: center; justify-content: center; padding: 18px;
}
.modal.hidden { display: none; }
.modal-card {
  background: #fff; border-radius: 18px; padding: 18px; width: 100%; max-width: 460px;
  box-shadow: 0 20px 50px rgba(0,0,0,.35);
}
.modal-card h3 { margin: 0 0 12px; font-size: 1.05rem; text-align: center; }
.modal-actions { display: flex; gap: 10px; margin-top: 14px; }
.modal-actions .btn { flex: 1; }
.btn-ghost { background: #f1f5f9; color: var(--text); }
.btn-ghost:active { background: #e2e8f0; }
.sig-card .sig-wrap { border-color: var(--teal); }
.gate-card { max-width: 340px; text-align: center; }
.gate-ico { font-size: 40px; }
.gate-input {
  width: 100%; padding: 14px; margin: 6px 0; border: 1px solid var(--border);
  border-radius: 12px; font-size: 1.3rem; text-align: center; letter-spacing: .3em;
}
.gate-error { color: var(--danger); font-size: .85rem; min-height: 1.1em; margin: 2px 0 0; }

/* ---------- BADGE ---------- */
.badge {
  display: inline-block; font-style: normal; font-size: .65rem; font-weight: 700;
  padding: 2px 6px; border-radius: 6px; background: var(--border); color: var(--muted);
  vertical-align: middle; margin-right: 6px;
}
.badge-epi { background: #ede9fe; color: #6d28d9; }

/* ---------- STOCK LIST ---------- */
.section-title { margin: 24px 0 10px; font-size: 1rem; }
.stock-list { display: flex; flex-direction: column; gap: 8px; }
.stock-row {
  display: flex; justify-content: space-between; align-items: center;
  background: var(--card); border: 1px solid var(--border); border-radius: 12px;
  padding: 12px 16px; box-shadow: var(--shadow);
}
.stock-row .qty { font-weight: 700; font-size: 1.1rem; color: var(--teal-dark); }
.stock-row .qty.zero { color: var(--danger); }

/* ---------- PLANTILLAS ---------- */
.search-box {
  width: 100%; padding: 13px; margin-bottom: 12px; border: 1px solid var(--border);
  border-radius: 12px; font-size: 1rem; background: #fff;
}
.search-box:focus { outline: none; border-color: var(--teal); box-shadow: 0 0 0 3px var(--teal-light); }
.plantillas-list { display: flex; flex-direction: column; gap: 10px; }
.plantilla-card {
  background: var(--card); border: 1px solid var(--border); border-radius: 14px;
  padding: 14px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 10px;
}
.plantilla-info { display: flex; align-items: center; gap: 10px; }
.plantilla-ico { font-size: 22px; }
.plantilla-nombre { font-weight: 600; font-size: .95rem; }
.plantilla-actions { display: flex; gap: 8px; }
.btn-mini {
  flex: 1; text-align: center; text-decoration: none; padding: 10px; border-radius: 10px;
  font-size: .9rem; font-weight: 600; border: 1px solid var(--border); color: var(--text); background: #f8fafc;
}
.btn-mini-primary { background: var(--teal); color: #fff; border-color: var(--teal); }
.btn-mini:active { opacity: .85; }

/* ---------- TOAST + LOADER ---------- */
.toast {
  position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%);
  background: #0f172a; color: #fff; padding: 14px 20px; border-radius: 12px;
  font-size: .92rem; z-index: 60; max-width: 90%; text-align: center; box-shadow: 0 8px 24px rgba(0,0,0,.3);
}
.toast.ok { background: var(--ok); }
.toast.err { background: var(--danger); }
.loader-overlay {
  position: fixed; top: 0; right: 0; bottom: 0; left: 0; background: rgba(255,255,255,.6); z-index: 70;
  display: flex; align-items: center; justify-content: center;
}
.spinner {
  width: 44px; height: 44px; border: 4px solid var(--teal-light);
  border-top-color: var(--teal); border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
