:root {
    --primary-color: #2563EB;
    --primary-hover: #1d4ed8;
    --bg-color: #f3f4f6;
    --card-bg: #ffffff;
    --text-main: #111827;
    --text-muted: #6b7280;
    --border-color: #e5e7eb;
    --error-color: #ef4444;
    --success-color: #10b981;
    --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    --radius: 12px;
    --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body {
    font-family: var(--font-family);
    background-color: var(--bg-color);
    color: var(--text-main);
    line-height: 1.5;
    -webkit-tap-highlight-color: transparent;
    padding-bottom: calc(80px + var(--safe-bottom));
}

.hidden { display: none !important; }

/* Header */
.app-header {
    background: linear-gradient(135deg, #2563EB, #1e40af);
    color: white;
    padding: calc(16px + var(--safe-top)) 16px 16px;
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: var(--shadow-sm);
}
.header-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    max-width: 768px;
    margin: 0 auto;
}
.app-header h1 { font-size: 1.25rem; font-weight: 600; margin: 0; flex-grow: 1; text-align: center; }
.icon-btn {
    background: none; border: none; color: inherit; cursor: pointer; padding: 8px;
    display: flex; align-items: center; justify-content: center; border-radius: 50%;
}
.google-auth { min-width: 40px; }
.avatar-img { width: 36px; height: 36px; border-radius: 50%; object-fit: cover; border: 2px solid white; }
.offline-indicator {
    background: #f59e0b; color: #fff; text-align: center; font-size: 0.875rem;
    padding: 4px; margin-top: 8px; border-radius: 4px;
}

/* Side Menu */
.side-menu {
    position: fixed; top: 0; left: -280px; width: 280px; height: 100%;
    background: var(--card-bg); z-index: 200; box-shadow: var(--shadow-md);
    transition: transform 0.3s ease; padding-top: var(--safe-top); display: flex; flex-direction: column;
}
.side-menu.open { transform: translateX(280px); }
.menu-header { display: flex; justify-content: space-between; align-items: center; padding: 16px; border-bottom: 1px solid var(--border-color); }
.menu-list { list-style: none; padding: 16px 0; }
.menu-list li button {
    width: 100%; text-align: left; background: none; border: none; padding: 16px 24px;
    font-size: 1rem; color: var(--text-main); cursor: pointer; transition: background 0.2s;
}
.menu-list li button:hover { background: var(--bg-color); }
.menu-overlay { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); z-index: 150; }

/* Main */
.app-main { padding: 16px; max-width: 768px; margin: 0 auto; }
.card { background: var(--card-bg); border-radius: var(--radius); padding: 20px; margin-bottom: 16px; box-shadow: var(--shadow-sm); }
.card h2 { font-size: 1.125rem; font-weight: 600; margin-bottom: 16px; color: var(--text-main); border-bottom: 1px solid var(--border-color); padding-bottom: 8px; }

/* Forms */
.form-group { margin-bottom: 16px; }
.flex-row { display: flex; gap: 12px; }
.flex-1 { flex: 1; }
label { display: block; font-size: 0.875rem; font-weight: 500; margin-bottom: 6px; color: var(--text-muted); }
.required { color: var(--error-color); }
input[type="text"], input[type="tel"], select, textarea {
    width: 100%; padding: 12px; border: 1px solid var(--border-color); border-radius: 8px;
    font-size: 1rem; font-family: inherit; transition: border-color 0.2s; background: var(--card-bg);
}
input[readonly] { background: var(--bg-color); color: var(--text-muted); }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary-color); }
.error-msg { display: none; color: var(--error-color); font-size: 0.75rem; margin-top: 4px; }
.has-error input, .has-error select { border-color: var(--error-color); }
.has-error .error-msg { display: block; }

/* Camera Area */
.camera-capture-area {
    border: 2px dashed var(--border-color); border-radius: var(--radius); padding: 32px 16px;
    text-align: center; cursor: pointer; transition: all 0.2s; background: #fafafa;
}
.camera-capture-area:active { background: #f3f4f6; }
.camera-placeholder { color: var(--text-muted); display: flex; flex-direction: column; align-items: center; gap: 8px; }
.hidden-input { display: none; }
.image-preview-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(80px, 1fr)); gap: 12px; margin-top: 16px; }
.img-preview-item { position: relative; padding-top: 100%; border-radius: 8px; overflow: hidden; box-shadow: var(--shadow-sm); }
.img-preview-item img { position: absolute; top: 0; left: 0; width: 100%; height: 100%; object-fit: cover; }
.remove-img-btn {
    position: absolute; top: 4px; right: 4px; background: rgba(0,0,0,0.6); color: white;
    border: none; border-radius: 50%; width: 24px; height: 24px; font-size: 14px; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
}

/* Pill Buttons */
.pill-group { display: flex; flex-wrap: wrap; gap: 8px; }
.pill-btn {
    padding: 8px 16px; border: 1px solid var(--border-color); border-radius: 9999px; background: var(--card-bg);
    color: var(--text-main); font-size: 0.875rem; cursor: pointer; transition: all 0.2s;
}
.pill-btn.active { background: var(--primary-color); border-color: var(--primary-color); color: white; }

/* Bottom Action Bar */
.bottom-action-bar {
    position: fixed; bottom: 0; left: 0; width: 100%; background: var(--card-bg);
    padding: 16px 16px calc(16px + var(--safe-bottom)); box-shadow: 0 -2px 10px rgba(0,0,0,0.05); z-index: 100;
}
.primary-btn {
    width: 100%; max-width: 768px; margin: 0 auto; display: flex; justify-content: center; align-items: center;
    background: var(--primary-color); color: white; border: none; border-radius: var(--radius);
    padding: 14px; font-size: 1rem; font-weight: 600; cursor: pointer; transition: background 0.2s;
}
.primary-btn:active { background: var(--primary-hover); }
.primary-btn:disabled { opacity: 0.7; cursor: not-allowed; }
.spinner {
    width: 20px; height: 20px; border: 3px solid rgba(255,255,255,0.3); border-radius: 50%;
    border-top-color: white; animation: spin 1s ease-in-out infinite; margin-left: 8px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Toast */
.toast-container { position: fixed; top: calc(20px + var(--safe-top)); left: 50%; transform: translateX(-50%); z-index: 10005; display: flex; flex-direction: column; gap: 10px; width: 90%; max-width: 400px; pointer-events: none; }
.toast { padding: 12px 20px; border-radius: 8px; color: white; font-size: 0.875rem; box-shadow: var(--shadow-md); animation: slideDown 0.3s, fadeOut 0.3s 2.7s forwards; text-align: center; pointer-events: auto; }
.toast.success { background: var(--success-color); }
.toast.error { background: var(--error-color); }
@keyframes slideDown { from { transform: translateY(-20px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
@keyframes fadeOut { to { opacity: 0; visibility: hidden; } }

/* Modal */
.modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.65); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); z-index: 10001; display: flex; align-items: center; justify-content: center; padding: 16px; }
.modal-content { background: var(--card-bg); border-radius: var(--radius); width: 100%; max-width: 500px; max-height: 85vh; display: flex; flex-direction: column; box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5); }
.modal-header { padding: 16px; border-bottom: 1px solid var(--border-color); display: flex; justify-content: space-between; align-items: center; }
.modal-body { padding: 16px; overflow-y: auto; }
.history-list { display: flex; flex-direction: column; gap: 12px; }
.history-item { display: flex; gap: 12px; padding: 12px; border: 1px solid var(--border-color); border-radius: 8px; }
.history-item-thumb { width: 60px; height: 60px; border-radius: 4px; object-fit: cover; }
.history-item-content { flex: 1; }
.history-item-title { font-weight: 600; font-size: 0.875rem; margin-bottom: 4px; }
.history-item-desc { font-size: 0.75rem; color: var(--text-muted); }
.status-badge { display: inline-block; padding: 2px 6px; border-radius: 4px; font-size: 0.7rem; font-weight: 600; margin-top: 4px; }
.status-synced { background: #d1fae5; color: #065f46; }
.status-pending { background: #fef3c7; color: #92400e; }

/* Real-time Time Badge */
.time-badge {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    background: linear-gradient(135deg, #eff6ff, #dbeafe);
    border: 1px solid #bfdbfe;
    color: #1e40af;
    padding: 8px 14px;
    border-radius: 9999px;
    font-size: 0.9rem;
    font-weight: 600;
    margin-bottom: 16px;
    width: 100%;
    box-sizing: border-box;
}

/* Select Box for Mobile Pickers */
.select-box {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    padding: 12px 14px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--card-bg);
    cursor: pointer;
    font-size: 1rem;
    min-height: 48px;
    box-sizing: border-box;
    transition: border-color 0.2s, background-color 0.2s;
}
.select-box:active { background: #f9fafb; border-color: var(--primary-color); }
.select-box.disabled { background: #f3f4f6; color: #9ca3af; cursor: not-allowed; }
.select-box .placeholder { color: #9ca3af; }
.select-box .selected-val { color: var(--text-main); font-weight: 500; }
.chevron-icon { color: #9ca3af; flex-shrink: 0; }

/* Readonly Highlight Field */
.readonly-highlight {
    background: #f8fafc !important;
    color: #0369a1 !important;
    font-weight: 600;
    border-color: #cbd5e1 !important;
}

/* Location Picker Modal */
.location-modal-content {
    max-height: 85vh;
    height: 600px;
    display: flex;
    flex-direction: column;
}
#backLocationBtn {
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 6px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: var(--primary-color);
}
#backLocationBtn:active {
    background: #f1f5f9;
}
.search-input-wrapper {
    position: relative;
    padding: 12px 16px;
    background: #f9fafb;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    gap: 8px;
}
.search-input-wrapper svg { color: #9ca3af; flex-shrink: 0; }
.search-input-wrapper input {
    width: 100%;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: 10px 36px 10px 12px;
    font-size: 0.95rem;
    background: #ffffff;
}
.search-input-wrapper input:focus {
    outline: none;
    border-color: var(--primary-color);
}
.clear-btn {
    position: absolute;
    right: 24px;
    background: none;
    border: none;
    font-size: 1.25rem;
    color: #9ca3af;
    cursor: pointer;
    padding: 4px;
    line-height: 1;
}
.location-modal-body {
    flex: 1;
    overflow-y: auto;
    padding: 0;
}
.location-list {
    list-style: none;
    margin: 0;
    padding: 0;
}
.location-item {
    padding: 14px 16px;
    border-bottom: 1px solid #f3f4f6;
    cursor: pointer;
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: background 0.15s;
}
.location-item:active, .location-item:hover {
    background: #eff6ff;
}
.location-item-main {
    font-size: 0.95rem;
    font-weight: 500;
    color: var(--text-main);
}
.location-item-sub {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-top: 2px;
}
.location-item-badge {
    font-size: 0.75rem;
    padding: 2px 8px;
    background: #e0f2fe;
    color: #0369a1;
    border-radius: 9999px;
    font-weight: 500;
}
.location-empty {
    padding: 32px 16px;
    text-align: center;
    color: var(--text-muted);
    font-size: 0.9rem;
}

/* Normalized Address Preview */
.addr-preview {
    margin-top: 6px;
    font-size: 0.8rem;
    color: #047857;
    background: #ecfdf5;
    padding: 6px 10px;
    border-radius: 6px;
    border: 1px solid #a7f3d0;
    display: none;
}

/* GPS Status Box */
.gps-status-box {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 14px;
    border-radius: 10px;
    margin-bottom: 12px;
    cursor: pointer;
    transition: all 0.2s ease;
    border: 1px solid #e2e8f0;
    background: #f8fafc;
}
.gps-status-box:active {
    opacity: 0.9;
    transform: scale(0.99);
}
.gps-status-box.gps-ready {
    background: #f0fdf4;
    border-color: #86efac;
    color: #166534;
}
.gps-status-box.gps-searching {
    background: #fffbeb;
    border-color: #fde68a;
    color: #92400e;
}
.gps-status-box.gps-denied, .gps-status-box.gps-unavailable, .gps-status-box.gps-timeout {
    background: #fef2f2;
    border-color: #fecaca;
    color: #991b1b;
}
.gps-status-dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: #94a3b8;
    flex-shrink: 0;
}
.gps-ready .gps-status-dot {
    background: #22c55e;
    box-shadow: 0 0 0 3px rgba(34, 197, 94, 0.25);
}
.gps-searching .gps-status-dot {
    background: #f59e0b;
    box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.25);
    animation: gpsPulse 1.5s infinite;
}
.gps-denied .gps-status-dot, .gps-unavailable .gps-status-dot, .gps-timeout .gps-status-dot {
    background: #ef4444;
    box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.25);
}
@keyframes gpsPulse {
    0% { transform: scale(0.95); opacity: 0.8; }
    50% { transform: scale(1.2); opacity: 1; }
    100% { transform: scale(0.95); opacity: 0.8; }
}
.gps-status-text {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}
.gps-status-text strong {
    font-size: 0.88rem;
    line-height: 1.3;
}
.gps-status-text span {
    font-size: 0.76rem;
    color: #64748b;
    margin-top: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.gps-ready .gps-status-text span {
    color: #15803d;
}
.gps-btn-retry {
    padding: 6px 12px;
    font-size: 0.78rem;
    font-weight: 600;
    border-radius: 6px;
    border: none;
    cursor: pointer;
    background: #2563eb;
    color: #ffffff;
    flex-shrink: 0;
    white-space: nowrap;
}
.gps-btn-retry:active {
    background: #1d4ed8;
}
.gps-ready .gps-btn-retry {
    background: #15803d;
}

/* Gatekeeper Overlay */
.gatekeeper-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: linear-gradient(135deg, rgba(15, 23, 42, 0.96), rgba(30, 58, 138, 0.96));
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    box-sizing: border-box;
}
.gatekeeper-card {
    background: #ffffff;
    border-radius: 20px;
    width: 100%;
    max-width: 440px;
    padding: 24px 20px;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.4);
    display: flex;
    flex-direction: column;
    gap: 16px;
    box-sizing: border-box;
    animation: gkAppear 0.3s ease-out;
}
@keyframes gkAppear {
    from { opacity: 0; transform: scale(0.94) translateY(10px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}
.gatekeeper-header {
    text-align: center;
}
.gatekeeper-badge {
    display: inline-block;
    background: #eff6ff;
    color: #2563eb;
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: 4px 10px;
    border-radius: 9999px;
    margin-bottom: 6px;
}
.gatekeeper-header h2 {
    font-size: 1.3rem;
    color: #0f172a;
    margin-bottom: 4px;
    font-weight: 700;
}
.gatekeeper-header p {
    font-size: 0.82rem;
    color: #64748b;
    line-height: 1.4;
}
.gatekeeper-steps {
    display: flex;
    flex-direction: column;
    gap: 12px;
}
.gatekeeper-step {
    border: 1px solid #e2e8f0;
    border-radius: 14px;
    padding: 14px;
    background: #f8fafc;
    transition: all 0.2s ease;
}
.gatekeeper-step.step-completed {
    border-color: #86efac;
    background: #f0fdf4;
}
.gk-step-header {
    display: flex;
    align-items: center;
    gap: 10px;
}
.gk-step-num {
    width: 26px;
    height: 26px;
    border-radius: 50%;
    background: #2563eb;
    color: #ffffff;
    font-size: 0.85rem;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}
.step-completed .gk-step-num {
    background: #22c55e;
}
.gk-step-info {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-width: 0;
}
.gk-step-info strong {
    font-size: 0.9rem;
    color: #1e293b;
}
.gk-step-info span {
    font-size: 0.74rem;
    color: #64748b;
    margin-top: 1px;
}
.gk-check {
    font-size: 1.1rem;
}
.gk-step-action {
    display: flex;
    justify-content: center;
    width: 100%;
    margin-top: 10px;
}
.gk-user-profile {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 12px;
    background: #ffffff;
    border-radius: 10px;
    border: 1px solid #bbf7d0;
    margin-top: 10px;
}
.gk-avatar {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    object-fit: cover;
}
.gk-user-details {
    display: flex;
    flex-direction: column;
    min-width: 0;
}
.gk-user-details strong {
    font-size: 0.88rem;
    color: #15803d;
}
.gk-user-details span {
    font-size: 0.74rem;
    color: #64748b;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.gk-btn {
    width: 100%;
    padding: 12px 16px;
    font-size: 0.92rem;
    font-weight: 600;
    border-radius: 10px;
    transition: all 0.2s ease;
}
.gk-btn.btn-searching {
    background: #eab308 !important;
    color: #ffffff !important;
    cursor: wait;
}
.gk-btn.btn-error {
    background: #dc2626 !important;
    color: #ffffff !important;
}
.gk-btn.btn-warning {
    background: #d97706 !important;
    color: #ffffff !important;
}
.gk-gps-success {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    padding: 8px 0 2px 0;
}
.gk-coords-badge {
    display: inline-block;
    background: #dcfce7;
    color: #166534;
    font-size: 0.85rem;
    font-weight: 600;
    padding: 6px 12px;
    border-radius: 8px;
    border: 1px solid #86efac;
}
.gatekeeper-footer {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 2px;
}
.gk-btn-enter {
    width: 100%;
    padding: 14px;
    font-size: 0.98rem;
    font-weight: 700;
    border-radius: 12px;
    transition: all 0.25s ease;
}
.gk-btn-enter.ready {
    background: #16a34a !important;
    color: #ffffff !important;
    box-shadow: 0 4px 14px rgba(22, 163, 74, 0.4);
    cursor: pointer;
    animation: pulseReady 1.5s infinite;
}
@keyframes pulseReady {
    0% { transform: scale(1); }
    50% { transform: scale(1.02); }
    100% { transform: scale(1); }
}
.gk-btn-enter.ready:hover, .gk-btn-enter.ready:active {
    background: #15803d !important;
    transform: translateY(-1px);
}
.gk-btn-enter.disabled, .gk-btn-enter:disabled {
    background: #cbd5e1;
    color: #94a3b8;
    cursor: not-allowed;
    box-shadow: none;
}
.gk-cookie-note {
    font-size: 0.72rem;
    color: #64748b;
    text-align: center;
    line-height: 1.35;
}
