:root {
    --bg: #f4f6f9;
    --surface: #ffffff;
    --border: #e2e8f0;
    --text: #1e293b;
    --text-muted: #64748b;
    --accent: #2563eb;
    --accent-hover: #1d4ed8;
    --success: #16a34a;
    --danger: #dc2626;
    --radius: 10px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: 'Segoe UI', system-ui, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
}

.hidden { display: none !important; }

.login-screen {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
}

.login-box {
    background: var(--surface);
    padding: 2rem;
    border-radius: var(--radius);
    box-shadow: 0 4px 24px rgba(0,0,0,.08);
    width: 100%;
    max-width: 380px;
    border: 1px solid var(--border);
}

.login-box h1 { font-size: 1.35rem; margin-bottom: .25rem; }
.subtitle { color: var(--text-muted); margin-bottom: 1.25rem; font-size: .9rem; }

.form-group { margin-bottom: .85rem; }
.form-group label { display: block; margin-bottom: .35rem; font-size: .85rem; color: var(--text-muted); }

input {
    width: 100%;
    padding: .6rem .8rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: .95rem;
}

input:focus { outline: none; border-color: var(--accent); }

.btn {
    padding: .55rem 1rem;
    border: none;
    border-radius: 6px;
    font-size: .9rem;
    font-weight: 500;
    cursor: pointer;
}

.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-secondary { background: var(--bg); border: 1px solid var(--border); color: var(--text); }
.btn-sm { padding: .35rem .7rem; font-size: .8rem; }
.btn-block { width: 100%; }

.error-msg { color: var(--danger); font-size: .85rem; margin-top: .5rem; }

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.5rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.header h1 { font-size: 1.15rem; }

.content { padding: 1.5rem; max-width: 1280px; margin: 0 auto; }

.ai-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: linear-gradient(135deg, #2563eb, #3b82f6);
    color: #fff;
    padding: 1rem 1.25rem;
    border-radius: var(--radius);
    margin-bottom: 1.25rem;
}

.ai-banner.off { background: linear-gradient(135deg, #64748b, #94a3b8); }

.toggle {
    position: relative;
    width: 48px;
    height: 26px;
    display: inline-block;
}

.toggle input { opacity: 0; width: 0; height: 0; }

.toggle-slider {
    position: absolute;
    inset: 0;
    background: rgba(255,255,255,.3);
    border-radius: 26px;
    cursor: pointer;
    transition: .2s;
}

.toggle-slider:before {
    content: '';
    position: absolute;
    width: 20px;
    height: 20px;
    left: 3px;
    bottom: 3px;
    background: #fff;
    border-radius: 50%;
    transition: .2s;
}

.toggle input:checked + .toggle-slider { background: rgba(255,255,255,.5); }
.toggle input:checked + .toggle-slider:before { transform: translateX(22px); }

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
}

table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { padding: .75rem 1rem; text-align: left; border-bottom: 1px solid var(--border); }
th { background: var(--bg); color: var(--text-muted); font-weight: 500; font-size: .8rem; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: #f8fafc; }

tr.row-unanswered td { background: #fef2f2; }
tr.row-unanswered:hover td { background: #fee2e2; }

.badge {
    display: inline-block;
    padding: .15rem .5rem;
    border-radius: 4px;
    font-size: .75rem;
    font-weight: 500;
    background: var(--bg);
    color: var(--text-muted);
}

.badge-active { background: #dcfce7; color: var(--success); }

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.4);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding: 1rem;
}

.modal {
    background: var(--surface);
    border-radius: var(--radius);
    width: 100%;
    max-width: 600px;
    max-height: 85vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 8px 32px rgba(0,0,0,.15);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--border);
}

.modal-body { flex: 1; overflow-y: auto; padding: 1rem; }

.close-btn {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--text-muted);
}

.chat-messages {
    display: flex;
    flex-direction: column;
    gap: .5rem;
    min-height: 250px;
    max-height: 400px;
    overflow-y: auto;
    padding: .5rem;
    background: var(--bg);
    border-radius: 8px;
}

.message {
    max-width: 80%;
    padding: .55rem .8rem;
    border-radius: 12px;
    font-size: .9rem;
}

.message-in { background: var(--surface); border: 1px solid var(--border); align-self: flex-start; }
.message-out { background: var(--accent); color: #fff; align-self: flex-end; }

.chat-input {
    display: flex;
    gap: .5rem;
    padding: .75rem 1rem;
    border-top: 1px solid var(--border);
}

.chat-input input { flex: 1; }

.toast {
    position: fixed;
    bottom: 1rem;
    right: 1rem;
    padding: .65rem 1rem;
    background: var(--text);
    color: #fff;
    border-radius: 8px;
    z-index: 200;
    max-width: min(420px, calc(100vw - 2rem));
}

.toast-error { background: #991b1b; }

.tabs {
    display: flex;
    gap: .25rem;
    padding: 0 1.5rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}

.tab {
    padding: .65rem 1rem;
    background: transparent;
    border: none;
    border-bottom: 2px solid transparent;
    color: var(--text-muted);
    cursor: pointer;
    font-size: .9rem;
}

.tab.active {
    color: var(--accent);
    border-bottom-color: var(--accent);
    font-weight: 500;
}

.tab-panel { display: none; }
.tab-panel.active { display: block; }

.panel-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
    flex-wrap: wrap;
    gap: .5rem;
}

.panel-bar h2 { font-size: 1.1rem; }

.products-section {
    margin-bottom: 1.25rem;
}

.products-section-title {
    font-size: .95rem;
    font-weight: 600;
    margin-bottom: .65rem;
    color: var(--text);
}

.products-section-title-muted {
    color: var(--text-muted);
}

.chat-layout {
    display: grid;
    grid-template-columns: 280px 1fr;
    gap: 1rem;
    min-height: 420px;
}

.chat-list {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow-y: auto;
    max-height: 480px;
}

.chat-item {
    padding: .75rem 1rem;
    border-bottom: 1px solid var(--border);
    cursor: pointer;
}

.chat-item:hover, .chat-item.active { background: #f1f5f9; }

.chat-item.chat-unanswered {
    background: #fef2f2;
    border-left: 3px solid #f87171;
}

.chat-item.chat-unanswered:hover,
.chat-item.chat-unanswered.active {
    background: #fee2e2;
}

.chat-unread-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    background: #dc2626;
    border-radius: 50%;
    vertical-align: middle;
    margin-left: 4px;
}

.chat-item-name { font-weight: 500; font-size: .9rem; }
.chat-item-preview { font-size: .8rem; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.chat-window {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    display: flex;
    flex-direction: column;
}

.chat-header {
    padding: .75rem 1rem;
    border-bottom: 1px solid var(--border);
    font-weight: 500;
}

.chat-messages {
    flex: 1;
    min-height: 280px;
    max-height: 400px;
    overflow-y: auto;
    padding: 1rem;
    background: var(--bg);
}

.message-time { font-size: .7rem; opacity: .6; margin-top: .15rem; }

.empty-hint { padding: 1rem; color: var(--text-muted); font-size: .9rem; }

@media (max-width: 768px) {
    .chat-layout { grid-template-columns: 1fr; }
    .chat-list { max-height: 180px; }
}

/* --- Dashboard --- */

.dashboard-loading {
    padding: 2rem;
    text-align: center;
    color: var(--text-muted);
}

.kpi-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: .75rem;
    margin-bottom: 1.25rem;
}

.kpi-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem;
}

.kpi-card .kpi-label {
    font-size: .75rem;
    color: var(--text-muted);
    margin-bottom: .35rem;
}

.kpi-card .kpi-value {
    font-size: 1.35rem;
    font-weight: 600;
    line-height: 1.2;
}

.kpi-card .kpi-sub {
    font-size: .75rem;
    color: var(--text-muted);
    margin-top: .25rem;
}

.kpi-card.accent { border-color: #bfdbfe; background: #eff6ff; }
.kpi-card.success { border-color: #bbf7d0; background: #f0fdf4; }
.kpi-card.warning { border-color: #fde68a; background: #fffbeb; }
.kpi-card.danger { border-color: #fecaca; background: #fef2f2; }

.charts-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 1rem;
    margin-bottom: 1rem;
}

.charts-grid-2 {
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
}

.chart-card {
    padding: 1rem;
}

.chart-card h3,
.dashboard-table-block h3 {
    font-size: .95rem;
    margin-bottom: .75rem;
    color: var(--text);
    font-weight: 600;
}

.chart-wrap {
    position: relative;
    height: 220px;
}

.chart-wrap-tall {
    height: 280px;
}

.chart-wide {
    min-width: 0;
}

.dashboard-tables {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
    gap: 1rem;
    margin-top: 1rem;
}

.dashboard-table-block {
    padding: 0;
    overflow: hidden;
}

.dashboard-table-block h3 {
    padding: 1rem 1rem 0;
}

.dashboard-table-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: .75rem;
    padding: 1rem 1rem 0;
    flex-wrap: wrap;
}

.dashboard-table-head h3 {
    padding: 0;
    margin: 0;
}

.dashboard-table-full {
    grid-column: 1 / -1;
}

tr.row-expired td { background: #fef2f2; }
tr.row-expiring td { background: #fffbeb; }
tr.row-expired:hover td { background: #fee2e2; }
.listing-actions {
    display: flex;
    gap: .35rem;
    flex-wrap: wrap;
}

.btn-auto {
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text-muted);
}

.btn-auto.active {
    background: #dcfce7;
    border-color: #86efac;
    color: #166534;
    font-weight: 600;
}

.btn-auto:disabled {
    opacity: .45;
    cursor: not-allowed;
}

.table-scroll {
    overflow-x: auto;
}

.badge-paid { background: #dcfce7; color: var(--success); }
.badge-pending { background: #fef9c3; color: #a16207; }
.badge-cancelled { background: #fee2e2; color: var(--danger); }
.badge-success { background: #dcfce7; color: var(--success); }
.badge-negotiation { background: #e0e7ff; color: #4338ca; }
