/* EZKLA: estilo "framed-sheet" (tokens de draw.mjs --app, seed 798161136). Sin sombras en reposo;
   el acento solo va en los datos y el foco; el botón primario es casi negro. */
@font-face { font-family: "IBM Plex Sans"; font-weight: 400; font-style: normal; font-display: swap; src: url("fonts/ibm-plex-sans-latin-400.woff2") format("woff2"); }
@font-face { font-family: "IBM Plex Sans"; font-weight: 500; font-style: normal; font-display: swap; src: url("fonts/ibm-plex-sans-latin-500.woff2") format("woff2"); }
@font-face { font-family: "IBM Plex Sans"; font-weight: 600; font-style: normal; font-display: swap; src: url("fonts/ibm-plex-sans-latin-600.woff2") format("woff2"); }

:root {
  color-scheme: light;
  --ground: #f9fbfc;
  --surface: #ffffff;
  --raised: #f4f6f8;
  --active: #ebedee;
  --border: #e8eaec;
  --rule: #f0f1f2;
  --control: #cdced1;
  --text: #131416;
  --muted: #5d5f60;
  --faint: #6b6b6b;
  --primary: #1d1f20;
  --primary-ink: #ffffff;
  --accent: #1b6c80;
  --positive: #1e7b4f;
  --negative: #c4242a;
  --pill-positive-bg: #dcfae5; --pill-positive-ink: #04250e;
  --pill-negative-bg: #ffedea; --pill-negative-ink: #831c1e;
  --pill-warning-bg: #fffbeb; --pill-warning-ink: #92400e;
  --pill-neutral-bg: #ebedee; --pill-neutral-ink: #353736;
  --radius-card: 8px;
  --radius-control: 6px;
  --gap: 24px;
  --control-height: 40px;
  --fs-title: 22px;
  --fs-section: 16px;
  --fs-kpi: 28px;
  --fs-body: 14px;
  --fs-small: 12px;
  font-family: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: var(--fs-body);
  line-height: 1.5;
  color: var(--text);
  background: var(--ground);
}

* { box-sizing: border-box; }
body { margin: 0; min-width: 280px; }
a { color: var(--text); text-underline-offset: 0.2em; }
button, input, textarea { font: inherit; }
button, a, input, textarea { -webkit-tap-highlight-color: transparent; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 8px; font-size: var(--fs-title); line-height: 1.25; font-weight: 600; }
h2 { margin-bottom: 8px; font-size: var(--fs-section); line-height: 1.4; font-weight: 600; }
.num { font-variant-numeric: tabular-nums; }
.brand { display: flex; align-items: center; gap: 8px; margin-bottom: 20px; color: var(--text); font-size: var(--fs-section); font-weight: 600; }
.brand-link { margin: 0; text-decoration: none; }
.brand-mark { flex: none; width: 24px; height: 24px; color: var(--accent); }
.intro { margin-bottom: 24px; color: var(--muted); max-width: 60ch; }
.muted { color: var(--muted); }

/* Entrada y error */
.entry-page { display: grid; min-height: 100svh; place-items: center; padding: 40px 16px; }
.entry-panel { width: min(100%, 360px); }
.entry-panel h1 { font-size: 26px; }

/* Formularios */
.form-stack { display: grid; gap: 20px; }
.field { display: grid; gap: 6px; }
label { font-weight: 500; font-size: var(--fs-body); }
input, textarea { width: 100%; min-height: var(--control-height); padding: 8px 12px; color: var(--text); background: var(--surface); border: 1px solid var(--control); border-radius: var(--radius-control); }
input::placeholder, textarea::placeholder { color: var(--faint); }
input:hover, textarea:hover { border-color: var(--muted); }
input[aria-invalid="true"] { border-color: var(--negative); }
.field-help, .field-error { margin: 0; font-size: var(--fs-small); line-height: 1.5; }
.field-help { color: var(--muted); }
.field-error { color: var(--negative); font-weight: 500; }
.button { display: inline-flex; align-items: center; justify-content: center; min-height: var(--control-height); padding: 8px 16px; border: 1px solid var(--control); border-radius: var(--radius-control); color: var(--text); background: var(--surface); font-size: var(--fs-body); font-weight: 500; line-height: 1.4; text-align: center; text-decoration: none; cursor: pointer; }
.button:hover { background: var(--raised); }
.button-primary { color: var(--primary-ink); background: var(--primary); border-color: var(--primary); }
.button-primary:hover { background: #3a3d3f; border-color: #3a3d3f; }
.button-full { width: 100%; }
.button-quiet { color: var(--text); background: var(--surface); border-color: var(--control); }
.entry-note { margin: 24px 0 0; color: var(--muted); font-size: var(--fs-small); line-height: 1.6; }
.notice { padding: 12px 16px; margin-bottom: 24px; border-radius: var(--radius-control); font-size: var(--fs-body); background: var(--pill-neutral-bg); color: var(--pill-neutral-ink); }
.notice p { margin-bottom: 0; }
.notice-error { color: var(--pill-negative-ink); background: var(--pill-negative-bg); }
.notice-success { color: var(--pill-positive-ink); background: var(--pill-positive-bg); }

/* Estructura de página */
/* Marco tintado + hoja blanca: la barra vive en el marco; el contenido, en la hoja a 8px de los bordes. */
.frame { display: flex; flex-direction: column; min-height: 100svh; padding-bottom: 8px; }
.topbar { display: flex; align-items: center; gap: 16px; height: 48px; padding: 0 16px; }
.topbar .brand { margin-bottom: 0; }
.topbar form { margin: 0; }
.topbar .button { min-height: 32px; padding: 4px 12px; }
.topbar-user { color: var(--muted); }
.topnav { display: flex; gap: 2px; margin-right: auto; }
.topnav a, .nav-menu a { display: flex; align-items: center; min-height: 32px; padding: 0 12px; border-radius: var(--radius-control); color: var(--muted); font-weight: 500; text-decoration: none; }
.topnav a:hover, .nav-menu a:hover { color: var(--text); background: var(--raised); }
.topnav a[aria-current], .nav-menu a[aria-current] { color: var(--text); background: var(--active); }
.nav-menu { display: none; position: relative; margin-right: auto; }
.nav-menu summary { display: flex; align-items: center; min-height: 44px; padding: 0 12px; border-radius: var(--radius-control); font-weight: 500; cursor: pointer; }
.nav-menu nav { position: absolute; top: 100%; left: 0; z-index: 10; display: grid; min-width: 200px; padding: 4px; border: 1px solid var(--border); border-radius: var(--radius-card); background: var(--surface); }
.nav-menu nav a { min-height: 44px; }
.sheet { flex: 1; margin: 0 8px; padding: 32px 24px 64px; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; }
.sheet > * { width: 100%; max-width: 1080px; margin-left: auto; margin-right: auto; }
.text-link { font-size: var(--fs-body); font-weight: 500; }
.home { max-width: 720px; }
.destinations { margin: 0 0 32px; padding: 0; list-style: none; border-top: 1px solid var(--rule); }
.destinations li { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 16px 0; border-bottom: 1px solid var(--rule); }
.destinations h2 { margin-bottom: 2px; }
.destinations p { margin: 0; color: var(--muted); }
.destinations .button { flex: none; }
.account-detail { margin: 0; }
.account-detail div { display: flex; justify-content: space-between; gap: 16px; align-items: center; min-height: 32px; }
dt { color: var(--muted); }
dd { margin: 0; font-weight: 500; text-align: right; }
.page-heading { margin-bottom: 24px; }
.page-heading .intro { margin-bottom: 0; }
.users-layout { display: grid; grid-template-columns: minmax(0, 1fr) 384px; gap: 32px; align-items: start; }
.create-panel { order: 2; padding: 24px; border: 1px solid var(--border); border-radius: var(--radius-card); background: var(--surface); }
.users-panel { order: 1; min-width: 0; }
.section-intro { margin-bottom: 16px; color: var(--muted); font-size: var(--fs-body); }
.table-scroll { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; text-align: left; font-size: var(--fs-body); font-variant-numeric: tabular-nums; }
thead th { color: var(--muted); font-size: var(--fs-small); font-weight: 400; }
th, td { padding: 12px; border-bottom: 1px solid var(--rule); vertical-align: top; }
thead th { border-bottom-color: var(--border); }
th:first-child { padding-left: 0; }
td:last-child, th:last-child { padding-right: 0; }
tbody th { font-weight: 500; overflow-wrap: anywhere; }
.role-label { display: inline-block; color: var(--muted); }
.users-table td, .users-table th { vertical-align: middle; }
.cell-user .sub { display: block; font-weight: 400; color: var(--muted); font-size: var(--fs-small); }
.cell-actions { white-space: nowrap; }
.users-table .row-actions { flex-wrap: nowrap; justify-content: flex-end; }
.more-actions { position: relative; }
.more-actions summary { list-style: none; }
.more-actions summary::-webkit-details-marker { display: none; }
.more-panel { position: absolute; right: 0; top: 100%; z-index: 10; display: grid; gap: 8px; width: 280px; margin-top: 4px; padding: 12px; border: 1px solid var(--border); border-radius: var(--radius-card); background: var(--surface); }
.more-panel .inline-form { display: flex; }
.more-panel .inline-form .input-small { flex: 1; width: auto; min-width: 0; }
.more-panel .inline-form > .button { flex: 1; }
.empty-state { padding: 24px 0; border-top: 1px solid var(--border); color: var(--muted); }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

/* Pills de estado y control segmentado (los usan Estadísticas y Usuarios) */
.pill { display: inline-flex; flex: none; align-items: center; height: 22px; padding: 0 8px; border-radius: 999px; font-size: var(--fs-small); font-weight: 500; white-space: nowrap; background: var(--pill-neutral-bg); color: var(--pill-neutral-ink); }
.pill-positive { background: var(--pill-positive-bg); color: var(--pill-positive-ink); }
.pill-negative { background: var(--pill-negative-bg); color: var(--pill-negative-ink); }
.pill-warning { background: var(--pill-warning-bg); color: var(--pill-warning-ink); }
.segmented { display: inline-flex; flex-wrap: wrap; width: max-content; max-width: 100%; gap: 2px; padding: 2px; border-radius: 8px; background: var(--raised); }
.segmented a { display: inline-flex; align-items: center; min-height: 32px; padding: 0 12px; border-radius: var(--radius-control); color: var(--muted); font-weight: 500; text-decoration: none; }
.segmented a[aria-current] { background: var(--active); color: var(--text); }

@media (max-width: 1100px) { .users-layout { grid-template-columns: 1fr; gap: 32px; } }
@media (max-width: 760px) {
  .topnav, .topbar-user { display: none; }
  .nav-menu { display: block; }
  .sheet { padding: 24px 16px 40px; }
}

@media (max-width: 440px) {
  .entry-page { padding: 20px 16px; }
  .create-panel { padding: 20px 16px; }
  .destinations li { flex-direction: column; align-items: stretch; }
}

/* Formularios y acciones (sustituyen a los estilos inline de las plantillas) */
.optional { color: var(--muted); font-weight: 400; }
.field-check { display: flex; align-items: center; gap: 8px; }
.field-check input { width: auto; min-height: 0; margin: 0; }
.field-check label { font-weight: 400; }
.form-actions { display: flex; flex-wrap: wrap; gap: 12px; }
.row-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.inline-form { display: inline-flex; align-items: center; gap: 4px; margin: 0; }
.button-small { min-height: 32px; padding: 4px 10px; font-size: var(--fs-small); }
.input-small { width: 9rem; min-height: 32px; padding: 4px 8px; font-size: var(--fs-small); }
.settings { max-width: 880px; }
.settings-form { display: grid; gap: 32px; }
.settings-section { display: grid; grid-template-columns: 240px minmax(0, 1fr); gap: 24px; padding-top: 24px; border-top: 1px solid var(--rule); }
.settings-label h2 { margin-bottom: 4px; }
.settings-label p { margin: 0; color: var(--muted); }
.settings-panel { padding: 20px; border: 1px solid var(--border); border-radius: var(--radius-card); background: var(--surface); }
.danger-zone { display: grid; gap: 4px; padding-top: 16px; border-top: 1px solid var(--rule); }
.danger-zone label { color: var(--negative); font-weight: 500; }
.danger-zone input { accent-color: var(--negative); }
@media (max-width: 760px) { .settings-section { grid-template-columns: 1fr; gap: 12px; } }
.chat-settings { display: grid; gap: 16px; margin: 0; min-width: 0; }
.chat-state { margin: 0; color: var(--muted); font-size: var(--fs-body); }

/* Chat: ocupa la altura de la ventana; los mensajes se desplazan y el compositor queda abajo. */
.frame-fill { height: 100dvh; min-height: 0; }
.sheet-chat { display: flex; min-height: 0; padding: 0; overflow: hidden; }
.sheet-chat > .chat-layout { max-width: none; margin: 0; }
.chat-layout { display: grid; flex: 1; grid-template-columns: minmax(0, 1fr) 320px; grid-template-rows: minmax(0, 1fr); min-height: 0; }
.chat-help { order: 2; min-height: 0; padding: 20px; border-left: 1px solid var(--border); font-size: var(--fs-body); overflow: auto; overflow-wrap: anywhere; }
.chat-help-panel > summary { margin-bottom: 8px; font-size: var(--fs-section); font-weight: 600; line-height: 1.4; cursor: pointer; }
.chat-help-panel:not([open]) > summary { margin-bottom: 0; }
.chat-help h3 { margin: 16px 0 6px; font-size: var(--fs-body); font-weight: 600; }
.chat-help p { margin: 0 0 8px; color: var(--muted); }
.chat-help ul, .chat-help ol { margin: 0; padding-left: 20px; }
.chat-help li { margin-bottom: 4px; }
.chat-help-list { color: var(--text); font-weight: 500; }
.chat-help-example { margin: 16px 0; padding: 12px 14px; border-radius: var(--radius-control); background: var(--raised); }
.chat-help-example h3 { margin-top: 0; }
.chat-help-example p { margin: 0; color: var(--text); font-style: italic; }
.chat-help-example .chat-help-note { margin-top: 8px; font-style: normal; font-weight: 500; }
.chat-help-more summary { cursor: pointer; color: var(--text); font-weight: 500; }
.chat-help-docs dt { margin-top: 10px; color: var(--text); font-weight: 500; }
.chat-help-docs dd { margin: 2px 0 0; color: var(--muted); text-align: left; font-weight: 400; }
.chat-panel { display: grid; grid-template-rows: minmax(0, 1fr) auto auto; gap: 12px; min-width: 0; min-height: 0; padding: 16px 24px 20px; }
.chat-stage { min-height: 0; overflow-y: auto; }
.chat-messages, .chat-empty, .chat-error, .chat-form { width: 100%; max-width: 760px; margin-left: auto; margin-right: auto; }
.chat-messages { display: flex; flex-direction: column; gap: 12px; margin-top: 0; margin-bottom: 0; padding: 0; list-style: none; }
.chat-message { max-width: 85%; padding: 8px 14px; border-radius: 12px; white-space: pre-wrap; overflow-wrap: anywhere; }
.chat-message-user { align-self: flex-end; background: var(--active); }
.chat-message-assistant { align-self: flex-start; background: var(--surface); border: 1px solid var(--border); }
/* Espera del asistente: tres puntos (SVG de datos, permitido por img-src) que laten; sin movimiento si se prefiere. */
.chat-message.is-pending { font-size: 0; }
.chat-message.is-pending::before { content: ""; display: block; width: 28px; height: 12px; background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 28 12'%3E%3Ccircle cx='4' cy='6' r='3' fill='%235d5f60'/%3E%3Ccircle cx='14' cy='6' r='3' fill='%235d5f60'/%3E%3Ccircle cx='24' cy='6' r='3' fill='%235d5f60'/%3E%3C/svg%3E") center / contain no-repeat; animation: pending-pulse 1.2s ease-in-out infinite; }
@keyframes pending-pulse { 50% { opacity: 0.35; } }
@media (prefers-reduced-motion: reduce) { .chat-message.is-pending::before { animation: none; } }
/* Estado vacío: solo cuando el historial ya se intentó cargar (data-ready) y no hay mensajes. */
.chat-empty { display: none; padding-top: 8vh; }
.chat-stage:has(#messages[data-ready]:empty) .chat-empty { display: block; }
.chat-empty p { max-width: 56ch; color: var(--muted); }
.contract-types { margin: 24px 0 0; padding: 0; list-style: none; border-top: 1px solid var(--rule); }
.contract-types li { display: grid; gap: 2px; padding: 12px 0; border-bottom: 1px solid var(--rule); }
.contract-types span { color: var(--muted); }
.chat-error { margin-top: 0; margin-bottom: 0; }
/* Compositor: una sola pieza enmarcada con los archivos encima; el foco se ve en el marco. */
.chat-form { display: grid; gap: 8px; padding: 8px; border: 1px solid var(--control); border-radius: var(--radius-card); background: var(--surface); }
.chat-form:focus-within { border-color: var(--accent); outline: 1px solid var(--accent); }
.chat-compose { display: flex; gap: 8px; align-items: flex-end; }
.chat-attach { cursor: pointer; }
.chat-files { display: flex; flex-wrap: wrap; gap: 6px; margin: 0; padding: 0; list-style: none; }
.chat-files li { display: inline-flex; gap: 6px; align-items: center; padding: 2px 4px 2px 10px; font-size: var(--fs-small); border-radius: 999px; background: var(--pill-neutral-bg); color: var(--pill-neutral-ink); overflow-wrap: anywhere; }
.chat-files button { padding: 0 6px; font: inherit; line-height: 1.4; color: var(--muted); border: 0; border-radius: 999px; background: transparent; cursor: pointer; }
.chat-form textarea { flex: 1; min-height: 44px; max-height: 160px; border: 0; resize: none; }
.chat-form textarea:focus-visible { outline: none; }
/* Móvil: una columna, ayuda plegable arriba, compositor abajo y controles de al menos 44 px. */
@media (max-width: 800px) {
  .chat-layout { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr); }
  .chat-help { order: 0; max-height: 50dvh; padding: 12px 16px; border-left: 0; border-bottom: 1px solid var(--border); }
  .chat-panel { padding: 12px 16px 16px; }
  .chat-message { max-width: 92%; }
  .chat-form { padding: 0; border: 0; }
  .chat-form:focus-within { outline: none; }
  .chat-compose { flex-wrap: wrap; }
  .chat-form textarea { flex: 1 1 100%; border: 1px solid var(--control); border-radius: var(--radius-control); }
  .chat-form textarea:focus-visible { outline: 2px solid var(--accent); }
  .chat-compose .chat-attach, .chat-compose #chat-send { flex: 1 1 0; }
  .chat-files button { min-width: 44px; min-height: 36px; }
}
/* Táctil: 44 px de objetivo y 16 px en los campos para que iOS no haga zoom al enfocar. */
@media (pointer: coarse) {
  .button, input, textarea { min-height: 44px; font-size: 16px; }
  .button-small, .input-small { min-height: 44px; font-size: var(--fs-body); }
}

[hidden] { display: none !important; }

/* Estadísticas */
.period-filter { margin-bottom: 24px; }
.kpis { display: grid; grid-template-columns: 1.4fr repeat(3, 1fr); margin: 0 0 32px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-card); }
.kpi { padding: 16px 20px; }
.kpi + .kpi { border-left: 1px solid var(--border); }
.kpi dt { font-size: var(--fs-small); }
.kpi dd { margin-top: 4px; font-size: 22px; font-weight: 400; line-height: 1.2; text-align: left; font-variant-numeric: tabular-nums; }
.kpi-total dd { font-size: var(--fs-kpi); }
.stats-section { margin-bottom: 32px; }
.user-picker { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; margin-bottom: 24px; }
.user-picker label { font-weight: 500; }
.user-picker select { min-width: 16rem; max-width: 100%; min-height: var(--control-height); padding: 8px 12px; color: var(--text); background: var(--surface); border: 1px solid var(--control); border-radius: var(--radius-control); font: inherit; }
.user-picker select:hover { border-color: var(--muted); }
.type-bars { display: grid; gap: 12px; margin: 0; padding: 0; list-style: none; }
.type-bars li { display: grid; grid-template-columns: minmax(150px, 240px) 1fr 3rem; align-items: center; gap: 12px; }
.type-bars progress { -webkit-appearance: none; appearance: none; width: 100%; height: 8px; border: 0; border-radius: 4px; background: var(--active); overflow: hidden; }
.type-bars progress::-webkit-progress-bar { background: var(--active); }
.type-bars progress::-webkit-progress-value { background: var(--accent); border-radius: 4px; }
.type-bars progress::-moz-progress-bar { background: var(--accent); border-radius: 4px; }
.type-count { font-weight: 500; text-align: right; font-variant-numeric: tabular-nums; }
td.num, th.num { text-align: right; }
/* En pantallas estrechas la tabla se desplaza dentro de su caja y la primera columna se queda fija. */
.table-scroll th:first-child { position: sticky; left: 0; background: var(--surface); white-space: nowrap; padding-right: 16px; }
td.zero { color: var(--faint); }
@media (max-width: 760px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .kpi:nth-child(odd) { border-left: 0; }
  .kpi:nth-child(n + 3) { border-top: 1px solid var(--border); }
}
@media (max-width: 440px) { .type-bars li { grid-template-columns: 1fr 3rem; } .type-bars progress { grid-column: 1 / -1; grid-row: 2; } }

/* Usuarios en teléfono: cada fila pasa a dos líneas (identidad y acciones; luego los estados) con el mismo marcado. */
@media (max-width: 640px) {
  .users-table thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0, 0, 0, 0); }
  .users-table, .users-table tbody { display: block; }
  .users-table tr { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px; padding: 12px 0; border-bottom: 1px solid var(--rule); }
  .users-table th, .users-table td { padding: 0; border: 0; }
  .users-table .cell-user { order: 1; flex: 1 1 0; min-width: 0; }
  .users-table .cell-actions { order: 2; flex: none; }
  .users-table tr::after { content: ""; order: 3; flex-basis: 100%; height: 0; }
  .users-table .cell-account, .users-table .cell-role, .users-table .cell-chat { order: 4; margin-top: 4px; }
  .more-panel { width: min(280px, calc(100vw - 48px)); }
}
