:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --ink: #1f2733;
  --muted: #6b7684;
  --line: #e2e7ee;
  --accent: #3b5ba5;
  --accent-dark: #2c4680;
  --ok: #1f9d55;
  --warn: #d97706;
  --danger: #d64545;
  --danger-bg: #fdecec;
  --a-badge: #2c4680;
  --shadow: 0 1px 3px rgba(20, 30, 50, .08), 0 8px 24px rgba(20, 30, 50, .06);
  --radius: 10px;
  font-size: 15px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
}
.hidden { display: none !important; }
/* El atributo hidden debe ganar aunque el elemento tenga display propio (ej. .brand-mark). */
[hidden] { display: none !important; }

/* ---------- Brand ---------- */
.brand { display: flex; align-items: center; gap: .5rem; }
.brand-mark { display: inline-flex; align-items: center; gap: .5rem; }
.brand-lg .brand-mark { flex-direction: column; gap: .2rem; }
.brand-img { max-height: 36px; width: auto; }
.brand-lg .brand-img { max-height: 68px; }
.brand .star { width: 30px; height: 30px; }
.brand .star path { fill: none; stroke: var(--muted); stroke-width: 4; stroke-linejoin: round; }
.brand .wordmark {
  font-size: 1.5rem; font-weight: 600; letter-spacing: .06em; color: #545b66;
}
.brand-lg { flex-direction: column; gap: .2rem; }
.brand-lg .star { width: 56px; height: 56px; }
.brand-lg .star path { stroke: var(--accent); }
.brand-lg .wordmark { font-size: 2.2rem; color: var(--accent); }

/* ---------- Login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; background: linear-gradient(160deg,#eef1f6,#dfe5ef); }
.login-card {
  background: var(--panel); padding: 2.4rem 2.2rem; border-radius: 16px;
  box-shadow: var(--shadow); width: 340px; display: flex; flex-direction: column; gap: .9rem;
}
.login-sub { text-align: center; color: var(--muted); margin: -.2rem 0 1rem; }
.login-card label { display: flex; flex-direction: column; gap: .3rem; font-size: .82rem; color: var(--muted); }
.login-card input { padding: .6rem .7rem; border: 1px solid var(--line); border-radius: 8px; font-size: 1rem; }

/* ---------- Topbar ---------- */
.topbar {
  display: flex; align-items: center; gap: 1.5rem; padding: .5rem 1.2rem;
  background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 20;
}
.tabs { display: flex; gap: .2rem; flex: 1; }
.tab {
  border: none; background: none; padding: .55rem .9rem; border-radius: 8px; cursor: pointer;
  font-size: .95rem; color: var(--muted); font-weight: 500;
}
.tab:hover { background: var(--bg); color: var(--ink); }
.tab.active { background: var(--accent); color: #fff; }
.badge { background: var(--danger); color: #fff; border-radius: 999px; padding: 0 .4rem; font-size: .72rem; margin-left: .2rem; }
.user-box { display: flex; align-items: center; gap: .7rem; color: var(--muted); font-size: .9rem; }

main { padding: 1.3rem 1.4rem; max-width: 1500px; margin: 0 auto; }
.view h2 { margin: .2rem 0 1rem; font-size: 1.3rem; }
.view h3 { margin: 1.4rem 0 .6rem; font-size: 1.05rem; }
.hint { color: var(--muted); margin: -.4rem 0 1rem; max-width: 70ch; }

/* ---------- Buttons ---------- */
.btn { border: 1px solid transparent; border-radius: 8px; padding: .5rem .9rem; cursor: pointer; font-size: .9rem; font-weight: 500; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }
.btn-ghost { background: transparent; border-color: var(--line); color: var(--ink); }
.btn-ghost:hover { background: var(--bg); }
.btn-sm { padding: .3rem .55rem; font-size: .8rem; }

/* ---------- KPIs ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: .9rem; margin-bottom: 1.1rem; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: .9rem 1.1rem; box-shadow: var(--shadow); }
.kpi .n { font-size: 1.9rem; font-weight: 700; }
.kpi .l { color: var(--muted); font-size: .82rem; }
.kpi.risk .n { color: var(--danger); }
.kpi.pend .n { color: var(--warn); }

/* ---------- Toolbar ---------- */
.toolbar { display: flex; flex-wrap: wrap; gap: .5rem; margin-bottom: .9rem; align-items: center; }
.toolbar input, .toolbar select { padding: .45rem .6rem; border: 1px solid var(--line); border-radius: 8px; font-size: .88rem; background: var(--panel); }
.toolbar #f-q { flex: 1; min-width: 200px; }
.toolbar #btn-export { margin-left: auto; }

/* ---------- Tables ---------- */
.table-wrap { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow-x: auto; box-shadow: var(--shadow); }
table.grid { width: 100%; border-collapse: collapse; font-size: .86rem; }
table.grid th, table.grid td { padding: .5rem .6rem; border-bottom: 1px solid var(--line); text-align: center; white-space: nowrap; }
table.grid th { background: #f7f9fc; color: var(--muted); font-weight: 600; position: sticky; top: 0; }
table.grid th.l, table.grid td.l { text-align: left; }
table.grid th.r, table.grid td.r { text-align: right; }
table.grid tbody tr { cursor: pointer; }
table.grid tbody tr:hover { background: #f2f6fd; }
td .code { font-weight: 600; color: var(--accent-dark); }
td .name { color: var(--muted); max-width: 320px; overflow: hidden; text-overflow: ellipsis; }

.pill { display: inline-block; padding: .05rem .45rem; border-radius: 999px; font-size: .72rem; font-weight: 600; }
.pill.A { background: #e7edfb; color: var(--a-badge); }
.pill.B { background: #eef3ea; color: #4a6b3a; }
.pill.C { background: #f2f2f4; color: #6b7280; }
.pill.risk { background: var(--danger-bg); color: var(--danger); }
.pill.disc { background: #f3e8e8; color: #9b5151; }
.pill.norot { background: #fdf0dd; color: #a15c11; }

/* ---- Tabla de revisión: identidad angosta+CONGELADA a la izq. y encabezado ESTÁTICO ---- */
/* Contenedor con altura acotada -> el encabezado (sticky top) y las columnas de
   identidad (sticky left) se quedan siempre visibles al desplazar. */
#view-revision .table-wrap { max-height: calc(100vh - 300px); min-height: 320px; overflow: auto; }

#review-table td, #review-table th { box-sizing: border-box; }
#review-table th:nth-child(1), #review-table td:nth-child(1) { width: 122px; min-width: 122px; max-width: 122px; }
#review-table th:nth-child(2), #review-table td:nth-child(2) { width: 150px; min-width: 150px; max-width: 150px; }
#review-table th:nth-child(3), #review-table td:nth-child(3) { width: 40px;  min-width: 40px;  max-width: 40px; }
/* Congelar (sticky) las 3 primeras columnas: siempre visibles al desplazar horizontal */
#review-table td:nth-child(-n+3) { position: sticky; background: var(--panel); z-index: 2; white-space: nowrap; overflow: hidden; }
#review-table th:nth-child(-n+3) { white-space: nowrap; overflow: hidden; }
#review-table thead th { z-index: 3; }
#review-table thead th:nth-child(-n+3) { z-index: 5; }
#review-table th:nth-child(3), #review-table td:nth-child(3) { border-right: 2px solid var(--line); }
#review-table tbody tr:hover td:nth-child(-n+3) { background: #f2f6fd; }
#review-table td:nth-child(2) .name { display: block; max-width: 138px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#review-table td:nth-child(1) .code { display: inline-block; max-width: 88px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; vertical-align: bottom; }

.month-col { min-width: 46px; font-variant-numeric: tabular-nums; }
th.month-col { font-size: .76rem; color: #58627a; background: #eef2f8; }
td.month-col { color: #3a4457; }
td.month-col.zero { color: #c4cad4; }

.qty-input { width: 68px; padding: .3rem; border: 1px solid var(--line); border-radius: 6px; text-align: right; font-size: .86rem; }
.qty-input.edited { border-color: var(--accent); background: #f2f6fd; font-weight: 600; }
.rev-check { width: 18px; height: 18px; cursor: pointer; }

.pager { display: flex; gap: 1rem; align-items: center; justify-content: center; margin: 1rem 0; color: var(--muted); }
.chk-inline { display: inline-flex; align-items: center; gap: .3rem; font-size: .85rem; color: var(--muted); white-space: nowrap; }

.subst-box { border: 1px dashed var(--line); border-radius: 8px; padding: .7rem .9rem; margin: 1rem 0; }
.subst-box .k { color: var(--muted); font-size: .82rem; margin-bottom: .3rem; }
#users-admin { margin-top: 1.5rem; }

/* ---------- Forms ---------- */
.inline-form { display: flex; flex-wrap: wrap; gap: .5rem; margin-bottom: 1rem; }
.inline-form input { padding: .5rem .6rem; border: 1px solid var(--line); border-radius: 8px; }
.params-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px,1fr)); gap: .9rem; background: var(--panel); padding: 1.2rem; border-radius: var(--radius); border: 1px solid var(--line); box-shadow: var(--shadow); align-items: end; }
.params-grid label { display: flex; flex-direction: column; gap: .3rem; font-size: .82rem; color: var(--muted); }
.params-grid input { padding: .5rem; border: 1px solid var(--line); border-radius: 8px; font-size: .95rem; }
.params-grid button { grid-column: 1 / -1; justify-self: start; }

/* ---------- Import ---------- */
.import-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px,1fr)); gap: 1rem; }
.import-card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.1rem; box-shadow: var(--shadow); }
.import-card h3 { margin: 0 0 .4rem; }
.import-card p { color: var(--muted); font-size: .85rem; min-height: 2.4em; }
.import-card input[type=file] { display: block; margin: .5rem 0; font-size: .82rem; }
.import-result { margin-top: .7rem; font-size: .84rem; }
.import-result .ok { color: var(--ok); }
.import-result .bad { color: var(--danger); }
#import-logs, #recon-list { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: .4rem 1rem; box-shadow: var(--shadow); }
.log-row { padding: .5rem 0; border-bottom: 1px solid var(--line); font-size: .85rem; display: flex; gap: 1rem; }
.log-row:last-child { border: none; }
.log-kind { font-weight: 600; text-transform: uppercase; font-size: .72rem; color: var(--accent); width: 90px; }

/* ---------- Reconcile ---------- */
.recon-item { padding: .8rem 0; border-bottom: 1px solid var(--line); }
.recon-item:last-child { border: none; }
.recon-head { display: flex; align-items: baseline; gap: .8rem; flex-wrap: wrap; }
.recon-raw { font-weight: 700; font-size: 1rem; }
.recon-meta { color: var(--muted); font-size: .82rem; }
.recon-actions { display: flex; gap: .4rem; flex-wrap: wrap; margin-top: .5rem; align-items: center; }
.recon-actions input { padding: .35rem .5rem; border: 1px solid var(--line); border-radius: 7px; }
.cand-btn { background: #eef3fb; border: 1px solid #cfdcf3; color: var(--accent-dark); border-radius: 7px; padding: .28rem .5rem; cursor: pointer; font-size: .8rem; }
.cand-btn:hover { background: #dfe9fa; }

/* ---------- Drawer ---------- */
.drawer { position: fixed; inset: 0; z-index: 50; }
.drawer-backdrop { position: absolute; inset: 0; background: rgba(20,28,44,.35); }
.drawer-panel { position: absolute; right: 0; top: 0; height: 100%; width: min(560px, 94vw); background: var(--panel); box-shadow: -8px 0 30px rgba(0,0,0,.15); padding: 1.5rem 1.6rem; overflow-y: auto; }
.drawer-close { position: absolute; top: 1rem; right: 1rem; border: none; background: none; font-size: 1.2rem; cursor: pointer; color: var(--muted); }
.d-code { font-size: 1.4rem; font-weight: 700; color: var(--accent-dark); }
.d-name { color: var(--muted); margin-bottom: 1rem; }
.d-grid { display: grid; grid-template-columns: 1fr 1fr; gap: .6rem 1.2rem; margin: 1rem 0; }
.d-grid .k { color: var(--muted); font-size: .82rem; }
.d-grid .v { font-weight: 600; font-size: 1.05rem; }
.method-box { border: 1px solid var(--line); border-radius: 8px; padding: .8rem 1rem; margin: .6rem 0; }
.method-box.sel { border-color: var(--accent); background: #f5f8fe; }
.method-box h4 { margin: 0 0 .3rem; font-size: .9rem; }
.method-num { font-size: 1.6rem; font-weight: 700; color: var(--accent); }
.d-final { display: flex; align-items: center; gap: .6rem; margin: 1rem 0; }
.d-final input { width: 110px; padding: .5rem; border: 2px solid var(--accent); border-radius: 8px; font-size: 1.2rem; text-align: right; font-weight: 700; }
.d-note { width: 100%; padding: .5rem; border: 1px solid var(--line); border-radius: 8px; min-height: 60px; }
.chart-box { margin: 1rem 0; }

/* ---------- Toast ---------- */
.toast { position: fixed; bottom: 1.4rem; left: 50%; transform: translateX(-50%); background: #1f2733; color: #fff; padding: .7rem 1.2rem; border-radius: 10px; box-shadow: var(--shadow); z-index: 100; font-size: .9rem; }
.toast.bad { background: var(--danger); }

.error { color: var(--danger); font-size: .85rem; text-align: center; }
.ok { color: var(--ok); }
.bad { color: var(--danger); }
