/* ===== Variables de tema ===== */
:root {
    --bg: #f5f6f8;
    --surface: #ffffff;
    --surface-2: #f0f2f5;
    --text: #1a1d21;
    --text-muted: #5a6470;
    --border: #d8dce2;
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --success: #15803d;
    --warning: #b45309;
    --error: #b91c1c;
    --danger: #dc2626;
    --info: #0369a1;
    --shadow: 0 1px 2px rgba(0,0,0,.05), 0 2px 8px rgba(0,0,0,.04);
    --radius: 8px;
}

html[data-theme="dark"] {
    --bg: #0f1115;
    --surface: #181c22;
    --surface-2: #22272e;
    --text: #e6e8eb;
    --text-muted: #9aa4b2;
    --border: #2c333c;
    --primary: #60a5fa;
    --primary-hover: #93c5fd;
    --success: #4ade80;
    --warning: #fbbf24;
    --error: #f87171;
    --danger: #f87171;
    --info: #7dd3fc;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 2px 8px rgba(0,0,0,.3);
}

/* ===== Reset y base ===== */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}
a { color: var(--primary); text-decoration: none; }
a:hover { color: var(--primary-hover); text-decoration: underline; }
hr { border: none; border-top: 1px solid var(--border); margin: 1.5rem 0; }
code { background: var(--surface-2); padding: 0.1em 0.35em; border-radius: 4px; font-size: 0.9em; }
h1, h2, h3 { margin-top: 0; }
h2 { font-size: 1.25rem; margin-bottom: 0.75rem; }
h3 { font-size: 1.05rem; margin-bottom: 0.5rem; }

.container { width: 100%; max-width: 1200px; margin: 0 auto; padding: 0 1rem; }
.main { flex: 1; padding: 2rem 1rem; display: flex; flex-direction: column; gap: 1.5rem; }

/* ===== Topbar ===== */
.topbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow);
    position: sticky;
    top: 0;
    z-index: 50;
}
.topbar-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1rem;
}
.brand { font-weight: 700; font-size: 1.1rem; color: var(--text); }
.brand:hover { text-decoration: none; }
.nav { display: flex; gap: 1rem; align-items: center; flex-wrap: wrap; }
.user-email { color: var(--text-muted); font-size: 0.85rem; }

.notif-bell {
    position: relative;
    padding: 0.2rem 0.5rem;
    font-size: 1.1rem;
    color: var(--text);
}
.notif-bell:hover { text-decoration: none; }
.notif-count {
    position: absolute;
    top: -6px;
    right: -4px;
    background: var(--danger);
    color: #fff;
    border-radius: 999px;
    padding: 0 6px;
    font-size: 0.7rem;
    min-width: 18px;
    text-align: center;
}

.inline-form { display: inline; margin: 0; }
.btn-link {
    background: none; border: none; color: var(--primary);
    cursor: pointer; padding: 0; font: inherit;
}
.btn-link:hover { color: var(--primary-hover); text-decoration: underline; }
.btn-danger-link { color: var(--danger); margin-left: 0.5rem; font-size: 0.85rem; }

.btn-icon {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.35rem 0.55rem;
    cursor: pointer;
    color: var(--text);
    line-height: 1;
}
html[data-theme="light"] .theme-icon-dark { display: none; }
html[data-theme="dark"]  .theme-icon-light { display: none; }

/* ===== Footer ===== */
.footer {
    border-top: 1px solid var(--border);
    background: var(--surface);
    padding: 1rem 0;
    color: var(--text-muted);
    text-align: center;
}

/* ===== Flashes ===== */
.flashes { display: flex; flex-direction: column; gap: 0.5rem; margin-bottom: 1rem; }
.flash {
    padding: 0.75rem 1rem;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--surface-2);
}
.flash-success { border-color: var(--success); color: var(--success); }
.flash-error   { border-color: var(--error);   color: var(--error); }
.flash-warning { border-color: var(--warning); color: var(--warning); }
.flash-info    { border-color: var(--info);    color: var(--info); }

/* ===== Cards y layout ===== */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.5rem;
    box-shadow: var(--shadow);
}
.card-narrow { max-width: 440px; margin: 0 auto; }
.card h1 { font-size: 1.5rem; margin-top: 0; display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }

.section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.75rem;
    margin-bottom: 1rem;
}
.section-header h1, .section-header h2 { margin: 0; }

/* ===== Formularios ===== */
.form-group { margin-bottom: 1rem; }
.form-group label {
    display: block;
    font-weight: 600;
    margin-bottom: 0.35rem;
    color: var(--text);
}
.form-group input, .form-group select, .form-group textarea {
    width: 100%;
    padding: 0.6rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface-2);
    color: var(--text);
    font: inherit;
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus {
    outline: 2px solid var(--primary);
    outline-offset: 1px;
}
.form-hint { color: var(--text-muted); font-size: 0.85rem; margin-top: 0.25rem; }
.field-error { color: var(--error); font-size: 0.85rem; margin-top: 0.25rem; }

.form-actions { display: flex; gap: 0.5rem; flex-wrap: wrap; margin-top: 1rem; }
.form-links { margin-top: 1rem; text-align: center; color: var(--text-muted); font-size: 0.9rem; }

.filters {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    align-items: center;
    margin-bottom: 1rem;
}
.filters input, .filters select {
    padding: 0.5rem 0.6rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--surface-2);
    color: var(--text);
    font: inherit;
}
.filters .checkbox {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    color: var(--text-muted);
    font-size: 0.9rem;
}
.filters .checkbox input { width: auto; }

/* ===== Botones ===== */
.btn {
    display: inline-block;
    padding: 0.55rem 1.1rem;
    border: none;
    border-radius: var(--radius);
    background: var(--primary);
    color: #fff;
    cursor: pointer;
    font: inherit;
    font-weight: 600;
    text-align: center;
    line-height: 1.2;
}
.btn:hover { background: var(--primary-hover); text-decoration: none; color: #fff; }
.btn-block { width: 100%; display: block; }
.btn-secondary {
    background: var(--surface-2);
    color: var(--text);
    border: 1px solid var(--border);
}
.btn-secondary:hover { background: var(--border); color: var(--text); }
.btn-danger { background: var(--danger); }
.btn-danger:hover { background: #b91c1c; color: #fff; }

/* ===== Tablas ===== */
.table-wrap { overflow-x: auto; }
.table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.95rem;
}
.table th, .table td {
    padding: 0.6rem 0.75rem;
    text-align: left;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}
.table th {
    background: var(--surface-2);
    color: var(--text-muted);
    font-weight: 600;
    text-transform: uppercase;
    font-size: 0.75rem;
    letter-spacing: 0.04em;
}
.table tr:hover td { background: var(--surface-2); }
.actions { white-space: nowrap; }

/* ===== Pills / badges ===== */
.badge {
    display: inline-block;
    background: var(--surface-2);
    color: var(--text-muted);
    padding: 0.1rem 0.55rem;
    border-radius: 999px;
    font-size: 0.8rem;
    font-weight: 600;
    border: 1px solid var(--border);
}
.pill {
    display: inline-block;
    padding: 0.15rem 0.6rem;
    border-radius: 999px;
    font-size: 0.78rem;
    font-weight: 600;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text-muted);
    text-transform: capitalize;
}
.pill-ok      { background: color-mix(in srgb, var(--success) 15%, transparent); color: var(--success); border-color: var(--success); }
.pill-muted   { background: var(--surface-2); color: var(--text-muted); }
.pill-state { text-transform: capitalize; }
.pill-creado                  { background: color-mix(in srgb, var(--info) 12%, transparent);    color: var(--info);    border-color: var(--info); }
.pill-en_cotizacion           { background: color-mix(in srgb, var(--warning) 15%, transparent); color: var(--warning); border-color: var(--warning); }
.pill-cotizado                { background: color-mix(in srgb, var(--info) 15%, transparent);    color: var(--info);    border-color: var(--info); }
.pill-pendiente_cliente       { background: color-mix(in srgb, var(--warning) 15%, transparent); color: var(--warning); border-color: var(--warning); }
.pill-enviado_comercializadora{ background: color-mix(in srgb, var(--primary) 15%, transparent); color: var(--primary); border-color: var(--primary); }
.pill-gestionado              { background: color-mix(in srgb, var(--success) 15%, transparent); color: var(--success); border-color: var(--success); }
.pill-devuelto                { background: color-mix(in srgb, var(--danger) 15%, transparent);  color: var(--danger);  border-color: var(--danger); }

/* ===== Pager ===== */
.pager { display: flex; gap: 0.25rem; margin-top: 1rem; flex-wrap: wrap; }
.pager a {
    padding: 0.35rem 0.65rem;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text);
    text-decoration: none;
}
.pager a.is-active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* ===== Key-value ===== */
.kv {
    display: grid;
    grid-template-columns: max-content 1fr;
    gap: 0.35rem 1rem;
    margin: 0;
}
.kv dt { color: var(--text-muted); font-weight: 600; }
.kv dd { margin: 0; }
.kv-cols { column-gap: 2rem; }
@media (min-width: 720px) {
    .kv-cols {
        grid-template-columns: max-content 1fr max-content 1fr;
    }
}

/* ===== Documentos ===== */
.docs-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1.5rem;
}
@media (min-width: 720px) {
    .docs-grid { grid-template-columns: 1fr 1fr; }
}
.doc-item {
    padding: 0.5rem 0.75rem;
    background: var(--surface-2);
    border-radius: var(--radius);
    margin-bottom: 0.5rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.5rem;
}
.cotizaciones-list { list-style: none; padding: 0; }
.cotizaciones-list li {
    padding: 0.6rem 0.75rem;
    background: var(--surface-2);
    border-radius: var(--radius);
    margin-bottom: 0.5rem;
}
.cotizaciones-list label { cursor: pointer; display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }

/* ===== Dashboard tiles ===== */
.dashboard-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 1rem;
    margin-top: 1rem;
}
.dash-tile {
    display: block;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem;
    color: var(--text);
    text-decoration: none;
    transition: transform 0.1s ease;
}
.dash-tile:hover { transform: translateY(-2px); border-color: var(--primary); color: var(--text); text-decoration: none; }
.dash-tile h3 { margin: 0 0 0.35rem; color: var(--primary); }
.dash-tile p { margin: 0; color: var(--text-muted); font-size: 0.9rem; }

/* ===== Notificaciones ===== */
.notif-list { list-style: none; padding: 0; margin: 0; }
.notif {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    margin-bottom: 0.5rem;
    background: var(--surface-2);
    overflow: hidden;
}
.notif.is-unread { border-left: 4px solid var(--primary); }
.notif-link { display: block; padding: 0.75rem 1rem; color: var(--text); text-decoration: none; }
.notif-link:hover { background: var(--surface); text-decoration: none; color: var(--text); }
.notif-title { font-weight: 600; display: flex; align-items: center; gap: 0.5rem; }
.notif-msg { color: var(--text-muted); font-size: 0.9rem; margin: 0.2rem 0; }
.notif-date { color: var(--text-muted); font-size: 0.8rem; }

/* ===== Drop zone cotizaciones ===== */
.drop-zone {
    border: 2px dashed var(--border);
    border-radius: 8px;
    padding: 2rem 1rem;
    text-align: center;
    cursor: pointer;
    background: var(--surface);
    transition: border-color 0.15s, background 0.15s;
    outline: none;
}
.drop-zone:hover,
.drop-zone:focus-visible {
    border-color: var(--primary, #2563eb);
}
.drop-zone-over {
    border-color: var(--primary, #2563eb);
    background: rgba(37, 99, 235, 0.08);
}
.drop-zone-inner { pointer-events: none; }
.drop-zone-resumen { margin-top: 0.5rem; font-size: 0.9rem; color: var(--text-muted); }

.lista-ficheros { margin: 1rem 0; display: flex; flex-direction: column; gap: 0.5rem; }
.fichero-row {
    display: grid;
    grid-template-columns: 1fr auto auto;
    gap: 0.75rem;
    align-items: center;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg);
}
.fichero-nombre { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.fichero-comision { width: 160px; }
@media (max-width: 600px) {
    .fichero-row { grid-template-columns: 1fr; }
    .fichero-comision { width: 100%; }
}

/* ===== Responsive ===== */
@media (max-width: 600px) {
    .topbar-inner { flex-direction: column; gap: 0.5rem; align-items: stretch; }
    .nav { justify-content: center; }
    .filters { flex-direction: column; align-items: stretch; }
    .filters input, .filters select { width: 100%; }
}
