:root {
    --bg-color: #f8fafc;
    --card-bg: #ffffff;
    --text-color: #0f172a;
    --muted-text: #64748b;
    --border-color: #e2e8f0;
    --table-header: #f1f5f9;
    --accent-primary: #0ea5e9;
    --accent-hover: #0284c7;
    --input-bg: #ffffff;
    --input-text: #0f172a;
}

[data-theme="dark"] {
    --bg-color: #020617;
    --card-bg: #0f172a;
    --text-color: #f8fafc;
    --muted-text: #94a3b8;
    --border-color: #1e293b;
    --table-header: #1e293b;
    --input-bg: #1e293b;
    --input-text: #f8fafc;
}

body { font-family: 'Inter', system-ui, sans-serif; background-color: var(--bg-color); color: var(--text-color); margin: 0; display: flex; transition: background 0.3s; }

.sidebar { width: 260px; height: 100vh; background: #0f172a; color: white; padding: 20px; position: fixed; box-sizing: border-box; overflow-y: auto; }
.main-content { 
    margin-left: 260px; 
    padding: 40px; 
    width: calc(100% - 260px); /* This pulls the right side back onto the monitor */
    min-height: 100vh; 
    box-sizing: border-box; 
    overflow-x: hidden; 
}

.card { background: var(--card-bg); border: 1px solid var(--border-color); border-radius: 12px; padding: 24px; margin-bottom: 24px; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1); }

/* =========================================
   SIDEBAR NAVIGATION (Liquid Pools)
   ========================================= */
.nav-links { 
    list-style: none; 
    padding: 0; 
    margin-top: 20px; 
    overflow-x: hidden; /* Prevents horizontal scrollbars in the sidebar */
}

.nav-links li { 
    padding: 10px 15px; 
    cursor: pointer; 
    border-radius: 25px; /* Creates a smooth, rounded puddle shape */
    margin-bottom: 6px; 
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1); 
    white-space: nowrap; /* MAGIC RULE: Absolutely forbids text from wrapping */
    overflow: hidden;
    text-overflow: ellipsis; /* Adds "..." if text is too long, rather than breaking */
    border: 1px solid transparent;
}

.nav-links li:hover, .nav-links li.active { 
    /* The Pool Effect */
    background: linear-gradient(135deg, rgba(14, 165, 233, 0.15), rgba(14, 165, 233, 0.05)); 
    color: var(--accent-primary); 
    font-weight: bold;
    border: 1px solid rgba(14, 165, 233, 0.3); /* Subtle surface reflection */
    box-shadow: 
        inset 0 2px 4px rgba(255, 255, 255, 0.05), /* Inner highlight (surface tension) */
        0 4px 8px rgba(0, 0, 0, 0.15); /* Soft outer drop shadow */
    transform: translateY(-1px); /* Makes the pool "float" slightly when active */
}

.module { display: none; }
.module.active { display: block; }

.form-group { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 15px; }
input, select { background: var(--input-bg); color: var(--input-text); border: 1px solid var(--border-color); padding: 10px; border-radius: 6px; }
button { background: var(--accent-primary); color: white; border: none; padding: 10px 15px; border-radius: 6px; cursor: pointer; font-weight: bold; transition: background 0.2s; }
button:hover { background: var(--accent-hover); }
.btn-delete { background: #ef4444; } .btn-delete:hover { background: #dc2626; }

/* =========================================
   TABLES & LEDGER
   ========================================= */
table { 
    width: 100%; 
    border-collapse: collapse; 
    margin-top: 15px; 
    font-size: 0.9em; /* Slightly smaller to prevent crowding */
}
th, td { 
    text-align: left; 
    padding: 12px 10px; 
    border-bottom: 1px solid var(--border-color); 
    white-space: nowrap; /* Keeps most data on a single clean line */
}
td:nth-child(6) { 
    white-space: normal; /* Allows ONLY the description to wrap */
    width: auto; 
    min-width: 150px; 
    max-width: 400px; /* Prevents the description from stealing all the space */
}
td:last-child, th:last-child {
    min-width: 90px;
    text-align: right; /* Pins the action buttons to the right side safely */
}
.text-inflow { color: #10b981; }
.text-expense { color: #f43f5e; }

/* MODALS */
.modal-overlay { display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.6); z-index: 1000; justify-content: center; align-items: center; backdrop-filter: blur(3px); }
.modal-content { background: var(--card-bg); padding: 30px; border-radius: 12px; width: 90%; max-width: 600px; border: 1px solid var(--border-color); box-shadow: 0 20px 25px -5px rgba(0,0,0,0.1); max-height: 90vh; overflow-y: auto; }
.btn-close-modal { float: right; background: transparent; color: var(--muted-text); border: 1px solid var(--border-color); }

/* LIQUID WAVE ENGINE (Cistern & Ponds) */
.pond-card { width: 200px; padding: 15px; border-radius: 12px; background: var(--table-header); border: 1px solid var(--border-color); display: flex; flex-direction: column; align-items: center; box-shadow: 0 4px 6px rgba(0,0,0,0.05); transition: transform 0.2s; }
.pond-card:hover { transform: translateY(-3px); }
.pond-title { color: var(--text-color); font-size: 0.95em; min-height: 2.8em; display: flex; align-items: center; text-align: center; line-height: 1.2; margin-bottom: 10px; width: 100%; justify-content: center; font-weight: bold; }
.btn-pond-save { width: 100%; padding: 8px; border-radius: 6px; background: var(--accent-primary) !important; color: white !important; border: none !important; font-weight: bold; cursor: pointer; margin-top: 5px; }

.pond-container, .cistern-container { border-radius: 50%; position: relative; overflow: hidden; background: var(--bg-color); border: 4px solid var(--border-color); box-shadow: inset 0 0 15px rgba(0,0,0,0.2); }
.pond-water, .cistern-water { position: absolute; width: 400%; height: 400%; background: rgba(14, 165, 233, 0.4); left: -150%; top: 100%; border-radius: 40% 45% 40% 45%; animation: spinWave 6s linear infinite; transition: top 0.8s cubic-bezier(0.4, 0, 0.2, 1), background-color 0.5s; z-index: 1; }
.pond-water.back-wave, .cistern-water.back-wave { background: rgba(2, 132, 199, 0.6); border-radius: 45% 40% 45% 40%; animation: spinWave 10s linear infinite; z-index: 0; }
.overspent { background: rgba(244, 63, 94, 0.4) !important; }
.back-wave.overspent { background: rgba(225, 29, 72, 0.6) !important; }

@keyframes spinWave { from { transform: rotate(0deg); } to { transform: rotate(360deg); } }

.cistern-label { position: absolute; width: 100%; top: 50%; transform: translateY(-50%); text-align: center; z-index: 5; text-shadow: 0 1px 3px rgba(0,0,0,0.8); }

/* =========================================
   CALENDAR
   ========================================= */
.calendar-grid { 
    display: grid; 
    grid-template-columns: repeat(7, 1fr); 
    gap: 8px; 
    margin-top: 15px; 
}
.calendar-day-title { 
    text-align: center; 
    font-weight: bold; 
    color: var(--muted-text); 
    padding: 10px 0; 
    text-transform: uppercase; 
    font-size: 0.85em; 
    letter-spacing: 1px; 
}
.calendar-day { 
    min-height: 120px; 
    border: 1px solid var(--border-color); 
    border-radius: 8px; 
    padding: 8px; 
    background: var(--table-header); 
    cursor: pointer; 
    display: flex; 
    flex-direction: column; /* This forces items to stack neatly */
    overflow: hidden; 
    transition: background 0.2s, border-color 0.2s;
}
.calendar-day:hover { 
    background: var(--card-bg); 
    border-color: var(--accent-primary); 
}
.calendar-day.empty { 
    background: transparent; 
    border: none; 
    cursor: default; 
}
.calendar-day.current-day { 
    border-color: var(--accent-primary); 
    box-shadow: 0 0 0 1px var(--accent-primary); 
    background: rgba(14, 165, 233, 0.05); 
}
.day-number { 
    font-weight: bold; 
    color: var(--muted-text); 
    margin-bottom: 8px; 
    font-size: 0.9em; 
    align-self: flex-start; 
}
.calendar-day { 
    height: 150px; /* Locks the height so the grid stays uniform */
    border: 1px solid var(--border-color); 
    border-radius: 8px; 
    padding: 8px; 
    background: var(--table-header); 
    cursor: pointer; 
    display: flex; 
    flex-direction: column; 
    overflow-x: hidden;
    overflow-y: auto; /* Activates internal scrolling */
    transition: background 0.2s, border-color 0.2s;
}

/* Slim, modern scrollbar for the calendar days */
.calendar-day::-webkit-scrollbar {
    width: 6px;
}
.calendar-day::-webkit-scrollbar-track {
    background: transparent;
}
.calendar-day::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 10px;
}
.calendar-day:hover::-webkit-scrollbar-thumb {
    background: var(--muted-text);
}
.cal-entry { 
    font-size: 0.75em; 
    padding: 4px 6px; 
    margin-bottom: 4px; 
    border-radius: 4px; 
    color: white; 
    white-space: nowrap; 
    overflow: hidden; 
    text-overflow: ellipsis; 
    font-weight: bold; 
    text-shadow: 0 1px 2px rgba(0,0,0,0.4); 
    flex-shrink: 0; /* THIS PREVENTS THE SQUISHING */
}
@media print { .no-print { display: none !important; } .main-content { margin: 0; padding: 0; } body { background: white; color: black; } }

/* ==========================================================================
   STICKY TABLE HEADERS
   ========================================================================== */
#module-dashboard table thead th {
    position: sticky;
    top: 0;
    z-index: 10;
    background-color: var(--table-header); /* Keeps the background solid */
    box-shadow: 0 2px 4px rgba(0,0,0,0.3); /* Adds a subtle drop shadow to the floating header */
}

/* ==========================================================================
   LEDGER HEADER BUTTONS (Sidebar Style)
   ========================================================================== */
/* Target ONLY the headers that have an 'onclick' sorting function */
#module-dashboard table thead th[onclick] {
    cursor: pointer;
    border-radius: 6px;
    transition: all 0.2s ease-in-out;
    border: 1px solid transparent; /* Keeps the box size stable on hover */
}

/* Apply the navigation sidebar hover effects */
#module-dashboard table thead th[onclick]:hover {
    background-color: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: #38bdf8; /* The wellspring cerulean blue */
}

/* ========================================================================== */
/* MOBILE RESPONSIVE LAYOUT (THE ULTIMATE MOBILE FIX)                        */
/* ========================================================================== */
@media (max-width: 768px) {
    /* 1. Force the body to stack neatly without side-scrolling the whole page */
    body {
        display: flex !important;
        flex-direction: column !important;
        overflow-x: hidden !important;
        margin: 0 !important;
        padding: 0 !important;
        background: #0f172a !important; /* Keep the dark background visible */
    }

    /* 2. The Sidebar Header */
    .sidebar {
        width: 100% !important;
        height: auto !important;
        min-height: auto !important;
        position: relative !important;
        padding: 15px 10px 5px 10px !important;
        box-sizing: border-box !important;
        z-index: 1000;
        background: #0f172a !important;
    }

    /* 3. The Swipable Ribbon */
    .nav-links {
        display: flex !important;
        flex-direction: row !important;
        flex-wrap: nowrap !important;
        overflow-x: auto !important;
        white-space: nowrap !important;
        padding: 0 0 10px 0 !important;
        margin: 15px 0 0 0 !important;
        gap: 10px !important;
        -webkit-overflow-scrolling: touch;
    }
    .nav-links li {
        display: block !important;
        margin: 0 !important;
        flex: 0 0 auto !important;
    }
    .nav-links li a {
        display: block !important;
        padding: 12px 18px !important;
        background: rgba(255,255,255,0.05);
        border-radius: 8px;
        color: white !important;
    }

    /* 4. Release the white main content area */
    .sidebar + div, #main-content, .main-content, .main, .content {
        margin-left: 0 !important;
        padding: 15px !important;
        width: 100% !important;
        max-width: 100vw !important;
        box-sizing: border-box !important;
    }

    /* 5. Force the internal flex containers to stack (The specific visual fix!) */
    .module div[style*="display: flex"] {
        flex-direction: column !important;
        align-items: stretch !important;
        gap: 15px !important;
    }
    /* ...but allow adjacent inputs in some cases */
    .module div[style*="display: flex"] .form-group[style*="display: flex"] {
        flex-direction: row !important;
        flex-wrap: nowrap !important;
        gap: 10px !important;
    }

    /* 6. Target the generic selectors from the image (F ⇳ and E ⇳) */
    .module select, .module input {
        min-width: 100% !important;
        box-sizing: border-box !important;
        padding: 15px !important; /* Larger tap target */
        margin-bottom: 0px !important;
        border-radius: 8px !important;
    }

    /* Fix the Save/Split actions in the same image */
    #quick-transaction-actions-container, .module-actions-row, .entry-form-actions {
        flex-direction: column !important;
        gap: 10px !important;
    }

    /* 7. Allow the Ledger table to swipe left/right */
    .module table {
        display: block !important;
        width: 100% !important;
        overflow-x: auto !important;
        white-space: nowrap !important;
        -webkit-overflow-scrolling: touch;
    }

    /* 8. Fix charts on mobile */
    .module canvas {
        width: 100% !important;
        max-width: 100% !important;
        height: 250px !important; /* Fixed smaller height */
    }
}