/* =============================================================================
   mobile.css — capa responsive + tema oscuro para USMLE Environment
   Se carga DESPUES de base.css y solo sobreescribe lo necesario.
   ========================================================================== */

:root {
  --bg:            #eef0f8;
  --surface:       #ffffff;
  --surface-2:     #f6f7fc;
  --border:        #d8dcec;
  --text:          #23262f;
  --text-dim:      #626a80;
  --brand:         #3852a4;
  --brand-dark:    #2e4285;
  --brand-soft:    #eaefff;
  --ok:            #12a150;
  --bad:           #e5484d;
  --warn:          #d9a400;
  --bar-h:         52px;
  --rail-w:        68px;
  --radius:        12px;
  --shadow:        0 6px 24px rgba(23, 32, 68, .12);
  --font-scale:    100%;
  color-scheme: light;
}

html[data-theme="dark"] {
  --bg:            #12141c;
  --surface:       #1b1e29;
  --surface-2:     #232736;
  --border:        #333a4f;
  --text:          #e7e9f2;
  --text-dim:      #99a0b8;
  --brand:         #7d97e8;
  --brand-dark:    #5b74c9;
  --brand-soft:    #232c4a;
  --shadow:        0 6px 24px rgba(0, 0, 0, .5);
  color-scheme: dark;
}

/* ------------------------------------------------------------- base fixes */
* { -webkit-tap-highlight-color: rgba(56, 82, 164, .15); }

html {
  font-size: var(--font-scale);
  -webkit-text-size-adjust: 100%;
  scrollbar-color: var(--brand) var(--bg);
}

html, body {
  background-color: var(--bg);
  color: var(--text);
  overflow-x: hidden;
}

body {
  margin: 0;
  padding-top: var(--bar-h);
  min-height: 100vh;
  min-height: 100dvh;
  font-size: 16px;
  line-height: 1.55;
}

img, video, iframe { max-width: 100%; height: auto; }

::-webkit-scrollbar-track { background: var(--bg); }

/* base.css deja p/table en 1.4em, que en movil es enorme y ademas compone */
p                          { font-size: 1rem; margin: .7em 0; }
table                      { font-size: 1rem; background: transparent; }
th, td                     { padding: 10px 12px; }
h1, h2, h3, h4, h5, h6     { color: var(--text); text-transform: none; }
.page-title                { color: var(--brand); }
.page-description          { font-size: 1rem; color: var(--text-dim); }
header                     { text-align: left; margin-bottom: 8px; }
.container                 { background: var(--surface); }

/* .page-body venia con width:80% + padding-right:100px */
.page-body {
  width: auto;
  margin: 0;
  padding-right: var(--page-right-pad, 0px);
}

/* ================================================================ APP BAR */
.app-bar {
  position: fixed;
  inset: 0 0 auto 0;
  height: var(--bar-h);
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 8px;
  padding-top: env(safe-area-inset-top);
  height: calc(var(--bar-h) + env(safe-area-inset-top));
  background: var(--brand-dark);
  color: #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, .18);
  z-index: 9500;
}

.app-bar__icon {
  flex: 0 0 auto;
  width: 40px; height: 40px;
  display: grid; place-items: center;
  background: transparent;
  border: none;
  border-radius: 10px;
  color: #fff;
  cursor: pointer;
}
.app-bar__icon:active { background: rgba(255, 255, 255, .18); }

.app-bar__title {
  flex: 1 1 auto;
  min-width: 0;
  font-weight: 600;
  font-size: .98rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.app-bar__search {
  display: flex;
  gap: 6px;
  align-items: center;
  flex: 2 1 320px;
  min-width: 0;
}
.app-bar__search input {
  flex: 1 1 auto;
  min-width: 0;
  height: 36px;
  padding: 0 12px;
  border: 1px solid rgba(255, 255, 255, .25);
  border-radius: 18px;
  background: rgba(255, 255, 255, .14);
  color: #fff;
  font-size: 16px; /* evita el zoom automatico de iOS */
}
.app-bar__search input::placeholder { color: rgba(255, 255, 255, .7); }
.app-bar__search button {
  height: 36px;
  padding: 0 14px;
  border: none;
  border-radius: 18px;
  background: #fff;
  color: var(--brand-dark);
  font-weight: 600;
  cursor: pointer;
}

.app-bar__sync {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 32px;
  padding: 0 10px;
  border: 1px solid rgba(255, 255, 255, .3);
  border-radius: 16px;
  background: rgba(255, 255, 255, .1);
  color: #fff;
  font-size: .8rem;
  cursor: pointer;
  white-space: nowrap;
}
.sync-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: #9aa3bd;
  flex: 0 0 auto;
}
.sync-dot[data-state="ok"]      { background: #46d17f; }
.sync-dot[data-state="syncing"] { background: #ffd452; animation: pulse 1s infinite; }
.sync-dot[data-state="error"]   { background: #ff6b6b; }
.sync-dot[data-state="off"]     { background: #9aa3bd; }
@keyframes pulse { 50% { opacity: .3; } }

/* En movil el buscador se despliega sobre la barra */
@media (max-width: 719px) {
  .app-bar__search {
    position: absolute;
    inset: env(safe-area-inset-top) 0 auto 0;
    height: var(--bar-h);
    padding: 0 8px;
    background: var(--brand-dark);
    display: none;
    z-index: 2;
  }
  .app-bar.is-searching .app-bar__search { display: flex; }
  .sync-label { display: none; }
  .app-bar__sync { padding: 0 8px; }
}
@media (min-width: 720px) {
  #searchToggleBtn { display: none; }
}

/* ============================================================ OVERFLOW ⋯ */
.sheet {
  position: fixed;
  top: calc(var(--bar-h) + env(safe-area-inset-top) + 6px);
  right: 8px;
  min-width: 230px;
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  z-index: 9600;
}
.sheet[hidden] { display: none; }
.sheet__item {
  display: block;
  width: 100%;
  padding: 12px 14px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font-size: .95rem;
  text-align: left;
  cursor: pointer;
}
.sheet__item:hover   { background: var(--surface-2); }
.sheet__item--muted  { color: var(--text-dim); }
.sheet__item--danger { color: var(--bad); }

/* ============================================================ NAV DRAWER */
.nav-drawer {
  position: fixed;
  top: calc(var(--bar-h) + env(safe-area-inset-top));
  bottom: 0;
  left: 0;
  width: min(86vw, 320px);
  padding-bottom: 24px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  overscroll-behavior: contain;
  transform: translateX(-102%);
  z-index: 9400;
  -webkit-overflow-scrolling: touch;
}
.nav-drawer.is-open { transform: translateX(0); }
/* La animacion solo cuando el cajon es deslizante; en escritorio esta fijo y
   una transicion pendiente lo dejaria fuera de pantalla. */
@media (max-width: 1099px) { .nav-drawer { transition: transform .22s ease; } }

.nav-drawer__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px 8px;
  font-weight: 700;
  color: var(--text-dim);
  font-size: .8rem;
  letter-spacing: .08em;
  text-transform: uppercase;
}
.nav-drawer__close {
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
}

.nav-item {
  display: block;
  width: 100%;
  padding: 11px 16px;
  border: none;
  border-left: 3px solid transparent;
  background: transparent;
  color: var(--text);
  text-align: left;
  cursor: pointer;
  font-size: .95rem;
}
.nav-item:hover        { background: var(--surface-2); }
.nav-item.is-active    { border-left-color: var(--brand); background: var(--brand-soft); }
.nav-item--home        { font-weight: 600; }
.nav-item__title       { display: block; }
.nav-item__meta {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 5px;
  font-size: .76rem;
  color: var(--text-dim);
}
.mini-bar {
  flex: 1 1 auto;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  overflow: hidden;
}
.mini-bar > i {
  display: block;
  height: 100%;
  background: var(--brand);
}

.scrim {
  position: fixed;
  inset: 0;
  background: rgba(10, 14, 30, .45);
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s;
  z-index: 9300;
}
.scrim.is-open { opacity: 1; pointer-events: auto; }

/* ================================================================== MAIN */
.app-main {
  padding: 14px 14px 120px;
  max-width: 1100px;
  margin: 0 auto;
}

@media (min-width: 1100px) {
  .nav-drawer {
    transform: none;
    box-shadow: none;
  }
  .scrim { display: none; }
  .app-main { margin-left: 320px; max-width: none; padding: 22px 32px 120px; }
  #navToggleBtn, .nav-drawer__close { display: none; }
  /* durante un test manda la lista de preguntas, no el indice de temas.
     El resto de los ajustes del panel van mas abajo, junto a su seccion,
     porque si no las reglas base los pisan por orden. */
  body.is-testing .nav-drawer { display: none; }
  body.is-testing .app-main { margin-left: 230px; }
}

/* ------------------------------------------------------------- dashboard */
.home__hero h1 { margin: 4px 0 2px; font-size: 1.4rem; }
.home__hero p  { color: var(--text-dim); margin: 0 0 16px; }

.stat-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 10px;
  margin-bottom: 18px;
}
.stat {
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.stat__num   { font-size: 1.5rem; font-weight: 700; line-height: 1.2; }
.stat__label { font-size: .78rem; color: var(--text-dim); }
.stat--ok  .stat__num { color: var(--ok); }
.stat--bad .stat__num { color: var(--bad); }

.home__actions { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.home__section { font-size: .82rem; letter-spacing: .08em; text-transform: uppercase; color: var(--text-dim); margin: 22px 0 10px; }

.topic-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 10px;
}
.topic-card {
  display: block;
  width: 100%;
  padding: 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-align: left;
  cursor: pointer;
  color: var(--text);
  transition: border-color .15s, transform .1s;
}
.topic-card:active   { transform: scale(.99); }
.topic-card:hover    { border-color: var(--brand); }
.topic-card__title   { font-weight: 600; font-size: .98rem; }
.topic-card__meta    { margin-top: 8px; font-size: .78rem; color: var(--text-dim); display: flex; align-items: center; gap: 8px; }

/* ------------------------------------------------------------ topic view */
.topic-head { margin-bottom: 10px; }
.topic-head h1 { margin: 0 0 8px; font-size: 1.25rem; color: var(--brand); }
.topic-progress { height: 6px; border-radius: 3px; background: var(--border); overflow: hidden; }
.topic-progress__bar { height: 100%; width: 0; background: var(--brand); transition: width .3s; }
.topic-meta { margin: 6px 0 0; font-size: .8rem; color: var(--text-dim); }

.topic-loading {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 40px 0;
  justify-content: center;
  color: var(--text-dim);
}

/* El HTML de Notion: contenedores de tema inyectados */
#topicHost article.page      { margin: 0; }
#topicHost header            { display: none; }        /* el titulo lo pone .topic-head */
#topicHost ul.toggle         { list-style: none; padding-left: 0; margin: 0; }
#topicHost ul.toggle ul.toggle { padding-left: 10px; }
#topicHost li                { margin-bottom: 0; }

#topicHost details {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  margin: 8px 0;
  overflow: hidden;
}
#topicHost details > summary {
  padding: 13px 14px;
  min-height: 24px;
  list-style: none;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  color: var(--text);
  background: var(--surface-2);
  user-select: none;
}
#topicHost details[open] > summary { border-bottom: 1px solid var(--border); }
#topicHost details > summary::-webkit-details-marker { display: none; }
#topicHost details > summary::before {
  content: "";
  flex: 0 0 auto;
  width: 8px; height: 8px;
  border-right: 2px solid var(--brand);
  border-bottom: 2px solid var(--brand);
  transform: rotate(-45deg);
  transition: transform .18s;
  margin-right: 2px;
}
#topicHost details[open] > summary::before { transform: rotate(45deg); }
#topicHost details > summary > strong { font-size: 1rem; color: var(--brand); }

#topicHost details > *:not(summary) { padding-left: 14px; padding-right: 14px; }
#topicHost details > ul.toggle      { padding: 0 8px 8px; }

#topicHost p     { font-size: 1rem; line-height: 1.6; }
#topicHost figure { margin: 14px 0; }

/* tablas de opciones: A. / texto / vacio */
.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 12px 0; }
#topicHost table {
  display: table;
  width: 100%;
  min-width: 0;
  margin: 0;
}
#topicHost td, #topicHost th { word-break: break-word; }
#topicHost table.simple-table {
  border-collapse: separate;
  border-spacing: 0 6px;
}
#topicHost table.simple-table td {
  padding: 13px 10px;
  font-size: .98rem;
  background: var(--surface);
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  color: var(--text);
}
#topicHost table.simple-table tr:hover td { background: var(--surface-2); }
#topicHost table.simple-table td:first-child {
  border-left: 1px solid var(--border);
  border-radius: 8px 0 0 8px;
  width: 44px;
  min-width: 44px;
}
#topicHost table.simple-table td:last-child { border-right: 1px solid var(--border); border-radius: 0 8px 8px 0; }
/* la tercera columna del export de Notion viene vacia */
#topicHost table.simple-table td.is-empty { display: none; }

td.choice-cell {
  font-weight: 700;
  color: var(--brand) !important;
  text-align: center;
  cursor: pointer;
}
tr:has(td.choice-cell) { cursor: pointer; }
tr.is-chosen td { background: var(--brand-soft) !important; }

/* ------------------------------------------------ bloque de respuesta UI */
.answer-ui {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin: 10px 0 4px;
  padding: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  position: sticky;
  bottom: 0;
  z-index: 5;
}
.answer-ui label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 40px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 20px;
  background: var(--surface);
  color: var(--text);
  font-size: .88rem;
  font-weight: 500;
  cursor: pointer;
  user-select: none;
}
.answer-ui select {
  min-height: 38px;
  padding: 0 8px;
  font-size: 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
}
.answer-ui input[type="checkbox"] {
  width: 20px;
  height: 20px;
  margin: 0;
  accent-color: var(--brand);
}
.answer-ui label:has(.markCheckbox:checked)      { background: #fff4d6; border-color: var(--warn); color: #6b4e00; }
.answer-ui label:has(.correctCheckbox:checked)   { background: #dcf7e6; border-color: var(--ok);   color: #06603a; }
.answer-ui label:has(.incorrectCheckbox:checked) { background: #ffe1e1; border-color: var(--bad);  color: #8b1a1d; }
html[data-theme="dark"] .answer-ui label:has(.markCheckbox:checked)      { background: #3d3213; color: #ffdf8a; }
html[data-theme="dark"] .answer-ui label:has(.correctCheckbox:checked)   { background: #12371f; color: #8ef0b6; }
html[data-theme="dark"] .answer-ui label:has(.incorrectCheckbox:checked) { background: #3b1618; color: #ffb3b5; }

.iconContainer { display: inline-flex; align-items: center; gap: 5px; }
.iconContainer svg { width: 15px; height: 15px; }

/* ========================================================== TOOLS RAIL */
.tools-rail {
  position: fixed;
  right: 10px;
  bottom: calc(12px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column-reverse;
  gap: 8px;
  z-index: 9200;
}
.tool, .tool-wrap > .tool {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  width: 52px;
  min-height: 52px;
  padding: 6px 2px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
  color: var(--brand);
  cursor: pointer;
}
.tool:active   { transform: scale(.96); }
.tool.is-on    { background: var(--brand); color: #fff; border-color: var(--brand); }
.tool__label   { font-size: .58rem; font-weight: 600; letter-spacing: .02em; }

.tool--timer { width: 92px; padding: 8px 6px; position: relative; }
#stopwatchDisplay { font-size: 1.05rem; font-weight: 700; color: var(--brand); font-variant-numeric: tabular-nums; }
.tool--timer.is-running #stopwatchDisplay { color: var(--ok); }
.timer-btns { display: flex; gap: 4px; margin-top: 4px; }
.timer-btns button {
  padding: 4px 8px;
  font-size: .68rem;
  border: none;
  border-radius: 6px;
  background: var(--brand);
  color: #fff;
  cursor: pointer;
}
#occludeBtn {
  position: absolute;
  top: 2px; right: 4px;
  width: 20px; height: 20px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
}
#stopwatchIcon { display: none; }

/* En movil el rail se vuelve una barra inferior horizontal */
@media (max-width: 719px) {
  .tools-rail {
    left: 0; right: 0; bottom: 0;
    flex-direction: row;
    justify-content: flex-start;
    gap: 6px;
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    background: var(--surface);
    border-top: 1px solid var(--border);
    overflow-x: auto;
    scrollbar-width: none;
  }
  .tools-rail::-webkit-scrollbar { display: none; }
  .tool, .tool-wrap > .tool { box-shadow: none; border-radius: 10px; flex: 0 0 auto; }
  .tools-rail.is-hidden { transform: translateY(110%); }
  .tools-rail { transition: transform .25s; }
  .app-main { padding-bottom: 96px; }
}

/* ======================================================= TEST MODE UI */
:root { --test-sidebar-w: 210px; }

/* El asa: SIEMPRE por encima del panel, y se desplaza con el para que nunca
   quede debajo. Antes tenia menos z-index que el panel y al abrirse quedaba
   sepultada: no habia forma de volver a cerrarlo. */
#sidebarToggleBtn {
  position: fixed;
  top: calc(var(--bar-h) + env(safe-area-inset-top) + 8px);
  left: 0;
  z-index: 9380;
  display: none;
  padding: 11px 12px;
  border: none;
  border-radius: 0 10px 10px 0;
  background: var(--brand-dark);
  color: #fff;
  cursor: pointer;
  box-shadow: 2px 2px 8px rgba(0, 0, 0, .25);
  transition: transform .25s ease;
}
body.is-testing #sidebarToggleBtn { display: block; }
body.test-list-open #sidebarToggleBtn { transform: translateX(var(--test-sidebar-w)); }

#testModeSidebar {
  position: fixed;
  top: calc(var(--bar-h) + env(safe-area-inset-top));
  left: 0;
  width: var(--test-sidebar-w);
  height: calc(100dvh - var(--bar-h) - env(safe-area-inset-top));
  background: var(--surface);
  border-right: 2px solid var(--brand);
  transform: translateX(-102%);
  transition: transform .25s ease;
  z-index: 9360;
  display: none;
  flex-direction: column;
  overflow: hidden;
}
body.is-testing #testModeSidebar { display: flex; }
body.test-list-open #testModeSidebar { transform: translateX(0); }

#testModeSidebarHeader {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 8px 10px 14px;
  background: var(--brand-dark);
  color: #fff;
  font-size: .9rem;
}
#testModeSidebarHeader strong { color: #fff; }
#testSidebarCloseBtn {
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
  border: none;
  border-radius: 8px;
  background: rgba(255, 255, 255, .15);
  color: #fff;
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
}
#testSidebarCloseBtn:active { background: rgba(255, 255, 255, .3); }

#testModeListScroll {
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}
#testModeSidebarFoot {
  flex: 0 0 auto;
  padding: 8px;
  border-top: 1px solid var(--border);
  padding-bottom: calc(8px + env(safe-area-inset-bottom));
}

#testModeQuestionsList tr { cursor: pointer; border-bottom: 1px solid var(--border); }
#testModeQuestionsList td { padding: 13px 12px; font-size: .9rem; color: var(--text); }
#testModeQuestionsList tr.is-current td { background: var(--brand-soft); font-weight: 700; }
#finishTestBtn {
  width: 100%;
  min-height: 44px;
  padding: 10px;
  border: none;
  border-radius: 8px;
  background: #ff4d4d;
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}

#testNavButtons {
  display: none;
  position: fixed;
  left: 50%;
  bottom: calc(14px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  align-items: center;
  gap: 14px;
  padding: 6px 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  box-shadow: var(--shadow);
  z-index: 9250;
}
body.is-testing #testNavButtons { display: flex; }
#testNavButtons button {
  display: grid;
  place-items: center;
  width: 46px; height: 46px;
  border: none;
  border-radius: 50%;
  background: var(--brand-soft);
  color: var(--brand);
  cursor: pointer;
}
#testNavButtons button:disabled { opacity: .35; cursor: default; }
#testProgressLabel { font-size: .85rem; font-weight: 600; color: var(--text-dim); min-width: 62px; text-align: center; }

@media (max-width: 719px) {
  /* la barra de herramientas ocupa los ~82 px de abajo: el navegador del
     test se apila encima, no sobre ella */
  #testNavButtons { bottom: calc(96px + env(safe-area-inset-bottom)); }
  body.is-testing .app-main { padding-bottom: 168px; }
  :root { --test-sidebar-w: min(78vw, 260px); }
}

/* En celular y tablet el panel flota sobre el texto, asi que se oscurece el
   fondo y se puede cerrar tocando fuera. */
@media (max-width: 1099px) {
  body.test-list-open .scrim { opacity: 1; pointer-events: auto; }
}

/* En pantalla ancha el panel es fijo y no tapa el enunciado: sobran el asa,
   el velo y el boton de cerrar. Va aqui, despues de las reglas base, para
   que gane por orden. */
@media (min-width: 1100px) {
  body.is-testing #sidebarToggleBtn { display: none; }
  body.is-testing #testModeSidebar { transform: none; }
  body.is-testing .scrim { display: none; }
  #testSidebarCloseBtn { display: none; }
}

/* ================================================================ MODALS */
.modal-backdrop {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(8, 12, 28, .55);
  z-index: 9700;
}
.modal-container, #downloadBackupModalContainer, #sortQuestionsModal {
  width: 100%;
  max-width: 620px;
  max-height: 86vh;
  max-height: 86dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  animation: fadeInUp .22s ease-out;
  padding: 0;
}
.modal-container--narrow { max-width: 420px; }

.modal-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.modal-header h2 { margin: 0; font-size: 1.05rem; color: var(--text); }
.modal-subhead { padding: 10px 16px 0; font-size: .9rem; }
.modal-body {
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px;
  margin: 0;
}
.modal-footer {
  flex: 0 0 auto;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
}
.modal-close-btn {
  padding: 9px 16px;
  min-height: 40px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
  font-size: .9rem;
  cursor: pointer;
}
.modal-header .modal-close-btn {
  width: 38px; height: 38px;
  min-height: 0;
  padding: 0;
  font-size: 1.3rem;
  line-height: 1;
  border-radius: 50%;
}

@media (max-width: 719px) {
  .modal-backdrop { padding: 0; align-items: stretch; }
  .modal-container, #downloadBackupModalContainer, #sortQuestionsModal {
    max-width: none;
    max-height: none;
    height: 100dvh;
    border-radius: 0;
    border: none;
  }
  .modal-header { padding-top: calc(14px + env(safe-area-inset-top)); }
}

/* ----------------------------------------------------------- form bits */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 42px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  font-size: .92rem;
  font-weight: 500;
  cursor: pointer;
}
.btn:hover          { border-color: var(--brand); }
.btn--primary       { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn--primary:hover { background: var(--brand-dark); }
.btn[disabled]      { opacity: .5; cursor: default; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; }
.field > label { font-weight: 600; font-size: .88rem; color: var(--text-dim); }
.field--inline { flex-direction: row; align-items: center; gap: 8px; margin: 0; }
.field__row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.check { display: inline-flex; align-items: center; gap: 8px; font-size: .92rem; cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--brand); }

input[type="text"], input[type="number"], input[type="date"], select, textarea {
  min-height: 42px;
  padding: 8px 12px;
  font-size: 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
}
input[type="range"] { width: 100%; accent-color: var(--brand); height: 36px; }

.hint { font-size: .84rem; color: var(--text-dim); line-height: 1.5; }
.hint code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }

.search-result {
  padding: 12px 14px;
  margin-bottom: 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  cursor: pointer;
}
.search-result:hover  { border-color: var(--brand); }
.search-result__id    { font-weight: 700; color: var(--brand); }
.search-result__topic { font-size: .76rem; color: var(--text-dim); }
.search-result__text  { font-size: .88rem; margin-top: 4px; }

.performance-stat {
  padding: 13px 15px;
  margin-bottom: 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  color: var(--text);
  font-weight: 500;
  cursor: pointer;
}
.performance-stat:hover { border-color: var(--brand); }

.year-nav { display: flex; align-items: center; justify-content: center; gap: 14px; margin-bottom: 14px; }
#heatmapYearLabel { font-weight: 700; }
.heatmap-wrap {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  padding: 10px;
  overflow-x: auto;
}
#heatmapMetrics { margin-top: 12px; font-size: .9rem; }
/* El grafico de barras crece con la cantidad de sistemas (26 barras = 830 px),
   asi que el contenedor se desplaza en vez de aplastarlas. */
.chart-wrap {
  position: relative;
  height: auto;
  min-height: 300px;
  max-height: 65vh;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  padding: 8px;
}
.chart-wrap canvas { width: 100% !important; }

#sortedQuestionsOutput {
  display: none;
  margin-top: 12px;
  padding: 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: .9rem;
  word-break: break-word;
}
#performanceDetailList { word-break: break-word; font-size: .9rem; }

.sync-status {
  padding: 12px 14px;
  margin-bottom: 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  font-size: .88rem;
  line-height: 1.6;
}
.sync-status b { color: var(--text); }

/* ============================================================== OVERLAYS */
#keyEntryOverlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 24px;
  background: linear-gradient(140deg, #1a1f30 0%, #333a52 100%);
  color: #fff;
  text-align: center;
}
#keyEntryOverlay h1 { color: #fff; margin: 0; font-size: 1.5rem; }
#keyInput {
  width: min(280px, 80vw);
  text-align: center;
  color: #23262f;
  background: #fff;
}
#keySubmitBtn {
  min-height: 46px;
  padding: 0 28px;
  border: none;
  border-radius: 10px;
  background: var(--brand);
  color: #fff;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}
#rememberKeyLabel { display: inline-flex; align-items: center; gap: 8px; font-size: .85rem; opacity: .85; }

#loadingOverlay {
  position: fixed;
  inset: 0;
  z-index: 9998;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: rgba(6, 9, 20, .82);
  color: #fff;
  font-size: 1.2rem;
}
.load-spinner {
  width: 44px; height: 44px;
  border: 5px solid var(--brand);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}
.load-spinner--sm { width: 24px; height: 24px; border-width: 3px; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(90px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  max-width: min(90vw, 420px);
  padding: 12px 18px;
  background: #23262f;
  color: #fff;
  border-radius: 10px;
  box-shadow: var(--shadow);
  font-size: .9rem;
  z-index: 9800;
  text-align: center;
}
.toast[hidden] { display: none; }

/* --------------------------------------------------------- dark tweaks */
html[data-theme="dark"] .highlighted { background-color: #6a5b00; color: #fff; }
html[data-theme="dark"] #topicHost img,
html[data-theme="dark"] .modal-body img { filter: brightness(.92); }
html[data-theme="dark"] .app-bar { background: #0f1220; }

@media print { .app-bar, .tools-rail, .nav-drawer, #testNavButtons, #sidebarToggleBtn { display: none !important; } }
@media (prefers-reduced-motion: reduce) { * { animation-duration: .01ms !important; transition-duration: .01ms !important; } }
