/* Merry Brothers — Mobile-first CSS
   Color scheme: Dark navy/white with yellow/orange urgency accents
   Spec: specs/14_ui_ux.md, specs/24_mobile_optimization.md
   Breakpoints: mobile <768px (default), tablet 768–1199px, desktop ≥1200px */

:root {
    --navy: #1a2744;
    --navy-light: #2d3f5e;
    --white: #ffffff;
    --gray-100: #f5f6f8;
    --gray-200: #e8eaed;
    --gray-400: #9aa0a6;
    --gray-600: #5f6368;
    --red: #d93025;
    --yellow: #f9ab00;
    --orange: #e8710a;
    --green: #1e8e3e;
    --blue: #1a73e8;
    --dark: #202124;
    --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: var(--font);
    font-size: 16px;
    background: var(--gray-100);
    color: var(--dark);
    line-height: 1.5;
    -webkit-text-size-adjust: 100%;
}

/* Header */
.app-header {
    background: var(--navy);
    color: var(--white);
    padding: 0 16px;
    position: sticky;
    top: 0;
    z-index: 10000;
}
.header-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    max-width: 1200px;
    margin: 0 auto;
    height: 56px;
}
.logo {
    color: var(--white);
    text-decoration: none;
    font-weight: 700;
    font-size: 18px;
}
.main-nav { display: flex; gap: 16px; align-items: center; flex-wrap: nowrap; }
.main-nav a {
    color: rgba(255,255,255,0.85);
    text-decoration: none;
    font-size: 15px;
    padding: 8px 0;
    min-height: 48px;
    display: flex;
    align-items: center;
    white-space: nowrap;
}
.main-nav a:hover { color: var(--white); }

/* Dropdown nav groups */
.nav-dropdown { position: relative; }
.dropdown-toggle {
    background: none;
    border: none;
    cursor: pointer;
    color: rgba(255,255,255,0.85);
    font-size: 15px;
    padding: 8px 0;
    min-height: 48px;
    display: flex;
    align-items: center;
    gap: 4px;
    white-space: nowrap;
    font-family: var(--font);
}
.dropdown-toggle:hover { color: var(--white); }
.dropdown-arrow { font-size: 9px; transition: transform 0.15s; }
.nav-dropdown.open .dropdown-arrow { transform: rotate(180deg); }
.dropdown-menu {
    display: none;
    position: absolute;
    top: calc(100% + 2px);
    left: -8px;
    background: var(--navy-light);
    border-radius: 8px;
    min-width: 160px;
    box-shadow: 0 4px 16px rgba(0,0,0,0.3);
    z-index: 9999;
    padding: 4px 0;
}
.nav-dropdown.open .dropdown-menu { display: block; }
.dropdown-menu a {
    display: flex;
    align-items: center;
    padding: 10px 16px;
    color: rgba(255,255,255,0.85);
    text-decoration: none;
    font-size: 15px;
    min-height: 44px;
    white-space: nowrap;
}
.dropdown-menu a:hover { background: rgba(255,255,255,0.1); color: var(--white); }
.nav-group-label { display: none; }

.nav-toggle {
    display: none;
    background: none;
    border: none;
    position: relative;
    z-index: 1001;
    color: var(--white);
    font-size: 24px;
    cursor: pointer;
    min-width: 48px;
    min-height: 48px;
}
@media (max-width: 1023px) {
    .nav-toggle { display: block; }
    .main-nav {
        display: none;
        position: absolute;
        top: 56px;
        left: 0;
        right: 0;
        background: var(--navy);
        flex-direction: column;
        padding: 8px 16px 16px;
        gap: 4px;
        z-index: 1000;
        max-height: calc(100vh - 56px);
        overflow-y: auto;
    }
    .main-nav.open { display: flex; }
    .main-nav { align-items: stretch; }
    .main-nav a { padding: 12px 8px; width: 100%; }
    /* Dropdowns on mobile: toggle button visible, content expands inline when open */
    .nav-dropdown { width: 100%; }
    .dropdown-toggle {
        width: 100%;
        justify-content: space-between;
        padding: 12px 8px;
        min-height: 44px;
    }
    .nav-group-label { display: none; }
    /* When open on mobile: show menu inline (not floating) */
    .nav-dropdown.open .dropdown-menu {
        display: block;
        position: static;
        box-shadow: none;
        border-radius: 0;
        background: transparent;
        padding: 0 0 4px 12px;
        min-width: auto;
        border-left: 2px solid rgba(255,255,255,0.15);
        margin-left: 8px;
    }
    .dropdown-menu a { padding: 10px 8px; min-height: 44px; font-size: 15px; }
}

/* Offline connectivity banner (Spec §24 §8) */
.offline-banner {
    display: none;
    background: var(--orange);
    color: var(--white);
    text-align: center;
    padding: 10px 16px;
    font-size: 14px;
    font-weight: 500;
    position: sticky;
    top: 56px;
    z-index: 9998;
}
.offline-banner.visible { display: block; }

/* Container — mobile default; expands at desktop */
.container {
    max-width: 100%;
    margin: 0 auto;
    padding: 16px;
}

/* Tablet and up: constrain to readable width */
@media (min-width: 768px) {
    .container { max-width: 1200px; padding: 24px; }
}

/* Desktop: wider max-width and centered (Spec §24 §10) */
@media (min-width: 1200px) {
    .container { max-width: 1400px; padding: 32px; }
}

/* Flash messages */
.flash-messages { margin-bottom: 16px; }
.flash {
    padding: 12px 16px;
    border-radius: 8px;
    margin-bottom: 8px;
    font-size: 16px;
}
.flash-success { background: #e6f4ea; color: var(--green); }
.flash-error { background: #fce8e6; color: var(--red); }
.flash-info { background: #e8f0fe; color: var(--blue); }

/* Back links */
.back-link {
    display: inline-block;
    margin-bottom: 8px;
    color: var(--blue);
    text-decoration: none;
    font-size: 16px;
}
.back-link:hover { text-decoration: underline; }

/* Typography */
h1 { font-size: 24px; margin-bottom: 16px; }
h2 { font-size: 20px; margin-bottom: 12px; margin-top: 24px; }

/* Desktop larger headings */
@media (min-width: 1200px) {
    h1 { font-size: 28px; }
    h2 { font-size: 22px; }
}

/* Buttons — min 48px tap target (Spec §24 §2) */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-height: 48px;
    padding: 12px 24px;
    border: 1px solid var(--gray-200);
    border-radius: 8px;
    background: var(--white);
    color: var(--dark);
    font-size: 16px;
    font-weight: 500;
    text-decoration: none;
    cursor: pointer;
    transition: background 0.15s;
}
.btn:hover { background: var(--gray-100); }
.btn-primary {
    background: var(--navy);
    color: var(--white);
    border-color: var(--navy);
}
.btn-primary:hover { background: var(--navy-light); }
.btn-danger { background: var(--red); color: var(--white); border-color: var(--red); }
.btn-sm { min-height: 48px; padding: 6px 12px; font-size: 14px; }
.btn-full { width: 100%; }

/* Primary action buttons: 56px tall + full-width on mobile (Spec §24 §2) */
@media (max-width: 767px) {
    .btn-primary, .btn-action {
        min-height: 56px;
        width: 100%;
    }
}

/* Forms */
.form-card {
    background: var(--white);
    border-radius: 12px;
    padding: 24px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}
.form-group { margin-bottom: 16px; }
.form-group label {
    display: block;
    font-size: 14px;
    font-weight: 500;
    color: var(--gray-600);
    margin-bottom: 4px;
}
.form-control {
    width: 100%;
    min-height: 48px;
    padding: 12px;
    border: 1px solid var(--gray-200);
    border-radius: 8px;
    font-size: 16px;
    font-family: var(--font);
    background: var(--white);
}
.form-control:focus {
    outline: none;
    border-color: var(--blue);
    box-shadow: 0 0 0 2px rgba(26,115,232,0.2);
}
textarea.form-control { min-height: 80px; resize: vertical; }
select.form-control { appearance: auto; }
.form-row { display: flex; gap: 12px; flex-wrap: wrap; }
.form-row .form-group { flex: 1; min-width: 200px; }
.form-actions { display: flex; gap: 12px; margin-top: 24px; flex-wrap: wrap; }

/* Single-column forms on mobile (Spec §24 §5) */
@media (max-width: 767px) {
    .form-row { flex-direction: column; gap: 0; }
    .form-row .form-group { min-width: 100%; flex: none; }
}
.checkbox-group label {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
    min-height: 48px;
    cursor: pointer;
}
.checkbox-group input[type="checkbox"] {
    width: 20px;
    height: 20px;
}

/* Form sections — card-style containers for groups of form fields */
.form-section {
    background: var(--white);
    border-radius: 12px;
    padding: 24px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
    margin-bottom: 24px;
}
.form-section h2 { margin-top: 0; }
@media (max-width: 767px) {
    .form-section { padding: 16px; }
}

/* Tables */
.table-responsive { overflow-x: auto; margin-bottom: 16px; }
.data-table {
    width: 100%;
    border-collapse: collapse;
    background: var(--white);
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}
.data-table th {
    background: var(--gray-100);
    padding: 12px 16px;
    font-size: 14px;
    font-weight: 600;
    text-align: left;
    color: var(--gray-600);
}
.data-table td {
    padding: 12px 16px;
    font-size: 16px;
    border-top: 1px solid var(--gray-200);
}
.data-table a { color: var(--blue); text-decoration: none; }
.row-overdue { background: #fce8e6; }
.row-due-soon { background: #fef7e0; }
.row-upcoming { background: #e6f4ea; }
.row-completed { background: var(--gray-200); }
.row-active { background: #e8f0fe; }
/* Ensure data-tables always scroll on mobile */
@media (max-width: 767px) {
    .data-table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }
}

/* Status badges */
.status-badge {
    display: inline-block;
    padding: 4px 10px;
    border-radius: 12px;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
}
.status-scheduled { background: #e8f0fe; color: var(--blue); }
.status-in_progress { background: #fef7e0; color: var(--orange); }
.status-completed { background: #e6f4ea; color: var(--green); }
.status-missed { background: #fce8e6; color: var(--red); }
.status-active { background: #e6f4ea; color: var(--green); }
.status-expired { background: #fce8e6; color: var(--red); }
.status-cancelled { background: var(--gray-200); color: var(--gray-600); }
.status-archived { background: #e8eaf6; color: #5c6bc0; }
.status-inactive { background: var(--gray-200); color: var(--gray-600); }

/* Summary cards */
.summary-cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 12px;
    margin-bottom: 24px;
}
.card {
    background: var(--white);
    border-radius: 12px;
    padding: 16px;
    text-align: center;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
    border-left: 4px solid transparent;
}
.card-red { border-left-color: var(--red); }
.card-yellow { border-left-color: var(--yellow); }
.card-green { border-left-color: var(--green); }
.card-blue { border-left-color: var(--blue); }
.card-dark { border-left-color: var(--dark); }
.card-count { font-size: 32px; font-weight: 700; }
.card-label { font-size: 14px; color: var(--gray-600); margin-top: 4px; }

/* Page header */
.page-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 12px;
    margin-bottom: 16px;
}
.page-header h1 { margin-bottom: 0; }

/* Section header */
.section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 8px;
}

/* Search bar */
.search-bar {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
}
.search-bar .form-control { flex: 1; }

/* Quick actions */
.quick-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    margin-bottom: 24px;
}

/* Detail cards */
.detail-card {
    background: var(--white);
    border-radius: 12px;
    padding: 24px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
    margin-bottom: 24px;
}
@media (max-width: 767px) {
    .detail-card { padding: 16px; }
}
.detail-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 16px;
}
.detail-item.full-width { grid-column: 1 / -1; }
.detail-label {
    display: block;
    font-size: 13px;
    font-weight: 500;
    color: var(--gray-400);
    text-transform: uppercase;
    margin-bottom: 4px;
}
.detail-value { font-size: 16px; }
.detail-value a { color: var(--blue); text-decoration: none; }

/* Renewal alerts */
.renewal-alerts { margin-bottom: 24px; }
.alert-list {
    background: var(--white);
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 1px 3px rgba(0,0,0,0.08);
}
.alert-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border-top: 1px solid var(--gray-200);
    gap: 12px;
}
.alert-item:first-child { border-top: none; }
.alert-customer { font-weight: 500; flex: 1; }
.alert-date { font-size: 14px; color: var(--gray-600); }

/* Timeline */
.timeline { margin-bottom: 24px; }
.timeline-item {
    display: flex;
    gap: 16px;
    padding: 12px 0;
    border-bottom: 1px solid var(--gray-200);
}
.timeline-date {
    min-width: 100px;
    font-size: 14px;
    color: var(--gray-600);
}

/* Pagination */
.pagination {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 12px;
    margin-top: 16px;
    font-size: 16px;
}

/* Empty state */
.empty-state {
    text-align: center;
    color: var(--gray-400);
    padding: 40px 16px;
    font-size: 16px;
}

/* Error page */
.error-page {
    text-align: center;
    padding: 60px 16px;
}
.error-page h1 { font-size: 28px; margin-bottom: 8px; }
.error-page p { color: var(--gray-600); margin-bottom: 24px; }

/* Login page */
.login-container {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 80vh;
}
.login-card {
    background: var(--white);
    border-radius: 16px;
    padding: 40px 32px;
    width: 100%;
    max-width: 400px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.12);
    text-align: center;
}
.login-card h1 {
    font-size: 28px;
    color: var(--navy);
    margin-bottom: 4px;
}
.login-card h2 {
    font-size: 16px;
    color: var(--gray-600);
    font-weight: 400;
    margin-bottom: 32px;
    margin-top: 0;
}

/* Sync status indicator */
.sync-status {
    font-size: 12px;
    padding: 4px 10px;
    border-radius: 12px;
    margin-left: 8px;
}
.sync-status.syncing { background: #fef7e0; color: #e8710a; }
.sync-status.synced { background: #e6f4ea; color: #1e8e3e; }
.sync-status.pending { background: #fce8e6; color: #d93025; }

/* Spinner */
@keyframes spin {
    to { transform: rotate(360deg); }
}
.spinner {
    display: inline-block;
    width: 24px;
    height: 24px;
    border: 3px solid var(--gray-200);
    border-top-color: var(--navy);
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}
.spinner-sm { width: 16px; height: 16px; border-width: 2px; }
.spinner-lg { width: 40px; height: 40px; border-width: 4px; }
.spinner-center { display: block; margin: 24px auto; }

/* Skeleton loading screens */
@keyframes shimmer {
    0% { background-position: -400px 0; }
    100% { background-position: 400px 0; }
}
.skeleton {
    background: linear-gradient(90deg, var(--gray-200) 25%, var(--gray-100) 50%, var(--gray-200) 75%);
    background-size: 800px 100%;
    animation: shimmer 1.5s ease-in-out infinite;
    border-radius: 6px;
}
.skeleton-text { height: 14px; margin-bottom: 8px; width: 80%; }
.skeleton-text-short { height: 14px; margin-bottom: 8px; width: 40%; }
.skeleton-heading { height: 24px; margin-bottom: 16px; width: 50%; }
.skeleton-card {
    height: 80px;
    border-radius: 12px;
    margin-bottom: 12px;
}
.skeleton-row {
    height: 48px;
    margin-bottom: 4px;
    border-radius: 4px;
}
.skeleton-avatar { width: 40px; height: 40px; border-radius: 50%; }

/* Loading overlay for form submissions */
.loading-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 9999;
    align-items: center;
    justify-content: center;
    flex-direction: column;
}
.loading-overlay.active { display: flex; }
.loading-overlay-content {
    background: var(--white);
    padding: 32px 48px;
    border-radius: 12px;
    text-align: center;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
}
.loading-overlay-content .spinner { margin-bottom: 12px; }
.loading-overlay-text {
    font-size: 16px;
    font-weight: 500;
    color: var(--dark);
}

/* Button loading state */
.btn-loading {
    pointer-events: none;
    opacity: 0.7;
    position: relative;
}
.btn-loading .spinner {
    width: 16px;
    height: 16px;
    border-width: 2px;
    margin-right: 8px;
    vertical-align: middle;
}

/* Form validation error styles */
.form-control.is-invalid {
    border-color: var(--red);
    box-shadow: 0 0 0 2px rgba(217, 48, 37, 0.15);
}
.form-error {
    color: var(--red);
    font-size: 14px;
    margin-top: 4px;
    display: block;
}
.form-error-summary {
    background: #fce8e6;
    color: var(--red);
    padding: 12px 16px;
    border-radius: 8px;
    margin-bottom: 16px;
    font-size: 16px;
}

/* Inline loading indicator for async selects */
.select-loading {
    position: relative;
}
.select-loading::after {
    content: '';
    position: absolute;
    right: 40px;
    top: 50%;
    transform: translateY(-50%);
    width: 16px;
    height: 16px;
    border: 2px solid var(--gray-200);
    border-top-color: var(--navy);
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

/* Print styles */
@media print {
    .app-header, .flash-messages, .no-print, .loading-overlay { display: none !important; }
    .container { max-width: none; padding: 0; }
    body { background: white; font-size: 11pt; }

    /* Service checklist print styles — hides interactive controls, shows text values */
    .checklist-result,
    .checklist-notes,
    .checklist-controls select,
    .form-actions,
    .quick-actions,
    .bulk-assign-form,
    .dashboard-filters,
    .btn { display: none !important; }

    /* Show print-friendly text values for checklist results */
    .checklist-controls .print-value { display: inline !important; }

    /* Checklist category page breaks */
    .checklist-category { page-break-before: auto; }
    .checklist-item { page-break-inside: avoid; }

    /* Table and card print cleanup */
    .data-table { font-size: 10pt; }
    .data-table th, .data-table td { padding: 3px 6px; }
    .detail-card { border: 1px solid #ccc; box-shadow: none; }
    .card { box-shadow: none; border: 1px solid #ccc; }

    /* Links print as text */
    a { text-decoration: none; color: #000; }
}

/* ===== OCR Contract Upload ===== */
.ocr-upload-card {
    background: var(--card-bg, #fff);
    border: 2px dashed var(--border-color, #d0d0d0);
    border-radius: 8px;
    padding: 1.5rem;
    margin-bottom: 1.5rem;
}
.ocr-upload-title { margin: 0 0 0.5rem; font-size: 1.1rem; }
.ocr-upload-hint { color: #666; font-size: 0.9rem; margin: 0 0 1rem; }
.ocr-input-row { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.ocr-file-input-hidden { display: none; }
.ocr-file-label { cursor: pointer; }
.ocr-filename-hint { font-size: 0.85rem; color: #888; }
.ocr-preview-wrap { margin: 1rem 0; }
.ocr-preview-img { max-width: 100%; max-height: 300px; border-radius: 4px; border: 1px solid var(--border-color, #d0d0d0); }
.ocr-status { padding: 0.5rem 0.75rem; border-radius: 4px; margin: 0.75rem 0; font-size: 0.9rem; }
.ocr-status-info  { background: #e8f4fd; color: #1a6fa8; }
.ocr-status-success { background: #e8f8ec; color: #1a7a3a; }
.ocr-status-warn  { background: #fdf6e3; color: #8a6a00; }
.ocr-status-error { background: #fdecea; color: #b00020; }
.ocr-result { background: #f8f9fa; border: 1px solid #e0e0e0; border-radius: 4px; padding: 0.75rem 1rem; margin-top: 0.75rem; }
.ocr-result ul { margin: 0.25rem 0 0.5rem 1.25rem; padding: 0; }
.ocr-raw-text { white-space: pre-wrap; font-size: 0.8rem; max-height: 200px; overflow-y: auto; }
.ocr-prefilled { border-color: #2196F3 !important; background: #e3f2fd !important; }
.ocr-field-hint { font-size: 0.85rem; color: #1a6fa8; margin: 0.25rem 0 0; }

/* Phase 100: Communications nav badge */
.nav-badge {
    display: inline-block;
    background: #dc3545;
    color: #fff;
    border-radius: 10px;
    font-size: 0.7rem;
    padding: 1px 6px;
    vertical-align: middle;
    margin-left: 3px;
    font-weight: 600;
    line-height: 1.4;
}
