:root {
  --brand: #0f766e; --brand-2: #115e59; --brand-soft: #ccfbf1;
  --bg: #f4f5f7; --panel: #ffffff; --line: #e3e6ea; --line-2: #eef0f3;
  --text: #1c2430; --muted: #6b7584; --danger: #c2410c; --ok: #15803d; --warn: #b45309;
  --radius: 10px; --shadow: 0 1px 2px rgba(16,24,40,.06), 0 1px 3px rgba(16,24,40,.08);
  font-family: Inter, system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  font-size: 14px; color: var(--text);
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0; } h2 { font-size: 17px; margin: 0 0 12px; } h3 { font-size: 14px; margin: 0 0 8px; }
.muted { color: var(--muted); } .small { font-size: 12px; } .right { text-align: right; } .nowrap { white-space: nowrap; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.pos { color: var(--ok); } .neg { color: var(--danger); }

/* layout */
.app { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.side { background: #0f1720; color: #cbd5e1; padding: 18px 12px; position: sticky; top: 0; height: 100vh; overflow: auto; }
.logo { font-weight: 800; font-size: 20px; color: #fff; letter-spacing: .5px; padding: 0 10px 18px; display: flex; gap: 8px; align-items: center; }
.logo b { background: var(--brand); border-radius: 7px; width: 28px; height: 28px; display: grid; place-items: center; font-size: 15px; }
.side a { display: flex; gap: 10px; align-items: center; color: #cbd5e1; padding: 9px 10px; border-radius: 8px; margin: 2px 0; }
.side a:hover { background: #1e2935; text-decoration: none; color: #fff; }
.side a.active { background: var(--brand); color: #fff; }
.side .grp { margin-top: 10px; } .side .grp-t { font-size: 11px; text-transform: uppercase; letter-spacing: .5px; color: #64748b; padding: 6px 10px 2px; font-weight: 700; }
.side a.sub { padding: 6px 10px 6px 26px; font-size: 13px; margin: 0; }
.kanban { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(230px, 1fr); gap: 12px; overflow-x: auto; padding-bottom: 10px; align-items: start; }
.kcol { background: #eef0f3; border-radius: 10px; min-height: 200px; }
.kcol-h { padding: 10px 12px; font-weight: 700; display: flex; justify-content: space-between; border-radius: 10px 10px 0 0; color: #fff; }
.kcol-b { padding: 8px; display: flex; flex-direction: column; gap: 8px; min-height: 120px; }
.kcol-b.over { background: #dbeafe; }
.kcard { background: #fff; border-radius: 8px; padding: 10px 12px; box-shadow: var(--shadow); cursor: grab; font-size: 13px; }
.kcard b { color: var(--brand); display: block; margin-bottom: 4px; }
.kcard .row { display: flex; gap: 6px; align-items: center; color: var(--muted); margin-top: 3px; }
.ratio { background: #fff; border: 1px solid var(--line); border-radius: 10px; padding: 10px 14px; display: flex; gap: 18px; align-items: center; }
.ratio .big { font-size: 30px; font-weight: 800; }
.b-en_proceso { background: #e5e7eb; color: #374151; } .b-pausado { background: #fde68a; color: #78350f; } .b-cobrada { background: #bbf7d0; color: #14532d; }
.chart { display: flex; gap: 6px; align-items: flex-end; height: 160px; padding-top: 10px; }
.chart .c { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px; font-size: 10px; color: var(--muted); height: 100%; justify-content: flex-end; }
.chart .c i { display: block; width: 70%; background: var(--brand); border-radius: 4px 4px 0 0; min-height: 1px; }
.side .sep { border-top: 1px solid #263241; margin: 12px 4px; }
.side .who { font-size: 12px; padding: 10px; color: #94a3b8; }
.main { padding: 22px 28px 60px; min-width: 0; }
.topbar { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; flex-wrap: wrap; }
.topbar .grow { flex: 1; }
.burger { display: none; }

/* components */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.card + .card { margin-top: 16px; }
.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: repeat(2, minmax(0,1fr)); } .g3 { grid-template-columns: repeat(3, minmax(0,1fr)); } .g4 { grid-template-columns: repeat(4, minmax(0,1fr)); }
.kpi .v { font-size: 22px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .l { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .4px; }
.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: #fff; color: var(--text); padding: 7px 12px; border-radius: 8px; cursor: pointer; font: inherit; font-weight: 500; white-space: nowrap; }
.btn:hover { background: #f8fafc; text-decoration: none; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-2); }
.btn.danger { color: var(--danger); }
.btn.sm { padding: 4px 8px; font-size: 12px; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btns { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
input, select, textarea { font: inherit; color: inherit; border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px; background: #fff; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand-soft); border-color: var(--brand); }
input[type=checkbox], input[type=radio], input[type=color] { width: auto; }
input[type=color] { width: 60px; height: 36px; padding: 2px; }
label.f { display: block; margin-bottom: 10px; } label.f > span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; font-weight: 500; }
.chk { display: flex; gap: 8px; align-items: center; margin: 6px 0; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 0 14px; }
.form-grid .full { grid-column: 1 / -1; }
table.t { width: 100%; border-collapse: collapse; }
table.t th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .4px; color: var(--muted); font-weight: 600; padding: 8px; border-bottom: 1px solid var(--line); background: #fafbfc; }
table.t td { padding: 8px; border-bottom: 1px solid var(--line-2); vertical-align: top; }
table.t tr.click { cursor: pointer; } table.t tr.click:hover td { background: #f8fafc; }
table.t tfoot td { font-weight: 700; border-top: 2px solid var(--line); }
.tw { overflow-x: auto; }
.badge { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: 11px; font-weight: 600; background: #eef2f6; color: #334155; white-space: nowrap; }
.b-borrador { background: #eef2f6; } .b-enviado { background: #dbeafe; color: #1e40af; } .b-visto { background: #ede9fe; color: #5b21b6; }
.b-aceptado { background: #dcfce7; color: #166534; } .b-rechazado { background: #fee2e2; color: #991b1b; } .b-en_obra { background: #fef3c7; color: #92400e; } .b-finalizado { background: #e2e8f0; color: #0f172a; }
.b-factura, .b-anticipo { background: #dcfce7; color: #166534; } .b-abono { background: #fee2e2; color: #991b1b; } .b-proforma { background: #e0e7ff; color: #3730a3; }
.b-anulada { background: #fee2e2; color: #991b1b; } .b-añadida { background: #fef3c7; color: #92400e; }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); margin: 16px 0; overflow-x: auto; }
.tabs a { padding: 9px 13px; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; font-weight: 500; }
.tabs a.active { color: var(--brand); border-color: var(--brand); text-decoration: none; }
.empty { text-align: center; padding: 36px; color: var(--muted); }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.toolbar input[type=search] { max-width: 280px; }

/* editor de presupuesto */
.chapter { overflow-x: auto; background: #fff; border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 14px; box-shadow: var(--shadow); }
.chapter-h { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--line); background: #fafbfc; border-radius: var(--radius) var(--radius) 0 0; }
.chapter-h input.chname { font-weight: 700; border-color: transparent; background: transparent; max-width: 420px; }
.chapter-h input.chname:hover { border-color: var(--line); background: #fff; }
.chapter-h .tot { margin-left: auto; font-weight: 700; font-variant-numeric: tabular-nums; }
.handle { cursor: grab; color: #9aa4b2; user-select: none; padding: 0 4px; }
.it { display: grid; grid-template-columns: 18px 70px minmax(200px,1fr) 56px 80px 92px 92px 100px 30px; gap: 6px; align-items: start; padding: 8px 12px; border-bottom: 1px solid var(--line-2); }
.it.nocost { grid-template-columns: 18px 70px minmax(200px,1fr) 56px 80px 100px 100px 30px; }
.it.head { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .3px; font-weight: 600; padding-top: 6px; padding-bottom: 6px; }
.it input { padding: 4px 6px; border-color: transparent; background: transparent; }
.it input:hover, .it textarea:hover { border-color: var(--line); background: #fff; }
.it textarea { padding: 4px 6px; border-color: transparent; background: transparent; resize: vertical; min-height: 30px; }
.it .amt { text-align: right; font-weight: 600; padding-top: 6px; font-variant-numeric: tabular-nums; }
.it.off { opacity: .55; } .it.off .amt { text-decoration: line-through; }
.it.dragging, .chapter.dragging { opacity: .4; }
.it .meta { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 3px; padding-left: 6px; }
.it .meta .badge { cursor: pointer; }
.chapter-f { padding: 8px 12px; display: flex; gap: 8px; }
.totals { position: sticky; bottom: 0; background: #0f1720; color: #fff; border-radius: var(--radius); padding: 12px 18px; display: flex; gap: 26px; flex-wrap: wrap; align-items: center; margin-top: 10px; box-shadow: 0 -4px 20px rgba(0,0,0,.08); z-index: 5; }
.totals div span { display: block; font-size: 11px; opacity: .7; text-transform: uppercase; }
.totals div b { font-size: 16px; font-variant-numeric: tabular-nums; }

/* editor v2 (estilo original) */
.it2 { display: grid; grid-template-columns: 18px 12px minmax(220px,1fr) 84px 96px 100px 92px 28px 28px 28px; gap: 8px; align-items: center; padding: 8px 12px; border-bottom: 1px solid var(--line-2); }
.it2.nocost { grid-template-columns: 18px 12px minmax(220px,1fr) 84px 96px 100px 28px 28px 28px; }
.it2.head { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .3px; font-weight: 600; padding-top: 6px; padding-bottom: 6px; }
.it2 .dot { width: 10px; height: 10px; border-radius: 50%; background: #64748b; }
.it2 .dot.ok { background: #16a34a; } .it2 .dot.no { background: #dc2626; } .it2 .dot.new { background: #f59e0b; }
.it2 .desc b { display: block; } .it2 .desc .muted { font-size: 11px; }
.it2 input.num { padding: 4px 6px; text-align: right; }
.it2 .ben { text-align: right; color: var(--ok); font-weight: 600; font-variant-numeric: tabular-nums; }
.it2.off { opacity: .5; } .it2.off .amt { text-decoration: line-through; }
.it2 .amt { text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }
.det { display: grid; grid-template-columns: 36px 1fr 1fr; gap: 14px; padding: 6px 14px 16px 30px; background: #fbfcfd; border-bottom: 1px solid var(--line); }
.det .icons { display: flex; flex-direction: column; gap: 8px; } .det .icons button { border: 0; background: none; cursor: pointer; font-size: 16px; color: var(--brand); padding: 2px; }
.det .prices { display: grid; grid-template-columns: 120px 1fr; gap: 6px 10px; align-items: center; }
.det .full { grid-column: 2 / -1; }
.pcards { display: flex; gap: 10px; overflow-x: auto; padding: 4px 0 8px; }
.pcard { flex: 0 0 150px; border: 2px solid var(--line); border-radius: 10px; background: #fff; overflow: hidden; cursor: pointer; position: relative; font-size: 12px; }
.pcard.on { border-color: #16a34a; box-shadow: 0 0 0 2px #bbf7d0; }
.pcard img, .pcard .noimg { width: 100%; height: 86px; object-fit: cover; display: block; background: #eef0f3; }
.pcard .pp { padding: 6px 8px; } .pcard .pp b { display: block; }
.pcard .x { position: absolute; top: 4px; right: 4px; background: #dc2626; color: #fff; border: 0; border-radius: 6px; font-size: 11px; cursor: pointer; padding: 1px 6px; }
.mrow { display: grid; grid-template-columns: 1fr 90px 50px 100px 100px 28px; gap: 6px; align-items: center; margin-bottom: 4px; }
.lock { color: #16a34a; font-size: 12px; }
.chapters-only .items, .chapters-only .it2.head, .chapters-only .chapter-f { display: none; }
.items-only .meta-opts { display: none; }
.chapter.folded .items, .chapter.folded .it2.head, .chapter.folded .chapter-f { display: none; }
.chapter.folded .chapter-h { border-bottom: 0; border-radius: var(--radius); }
.chapter-h[data-fold-area], .chapter-h input.chname[readonly] { cursor: pointer; }
.chapter-h .fold-btn { font-size: 14px; min-width: 28px; }
.chapter-h .fold-count { white-space: nowrap; }

/* modal & toast */
.modal-bg { position: fixed; inset: 0; background: rgba(15,23,32,.45); display: flex; align-items: flex-start; justify-content: center; padding: 50px 16px; z-index: 50; overflow: auto; }
.modal { background: #fff; border-radius: 12px; width: 100%; max-width: 640px; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.modal.wide { max-width: 980px; }
.modal-h { padding: 14px 18px; border-bottom: 1px solid var(--line); display: flex; align-items: center; } .modal-h h2 { margin: 0; flex: 1; }
.modal-b { padding: 18px; max-height: 70vh; overflow: auto; }
.modal-f { padding: 12px 18px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; }
#toast { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 99; }
#toast div { background: #0f1720; color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); max-width: 380px; }
#toast div.err { background: var(--danger); }

/* login */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: linear-gradient(135deg, #0f1720 0%, #134e4a 100%); }
.auth .card { width: 100%; max-width: 400px; }
.auth .logo { color: var(--text); padding: 0 0 14px; }

.bar { height: 8px; background: var(--line-2); border-radius: 99px; overflow: hidden; } .bar > i { display: block; height: 100%; background: var(--brand); }
.list-row { display: flex; gap: 10px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line-2); }
.list-row:last-child { border-bottom: 0; }
.opt-row { display: grid; grid-template-columns: 1fr 100px 100px 1fr 30px; gap: 6px; margin-bottom: 6px; align-items: center; }
.thumb { width: 44px; height: 44px; object-fit: cover; border-radius: 6px; background: var(--line-2); }

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .side { position: fixed; left: -240px; width: 230px; z-index: 40; transition: left .2s; }
  .side.open { left: 0; }
  .burger { display: inline-flex; }
  .main { padding: 16px; }
  .g2, .g3, .g4, .form-grid { grid-template-columns: 1fr; }
  .it, .it.nocost { grid-template-columns: 18px 1fr 70px 90px 30px; }
  .it .hide-m { display: none; }
  .it.head { display: none; }
}
@media print { .side, .topbar, .tabs, .toolbar, .totals { display: none !important; } .app { display: block; } }
.unit-edit { cursor: pointer; } .unit-edit:hover { color: var(--brand, #0f766e); text-decoration: underline; }
.colorpick { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; } .colorpick input[type=color] { width: 44px; height: 36px; padding: 2px; flex: none; }
.colorpick .colorref { width: 150px; flex: none; } .colorpick .colorhint { flex: 1 1 100%; min-height: 14px; }
/* Generar con IA */
.aimode { cursor: pointer; margin: 0; border: 2px solid transparent; } .aimode.on { border-color: var(--brand, #0f766e); background: color-mix(in srgb, var(--brand, #0f766e) 6%, #fff); }
.aitab tr.aich td { background: #f6f7f9; } .aitab td { vertical-align: top; }

/* Editor de cartas */
.lt-toolbar { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin: 12px 0 8px; padding: 8px; background: #f6f7f9; border-radius: 8px; position: sticky; top: 0; z-index: 2; }
.lt-toolbar select { font: inherit; padding: 5px 8px; border-radius: 6px; border: 1px solid #d5d9df; }
.lt-paper { background: #fff; color: #111; max-width: 210mm; min-height: 160mm; margin: 0 auto; padding: 14mm 18mm !important; border: 1px solid #e3e6ea; border-radius: 6px; box-shadow: 0 2px 10px rgba(0,0,0,.06); outline: none; font-family: Arial, Helvetica, sans-serif; font-size: 10.6pt; line-height: 1.42; }
.lt-paper p { margin: 0 0 11px; } .lt-paper h4 { font-size: 10.6pt; margin: 26px 0 12px; }
.lt-paper:focus { border-color: var(--brand, #0f766e); }
