* { box-sizing: border-box; }

html, body {
    margin: 0;
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    color: #2a2a2a;
    background: #f5f5f4;
}

body {
    display: grid;
    grid-template-columns: 260px 1fr;
    height: 100vh;
}

.hidden { display: none !important; }

/* ---------- sidebar ---------- */
.sidebar {
    background: #1f2937;
    color: #e5e7eb;
    display: flex;
    flex-direction: column;
    padding: 16px;
    gap: 12px;
}

.sidebar-header strong {
    display: block;
    font-size: 14px;
    color: #f9fafb;
}

.sidebar-header small {
    color: #9ca3af;
    font-size: 11px;
}

.nav {
    display: flex;
    flex-direction: column;
    gap: 4px;
    border-top: 1px solid #374151;
    padding-top: 12px;
}

.nav-btn {
    background: transparent;
    color: #d1d5db;
    border: none;
    padding: 10px 12px;
    text-align: left;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
}

.nav-btn:hover { background: #374151; }
.nav-btn.active { background: #2563eb; color: white; font-weight: 600; }

.btn-new {
    background: #374151;
    color: #f9fafb;
    border: 1px solid #4b5563;
    padding: 8px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
}

.btn-new:hover { background: #4b5563; }

.conv-list {
    flex: 1;
    overflow-y: auto;
    border-top: 1px solid #374151;
    border-bottom: 1px solid #374151;
    padding: 8px 0;
}

.conv-empty {
    color: #6b7280;
    font-size: 11px;
    padding: 8px;
    display: block;
}

.conv-item {
    padding: 8px;
    font-size: 12px;
    color: #d1d5db;
    cursor: pointer;
    border-radius: 4px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.conv-item.active, .conv-item:hover { background: #374151; }

.sidebar-footer {
    margin-top: auto;
    font-size: 11px;
    color: #9ca3af;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

#cost-display {
    color: #f9fafb;
    font-size: 14px;
    font-weight: 600;
}

/* ---------- main ---------- */
.main {
    display: flex;
    flex-direction: column;
    height: 100vh;
    overflow: hidden;
}

.view {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

/* ---------- HOME (dashboard) ---------- */
.home-header {
    padding: 20px 28px 8px;
    background: white;
    border-bottom: 1px solid #e5e7eb;
}

.home-header h1 {
    margin: 0;
    font-size: 22px;
    color: #1f2937;
}

.home-sub {
    margin: 4px 0 0;
    color: #6b7280;
    font-size: 13px;
}

.home-grid {
    padding: 20px 28px 40px;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 16px;
}

.loading {
    grid-column: span 4;
    text-align: center;
    color: #6b7280;
    padding: 60px;
}

/* ----- KPI card ----- */
.kpi-card {
    background: white;
    border: 1px solid #e5e7eb;
    border-radius: 10px;
    padding: 16px;
    grid-column: span 1;
    display: flex;
    flex-direction: column;
    gap: 6px;
    min-height: 130px;
    position: relative;
}

.kpi-card .kpi-title {
    color: #6b7280;
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-weight: 600;
}

.kpi-card .kpi-value {
    color: #1f2937;
    font-size: 28px;
    font-weight: 700;
    line-height: 1.1;
    margin-top: 4px;
}

.kpi-card .kpi-value.muted {
    color: #9ca3af;
    font-size: 18px;
    font-weight: 500;
}

/* ----- Chart card ----- */
.chart-card {
    background: white;
    border: 1px solid #e5e7eb;
    border-radius: 10px;
    padding: 16px;
    grid-column: span 2;
    min-height: 320px;
    display: flex;
    flex-direction: column;
    position: relative;
}

.chart-card.wide {
    grid-column: span 4;
    min-height: 420px;
}

.chart-card.wide .chart-canvas-wrap {
    min-height: 340px;
}

.chart-card .chart-title {
    color: #1f2937;
    font-size: 14px;
    font-weight: 600;
    margin-bottom: 12px;
}

.chart-card .chart-canvas-wrap {
    flex: 1;
    position: relative;
    min-height: 240px;
}

.card-actions {
    position: absolute;
    top: 14px;
    right: 14px;
}

.ask-btn {
    background: #f3f4f6;
    border: 1px solid #d1d5db;
    color: #2563eb;
    padding: 4px 10px;
    border-radius: 6px;
    font-size: 11px;
    cursor: pointer;
    font-weight: 500;
}

.ask-btn:hover { background: #eff6ff; border-color: #2563eb; }

.card-error {
    color: #991b1b;
    background: #fef2f2;
    border: 1px solid #fecaca;
    padding: 8px;
    border-radius: 6px;
    font-size: 12px;
    margin-top: 8px;
}

@media (max-width: 1100px) {
    .home-grid { grid-template-columns: repeat(2, 1fr); }
    .chart-card { grid-column: span 2; }
    .chart-card.wide { grid-column: span 2; }
    .loading { grid-column: span 2; }
}

/* ---------- CHAT ---------- */
#view-chat { padding: 0; }

.chat {
    flex: 1;
    overflow-y: auto;
    padding: 24px;
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.welcome {
    text-align: center;
    margin-top: 60px;
}

.welcome h1 {
    font-size: 24px;
    margin: 0 0 8px;
    color: #1f2937;
}

.welcome-sub {
    color: #6b7280;
    margin: 0 0 24px;
}

.suggestions {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}

.sug {
    background: white;
    border: 1px solid #e5e7eb;
    padding: 12px 16px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 13px;
    color: #1f2937;
    max-width: 540px;
    width: 100%;
    text-align: left;
}

.sug:hover {
    border-color: #2563eb;
    background: #eff6ff;
}

/* ---------- mensajes chat ---------- */
.msg { max-width: 820px; width: 100%; margin: 0 auto; }

.msg-user, .msg-bot {
    padding: 12px 16px;
    border-radius: 10px;
}

.msg-user {
    background: #2563eb;
    color: white;
    align-self: flex-end;
    max-width: 520px;
    margin-left: auto;
}

.msg-bot {
    background: white;
    border: 1px solid #e5e7eb;
}

.msg-bot pre.sql {
    background: #f3f4f6;
    border: 1px solid #e5e7eb;
    padding: 10px;
    border-radius: 6px;
    font-size: 12px;
    overflow-x: auto;
    margin: 8px 0 0;
}

.msg-bot .actions {
    margin-top: 12px;
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.msg-bot .actions button {
    background: #f3f4f6;
    border: 1px solid #d1d5db;
    padding: 4px 10px;
    border-radius: 6px;
    font-size: 12px;
    cursor: pointer;
    color: #374151;
}

.msg-bot .actions button:hover { background: #e5e7eb; }

.msg-bot .sql-block { display: none; }
.msg-bot .sql-block.open { display: block; }

.steps { color: #6b7280; font-size: 13px; font-style: italic; }
.steps .done { color: #059669; }

table.data-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 12px;
    font-size: 13px;
}

table.data-table th, table.data-table td {
    border: 1px solid #e5e7eb;
    padding: 6px 10px;
    text-align: left;
}

table.data-table th {
    background: #f9fafb;
    font-weight: 600;
}

table.data-table td.num { text-align: right; }

.chart-wrap {
    margin-top: 16px;
    background: #fafafa;
    border: 1px solid #e5e7eb;
    border-radius: 8px;
    padding: 12px;
    height: 320px;
}

.err {
    background: #fef2f2;
    border: 1px solid #fecaca;
    color: #991b1b;
    padding: 10px;
    border-radius: 6px;
    margin-top: 8px;
}

.usage-line {
    color: #9ca3af;
    font-size: 11px;
    margin-top: 8px;
}

/* ---------- input ---------- */
.input-area {
    border-top: 1px solid #e5e7eb;
    padding: 12px 24px;
    background: white;
    display: flex;
    gap: 8px;
    align-items: flex-end;
}

.input-area textarea {
    flex: 1;
    resize: none;
    border: 1px solid #d1d5db;
    border-radius: 8px;
    padding: 10px;
    font-family: inherit;
    font-size: 14px;
}

.input-area textarea:focus { outline: 2px solid #2563eb; }

.input-area button {
    background: #2563eb;
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
}

.input-area button:disabled { opacity: 0.5; cursor: not-allowed; }
