/* ── Profile View ──
   Rendered as a fullscreen view inside #app-content.
   Uses a card-per-section layout for a modern SaaS-style profile page. */

.profile-view {
    position: absolute;
    /* 2px inset matches .calendar and .datatable — frame visuals come from .app-container */
    inset: 2px;
    display: flex;
    flex-direction: column;
    visibility: hidden;
    z-index: 10;
}

.profile-view.show {
    visibility: visible;
}

/* ── Scrollable content area (no border — cards provide visual containers) ── */
.profile-content {
    overflow-y: auto;
    flex: 1;
    min-height: 0;
    max-width: 720px;
    box-sizing: border-box;
    padding: 24px 32px 32px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

/* Container receives programmatic focus on entering edit mode (so no field is
   pre-focused). Suppress the outline — keyboard users see focus via the form
   fields they Tab to, not on the scroll container itself. */
.profile-content:focus {
    outline: none;
}

/* ── Card container (shared by header + section cards) ── */
.profile-card {
    background-color: color-mix(in srgb, var(--color-border) 8%, var(--color-surface-primary));
    border: none;
    border-radius: 10px;
    padding: 1.25rem;
    box-sizing: border-box;
}

/* ── Header card ── */
.profile-header-card {
    display: flex;
    align-items: center;
    gap: 16px;
}

.profile-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    user-select: none;
}

/* Initials styling — only when no photo is present */
.profile-avatar[data-has-photo="false"],
.profile-avatar:not([data-has-photo]) {
    background-color: var(--color-context-background);
    color: var(--color-context-foreground);
    font-size: 18px;
    font-weight: 600;
}

/* Avatar image fills the circular container (inserted by JS at runtime) */
.profile-avatar__img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: inherit;
    display: block;
}

/* Initials are hidden when a photo is present */
.profile-avatar[data-has-photo="true"] .profile-avatar__initials {
    display: none;
}

/* In edit mode, the avatar becomes an actionable upload target */
.profile-view--editing .profile-avatar {
    cursor: pointer;
    position: relative;
}
.profile-view--editing .profile-avatar:hover,
.profile-view--editing .profile-avatar:focus-visible {
    outline: 2px solid var(--color-interactive-cta);
    outline-offset: 2px;
}

/* Camera-icon badge: sits inside the circle at the bottom-right, with a
   contrasting ring so it stays visible on top of any photo. */
.profile-avatar__upload-badge {
    position: absolute;
    right: 4px;
    bottom: 4px;
    width: 26px;
    height: 26px;
    border-radius: 50%;
    background: var(--color-interactive-cta);
    color: var(--color-surface-primary);
    border: 2px solid var(--color-surface-primary);
    display: none;
    align-items: center;
    justify-content: center;
    font-size: 13px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
    pointer-events: none; /* click passes through to .profile-avatar */
}
.profile-view--editing .profile-avatar__upload-badge {
    display: inline-flex;
}

.profile-header-info {
    min-width: 0;
}

.profile-title {
    font-size: 20px;
    font-weight: 600;
    color: var(--color-text-primary);
    margin: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.profile-subtitle {
    font-size: 13px;
    color: var(--color-text-secondary);
    margin: 2px 0 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ── Section titles (inside cards) ── */
.profile-section-title {
    font-size: 12px;
    font-weight: 600;
    color: var(--color-text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin: 0 0 14px;
}

/* Append colon to field labels */
.profile-view .form-label::after {
    content: ':';
}

/* ── Field grid ── */
.profile-fields {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px 32px;
}

.profile-field--full {
    grid-column: 1 / -1;
}

/* ── Field label hierarchy ── */
.profile-card .form-label {
    font-size: 13px;
    color: var(--color-text-secondary);
    margin-bottom: 2px;
}

/* ── View mode: inputs hidden, spans visible ── */
.profile-view .profile-field-input {
    display: none;
}

.profile-view .profile-field-value {
    display: block;
    font-size: 14px;
    color: var(--color-text-primary);
    line-height: 1.5;
    min-height: 20px;
    word-break: break-word;
}

.profile-field-value--empty {
    color: var(--color-text-secondary);
    font-style: italic;
}

/* Ensure italic empty values meet WCAG 2.1 AA 4.5:1 contrast in dark theme
   (#9E9E9E on #424242 = ~3.96:1 — below threshold; #ADADAD = ~4.6:1) */
[data-theme="dark"] .profile-field-value--empty {
    color: #ADADAD;
}

/* ── Edit mode: inputs visible, spans hidden ── */
.profile-view--editing .profile-field-input {
    display: block;
}

.profile-view--editing .profile-field-input::selection {
    background-color: var(--color-context-background);
    color: #FFFFFF;
}

.profile-view--editing .profile-field-value {
    display: none;
}

/* ── Compact spacing in view mode ── */
.profile-view .form-group {
    margin-bottom: 0;
}

.profile-view--editing .form-group {
    margin-bottom: var(--form-control-spacing-vertical);
}

/* ── Tags ── */
.profile-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 4px;
}

.profile-tag {
    display: inline-flex;
    align-items: center;
    border-radius: 6px;
    padding: 2px 10px;
    font-size: 12px;
    font-weight: 500;
    white-space: nowrap;
    border: 1px solid var(--color-border);
}

/* ── Textarea fields (medical history, notes, comment) ── */
.profile-view .profile-field-textarea {
    display: none;
}

.profile-view--editing .profile-field-textarea {
    display: block;
    width: 100%;
    min-height: 80px;
    resize: vertical;
}

/* ── Checkbox / toggle fields (boolean flags) ── */
/* Label and checkbox sit on one row: label left, checkbox (or Yes/No text in view mode) right. */
.form-group:has(> .profile-checkbox-group) {
    display: flex;
    align-items: center;
    gap: 12px;
}

.form-group:has(> .profile-checkbox-group) > .form-label {
    margin-bottom: 0;
    flex: 0 0 auto;
}

.profile-checkbox-group {
    display: flex;
    align-items: center;
    gap: 8px;
    min-height: 20px;
    flex: 1 1 auto;
}

.profile-field-checkbox {
    display: none;
}

.profile-view--editing .profile-field-checkbox {
    display: inline-block;
    width: 16px;
    height: 16px;
    accent-color: var(--color-context-background);
    cursor: pointer;
}

.profile-view--editing .profile-checkbox-group .profile-field-value {
    display: none;
}

/* ── Select / dropdown fields ── */
.profile-view .profile-field-select {
    display: none;
}

.profile-view--editing .profile-field-select {
    display: block;
    width: 100%;
}

.profile-view--editing .form-group:has(.profile-field-select) .profile-field-value {
    display: none;
}

/* ── Read-only fields (system, auto-generated) ── */
.profile-field-readonly {
    font-size: 14px;
    color: var(--color-text-secondary);
    line-height: 1.5;
    min-height: 20px;
    word-break: break-word;
}

/* ── Collapsible sections (alt address, system) ── */
.profile-collapsible {
    margin: 0;
}

.profile-collapsible > summary {
    cursor: pointer;
    user-select: none;
    list-style: none;
    display: flex;
    align-items: center;
    gap: 6px;
}

.profile-collapsible > summary::-webkit-details-marker {
    display: none;
}

.profile-collapsible > summary::before {
    /* Use the IconSimpliMed navigate_down glyph (U+E9AC) so the caret is visually identical
       to the toolbar dropdown indicators. Rotated -90deg when collapsed (apex points right),
       0deg when open (apex points down). */
    font-family: 'IconSimpliMed';
    content: '\e9ac';
    font-size: 16px;
    line-height: 1;
    color: var(--color-text-secondary);
    display: inline-block;
    transform: rotate(-90deg);
    transition: transform 0.15s ease;
    flex-shrink: 0;
}

.profile-collapsible[open] > summary::before {
    transform: rotate(0deg);
}

/* Section-level collapsible (alt address) — title is the summary */
.profile-section-collapsible > summary {
    font-size: 12px;
    font-weight: 600;
    color: var(--color-text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin: 0;
}

/* ── System footer card ── */
.profile-system-footer > summary {
    font-size: 12px;
    font-weight: 600;
    color: var(--color-text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin: 0;
}

.profile-system-footer .profile-fields {
    margin-top: 10px;
}

.profile-system-footer .profile-field-readonly {
    font-size: 12px;
}

/* ── Error message ── */
.profile-error {
    color: var(--color-error, #dc2626);
    font-size: 14px;
    padding: 0.75rem 1.25rem;
    background-color: color-mix(in srgb, var(--color-error, #dc2626) 8%, var(--color-surface-secondary));
    border: 1px solid color-mix(in srgb, var(--color-error, #dc2626) 25%, transparent);
    border-radius: 10px;
}

/* ── Narrow viewports ── */
@media (max-width: 640px) {
    .profile-content {
        padding: 16px 16px 20px;
        gap: 10px;
    }

    .profile-title {
        font-size: 18px;
    }

    .profile-card {
        padding: 1rem;
    }

    .profile-fields {
        grid-template-columns: 1fr;
    }
}

/* ── Lock container scroll and hide FAB when profile is open ── */
body:has(#app-content .profile-view.show) #app-content {
    overflow-y: hidden;
}

body:has(#app-content .profile-view.show) .fab {
    display: none !important;
}

body:has(#app-content .profile-view.show) .datatable__footer {
    display: none;
}
