:root {
    --bg: #fafafa;
    --card: #ffffff;
    --border: #e7e7ea;
    --text: #16181d;
    --muted: #6b7078;
    --primary: #4550e5;
    --primary-dark: #3739c4;
    --danger: #c23636;
    --danger-bg: #fdf3f3;
    --danger-border: #f1d4d4;
    --warn: #b45309;
    --ok: #1a7f4e;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
}

header.topbar {
    position: sticky;
    top: 0;
    z-index: 100;
    background: var(--card);
    color: var(--text);
    border-bottom: 1px solid var(--border);
    padding: 0 1.5rem;
}

header.topbar a { color: inherit; text-decoration: none; }

.topbar-row {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 0;
}

.topbar-center {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    display: flex;
    gap: 0.6rem;
}

header.topbar .brand { font-weight: 700; font-size: 1.05rem; letter-spacing: -0.01em; }

.subnav {
    display: flex;
    gap: 0.6rem;
    padding: 0.65rem 0;
    border-top: 1px solid var(--border);
}

.topbar-btn {
    display: inline-block;
    background: #fff;
    color: var(--muted);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.35rem 0.8rem;
    font-size: 0.78rem;
    font-weight: 500;
    cursor: pointer;
}
.topbar-btn:hover { border-color: var(--muted); color: var(--text); }

@media (max-width: 900px) {
    .topbar-row { flex-wrap: wrap; row-gap: 0.6rem; }
    .topbar-center { position: static; transform: none; order: 3; width: 100%; justify-content: center; }
}

nav.tabs {
    display: flex;
    align-items: center;
    gap: 1.4rem;
    font-size: 0.88rem;
}

nav.tabs a { color: var(--muted); font-weight: 500; }
nav.tabs a:hover { color: var(--text); }

.user-menu { position: relative; display: flex; align-items: center; }
.user-menu-trigger {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: var(--primary);
    border: none;
    padding: 0;
    font: inherit;
    font-size: 0.78rem;
    font-weight: 600;
    color: #fff;
    cursor: pointer;
}
.alerta-punto {
    position: absolute;
    top: -2px;
    right: -2px;
    width: 11px;
    height: 11px;
    border-radius: 50%;
    background: var(--danger);
    border: 2px solid var(--card);
}
.alerta-punto-inline {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--danger);
    flex-shrink: 0;
}
.user-menu-trigger:hover { background: var(--primary-dark); }
.user-menu-dropdown {
    position: absolute;
    top: 100%;
    right: 0;
    margin-top: 0.6rem;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.08);
    min-width: 150px;
    padding: 0.4rem;
    z-index: 150;
}
.user-menu-dropdown a {
    display: block;
    padding: 0.5rem 0.7rem;
    border-radius: 6px;
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text);
    text-decoration: none;
}
.user-menu-dropdown a:hover { background: var(--bg); }
.user-menu-idioma {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.4rem 0.7rem 0.6rem;
    margin-bottom: 0.3rem;
    border-bottom: 1px solid var(--border);
}
.user-menu-idioma a {
    padding: 0.1rem 0.3rem;
    border-radius: 4px;
    font-size: 0.78rem;
    font-weight: 600;
    color: var(--muted);
    text-decoration: none;
}
.user-menu-idioma a.active { color: var(--primary); }
.user-menu-idioma a:hover { color: var(--text); }

main {
    max-width: 1100px;
    margin: 0 auto;
    padding: 1.5rem;
}

.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 1.25rem;
    margin-bottom: 1.25rem;
}

h1 { font-size: 1.3rem; font-weight: 600; margin-top: 0; letter-spacing: -0.01em; }
h2 { font-size: 0.95rem; font-weight: 600; letter-spacing: normal; text-transform: none; }

.jurisprudencia-item { margin-bottom: 0.8rem; }
.jurisprudencia-row { display: flex; align-items: flex-start; gap: 1rem; }
.jurisprudencia-row-controles { display: flex; align-items: center; gap: 0.6rem; flex-wrap: wrap; flex-shrink: 0; }
.jurisprudencia-preview {
    flex: 1 1 auto;
    min-width: 200px;
    height: 220px;
    border: 1px solid var(--border);
    border-radius: 8px;
    overflow: hidden;
}
.jurisprudencia-preview iframe { width: 100%; height: 100%; border: none; }

.grid-3 {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1rem;
}

@media (max-width: 700px) {
    .grid-3 { grid-template-columns: 1fr; }
}

.cdp-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem; align-items: stretch; }
@media (max-width: 900px) {
    .cdp-grid { grid-template-columns: 1fr; }
}
.cdp-card { display: flex; flex-direction: column; }
.cdp-lista { list-style: none; padding: 0; margin: 0; }
.cdp-lista li { padding: 0.4rem 0; border-bottom: 1px solid var(--border); }
.cdp-lista li:last-child { border-bottom: none; }
.cdp-lista label { display: flex; align-items: center; gap: 0.6rem; cursor: pointer; }
.cdp-lista input[type="checkbox"] { width: 16px; height: 16px; flex-shrink: 0; }
.cdp-carta {
    white-space: pre-wrap;
    font-family: inherit;
    font-size: 0.9rem;
    line-height: 1.5;
    margin: 0;
}
.cdp-firma {
    text-align: center;
    font-size: 0.9rem;
    line-height: 1.5;
}
.cdp-controles { display: flex; align-items: flex-end; gap: 1rem; flex-wrap: wrap; }
.cdp-controles > div { display: flex; flex-direction: column; gap: 0.3rem; }

.codigos-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    grid-template-rows: repeat(6, 1fr);
    gap: 0.6rem;
    height: calc(100vh - 230px);
}

.codigo-box {
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 0.5rem;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    font-size: 0.72rem;
    font-weight: 600;
    line-height: 1.25;
    color: var(--text);
    overflow: hidden;
    text-decoration: none;
    cursor: pointer;
    transition: background 0.1s, color 0.1s, border-color 0.1s;
}

.codigo-box:not(.vacio):hover { background: var(--primary); border-color: var(--primary); color: #ffffff; }

.codigo-box.vacio {
    border-style: dashed;
    background: transparent;
    cursor: default;
}

@media (max-width: 900px) {
    .codigos-grid { grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(10, 1fr); height: calc(100vh - 280px); }
}

@media (max-width: 600px) {
    .codigos-grid { grid-template-columns: repeat(2, 1fr); grid-template-rows: repeat(15, 1fr); }
}

.stat { text-align: center; }
.stat .num { font-size: 1.7rem; font-weight: 700; letter-spacing: -0.01em; }
.stat .label { color: var(--muted); font-size: 0.8rem; font-weight: 500; }

.stat-link { text-decoration: none; display: block; cursor: pointer; transition: background 0.1s, color 0.1s; }
.stat-link:hover { background: var(--primary); }
.stat-link:hover .num, .stat-link:hover .label { color: #ffffff; }
.stat-link:active, .stat-link.active { background: var(--primary); }
.stat-link:active .num, .stat-link:active .label,
.stat-link.active .num, .stat-link.active .label { color: #ffffff; }

.filter-bar { display: flex; flex-wrap: wrap; justify-content: center; gap: 0.5rem; margin: 0.75rem 0; }
.filter-btn {
    display: inline-block;
    padding: 0.4rem 0.85rem;
    border-radius: 20px;
    border: 1px solid var(--border);
    background: #fff;
    color: var(--muted);
    font-size: 0.76rem;
    font-weight: 500;
    text-decoration: none;
    cursor: pointer;
}
.filter-btn:hover { border-color: var(--muted); }
.filter-btn:active, .filter-btn.active { background: var(--text); color: #ffffff; border-color: var(--text); }
.filter-btn.danger { border-color: var(--danger-border); color: var(--danger); background: var(--danger-bg); }
.filter-btn.danger:hover, .filter-btn.danger:active { background: var(--danger); color: #ffffff; border-color: var(--danger); }

.estadisticas-grid { display: flex; flex-wrap: nowrap; gap: 1.5rem; align-items: flex-start; overflow-x: auto; }
.estadistica-col { flex: 1 1 0; min-width: 220px; text-align: center; }
.estadistica-col h3 { margin-bottom: 0.9rem; font-size: 0.92rem; font-weight: 600; }

.grafico-torta-wrap { position: relative; width: 170px; height: 170px; margin: 0 auto 1.2rem; }
.grafico-torta-svg { width: 100%; height: 100%; animation: grafico-aparecer 0.5s ease-out; }
.grafico-segmento { transition: stroke-width 0.15s ease, opacity 0.15s ease; cursor: pointer; }
.grafico-segmento.activo { stroke-width: 24; }
.grafico-segmento.atenuado { opacity: 0.3; }
@keyframes grafico-aparecer {
    from { opacity: 0; transform: scale(0.8); }
    to { opacity: 1; transform: scale(1); }
}
.grafico-centro {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    pointer-events: none;
}
.grafico-centro .num { font-size: 1.5rem; font-weight: 700; line-height: 1.1; color: var(--text); }
.grafico-centro .label { font-size: 0.7rem; color: var(--muted); max-width: 100px; }

.grafico-leyenda { list-style: none; padding: 0; margin: 0; text-align: left; font-size: 0.8rem; }
.grafico-leyenda li { display: flex; align-items: center; gap: 0.5rem; padding: 0.3rem 0.45rem; border-radius: 6px; cursor: pointer; transition: background 0.12s ease; }
.grafico-leyenda li:hover, .grafico-leyenda li.activo { background: var(--bg); }
.grafico-color { display: inline-block; width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.grafico-etiqueta { flex: 1 1 auto; }
.grafico-leyenda .cantidad { font-weight: 600; color: var(--text); }
.grafico-leyenda .porcentaje { color: var(--muted); font-size: 0.72rem; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 0.6rem 0.6rem; border-bottom: 1px solid var(--border); font-size: 0.78rem; }
th { color: var(--muted); font-weight: 500; font-size: 0.74rem; text-transform: none; }
td:not(:last-child) { white-space: nowrap; }
tr:last-child td { border-bottom: none; }

.btn {
    display: inline-block;
    background: var(--primary);
    color: white;
    border: 1px solid var(--primary);
    padding: 0.5rem 0.9rem;
    border-radius: 8px;
    cursor: pointer;
    text-decoration: none;
    font-size: 0.85rem;
    font-weight: 500;
}
.btn:hover { background: var(--primary-dark); border-color: var(--primary-dark); }
.btn.secondary { background: #fff; color: var(--text); border: 1px solid var(--border); }
.btn.secondary:hover { border-color: var(--muted); }
.btn.danger { background: var(--danger-bg); color: var(--danger); border: 1px solid var(--danger-border); }
.btn.danger:hover { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn.small { padding: 0.32rem 0.65rem; font-size: 0.78rem; }
.btn-login { display: block; width: 100%; text-align: center; background: #0e1d32; border-color: #0e1d32; }
.btn-login:hover { background: #162a44; border-color: #162a44; }

.calendar-grid {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 1px;
    background: var(--border);
    border: 1px solid var(--border);
    border-radius: 10px;
    overflow: hidden;
}
.calendar-grid .cal-head {
    background: var(--card);
    padding: 0.5rem;
    text-align: center;
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--muted);
}
.cal-day {
    background: var(--card);
    min-height: 108px;
    padding: 0.4rem;
}
.cal-day.otro-mes { background: var(--bg); }
.cal-day.hoy { box-shadow: inset 0 0 0 2px var(--primary); }
.cal-day .num { font-weight: 600; font-size: 0.85rem; margin-bottom: 0.3rem; }
.cal-day-num {
    display: block;
    width: 100%;
    text-align: left;
    background: none;
    border: none;
    padding: 0;
    font-family: inherit;
    color: inherit;
    cursor: pointer;
}
.cal-day-num:hover { text-decoration: underline; }
.cal-event {
    display: block;
    border-radius: 4px;
    padding: 0.1rem 0.4rem;
    margin-bottom: 0.2rem;
    font-size: 0.68rem;
    text-decoration: none;
    color: white;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.cal-event-audiencia { background: var(--primary); }
.cal-event-plazo { background: var(--warn); }
.cal-event-vencido { background: var(--danger); }
.cal-event-google { background: var(--ok); }
.cal-event-corte { background: #6d28d9; }
.cal-event-corte-suprema { background: #9333ea; }
.cal-event-visita { background: #be185d; }
.cal-event-gestion { background: #0891b2; }
.cal-event[data-popup] { cursor: pointer; }

.cal-popup {
    position: absolute;
    z-index: 200;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 10px 28px rgba(0, 0, 0, 0.12);
    padding: 1rem 1.1rem;
    width: 280px;
    font-size: 0.82rem;
}
.cal-popup-close {
    position: absolute;
    top: 0.4rem;
    right: 0.6rem;
    background: none;
    border: none;
    font-size: 1.2rem;
    line-height: 1;
    cursor: pointer;
    color: var(--muted);
}
.cal-popup h3 { margin: 0 1.2rem 0.7rem 0; font-size: 0.9rem; font-weight: 600; }
.cal-popup dl { display: grid; grid-template-columns: auto 1fr; gap: 0.25rem 0.6rem; margin: 0 0 0.8rem; }
.cal-popup dt { color: var(--muted); font-weight: 600; white-space: nowrap; }
.cal-popup dd { margin: 0; text-align: right; }

.cal-day-popup {
    width: 260px;
    max-height: 320px;
    overflow-y: auto;
}
.cal-day-popup-item {
    display: block;
    width: 100%;
    text-align: left;
    border: none;
    border-radius: 4px;
    padding: 0.3rem 0.5rem;
    margin-bottom: 0.25rem;
    font-size: 0.78rem;
    color: white;
    cursor: pointer;
}

form.inline { display: inline; }

input[type=text], input[type=email], input[type=date], input[type=number], input[type=password], select, textarea {
    width: 100%;
    padding: 0.5rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.9rem;
    font-family: inherit;
    color: var(--text);
    background: #fff;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); }
input[readonly] { background: var(--bg); color: var(--muted); cursor: default; }

label { font-size: 0.82rem; color: var(--muted); display: block; margin: 0.6rem 0 0.2rem; }

.field-row { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.5rem 1rem; }
@media (max-width: 700px) { .field-row { grid-template-columns: 1fr; } }

.badge {
    display: inline-block;
    padding: 0.15rem 0.55rem;
    border-radius: 999px;
    font-size: 0.72rem;
    font-weight: 600;
}
.badge.vigente { background: #eceafc; color: #4338ca; }
.badge.suspendida { background: #fef3c7; color: #92400e; }
.badge.terminada { background: #e7f5ec; color: #1a7f4e; }
.badge.vencido { background: #fdecec; color: #c23636; }
.badge.pendiente { background: #fef3c7; color: #92400e; }
.badge.cumplido { background: #e7f5ec; color: #1a7f4e; }
.badge.cumplida { background: #e7f5ec; color: #1a7f4e; }
.badge.orden-pendiente { background: #fdecec; color: #c23636; }
.badge.orden-contraorden { background: #e7f5ec; color: #1a7f4e; }

.visto-hoy { color: #1a7f4e; font-weight: 600; }
.visto-atrasado { color: var(--danger); font-weight: 600; }

.flash { padding: 0.7rem 1rem; border-radius: 8px; margin-bottom: 1rem; font-size: 0.88rem; }
.flash.success { background: #e7f5ec; color: #1a7f4e; }
.flash.error { background: #fdecec; color: #c23636; }

.muted { color: var(--muted); }
.search-bar { display: flex; gap: 0.5rem; margin-bottom: 1rem; }
.search-bar input { flex: 1; }
.actions-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem; gap: 1rem; }
a.link { color: var(--primary); font-weight: 500; text-decoration: none; }
a.link:hover { text-decoration: underline; }
.login-wrap { max-width: 360px; margin: 4rem auto; }
.empty { color: var(--muted); font-size: 0.88rem; padding: 0.5rem 0; }
