:root {
    --bg: #f4f6f9;
    --surface: #ffffff;
    --text: #1f2933;
    --muted: #6b7785;
    --border: #d9dee5;
    --primary: #2f5fb3;
    --primary-dark: #244a8c;
    --nav: #1d2733;
    --nav-text: #e8edf3;
    --header-bg: #4472c4;
    --danger: #b3261e;
    --success: #1e7b3a;
    --warning: #8a5a00;
    --radius: 6px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
    margin: 0;
    font-family: Roboto, "Segoe UI", Helvetica, Arial, sans-serif;
    font-size: 14px;
    line-height: 1.45;
    color: var(--text);
    background: var(--bg);
}
h1, h2, h3 { margin: 0; font-weight: 400; }
a { color: var(--primary); }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); border: 0; }
.container { width: 100%; max-width: 1100px; margin: 0 auto; padding: 0 16px; }

/* ---------- Controles ---------- */

input, select, textarea {
    font: inherit;
    color: inherit;
    padding: 6px 10px;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: #fff;
    min-height: 34px;
}
input:focus, select:focus, textarea:focus, button:focus-visible, .tree-row:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 1px;
}
input:disabled, select:disabled, textarea:disabled { background: #eef0f3; color: #59616b; cursor: not-allowed; }
textarea { width: 100%; resize: vertical; }

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    font: inherit;
    padding: 6px 14px;
    min-height: 34px;
    border: 1px solid transparent;
    border-radius: var(--radius);
    cursor: pointer;
    text-decoration: none;
}
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); }
.btn-light { background: #fff; border-color: var(--border); color: var(--text); }
.btn-light:hover:not(:disabled) { background: #eef2f7; }
.btn-light.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-link { background: none; color: var(--primary); padding-left: 0; }
.btn-lg { min-height: 44px; font-size: 16px; }
.btn.disabled { opacity: .55; cursor: not-allowed; pointer-events: none; }
.btn-sm { min-height: 28px; padding: 3px 10px; font-size: 13px; }
.btn-block { width: 100%; }

.icon-btn { background: none; border: 0; padding: 4px; cursor: pointer; border-radius: var(--radius); line-height: 0; }
.icon-btn:hover:not(:disabled) { background: #e8eef9; }
.icon-pdf { color: #c62828; }
.icon-doc { color: #2f5fb3; }
.acciones { display: inline-flex; align-items: center; gap: 6px; }
.icon-del { color: var(--danger); font-size: 18px; line-height: 1; }
.icon-btn.busy { opacity: .5; cursor: progress; }

.alert { position: relative; padding: 12px 36px 12px 14px; border-radius: var(--radius); margin-bottom: 14px; border: 1px solid; }
.alert p { margin: 0; }
.alert-warning { background: #fff6e0; border-color: #f0d28a; color: var(--warning); }
.alert-success { background: #e6f4ea; border-color: #a8d5b5; color: var(--success); }
.alert-danger { background: #fdecea; border-color: #f2b8b5; color: var(--danger); }
.alert-close { position: absolute; top: 6px; right: 10px; border: 0; background: none; font-size: 20px; cursor: pointer; color: inherit; }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: 0 1px 3px rgba(0, 0, 0, .06); }
.panel-heading { padding: 12px 16px; border-bottom: 1px solid var(--border); background: #f7f8fa; border-radius: var(--radius) var(--radius) 0 0; }
.panel-title { font-size: 16px; }
.panel-body { padding: 16px; }

/* ---------- Login ---------- */

#login { background: url(../img/bg.jpg) no-repeat center / cover fixed; min-height: 100%; }
.login-header { background: #fff; padding: 15px 0; }
.login-main { display: grid; grid-template-columns: 1fr 1fr; gap: 32px; padding-top: 50px; padding-bottom: 100px; align-items: start; }
.welcome { color: #fff; text-shadow: 1px 1px rgba(0, 0, 0, .5); }
.welcome h1 { font-size: 42px; margin-bottom: 14px; }
.welcome p { font-size: 18px; max-width: 460px; }
.form-group { margin-bottom: 14px; }
.input-group { display: flex; }
.input-addon { display: flex; align-items: center; padding: 0 12px; background: #eef0f3; border: 1px solid var(--border); border-right: 0; border-radius: var(--radius) 0 0 var(--radius); }
.input-group .form-control { flex: 1; min-width: 0; border-radius: 0 var(--radius) var(--radius) 0; min-height: 40px; }

/* ---------- Dashboard ---------- */

#dashboard { padding-top: 50px; }
.topbar { position: fixed; top: 0; left: 0; right: 0; height: 50px; z-index: 50; display: flex; align-items: center; gap: 12px; padding: 0 16px; background: var(--nav); color: var(--nav-text); }
.brand { width: 186px; height: 30px; background: url(../img/logo-white.png) no-repeat left center / contain; margin-right: auto; }
.menu-toggle { display: none; background: none; border: 0; color: inherit; font-size: 22px; cursor: pointer; }
.user-menu { position: relative; }
.user-btn { background: none; border: 0; color: inherit; font: inherit; cursor: pointer; padding: 8px; }
.dropdown { position: absolute; right: 0; top: 100%; margin: 4px 0 0; padding: 4px 0; list-style: none; min-width: 180px; background: #fff; border: 1px solid var(--border); border-radius: var(--radius); box-shadow: 0 4px 12px rgba(0, 0, 0, .15); }
.dropdown a { display: block; padding: 8px 14px; color: var(--text); text-decoration: none; }
.dropdown a:hover { background: #eef2f7; }

.layout { display: flex; min-height: calc(100vh - 50px); }
.sidebar { width: 250px; flex: none; background: #fff; border-right: 1px solid var(--border); padding: 12px 0; overflow-y: auto; }
.content { flex: 1; min-width: 0; padding: 20px 24px 40px; }

.tree, .tree ul { list-style: none; margin: 0; padding: 0; }
.tree ul { padding-left: 16px; }
.tree-row { display: flex; align-items: center; gap: 6px; padding: 7px 14px; cursor: pointer; user-select: none; border-left: 3px solid transparent; }
.tree-row:hover { background: #eef2f7; }
.tree-row.selected { background: #e3ebf9; border-left-color: var(--primary); color: var(--primary-dark); font-weight: 500; }
.tree-arrow { width: 12px; color: var(--muted); }
.tree-empty { padding: 8px 14px; color: var(--muted); }
.empty-state { color: var(--muted); padding: 40px 0; text-align: center; }
.loading-text { color: var(--muted); }

/* ---------- Páginas de módulos ---------- */

.page-header { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; padding-bottom: 10px; border-bottom: 1px solid var(--border); }
.page-header h1 { font-size: 26px; }
.page-header h1 small { font-size: 14px; color: var(--muted); margin-left: 8px; }
.page-header-actions h1 { font-size: 22px; }
.spacer { flex: 1; }

.form-inline { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px 18px; margin-bottom: 16px; }
.form-inline .form-group { margin: 0; display: flex; align-items: center; gap: 8px; }

.form-grid { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; }
.form-row { display: grid; grid-template-columns: 160px minmax(0, 1fr); align-items: center; gap: 8px 12px; margin-bottom: 12px; }
.form-row.two { grid-template-columns: 160px minmax(0, 1fr) auto minmax(0, 1fr); }
.form-row label { color: var(--muted); }
.form-row label.inline { text-align: right; }
.form-row .form-row { margin: 0; }
.inline-fields { display: flex; align-items: center; gap: 8px; min-width: 0; }
.inline-fields input[type=text] { flex: 1; min-width: 0; }
.w-xs { width: 90px; }
.w-sm { width: 130px; }
.w-lg { width: 100%; max-width: 420px; }
.hint { color: var(--muted); font-size: 13px; margin: 0; }
.nota-observada { color: var(--danger); }

.upload-box { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin-bottom: 12px; }

/* ---------- Grilla ---------- */

.grid { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); position: relative; }
.grid.loading::after { content: "Cargando…"; position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(255, 255, 255, .7); color: var(--muted); z-index: 3; }
.grid-toolbar { display: flex; gap: 8px; padding: 8px; border-bottom: 1px solid var(--border); background: #f7f8fa; }
.grid-scroll { overflow: auto; }
.grid-table { width: 100%; border-collapse: collapse; }
.grid-table th, .grid-table td { padding: 8px 10px; border-bottom: 1px solid #edf0f4; text-align: left; vertical-align: middle; }
.grid-table thead th { position: sticky; top: 0; background: #f0f3f8; font-weight: 600; white-space: nowrap; z-index: 2; }
.grid-table th.sortable { cursor: pointer; user-select: none; }
.sort-ind { margin-left: 6px; font-size: 10px; color: var(--primary); }
.grid-table .filter-row th { top: 38px; padding: 4px 6px; background: #f7f9fc; font-weight: 400; z-index: 1; }
.filter-input { width: 100%; min-height: 28px; padding: 2px 8px; font-size: 13px; }
.grid-table tbody tr.clickable { cursor: pointer; }
.grid-table tbody tr.clickable:hover { background: #eef4ff; }
.grid-table tbody tr.selected { background: #dce8fb; }
.grid-table tr.empty td { text-align: center; color: var(--muted); padding: 24px; }
.grid-info { padding: 6px 10px; font-size: 12px; color: var(--muted); text-align: right; }
.grid-pager { display: flex; gap: 4px; justify-content: center; padding: 8px; }
.grid.readonly tr > :last-child { display: none; }

/* ---------- Modal ---------- */

.modal { position: fixed; inset: 0; z-index: 100; display: none; align-items: flex-start; justify-content: center; padding: 60px 16px 16px; background: rgba(15, 23, 32, .5); overflow-y: auto; }
.modal.open { display: flex; }
body.modal-open { overflow: hidden; }
.modal-content { background: #fff; border-radius: 8px; width: 100%; max-width: 520px; box-shadow: 0 10px 30px rgba(0, 0, 0, .3); }
.modal-content.modal-lg { max-width: 720px; }
.modal-header, .modal-footer { display: flex; align-items: center; gap: 8px; padding: 12px 16px; }
.modal-header { border-bottom: 1px solid var(--border); justify-content: space-between; }
.modal-footer { border-top: 1px solid var(--border); justify-content: flex-end; }
.modal-title { font-size: 18px; }
.modal-x { border: 0; background: none; font-size: 24px; cursor: pointer; line-height: 1; }
.modal-body { padding: 16px; }
.modal-body .form-row { grid-template-columns: 70px minmax(0, 1fr) auto minmax(0, 1fr); }
.modal-body .form-row select { grid-column: 2 / -1; }

/* ---------- Toasts ---------- */

#toasts { position: fixed; top: 60px; right: 16px; z-index: 200; display: flex; flex-direction: column; gap: 8px; max-width: min(420px, calc(100vw - 32px)); }
.toast { display: flex; align-items: flex-start; gap: 10px; padding: 10px 14px; border-radius: var(--radius); color: #fff; box-shadow: 0 4px 12px rgba(0, 0, 0, .25); background: #2d4a7a; }
.toast span { flex: 1; }
.toast button { background: none; border: 0; color: inherit; font-size: 18px; line-height: 1; cursor: pointer; }
.toast-success { background: #1e7b3a; }
.toast-warning { background: #a86a00; }
.toast-error { background: #b3261e; }

/* ---------- Orden de servicio (vista) ---------- */

.order-actions { align-items: center; }
/* La orden se muestra y se imprime como una hoja A4 (210 x 297 mm = 794 x 1123 px); el contenido se reduce para caber en una sola hoja */
.hoja-escala { display: flex; justify-content: center; }
.orden-doc { position: relative; overflow: hidden; flex: none; box-sizing: border-box; width: 794px; height: 1123px; padding: 38px; background: #fff; border: 1px solid var(--border); box-shadow: 0 2px 8px rgba(0, 0, 0, .15); font-size: 12px; }
.orden-doc.anulada::before { content: "ANULADO"; position: absolute; top: 40%; left: 50%; transform: translate(-50%, -50%) rotate(-25deg); font-size: 110px; font-weight: 700; color: rgba(179, 38, 30, .15); pointer-events: none; letter-spacing: 6px; }
.orden-head { display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: 14px; }
.orden-logo img { max-width: 220px; max-height: 80px; }
.orden-tipo { font-size: 20px; font-weight: 500; }
.orden-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; margin-bottom: 14px; }
.orden-datos, .totales { display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; margin: 0; align-content: start; }
.orden-datos dt, .totales dt { text-align: right; }
.orden-datos dd, .totales dd { margin: 0; border: 1px solid var(--border); padding: 2px 8px; }
.titulo-fondo { background: var(--header-bg); color: #fff; padding: 3px 8px; margin-bottom: 4px; font-weight: 500; }
.orden-tabla { width: 100%; border-collapse: collapse; margin-bottom: 12px; }
.orden-tabla th { background: var(--header-bg); color: #fff; padding: 4px 6px; border: 1px solid var(--border); font-weight: 500; }
.orden-tabla td { padding: 4px 6px; border: 1px solid #ddd; vertical-align: top; }
.orden-tabla .c, .orden-tabla tr.c td { text-align: center; }
.orden-tabla .r { text-align: right; }
.table-scroll { overflow-x: auto; }
.orden-desc { margin: 10px 0; }
.orden-pie { display: grid; grid-template-columns: 1.4fr 1fr; gap: 18px; border-top: 1px solid #ddd; padding-top: 10px; }
.requisitos { border: 1px solid var(--border); padding: 6px; }
.totales { margin-left: auto; width: 100%; max-width: 320px; }
.totales dt { white-space: nowrap; }
.totales dd { text-align: right; font-variant-numeric: tabular-nums; }
.totales dt.total, .totales dd.total { font-weight: 700; border-top: 3px double #000; }
.orden-firmas { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; text-align: center; margin-top: 18px; }
.orden-firmas img { max-width: 40%; max-height: 90px; }
.orden-contacto { text-align: center; margin-top: 16px; }

/* ---------- Responsive ---------- */

@media screen and (max-width: 800px) {
    .login-main { grid-template-columns: 1fr; padding-top: 24px; }
    .welcome h1 { font-size: 30px; }
    .menu-toggle { display: block; }
    .sidebar { position: fixed; top: 50px; bottom: 0; left: 0; z-index: 40; transform: translateX(-100%); transition: transform .2s; box-shadow: 2px 0 10px rgba(0, 0, 0, .2); }
    .sidebar.open { transform: none; }
    .content { padding: 16px; }
    .form-row, .form-row.two, .modal-body .form-row { grid-template-columns: 1fr; }
    .form-row label.inline { text-align: left; }
    .page-header h1 small { display: block; margin: 4px 0 0; }

    /* Grillas como lista de tarjetas (estilo ListView de Android) */
    .grid-search { display: block; width: calc(100% - 16px); margin: 8px; }
    .grid-scroll { max-height: none !important; overflow: visible; }
    .grid-table, .grid-table tbody { display: block; }
    .grid-table thead { display: none; }
    .grid-table tbody tr { display: block; margin: 10px 8px; padding: 10px 12px; background: #fff; border: 1px solid var(--border); border-radius: 10px; box-shadow: 0 1px 3px rgba(0, 0, 0, .1); }
    .grid-table tbody tr.clickable:active { background: #eef4ff; }
    .grid-table tbody tr.selected { background: #dce8fb; }
    .grid-table td { display: flex; justify-content: space-between; align-items: center; gap: 12px; border: 0; padding: 4px 0; text-align: right !important; }
    .grid-table td::before { content: attr(data-label); flex: none; font-weight: 600; color: var(--muted); text-align: left; }
    .grid-table td:empty { display: none; }
    .grid-table tr.empty { box-shadow: none; border: 0; background: none; }
    .grid-table tr.empty td { justify-content: center; }
    .grid-table tr.empty td::before { content: none; }
    .grid.readonly tr > :last-child { display: none; }
    .grid-toolbar { flex-wrap: wrap; }
}
@media (min-width: 801px) {
    .grid-search { display: none; }
}

/* ---------- Impresión ---------- */

@page { size: A4 portrait; margin: 0; }
@media print {
    .no-print, .topbar, .sidebar, #toasts { display: none !important; }
    body, #dashboard { padding: 0; background: #fff; }
    .content { padding: 0; }
    html, body { height: auto; }
    .layout { display: block; min-height: 0; }
    .hoja-escala { display: block; zoom: 1 !important; }
    .orden-doc { width: 210mm; height: 296mm; padding: 10mm; border: 0; box-shadow: none; margin: 0; break-after: avoid; page-break-inside: avoid; }
    .orden-doc.anulada::before { color: rgba(179, 38, 30, .25); }
    .titulo-fondo, .orden-tabla th { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
