/* Shared app layout — Screens 1–3 header, sidebar, login, dashboard
   Visual tokens: see survey.css :root and .cursor/docs/06_aegis_design_standards.md */

#appHeader {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-12);
    min-height: 56px;
    padding: 8px 24px;
    background: var(--card);
    border-bottom: 1px solid var(--border);
}

body.authenticated #appHeader {
    position: sticky;
    top: 0;
    z-index: 1100;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
}

.header-actions {
    display: flex;
    align-items: center;
    gap: var(--space-12);
    margin-left: auto;
}

.btn-return-dashboard {
    font-size: 14px;
    font-weight: 500;
    color: var(--text-secondary);
    text-decoration: none;
    padding: 0.35rem 0;
    white-space: nowrap;
}

.btn-return-dashboard:hover {
    color: var(--color-primary);
}

.observation-access-denied {
    max-width: 32rem;
    margin: 2rem auto;
    padding: var(--space-24);
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    text-align: center;
}

.observation-access-denied h2 {
    margin: 0 0 0.75rem;
    font-size: 20px;
    font-weight: 500;
    color: var(--text-primary);
}

.observation-access-denied p {
    margin: 0 0 1.25rem;
    font-size: 14px;
    line-height: 1.5;
    color: var(--text-secondary);
}

.btn-logout {
    min-height: 36px;
    padding: 8px 12px;
    font-size: 14px;
    font-weight: 500;
    font-family: inherit;
    color: var(--text-primary);
    background: var(--color-secondary-btn);
    border: 1px solid var(--color-secondary-btn);
    border-radius: var(--radius-lg);
    cursor: pointer;
}

.btn-logout:hover {
    background: #d6d6d6;
    border-color: #d6d6d6;
}

.sidebar-link-logout {
    color: #fca5a5 !important;
}

.sidebar-link-logout:hover {
    background: rgba(0, 0, 0, 0.12);
    color: #fecaca !important;
}

.header-brand {
    display: flex;
    align-items: center;
    gap: var(--space-12);
}

.header-icon {
    font-size: 1.25rem;
    color: var(--color-primary);
}

.header-logo {
    display: block;
    width: 40px;
    height: 40px;
    object-fit: contain;
    flex-shrink: 0;
    border-radius: var(--radius-lg);
}

.header-title {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
}

.header-nav {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-12);
}

.nav-link {
    font-size: 14px;
    font-weight: 500;
    color: var(--text-secondary);
    text-decoration: none;
    padding: 8px 4px;
}

.nav-link:hover {
    color: var(--color-primary);
}

.nav-link-active {
    color: var(--color-primary);
    font-weight: 600;
}

.nav-link-disabled {
    color: var(--text-muted);
    pointer-events: none;
    cursor: default;
}

/* Authenticated layout with sidebar */
.app-layout {
    display: flex;
    min-height: calc(100vh - 4.5rem);
    min-height: calc(100dvh - 4.5rem);
}

#appSidebar {
    width: 260px;
    flex-shrink: 0;
    background: #1a1a2e;
    color: #fff;
    display: flex;
    flex-direction: column;
    padding: 1.25rem 0;
}

.sidebar-nav {
    list-style: none;
    margin: 0;
    padding: 0;
}

.sidebar-link {
    display: block;
    padding: 0.75rem 1.25rem;
    color: #cbd5e1;
    text-decoration: none;
    font-size: 0.9375rem;
    font-weight: 500;
}

.sidebar-link:hover {
    background: #334155;
    color: #fff;
}

.sidebar-link-active {
    background: #334155;
    color: #fff;
    font-weight: 700;
}

.sidebar-divider {
    height: 1px;
    background: #334155;
    margin: 1rem 1.25rem;
}

.sidebar-section-label {
    padding: 0.5rem 1.25rem;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #94a3b8;
}

.sidebar-spacer {
    flex: 1;
}

.pending-widget {
    margin: 1rem 1.25rem 0;
    padding: 1rem;
    background: #334155;
    border-radius: 0.5rem;
}

.pending-widget-title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.875rem;
    font-weight: 700;
    margin-bottom: 0.75rem;
}

.pending-widget-title .fa-bell {
    color: var(--warning);
}

.pending-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.8rem;
    margin-bottom: 0.35rem;
}

.pending-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 1.5rem;
    padding: 0.1rem 0.45rem;
    border-radius: 9999px;
    font-size: 0.75rem;
    font-weight: 600;
}

.pending-badge-rectify { background: var(--color-warning); color: #fff; }
.pending-badge-close { background: var(--color-success); color: #fff; }

#dashboardMain {
    flex: 1;
    padding: var(--space-24) var(--space-56) var(--space-24);
    min-width: 0;
    max-width: 1366px;
}

.page-title {
    font-size: 20px;
    font-weight: 500;
    margin: 0 0 var(--space-20);
    color: var(--text-primary);
}

/* Screen 1 — Login */
body.login-page {
    background: var(--background);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-24);
    min-height: 100vh;
    min-height: 100dvh;
}

.login-card {
    display: flex;
    width: 100%;
    max-width: 28rem;
    background: var(--card);
    border-radius: var(--radius-lg);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
    overflow: hidden;
}

.login-form-panel {
    flex: 1;
    padding: 40px var(--space-24);
    min-width: 0;
}

.login-brand {
    display: flex;
    align-items: center;
    gap: var(--space-12);
    margin-bottom: var(--space-24);
}

.login-brand .header-title {
    font-size: 16px;
    font-weight: 600;
}

.login-heading {
    font-size: 20px;
    font-weight: 500;
    margin: 0 0 var(--space-12);
    color: var(--text-primary);
}

.login-description {
    font-size: 14px;
    color: var(--text-secondary);
    line-height: 1.5;
    margin: 0 0 var(--space-24);
}

.login-field {
    margin-bottom: var(--space-16);
}

.login-field label {
    display: block;
    font-size: 12px;
    font-weight: 400;
    margin-bottom: 6px;
    color: var(--text-secondary);
}

.login-field input {
    width: 100%;
    font-size: 16px;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    font-family: inherit;
    color: var(--text-primary);
}

.login-field input:focus {
    outline: 2px solid rgba(63, 91, 255, 0.35);
    outline-offset: 1px;
    border-color: var(--color-primary);
}

.login-error {
    display: none;
    margin-bottom: var(--space-16);
    padding: 12px var(--space-16);
    background: rgba(255, 84, 84, 0.08);
    border: 1px solid rgba(255, 84, 84, 0.4);
    border-radius: var(--radius-lg);
    color: var(--color-error);
    font-size: 14px;
}

.login-error.visible {
    display: block;
}

.login-turnstile-field {
    margin-top: var(--space-8);
    width: 100%;
}

.turnstile-widget {
    width: 100%;
    min-height: 65px;
    display: flex;
    justify-content: center;
}

.turnstile-widget iframe {
    max-width: 100%;
}

.btn-login {
    width: 100%;
    margin-top: var(--space-8);
}

.login-help {
    margin-top: var(--space-20);
    font-size: 12px;
    color: var(--text-muted);
    line-height: 1.4;
}

.login-info-panel {
    flex: 1;
    background: var(--color-primary);
    color: #fff;
    padding: 40px var(--space-24);
    min-width: 0;
}

.login-info-title {
    font-size: 1.5rem;
    font-weight: 700;
    margin: 0 0 1.5rem;
}

.info-card {
    display: flex;
    gap: 1rem;
    margin-bottom: 1.25rem;
    align-items: flex-start;
}

.info-card-icon {
    width: 2.25rem;
    height: 2.25rem;
    border-radius: 0.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 1rem;
}

.info-card-icon.orange { background: rgba(245, 158, 11, 0.2); color: #fbbf24; }
.info-card-icon.green { background: rgba(22, 163, 74, 0.2); color: #4ade80; }
.info-card-icon.blue { background: rgba(59, 130, 246, 0.2); color: #60a5fa; }
.info-card-icon.purple { background: rgba(168, 85, 247, 0.2); color: #c084fc; }

.info-card h3 {
    margin: 0 0 0.25rem;
    font-size: 0.95rem;
    font-weight: 700;
}

.info-card p {
    margin: 0;
    font-size: 0.8rem;
    color: #cbd5e1;
    line-height: 1.4;
}

.info-card a {
    color: #93c5fd;
    text-decoration: none;
    font-size: 0.8rem;
}

/* Dashboard */
.dashboard-tabs {
    display: flex;
    gap: 0;
    border-bottom: 1px solid var(--color-divider);
    margin-bottom: var(--space-20);
}

.dashboard-tab {
    padding: 9px 16px;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-secondary);
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    cursor: pointer;
    font-family: inherit;
    margin-bottom: -1px;
}

.dashboard-tab-active {
    color: var(--color-primary);
    font-weight: 600;
    border-bottom-color: var(--color-primary);
}

.tab-count {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 1.35rem;
    margin-left: 0.4rem;
    padding: 0.1rem 0.45rem;
    border-radius: var(--radius-full);
    font-size: 12px;
    font-weight: 500;
    background: rgba(0, 0, 0, 0.08);
    color: var(--text-primary);
}

.dashboard-tab-active .tab-count {
    background: var(--color-primary);
    color: #fff;
}

body.dashboard-page #dashboardMain {
    max-width: 1366px;
    margin: 0 auto;
    padding: var(--space-24) var(--space-56) var(--space-24);
    min-height: calc(100vh - 56px);
    min-height: calc(100dvh - 56px);
}

.filter-bar {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-16);
    align-items: flex-end;
    justify-content: space-between;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: var(--space-16);
    margin-bottom: var(--space-20);
}

.filter-group {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    align-items: flex-end;
}

.filter-field label {
    display: block;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 0.25rem;
}

.filter-field select,
.filter-field input {
    font-size: 14px;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    min-width: 10rem;
    font-family: inherit;
}

.obs-table-wrap {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    overflow-x: auto;
}

.obs-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
}

.obs-table thead th {
    background: #f9fafb;
    text-align: left;
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--text-secondary);
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}

.obs-table tbody td {
    padding: 0.75rem 1rem;
    border-bottom: 1px solid #f3f4f6;
    vertical-align: middle;
}

.obs-table tbody tr:hover {
    background: #f9fafb;
}

.obs-title {
    font-weight: 600;
    color: var(--color-primary);
}

.obs-id {
    color: var(--text-secondary);
    font-size: 12px;
}

/* Status chips — Aegis pill pattern (colored fill, white text) */
.badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    height: 32px;
    padding: 0 12px;
    border-radius: var(--radius-full);
    font-size: 14px;
    font-weight: 400;
    white-space: nowrap;
    line-height: 1;
}

.badge-pending { background: var(--color-warning); color: #fff; }
.badge-draft { background: var(--color-info); color: #fff; }
.badge-progress { background: var(--color-warning); color: #fff; }
.badge-closed { background: var(--color-success); color: #fff; }
.badge-priority-high { background: var(--color-error); color: #fff; }
.badge-priority-medium { background: var(--color-warning); color: #fff; }
.badge-priority-low { background: rgba(0, 0, 0, 0.08); color: var(--text-primary); }

.btn-view {
    font-size: 14px;
    padding: 8px 12px;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    color: var(--color-primary);
    cursor: pointer;
    font-family: inherit;
    font-weight: 500;
}

.btn-view:hover {
    background: rgba(63, 91, 255, 0.06);
}

.tasks-placeholder {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 3rem 2rem;
    text-align: center;
    color: var(--text-secondary);
}

.dashboard-load-error {
    font-size: 14px;
    color: var(--color-error);
    background: rgba(255, 84, 84, 0.08);
    border: 1px solid rgba(255, 84, 84, 0.4);
    border-radius: var(--radius-lg);
    padding: 12px var(--space-16);
    margin: 0 0 var(--space-16);
}

.grid-toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-12) var(--space-16);
    align-items: flex-end;
    justify-content: space-between;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    border-bottom: none;
    padding: var(--space-16);
}

.grid-toolbar-search {
    flex: 1 1 14rem;
    min-width: 12rem;
}

.grid-toolbar-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-12);
    flex: 1 1 100%;
    width: 100%;
}

.grid-toolbar-utils {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px;
}

/* Desktop+: Create sits with search row (right); utils on full-width row below */
@media (min-width: 768px) {
    .grid-toolbar {
        display: grid;
        grid-template-columns: 1fr auto;
        grid-template-areas:
            "search create"
            "utils utils";
        align-items: end;
    }

    .grid-toolbar-search {
        grid-area: search;
        flex: none;
        min-width: 0;
    }

    .grid-toolbar-actions {
        display: contents;
    }

    .grid-toolbar-utils {
        grid-area: utils;
        justify-self: start;
    }

    .grid-toolbar-actions .btn-create {
        grid-area: create;
        justify-self: end;
    }
}

.grid-search-label {
    display: block;
    font-size: 12px;
    font-weight: 400;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

.grid-search-input {
    width: 100%;
    max-width: 20rem;
    font-size: 16px;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    font-family: inherit;
}

.grid-search-input:focus {
    outline: 2px solid rgba(63, 91, 255, 0.35);
    outline-offset: 1px;
    border-color: var(--color-primary);
}

/* MUI-style text/icon toolbar buttons (Clear filters / Export) */
.btn-toolbar-icon {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    min-height: 40px;
    padding: 8px 12px;
    font-size: 14px;
    font-weight: 500;
    font-family: inherit;
    color: var(--text-primary);
    background: transparent;
    border: none;
    border-radius: var(--radius-lg);
    cursor: pointer;
    white-space: nowrap;
    line-height: 1.25;
}

.btn-toolbar-icon:hover {
    background: rgba(0, 0, 0, 0.04);
}

.btn-toolbar-icon .fas {
    font-size: 16px;
    width: 1.1em;
    text-align: center;
    color: var(--text-primary);
}

.btn-toolbar-label {
    font-size: 14px;
    font-weight: 500;
}

body.dashboard-page .btn-create {
    font-size: 14px;
}

.btn-create {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    white-space: nowrap;
    width: auto;
    min-width: 0;
    padding: 10px 16px;
    margin-left: auto;
}

/* Phone: filter + export icons (left) and Create (right) on one row under search */
@media (max-width: 640px) {
    .grid-toolbar {
        flex-direction: column;
        align-items: stretch;
    }

    .grid-toolbar-search {
        flex: 0 0 auto;
        width: 100%;
        min-width: 0;
    }

    .grid-search-input {
        max-width: none;
    }

    .grid-toolbar-actions {
        display: flex;
        flex: 0 0 auto;
        flex-wrap: nowrap;
        align-items: center;
        justify-content: space-between;
        width: 100%;
        gap: var(--space-8);
    }

    .grid-toolbar-utils {
        display: flex;
        flex-wrap: nowrap;
        flex-shrink: 0;
        gap: 0;
    }

    .btn-toolbar-icon {
        min-width: 40px;
        min-height: 40px;
        padding: 8px;
        justify-content: center;
        flex-shrink: 0;
    }

    .btn-toolbar-label {
        display: none;
    }

    .btn-create {
        margin-left: auto;
        flex: 0 0 auto;
        padding: 10px 14px;
    }
}

body.dashboard-page #obsGrid .badge {
    font-size: 14px;
}

.grid-loading {
    padding: var(--space-16);
    text-align: center;
    color: var(--text-secondary);
    font-size: 14px;
    background: var(--card);
    border: 1px solid var(--border);
    border-top: none;
}

#obsGrid {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 0 0 var(--radius-lg) var(--radius-lg);
    overflow: hidden;
}

#obsGrid .tabulator {
    border: none;
    font-size: 14px;
    font-family: inherit;
    background: transparent;
}

#obsGrid .tabulator-header {
    background: #FAFAFA;
    border-bottom: 1px solid var(--color-divider);
    font-weight: 600;
}

#obsGrid .tabulator-col {
    border-right-color: var(--color-divider);
}

#obsGrid .tabulator-col-title {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-secondary);
}

#obsGrid .tabulator-row {
    border-bottom: 1px solid var(--color-divider);
}

#obsGrid .tabulator-row:hover {
    background: rgba(63, 91, 255, 0.04) !important;
}

#obsGrid .tabulator-cell {
    padding: 10px 12px;
    border-right-color: transparent;
    font-size: 14px;
    color: var(--text-primary);
}

#obsGrid .tabulator-header-filter input,
#obsGrid .tabulator-header-filter select {
    font-size: 12px;
    padding: 6px 8px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-family: inherit;
}

.btn-view-grid {
    font-size: 14px;
}

.grid-attachment-link {
    display: inline-block;
    font-size: 14px;
    color: var(--color-primary);
    word-break: break-all;
    line-height: 1.35;
}

.grid-attachment-link:hover {
    text-decoration: underline;
}

/* Pending Rectification and beyond: Pending Correction → Attachments → Observation Details */
.survey-detail-mode:not(.survey-draft-edit) #surveyContainer .sv_p_root {
    display: flex;
    flex-direction: column;
}

.survey-detail-mode:not(.survey-draft-edit) #surveyContainer .sqn-panel-pending-rectification {
    order: 1;
}

.survey-detail-mode:not(.survey-draft-edit) #surveyContainer .sqn-panel-attachments {
    order: 2;
}

.survey-detail-mode:not(.survey-draft-edit) #surveyContainer .sqn-panel-observation-details {
    order: 3;
}

.survey-detail-mode.survey-rectify-editable .sv_complete_btn,
.survey-detail-mode:not(.survey-draft-edit) .sv_complete_btn {
    display: none !important;
}

.survey-detail-mode.survey-view-only #formActions,
.survey-detail-mode.survey-view-only #rectifyActions,
.survey-detail-mode:not(.survey-draft-edit):not(.survey-rectify-editable) #formActions,
.survey-detail-mode:not(.survey-draft-edit):not(.survey-rectify-editable) #rectifyActions {
    display: none !important;
}

.survey-detail-mode:not(.survey-draft-edit) .panel-required-hint {
    display: none;
}

.survey-loading {
    text-align: center;
    padding: 2rem;
    color: var(--text-secondary);
}

.package-context-banner {
    font-size: 14px;
    color: var(--text-secondary);
    margin: -0.25rem 0 var(--space-16);
}

.flash-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-16);
    margin: var(--space-8) var(--space-16) 0;
    padding: 12px var(--space-16);
    color: #fff;
    font-size: 16px;
    font-weight: 400;
    border-radius: var(--radius-lg);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
    z-index: 1000;
}

.flash-bar-success {
    background: var(--color-success);
}

.flash-bar-error {
    background: var(--color-error);
}

.flash-bar-text {
    flex: 1;
}

.flash-bar-dismiss {
    background: transparent;
    border: none;
    color: inherit;
    font-size: 1.25rem;
    line-height: 1;
    cursor: pointer;
    padding: 0 0.25rem;
    opacity: 0.9;
}

.flash-bar-dismiss:hover {
    opacity: 1;
}

@media (max-width: 768px) {
    .login-card {
        flex-direction: column;
    }

    .app-layout {
        flex-direction: column;
    }

    #appSidebar {
        width: 100%;
    }

    .sidebar-spacer,
    .pending-widget {
        display: none;
    }

    body.dashboard-page #dashboardMain,
    #dashboardMain {
        padding: var(--space-16);
    }

    #appHeader {
        padding: 8px 16px;
    }
}

.sqn-submit-loading {
    position: fixed;
    inset: 0;
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.45);
}

.sqn-submit-loading[hidden] {
    display: none !important;
}

.sqn-submit-loading-panel {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-16);
    padding: var(--space-24) var(--space-24);
    background: #fff;
    border-radius: var(--radius-lg);
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
    min-width: 12rem;
}

.sqn-submit-loading-spinner {
    width: 2.5rem;
    height: 2.5rem;
    border: 3px solid var(--color-divider);
    border-top-color: var(--color-primary);
    border-radius: 50%;
    animation: sqn-spin 0.75s linear infinite;
}

.sqn-submit-loading-text {
    font-size: 14px;
    font-weight: 500;
    color: var(--text-primary);
}

body.sqn-submit-busy {
    overflow: hidden;
}

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