:root {
  --bg: #f3f4f6;
  --panel: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --border: #d1d5db;
  --border-soft: #e5e7eb;
  --primary: #1d4ed8;
  --primary-dark: #1e3a8a;
  --danger: #b91c1c;
  --ok: #047857;
  --warn: #b45309;
  --bar: #111827;
  --radius: 6px;
  font-size: 14px;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.4;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.45rem; margin: 0; }
h2 { font-size: 1.1rem; margin: 0 0 .6rem; }
h3 { font-size: .95rem; margin: 0 0 .5rem; }
small, .muted { color: var(--muted); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
[hidden] { display: none !important; }

/* ---------- top bar & menu ---------- */
.topbar {
  background: var(--bar);
  color: #fff;
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0 1rem;
  height: 48px;
  position: sticky;
  top: 0;
  z-index: 20;
}
.brand { color: #fff; font-weight: 700; letter-spacing: .02em; font-size: 1.05rem; }
.brand span { color: #93c5fd; }
.brand:hover { text-decoration: none; }
.menu { display: flex; gap: .1rem; list-style: none; margin: 0; padding: 0; flex: 1; }
.menu > li { position: relative; }
.menu > li > a, .menu > li > button {
  display: block; color: #e5e7eb; padding: .85rem .7rem; font: inherit; background: none; border: 0; cursor: pointer;
}
.menu > li > a:hover, .menu > li > button:hover, .menu > li:focus-within > button, .menu > li.active > a, .menu > li.active > button {
  background: #374151; color: #fff; text-decoration: none;
}
.menu ul {
  display: none; position: absolute; top: 100%; left: 0; min-width: 230px; list-style: none; margin: 0; padding: .3rem 0;
  background: #fff; border: 1px solid var(--border); border-radius: 0 0 var(--radius) var(--radius); box-shadow: 0 8px 24px rgba(0,0,0,.15);
}
.menu > li:hover ul, .menu > li:focus-within ul { display: block; }
.menu ul a { display: block; padding: .45rem 1rem; color: var(--text); }
.menu ul a:hover { background: #eff6ff; text-decoration: none; }
.menu ul .sep { border-top: 1px solid var(--border-soft); margin: .3rem 0; }
.topsearch input {
  width: 220px; border: 0; border-radius: var(--radius); padding: .4rem .6rem; background: #374151; color: #fff;
}
.topsearch input::placeholder { color: #9ca3af; }
.userbox { display: flex; align-items: center; gap: .6rem; color: #d1d5db; white-space: nowrap; }
.userbox a, .userbox button { color: #d1d5db; background: none; border: 0; font: inherit; cursor: pointer; padding: 0; }

/* ---------- layout ---------- */
main { max-width: 1440px; margin: 0 auto; padding: 1rem 1.25rem 3rem; }
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 1rem; flex-wrap: wrap; }
.page-head .actions { display: flex; gap: .4rem; flex-wrap: wrap; align-items: center; }
.panel { background: var(--panel); border: 1px solid var(--border-soft); border-radius: var(--radius); padding: 1rem; margin-bottom: 1rem; }
.panel.tight { padding: 0; overflow: hidden; }
.panel > h2:first-child { display: flex; justify-content: space-between; align-items: center; }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 1rem; }
.grid3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1rem; }
@media (max-width: 900px) { .grid2, .grid3 { grid-template-columns: 1fr; } .menu { display: none; } }

/* ---------- KPIs ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: .75rem; margin-bottom: 1rem; }
.kpi { background: var(--panel); border: 1px solid var(--border-soft); border-radius: var(--radius); padding: .8rem 1rem; display: block; color: inherit; }
.kpi:hover { text-decoration: none; border-color: var(--primary); }
.kpi .v { font-size: 1.5rem; font-weight: 700; }
.kpi .l { color: var(--muted); font-size: .85rem; }
.kpi.warn .v { color: var(--warn); }
.kpi.bad .v { color: var(--danger); }

/* ---------- tables ---------- */
table { border-collapse: collapse; width: 100%; }
.table th, .table td { padding: .45rem .6rem; border-bottom: 1px solid var(--border-soft); text-align: left; vertical-align: top; }
.table th { background: #f9fafb; font-weight: 600; color: #374151; font-size: .85rem; white-space: nowrap; }
.table tbody tr:hover { background: #f9fafb; }
.table td.num, .table th.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.table tfoot td { font-weight: 700; background: #f9fafb; }
.table tr.late td:first-child { box-shadow: inset 3px 0 0 var(--danger); }
.table .empty { text-align: center; color: var(--muted); padding: 1.5rem; }
.table-wrap { overflow-x: auto; }

/* ---------- badges ---------- */
.badge { display: inline-block; padding: .1rem .5rem; border-radius: 999px; font-size: .78rem; font-weight: 600; background: #e5e7eb; color: #374151; white-space: nowrap; }
.badge.abierta, .badge.pendiente, .badge.pedido { background: #dbeafe; color: #1e40af; }
.badge.en_curso { background: #fef3c7; color: #92400e; }
.badge.espera_piezas { background: #ffedd5; color: #9a3412; }
.badge.terminada, .badge.aceptado, .badge.disponible, .badge.recibido, .badge.cobrada { background: #d1fae5; color: #065f46; }
.badge.entregada { background: #e0e7ff; color: #3730a3; }
.badge.facturada { background: #ede9fe; color: #5b21b6; }
.badge.anulada, .badge.rechazado, .badge.anulado, .badge.baja { background: #fee2e2; color: #991b1b; }
.badge.prestado, .badge.recibida { background: #fef3c7; color: #92400e; }
.badge.r1 { background: #fee2e2; color: #991b1b; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: .35rem; padding: .45rem .85rem; border-radius: var(--radius);
  border: 1px solid var(--border); background: #fff; color: var(--text); font: inherit; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: #f3f4f6; text-decoration: none; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.danger { color: var(--danger); border-color: #fca5a5; }
.btn.danger:hover { background: #fef2f2; }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.small { padding: .2rem .5rem; font-size: .85rem; }
.btn.link { border: 0; background: none; color: var(--primary); padding: 0; }
form.inline { display: inline; }

/* ---------- forms ---------- */
.form-grid { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: .75rem 1rem; }
.field { grid-column: span 3; display: flex; flex-direction: column; gap: .25rem; }
.field.sm { grid-column: span 2; }
.field.md { grid-column: span 3; }
.field.lg { grid-column: span 6; }
.field.full { grid-column: 1 / -1; }
@media (max-width: 900px) { .field, .field.sm, .field.md, .field.lg { grid-column: 1 / -1; } }
label { font-size: .82rem; font-weight: 600; color: #374151; }
.field .help { font-size: .78rem; color: var(--muted); }
.field .err { font-size: .8rem; color: var(--danger); }
.field.has-error input, .field.has-error select, .field.has-error textarea { border-color: var(--danger); }
input, select, textarea {
  font: inherit; padding: .42rem .55rem; border: 1px solid var(--border); border-radius: var(--radius); background: #fff; color: var(--text); width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid #93c5fd; outline-offset: 0; border-color: var(--primary); }
input[readonly], input:disabled, select:disabled, textarea:disabled { background: #f3f4f6; color: #4b5563; }
input[type=checkbox], input[type=radio] { width: auto; }
.check { flex-direction: row; align-items: center; gap: .5rem; padding-top: 1.3rem; }
textarea { min-height: 70px; resize: vertical; }
.form-actions { display: flex; gap: .5rem; margin-top: 1rem; align-items: center; flex-wrap: wrap; }
.form-actions .spacer { flex: 1; }
.filters { display: flex; gap: .5rem; align-items: end; flex-wrap: wrap; margin-bottom: .75rem; }
.filters .field { grid-column: auto; min-width: 150px; }
.filters input[type=search] { min-width: 260px; }

/* ---------- flash ---------- */
.flash { padding: .65rem 1rem; border-radius: var(--radius); margin-bottom: .75rem; border: 1px solid; }
.flash.ok { background: #ecfdf5; border-color: #a7f3d0; color: #065f46; }
.flash.error, .nojs-warning { background: #fef2f2; border-color: #fecaca; color: #991b1b; }
/* Not a .flash: API clients read flash messages as `main > .flash`. */
.nojs-warning { padding: .65rem 1rem; border: 1px solid #fecaca; border-radius: var(--radius); }

/* ---------- tabs ---------- */
.tabs { display: flex; gap: .2rem; border-bottom: 1px solid var(--border); margin-bottom: 1rem; flex-wrap: wrap; }
.tabs button {
  border: 1px solid transparent; border-bottom: 0; background: none; padding: .5rem .9rem; font: inherit; cursor: pointer;
  border-radius: var(--radius) var(--radius) 0 0; color: var(--muted); margin-bottom: -1px;
}
.tabs button[aria-selected=true] { background: var(--panel); border-color: var(--border); color: var(--text); font-weight: 600; }

/* ---------- line editor ---------- */
.lines th, .lines td { padding: .25rem .3rem; border-bottom: 1px solid var(--border-soft); }
.lines th { font-size: .8rem; text-align: left; color: #374151; background: #f9fafb; }
.lines input, .lines select { padding: .3rem .4rem; }
.lines .c-kind { width: 120px; }
.lines .c-ref { width: 150px; }
.lines .c-qty, .lines .c-dto, .lines .c-vat { width: 80px; }
.lines .c-price, .lines .c-amount { width: 105px; }
.lines .c-del { width: 34px; }
.lines td.amount { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; padding-right: .6rem; }
.lines tr.kind-mo td:first-child { box-shadow: inset 3px 0 0 #2563eb; }
.lines tr.kind-rec td:first-child { box-shadow: inset 3px 0 0 #059669; }
.lines tr.kind-otro td:first-child { box-shadow: inset 3px 0 0 #9ca3af; }
.lines-toolbar { display: flex; gap: .4rem; margin-top: .5rem; flex-wrap: wrap; align-items: center; }
.totals { margin-left: auto; min-width: 280px; }
.totals td { padding: .2rem .5rem; }
.totals td:last-child { text-align: right; font-variant-numeric: tabular-nums; }
.totals tr.grand td { font-weight: 700; font-size: 1.1rem; border-top: 2px solid var(--text); }

/* ---------- checklist ---------- */
.checklist { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 1rem; }
.checklist table td { padding: .2rem .3rem; }
.checklist .opt { text-align: center; width: 64px; font-size: .8rem; }

/* ---------- dl ---------- */
dl.props { display: grid; grid-template-columns: max-content 1fr; gap: .3rem 1rem; margin: 0; }
dl.props dt { color: var(--muted); }
dl.props dd { margin: 0; }

/* ---------- reports ---------- */
.chart { display: flex; flex-direction: column; gap: .25rem; margin: .5rem 0 1rem; }
.chart .row { display: grid; grid-template-columns: 140px 1fr 110px; gap: .5rem; align-items: center; font-size: .85rem; }
.chart .bar { background: #3b82f6; height: 14px; border-radius: 3px; min-width: 1px; }
.report-list { list-style: none; padding: 0; margin: 0; }
.report-list li { padding: .5rem 0; border-bottom: 1px solid var(--border-soft); }

/* ---------- agenda ---------- */
.week { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: .5rem; }
.day { background: var(--panel); border: 1px solid var(--border-soft); border-radius: var(--radius); min-height: 260px; display: flex; flex-direction: column; }
.day.today { border-color: var(--primary); box-shadow: 0 0 0 1px var(--primary); }
.day header { padding: .4rem .6rem; border-bottom: 1px solid var(--border-soft); display: flex; justify-content: space-between; font-weight: 600; font-size: .85rem; }
.day .items { padding: .4rem; display: flex; flex-direction: column; gap: .35rem; flex: 1; }
.appt { border: 1px solid var(--border-soft); border-left: 3px solid var(--primary); border-radius: 4px; padding: .3rem .4rem; font-size: .82rem; background: #f9fafb; }
.appt.anulada { opacity: .5; text-decoration: line-through; border-left-color: var(--danger); }
.appt.recibida { border-left-color: var(--ok); }
@media (max-width: 1100px) { .week { grid-template-columns: 1fr; } }

/* ---------- terminal ---------- */
.terminal { max-width: 760px; margin: 0 auto; }
.terminal input { font-size: 1.4rem; padding: .6rem .8rem; }
.terminal label { font-size: 1rem; }

/* ---------- login ---------- */
.login { max-width: 360px; margin: 10vh auto; }
.login h1 { margin-bottom: 1rem; }

.pagination { display: flex; gap: .3rem; align-items: center; justify-content: flex-end; padding: .6rem; }
