@import url('tokens.css');

/**
 * Global Theme System - Light/Dark Mode
 * MilpAI Agro
 *
 * Las fuentes y los tokens viven en tokens.css, que se importa arriba: doce
 * paginas cargan este archivo, y tener el mismo <link> repetido doce veces es
 * como se desincroniza un sistema de diseno. Quien incluya theme.css hereda
 * la tipografia y la paleta.
 */

/* ── Tipografia ──────────────────────────────────────────────────────────────
   Escala fija de la guia: 40, 28, 20, 15, 13 y 12. Se enfatiza con peso o
   color, no con tamanos intermedios. */

body {
    font-family: var(--font-body);
    font-size: 15px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

h1, h2, h3, h4, h5, h6,
.view-title, .logo-text, .card-title, .macro-card-title {
    font-family: var(--font-heading);
    font-weight: 600;
    line-height: 1.1;
    letter-spacing: -0.01em;
}

p {
    line-height: 1.5;
}

/* Los controles de formulario no heredan la fuente por defecto: sin esto
   cada boton sale en la letra del sistema. */
button, input, select, textarea, optgroup {
    font-family: inherit;
}

/* Eyebrow: mayusculas con tracking de 0.28em, siempre encima de un titulo.
   Es la otra cara del dorado de la vista: en gold-text, que si lee. */
.eyebrow {
    display: block;
    font-family: var(--font-heading);
    font-size: 12px;
    font-weight: 600;
    letter-spacing: 0.28em;
    text-transform: uppercase;
    color: var(--gold-text) !important;
}

/* Las cifras en mono: numeros, folios, fechas y horas. Tabulares, para que
   las columnas no bailen cuando el valor cambia. */
.metric-value, .kpi-value, .stat-value, .mono, .num,
code, kbd, samp {
    font-family: var(--font-mono);
    font-variant-numeric: tabular-nums;
}

.macro-td, .macro-th, table td, table th {
    font-variant-numeric: tabular-nums;
}

/* Global Transitions: cortas y ease-out (la guia pide 150 a 250 ms) */
* {
    transition: background-color var(--dur) var(--ease), color var(--dur) var(--ease), border-color var(--dur) var(--ease);
}

@media (prefers-reduced-motion: reduce) {
    * {
        transition: none !important;
        animation-duration: 0.01ms !important;
    }
}

/* Theme Toggle Button */
.theme-toggle-btn {
    background: transparent;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    padding: 6px 10px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 6px;
    transition: all var(--dur) var(--ease);
    font-size: 12px;
    font-weight: 500;
    color: var(--text-secondary);
}

.theme-toggle-btn:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
    border-color: var(--border-hover);
}

.theme-toggle-btn i {
    font-size: 13px;
    color: var(--accent-primary);
}

/* Logo Switching */
.logo-light {
    display: block;
}

.logo-dark {
    display: none;
}

body.dark-mode .logo-light {
    display: none;
}

body.dark-mode .logo-dark {
    display: block;
}

/* The dark-mode source is tighter-cropped than the light one, so scale it up
   (~1.7×) to match the visual size in the navbar. */
.logo img.logo-dark {
    height: 46px !important;
    max-height: none !important;
    width: auto;
}

@media (max-width: 768px) {
    .logo img.logo-dark {
        height: 40px !important;
    }
}

/* Navbar: clara, con la linea de la guia. No es forest porque el logo es
   tinta forest sobre transparente y no hay version clara. */
.navbar {
    background: var(--bg-secondary) !important;
    border-bottom: 1px solid var(--border-color) !important;
    box-shadow: none !important;
}

/* En oscuro el logo (tinta forest) se pierde: va sobre una placa clara */
body.dark-mode .navbar .logo img.logo-full {
    background: #FEFDFB;
    border-radius: 8px;
    padding: 3px 8px;
}

.navbar h1,
.navbar .logo-text {
    color: var(--text-primary) !important;
}

.nav-btn {
    color: var(--text-secondary) !important;
    font-weight: 500;
}

.nav-btn:hover {
    color: var(--text-primary) !important;
}

.nav-btn.active {
    color: var(--text-primary) !important;
    font-weight: 600;
    border-bottom-color: var(--accent-primary) !important;
}

/* Cards: nivel 1 (tarjeta). Radio 20, borde line y sombra suave en dos capas. */
.card,
.metric-card,
.info-panel,
.login-box {
    background: var(--bg-secondary) !important;
    border: 1px solid var(--border-color) !important;
    box-shadow: var(--shadow-1) !important;
    border-radius: var(--radius) !important;
}

/* Buttons: radio 12. Primario forest, secundario con borde, texto en
   gold-text y el dorado solo para la accion principal de la vista. */
.btn,
.btn-primary,
.btn-secondary,
.btn-new,
.btn-catalog,
.btn-filter,
.btn-dashboard,
.btn-bulk-upload,
.btn-accent,
.btn-text,
.filter-btn,
.login-btn,
.submit-btn {
    border-radius: var(--radius-sm) !important;
    font-family: var(--font-heading);
    font-weight: 600;
}

/* Text */
h1, h2, h3, h4, h5, h6 {
    color: var(--text-primary) !important;
}

p, span, label {
    color: var(--text-secondary) !important;
}

/* Inputs: planos (nivel 0), radio 12, foco forest con anillo */
input,
select,
textarea {
    background: var(--bg-secondary) !important;
    border: 1px solid var(--border-color) !important;
    border-radius: var(--radius-sm);
    color: var(--text-primary) !important;
    font-family: var(--font-body);
}

input:focus,
select:focus,
textarea:focus {
    border-color: var(--accent-primary) !important;
    background: var(--bg-secondary) !important;
    box-shadow: var(--ring);
    outline: none;
}

input::placeholder,
textarea::placeholder {
    color: var(--text-tertiary) !important;
}

/* Buttons */
.btn,
.login-btn {
    background: var(--accent-primary) !important;
    color: var(--accent-on) !important;
    border: none !important;
}

.btn:hover,
.login-btn:hover {
    background: var(--accent-hover) !important;
}

.btn-secondary {
    background: transparent !important;
    color: var(--text-primary) !important;
    border: 1px solid var(--accent-primary) !important;
}

.btn-secondary:hover {
    background: var(--bg-hover) !important;
}

.btn-text {
    background: transparent !important;
    color: var(--gold-text) !important;
    border: none !important;
}

.btn-accent {
    background: var(--gold) !important;
    color: var(--forest) !important;
    border: none !important;
}

.btn-accent:hover {
    background: #C98E02 !important;
}

/* Destructiva: no puede verse como la primaria (.btn la pinta forest) */
.btn-danger,
.btn.btn-danger {
    background: var(--error) !important;
    color: #FFFFFF !important;
    border: none !important;
}

.btn-danger:hover,
.btn.btn-danger:hover {
    background: #9A3632 !important;
}

body.dark-mode .btn-danger,
body.dark-mode .btn.btn-danger {
    color: var(--night) !important;
}

/* Navigation buttons - override to keep them plain */
.nav-btn {
    background: transparent !important;
    border: none !important;
    border-bottom: 2px solid transparent !important;
    box-shadow: none !important;
}

/* Sidebar */
.right-sidebar,
.sidebar {
    background: var(--bg-secondary) !important;
    border: 1px solid var(--border-color) !important;
    box-shadow: var(--shadow-sm) !important;
}

.sidebar-btn {
    color: var(--text-secondary) !important;
}

.sidebar-btn:hover,
.sidebar-btn.active {
    background: var(--accent-primary) !important;
    color: var(--accent-on) !important;
}

/* Tables: nivel 0. Encabezado en cream, rotulo en mayusculas con tracking. */
table {
    background: var(--bg-secondary) !important;
    border: 1px solid var(--border-color) !important;
}

th {
    background: var(--bg-tertiary) !important;
    color: var(--text-tertiary) !important;
    border-bottom: 1px solid var(--border-color) !important;
    font-size: 12px;
    font-weight: 600;
    letter-spacing: 0.12em;
    text-transform: uppercase;
}

td {
    color: var(--text-primary) !important;
    border-bottom: 1px solid var(--border-color) !important;
}

tr:hover {
    background: var(--bg-hover) !important;
}

/* Modals: nivel 2 (flotante) */
.modal {
    background: var(--overlay) !important;
}

.modal-content {
    background: var(--bg-secondary) !important;
    border: 1px solid var(--border-color) !important;
    box-shadow: var(--shadow-2) !important;
    border-radius: var(--radius) !important;
}

/* Badges y pildoras: radio 999, fondo lavado y tinta legible. Siempre con
   palabra: el color acompana, no informa solo. */
.badge,
.pill {
    background: var(--bg-tertiary) !important;
    color: var(--text-primary) !important;
    border: 1px solid transparent !important;
    border-radius: var(--radius-pill) !important;
    font-size: 12px;
    font-weight: 600;
}

.badge-success,
.pill-ok {
    background: var(--success-wash) !important;
    color: var(--success-text) !important;
}

.badge-warning,
.pill-watch {
    background: var(--warning-wash) !important;
    color: var(--warning-text) !important;
}

.badge-error,
.pill-risk {
    background: var(--error) !important;
    color: #FFFFFF !important;
}

.badge-info {
    background: var(--info-wash) !important;
    color: var(--info-text) !important;
}

/* Punto de estado delante de la palabra (Atendido, En observacion, En riesgo) */
.pill-ok::before,
.pill-watch::before,
.pill-risk::before {
    content: '';
    display: inline-block;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    margin-right: 6px;
    vertical-align: 1px;
    background: currentColor;
}

.pill-ok::before { background: var(--leaf); }
.pill-watch::before { background: var(--gold); }
.pill-risk::before { background: #FFFFFF; }

/* En oscuro el rojo es claro: la palabra va en night para que lea */
body.dark-mode .badge-error,
body.dark-mode .pill-risk { color: var(--night) !important; }
body.dark-mode .pill-risk::before { background: var(--night); }

/* Prioridad: contorno, no relleno. Urgente en danger, Alta en dorado. */
.prio {
    display: inline-block;
    padding: 2px 10px;
    border-radius: var(--radius-pill);
    border: 1px solid var(--border-color);
    background: transparent;
    font-size: 12px;
    font-weight: 600;
    color: var(--text-primary) !important;
}

.prio-urgente { border-color: var(--danger); color: var(--error-text) !important; }
.prio-alta { border-color: var(--gold); color: var(--warning-text) !important; }
.prio-baja { color: var(--text-tertiary) !important; }

/* Puntos clave: borde izquierdo dorado de 5px */
.punto-clave {
    border-left: 5px solid var(--gold);
    padding-left: 14px;
}

/* Fondo heroe: forest con brillo (login, estados vacios) */
.hero-forest {
    background: var(--hero) !important;
    color: var(--surface) !important;
}

/* View Panels */
.view-panel {
    background: var(--bg-primary) !important;
}

.view-title {
    color: var(--text-primary) !important;
}

/* Legend */
.legend {
    background: var(--bg-secondary) !important;
    border: 1px solid var(--border-color) !important;
    box-shadow: var(--shadow-sm) !important;
}

.legend-title {
    color: var(--text-primary) !important;
}

.legend-item {
    color: var(--text-secondary) !important;
}

/* Responsive */
@media (max-width: 768px) {
    .theme-toggle-btn {
        top: 10px;
        right: 10px;
        padding: 8px 14px;
        font-size: 12px;
    }
}

/* Chart Value Badges - Adaptive */
.chart-value-badge {
    font-family: var(--font-mono);
    font-size: 12px;
    font-weight: 600;
    white-space: nowrap;
    pointer-events: none;
    padding: 2px 6px;
    border-radius: 6px;

    /* Default (Light Mode) - Clean white translucent background with dark text */
    background: rgba(255, 255, 255, 0.4);
    color: var(--ink);
    text-shadow: none;
    backdrop-filter: blur(2px);
    border: 1px solid rgba(255, 255, 255, 0.5);
}

body.dark-mode .chart-value-badge {
    /* Dark Mode - Dark translucent background with white text */
    background: rgba(0, 0, 0, 0.4);
    color: #fff;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

/* ============================================================
   Sun ↔ moon theme toggle — compact (40 x 22 px, fixed)
   Uses px values so it can't inherit a bigger font-size.
   ============================================================ */
.theme {
    display: inline-flex;
    align-items: center;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    line-height: 0;
}

.theme__toggle-wrap {
    position: relative;
    display: inline-block;
    width: 40px;
    height: 22px;
}

.theme__fill { display: none; }

.theme__toggle {
    -webkit-appearance: none !important;
    -moz-appearance: none !important;
    appearance: none !important;
    margin: 0;
    display: block;
    width: 40px;
    height: 22px;
    background: var(--gold-wash);
    border: 0 !important;
    outline: none !important;
    box-shadow: none !important;
    border-radius: 999px;
    padding: 0;
    cursor: pointer;
    transition: background var(--dur) var(--ease);
    box-sizing: border-box;
    accent-color: transparent;
}

.theme__toggle::before {
    content: '';
    position: absolute;
    top: 2px;
    left: 2px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--gold);
    transition: transform var(--dur) var(--ease), background var(--dur) var(--ease);
}

.theme,
.theme:focus,
.theme:focus-within,
.theme:active,
.theme__toggle-wrap,
.theme__toggle:focus,
.theme__toggle:focus-visible,
.theme__toggle:active,
.theme__toggle:hover,
.theme:focus-within .theme__toggle {
    outline: none !important;
    box-shadow: none !important;
    border: 0 !important;
}

.theme__toggle:checked {
    background: var(--forest);
}
.theme__toggle:checked::before {
    background: #9DD28B;
    transform: translateX(18px);
}

/* Icon overlays the knob and slides with it */
.theme__icon {
    position: absolute;
    top: 4px;
    left: 4px;
    width: 14px;
    height: 14px;
    pointer-events: none;
    transition: transform var(--dur) var(--ease);
}

.theme__toggle:checked ~ .theme__icon {
    transform: translateX(18px);
}

.theme__icon-part {
    position: absolute;
    background: #ffffff;
    border-radius: 50%;
    transition: transform var(--dur) var(--ease), opacity var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}

/* Center disc (sun body / moon body) */
.theme__icon-part:nth-child(1) {
    top: 50%;
    left: 50%;
    width: 6px;
    height: 6px;
    margin: -3px 0 0 -3px;
    box-shadow: inset 0 0 0 1px #ffffff;
}

.theme__toggle:checked ~ .theme__icon .theme__icon-part:nth-child(1) {
    width: 10px;
    height: 10px;
    margin: -5px 0 0 -5px;
    background: transparent;
    box-shadow: inset 3px -2px 0 0 var(--night);
}

/* 8 rays — kept short so the sun stays within the knob */
.theme__icon-part:nth-child(n+2) {
    top: 50%;
    left: 50%;
    width: 1.5px;
    height: 2px;
    margin: -1px 0 0 -0.75px;
    border-radius: 1px;
    transform-origin: 50% 0;
}
.theme__icon-part:nth-child(2) { transform: rotate(0deg)   translateY(3.5px); }
.theme__icon-part:nth-child(3) { transform: rotate(45deg)  translateY(3.5px); }
.theme__icon-part:nth-child(4) { transform: rotate(90deg)  translateY(3.5px); }
.theme__icon-part:nth-child(5) { transform: rotate(135deg) translateY(3.5px); }
.theme__icon-part:nth-child(6) { transform: rotate(180deg) translateY(3.5px); }
.theme__icon-part:nth-child(7) { transform: rotate(225deg) translateY(3.5px); }
.theme__icon-part:nth-child(8) { transform: rotate(270deg) translateY(3.5px); }
.theme__icon-part:nth-child(9) { transform: rotate(315deg) translateY(3.5px); }

.theme__toggle:checked ~ .theme__icon .theme__icon-part:nth-child(n+2) {
    opacity: 0;
}

/* ============================================================
   AgentesIA — light-mode fixes
   The cards/modals in the Agentes view use hardcoded dark
   gradients/backgrounds inline; these rules override them
   only in light mode without touching dark mode.
   ============================================================ */
body:not(.dark-mode) .agent-card {
    background: var(--bg-secondary) !important;
    border: 1px solid var(--border-color) !important;
    box-shadow: var(--shadow-1) !important;
}

body:not(.dark-mode) .agent-card h3 {
    color: var(--text-primary) !important;
}

/* Inner stats panel (Schedule / Último run) */
body:not(.dark-mode) .agent-card > div[style*="rgba(0,0,0,0.2)"] {
    background: var(--bg-tertiary) !important;
}

/* Action bar separator */
body:not(.dark-mode) .agent-card > div[style*="border-top:1px solid rgba(255,255,255"] {
    border-top-color: var(--border-color) !important;
}

/* Tool pills inside agent cards */
body:not(.dark-mode) .agent-card span[style*="rgba(168,85,247,0.1)"] {
    background: var(--leaf-wash) !important;
    color: var(--leaf-text) !important;
}

/* "Nuevo" / source label */
body:not(.dark-mode) .agent-card div[style*="color:#666"] {
    color: var(--text-secondary) !important;
}

/* "Crear con IA" + "Catálogo de Plantillas" modal panels */
body:not(.dark-mode) #agentes-create-overlay > div[style*="position:relative"] {
    background: var(--bg-secondary) !important;
    border-color: var(--border-color) !important;
    box-shadow: var(--shadow-2) !important;
}

body:not(.dark-mode) #agentes-create-overlay h3,
body:not(.dark-mode) #agentes-create-overlay h4 {
    color: var(--text-primary) !important;
}

body:not(.dark-mode) #agentes-create-overlay p {
    color: var(--text-secondary) !important;
}

/* Modal footer (Cancelar / Generar) */
body:not(.dark-mode) #agentes-create-overlay div[style*="border-top:1px solid"][style*="background:var(--bg-primary"] {
    background: var(--bg-primary) !important;
    border-top-color: var(--border-color) !important;
}

/* NL preview card (Generar Vista Previa output) */
body:not(.dark-mode) #nl-preview-container > div {
    background: var(--bg-secondary) !important;
    border-color: var(--border-color) !important;
    box-shadow: var(--shadow-sm);
}

body:not(.dark-mode) #nl-preview-container h4 {
    color: var(--text-primary) !important;
}

/* Inner stat panels inside the NL preview */
body:not(.dark-mode) #nl-preview-container div[style*="rgba(0,0,0,0.25)"] {
    background: var(--bg-tertiary) !important;
}

/* Suggestion chips — keep visible in light */
body:not(.dark-mode) #agentes-create-overlay button[style*="rgba(34,197,94,0.08)"] {
    color: var(--leaf-text) !important;
}

/* Template catalog cards */
body:not(.dark-mode) .tpl-card {
    background: var(--bg-secondary) !important;
    border-color: var(--border-color) !important;
    box-shadow: var(--shadow-sm);
}

body:not(.dark-mode) .tpl-card h4 {
    color: var(--text-primary) !important;
}

/* Empty-state icon wrapper inside agentes-list */
body:not(.dark-mode) #agentes-list h3 {
    color: var(--text-primary) !important;
}

/* ============================================================
   Floating Copiloto Agrícola IA chat widget — light mode
   The chat widget hardcoded rgba(20,20,30,0.98) backgrounds and
   white text inline, leaving dark-on-dark in light mode.
   Burbujas de la guia: la persona en cream y el bot en leaf-wash. La
   esquina de 6px apunta a quien habla; aqui la persona va a la derecha
   y el bot a la izquierda, al reves que la guia.
   ============================================================ */
body:not(.dark-mode) .floating-chat-widget {
    background: var(--bg-secondary) !important;
    border: 1px solid var(--border-color) !important;
    box-shadow: var(--shadow-2) !important;
    border-radius: var(--radius) !important;
}

body:not(.dark-mode) .chat-header {
    border-bottom: 1px solid var(--border-color) !important;
    background: var(--bg-secondary) !important;
}

body:not(.dark-mode) .chat-header h4 {
    color: var(--text-primary) !important;
}

body:not(.dark-mode) .chat-messages {
    background: var(--bg-secondary) !important;
}

body:not(.dark-mode) .chat-welcome {
    background: var(--bg-primary) !important;
    border: 1px solid var(--border-color);
}

body:not(.dark-mode) .chat-welcome h3,
body:not(.dark-mode) .chat-welcome h4 {
    color: var(--text-primary) !important;
}

body:not(.dark-mode) .chat-welcome p,
body:not(.dark-mode) .chat-welcome ul,
body:not(.dark-mode) .chat-welcome ul li {
    color: var(--text-secondary) !important;
}

body:not(.dark-mode) .chat-input-container {
    border-top: 1px solid var(--border-color) !important;
    background: var(--bg-secondary) !important;
}

body:not(.dark-mode) #floatingChatInput,
body:not(.dark-mode) #chatInput {
    background: var(--bg-secondary) !important;
    border: 1px solid var(--border-color) !important;
    color: var(--text-primary) !important;
}

body:not(.dark-mode) #floatingChatInput::placeholder,
body:not(.dark-mode) #chatInput::placeholder {
    color: var(--text-tertiary) !important;
}

body:not(.dark-mode) .message.assistant {
    background: var(--leaf-wash) !important;
    border: 1px solid transparent !important;
    border-radius: 16px 16px 16px 6px !important;
    color: var(--text-primary) !important;
}

body:not(.dark-mode) .message.user {
    background: var(--cream) !important;
    border: 1px solid transparent !important;
    border-radius: 16px 16px 6px 16px !important;
    color: var(--text-primary) !important;
}

body:not(.dark-mode) .typing-indicator {
    background: var(--leaf-wash) !important;
    border: 1px solid transparent !important;
}

/* Welcome bubble that hangs above the toggle button */
body:not(.dark-mode) .chat-welcome-bubble {
    box-shadow: var(--shadow-1) !important;
}
