/**
 * Global Theme System - Light/Dark Mode
 * Kesherat AgroMonitor
 */

:root {
    /* Light Mode (Default) */
    --bg-primary: #f5f7fa;
    --bg-secondary: #ffffff;
    --bg-tertiary: #e8ecf1;
    --bg-hover: rgba(15, 23, 42, 0.05);
    --text-primary: #1a202c;
    --text-secondary: #4a5568;
    --text-tertiary: #95a5a6;
    --accent-primary: #1E293B;
    --accent-secondary: #334155;
    --accent-hover: #0F172A;
    --accent-on: #ffffff;
    --border-color: #e1e8ed;
    --border-hover: #cbd5e0;
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.08);
    --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.12);
    --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.16);
    --overlay: rgba(0, 0, 0, 0.5);

    /* Status Colors */
    --success: #4B903A;
    --warning: #f59e0b;
    --error: #ef4444;
    --info: #3b82f6;

    /* Unified radius scale */
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;

    /* Chart Colors */
    --chart-1: #1E293B;
    --chart-2: #334155;
    --chart-3: #64748b;
    --chart-4: #94a3b8;
    --chart-5: #cbd5e1;
}

body.dark-mode {
    /* Dark Mode */
    --bg-primary: #0a0a0a;
    --bg-secondary: #1a1a1a;
    --bg-tertiary: #2d2d2d;
    --bg-hover: rgba(255, 255, 255, 0.06);
    --text-primary: #e8e8e8;
    --text-secondary: #b8b8b8;
    --text-tertiary: #666666;
    --accent-primary: #4B903A;
    --accent-secondary: #3F7A32;
    --accent-hover: #3F7A32;
    --accent-on: #ffffff;
    --border-color: rgba(255, 255, 255, 0.1);
    --border-hover: rgba(255, 255, 255, 0.2);
    --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.5);
    --overlay: rgba(0, 0, 0, 0.7);

    /* Status Colors */
    --success: #22c55e;
    --warning: #f59e0b;
    --error: #ef4444;
    --info: #3b82f6;

    /* Chart Colors */
    --chart-1: #4B903A;
    --chart-2: #3F7A32;
    --chart-3: #34d399;
    --chart-4: #6ee7b7;
    --chart-5: #a7f3d0;
}

/* Global Transitions */
* {
    transition: background-color 0.3s ease, color 0.3s ease, border-color 0.3s ease;
}

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

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

.theme-toggle-btn i {
    font-size: 14px;
    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 — extra contrast in light mode */
.navbar {
    background: var(--bg-secondary) !important;
    border-bottom: 1px solid var(--border-color) !important;
    box-shadow: 0 1px 0 var(--border-color), var(--shadow-sm) !important;
}

body:not(.dark-mode) .navbar {
    background: #f1f5f9 !important;
    border-bottom: 1px solid #cbd5e1 !important;
    box-shadow: 0 1px 0 #cbd5e1, 0 6px 16px -10px rgba(15, 23, 42, 0.25) !important;
}

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

body:not(.dark-mode) .nav-btn {
    color: #475569 !important;
}

body:not(.dark-mode) .nav-btn:hover {
    color: #0f172a !important;
}

body:not(.dark-mode) .nav-btn.active {
    color: #0f172a !important;
    border-bottom-color: #0f172a !important;
}

/* Cards — unified radius */
.card,
.metric-card,
.info-panel,
.login-box {
    background: var(--bg-secondary) !important;
    border: 1px solid var(--border-color) !important;
    box-shadow: var(--shadow-md) !important;
    border-radius: var(--radius-md, 12px) !important;
}

/* Buttons pick up the same scale so pills and cards feel part of the same system */
.btn,
.btn-primary,
.btn-secondary,
.btn-new,
.btn-catalog,
.btn-filter,
.btn-dashboard,
.btn-bulk-upload,
.filter-btn,
.login-btn,
.submit-btn {
    border-radius: var(--radius-sm, 8px) !important;
}

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

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

/* Inputs */
input,
select,
textarea {
    background: var(--bg-primary) !important;
    border: 2px solid var(--border-color) !important;
    color: var(--text-primary) !important;
}

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

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

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

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

.btn-secondary {
    background: var(--bg-tertiary) !important;
    color: var(--text-primary) !important;
    border: 1px solid var(--border-color) !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-md) !important;
}

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

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

/* Tables */
table {
    background: var(--bg-secondary) !important;
    border: 1px solid var(--border-color) !important;
}

th {
    background: var(--bg-tertiary) !important;
    color: var(--text-primary) !important;
    border-bottom: 2px solid var(--border-color) !important;
}

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

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

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

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

/* Badges */
.badge {
    background: var(--bg-tertiary) !important;
    color: var(--text-primary) !important;
    border: 1px solid var(--border-color) !important;
}

.badge-success {
    background: rgba(75, 144, 58, 0.1) !important;
    color: var(--success) !important;
    border-color: var(--success) !important;
}

.badge-warning {
    background: rgba(245, 158, 11, 0.1) !important;
    color: var(--warning) !important;
    border-color: var(--warning) !important;
}

.badge-error {
    background: rgba(239, 68, 68, 0.1) !important;
    color: var(--error) !important;
    border-color: var(--error) !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-md) !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-size: 10px;
    font-weight: 700;
    white-space: nowrap;
    pointer-events: none;
    padding: 2px 6px;
    border-radius: 4px;
    
    /* Default (Light Mode) - Clean white translucent background with dark text */
    background: rgba(255, 255, 255, 0.4);
    color: #1e293b;
    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: #fcd34d;
    border: 0 !important;
    outline: none !important;
    box-shadow: none !important;
    border-radius: 999px;
    padding: 0;
    cursor: pointer;
    transition: background 0.25s 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: #f59e0b;
    transition: transform 0.25s ease, background 0.25s 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: #0b2540;
}
.theme__toggle:checked::before {
    background: #38bdf8;
    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 0.25s ease;
}

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

.theme__icon-part {
    position: absolute;
    background: #ffffff;
    border-radius: 50%;
    transition: transform 0.25s ease, opacity 0.2s ease, box-shadow 0.25s 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 #ffffff;
}

/* 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-md) !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: rgba(168, 85, 247, 0.12) !important;
    color: #6d28d9 !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-lg) !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: #15803d !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.
   ============================================================ */
body:not(.dark-mode) .floating-chat-widget {
    background: var(--bg-secondary) !important;
    border: 1px solid var(--border-color) !important;
    box-shadow: var(--shadow-lg) !important;
}

body:not(.dark-mode) .chat-header {
    border-bottom: 1px solid var(--border-color) !important;
    background: var(--bg-tertiary) !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-tertiary) !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-tertiary) !important;
}

body:not(.dark-mode) #floatingChatInput,
body:not(.dark-mode) #chatInput {
    background: var(--bg-primary) !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(--bg-tertiary) !important;
    border: 1px solid var(--border-color) !important;
    color: var(--text-primary) !important;
}

body:not(.dark-mode) .message.user {
    background: rgba(75, 144, 58, 0.12) !important;
    border: 1px solid rgba(75, 144, 58, 0.35) !important;
    color: var(--text-primary) !important;
}

body:not(.dark-mode) .typing-indicator {
    background: var(--bg-tertiary) !important;
    border: 1px solid var(--border-color) !important;
}

/* Welcome bubble that hangs above the toggle button */
body:not(.dark-mode) .chat-welcome-bubble {
    /* gradient already looks fine in both themes; just dim the shadow */
    box-shadow: 0 4px 16px rgba(102, 126, 234, 0.25) !important;
}

