:root {
  --bg:       #fafafa;
  --fg:       #111;
  --muted:    #94897f;
  --border:   #e4e4e4;
  --accent:   #4805ff;
  --danger:   #c81e1e;
  --hover:    #f3f3f3;
}

* { box-sizing: border-box; }

body {
  font-family: "Montserrat", system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--fg);
  margin: 0;
  font-size: 14px;
  line-height: 1.4;
}

h1 { font-size: 20px; margin: 0; font-weight: 600; }

button {
  font: inherit;
  cursor: pointer;
  padding: 7px 14px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #fff;
}
button:hover { background: var(--hover); }
button.link {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 7px 4px;
  text-decoration: underline;
}
button.danger {
  color: var(--danger);
  border-color: var(--danger);
}
button.danger:hover { background: var(--danger); color: #fff; }
.detail-danger {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
}

input, select {
  font: inherit;
  padding: 7px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: #fff;
  outline: none;
}
input:focus, select:focus { border-color: var(--muted); }

/* ---------------- Login ---------------- */
.login {
  max-width: 360px;
  margin: 80px auto;
  padding: 28px;
  background: #fff;
  border-radius: 10px;
  border: 1px solid var(--border);
  box-shadow: 0 4px 16px rgba(0,0,0,0.04);
}
.login h1 { margin-bottom: 18px; }
.login form { display: flex; flex-direction: column; gap: 14px; }
.login label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 13px; }
.login input { font-size: 14px; }
.login button[type=submit] {
  background: var(--accent); color: #fff; border-color: var(--accent);
  font-weight: 600;
}
.login button[type=submit]:hover { filter: brightness(0.95); }
.error { color: var(--danger); font-size: 13px; margin: 0; }

/* ---------------- Inbox ---------------- */
.inbox { padding: 20px; max-width: 1200px; margin: 0 auto; }
.bar {
  display: flex; justify-content: space-between; align-items: center;
  gap: 16px; margin-bottom: 16px; flex-wrap: wrap;
}
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

table {
  width: 100%;
  border-collapse: collapse;
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  border: 1px solid var(--border);
}
thead th {
  text-align: left;
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--muted);
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  background: #fbfbfb;
}
tbody td {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--hover); }
.msg { max-width: 480px; }
.muted { color: var(--muted); font-size: 12px; }

.empty {
  text-align: center;
  color: var(--muted);
  padding: 60px 0;
}

/* View toggle (Lista / Por cluster) */
.view-toggle {
  display: inline-flex;
  border: 1px solid var(--border); border-radius: 6px;
  overflow: hidden; background: #fff;
}
.view-btn {
  border: none; border-radius: 0;
  padding: 7px 12px; font-size: 13px;
  background: #fff; color: var(--muted);
}
.view-btn + .view-btn { border-left: 1px solid var(--border); }
.view-btn:hover { background: var(--hover); }
.view-btn.current {
  background: var(--accent); color: #fff;
}

/* Cluster cards (vista agrupada) */
#grouped { display: flex; flex-direction: column; gap: 12px; }
.cluster-card {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.cluster-header {
  display: flex; align-items: flex-start; gap: 12px;
  padding: 12px 14px;
  background: #fbfbfb;
  border-bottom: 1px solid var(--border);
}
.cluster-select { padding-top: 2px; }
.cluster-meta { flex: 1; min-width: 0; }
.cluster-actions { flex-shrink: 0; }
.cluster-actions button {
  font-size: 12px; padding: 6px 12px;
  background: #4805ff; color: #fff; border-color: #4805ff;
}
.cluster-actions button:hover { filter: brightness(0.95); background: #4805ff; }
.cluster-meta h3 {
  margin: 0 0 4px; font-size: 14px; font-weight: 600;
}
.cluster-meta h3 code {
  background: rgba(72,5,255,0.06); padding: 1px 8px; border-radius: 4px;
  color: var(--accent); font-size: 13px;
}
.cluster-badges { margin: 0; font-size: 12px; }
.cluster-statuses { margin: 6px 0 0; display: flex; gap: 4px; flex-wrap: wrap; }
.cluster-unclassified .cluster-header { background: #fafafa; }
.cluster-unclassified h3 { color: var(--muted); }

.cluster-rows { list-style: none; margin: 0; padding: 0; }
.cluster-row {
  display: grid;
  grid-template-columns: auto 110px 160px 1fr auto;
  gap: 10px; align-items: center;
  padding: 8px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.cluster-row:last-child { border-bottom: none; }
.cluster-row:hover { background: var(--hover); }
.cluster-row .row-msg {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

@media (max-width: 720px) {
  .cluster-row {
    grid-template-columns: auto 1fr auto;
    grid-template-areas:
      "check user btn"
      "check when btn"
      "msg msg msg";
  }
  .cluster-row .row-check { grid-area: check; }
  .cluster-row .row-user  { grid-area: user; }
  .cluster-row .row-when  { grid-area: when; }
  .cluster-row .row-msg   { grid-area: msg; white-space: normal; }
  .cluster-row .open      { grid-area: btn; }
}

/* Bulk actions bar */
.bulk-bar {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 14px; margin-bottom: 10px;
  background: #f3eaff; border: 1px solid #d9c4ff; border-radius: 8px;
  font-size: 13px;
}
.bulk-info strong { color: var(--accent); }
.bulk-bar button { font-size: 13px; }

/* Checkbox column */
.col-check { width: 32px; padding-right: 0 !important; }
.col-check input { cursor: pointer; }

.footer-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 0;
}

/* Status badges */
.badge {
  display: inline-block; padding: 2px 8px; border-radius: 10px;
  font-size: 12px; font-weight: 500;
  background: var(--hover); color: var(--muted);
  border: 1px solid var(--border);
}
.badge-nuevo        { background: #eef2ff; color: #3530c8; border-color: #c8caf6; }
.badge-en_revision  { background: #fff7e6; color: #9a5a00; border-color: #f5dba8; }
.badge-planificado  { background: #e6f4ff; color: #08527d; border-color: #b3dcf5; }
.badge-hecho        { background: #e6f9ec; color: #0d6e2a; border-color: #b6e6c4; }
.badge-descartado   { background: #f7f7f7; color: #888;    border-color: #ddd; }

/* IA badges en el modal de detalle */
.badge-cat          { background: #f3eaff; color: #4805ff; border-color: #d9c4ff; font-weight: 600; }
.badge-sev-critica  { background: #fce7e7; color: #c81e1e; border-color: #f1b8b8; font-weight: 600; }
.badge-sev-alta     { background: #ffe9d6; color: #b04a00; border-color: #f5c896; }
.badge-sev-media    { background: #fff7e6; color: #9a5a00; border-color: #f5dba8; }
.badge-sev-baja     { background: #eef2ff; color: #6c6c8a; border-color: #d4d8ea; }

/* Score de prioridad del cluster (vista cluster_scores, db/003) */
.badge-score        { background: #10182b; color: #fff;    border-color: #10182b; font-weight: 700; }

/* ----- Banner de estado del pipeline ----- */
.pipeline-bar {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  border: 1px solid #d9c4ff; background: #f8f5ff; border-radius: 10px;
  padding: 10px 14px; margin-bottom: 14px; font-size: 14px;
}
#analyze-all { font-weight: 600; }
.nav-count {
  display: inline-block; min-width: 18px; padding: 1px 6px; border-radius: 9px;
  background: #4805ff; color: #fff; font-size: 11.5px; font-weight: 700;
  text-align: center; vertical-align: 1px;
}
.row-ia { margin-top: 3px; }
.mini { font-size: 11.5px; }

/* ----- Topnav (Inbox | Planes | Apps) ----- */
.topnav {
  display: flex; gap: 4px; padding: 10px 24px 0;
}
.topnav button {
  padding: 6px 14px; border: 1px solid var(--border); background: transparent;
  border-radius: 8px 8px 0 0; border-bottom: none; cursor: pointer;
  font-size: 14px; color: var(--muted);
}
.topnav button.current { background: var(--hover); color: inherit; font-weight: 600; }

/* ----- Planes (Fase 5 / Etapa B) ----- */
.plans-hint { margin: 0 0 16px; }
.plan-card {
  border: 1px solid var(--border); border-radius: 10px;
  padding: 14px 16px; margin-bottom: 14px;
}
.plan-header { display: flex; align-items: flex-start; gap: 12px; }
.plan-meta { flex: 1; min-width: 0; }
.plan-meta h3 { margin: 0 0 6px; font-size: 15px; }
.plan-actions { display: flex; gap: 8px; flex-shrink: 0; }
.plan-prompt { margin-top: 10px; }
.plan-prompt summary { cursor: pointer; color: var(--muted); font-size: 13px; }
.plan-prompt pre {
  background: #fafaff; border: 1px solid #ece8f7; border-radius: 8px;
  padding: 12px; margin-top: 8px; white-space: pre-wrap; word-break: break-word;
  font-size: 12.5px; max-height: 420px; overflow: auto;
}
.badge-plan-propuesto    { background: #eef2ff; color: #3530c8; border-color: #c8caf6; font-weight: 600; }
.badge-plan-aprobado     { background: #e6f4ff; color: #08527d; border-color: #b3dcf5; font-weight: 600; }
.badge-plan-en_curso     { background: #fff7e6; color: #9a5a00; border-color: #f5dba8; }
.badge-plan-implementado { background: #e6f9ec; color: #0d6e2a; border-color: #b6e6c4; }
.badge-plan-descartado   { background: #f7f7f7; color: #888;    border-color: #ddd; }

/* ----- Apps monitoreadas ----- */
.app-form {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
  margin-bottom: 18px;
}
.app-form input[type="text"], .app-form input:not([type]) { min-width: 180px; }
.app-form input[name="score_threshold"] { width: 170px; }
.checkline { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; }
.apps-actions { white-space: nowrap; }
.app-form textarea {
  flex-basis: 100%; font: inherit; font-size: 13px; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 8px; resize: vertical;
}

/* ----- Respuestas de capacitacion (Fase 5 / Etapa E) ----- */
.tr-text {
  width: 100%; margin-top: 10px; font: inherit; font-size: 13.5px;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px;
  resize: vertical; background: #fafaff;
}

/* ----- Metricas (Fase 5 / Etapa D) ----- */
.stat-row {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px; margin-bottom: 24px;
}
.stat-tile {
  border: 1px solid var(--border); border-radius: 10px;
  padding: 14px 16px; text-align: center;
}
.stat-value { font-size: 26px; font-weight: 700; }
.stat-label { font-size: 12.5px; color: var(--muted); margin-top: 4px; }
#metrics-body h2 { font-size: 15px; margin: 22px 0 8px; }
.metrics-table { border-collapse: collapse; width: 100%; }
.metrics-table th, .metrics-table td {
  text-align: left; padding: 6px 10px; border-bottom: 1px solid var(--border);
  font-size: 13.5px;
}
.metrics-table th { color: var(--muted); font-weight: 600; font-size: 12.5px; }

.analysis {
  background: #fafaff; border: 1px solid #ece8f7; border-radius: 8px;
  padding: 10px 12px; margin: 12px 0;
}
.analysis.pending { background: #fafafa; border-color: var(--border); }
.analysis .summary {
  margin: 6px 0 8px; font-size: 14px; color: var(--fg);
}
.analysis code {
  background: rgba(72,5,255,0.06); padding: 1px 6px; border-radius: 4px;
  font-size: 12px; color: var(--accent);
}
.triage-btn {
  font-size: 12px; padding: 4px 10px;
}

/* ---------------- Detail dialog ---------------- */
dialog {
  border: none;
  border-radius: 10px;
  padding: 0;
  max-width: 720px;
  width: 92vw;
  box-shadow: 0 12px 48px rgba(0,0,0,0.18);
}
dialog::backdrop { background: rgba(0,0,0,0.4); }

.detail-body { padding: 22px 24px 16px; }
.detail-body h2 { margin: 0 0 6px; font-size: 16px; }
.detail-body p.meta { margin: 4px 0; color: var(--muted); font-size: 12px; }
.detail-body pre.message {
  background: #fafafa; border: 1px solid var(--border); border-radius: 8px;
  padding: 14px; white-space: pre-wrap; word-break: break-word;
  font: inherit; margin: 14px 0;
}
.detail-body img { max-width: 100%; max-height: 360px; border-radius: 6px; border: 1px solid var(--border); margin: 8px 0; }
.detail-body .status-row {
  display: flex; gap: 6px; flex-wrap: wrap;
  margin-top: 16px; padding-top: 12px;
  border-top: 1px solid var(--border);
}
.status-btn {
  font-size: 13px; padding: 6px 12px;
}
.status-btn.current {
  background: var(--accent); color: #fff; border-color: var(--accent);
}
.detail-close {
  padding: 12px 24px 20px;
  text-align: right;
  border-top: 1px solid var(--border);
  background: #fafafa;
}

/* Prompt modal (Fases 3+4: prompts por app + cerrar el loop por app) */
#prompt-modal { max-width: 920px; }
.prompt-body { padding: 22px 24px 16px; }
.prompt-body h2 { margin: 0 0 6px; font-size: 16px; }
.prompt-meta { margin: 4px 0 14px; }
.prompt-loading {
  padding: 32px 0; text-align: center; color: var(--muted); font-size: 13px;
}
.prompt-error { margin: 10px 0 0; }
.prompt-footer {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 24px 20px;
  border-top: 1px solid var(--border);
  background: #fafafa;
}

.prompts-list { display: flex; flex-direction: column; gap: 18px; }
.prompt-section {
  border: 1px solid var(--border); border-radius: 8px;
  padding: 14px 16px; background: #fff;
}
.prompt-section + .prompt-section { margin-top: 0; }
.prompt-section-header {
  display: flex; align-items: center; gap: 10px;
  margin-bottom: 10px;
}
.prompt-section-header h3 {
  margin: 0; font-size: 14px; font-weight: 500; flex: 1;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color: var(--accent);
}
.prompt-section-header h3 strong { font-weight: 700; }
.prompt-copy {
  background: var(--accent); color: #fff; border-color: var(--accent);
  font-weight: 600; font-size: 13px;
}
.prompt-copy:hover { filter: brightness(0.95); }
.prompt-copy-ok { color: #0d6e2a; font-weight: 500; }
.mark-done-ok   { color: #0d6e2a; font-weight: 500; }

.prompt-text {
  width: 100%; min-height: 280px;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px; line-height: 1.5;
  padding: 12px; border: 1px solid var(--border); border-radius: 8px;
  resize: vertical; outline: none; background: #fbfbfb;
}
.prompt-text:focus { border-color: var(--muted); }

.resolution-section {
  margin-top: 14px; padding-top: 14px;
  border-top: 1px dashed var(--border);
}
.resolution-section h4 {
  margin: 0 0 4px; font-size: 13px; font-weight: 600; color: #0d6e2a;
}
.resolution-section p.muted { margin: 0 0 10px; font-size: 12px; }
.resolution-label {
  display: flex; flex-direction: column; gap: 6px;
  font-size: 13px; color: var(--muted);
}
.resolution-notes {
  width: 100%; padding: 10px;
  font: inherit; color: inherit;
  border: 1px solid var(--border); border-radius: 8px; outline: none;
  resize: vertical;
}
.resolution-notes:focus { border-color: var(--muted); }
.resolution-actions {
  display: flex; align-items: center; gap: 12px;
  margin-top: 10px;
}
.resolution-error { margin: 0; }
.mark-done {
  background: #0d6e2a; color: #fff; border-color: #0d6e2a;
  font-weight: 600; font-size: 13px;
}
.mark-done:hover:not(:disabled) { filter: brightness(0.95); }
.mark-done:disabled { opacity: 0.6; cursor: progress; }

/* Resolution block en el detail modal (cuando una fila esta hecha) */
.resolution-shown {
  background: #e6f9ec; border: 1px solid #b6e6c4; border-radius: 8px;
  padding: 10px 12px; margin: 12px 0;
}
.resolution-shown h4 {
  margin: 0 0 4px; font-size: 13px; color: #0d6e2a; font-weight: 600;
}
.resolution-shown p { margin: 0; font-size: 13px; white-space: pre-wrap; }
.resolution-shown .meta { color: var(--muted); font-size: 12px; margin-top: 6px; }

@media (max-width: 720px) {
  .bar { flex-direction: column; align-items: stretch; }
  thead { display: none; }
  tbody td { display: block; padding: 6px 12px; border: none; }
  tbody tr {
    display: block; border-bottom: 1px solid var(--border);
    padding: 12px 0;
  }
}
