@import url('https://fonts.googleapis.com/css2?family=Manrope:wght@400;600;700&display=swap');

:root {
    --brand: #1261d1;
    --brand-strong: #0f4fa9;
    --ring: rgba(18, 97, 209, 0.22);
    --surface: #ffffff;
    --surface-soft: #f4f8fc;
    --sidebar-bg: #0f2040;
    --sidebar-text: #c8d9f0;
    --sidebar-active: #1261d1;
    --text-main: #182235;
    --text-subtle: #54627a;
    --border: #dde5f0;
    --error: #c0392b;
    --success: #27ae60;
    --bubble-user: #dceaff;
    --bubble-agent: #edf3fa;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: 'Manrope', sans-serif;
    min-height: 100vh;
    display: flex;
    background: #f0f4fa;
    color: var(--text-main);
}

.sidebar {
    width: 230px; flex-shrink: 0;
    background: var(--sidebar-bg);
    display: flex; flex-direction: column;
    padding: 24px 0;
    position: fixed; top: 0; left: 0; bottom: 0;
}
.sidebar-logo { font-size: 1.3rem; font-weight: 700; color: #fff; padding: 0 22px 28px; border-bottom: 1px solid rgba(255,255,255,.08); }
.sidebar-nav  { flex: 1; padding: 16px 0; }
.nav-item {
    display: flex; align-items: center; gap: 10px;
    padding: 11px 22px; color: var(--sidebar-text);
    font-size: .93rem; font-weight: 600; cursor: pointer;
    border-left: 3px solid transparent;
    transition: background .15s, border-color .15s, color .15s;
}
.nav-item:hover { background: rgba(255,255,255,.06); color: #fff; }
.nav-item.active { border-left-color: var(--sidebar-active); background: rgba(18,97,209,.18); color: #fff; }
.sidebar-footer { padding: 16px 22px; border-top: 1px solid rgba(255,255,255,.08); }
.user-info { font-size: .82rem; color: var(--sidebar-text); margin-bottom: 10px; word-break: break-all; }
.btn-logout { width: 100%; border: 1px solid rgba(255,255,255,.2); background: transparent; color: var(--sidebar-text); border-radius: 8px; padding: 8px; font-size: .85rem; font-family: inherit; cursor: pointer; }
.btn-logout:hover { background: rgba(255,255,255,.08); color: #fff; }

.main { margin-left: 230px; flex: 1; display: flex; flex-direction: column; min-height: 100vh; }
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); padding: 16px 28px; font-size: 1.05rem; font-weight: 700; }
.view { display: none; padding: 28px; flex: 1; }
.view.active { display: block; }

.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 24px; box-shadow: 0 4px 16px rgba(40,72,115,.07); }
.panel + .panel { margin-top: 20px; }
h2 { font-size: 1.1rem; margin-bottom: 18px; }
h3 { font-size: .97rem; margin-bottom: 14px; color: var(--text-subtle); }

.section-divider { border: none; border-top: 1px solid var(--border); margin: 22px 0; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-grid.one-col { grid-template-columns: 1fr; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field.span-2 { grid-column: span 2; }
label { font-size: .85rem; font-weight: 600; color: var(--text-subtle); }

input, select, textarea {
    border: 1px solid #ccd8e7; border-radius: 10px;
    padding: 10px 12px; font-size: .93rem; font-family: inherit;
    outline: none; transition: border-color .2s, box-shadow .2s;
    background: var(--surface-soft);
}
input:focus, select:focus, textarea:focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--ring); background: #fff; }
input:disabled, select:disabled { opacity: .6; cursor: not-allowed; }
textarea { resize: vertical; min-height: 90px; }

.btn { border: 0; border-radius: 10px; padding: 10px 18px; font-size: .93rem; font-weight: 700; font-family: inherit; cursor: pointer; transition: opacity .2s; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary   { background: linear-gradient(160deg, var(--brand), var(--brand-strong)); color: #fff; }
.btn-danger    { background: #e74c3c; color: #fff; }
.btn-secondary { background: var(--surface-soft); border: 1px solid var(--border); color: var(--text-main); }
.btn-sm { padding: 6px 12px; font-size: .83rem; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th { text-align: left; padding: 10px 14px; background: var(--surface-soft); border-bottom: 1px solid var(--border); font-size: .82rem; color: var(--text-subtle); font-weight: 700; text-transform: uppercase; letter-spacing: .04em; }
td { padding: 11px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: #f8fbff; }

.badge { display: inline-block; padding: 2px 9px; border-radius: 20px; font-size: .78rem; font-weight: 700; }
.badge-superadmin { background: #fde8e8; color: #c0392b; }
.badge-admin      { background: #e8f0fd; color: #1261d1; }
.badge-user       { background: #e8fdf0; color: #1a7a40; }
.badge-erp        { background: #f0e8fd; color: #6b21a8; }
.badge-active     { background: #e8fdf0; color: #1a7a40; }
.badge-inactive   { background: #f5f5f5; color: #888; }
.badge-role-0 { background: #f0e8fd; color: #6b21a8; }
.badge-role-1 { background: #e8f0fd; color: #1261d1; }
.badge-role-2 { background: #e8fdf0; color: #1a7a40; }
.badge-role-3 { background: #fdf3e8; color: #b45309; }
.badge-role-4 { background: #e8faf9; color: #0f766e; }
.badge-role-5 { background: #fde8f4; color: #9d174d; }

.erp-display { padding: 10px 12px; background: var(--surface-soft); border: 1px solid #ccd8e7; border-radius: 10px; font-size: .93rem; }

.feedback { padding: 10px 14px; border-radius: 10px; font-size: .88rem; margin-top: 12px; display: none; }
.feedback.ok  { background: #e8fdf0; color: #1a7a40; display: block; }
.feedback.err { background: #fde8e8; color: var(--error); display: block; }

/* ── Field editor rows (ERP type definition) ── */
.field-def-row {
    display: grid;
    grid-template-columns: 1fr 1fr 110px auto 32px;
    gap: 8px;
    align-items: center;
    margin-bottom: 8px;
}
.field-def-row input, .field-def-row select { padding: 8px 10px; font-size: .85rem; }
.req-label { display: flex; align-items: center; gap: 5px; font-size: .82rem; font-weight: 600; color: var(--text-subtle); white-space: nowrap; }
.req-label input[type=checkbox] { width: 15px; height: 15px; cursor: pointer; border: none; border-radius: 3px; background: none; box-shadow: none; padding: 0; }

/* ── Chat ── */
.chat-topbar { display: flex; justify-content: flex-end; margin-bottom: 8px; }
.chat-wrap { display: grid; grid-template-rows: 1fr auto; height: calc(100vh - 155px); border-radius: 16px; overflow: hidden; border: 1px solid var(--border); background: var(--surface); box-shadow: 0 4px 16px rgba(40,72,115,.07); }
.debug-open .chat-wrap { height: calc(100vh - 155px - 210px); }
.debug-panel { margin-top: 10px; background: #0d1117; color: #e6edf3; font-family: 'Courier New', monospace; font-size: .8rem; border-radius: 12px; padding: 14px 16px; max-height: 200px; overflow-y: auto; border: 1px solid #30363d; }
.debug-panel-title { font-size: .75rem; font-weight: 700; color: #8b949e; text-transform: uppercase; letter-spacing: .05em; margin-bottom: 10px; }
.debug-content { display: flex; flex-direction: column; gap: 5px; }
.debug-hint { color: #8b949e; }
.debug-row { display: flex; gap: 12px; align-items: flex-start; }
.debug-key { color: #79c0ff; min-width: 140px; flex-shrink: 0; }
.debug-val { color: #aff5b4; }
.debug-tag { background: #1f2937; border: 1px solid #30363d; border-radius: 4px; padding: 1px 6px; color: #d2a8ff; font-size: .75rem; }
.chat-log  { padding: 20px; overflow-y: auto; display: flex; flex-direction: column; gap: 10px; background: linear-gradient(to bottom, #fff, #f8fbff); }
.message   { max-width: 80%; padding: 10px 14px; border-radius: 14px; line-height: 1.5; font-size: .95rem; animation: rise .2s ease-out; }
.message.user  { align-self: flex-end; background: var(--bubble-user); white-space: pre-wrap; }
.message.agent { align-self: flex-start; background: var(--bubble-agent); }
.message.agent p { margin: 0 0 .5em; }
.message.agent p:last-child { margin-bottom: 0; }
.message.agent strong { font-weight: 700; }
.message.agent em { font-style: italic; }
.message.agent ul, .message.agent ol { margin: .4em 0 .4em 1.4em; }
.message.agent li { margin-bottom: .2em; }
.message.agent h1,.message.agent h2,.message.agent h3 { font-weight: 700; margin: .6em 0 .3em; line-height: 1.2; }
.message.agent h1 { font-size: 1.1em; }
.message.agent h2 { font-size: 1.05em; }
.message.agent h3 { font-size: 1em; }
.message.agent code { font-family: 'Courier New', monospace; font-size: .88em; background: rgba(0,0,0,.06); padding: .1em .35em; border-radius: 4px; }
.message.agent pre { background: rgba(0,0,0,.06); border-radius: 8px; padding: 10px 12px; overflow-x: auto; margin: .5em 0; }
.message.agent:last-child { margin-bottom: 0; }
.message.agent pre code { background: none; padding: 0; font-size: .85em; }
.message.agent blockquote { border-left: 3px solid var(--brand); padding-left: 10px; color: var(--text-subtle); margin: .4em 0; }
.message.agent hr { border: none; border-top: 1px solid var(--border); margin: .5em 0; }
.chat-composer { padding: 14px 16px; border-top: 1px solid var(--border); display: flex; gap: 10px; background: var(--surface); }
.chat-composer input { flex: 1; border-radius: 12px; padding: 11px 14px; }
.chat-composer button { border: 0; border-radius: 12px; padding: 11px 20px; background: linear-gradient(160deg, var(--brand), var(--brand-strong)); color: #fff; font-weight: 700; font-family: inherit; cursor: pointer; }
.chat-composer button:disabled { opacity: .5; cursor: not-allowed; }

/* ── Modal ── */
.modal-overlay { display: none; position: fixed; inset: 0; background: rgba(10,20,40,.45); z-index: 100; place-items: center; }
.modal-overlay.open { display: grid; }
.modal { background: var(--surface); border-radius: 18px; padding: 28px; width: min(520px, 92vw); box-shadow: 0 24px 60px rgba(10,20,40,.22); animation: enter .25s ease-out; max-height: 90vh; overflow-y: auto; }
.modal-wide { width: min(700px, 92vw); }
.modal h2 { margin-bottom: 20px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }

/* ── WhatsApp ── */
.wa-panel { display: flex; align-items: flex-start; gap: 24px; flex-wrap: wrap; }
.wa-status-row { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; }
.wa-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.wa-dot.connected    { background: var(--success); }
.wa-dot.connecting   { background: #f39c12; animation: pulse 1.2s infinite; }
.wa-dot.disconnected { background: #aab4c4; }
.wa-qr-wrap { border: 1px solid var(--border); border-radius: 12px; padding: 12px; background: var(--surface-soft); text-align: center; }
.wa-qr-wrap img { width: 200px; height: 200px; display: block; }
.wa-qr-wrap p   { font-size: .8rem; color: var(--text-subtle); margin-top: 8px; }
.wa-actions { display: flex; gap: 8px; flex-wrap: wrap; }

@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .3; } }
@keyframes enter { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
@keyframes rise  { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }

/* ── Metrics ── */
.metric-card { background: var(--surface-soft); border: 1px solid var(--border); border-radius: 12px; padding: 18px 20px; text-align: center; }
.metric-value { font-size: 1.8rem; font-weight: 700; color: var(--brand); line-height: 1.2; }
.metric-label { font-size: .82rem; color: var(--text-subtle); margin-top: 4px; font-weight: 600; }

@media (max-width: 768px) {
    .sidebar { display: none; }
    .main { margin-left: 0; }
    .form-grid { grid-template-columns: 1fr; }
    .field.span-2 { grid-column: span 1; }
    .field-def-row { grid-template-columns: 1fr 1fr; }
    .field-def-row .et-ftype, .field-def-row .req-label { display: none; }
}
