/* --- 1. MODERN BASE STYLES & VARIABLES --- */
:root {
    /* Colors */
    --primary-color: #2563eb;
    --primary-hover: #1d4ed8;
    --danger-color: #ef4444;
    --danger-hover: #dc2626;
    --bg-color: #f1f5f9;
    --paper-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 10px 10px -5px rgba(0, 0, 0, 0.04);
    
    /* Input Styles */
    --input-bg: rgba(255, 255, 255, 0.6);
    --input-focus-ring: rgba(37, 99, 235, 0.2);
    --active-block-bg: rgba(37, 99, 235, 0.05);

    /* --- PRN CALIBRATION VARIABLES (Based on your Two Start Date Data) --- */
    /* Anchor: 230.0002mm from bottom = 67.1998mm from top */
    --prn-start-top: 67.1998mm; 
    /* Step: 230.0002 - 193.9999 = 36.0003mm */
    --prn-step: 36.0003mm;         
}

body {
    margin: 0;
    padding: 40px 20px;
    background-color: var(--bg-color);
    font-family: 'Inter', sans-serif;
    color: #334155;
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100vh;
}

/* --- 2. HEADER & CONTROLS --- */
.no-print {
    width: 100%;
    max-width: 210mm;
    margin-bottom: 30px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: white;
    padding: 20px 30px;
    border-radius: 12px;
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
    box-sizing: border-box;
    position: sticky;
    top: 20px;
    z-index: 1000;
}

.app-title h1 { margin: 0; font-size: 20px; font-weight: 600; color: #0f172a; }

/* Chart Type Toggle */
.chart-toggle {
    display: flex;
    background: #e2e8f0;
    padding: 4px;
    border-radius: 8px;
    margin-top: 8px;
    width: fit-content;
}

.toggle-btn {
    padding: 6px 16px;
    font-size: 13px;
    font-weight: 500;
    border-radius: 6px;
    background: transparent;
    color: #64748b;
    border: none;
    cursor: pointer;
    transition: all 0.2s;
}

.toggle-btn.active {
    background: white;
    color: var(--primary-color);
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    font-weight: 600;
}

.toggle-btn:hover:not(.active) {
    color: #334155;
    background: rgba(255,255,255,0.5);
}

.controls-group { display: flex; gap: 12px; align-items: center; }

button {
    border: none;
    padding: 10px 20px;
    font-size: 14px;
    font-weight: 600;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    gap: 8px;
    white-space: nowrap;
}

button.secondary-btn {
    background-color: white;
    color: #334155;
    border: 1px solid #e2e8f0;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}
button.secondary-btn:hover { background-color: #f8fafc; border-color: #cbd5e1; }

button.danger-btn {
    background-color: #fef2f2;
    color: var(--danger-color);
    border: 1px solid #fee2e2;
}
button.danger-btn:hover { background-color: #fee2e2; border-color: #fca5a5; }

button.print-btn {
    background-color: var(--primary-color);
    color: white;
    box-shadow: 0 4px 6px -1px rgba(37, 99, 235, 0.2);
}
button.print-btn:hover { background-color: var(--primary-hover); transform: translateY(-1px); }

/* Dropdown Styles */
.dropdown { position: relative; display: inline-block; }

.icon-btn {
    background: white;
    border: 1px solid #e2e8f0;
    color: #334155;
    padding: 10px;
    border-radius: 8px;
    cursor: pointer;
}
.icon-btn:hover, .icon-btn:focus { background: #f8fafc; border-color: #cbd5e1; }

.dropdown-content {
    display: none;
    position: absolute;
    right: 0;
    top: 110%;
    background-color: white;
    min-width: 180px;
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    border: 1px solid #e2e8f0;
    border-radius: 8px;
    z-index: 2000;
    overflow: hidden;
    padding: 4px 0;
}

.dropdown-content.show { display: block; animation: fadeIn 0.2s ease; }

.dropdown-content a {
    color: #334155;
    padding: 10px 16px;
    text-decoration: none;
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    font-weight: 500;
}

.dropdown-content a:hover { background-color: #f8fafc; }
.dropdown-content a.text-danger { color: var(--danger-color); }
.dropdown-content a.text-danger:hover { background-color: #fef2f2; }

.dropdown-divider {
    height: 1px;
    background-color: #e2e8f0;
    margin: 4px 0;
}

@keyframes fadeIn { from { opacity: 0; transform: translateY(-5px); } to { opacity: 1; transform: translateY(0); } }

/* Remove Page Button */
.remove-page-btn {
    position: absolute;
    top: 10px;
    right: -50px;
    background-color: white;
    color: var(--danger-color);
    border: 1px solid #fee2e2;
    padding: 8px;
    border-radius: 50%;
    box-shadow: 0 2px 4px rgba(0,0,0,0.05);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
}
.remove-page-btn:hover {
    background-color: #fef2f2;
    transform: scale(1.1);
    border-color: var(--danger-color);
}

/* --- 3. THE PAPER (Canvas) --- */
.page-wrapper {
    position: relative;
    margin-bottom: 40px;
}

.page {
    position: relative;
    width: 210mm;
    height: 297.2mm;
    background-color: white;
    background-image: url('background.jpg'); 
    background-size: 100% 100%;
    background-repeat: no-repeat;
    background-position: 0.7mm 1.7mm;
    box-shadow: var(--paper-shadow);
    border-radius: 2px;
}

/* --- 4. INPUT FIELDS --- */
input {
    position: absolute;
    background-color: var(--input-bg);
    border: 1px solid transparent;
    border-bottom: 1px solid rgba(0,0,0,0.1);
    border-radius: 4px;
    transition: border-color 0.2s, box-shadow 0.2s, background-color 0.2s;
    font-family: 'Roboto Mono', 'Consolas', monospace;
    font-size: 11pt; 
    color: #1e293b;
    font-weight: 600;
    text-transform: uppercase;
    padding: 0 4px; 
    box-sizing: border-box;
    z-index: 10;
    white-space: nowrap;
    overflow: hidden;
    line-height: normal; 
    display: flex;
    align-items: center;
}

input:hover { background-color: rgba(255, 255, 255, 0.9); border-color: #94a3b8; }
input:focus {
    background-color: #ffffff;
    border-color: var(--primary-color);
    outline: none;
    box-shadow: 0 0 0 3px var(--input-focus-ring);
}
input::placeholder {
    color: #94a3b8; font-weight: 400; font-size: 10pt;
    font-family: 'Inter', sans-serif; opacity: 0.6; text-transform: none;
}

/* Updated: Supports both Regular (.med-block) and PRN (.prn-block) */
div[class^="med-block"].active-row, div[class^="prn-block"].active-row {
    background-color: var(--active-block-bg);
    border-radius: 4px;
    box-shadow: 0 0 0 2px var(--active-block-bg); 
}

/* ========================================= */
/* REGULAR CHART CONFIGURATION        */
/* ========================================= */

.med-block-1 { top: 116.1999mm; } 
.med-block-2 { top: 150.2mm; } 
.med-block-3 { top: 184.2mm; }
.med-block-4 { top: 218.2mm; }
.med-block-5 { top: 252.2mm; } 

div[class^="med-block"] {
    position: absolute; 
    width: 100%; 
    height: 34mm;
    transition: background-color 0.2s;
}

/* Regular Fields */
.page-regular .field-start-date { left: 16mm; top: 0mm; width: 22.2055mm; height: 7.7611mm; }
.page-regular .autocomplete-wrapper { left: 40.9998mm; top: 0mm; width: 52.9167mm; height: 7.7611mm; }
.page-regular .field-medicine { position: relative; left: 0; top: 0; width: 100%; height: 100%; }
.page-regular .field-route { left: 16mm; top: 9.0001mm; width: 21.9748mm; height: 7.7611mm; }
.page-regular .field-dose { left: 40.9998mm; top: 9.0001mm; width: 43.4495mm; height: 7.7611mm; }
.page-regular .field-freq { left: 85mm; top: 9.0001mm; width: 23.822mm; height: 7.7611mm; }

.time-stack-container { position: absolute; left: 110mm; top: 0mm; width: 11.3527mm; height: 100%; }

.time-box { 
    position: absolute; left: 0; width: 100%; height: 5.2211mm; 
    font-size: 9px; 
    text-align: center; padding: 0; 
    display: flex; align-items: center; justify-content: center;
}
.t-0800 { top: 4.0002mm; }  
.t-1200 { top: 10.0002mm; } 
.t-1800 { top: 16.0002mm; } 
.t-2200 { top: 22.0002mm; }

/* ========================================= */
/* PRN CHART CONFIGURATION          */
/* ========================================= */

/* Page Background for PRN */
.page.page-prn {
    /* Use PRN Background if you upload it as medChartPRN.jpg */
    background-image: url('medChartPRN.jpg'); 
    background-position: -2mm -1mm; 
}

/* Blocks (Using CSS Variables for easy tuning) */
.prn-block-1 { position: absolute; width: 100%; height: 36mm; top: var(--prn-start-top); }
.prn-block-2 { position: absolute; width: 100%; height: 36mm; top: calc(var(--prn-start-top) + var(--prn-step)); }
.prn-block-3 { position: absolute; width: 100%; height: 36mm; top: calc(var(--prn-start-top) + (var(--prn-step) * 2)); }
.prn-block-4 { position: absolute; width: 100%; height: 36mm; top: calc(var(--prn-start-top) + (var(--prn-step) * 3)); }
.prn-block-5 { position: absolute; width: 100%; height: 36mm; top: calc(var(--prn-start-top) + (var(--prn-step) * 4)); }
.prn-block-6 { position: absolute; width: 100%; height: 36mm; top: calc(var(--prn-start-top) + (var(--prn-step) * 5)); }


/* --- PRN UI FIX: CLEANER INPUTS --- */
.page-prn input {
    /* Make background transparent so grid lines show through */
    background-color: rgba(255, 255, 255, 0.2); 
    /* Hide borders by default so overlap isn't visible */
    border: 1px solid transparent; 
    border-bottom: 1px solid transparent; 
    
    /* VITAL: Reset display to block to allow padding hacks */
    display: block;
    
    /* VITAL: Anchor text to the top-left (matches print logic) */
    /* This creates a "safe zone" 1.5mm from the top border before text starts */
    padding-top: 1.5mm; 
    padding-left: 1mm;
}

.page-prn input:focus {
    background-color: #ffffff;
    border: 1px solid var(--primary-color);
    box-shadow: 0 0 0 2px var(--input-focus-ring);
    /* When focused, we can show the full box size, or keep it small */
}

.page-prn input:hover {
    background-color: rgba(255, 255, 255, 0.6);
    border-bottom: 1px solid #cbd5e1;
}


/* --- PRN FIELD POSITIONS (Exact Millimeters) --- */
/* Note: I have slightly REDUCED the width/height here to fix the overlap 
   visuals, but kept 'top' and 'left' the same to preserve start position. */

/* Row 1: Date & Med */
.page-prn .field-start-date { 
    left: 7mm; top: 0mm; 
    width: 18mm; /* Reduced from 18.28mm to fit grid */
    height: 6.5mm; /* Reduced from 7.76mm to fit grid height */
}
.page-prn .autocomplete-wrapper { 
    left: 26mm; top: 0mm; 
    width: 55mm; /* Reduced from 55.9mm */
    height: 6.5mm; 
}
.page-prn .field-medicine { 
    position: relative; left: 0; top: 0; width: 100%; height: 100%; 
    padding-top: 1.5mm; /* Ensure inner input matches padding logic */
}

/* Row 2: Route, Dose, Freq */
.page-prn .field-route { 
    left: 7mm; top: 7mm; 
    width: 16mm; /* Reduced from 16.89mm */
    height: 6.5mm; 
}
.page-prn .field-dose { 
    left: 26mm; top: 7mm; 
    width: 32mm; /* Reduced from 33.2mm */
    height: 6.5mm; 
}
.page-prn .field-freq { 
    left: 69.9999mm; top: 7mm; 
    width: 12mm; /* Reduced from 22.2mm */
    height: 6.5mm;
}

/* Row 3: Max Dose */
.page-prn .field-max-dose {
    position: absolute;
    left: 43.9999mm; top: 14mm; 
    width: 36mm; /* Reduced from 37.9mm */
    height: 6.5mm;
    
    /* Remove flex center to use padding logic */
    display: block; 
    padding-top: 1.5mm;
    
    background-color: var(--input-bg); border-radius: 4px; 
    font-family: 'Roboto Mono', monospace; font-size: 11pt; font-weight: 600; 
    text-transform: uppercase; 
    white-space: nowrap; overflow: hidden;
}

/* ========================================= */
/* SHARED UI COMPONENTS          */
/* ========================================= */

/* --- 6. AUTOCOMPLETE --- */
.autocomplete-list {
    position: absolute; top: calc(100% + 4px); left: 0; right: 0; z-index: 100;
    background-color: white; border-radius: 8px;
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
    border: 1px solid #e2e8f0; 
    max-height: 250px; 
    overflow-y: auto;
    font-family: 'Inter', sans-serif; font-size: 13px; text-transform: none;
    
    /* Clean scrolling */
    overflow-x: hidden;
    padding-right: 0;
    scrollbar-width: thin; 
    scrollbar-color: #cbd5e1 #f1f5f9; 
}

.autocomplete-list::-webkit-scrollbar { width: 6px; height: 6px; }
.autocomplete-list::-webkit-scrollbar-track { background: transparent; margin: 4px 0; }
.autocomplete-list::-webkit-scrollbar-thumb { background-color: #cbd5e1; border-radius: 10px; border: 2px solid transparent; background-clip: content-box; }
.autocomplete-list::-webkit-scrollbar-thumb:hover { background-color: #94a3b8; }

.autocomplete-item { padding: 10px 12px; cursor: pointer; background-color: #fff; border-bottom: 1px solid #f1f5f9; color: #334155; }
.autocomplete-item:hover { background-color: #f8fafc; }
.autocomplete-item strong { color: var(--primary-color); font-weight: 600; }
.note-text { display: block; font-size: 11px; color: #94a3b8; margin-top: 3px; font-weight: 400; }
.autocomplete-active { background-color: var(--primary-color) !important; color: #ffffff !important; }
.autocomplete-active strong { color: white; }
.autocomplete-active .note-text { color: rgba(255,255,255,0.8); }

.autocomplete-wrapper {
    position: absolute; left: 40.9998mm; top: 0mm; 
    width: 52.9167mm; height: 7.7611mm;
}
.autocomplete-wrapper input { position: relative; left: 0; top: 0; width: 100%; height: 100%; }

/* --- 7. MODAL STYLES --- */
.modal-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(255, 255, 255, 0.8); z-index: 2000;
    display: none; justify-content: center; align-items: center;
    backdrop-filter: blur(5px);
}
.modal {
    background: white; padding: 24px 32px; border-radius: 12px;
    box-shadow: 0 20px 25px -5px rgba(0, 0, 0, 0.1), 0 0 0 1px rgba(0,0,0,0.05);
    text-align: center; width: 320px;
}
.modal h3 { margin: 0 0 10px 0; color: #1e293b; }
.modal p { margin: 0 0 20px 0; color: #64748b; font-size: 14px; }
.modal-actions { display: flex; gap: 12px; justify-content: center; }
.btn-cancel { background: #f1f5f9; color: #334155; padding: 8px 16px; border-radius: 6px; cursor: pointer; }
.btn-confirm { background: var(--danger-color); color: white; padding: 8px 16px; border-radius: 6px; cursor: pointer; }
.btn-confirm:hover { background: var(--danger-hover); }

/* --- 8. LOGIN OVERLAY STYLES --- */
#login-overlay {
    background: rgba(241, 245, 249, 0.95);
    z-index: 9999;
    display: flex;
}

.login-card {
    background: white;
    padding: 40px;
    border-radius: 16px;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    width: 100%;
    max-width: 360px;
    text-align: center;
}

.login-card h2 { margin-top: 0; color: #1e293b; }
.login-card input {
    position: relative;
    width: 100%;
    margin-bottom: 15px;
    padding: 12px;
    border: 1px solid #cbd5e1;
    background: #fff;
    font-family: 'Inter', sans-serif;
    text-transform: none;
    font-weight: 400;
    border-radius: 8px;
    box-sizing: border-box;
}
.login-btn {
    width: 100%;
    padding: 12px;
    background-color: var(--primary-color);
    color: white;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
    margin-top: 10px;
    display: flex;
    justify-content: center;
    align-items: center;
}
.login-btn:disabled {
    background-color: #93c5fd;
    cursor: not-allowed;
}
.login-btn:hover:not(:disabled) { background-color: var(--primary-hover); }
.error-msg { color: var(--danger-color); font-size: 13px; margin-top: 10px; display: none; }

/* --- SPINNER ANIMATION --- */
.spinner {
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 2px solid rgba(255,255,255,0.3);
    border-radius: 50%;
    border-top-color: #fff;
    animation: spin 1s ease-in-out infinite;
    margin-right: 8px;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* --- 9. TOAST --- */
.toast {
    visibility: hidden;
    min-width: 250px;
    background-color: #1e293b;
    color: #fff;
    text-align: center;
    border-radius: 8px;
    padding: 12px;
    position: fixed;
    z-index: 3000;
    left: 50%;
    bottom: 30px;
    transform: translateX(-50%);
    box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.2);
    font-size: 14px;
    opacity: 0;
    transition: opacity 0.3s, bottom 0.3s;
}
.toast.show { visibility: visible; opacity: 1; bottom: 40px; }

/* --- 10. PRINT SETTINGS --- */
@media print {
    @page { margin: 0; size: A4; }
    body { margin: 0; padding: 0; background-color: white; display: block; }
    .no-print, .remove-page-btn, .modal-overlay, .toast, #login-overlay { display: none !important; }
    .page { 
        background-image: none !important; box-shadow: none; border-radius: 0; 
        width: 210mm; height: 296mm; margin: 0; 
        position: relative; overflow: hidden; page-break-after: always; break-after: page;
    }
    div[class^="med-block"].active-row, div[class^="prn-block"].active-row { background-color: transparent !important; box-shadow: none !important; }
    .page:last-child { page-break-after: auto; }
    input { border: none !important; background-color: transparent !important; box-shadow: none !important; color: black; }
    input::placeholder { color: transparent; }
    .autocomplete-list { display: none !important; }
}