/* ── Variables ── */
:root {
  --bg: #f4f6fb;
  --bg-panel: #fff;
  --bg-side: #111827;
  --txt: #1f2937;
  --txt-soft: #6b7280;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --danger: #dc2626;
  --success: #16a34a;
  --border: #e5e7eb;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.04);
  --radius: 0.6rem;
  --topbar-h: 56px;
  --sidebar-w: 240px;
}

*, *::before, *::after { box-sizing: border-box; }
html { font-size: 16px; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--txt);
  min-height: 100vh;
  display: flex; flex-direction: column;
  /* iOS Safari */
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
}
.hidden { display: none !important; }

/* ─── Topbar ─── */
.topbar {
  display: flex; align-items: center; gap: 0.6rem;
  height: var(--topbar-h);
  padding: 0 0.8rem;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0;
  z-index: 30;
}
.hamburger {
  background: none; border: 0;
  font-size: 1.5rem; cursor: pointer;
  padding: 0.35rem 0.55rem; line-height: 1;
}
.brand { font-weight: 700; font-size: 1.05rem; }
.search-wrap { flex: 1; min-width: 0; position: relative; }
#global-search {
  width: 100%; padding: 0.5rem 0.7rem;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--bg);
  font-size: 0.95rem;
}
.user-chip {
  display: flex; align-items: center; gap: 0.5rem;
  min-width: 0;
}
.user-chip .avatar {
  width: 34px; height: 34px;
  display: inline-flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, var(--primary), #6366f1);
  color: #fff; font-weight: 700;
  border-radius: 50%; font-size: 0.95rem;
  user-select: none; flex: 0 0 auto;
}
.user-chip .user-name {
  font-weight: 600; max-width: 90px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  display: none;
}
.btn-icon {
  display: inline-flex; align-items: center; justify-content: center;
  width: 38px; height: 38px;
  background: var(--bg); color: var(--txt);
  border: 0; border-radius: 50%;
  cursor: pointer; padding: 0;
  transition: background 0.15s, color 0.15s, transform 0.1s;
}
.btn-icon:hover { background: var(--danger); color: #fff; }
.btn-icon:active { transform: scale(0.92); }

/* ─── Search results ─── */
.search-results {
  position: absolute; top: calc(100% + 6px); left: 0; right: 0;
  background: var(--bg-panel);
  border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: 0 8px 25px rgba(0,0,0,0.18);
  max-height: 70vh; overflow: auto;
  z-index: 50;
}
.search-results .item {
  padding: 0.6rem 0.8rem; border-bottom: 1px solid var(--border);
  cursor: pointer;
}
.search-results .item:last-child { border-bottom: 0; }
.search-results .item.label {
  background: var(--bg); cursor: default;
  font-size: 0.7rem; letter-spacing: 0.05em;
  color: var(--txt-soft); text-transform: uppercase;
}
.search-results .item:not(.label):hover,
.search-results .item:not(.label):active { background: var(--bg); }

/* ─── Sidebar (drawer en móvil) ─── */
.sidebar {
  position: fixed;
  top: var(--topbar-h); left: 0; bottom: 0;
  width: var(--sidebar-w);
  background: var(--bg-side); color: #fff;
  padding: 0.75rem 0.5rem;
  display: flex; flex-direction: column; gap: 0.2rem;
  transform: translateX(-100%);
  transition: transform 0.2s;
  z-index: 25;
  overflow-y: auto;
}
.sidebar.open { transform: translateX(0); box-shadow: 0 10px 30px rgba(0,0,0,0.3); }
.sidebar-backdrop {
  position: fixed; inset: var(--topbar-h) 0 0 0;
  background: rgba(0,0,0,0.4);
  z-index: 24;
}
.nav-item {
  background: none; border: 0; color: rgba(255,255,255,0.85);
  padding: 0.75rem 0.85rem; border-radius: var(--radius);
  text-align: left;
  cursor: pointer;
  font-size: 0.95rem;
}
.nav-item:hover, .nav-item:active { background: rgba(255,255,255,0.08); color: #fff; }
.nav-item.active { background: var(--primary); color: #fff; }

/* ─── Main ─── */
.content { padding: 1rem; flex: 1; min-width: 0; }
.view { display: none; }
.view.active { display: block; }

.view-header {
  display: flex; flex-direction: column;
  align-items: stretch; gap: 0.6rem; margin-bottom: 0.9rem;
}
.view-header h2 { margin: 0; font-size: 1.3rem; }
.view-header input[type=search] {
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--border); border-radius: var(--radius);
  font-size: 0.95rem;
}
.view-header .actions { display: flex; flex-wrap: wrap; gap: 0.4rem; }

/* ─── Buttons (touch-friendly) ─── */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: 0.4rem;
  min-height: 44px; padding: 0.55rem 1rem;
  border: 0; border-radius: var(--radius);
  font-size: 0.95rem; font-weight: 500;
  cursor: pointer; font-family: inherit;
  transition: background 0.15s;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost { background: transparent; color: var(--txt); }
.btn-ghost:hover, .btn-ghost:active { background: var(--bg); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-sm { min-height: 34px; padding: 0.35rem 0.7rem; font-size: 0.85rem; }

/* ─── Auth ─── */
.auth-card {
  background: var(--bg-panel); padding: 1.5rem;
  border-radius: var(--radius); box-shadow: var(--shadow);
  width: 100%; max-width: 380px;
  margin: 2rem auto;
}
.auth-card h1 { text-align: center; margin-top: 0; }
.auth-card label { display: block; margin-bottom: 0.75rem; }
.auth-card input {
  display: block; width: 100%;
  padding: 0.6rem 0.8rem;
  border: 1px solid var(--border); border-radius: var(--radius);
  margin-top: 0.3rem; font-size: 1rem; font-family: inherit;
}
.tabs { display: flex; border-bottom: 1px solid var(--border); margin-bottom: 1rem; }
.tab {
  flex: 1; background: none; border: 0; padding: 0.7rem;
  cursor: pointer; color: var(--txt-soft); font-weight: 600;
  border-bottom: 2px solid transparent;
  min-height: 44px;
}
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }
.tabpanel { display: none; }
.tabpanel.active { display: block; }

/* ─── Stats ─── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.7rem; margin: 1rem 0;
}
.stat-card {
  background: var(--bg-panel); padding: 0.9rem;
  border-radius: var(--radius); box-shadow: var(--shadow);
  text-align: center;
}
.stat-card .v {
  font-size: 1.55rem; font-weight: 700;
  color: var(--primary); line-height: 1.1;
}
.stat-card .l { color: var(--txt-soft); font-size: 0.85rem; }

/* ─── Action cards ─── */
.actions-grid {
  display: grid; grid-template-columns: 1fr;
  gap: 0.7rem; margin-top: 1rem;
}
.action-card {
  background: var(--bg-panel); border: 0;
  border-radius: var(--radius); padding: 1.1rem 1rem;
  box-shadow: var(--shadow);
  text-align: left; cursor: pointer;
  display: flex; flex-direction: column; gap: 0.2rem;
  min-height: 80px;
}
.action-card:active { transform: scale(0.98); }
.action-card .ico { font-size: 1.5rem; }
.action-card strong { font-size: 1.05rem; }
.action-card small { color: var(--txt-soft); }

/* ─── Listas ─── */
#tests-list { display: flex; flex-direction: column; gap: 0.55rem; }
.test-row {
  background: var(--bg-panel); padding: 0.85rem 1rem;
  border-radius: var(--radius); box-shadow: var(--shadow);
  display: grid;
  grid-template-columns: 28px 1fr auto;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
}
.test-row .titulo { font-weight: 600; word-break: break-word; }
.test-row .tags { grid-column: 2 / -1; display: flex; flex-wrap: wrap; gap: 0.25rem; }
.test-row .meta { color: var(--txt-soft); font-size: 0.78rem; white-space: nowrap; }
.test-row .star { font-size: 1.2rem; }

.question-list {
  list-style: none; padding: 0;
  display: flex; flex-direction: column; gap: 0.55rem;
}
.q-row {
  background: var(--bg-panel); padding: 0.85rem 1rem;
  border-radius: var(--radius); box-shadow: var(--shadow);
}
.q-row .q-text { font-weight: 500; word-break: break-word; }
.q-row .q-meta { color: var(--txt-soft); font-size: 0.8rem; margin-top: 0.3rem; }
.q-row .tags { display: flex; flex-wrap: wrap; gap: 0.25rem; margin-top: 0.4rem; }
.q-row .q-actions {
  display: flex; gap: 0.3rem; margin-top: 0.5rem;
  justify-content: flex-end;
}
.tag {
  background: rgba(37, 99, 235, 0.1); color: var(--primary);
  padding: 0.15rem 0.55rem; border-radius: 999px; font-size: 0.75rem;
}

/* ─── Quiz ─── */
.quiz-header {
  background: var(--bg-panel); padding: 0.65rem 0.9rem;
  border-radius: var(--radius); box-shadow: var(--shadow);
  display: flex; align-items: center;
  flex-wrap: wrap; gap: 0.4rem 0.9rem;
}
.quiz-header #quiz-title { font-weight: 600; flex: 1; min-width: 0; word-break: break-word; }
.quiz-card {
  background: var(--bg-panel); padding: 1.3rem 1.1rem;
  border-radius: var(--radius); box-shadow: var(--shadow);
  margin-top: 0.7rem;
}
.quiz-card #quiz-question { font-size: 1.1rem; line-height: 1.45; margin: 0; word-break: break-word; }
.quiz-timer { font-variant-numeric: tabular-nums; font-weight: 700; color: var(--primary); }
.options { display: flex; flex-direction: column; gap: 0.5rem; margin-top: 1.2rem; }
.option-btn {
  background: var(--bg); border: 1px solid var(--border);
  padding: 0.85rem 1rem; border-radius: var(--radius);
  text-align: left; cursor: pointer;
  font-size: 0.95rem; font-family: inherit;
  min-height: 50px;
  word-break: break-word;
}
.option-btn:hover { background: #e0e7ff; }
.option-btn.correct { background: #d1fae5; border-color: var(--success); }
.option-btn.wrong { background: #fee2e2; border-color: var(--danger); }
.option-btn.disabled { pointer-events: none; opacity: 0.85; }
.explanation {
  background: #fef3c7; padding: 0.8rem 1rem;
  border-radius: var(--radius); margin-top: 1rem;
  word-break: break-word;
}
.quiz-actions { margin-top: 1.3rem; display: flex; gap: 0.45rem; flex-wrap: wrap; }
.quiz-actions > span { display: none; } /* spacer solo en desktop */

/* ─── Tiempo per-pregunta input ─── */
.tiempo-input-wrap {
  display: inline-flex; align-items: center; gap: 0.3rem;
  background: var(--bg-panel); padding: 0.35rem 0.7rem;
  border: 1px solid var(--border); border-radius: var(--radius);
  min-height: 44px;
}
.tiempo-input-wrap input {
  width: 64px; border: 0; background: transparent;
  font-size: 0.95rem; font-weight: 600; text-align: right;
  font-family: inherit; outline: none;
}

/* ─── Etiquetas ─── */
.inline-form {
  display: flex; flex-direction: column;
  gap: 0.5rem; margin-bottom: 1rem;
}
.inline-form input {
  padding: 0.6rem 0.8rem;
  border: 1px solid var(--border); border-radius: var(--radius);
  font-size: 0.95rem; font-family: inherit; width: 100%;
}
.etiquetas-list {
  list-style: none; padding: 0;
  display: flex; flex-direction: column; gap: 0.55rem;
}
.etiqueta-row {
  background: var(--bg-panel); padding: 0.85rem 1rem;
  border-radius: var(--radius); box-shadow: var(--shadow);
  display: grid;
  grid-template-columns: 16px 1fr auto;
  align-items: start;
  gap: 0.5rem;
}
.etiqueta-row > div { min-width: 0; }
.etiqueta-row .nombre { font-weight: 600; word-break: break-word; }
.etiqueta-row .descripcion { color: var(--txt-soft); font-size: 0.85rem; word-break: break-word; }
.etiqueta-row .count {
  background: var(--bg); padding: 0.2rem 0.55rem;
  border-radius: 999px; font-size: 0.78rem; white-space: nowrap;
}
.etiqueta-row button { padding: 0.3rem 0.55rem; min-height: 36px; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); margin-top: 6px; }
.dot.ok { background: var(--success); }

/* ─── Tag chips ─── */
.tag-chips {
  display: flex; flex-wrap: wrap; gap: 0.35rem;
  margin: 0 0 0.8rem;
}
.tag-chips .chip {
  background: var(--bg-panel); border: 1px solid var(--border);
  padding: 0.35rem 0.75rem; border-radius: 999px;
  font-size: 0.85rem; cursor: pointer; color: var(--txt);
  min-height: 34px; display: inline-flex; align-items: center;
}
.tag-chips .chip:active { transform: scale(0.95); }
.tag-chips .chip.active {
  background: var(--primary); color: #fff; border-color: var(--primary);
}

/* ─── Progreso ─── */
.por-test { list-style: none; padding: 0; }
.por-test li {
  background: var(--bg-panel); padding: 0.75rem 1rem;
  border-radius: var(--radius); box-shadow: var(--shadow);
  margin-bottom: 0.5rem; word-break: break-word;
}

/* ─── Pagination ─── */
.pagination {
  display: flex; gap: 0.25rem; justify-content: center;
  flex-wrap: wrap; margin-top: 1rem;
}
.pagination .page {
  background: var(--bg-panel); border: 1px solid var(--border);
  padding: 0.4rem 0.65rem; border-radius: 0.4rem;
  cursor: pointer; min-width: 38px; min-height: 38px;
}
.pagination .page.active {
  background: var(--primary); color: #fff; border-color: var(--primary);
}

/* ─── Toast ─── */
.toast {
  position: fixed; bottom: 1rem;
  left: 1rem; right: 1rem;
  background: #1f2937; color: #fff;
  padding: 0.7rem 1rem; border-radius: var(--radius);
  text-align: center; z-index: 100; line-height: 1.3;
}

/* ─── Forms ─── */
textarea {
  width: 100%; min-height: 180px;
  padding: 0.7rem; border: 1px solid var(--border);
  border-radius: var(--radius); font-family: monospace;
  font-size: 0.85rem;
}
.muted { color: var(--txt-soft); font-size: 0.9rem; }
.actions { display: flex; gap: 0.45rem; align-items: center; flex-wrap: wrap; margin-top: 0.5rem; }

/* ─── Modal ─── */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex; z-index: 200;
  padding: 0.5rem;
}
.modal-card {
  background: var(--bg-panel);
  width: 100%; max-width: 560px;
  margin: auto;
  border-radius: var(--radius);
  max-height: 95vh; overflow-y: auto;
  box-shadow: 0 20px 50px rgba(0,0,0,0.3);
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0.9rem 1rem; border-bottom: 1px solid var(--border);
}
.modal-header h3 { margin: 0; }
.modal form { padding: 0.9rem 1rem; display: flex; flex-direction: column; gap: 0.7rem; }
.modal label { display: block; font-weight: 600; }
.modal input, .modal textarea {
  display: block; width: 100%;
  margin-top: 0.3rem;
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--border); border-radius: var(--radius);
  font-family: inherit; font-size: 0.95rem;
}
.modal textarea { font-family: monospace; font-size: 0.85rem; }
.modal-actions {
  display: flex; gap: 0.5rem; margin-top: 0.4rem; flex-wrap: wrap;
}

/* Gestión solo a quien tenga permisos */
body:not(.puede-gestionar) .gestion { display: none !important; }


/* ════════════════ TABLET (>= 600 px) ════════════════ */
@media (min-width: 600px) {
  .user-chip .user-name { display: inline; max-width: 140px; }
  .stats-grid { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
  .actions-grid { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }
  .view-header {
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
  }
  .view-header h2 { font-size: 1.4rem; flex: 1; min-width: 200px; }
  .inline-form {
    flex-direction: row; flex-wrap: wrap; align-items: stretch;
  }
  .inline-form input { width: auto; flex: 1; min-width: 200px; }
}


/* ════════════════ DESKTOP (>= 900 px) ════════════════ */
@media (min-width: 900px) {
  body {
    display: grid;
    grid-template:
      "header header" var(--topbar-h)
      "side main" 1fr
      / var(--sidebar-w) 1fr;
  }
  .topbar {
    grid-area: header; position: sticky;
    padding: 0 1.5rem;
    gap: 1.5rem;
  }
  .brand { font-size: 1.15rem; }
  /* La barra de búsqueda queda centrada en el espacio sobrante;
     el chip de usuario se empuja al borde derecho con margin auto. */
  .search-wrap {
    flex: 0 1 520px;
    margin: 0 auto;
  }
  .user-chip {
    margin-left: auto;
    padding-left: 0.4rem;
  }
  .user-chip .user-name { max-width: 180px; }
  .hamburger { display: none; }
  .sidebar {
    grid-area: side;
    position: static;
    transform: none;
    height: auto;
  }
  .sidebar-backdrop { display: none !important; }
  .content { grid-area: main; padding: 1.5rem 2.5rem; max-width: none; overflow-x: hidden; }
  .quiz-actions > span { display: block; flex: 1; }
  .toast {
    left: 50%; right: auto;
    transform: translateX(-50%); bottom: 2rem;
    text-align: left; min-width: 240px;
  }
  .auth-card { margin-top: 3rem; padding: 2rem; }
}
