:root {
  --bg: #f4f2ec; --surface: #ffffff; --text: #1d241e; --muted: #5d685f; --line: #d9d6cc;
  --primary: #2f5d3a; --primary-2: #3f7a4c; --on-primary: #fff; --accent: #c98a1b;
  --chip: #e8eee6; --warn: #b3261e; --ok: #2e7d32; --pend: #b26a00;
  --radius: 14px; --tabs-h: 64px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121512; --surface: #1c211d; --text: #e9ece6; --line: #2f3630;
    --primary: #5fa36f; --primary-2: #77b885; --on-primary: #0d130e; --chip: #26302a;
    --warn: #ef6b62; --ok: #6fcf75; --pend: #f0a640; --muted: #a2aba0;
  }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom));
}
.top {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px; background: var(--primary); color: var(--on-primary);
}
.brand { display: flex; gap: 8px; align-items: center; font-weight: 700; font-size: 18px; }
.brand img { width: 26px; height: 26px; border-radius: 6px; }
.pill {
  display: flex; align-items: center; gap: 8px; border: 0; border-radius: 99px; padding: 7px 12px;
  background: rgba(255,255,255,.16); color: inherit; font: inherit; font-size: 14px; cursor: pointer;
}
.dot { width: 9px; height: 9px; border-radius: 50%; background: #aaa; }
.dot.on { background: #8ef59a; } .dot.off { background: #ffb4a8; }
.dot.busy { background: #ffd36b; animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .3; } }

main { max-width: 760px; margin: 0 auto; padding: 16px; }
.view { display: none; } .view.active { display: block; }

label { display: block; margin: 0 0 14px; font-size: 14px; color: var(--muted); font-weight: 600; }
.lbl { font-size: 14px; color: var(--muted); font-weight: 600; margin: 0 0 6px; }
input, select, textarea {
  display: block; width: 100%; margin-top: 5px; padding: 12px; font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px; min-height: 48px;
}
textarea { resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary-2); border-color: transparent; }
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.hint { font-size: 13px; color: var(--muted); margin: -6px 0 14px; }
.center { text-align: center; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 46px;
  padding: 10px 16px; border-radius: 12px; font: inherit; font-weight: 600; cursor: pointer;
  border: 1px solid var(--line); background: var(--surface); color: var(--text); text-decoration: none; margin: 0;
}
.btn.primary { background: var(--primary); color: var(--on-primary); border-color: var(--primary); }
.btn.big { width: 100%; min-height: 56px; font-size: 18px; margin-top: 8px; }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: 14px; }
.btn:disabled { opacity: .5; }
.btn.rec { background: var(--warn); color: #fff; border-color: var(--warn); }

.seg { display: flex; background: var(--chip); border-radius: 12px; padding: 4px; gap: 4px; margin-bottom: 16px; }
.seg button {
  flex: 1; border: 0; background: transparent; padding: 12px 6px; border-radius: 9px; font: inherit;
  font-weight: 600; color: var(--muted); cursor: pointer;
}
.seg button.on { background: var(--surface); color: var(--text); box-shadow: 0 1px 3px rgba(0,0,0,.15); }
.seg.small button { padding: 9px 4px; }

.chips, .sugs { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.chips button, .sugs button {
  border: 1px solid var(--line); background: var(--surface); color: var(--text); border-radius: 99px;
  padding: 9px 14px; font: inherit; font-size: 14px; cursor: pointer;
}
.chips button.on { background: var(--primary); color: var(--on-primary); border-color: var(--primary); }

.ta-wrap { position: relative; }
.ta-wrap textarea { padding-right: 52px; }
.mic {
  position: absolute; right: 6px; bottom: 6px; width: 42px; height: 42px; border-radius: 50%;
  border: 0; background: var(--chip); font-size: 20px; cursor: pointer;
}
.mic.on { background: var(--warn); }

.attach { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 12px; }
.attach .btn { margin: 0; color: var(--text); font-size: 15px; padding: 10px 6px; }
.thumbs { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 8px; margin-bottom: 10px; }
.thumbs div { position: relative; aspect-ratio: 1; }
.thumbs img { width: 100%; height: 100%; object-fit: cover; border-radius: 10px; }
.x {
  position: absolute; top: 4px; right: 4px; width: 28px; height: 28px; border-radius: 50%;
  border: 0; background: rgba(0,0,0,.65); color: #fff; font-size: 15px; cursor: pointer; line-height: 1;
}
.audios > div { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.audios audio { flex: 1; height: 40px; }
.audios .x { position: static; flex: none; }

.filters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.filters input { grid-column: 1 / -1; }
.filters select, .filters input { margin: 0; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0; }

.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; margin-bottom: 12px;
}
.card h3 { margin: 0 0 12px; font-size: 17px; }
.card .btn { margin: 0 8px 8px 0; }
.item header { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-bottom: 6px; }
.item .when { font-weight: 700; }
.item .lote { color: var(--muted); font-size: 14px; }
.item p { margin: 6px 0; white-space: pre-wrap; }
.item .meta { font-size: 13px; color: var(--muted); }
.item .files { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.item .files a { font-size: 14px; color: var(--primary-2); }
.item .files img { width: 64px; height: 64px; object-fit: cover; border-radius: 8px; }
.badge {
  display: inline-block; font-size: 12px; font-weight: 700; padding: 3px 8px; border-radius: 99px;
  background: var(--chip); color: var(--text);
}
.badge.apl { background: #e3ecf8; color: #1d4f91; }
.badge.Alta, .badge.Urgente { background: #fbe0dd; color: var(--warn); }
.badge.Media { background: #fdf0d7; color: #8a5800; }
.badge.Baja { background: #e2f1e3; color: var(--ok); }
.badge.Pendiente { background: #fdf0d7; color: #8a5800; }
.badge.Realizada { background: #e2f1e3; color: var(--ok); }
.badge.Cancelada { background: #eee; color: #666; }
.badge.sync { background: #fdf0d7; color: var(--pend); }
.badge.err { background: #fbe0dd; color: var(--warn); }
@media (prefers-color-scheme: dark) {
  .badge.apl { background: #1f3350; color: #9cc2f5; }
  .badge.Alta, .badge.Urgente, .badge.err { background: #4a2220; }
  .badge.Media, .badge.Pendiente, .badge.sync { background: #47361a; color: #f0c06b; }
  .badge.Baja, .badge.Realizada { background: #1f3a22; }
}

.chat-top { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; margin-bottom: 12px; }
.chat-top select { margin: 0; }
.msgs { display: flex; flex-direction: column; gap: 10px; margin-bottom: 12px; }
.msg { max-width: 92%; padding: 10px 14px; border-radius: 14px; background: var(--surface); border: 1px solid var(--line); overflow-wrap: anywhere; }
.msg.user { align-self: flex-end; background: var(--primary); color: var(--on-primary); border-color: var(--primary); white-space: pre-wrap; }
.msg.err { border-color: var(--warn); }
.msg h2, .msg h3, .msg h4, .msg h5 { margin: 12px 0 6px; font-size: 16px; }
.msg h2 { font-size: 19px; }
.msg p { margin: 6px 0; } .msg ul { margin: 6px 0; padding-left: 20px; }
.msg .tools { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.tbl { overflow-x: auto; margin: 8px 0; }
.tbl table { border-collapse: collapse; font-size: 14px; min-width: 100%; }
.tbl th, .tbl td { border: 1px solid var(--line); padding: 6px 8px; text-align: left; vertical-align: top; }
.tbl th { background: var(--chip); }
.typing { color: var(--muted); font-style: italic; }
.chat-in {
  position: sticky; bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom)); display: flex; gap: 8px;
  background: var(--bg); padding: 8px 0;
}
.chat-in textarea { margin: 0; max-height: 140px; }
.chat-in .btn { flex: none; }

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--surface); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.tabs button {
  height: var(--tabs-h); border: 0; background: none; color: var(--muted); font: inherit; font-size: 12px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; cursor: pointer;
}
.tabs button b { font-size: 21px; font-weight: normal; filter: grayscale(1); opacity: .7; }
.tabs button.active { color: var(--primary); font-weight: 700; }
.tabs button.active b { filter: none; opacity: 1; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabs-h) + 16px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: #222; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 15px; z-index: 20;
  opacity: 0; pointer-events: none; transition: opacity .2s; max-width: calc(100% - 32px); text-align: center;
}
#toast.show { opacity: 1; }
#printArea { display: none; }

@media (min-width: 900px) {
  main { padding-top: 24px; }
}
@media print {
  body { background: #fff; color: #000; padding: 0; }
  body > *:not(#printArea) { display: none !important; }
  #printArea { display: block; font-size: 12px; }
  #printArea .card { border: 1px solid #bbb; break-inside: avoid; }
  #printArea .no-print { display: none; }
  .badge { border: 1px solid #999; background: none !important; color: #000 !important; }
}
