/* ==========================================
   INTERNAL AUDIT MANAGEMENT SYSTEM - STYLES
   ========================================== */

/* === BASE STYLES === */
* {
    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;
}

/* === LAYOUT === */
.container {
    max-width: 1200px;
    margin: 0 auto;
    background: white;
    border-radius: 15px;
    box-shadow: 0 10px 40px rgba(0,0,0,0.2);
    overflow: hidden;
}

.content {
    padding: 30px;
}

/* === HEADER === */
.header {
    background: linear-gradient(135deg, #2d3748 0%, #1a202c 100%);
    color: white;
    padding: 30px;
    text-align: center;
}

.header h1 {
    font-size: 2em;
    margin-bottom: 10px;
}

.org-info {
    font-size: 0.9em;
    opacity: 0.9;
    margin-top: 10px;
}

/* === TABS === */
.tabs {
    display: flex;
    background: #f7fafc;
    border-bottom: 2px solid #e2e8f0;
}

.tab {
    flex: 1;
    padding: 15px 20px;
    text-align: center;
    cursor: pointer;
    background: transparent;
    border: none;
    font-size: 1em;
    font-weight: 600;
    color: #4a5568;
    transition: all 0.3s;
}

.tab:hover {
    background: #edf2f7;
}

.tab.active {
    background: white;
    color: #667eea;
    border-bottom: 3px solid #667eea;
}

/* === TAB CONTENT === */
.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
    animation: fadeIn 0.3s;
}

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

/* === CARDS === */
.card {
    background: #f7fafc;
    padding: 25px;
    border-radius: 10px;
    margin-bottom: 20px;
    border: 1px solid #e2e8f0;
}

.card h3 {
    color: #2d3748;
    margin-bottom: 15px;
    font-size: 1.3em;
}

/* === FORMS === */
.form-group {
    margin-bottom: 20px;
}

/* Grid wrapper for two-column layout on larger screens */
.form-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 20px;
}

/* Tablet and up: two columns */
@media (min-width: 768px) {
    .form-grid {
        grid-template-columns: 1fr 1fr;
        align-items: start;
    }
}

label {
    display: block;
    margin-bottom: 8px;
    color: #4a5568;
    font-weight: 600;
}

input[type="text"],
input[type="number"],
input[type="date"],
select,
textarea {
    width: 100%;
    padding: 12px;
    border: 2px solid #e2e8f0;
    border-radius: 8px;
    font-size: 1em;
    transition: border-color 0.3s;
}

input:focus,
select:focus,
textarea:focus {
    outline: none;
    border-color: #667eea;
}

textarea {
    resize: vertical;
    min-height: 80px;
}

/* === BUTTONS === */
.btn {
    padding: 12px 25px;
    border: none;
    border-radius: 8px;
    font-size: 1em;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    margin-right: 10px;
}

.btn-primary {
    background: #667eea;
    color: white;
}

.btn-primary:hover {
    background: #5568d3;
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}

.btn-success {
    background: #48bb78;
    color: white;
}

.btn-success:hover {
    background: #38a169;
    transform: translateY(-2px);
}

.btn-secondary {
    background: #718096;
    color: white;
}

.btn-secondary:hover {
    background: #4a5568;
    transform: translateY(-2px);
}

/* Process buttons used in Checklists tab */
.process-btn {
    background: #edf2f7;
    color: #2d3748;
    border: 1px solid #e2e8f0;
    padding: 8px 12px;
    border-radius: 8px;
    cursor: pointer;
}

.process-btn:hover {
    background: #e6eef9;
}

.process-btn-selected {
    background: #667eea;
    color: white;
    border-color: #667eea;
    box-shadow: 0 6px 18px rgba(102,126,234,0.18);
}

.btn-danger {
    background: #f56565;
    color: white;
}

.btn-danger:hover {
    background: #e53e3e;
}

/* === PROCESS LIST === */
.process-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 15px;
    margin-top: 20px;
}

.process-item {
    background: white;
    padding: 20px;
    border-radius: 8px;
    border: 2px solid #e2e8f0;
    cursor: pointer;
    transition: all 0.3s;
    text-align: center;
}

.process-item:hover {
    border-color: #667eea;
    transform: translateY(-5px);
    box-shadow: 0 5px 15px rgba(0,0,0,0.1);
}

.process-item.selected {
    background: #667eea;
    color: white;
    border-color: #667eea;
}

/* === CHECKLIST TABLE === */
.checklist-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 20px;
}

.checklist-table th,
.checklist-table td {
    padding: 12px;
    text-align: left;
    border-bottom: 1px solid #e2e8f0;
}

.checklist-table th {
    background: #2d3748;
    color: white;
    font-weight: 600;
}

.checklist-table tr:hover {
    background: #f7fafc;
}

/* === AUDIT CHECKLIST TABLE === */
.audit-checklist-table {
    width: 100%;
    border-collapse: collapse;
    background: white;
}

.audit-checklist-table th,
.audit-checklist-table td {
    padding: 10px;
    text-align: left;
    border: 1px solid #e2e8f0;
}

.audit-checklist-table th {
    background: #2d3748;
    color: white;
    font-weight: 600;
    text-align: center;
}

.audit-checklist-table tbody tr:hover {
    background: #f7fafc;
}

.table-responsive {
    overflow-x: auto;
    border-radius: 8px;
    border: 1px solid #e2e8f0;
}

/* Table Input Styles */
.table-input {
    width: 100%;
    padding: 8px;
    border: 1px solid #e2e8f0;
    border-radius: 4px;
    font-size: 0.95em;
}

.table-input:focus {
    outline: none;
    border-color: #667eea;
}

.table-textarea {
    width: 100%;
    padding: 8px;
    border: 1px solid #e2e8f0;
    border-radius: 4px;
    font-size: 0.9em;
    font-family: inherit;
    resize: vertical;
}

.table-textarea:focus {
    outline: none;
    border-color: #667eea;
}

.btn-small {
    padding: 5px 10px;
    border: none;
    border-radius: 4px;
    font-size: 0.85em;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

input[type="radio"] {
    cursor: pointer;
    width: 16px;
    height: 16px;
}

.evaluation-header {
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    margin-top: 6px;
    font-size: 1.1em;
}

.evaluation-options {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    min-height: 100%;
}

.evaluation-options label {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 8px;
    font-weight: 600;
    cursor: pointer;
}

.evaluation-options label span {
    font-size: 1.2em;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 1.6em;
}

.evaluation-options label input[type="radio"] {
    margin: 0;
}

.evaluation-options label span {
    font-size: 1.1em;
}

/* === CHECKLIST ITEMS === */
.checklist-item {
    background: white;
    padding: 15px;
    border-radius: 8px;
    margin-bottom: 15px;
    border: 1px solid #e2e8f0;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.checklist-item.editing {
    border-color: #667eea;
    background: #ebf4ff;
    box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
}

.checklist-item-content {
    flex: 1;
}

.checklist-item-actions {
    display: flex;
    gap: 10px;
}

/* === ALERTS === */
.alert {
    padding: 15px;
    border-radius: 8px;
    margin-bottom: 20px;
    animation: slideIn 0.3s;
}

@keyframes slideIn {
    from { transform: translateX(-100%); opacity: 0; }
    to { transform: translateX(0); opacity: 1; }
}

.alert-success {
    background: #c6f6d5;
    color: #22543d;
    border-left: 4px solid #48bb78;
}

.alert-info {
    background: #bee3f8;
    color: #2c5282;
    border-left: 4px solid #4299e1;
}

/* ==========================
   Mobile Optimizations
   ========================== */
/* Prefer mobile-first: tighten spacing, improve touch targets, reduce animations */
@media (max-width: 767px) {
    body {
        padding: 10px;
        -webkit-font-smoothing: antialiased;
    }

    .container {
        margin: 0; /* full-bleed on mobile */
        border-radius: 10px;
        box-shadow: 0 6px 18px rgba(0,0,0,0.12);
    }

    .content {
        padding: 16px;
    }

    .header {
        padding: 18px;
    }

    .header h1 {
        font-size: 1.25em;
    }

    .tabs {
        display: flex;
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .tab {
        flex: 0 0 auto;
        padding: 12px 14px;
        font-size: 0.95em;
    }

    .card {
        padding: 16px;
        margin-bottom: 14px;
    }

    .form-grid {
        grid-template-columns: 1fr;
    }

    input[type="text"],
    input[type="number"],
    input[type="date"],
    select,
    textarea,
    .btn {
        font-size: 1em;
    }

    .btn {
        padding: 12px 16px;
        min-height: 44px; /* comfortable touch target */
    }

    .process-list {
        grid-template-columns: 1fr; /* stack processes vertically */
    }

    .checklist-item {
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
    }

    .checklist-item-actions {
        width: 100%;
        justify-content: flex-end;
    }

    .table-responsive {
        border-radius: 8px;
        margin-bottom: 8px;
    }

    .audit-checklist-table th,
    .audit-checklist-table td {
        padding: 8px;
        font-size: 0.9em;
    }

    .evaluation-header {
        display: flex;
        flex-direction: row;
        gap: 8px;
    }

    /* Transform audit checklist table into stacked cards on mobile */
    .audit-checklist-table thead { display: none; }
    .audit-checklist-table, .audit-checklist-table tbody, .audit-checklist-table tr, .audit-checklist-table td {
        display: block;
        width: 100%;
    }

    .audit-checklist-table tr {
        margin-bottom: 12px;
        border: 1px solid #e2e8f0;
        border-radius: 8px;
        padding: 10px;
        background: #fff;
    }

    /* Alternate backgrounds and accent border for easier scanning on mobile */
    .audit-checklist-table tr:nth-child(odd) {
        background: linear-gradient(180deg, #ffffff, #fbfdff);
    }

    .audit-checklist-table tr:nth-child(even) {
        background: #f8fbff;
    }

    .audit-checklist-table tr:nth-child(4n+1) { border-left: 6px solid #667eea; }
    .audit-checklist-table tr:nth-child(4n+2) { border-left: 6px solid #48bb78; }
    .audit-checklist-table tr:nth-child(4n+3) { border-left: 6px solid #f6ad55; }
    .audit-checklist-table tr:nth-child(4n)   { border-left: 6px solid #ed64a6; }

    .audit-checklist-table td {
        padding: 8px 12px;
        position: relative;
        text-align: left;
        border: none;
        display: block;
    }

    /* Place label above content for better mobile readability */
    .audit-checklist-table td:before {
        display: block;
        position: static;
        content: attr(data-label);
        font-weight: 700;
        color: #4a5568;
        margin-bottom: 6px;
    }

    .audit-checklist-table td:nth-child(1):before { content: "Clause No."; }
    .audit-checklist-table td:nth-child(2):before { content: "Clause Title"; }
    .audit-checklist-table td:nth-child(3):before { content: "Question"; }
    .audit-checklist-table td:nth-child(4):before { content: "Evaluation"; }
    .audit-checklist-table td:nth-child(5):before { content: "Notes"; }

    /* Ensure inner elements take full width */
    .audit-checklist-table td > * { margin: 0; width: 100%; }

    /* Make notes textarea taller for easier input on mobile */
    .audit-checklist-table td textarea, .table-textarea {
        min-height: 100px;
    }

    /* Fix Evaluation cell spacing: show radios inline and remove large gaps */
    .audit-checklist-table td:nth-child(4) {
        display: flex;
        flex-direction: row;
        align-items: center;
        gap: 12px;
        padding-top: 6px;
    }

    .audit-checklist-table td:nth-child(4) > * {
        width: auto; /* allow inner controls to size naturally */
        margin: 0;
    }

    /* Make evaluation options inline and compact */
    .audit-checklist-table td:nth-child(4) .evaluation-options,
    .evaluation-options {
        flex-direction: row;
        align-items: center;
        gap: 8px;
        min-height: auto;
    }

    .audit-checklist-table td:nth-child(4) .evaluation-options label {
        font-weight: 600;
        gap: 6px;
    }

    input[type="radio"] {
        width: 18px;
        height: 18px;
        margin: 0;
    }

    /* Mobile audit summary row (visible on small screens) */
    .audit-summary-mobile {
        display: flex;
        justify-content: space-between;
        gap: 10px;
        background: linear-gradient(180deg, #ffffff, #f7fbff);
        border: 1px solid #e2eef9;
        padding: 10px 12px;
        border-radius: 10px;
        margin-bottom: 12px;
        align-items: center;
        font-weight: 600;
        color: #1a202c;
    }

    /* Make top tabs 2x2 grid on small screens for easier tapping */
    .tabs { overflow: visible; }
    .tabs .tab { white-space: normal; }
    @media (max-width: 767px) {
        .tabs {
            display: grid;
            grid-template-columns: repeat(2, 1fr);
            gap: 8px;
            padding: 8px;
        }

        .tab {
            flex: none;
            width: 100%;
            padding: 12px 10px;
            text-align: center;
            border-radius: 10px;
        }

        .tab.active {
            border-bottom: none;
            box-shadow: 0 4px 14px rgba(102,126,234,0.12);
        }
    }

    .audit-summary-mobile .audit-summary-left,
    .audit-summary-mobile .audit-summary-right {
        flex: 1 1 auto;
        font-size: 0.95em;
    }

    .audit-summary-mobile .audit-summary-left { text-align: left; }
    .audit-summary-mobile .audit-summary-right { text-align: right; }

    /* Very small phones: stack summary */
    @media (max-width: 400px) {
        .audit-summary-mobile { flex-direction: column; gap: 6px; text-align: left; }
        .audit-summary-mobile .audit-summary-right { text-align: left; }
    }

    /* History of Audits - mobile optimizations */
    #auditHistoryCard {
        padding: 14px;
        margin-bottom: 12px;
    }

    #auditHistoryCard h3 {
        font-size: 1.05em;
        margin-bottom: 8px;
    }

    #auditHistoryList {
        display: flex;
        flex-direction: column;
        gap: 10px;
    }

    /* History item style - when audits are listed dynamically, use .history-item */
    #auditHistoryList .history-item {
        background: #fff;
        border: 1px solid #e6eef9;
        border-radius: 10px;
        padding: 10px 12px;
        display: flex;
        flex-direction: column;
        gap: 6px;
    }

    /* Desktop/tablet: show meta and actions side-by-side */
    @media (min-width: 768px) {
        #auditHistoryList .history-item {
            display: flex;
            flex-direction: row;
            align-items: center;
            justify-content: space-between;
        }

        #auditHistoryList .history-actions {
            display: flex;
            gap: 8px;
        }
    }

    /* Mobile: ensure actions are on second row and right-aligned */
    @media (max-width: 767px) {
        #auditHistoryList .history-item {
            display: flex;
            flex-direction: column;
            align-items: stretch;
        }

        #auditHistoryList .history-meta { order: 1; }
        #auditHistoryList .history-actions { order: 2; display: flex; gap: 8px; justify-content: flex-end; }

        /* Make small action buttons comfortably tappable and responsive */
        #auditHistoryList .history-actions .btn { min-height: 40px; padding: 10px 12px; }
    }

    #auditHistoryList .history-item .meta {
        font-size: 0.9em;
        color: #4a5568;
    }

    #auditHistoryList .history-item .summary {
        font-size: 1em;
        color: #1a202c;
        font-weight: 600;
    }

    /* Empty state */
    #auditHistoryList:empty,
    #auditHistoryList:contains("No audits yet.") {
        text-align: center;
        color: #4a5568;
        padding: 12px 6px;
        background: transparent;
    }

    /* Action buttons inside history (e.g., View, Export) should be full-width on mobile */
    #auditHistoryList .history-actions,
    #auditHistoryCard .history-actions {
        display: flex;
        flex-direction: column;
        gap: 8px;
        margin-top: 6px;
    }

    #auditHistoryList .history-actions .btn,
    #auditHistoryCard .history-actions .btn {
        width: 100%;
        min-height: 44px;
    }

    /* Action buttons: make full width and stacked for comfortable tapping */
    .audit-table-actions, .audit-actions { width: 100%; display: flex; gap: 10px; }
    .audit-table-actions .btn, .audit-actions .btn { flex: 1 1 auto; }
    .audit-table-actions .btn, .audit-actions .btn { min-height: 44px; }
}

/* Disable or tone down heavy animations for mobile and users who prefer reduced motion */
@media (max-width: 767px), (prefers-reduced-motion: reduce) {
    * {
        transition: none !important;
        animation: none !important;
    }
}

/* Small device tweaks (very narrow phones) */
@media (max-width: 360px) {
    .tab {
        padding: 10px 12px;
        font-size: 0.9em;
    }

    .header h1 {
        font-size: 1.1em;
    }

    .calibration-header-title, .calibration-footer-title {
        font-size: 18px; /* ensure embedded header/footer look balanced */
    }
}

/* Hide mobile summary on larger screens; shown in mobile media query */
.audit-summary-mobile { display: none; }
