* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    padding: 20px;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    background: white;
    border-radius: 20px;
    box-shadow: 0 20px 40px rgba(0,0,0,0.1);
    overflow: hidden;
}

header {
    background: linear-gradient(135deg, #2c3e50, #34495e);
    color: white;
    padding: 30px;
    text-align: center;
}

header h1 {
    font-size: 2.5em;
    margin-bottom: 10px;
}

.subtitle {
    font-size: 1.1em;
    opacity: 0.9;
}

.app-container {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 30px;
    padding: 30px;
}

.input-section, .preview-section {
    background: #f8f9fa;
    padding: 25px;
    border-radius: 15px;
    border: 1px solid #e9ecef;
}

.input-section h2, .preview-section h2 {
    color: #2c3e50;
    margin-bottom: 20px;
    padding-bottom: 10px;
    border-bottom: 2px solid #4a90e2;
}

.form-group {
    margin-bottom: 15px;
}

.form-group label {
    display: block;
    margin-bottom: 6px;
    font-weight: 600;
    color: #495057;
}

.form-group input {
    width: 100%;
    padding: 10px;
    border: 2px solid #e9ecef;
    border-radius: 6px;
    font-size: 14px;
    transition: border-color 0.3s;
}

.form-group input:focus {
    outline: none;
    border-color: #4a90e2;
}

.upload-area {
    border: 2px dashed #dee2e6;
    border-radius: 6px;
    padding: 20px;
    text-align: center;
    cursor: pointer;
    transition: all 0.3s;
    background: #f8f9fa;
}

.upload-area:hover {
    border-color: #4a90e2;
    background: #e3f2fd;
}

.upload-icon {
    font-size: 1.8em;
    margin-bottom: 8px;
}

.upload-area p {
    color: #6c757d;
    margin: 0;
    font-size: 14px;
}

.btn {
    background: #4a90e2;
    color: white;
    border: none;
    padding: 10px 16px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    transition: all 0.3s;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.btn:hover {
    background: #357abd;
    transform: translateY(-1px);
    box-shadow: 0 3px 10px rgba(0,0,0,0.2);
}

.btn-generate { background: #28a745; }
.btn-generate:hover { background: #218838; }

.btn-print { background: #17a2b8; }
.btn-print:hover { background: #138496; }

.btn-save { background: #ffc107; color: #212529; }
.btn-save:hover { background: #e0a800; }

.btn-clear { background: #dc3545; }
.btn-clear:hover { background: #c82333; }

.btn-view { background: #6f42c1; }
.btn-view:hover { background: #5a2d91; }

.camera-btn {
    width: 100%;
    margin-top: 8px;
    background: #6c757d;
    font-size: 13px;
    padding: 8px 12px;
}

.camera-btn:hover {
    background: #5a6268;
}

/* Card Styles */
.card-container {
    perspective: 1000px;
    margin-bottom: 15px;
}

.card {
    width: 360px;
    height: 200px;
    position: relative;
    transform-style: preserve-3d;
    transition: transform 0.6s;
    cursor: pointer;
    margin: 0 auto;
}

.card.flipped {
    transform: rotateY(180deg);
}

.card-front,
.card-back {
    position: absolute;
    width: 100%;
    height: 100%;
    backface-visibility: hidden;
    border-radius: 10px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    background: white;
    padding: 12px;
    display: flex;
    flex-direction: column;
    border: 1px solid #e0e0e0;
}

.card-back {
    transform: rotateY(180deg);
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}

.backside-content {
    width: 100%;
    height: 100%;
    position: relative;
}

.backside-template {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 6px;
}

.school-watermark {
    position: absolute;
    bottom: 8px;
    right: 8px;
    width: 50px;
    height: 50px;
    opacity: 0.3;
}

/* Enhanced Card Header with Color */
.card-header {
    display: flex;
    align-items: center;
    margin-bottom: 6px;
    padding: 6px 8px;
    border-radius: 6px;
    background: linear-gradient(135deg, #4a90e2, #357abd);
    color: white;
    min-height: 45px;
}

.header-center {
    text-align: center;
    flex-grow: 1;
    margin: 0 8px;
}

.school-name {
    font-size: 14px;
    font-weight: bold;
    text-align: center;
}

.card-identifier {
    font-size: 11px;
    font-weight: bold;
    text-align: center;
    margin-top: 1px;
    opacity: 0.9;
}

.school-logo {
    width: 35px;
    height: 35px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px solid white;
    flex-shrink: 0;
}

/* FIXED: Optimized Card Body with consistent telephone positioning */
.card-body {
    display: flex;
    gap: 10px;
    flex: 1;
    padding: 2px 0;
    height: calc(100% - 55px); /* Account for header height */
}

.student-photo {
    width: 70px;
    height: 85px;
    object-fit: cover;
    border: 2px solid #4a90e2;
    border-radius: 4px;
    flex-shrink: 0;
}

.student-details {
    flex-grow: 1;
    display: flex;
    flex-direction: column;
    justify-content: flex-start; /* Changed from space-between */
    padding: 2px 0;
    height: 100%;
}

.detail-row {
    display: flex;
    align-items: center;
    margin-bottom: 2px;
    padding: 1px 4px;
    min-height: 16px;
}

.name-row {
    margin-bottom: 4px;
    border-bottom: 1px solid #eee;
    padding-bottom: 3px;
}

/* FIXED: Telephone row positioning - consistent with other fields */
.telephone-row {
    margin-top: 2px;
    padding: 3px 5px;
    border-top: 1px solid #eee;
    margin-bottom: 0;
    width: calc(100% - 10px);
    box-sizing: border-box;
    background: #f8f9fa;
    border-radius: 4px;
    border: 1px solid #dee2e6;
}

.detail-label {
    font-weight: bold;
    color: #2c3e50;
    margin-right: 6px;
    font-size: 11px;
    min-width: 55px;
}

.detail-value {
    font-size: 11px;
    color: #495057;
    font-weight: 500;
    flex: 1;
}

.name-row .detail-value {
    font-size: 12px;
    font-weight: bold;
    color: #2c3e50;
}

.telephone-label {
    color: #dc3545;
    font-weight: bold;
}

.telephone-value {
    color: #dc3545;
    font-weight: bold;
    font-size: 11px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.action-buttons {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin-top: 15px;
}

.card-flip-buttons {
    text-align: center;
    margin: 8px 0;
}

.btn-flip {
    background: #6c757d;
    color: white;
    width: 180px;
    justify-content: center;
    padding: 8px 12px;
    font-size: 13px;
}

.btn-flip:hover {
    background: #5a6268;
}

/* Modal Styles */
.crop-modal, .view-modal, .print-modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.8);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.crop-container, .view-container, .print-container {
    background: white;
    padding: 25px;
    border-radius: 12px;
    max-width: 90%;
    max-height: 90%;
    overflow-y: auto;
}

.crop-area {
    margin: 15px 0;
    text-align: center;
}

#image-to-crop {
    max-width: 400px;
    max-height: 400px;
    border: 2px solid #ddd;
    border-radius: 6px;
}

.crop-controls {
    display: flex;
    gap: 8px;
    justify-content: center;
}

.view-container {
    width: 95%;
    height: 95%;
}

.modal-controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
    padding-bottom: 12px;
    border-bottom: 2px solid #eee;
}

.selection-info {
    font-weight: bold;
    color: #495057;
    font-size: 14px;
}

.modal-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.btn-print-all {
    background: #28a745;
}

.btn-print-all:hover {
    background: #218838;
}

.btn-mark-printed {
    background: #20c997;
}

.btn-mark-printed:hover {
    background: #1ba87e;
}

.filter-controls {
    display: flex;
    gap: 8px;
    margin-bottom: 15px;
    justify-content: center;
}

.btn-filter {
    background: #6c757d;
    padding: 8px 12px;
    font-size: 13px;
}

.btn-filter.active {
    background: #4a90e2;
}

/* Improved Cards Grid */
.cards-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
    gap: 15px;
    max-height: calc(100vh - 200px);
    overflow-y: auto;
    padding: 8px;
}

.card-item {
    position: relative;
    margin-bottom: 12px;
}

.card-checkbox {
    position: absolute;
    top: 8px;
    left: 8px;
    z-index: 10;
    width: 18px;
    height: 18px;
    cursor: pointer;
}

.card-item.selected .card {
    border: 2px solid #007bff;
    box-shadow: 0 0 12px rgba(0,123,255,0.4);
}

.card-item.printed .card {
    border: 2px solid #28a745;
    background: #f8fff8;
}

.card-item.printed::after {
    content: "✅ PRINTED";
    position: absolute;
    top: 8px;
    right: 8px;
    background: #28a745;
    color: white;
    padding: 2px 6px;
    border-radius: 8px;
    font-size: 9px;
    font-weight: bold;
    z-index: 10;
}

.edit-form {
    margin-top: 8px;
    padding: 8px;
    background: #f8f9fa;
    border-radius: 6px;
    border: 1px solid #dee2e6;
}

.edit-form input {
    width: 100%;
    padding: 6px;
    margin-bottom: 6px;
    border: 1px solid #ced4da;
    border-radius: 4px;
    font-size: 12px;
}

.btn-update {
    background: #17a2b8;
    padding: 6px 12px;
    font-size: 12px;
    width: 100%;
}

.btn-update:hover {
    background: #138496;
}

/* Responsive Design */
@media (max-width: 768px) {
    .app-container {
        grid-template-columns: 1fr;
    }
    
    .card {
        width: 100%;
        max-width: 340px;
    }
    
    .action-buttons {
        grid-template-columns: 1fr;
    }
    
    .cards-grid {
        grid-template-columns: 1fr;
    }
    
    .modal-controls {
        flex-direction: column;
        gap: 8px;
    }
    
    .modal-actions {
        justify-content: center;
    }
}

/* Crop Modal Styles */
.crop-modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.9);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.crop-container {
    background: white;
    padding: 20px;
    border-radius: 12px;
    max-width: 90%;
    max-height: 90%;
    width: 700px;
    display: flex;
    flex-direction: column;
}

.crop-container h2 {
    color: #2c3e50;
    margin-bottom: 8px;
    text-align: center;
    font-size: 1.5em;
}

.crop-container p {
    text-align: center;
    color: #666;
    margin-bottom: 15px;
    font-size: 14px;
}

.crop-area {
    width: 100%;
    height: 350px;
    margin: 15px 0;
    background: #f8f9fa;
    border: 2px dashed #dee2e6;
    border-radius: 6px;
    overflow: hidden;
    position: relative;
}

#image-to-crop {
    display: block;
    max-width: 100%;
    max-height: 100%;
    margin: 0 auto;
}

.crop-controls {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.crop-actions {
    display: flex;
    gap: 8px;
    justify-content: center;
    flex-wrap: wrap;
}

.crop-buttons {
    display: flex;
    gap: 8px;
    justify-content: center;
}

.btn-rotate, .btn-reset {
    background: #6c757d;
    color: white;
    padding: 8px 12px;
    font-size: 13px;
}

.btn-rotate:hover, .btn-reset:hover {
    background: #5a6268;
}

.btn-cancel {
    background: #dc3545;
    color: white;
    padding: 10px 20px;
}

.btn-cancel:hover {
    background: #c82333;
}

.btn-apply {
    background: #28a745;
    color: white;
    padding: 10px 20px;
}

.btn-apply:hover {
    background: #218838;
}

/* Data Management Styles */
.data-management {
    margin-top: 25px;
    padding: 15px;
    background: #f8f9fa;
    border-radius: 8px;
    border: 1px solid #e9ecef;
}

.data-management h3 {
    color: #2c3e50;
    margin-bottom: 12px;
    text-align: center;
    font-size: 1.2em;
}

.data-buttons {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    margin-bottom: 8px;
}

.btn-export {
    background: #28a745;
    color: white;
}

.btn-export:hover {
    background: #218838;
}

.btn-import {
    background: #17a2b8;
    color: white;
}

.btn-import:hover {
    background: #138496;
}

.btn-export-selected {
    background: #20c997;
    color: white;
}

.btn-export-selected:hover {
    background: #1ba87e;
}

.data-management small {
    display: block;
    text-align: center;
    color: #6c757d;
    font-size: 11px;
}

/* Success/Error Messages */
.alert {
    padding: 10px 12px;
    border-radius: 4px;
    margin: 8px 0;
    text-align: center;
    font-weight: 500;
    font-size: 14px;
}

.alert-success {
    background: #d4edda;
    color: #155724;
    border: 1px solid #c3e6cb;
}

.alert-error {
    background: #f8d7da;
    color: #721c24;
    border: 1px solid #f5c6cb;
}

/* PRINT STYLES - UPDATED TO MATCH SAVED PORTAL EXACTLY */
@media print {
    /* Reset everything for printing */
    body * {
        visibility: hidden;
        margin: 0 !important;
        padding: 0 !important;
        box-shadow: none !important;
    }
    
    /* Only show card elements */
    .print-card,
    .print-card * {
        visibility: visible !important;
    }
    
    /* EXACT SAME CARD DIMENSIONS AS SAVED PORTAL */
    .print-card {
        position: relative !important;
        width: 360px !important;
        height: 200px !important; /* SAME HEIGHT AS SAVED PORTAL */
        margin: 0 auto !important;
        padding: 12px !important;
        border: 1px solid #000 !important;
        border-radius: 10px !important;
        background: white !important;
        box-shadow: none !important;
        page-break-inside: avoid !important;
        break-inside: avoid !important;
        transform: none !important;
        left: 0 !important;
        top: 0 !important;
    }
    
    /* EXACT SAME CARD HEADER AS SAVED PORTAL */
    .print-card .card-header {
        display: flex !important;
        align-items: center !important;
        margin-bottom: 6px !important;
        padding: 6px 8px !important;
        border-radius: 6px !important;
        background: linear-gradient(135deg, #4a90e2, #357abd) !important;
        color: white !important;
        min-height: 45px !important;
    }
    
    .print-card .school-name {
        font-size: 14px !important;
        font-weight: bold !important;
        text-align: center !important;
    }
    
    .print-card .card-identifier {
        font-size: 11px !important;
        font-weight: bold !important;
        text-align: center !important;
        margin-top: 1px !important;
        opacity: 0.9 !important;
    }
    
    .print-card .school-logo {
        width: 35px !important;
        height: 35px !important;
        border-radius: 50% !important;
        object-fit: cover !important;
        border: 2px solid white !important;
        flex-shrink: 0 !important;
    }
    
    /* EXACT SAME CARD BODY LAYOUT AS SAVED PORTAL */
    .print-card .card-body {
        display: flex !important;
        gap: 10px !important;
        flex: 1 !important;
        padding: 2px 0 !important;
        height: calc(100% - 55px) !important; /* SAME HEIGHT CALCULATION */
    }
    
    .print-card .student-photo {
        width: 70px !important;
        height: 85px !important;
        object-fit: cover !important;
        border: 2px solid #4a90e2 !important;
        border-radius: 4px !important;
        flex-shrink: 0 !important;
    }
    
    /* EXACT SAME STUDENT DETAILS LAYOUT AS SAVED PORTAL */
    .print-card .student-details {
        flex-grow: 1 !important;
        display: flex !important;
        flex-direction: column !important;
        justify-content: flex-start !important; /* SAME AS SAVED PORTAL */
        padding: 2px 0 !important;
        height: 100% !important;
    }
    
    .print-card .detail-row {
        display: flex !important;
        align-items: center !important;
        margin-bottom: 2px !important;
        padding: 1px 4px !important;
        min-height: 16px !important;
    }
    
    .print-card .name-row {
        margin-bottom: 4px !important;
        border-bottom: 1px solid #eee !important;
        padding-bottom: 3px !important;
    }
    
    /* EXACT SAME TELEPHONE POSITIONING AS SAVED PORTAL */
    .print-card .telephone-row {
        margin-top: 2px !important; /* SAME AS SAVED PORTAL - not auto */
        padding: 3px 5px !important;
        border-top: 1px solid #eee !important;
        margin-bottom: 0 !important;
        width: calc(100% - 10px) !important;
        box-sizing: border-box !important;
        background: #f8f9fa !important;
        border-radius: 4px !important;
        border: 1px solid #dee2e6 !important;
    }
    
    .print-card .detail-label {
        font-weight: bold !important;
        color: #2c3e50 !important;
        margin-right: 6px !important;
        font-size: 11px !important;
        min-width: 55px !important;
    }
    
    .print-card .detail-value {
        font-size: 11px !important;
        color: #495057 !important;
        font-weight: 500 !important;
        flex: 1 !important;
    }
    
    .print-card .name-row .detail-value {
        font-size: 12px !important;
        font-weight: bold !important;
        color: #2c3e50 !important;
    }
    
    .print-card .telephone-label {
        color: #dc3545 !important;
        font-weight: bold !important;
    }
    
    .print-card .telephone-value {
        color: #dc3545 !important;
        font-weight: bold !important;
        font-size: 11px !important;
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
    }
    
    .print-card .backside-content {
        width: 100% !important;
        height: 100% !important;
        position: relative !important;
    }
    
    .print-card .backside-template {
        width: 100% !important;
        height: 100% !important;
        object-fit: cover !important;
        border-radius: 6px !important;
    }
    
    .print-card .school-watermark {
        position: absolute !important;
        bottom: 8px !important;
        right: 8px !important;
        width: 50px !important;
        height: 50px !important;
        opacity: 0.3 !important;
    }
    
    /* Multiple cards printing layout */
    .multi-card-page {
        page-break-after: always !important;
        display: grid !important;
        grid-template-columns: repeat(2, 360px) !important;
        grid-template-rows: repeat(5, 200px) !important; /* SAME HEIGHT AS SAVED PORTAL */
        gap: 10px !important;
        padding: 10px !important;
        justify-content: center !important;
        align-content: start !important;
        width: 100% !important;
        min-height: 100vh !important;
    }
    
    .multi-card-page .print-card {
        width: 360px !important;
        height: 200px !important; /* SAME HEIGHT AS SAVED PORTAL */
        margin: 0 !important;
    }
    
    /* Single card printing */
    .single-card-page {
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        height: 100vh !important;
        width: 100vw !important;
    }
    
    .single-card-page .print-card {
        transform: scale(1) !important;
        margin: 0 auto !important;
    }
    
    /* Force colors to print */
    * {
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
        color-adjust: exact !important;
    }
    
    .card-header {
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
        background: linear-gradient(135deg, #4a90e2, #357abd) !important;
    }
    
    .telephone-row {
        background: #f8f9fa !important;
        border: 1px solid #dee2e6 !important;
    }
    
    @page {
        margin: 10mm !important;
        size: A4 landscape !important;
    }
}

/* Print layout containers */
.print-multiple-container {
    display: none;
}

@media print {
    .print-multiple-container {
        display: block !important;
    }
}

/* NEW: Print Modal Styles */
.print-modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.8);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.print-container {
    background: white;
    padding: 25px;
    border-radius: 12px;
    max-width: 400px;
    width: 90%;
    text-align: center;
}

.print-options {
    margin: 20px 0;
    text-align: left;
}

.print-option {
    margin: 15px 0;
    padding: 15px;
    border: 2px solid #e9ecef;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s;
}

.print-option:hover {
    border-color: #4a90e2;
    background: #f8f9fa;
}

.print-option input[type="radio"] {
    display: none;
}

.print-option input[type="radio"]:checked + label {
    color: #4a90e2;
    font-weight: bold;
}

.print-option label {
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
    margin: 0;
}

.print-icon {
    font-size: 1.2em;
}

.print-text {
    font-size: 16px;
}

.print-buttons {
    display: flex;
    gap: 10px;
    justify-content: center;
}

.btn-print-confirm {
    background: #28a745;
    color: white;
}

.btn-print-confirm:hover {
    background: #218838;
}

/* NEW: Export Selected Section Styles */
.export-selected-section {
    display: flex;
    gap: 8px;
}

/* NEW: Imported Cards Filter */
.btn-filter[data-filter="imported"] {
    background: #6f42c1;
}

.btn-filter[data-filter="imported"].active {
    background: #5a2d91;
}

/* Enhanced Modal Controls for New Buttons */
.modal-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    align-items: center;
}

.modal-actions .btn {
    padding: 8px 12px;
    font-size: 13px;
    white-space: nowrap;
}

/* Card Import Status */
.card-item.imported .card {
    border: 2px solid #6f42c1;
}

.card-item.imported::before {
    content: "📥 IMPORTED";
    position: absolute;
    top: 8px;
    right: 8px;
    background: #6f42c1;
    color: white;
    padding: 2px 6px;
    border-radius: 8px;
    font-size: 9px;
    font-weight: bold;
    z-index: 10;
}