:root {
    color-scheme: light;
    font-family: Inter, "Segoe UI", system-ui, sans-serif;
    --sage: #9fc8ad;
    --sage-dark: #426b52;
    --cream: #f5faf6;
    --peach: #e8f4ec;
    --lavender: #dceee2;
    --ink: #294235;
    --white: #ffffff;
    --muted: #64796d;
    --border: #cfe1d4;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    min-height: 100vh;
    min-height: 100dvh;
    background: linear-gradient(135deg, #dceee2, var(--cream) 52%, #e8f4ec);
    color: var(--ink);
    overflow-wrap: break-word;
    -webkit-text-size-adjust: 100%;
}

.login-page,
.welcome-page {
    min-height: 100vh;
    min-height: 100dvh;
    display: grid;
    place-items: center;
    padding: max(1.5rem, env(safe-area-inset-top)) max(1.5rem, env(safe-area-inset-right)) max(1.5rem, env(safe-area-inset-bottom)) max(1.5rem, env(safe-area-inset-left));
}

.login-card,
.welcome-card {
    width: min(100%, 430px);
    padding: 2.5rem;
    border: 1px solid rgba(82, 117, 100, .18);
    border-radius: 28px;
    background: rgba(255, 255, 255, .92);
    box-shadow: 0 24px 70px rgba(82, 117, 100, .14);
}

.brand-mark {
    width: 58px;
    height: 58px;
    display: grid;
    place-items: center;
    margin-bottom: 1.25rem;
    border-radius: 18px;
    background: var(--lavender);
    color: var(--sage-dark);
    font-size: 1.8rem;
}

.eyebrow {
    margin: 0 0 .4rem;
    color: var(--sage-dark);
    font-size: .78rem;
    font-weight: 700;
    letter-spacing: .12em;
    text-transform: uppercase;
}

h1 {
    margin: 0;
    font-size: 2rem;
}

.subtitle {
    margin: .6rem 0 1.8rem;
    color: var(--muted);
}

label {
    display: block;
    margin: 1rem 0 .45rem;
    font-weight: 650;
}

input {
    width: 100%;
    padding: .9rem 1rem;
    border: 1px solid #c7d8cd;
    border-radius: 12px;
    outline: none;
    background: #fbfdfb;
    color: var(--ink);
    font: inherit;
}

input:focus {
    border-color: var(--sage);
    box-shadow: 0 0 0 4px rgba(158, 185, 168, .22);
}

button {
    width: 100%;
    margin-top: 1.5rem;
    padding: .95rem;
    border: 0;
    border-radius: 12px;
    background: var(--sage-dark);
    color: white;
    font: inherit;
    font-weight: 700;
    cursor: pointer;
    touch-action: manipulation;
}

button:hover {
    background: #416252;
}

.alert {
    padding: .8rem 1rem;
    border-radius: 10px;
    background: #f9dfdc;
    color: #7d4540;
}

.topbar {
    display: flex;
    justify-content: space-between;
    padding: 1rem 1.5rem;
    background: rgba(255, 255, 255, .88);
}

.topbar a {
    color: var(--sage-dark);
    font-weight: 700;
}

.welcome-page {
    min-height: calc(100vh - 56px);
}

.welcome-card {
    width: min(100%, 680px);
    text-align: center;
}

.welcome-card p:last-child {
    color: #718078;
}

a {
    color: var(--sage-dark);
}

a:focus-visible,
button:focus-visible,
summary:focus-visible {
    outline: 3px solid rgba(66, 107, 82, .35);
    outline-offset: 3px;
}

.app-header {
    position: sticky;
    top: 0;
    z-index: 20;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: max(.85rem, env(safe-area-inset-top)) max(1rem, calc((100% - 1180px) / 2), env(safe-area-inset-right)) .85rem max(1rem, calc((100% - 1180px) / 2), env(safe-area-inset-left));
    border-bottom: 1px solid #dce8df;
    background: rgba(247, 251, 248, .94);
    backdrop-filter: blur(12px);
}

.app-header nav,
.app-brand,
.heading-actions,
.form-actions,
.check-group {
    display: flex;
    align-items: center;
    gap: .8rem;
}

.app-header nav a {
    display: inline-flex;
    min-height: 44px;
    align-items: center;
    padding: 0 .25rem;
}

.app-brand {
    min-width: 0;
    color: var(--ink);
    font-weight: 800;
    text-decoration: none;
}

.mini-mark {
    display: grid;
    width: 36px;
    height: 36px;
    place-items: center;
    border-radius: 11px;
    background: var(--lavender);
}

.user-chip,
.badge {
    padding: .35rem .65rem;
    border-radius: 999px;
    background: #e5f2e9;
    font-size: .82rem;
}

.badge.success {
    background: #dceddf;
    color: #315c45;
}

.app-content {
    width: min(1180px, calc(100% - 2rem));
    margin: 2rem auto 5rem;
    padding-bottom: env(safe-area-inset-bottom);
}

.hero-panel,
.page-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 2rem;
    margin-bottom: 1.5rem;
}

.hero-panel {
    padding: 3rem;
    border-radius: 28px;
    background: linear-gradient(120deg, rgba(220, 238, 226, .97), rgba(237, 247, 240, .94));
}

.quick-grid,
.record-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 1rem;
}

.quick-card,
.panel,
.clinical-section {
    border: 1px solid rgba(82, 117, 100, .16);
    border-radius: 18px;
    background: rgba(255, 255, 255, .94);
    box-shadow: 0 12px 35px rgba(82, 117, 100, .08);
}

.quick-card {
    display: grid;
    gap: .4rem;
    padding: 1.4rem;
    color: var(--ink);
    text-decoration: none;
}

.quick-card span {
    color: var(--muted);
}

.panel {
    padding: 1.4rem;
    margin-bottom: 1rem;
}

.button,
button {
    display: inline-flex;
    width: auto;
    align-items: center;
    justify-content: center;
    margin: 0;
    padding: .8rem 1.1rem;
    border: 0;
    border-radius: 11px;
    background: var(--sage-dark);
    color: white;
    font: inherit;
    font-weight: 700;
    text-decoration: none;
    cursor: pointer;
    min-height: 44px;
    touch-action: manipulation;
}

.button.secondary,
button.secondary {
    border: 1px solid #b7d0bf;
    background: #f7fbf8;
    color: var(--sage-dark);
}

.search-bar {
    display: flex;
    gap: .6rem;
    margin-bottom: 1rem;
}

.search-bar input {
    flex: 1;
}

.responsive-table {
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    -webkit-overflow-scrolling: touch;
    scrollbar-color: var(--sage) transparent;
}

table {
    width: 100%;
    min-width: 680px;
    border-collapse: collapse;
}

th,
td {
    padding: .9rem;
    border-bottom: 1px solid #e6eee8;
    text-align: left;
}

th {
    color: #687a70;
    font-size: .8rem;
    text-transform: uppercase;
}

.empty-state {
    padding: 2rem;
    text-align: center;
    color: var(--muted);
}

.clinical-section {
    margin-bottom: 1rem;
    overflow: clip;
}

.clinical-section summary {
    padding: 1.1rem 1.3rem;
    background: #edf5ef;
    color: #365847;
    font-weight: 800;
    cursor: pointer;
}

.details-content {
    padding: 1.3rem;
}

.form-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 1rem;
}

.field {
    min-width: 0;
}

.field.span-2,
.span-2 {
    grid-column: 1 / -1;
}

.field label {
    margin-top: 0;
}

input,
select,
textarea {
    width: 100%;
    padding: .8rem .9rem;
    border: 1px solid #c7d8cd;
    border-radius: 10px;
    outline: none;
    background: #fbfdfb;
    color: var(--ink);
    font: inherit;
    min-height: 44px;
}

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

input[type="range"] {
    padding-inline: 0;
    accent-color: var(--sage-dark);
}

input[type="checkbox"] {
    accent-color: var(--sage-dark);
}

input:focus,
select:focus,
textarea:focus {
    border-color: var(--sage);
    box-shadow: 0 0 0 4px rgba(158, 185, 168, .2);
}

.check-field {
    display: flex;
    align-items: center;
    gap: .55rem;
    margin: .5rem 0;
}

.check-field input {
    width: auto;
    min-width: 22px;
    min-height: 22px;
}

.field-error {
    display: block;
    margin-top: .35rem;
    color: #a04444;
}

.form-actions {
    justify-content: flex-end;
    margin-top: 1.2rem;
}

.sticky-actions {
    position: sticky;
    bottom: max(1rem, env(safe-area-inset-bottom));
    z-index: 10;
    padding: .8rem;
    border: 1px solid #d7e5da;
    border-radius: 14px;
    background: rgba(255, 253, 249, .95);
    box-shadow: 0 10px 30px rgba(53, 67, 60, .15);
}

.repeat-item {
    margin-bottom: 1rem;
    padding: 1rem;
    border: 1px dashed #b8ccbe;
    border-radius: 12px;
    background: #fbfdfb;
}

.remove-row {
    grid-column: 1 / -1;
    background: #f7e2df;
    color: #7d4540;
}

.pain-scale {
    display: flex;
    justify-content: space-between;
    color: var(--muted);
    font-size: .8rem;
}

.timeline {
    border-left: 3px solid #c9dccf;
    padding-left: 1.4rem;
}

.timeline-item {
    position: relative;
    margin-bottom: 1rem;
    padding: 1rem;
    border-radius: 12px;
    background: #f8fbf9;
}

.timeline-item::before {
    position: absolute;
    top: 1.25rem;
    left: -1.83rem;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: var(--sage-dark);
    content: "";
}

.timeline-item > div {
    display: flex;
    justify-content: space-between;
}

.record-list dt {
    color: var(--muted);
    font-size: .82rem;
    font-weight: 700;
}

.record-list dd {
    margin: .2rem 0 1rem;
}

.pain-comparison {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1.2rem;
    padding: 1rem;
}

.pain-comparison div {
    display: grid;
    text-align: center;
}

.pain-comparison strong {
    font-size: 2rem;
}

.summary-list > div {
    display: flex;
    justify-content: space-between;
    padding: .7rem 0;
    border-bottom: 1px solid #e6eee8;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
}

.print-view main {
    width: min(800px, calc(100% - 2rem));
    margin: 2rem auto;
    background: white;
}

@media (max-width: 760px) {
    .app-header {
        gap: .35rem;
        padding-top: max(.6rem, env(safe-area-inset-top));
        padding-bottom: .35rem;
    }

    .app-header,
    .page-heading,
    .hero-panel {
        align-items: flex-start;
        flex-direction: column;
    }

    .app-brand {
        width: 100%;
    }

    .app-brand > span:last-child {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .app-header nav {
        width: 100%;
        justify-content: space-between;
        border-top: 1px solid #e6eee8;
    }

    .user-chip {
        display: none;
    }

    .app-content {
        width: min(100% - 1.25rem, 1180px);
        margin-top: 1.25rem;
        margin-bottom: calc(3rem + env(safe-area-inset-bottom));
    }

    h1 {
        font-size: clamp(1.65rem, 8vw, 2rem);
        line-height: 1.12;
    }

    h2 {
        font-size: 1.25rem;
    }

    .hero-panel {
        gap: 1.25rem;
        padding: 1.5rem;
        border-radius: 20px;
    }

    .hero-panel .button {
        width: 100%;
    }

    .login-card,
    .welcome-card {
        padding: 1.5rem;
        border-radius: 20px;
    }

    .panel {
        padding: 1rem;
    }

    .clinical-section summary {
        min-height: 48px;
        padding: .9rem 1rem;
    }

    .details-content {
        padding: 1rem;
    }

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

    .check-group {
        align-items: flex-start;
        flex-direction: column;
    }

    .patients-table,
    .patients-table tbody,
    .patients-table tr,
    .patients-table td {
        display: block;
        width: 100%;
    }

    .patients-table {
        min-width: 0;
    }

    .patients-table thead {
        position: absolute;
        width: 1px;
        height: 1px;
        overflow: hidden;
        clip: rect(0, 0, 0, 0);
    }

    .patients-table tr {
        margin-bottom: .85rem;
        padding: .9rem;
        border: 1px solid var(--border);
        border-radius: 14px;
        background: #f8fcf9;
    }

    .patients-table tr:last-child {
        margin-bottom: 0;
    }

    .patients-table td {
        display: flex;
        align-items: baseline;
        justify-content: space-between;
        gap: 1rem;
        padding: .55rem 0;
        border-bottom: 1px solid #e2eee5;
        text-align: right;
    }

    .patients-table td::before {
        color: var(--muted);
        content: attr(data-label);
        font-size: .75rem;
        font-weight: 800;
        letter-spacing: .05em;
        text-align: left;
        text-transform: uppercase;
    }

    .patients-table td:first-child {
        display: block;
        padding-top: 0;
        font-size: 1.05rem;
        text-align: left;
    }

    .patients-table td:first-child::before,
    .patients-table .table-action::before {
        display: none;
    }

    .patients-table .table-action {
        display: block;
        padding: .75rem 0 0;
        border-bottom: 0;
    }

    .patients-table .table-action .button {
        width: 100%;
    }

    .field.span-2,
    .span-2 {
        grid-column: auto;
    }
    .heading-actions,
    .form-actions {
        width: 100%;
        flex-wrap: wrap;
    }

    .heading-actions .button,
    .form-actions .button,
    .form-actions button {
        flex: 1;
        min-width: min(100%, 140px);
    }

    .search-bar input,
    input,
    select,
    textarea {
        font-size: 16px;
    }

    .timeline {
        padding-left: 1rem;
    }

    .timeline-item {
        padding: .85rem;
    }

    .timeline-item::before {
        left: -1.43rem;
    }

    .timeline-item > div,
    .summary-list > div {
        align-items: flex-start;
        flex-direction: column;
        gap: .35rem;
    }

    .sticky-actions {
        bottom: max(.5rem, env(safe-area-inset-bottom));
    }
}

@media (max-width: 430px) {
    .login-page,
    .welcome-page {
        padding-right: max(.75rem, env(safe-area-inset-right));
        padding-left: max(.75rem, env(safe-area-inset-left));
    }

    .search-bar {
        flex-direction: column;
    }

    .search-bar button {
        width: 100%;
    }

    .heading-actions,
    .form-actions {
        flex-direction: column;
    }

    .heading-actions .button,
    .form-actions .button,
    .form-actions button {
        width: 100%;
        flex: none;
    }

    .pain-comparison {
        gap: .75rem;
    }

    .repeat-item {
        padding: .75rem;
    }
}

@media (hover: hover) and (pointer: fine) {
    button:hover,
    .button:hover {
        background: #416252;
    }

    .button.secondary:hover,
    button.secondary:hover {
        background: #e5f2e9;
        color: #315440;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        scroll-behavior: auto !important;
        transition-duration: .01ms !important;
        animation-duration: .01ms !important;
        animation-iteration-count: 1 !important;
    }
}

.required-note {
    color: #6a5042;
    font-size: .9rem;
    margin: 0 0 1rem;
}

.required-note span {
    color: #a33b2b;
    font-weight: 800;
}

.field:has([name^="meridians"][name$="[code]"]) > label::after,
.field:has([name^="motor_assessments"][name$="[body_region]"]) > label::after,
.field:has([name^="motor_assessments"][name$="[movement_test]"]) > label::after,
.field:has([name^="motor_assessments"][name$="[result]"]) > label::after,
.field:has([name^="pulse_findings"][name$="[side]"]) > label::after,
.field:has([name^="pulse_findings"][name$="[position]"]) > label::after,
.field:has([name^="procedures"][name$="[procedure_type]"]) > label::after,
.field:has([name^="treatment_points"][name$="[point_code]"]) > label::after {
    color: #a33b2b;
    content: " **";
    font-weight: 800;
}

@media print {
    .app-header,
    .form-actions,
    button,
    .button {
        display: none !important;
    }
    body {
        background: white;
    }
    .panel {
        box-shadow: none;
        break-inside: avoid;
    }
}
