:root {
  --bg: #f1f4f9;
  --panel: #ffffff;
  --border: #dfe5ee;
  --text: #1c2733;
  --muted: #6b7889;
  --primary: #1666c4;
  --primary-dark: #0f4f99;
  --danger: #d64545;
  --success: #1e9e5a;
  --warning: #d98a1f;
  --radius: 10px;
  --shadow: 0 2px 10px rgba(20, 40, 70, .08);
  --card-hover-shadow: 0 6px 16px rgba(20, 40, 70, .12);
  --modal-shadow: 0 20px 50px rgba(10, 25, 50, .3);
  --overlay: rgba(12, 28, 52, .5);
  --th-bg: #f7f9fc;
  --row-hover: #f6f9fd;
  --input-bg: #ffffff;
  --toast-bg: #1f2d3d;
  --toast-text: #ffffff;
  --scrollbar-thumb: #c6d0dd;
  --scrollbar-thumb-hover: #aebacc;
  --badge-green-bg: #e3f4ea; --badge-green-text: #187a46;
  --badge-red-bg: #fbe7e7; --badge-red-text: #b33232;
  --badge-orange-bg: #fdf0dc; --badge-orange-text: #a96a14;
  --badge-blue-bg: #e2edfb; --badge-blue-text: #17508f;
  --badge-gray-bg: #eef1f5; --badge-gray-text: #5c6877;
  --tag-bg: #eef1f5; --tag-text: #4a5768;
  --login-error-bg: #fdeeee; --login-error-text: #b42318; --login-error-border: #f5c6c6;
  --code-bg: #eef2f7;
  --primary-ring: rgba(22, 102, 196, .15);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1420;
    --panel: #151d2b;
    --border: #283244;
    --text: #e8edf5;
    --muted: #8996a8;
    --primary: #4c8dff;
    --primary-dark: #2f6fe0;
    --danger: #ff6b6b;
    --success: #3ddc84;
    --warning: #ffb454;
    --shadow: 0 2px 10px rgba(0, 0, 0, .35);
    --card-hover-shadow: 0 6px 18px rgba(0, 0, 0, .45);
    --modal-shadow: 0 20px 50px rgba(0, 0, 0, .55);
    --overlay: rgba(0, 0, 0, .6);
    --th-bg: #1b2433;
    --row-hover: #1c2636;
    --input-bg: #101826;
    --scrollbar-thumb: #354256;
    --scrollbar-thumb-hover: #46536b;
    --badge-green-bg: rgba(61, 220, 132, .16); --badge-green-text: #5fe3a0;
    --badge-red-bg: rgba(255, 107, 107, .16); --badge-red-text: #ff8f8f;
    --badge-orange-bg: rgba(255, 180, 84, .16); --badge-orange-text: #ffc477;
    --badge-blue-bg: rgba(76, 141, 255, .18); --badge-blue-text: #7fabff;
    --badge-gray-bg: rgba(255, 255, 255, .08); --badge-gray-text: #aab4c4;
    --tag-bg: rgba(255, 255, 255, .08); --tag-text: #b7c1d1;
    --login-error-bg: rgba(255, 107, 107, .12); --login-error-text: #ff9b9b; --login-error-border: rgba(255, 107, 107, .35);
    --code-bg: rgba(255, 255, 255, .08);
    --primary-ring: rgba(76, 141, 255, .25);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0d1420;
  --panel: #151d2b;
  --border: #283244;
  --text: #e8edf5;
  --muted: #8996a8;
  --primary: #4c8dff;
  --primary-dark: #2f6fe0;
  --danger: #ff6b6b;
  --success: #3ddc84;
  --warning: #ffb454;
  --shadow: 0 2px 10px rgba(0, 0, 0, .35);
  --card-hover-shadow: 0 6px 18px rgba(0, 0, 0, .45);
  --modal-shadow: 0 20px 50px rgba(0, 0, 0, .55);
  --overlay: rgba(0, 0, 0, .6);
  --th-bg: #1b2433;
  --row-hover: #1c2636;
  --input-bg: #101826;
  --scrollbar-thumb: #354256;
  --scrollbar-thumb-hover: #46536b;
  --badge-green-bg: rgba(61, 220, 132, .16); --badge-green-text: #5fe3a0;
  --badge-red-bg: rgba(255, 107, 107, .16); --badge-red-text: #ff8f8f;
  --badge-orange-bg: rgba(255, 180, 84, .16); --badge-orange-text: #ffc477;
  --badge-blue-bg: rgba(76, 141, 255, .18); --badge-blue-text: #7fabff;
  --badge-gray-bg: rgba(255, 255, 255, .08); --badge-gray-text: #aab4c4;
  --tag-bg: rgba(255, 255, 255, .08); --tag-text: #b7c1d1;
  --login-error-bg: rgba(255, 107, 107, .12); --login-error-text: #ff9b9b; --login-error-border: rgba(255, 107, 107, .35);
  --code-bg: rgba(255, 255, 255, .08);
  --primary-ring: rgba(76, 141, 255, .25);
  color-scheme: dark;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  display: flex;
  transition: background-color .2s ease, color .2s ease;
}

/* ---------- Sidebar ---------- */
.sidebar {
  position: relative;
  width: 250px;
  min-width: 250px;
  background: linear-gradient(180deg, #12305c, #0b2142);
  color: #dbe7f6;
  display: flex;
  flex-direction: column;
  padding: 20px 14px;
  gap: 18px;
  transition: width .2s ease, min-width .2s ease;
}

.sidebar-collapse-btn {
  display: none;
  position: absolute; top: 22px; right: -13px; z-index: 5;
  width: 26px; height: 26px; border-radius: 50%;
  background: var(--primary); color: #fff; border: 2px solid var(--bg);
  align-items: center; justify-content: center; cursor: pointer; padding: 0;
  transition: background-color .15s;
}
.sidebar-collapse-btn:hover { background: var(--primary-dark); }
.sidebar-collapse-btn svg { width: 13px; height: 13px; fill: currentColor; transition: transform .2s ease; }

@media (min-width: 861px) {
  .sidebar-collapse-btn { display: inline-flex; }

  html.sidebar-collapsed .sidebar { width: 68px; min-width: 68px; }
  html.sidebar-collapsed .sidebar-collapse-btn svg { transform: rotate(180deg); }
  html.sidebar-collapsed .brand { justify-content: center; padding: 4px 0 16px; }
  html.sidebar-collapsed .brand-text,
  html.sidebar-collapsed .nav-item span,
  html.sidebar-collapsed .sidebar-footer .btn-ghost span { display: none; }
  html.sidebar-collapsed .nav-item { justify-content: center; padding: 11px 0; }
  html.sidebar-collapsed .sidebar-footer .btn-ghost { justify-content: center; padding: 9px 0; }
}

.brand { display: flex; align-items: center; gap: 12px; padding: 4px 6px 16px; border-bottom: 1px solid rgba(255,255,255,.12); }

.brand-logo {
  width: 42px; height: 42px;
  background: var(--primary);
  color: #fff;
  border-radius: 10px;
  display: grid; place-items: center;
  font-size: 22px; font-weight: 700;
}

.brand h1 { font-size: 16px; color: #fff; }
.brand p { font-size: 11px; color: #9db4d1; margin-top: 2px; }

.nav { display: flex; flex-direction: column; gap: 4px; flex: 1; }

.nav-item {
  display: flex; align-items: center; gap: 12px;
  background: transparent; border: none; color: #c5d5ea;
  padding: 11px 12px; border-radius: 8px;
  font-size: 14px; cursor: pointer; text-align: left;
  width: 100%; font-family: inherit;
  transition: background .15s, color .15s;
}

.nav-item svg { width: 19px; height: 19px; fill: currentColor; }

.nav-item:hover { background: rgba(255,255,255,.08); color: #fff; }
.nav-item.active { background: var(--primary); color: #fff; box-shadow: 0 2px 8px rgba(22,102,196,.4); }

.sidebar-footer { border-top: 1px solid rgba(255,255,255,.12); padding-top: 14px; }

.btn-ghost {
  background: transparent; border: 1px solid rgba(255,255,255,.25);
  color: #dbe7f6; border-radius: 8px; padding: 9px 12px;
  cursor: pointer; font-size: 13px; font-family: inherit;
  display: inline-flex; align-items: center; gap: 8px;
  transition: background .15s;
}
.btn-ghost:hover { background: rgba(255,255,255,.1); }
.btn-ghost svg { width: 16px; height: 16px; fill: currentColor; }

.nav-toggle {
  display: none;
  margin-left: auto;
  background: transparent; border: 1px solid rgba(255,255,255,.25);
  color: #dbe7f6; border-radius: 8px; width: 38px; height: 38px;
  align-items: center; justify-content: center; cursor: pointer; flex-shrink: 0;
}
.nav-toggle svg { width: 20px; height: 20px; fill: currentColor; }

/* ---------- Main ---------- */
.main { flex: 1; overflow-y: auto; padding: 28px 32px; }

.tab { display: none; }
.tab.active { display: block; }

.section-head {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 16px; margin-bottom: 22px; flex-wrap: wrap;
}

.section-head h2 { font-size: 21px; font-weight: 600; }
.section-head p { color: var(--muted); margin-top: 4px; }

.head-actions { display: flex; gap: 10px; align-items: center; }

.search {
  width: 260px; padding: 9px 12px; border: 1px solid var(--border);
  border-radius: 8px; font-size: 13px; font-family: inherit;
  background: var(--panel);
}
.search:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-ring); }

.btn {
  border: none; border-radius: 8px; padding: 9px 16px;
  font-size: 13px; font-weight: 600; cursor: pointer; font-family: inherit;
  transition: filter .15s;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { filter: brightness(.92); }
.btn-sm { padding: 5px 10px; font-size: 12px; font-weight: 500; }

/* ---------- Cards ---------- */
.cards {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 16px; margin-bottom: 24px;
}

.card {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 18px; cursor: pointer; transition: transform .15s, box-shadow .15s;
}
.card:hover { transform: translateY(-2px); box-shadow: var(--card-hover-shadow); }
.card .card-num { font-size: 30px; font-weight: 700; color: var(--primary); }
.card .card-label { color: var(--muted); margin-top: 4px; font-size: 13px; }

/* ---------- Panel ---------- */
.panel {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px;
}
.panel h3 { font-size: 15px; margin-bottom: 14px; }

.nota-mini {
  border-bottom: 1px solid var(--border); padding: 10px 2px;
  display: flex; justify-content: space-between; gap: 12px; align-items: baseline;
}
.nota-mini:last-child { border-bottom: none; }
.nota-mini .nm-title { font-weight: 600; }
.nota-mini .nm-meta { color: var(--muted); font-size: 12px; white-space: nowrap; }

/* ---------- Tables ---------- */
.table-wrap {
  background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden;
}

table { width: 100%; border-collapse: collapse; }

th {
  text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); background: var(--th-bg); padding: 12px 14px;
  border-bottom: 1px solid var(--border); white-space: nowrap;
}

td { padding: 11px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:hover { background: var(--row-hover); }
tbody tr:last-child td { border-bottom: none; }
.col-actions { width: 110px; text-align: right; }

.badge {
  display: inline-block; padding: 3px 9px; border-radius: 20px;
  font-size: 11px; font-weight: 600; white-space: nowrap;
}
.badge-green { background: var(--badge-green-bg); color: var(--badge-green-text); }
.badge-red { background: var(--badge-red-bg); color: var(--badge-red-text); }
.badge-orange { background: var(--badge-orange-bg); color: var(--badge-orange-text); }
.badge-blue { background: var(--badge-blue-bg); color: var(--badge-blue-text); }
.badge-gray { background: var(--badge-gray-bg); color: var(--badge-gray-text); }

.tag {
  display: inline-block; background: var(--tag-bg); color: var(--tag-text);
  border-radius: 4px; padding: 2px 7px; font-size: 11px; margin: 2px 2px 0 0;
}

.cell-actions { display: flex; gap: 6px; justify-content: flex-end; }

.icon-btn {
  background: transparent; border: 1px solid var(--border); border-radius: 6px;
  width: 30px; height: 30px; cursor: pointer; color: var(--muted);
  display: inline-grid; place-items: center; transition: all .15s;
}
.icon-btn svg { width: 15px; height: 15px; fill: currentColor; }
.icon-btn:hover { color: var(--primary); border-color: var(--primary); }
.icon-btn.danger:hover { color: var(--danger); border-color: var(--danger); }

.empty { text-align: center; color: var(--muted); padding: 32px; }

.pw-dots { letter-spacing: 2px; color: var(--muted); cursor: pointer; user-select: none; }
.pw-dots:hover { color: var(--primary); }

/* ---------- Modal ---------- */
.modal-backdrop, .confirm-backdrop {
  position: fixed; inset: 0; background: var(--overlay);
  display: flex; align-items: center; justify-content: center;
  z-index: 50; padding: 20px; overflow-y: auto;
}

.modal {
  background: var(--panel); border-radius: 12px; width: 620px; max-width: 100%;
  max-height: 92vh; display: flex; flex-direction: column;
  box-shadow: var(--modal-shadow);
}

.modal-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 18px 22px; border-bottom: 1px solid var(--border);
}
.modal-head h3 { font-size: 16px; }

.modal-close {
  background: transparent; border: none; font-size: 24px; cursor: pointer;
  color: var(--muted); line-height: 1;
}
.modal-close:hover { color: var(--text); }

.modal-body { padding: 20px 22px; overflow-y: auto; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid .full { grid-column: 1 / -1; }

.field label { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 5px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--border);
  border-radius: 8px; font-size: 13px; font-family: inherit; background: var(--input-bg); color: var(--text);
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-ring);
}
.field textarea { resize: vertical; min-height: 90px; }
.field .hint { font-size: 11px; color: var(--muted); margin-top: 4px; }

.modal-foot {
  display: flex; justify-content: flex-end; gap: 10px;
  padding: 15px 22px; border-top: 1px solid var(--border);
}

.link-title {
  color: var(--primary); text-decoration: none; font-weight: 600;
  cursor: pointer;
}
.link-title:hover { text-decoration: underline; }

.modal-lg { width: 760px; }

.view-meta {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  color: var(--muted); font-size: 12px; margin-bottom: 14px;
}

.view-content {
  background: var(--th-bg); border: 1px solid var(--border); border-radius: 8px;
  padding: 18px; font-size: 14px; line-height: 1.65; white-space: normal;
  max-height: 55vh; overflow-y: auto;
}

.nota-mini.clickable { cursor: pointer; border-radius: 6px; padding: 10px 8px; transition: background .15s; }
.nota-mini.clickable:hover { background: var(--row-hover); }

.ticket-status {
  margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--border);
}
.ticket-status > label { font-size: 12px; font-weight: 600; color: var(--muted); display: block; margin-bottom: 6px; }
.status-row { display: flex; gap: 10px; align-items: center; }
.status-row select {
  flex: 1; padding: 9px 11px; border: 1px solid var(--border); border-radius: 8px;
  font-size: 13px; font-family: inherit; background: var(--input-bg); color: var(--text);
}
.status-row select:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-ring); }

.history { margin-top: 20px; }
.history h4 { font-size: 14px; margin-bottom: 10px; }

.history-list { border: 1px solid var(--border); border-radius: 8px; overflow: hidden; max-height: 280px; overflow-y: auto; }
.history-item { padding: 10px 14px; border-bottom: 1px solid var(--border); background: var(--panel); }
.history-item:last-child { border-bottom: none; }
.history-head { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.history-accion { font-weight: 600; font-size: 13px; }
.history-fecha { color: var(--muted); font-size: 11px; white-space: nowrap; }
.history-desc { color: var(--text); font-size: 13px; margin-top: 3px; white-space: pre-wrap; }

.history-add { margin-top: 10px; display: flex; gap: 8px; align-items: flex-end; }
.history-add textarea {
  flex: 1; padding: 9px 11px; border: 1px solid var(--border); border-radius: 8px;
  font-size: 13px; font-family: inherit; min-height: 52px; resize: vertical;
}
.history-add textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-ring); }

/* ---------- Confirm ---------- */
.confirm {
  background: var(--panel); border-radius: 12px; padding: 24px;
  width: 380px; max-width: 100%; box-shadow: var(--modal-shadow);
}
.confirm h3 { font-size: 16px; margin-bottom: 8px; }
.confirm p { color: var(--muted); margin-bottom: 20px; }
.confirm-actions { display: flex; justify-content: flex-end; gap: 10px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 24px; right: 24px; z-index: 100;
  background: var(--toast-bg); color: var(--toast-text); padding: 12px 18px; border-radius: 8px;
  font-size: 13px; box-shadow: 0 6px 18px rgba(0,0,0,.25);
  opacity: 0; transform: translateY(8px); transition: all .25s;
}
.toast.show { opacity: 1; transform: translateY(0); }

/* ---------- Scrollbar ---------- */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 6px; }
::-webkit-scrollbar-thumb:hover { background: var(--scrollbar-thumb-hover); }

@media (max-width: 860px) {
  body { flex-direction: column; }
  .sidebar { width: 100%; min-width: 0; padding: 10px 14px; gap: 0; }
  .brand { border-bottom: none; padding: 4px 0; }

  /* Menú (Inicio, Notas, Credenciales...) colapsado por defecto: se abre con el botón */
  .nav-toggle { display: inline-flex; }
  .nav, .sidebar-footer { display: none; }
  .sidebar.nav-open .nav {
    display: flex; flex-direction: column; gap: 2px;
    margin-top: 10px; padding-top: 10px; border-top: 1px solid rgba(255,255,255,.12);
  }
  .sidebar.nav-open .nav-item { width: 100%; }
  .sidebar.nav-open .sidebar-footer {
    display: flex; flex-direction: column; gap: 8px; margin-top: 10px;
  }

  .form-grid { grid-template-columns: 1fr; }
  .main { padding: 16px; }
  .search { width: 100%; }

  /* Evita que Safari en iOS haga zoom automático al enfocar campos */
  .search, .field input, .field select, .field textarea, .login-box input {
    font-size: 16px;
  }

  /* Tablas -> tarjetas apiladas (en vez de forzar scroll horizontal) */
  .table-wrap { overflow-x: visible; background: transparent; border: none; box-shadow: none; }
  .table-wrap table, .table-wrap thead, .table-wrap tbody,
  .table-wrap tr, .table-wrap th, .table-wrap td { display: block; }
  .table-wrap thead { display: none; }
  .table-wrap tbody { display: flex; flex-direction: column; gap: 10px; }
  .table-wrap tbody tr {
    background: var(--panel); border: 1px solid var(--border);
    border-radius: var(--radius); box-shadow: var(--shadow);
    padding: 14px; transition: box-shadow .15s, transform .15s;
  }
  .table-wrap tbody tr:active { transform: scale(.99); }
  .table-wrap td {
    border-bottom: none; padding: 5px 0;
    display: flex; align-items: center; justify-content: space-between; gap: 10px;
    text-align: right;
  }
  .table-wrap td:empty { display: none; }
  .table-wrap td:first-child {
    display: block; text-align: left; font-size: 15.5px; font-weight: 600;
    padding: 0 0 9px; margin-bottom: 6px; border-bottom: 1px solid var(--border);
  }
  .table-wrap td[data-label]::before {
    content: attr(data-label);
    font-size: 11px; font-weight: 600; text-transform: uppercase;
    letter-spacing: .03em; color: var(--muted); flex-shrink: 0;
  }
  .table-wrap td:last-child { justify-content: flex-end; padding-top: 10px; }
}

/* ---------- Teléfonos (iPhone 15 y similares, ancho ~390-430px) ---------- */
@media (max-width: 480px) {
  .brand-logo { width: 36px; height: 36px; font-size: 18px; }
  .brand h1 { font-size: 14px; }
  .brand p { display: none; }

  .section-head h2 { font-size: 18px; }
  .head-actions { width: 100%; flex-direction: column; align-items: stretch; }
  .head-actions .btn { width: 100%; }

  .icon-btn { width: 38px; height: 38px; }

  .modal-head { padding: 14px 16px; }
  .modal-body { padding: 16px; }
  .modal-foot { padding: 12px 16px; flex-direction: column-reverse; }
  .modal-foot .btn { width: 100%; }

  .history-add { flex-direction: column; align-items: stretch; }
  .status-row { flex-direction: column; align-items: stretch; }

  .login-box { padding: 22px 18px; }

  .toast {
    left: 16px; right: 16px;
    bottom: calc(16px + env(safe-area-inset-bottom));
    text-align: center;
  }
}

/* ---------- Login ---------- */
body.logged-out .sidebar,
body.logged-out .main { display: none; }

.login-backdrop {
  position: fixed; inset: 0; z-index: 200;
  background: linear-gradient(160deg, #12305c, #0b2142);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}

.login-box {
  width: 360px; max-width: 100%;
  background: var(--panel); border-radius: 14px;
  padding: 28px 30px;
  box-shadow: 0 18px 50px rgba(0,0,0,.35);
}

.login-brand { text-align: center; margin-bottom: 22px; }
.login-brand .brand-logo { width: 54px; height: 54px; font-size: 28px; margin: 0 auto 10px; }
.login-brand h1 { font-size: 19px; color: var(--text); }
.login-brand p { font-size: 12px; color: var(--muted); margin-top: 4px; }

.login-box label { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin: 12px 0 5px; }
.login-box input {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: 8px; font-size: 14px; font-family: inherit;
  background: var(--input-bg); color: var(--text);
}
.login-box input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-ring); }
.login-box .btn { width: 100%; margin-top: 18px; }

.login-error {
  margin-top: 12px; background: var(--login-error-bg); color: var(--login-error-text);
  border: 1px solid var(--login-error-border); border-radius: 8px; padding: 9px 12px;
  font-size: 13px;
}

.login-hint { text-align: center; font-size: 11px; color: var(--muted); margin-top: 16px; }
.login-hint code { background: var(--code-bg); padding: 1px 5px; border-radius: 4px; }
