/* ============================================================
   NCD Assessment — editorial clinical aesthetic
   ============================================================
   Palette: warm parchment background, deep ink text,
   single accent in deep medical green (oxford-green),
   secondary accent in burnt sienna for warnings.

   Type system:
     · Display:  Fraunces (variable serif, with optical sizes)
     · Body/UI:  IBM Plex Sans Thai Looped (excellent for Thai)
     · Mono:     JetBrains Mono (numerics, kbd)
   ============================================================ */

:root {
    /* paper & ink */
    --paper:        #FBF8F2;
    --paper-2:      #F4EFE4;
    --paper-3:      #ECE5D5;
    --rule:         #D8CFB9;
    --rule-soft:    #E5DDC9;
    --ink:          #1B1F1A;
    --ink-2:        #3A3F38;
    --ink-3:        #6B6F66;
    --ink-4:        #9A9D93;

    /* accents */
    --accent:       #1F4D3A;     /* oxford / forest medical */
    --accent-2:     #2D6A4F;
    --accent-soft:  #DDE7DC;
    --warm:         #B5481E;     /* burnt sienna */
    --warm-soft:    #F2DCD0;
    --highlight:    #FFE9A8;     /* search hit */

    /* metrics */
    --sidebar-w:    320px;
    --content-pad:  56px;
    --radius:       2px;

    /* fonts */
    --font-display: "Fraunces", "Noto Serif Thai", Georgia, serif;
    --font-body:    "IBM Plex Sans Thai Looped", system-ui, sans-serif;
    --font-mono:    "JetBrains Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    padding: 0;
    background: var(--paper);
    color: var(--ink);
    font-family: var(--font-body);
    font-size: 15px;
    line-height: 1.55;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Subtle paper grain — extremely soft, only visible on close inspection */
body::before {
    content: "";
    position: fixed; inset: 0;
    background-image:
        radial-gradient(circle at 25% 25%, rgba(120,90,40,.025) 0, transparent 1px),
        radial-gradient(circle at 75% 75%, rgba(60,80,40,.022) 0, transparent 1px);
    background-size: 4px 4px, 7px 7px;
    pointer-events: none;
    z-index: 0;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; text-underline-offset: 3px; }

.skip-link {
    position: absolute; left: -9999px; top: 8px;
    background: var(--ink); color: var(--paper);
    padding: 8px 12px; border-radius: var(--radius);
    z-index: 1000;
}
.skip-link:focus { left: 8px; }

/* ─────────── Sidebar ─────────── */

.sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: var(--sidebar-w);
    background: var(--paper-2);
    border-right: 1px solid var(--rule);
    overflow-y: auto;
    overflow-x: hidden;
    z-index: 50;
    display: flex;
    flex-direction: column;
}

.sidebar__brand {
    padding: 28px 24px 22px;
    border-bottom: 1px solid var(--rule);
    display: flex;
    gap: 14px;
    align-items: flex-start;
}
.brand__monogram {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 46px; height: 46px;
    background: var(--ink);
    color: var(--paper);
    font-family: var(--font-display);
    font-weight: 600;
    font-size: 14px;
    letter-spacing: .04em;
    border-radius: var(--radius);
}
.brand__monogram span {
    line-height: 1;
    transform: translateY(-1px);
}
.brand__monogram span + span { margin-left: 1px; }

.brand__title {
    font-family: var(--font-display);
    font-weight: 600;
    font-size: 19px;
    margin: 0 0 4px;
    line-height: 1.1;
    font-variation-settings: "opsz" 14;
    letter-spacing: -0.005em;
}
.brand__sub {
    margin: 0;
    font-size: 11px;
    color: var(--ink-3);
    line-height: 1.4;
    letter-spacing: .01em;
}

/* Search */
.sidebar__search {
    padding: 16px 20px;
    border-bottom: 1px solid var(--rule);
    position: relative;
}
.search {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--paper);
    border: 1px solid var(--rule);
    border-radius: var(--radius);
    padding: 8px 10px;
    transition: border-color .15s, box-shadow .15s;
}
.search:focus-within {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}
.search__icon {
    width: 16px; height: 16px;
    color: var(--ink-3);
    flex: 0 0 auto;
}
.search__input {
    flex: 1;
    border: 0;
    background: transparent;
    outline: 0;
    font-family: inherit;
    font-size: 13px;
    color: var(--ink);
}
.search__input::placeholder { color: var(--ink-4); }
.search__kbd {
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--ink-3);
    background: var(--paper-3);
    padding: 1px 6px;
    border-radius: 3px;
    border: 1px solid var(--rule);
}

/* Search results dropdown */
.search__results {
    position: absolute;
    top: calc(100% - 6px);
    left: 16px;
    right: 16px;
    background: var(--paper);
    border: 1px solid var(--rule);
    border-radius: var(--radius);
    box-shadow: 0 10px 30px -8px rgba(20,30,15,.18);
    max-height: 70vh;
    overflow-y: auto;
    z-index: 100;
}
.search-result {
    display: block;
    padding: 12px 14px;
    border-bottom: 1px solid var(--rule-soft);
    cursor: pointer;
    transition: background .12s;
}
.search-result:last-child { border-bottom: 0; }
.search-result:hover,
.search-result.is-focused {
    background: var(--paper-2);
}
.search-result__head {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 4px;
}
.search-result__disease {
    font-family: var(--font-display);
    font-weight: 600;
    font-size: 13px;
    color: var(--ink);
}
.search-result__section {
    font-size: 10.5px;
    color: var(--ink-3);
    text-transform: uppercase;
    letter-spacing: .08em;
}
.search-result__snippet {
    font-size: 12.5px;
    color: var(--ink-2);
    line-height: 1.4;
}
.search-result mark {
    background: var(--highlight);
    color: var(--ink);
    padding: 0 2px;
    border-radius: 1px;
}
.search-result--empty {
    padding: 18px;
    text-align: center;
    color: var(--ink-3);
    font-size: 13px;
}
.search-result__count {
    padding: 8px 14px;
    background: var(--paper-2);
    font-size: 11px;
    color: var(--ink-3);
    text-transform: uppercase;
    letter-spacing: .08em;
    border-bottom: 1px solid var(--rule-soft);
    position: sticky; top: 0;
}

/* Nav */
.sidebar__nav {
    flex: 1;
    padding: 14px 12px 24px;
}
.nav-group { margin-bottom: 18px; }
.nav-group__title {
    font-family: var(--font-display);
    font-style: italic;
    font-weight: 400;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: .14em;
    color: var(--ink-3);
    margin: 0 12px 8px;
    font-variation-settings: "opsz" 9;
}
.nav-group__list {
    list-style: none;
    margin: 0; padding: 0;
}
.nav-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 12px;
    border-radius: var(--radius);
    color: var(--ink-2);
    transition: background .12s, color .12s;
    text-decoration: none;
    border: 1px solid transparent;
}
.nav-item:hover {
    background: var(--paper-3);
    color: var(--ink);
    text-decoration: none;
}
.nav-item.is-active {
    background: var(--paper);
    color: var(--ink);
    border-color: var(--rule);
    box-shadow: inset 3px 0 0 var(--accent);
}
.nav-item__short {
    font-family: var(--font-mono);
    font-size: 10px;
    color: var(--ink-3);
    background: var(--paper);
    border: 1px solid var(--rule);
    padding: 2px 5px;
    border-radius: 2px;
    flex: 0 0 auto;
    min-width: 32px;
    text-align: center;
    letter-spacing: .04em;
}
.nav-item.is-active .nav-item__short {
    background: var(--accent);
    color: var(--paper);
    border-color: var(--accent);
}
.nav-item__name { display: flex; flex-direction: column; line-height: 1.25; }
.nav-item__th { font-size: 13.5px; font-weight: 500; }
.nav-item__en { font-size: 10.5px; color: var(--ink-3); margin-top: 1px; }

/* Search-hit highlight on nav items */
.nav-item.has-hit {
    background: linear-gradient(90deg, var(--highlight) 0, transparent 30%);
}
.nav-item.has-hit::after {
    content: attr(data-hit-count);
    margin-left: auto;
    font-family: var(--font-mono);
    font-size: 10px;
    color: var(--warm);
    background: var(--warm-soft);
    padding: 1px 6px;
    border-radius: 9px;
}

.sidebar__footer {
    padding: 14px 24px;
    border-top: 1px solid var(--rule);
    font-size: 11px;
    color: var(--ink-3);
}
.sidebar__footer p { margin: 0; }
.save-status {
    color: var(--ink-3);
    font-family: var(--font-mono);
    font-size: 10px;
}
.save-status.is-saved { color: var(--accent-2); }

.menu-toggle {
    display: none;
    position: fixed;
    top: 14px; left: 14px;
    z-index: 200;
    width: 40px; height: 40px;
    background: var(--paper);
    border: 1px solid var(--rule);
    border-radius: var(--radius);
    cursor: pointer;
    padding: 0;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 4px;
}
.menu-toggle span {
    display: block;
    width: 18px; height: 1.5px;
    background: var(--ink);
    transition: transform .2s;
}

/* ─────────── Main content ─────────── */

.main {
    margin-left: var(--sidebar-w);
    padding: 48px var(--content-pad) 96px;
    min-height: 100vh;
    max-width: 1080px;
    position: relative;
    z-index: 1;
}

/* Page header */
.page-head {
    border-bottom: 1px solid var(--rule);
    padding-bottom: 28px;
    margin-bottom: 36px;
    position: relative;
}
.page-head__eyebrow {
    font-family: var(--font-display);
    font-style: italic;
    font-weight: 400;
    font-size: 13px;
    color: var(--accent);
    letter-spacing: .05em;
    text-transform: uppercase;
    margin-bottom: 12px;
    font-variation-settings: "opsz" 12;
}
.page-head__dot { margin: 0 6px; opacity: .5; }
.page-head__title {
    margin: 0 0 16px;
    line-height: 1.05;
}
.page-head__title-th {
    display: block;
    font-family: var(--font-display);
    font-size: 48px;
    font-weight: 600;
    color: var(--ink);
    letter-spacing: -0.015em;
    font-variation-settings: "opsz" 60;
}
.page-head__title-en {
    display: block;
    font-family: var(--font-display);
    font-style: italic;
    font-weight: 400;
    font-size: 20px;
    color: var(--ink-3);
    margin-top: 6px;
    font-variation-settings: "opsz" 18;
}
.page-head__actions {
    position: absolute;
    top: 0;
    right: 0;
    display: flex;
    gap: 8px;
}

.btn {
    font-family: inherit;
    font-size: 12.5px;
    font-weight: 500;
    padding: 8px 16px;
    border-radius: var(--radius);
    cursor: pointer;
    transition: background .14s, color .14s, border-color .14s;
    letter-spacing: .01em;
}
.btn--ghost {
    background: transparent;
    color: var(--ink-2);
    border: 1px solid var(--rule);
}
.btn--ghost:hover { background: var(--paper-2); color: var(--ink); }
.btn--solid {
    background: var(--ink);
    color: var(--paper);
    border: 1px solid var(--ink);
}
.btn--solid:hover { background: var(--accent); border-color: var(--accent); }
.btn:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

/* Patient strip */
.patient-strip {
    display: grid;
    grid-template-columns: 110px 1fr 90px 110px 150px;
    gap: 16px;
    padding: 18px 22px;
    background: var(--paper-2);
    border: 1px solid var(--rule);
    border-radius: var(--radius);
    margin-bottom: 36px;
}
.patient-strip__field { display: flex; flex-direction: column; }
.patient-strip__field label {
    font-family: var(--font-display);
    font-style: italic;
    font-weight: 400;
    font-size: 11px;
    color: var(--ink-3);
    margin-bottom: 4px;
    text-transform: uppercase;
    letter-spacing: .08em;
    font-variation-settings: "opsz" 10;
}
.patient-strip input,
.patient-strip select {
    border: 0;
    background: transparent;
    border-bottom: 1px solid var(--rule);
    padding: 4px 0;
    font-family: inherit;
    font-size: 14px;
    color: var(--ink);
    outline: 0;
    transition: border-color .15s;
}
.patient-strip input:focus,
.patient-strip select:focus { border-bottom-color: var(--accent); }

/* Sections */
.section {
    margin-bottom: 40px;
    scroll-margin-top: 24px;
}
.section__head {
    display: flex;
    align-items: baseline;
    gap: 16px;
    margin-bottom: 18px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--rule-soft);
}
.section__num {
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--ink-4);
    letter-spacing: .04em;
}
.section__title {
    font-family: var(--font-display);
    font-weight: 600;
    font-size: 22px;
    color: var(--ink);
    margin: 0;
    letter-spacing: -0.005em;
    font-variation-settings: "opsz" 24;
}
.section__body { padding: 0 0 0 30px; }

/* Subheadings inside a section */
.subheading {
    font-family: var(--font-display);
    font-style: italic;
    font-weight: 500;
    font-size: 14px;
    color: var(--ink-2);
    margin: 18px 0 10px;
    padding-bottom: 4px;
    border-bottom: 1px dashed var(--rule);
    font-variation-settings: "opsz" 14;
    letter-spacing: .01em;
}

/* Notes */
.note {
    background: var(--paper-2);
    border-left: 2px solid var(--accent);
    padding: 12px 16px;
    margin: 12px 0 14px;
    font-size: 13px;
    line-height: 1.6;
    color: var(--ink-2);
    border-radius: 0 var(--radius) var(--radius) 0;
}

/* Fields */
.field {
    margin: 12px 0;
    display: flex;
    flex-direction: column;
    gap: 4px;
}
.field__label {
    font-size: 12px;
    color: var(--ink-3);
    font-weight: 500;
    letter-spacing: .005em;
}
.field__input-wrap {
    display: flex;
    align-items: baseline;
    gap: 8px;
    border-bottom: 1px solid var(--rule);
    padding-bottom: 2px;
    transition: border-color .15s;
}
.field__input-wrap:focus-within { border-bottom-color: var(--accent); }
.field__input,
.field__textarea {
    border: 0;
    background: transparent;
    font-family: inherit;
    font-size: 14px;
    color: var(--ink);
    outline: 0;
    width: 100%;
    padding: 4px 0;
}
.field__input::placeholder,
.field__textarea::placeholder { color: var(--ink-4); }
.field__unit {
    font-family: var(--font-mono);
    font-size: 11px;
    color: var(--ink-3);
    flex: 0 0 auto;
    letter-spacing: .03em;
}
.field--textarea .field__textarea {
    border: 1px solid var(--rule);
    padding: 10px 12px;
    border-radius: var(--radius);
    line-height: 1.55;
    background: var(--paper);
    resize: vertical;
}
.field--textarea .field__textarea:focus { border-color: var(--accent); }

/* Date inputs match the underline style */
.field--date .field__input {
    border-bottom: 1px solid var(--rule);
    padding-bottom: 2px;
    width: auto;
    min-width: 160px;
    color-scheme: light;
}
.field--date .field__input:focus { border-bottom-color: var(--accent); }

/* Row layout */
.field-row {
    display: grid;
    grid-template-columns: repeat(var(--cols, 2), 1fr);
    gap: 20px;
    margin: 12px 0;
}

/* Checkboxes — custom mark */
.check {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    cursor: pointer;
    padding: 6px 0;
    user-select: none;
}
.check__box {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}
.check__mark {
    flex: 0 0 auto;
    width: 16px;
    height: 16px;
    border: 1.5px solid var(--ink-3);
    border-radius: 2px;
    background: var(--paper);
    margin-top: 2px;
    transition: all .15s;
    position: relative;
}
.check__mark::after {
    content: "";
    position: absolute;
    inset: 2px;
    background: var(--accent);
    border-radius: 1px;
    transform: scale(0);
    transition: transform .15s cubic-bezier(.34,1.56,.64,1);
}
.check:hover .check__mark { border-color: var(--accent); }
.check__box:checked + .check__mark {
    border-color: var(--accent);
    background: var(--paper);
}
.check__box:checked + .check__mark::after { transform: scale(1); }
.check__box:focus-visible + .check__mark {
    box-shadow: 0 0 0 3px var(--accent-soft);
}
.check__label {
    font-size: 13.5px;
    line-height: 1.5;
    color: var(--ink-2);
}
.check__box:checked ~ .check__label { color: var(--ink); }

/* Check + inline input */
.check-input {
    display: grid;
    grid-template-columns: 1fr 220px;
    gap: 16px;
    align-items: start;
    margin: 6px 0;
}
.check-input__field {
    border: 0;
    background: transparent;
    border-bottom: 1px solid var(--rule);
    padding: 6px 0;
    font-family: inherit;
    font-size: 13px;
    color: var(--ink);
    outline: 0;
    transition: border-color .15s;
}
.check-input__field:focus { border-bottom-color: var(--accent); }
.check-input__field::placeholder { color: var(--ink-4); font-size: 12px; }

/* Radio-like groups */
.radio-group {
    display: flex;
    flex-direction: column;
    gap: 2px;
    margin: 6px 0;
}
.radio-row {
    display: grid;
    grid-template-columns: 1fr 200px;
    gap: 16px;
    align-items: center;
}
.radio-row .check-input__field { width: 100%; }

/* Form footer */
.form__footer {
    margin-top: 48px;
    padding-top: 20px;
    border-top: 1px solid var(--rule);
}
.form__hint {
    font-family: var(--font-display);
    font-style: italic;
    font-size: 13px;
    color: var(--ink-3);
    text-align: center;
    margin: 0;
    font-variation-settings: "opsz" 14;
}

/* In-page search highlights */
.search-hit-target {
    background: var(--highlight);
    transition: background 1.2s;
}

/* ─────────── Responsive ─────────── */

@media (max-width: 980px) {
    :root { --content-pad: 24px; }
    .page-head__title-th { font-size: 36px; }
    .page-head__actions {
        position: static;
        margin-top: 16px;
    }
    .patient-strip {
        grid-template-columns: 1fr 1fr;
    }
    .check-input,
    .radio-row {
        grid-template-columns: 1fr;
    }
    .check-input__field { width: 100%; max-width: 320px; }
}

@media (max-width: 760px) {
    :root { --sidebar-w: 280px; --content-pad: 18px; }
    .menu-toggle { display: flex; }
    .sidebar {
        transform: translateX(-100%);
        transition: transform .25s ease;
        box-shadow: 0 0 0 rgba(0,0,0,0);
    }
    .sidebar.is-open {
        transform: translateX(0);
        box-shadow: 8px 0 32px rgba(20,30,15,.25);
    }
    .main {
        margin-left: 0;
        padding-top: 72px;
    }
    .page-head__title-th { font-size: 28px; }
    .page-head__title-en { font-size: 16px; }
    .field-row { grid-template-columns: 1fr 1fr; }
    .section__body { padding-left: 0; }
}

/* ─────────── Print ─────────── */

@media print {
    :root { --content-pad: 0; }
    body { background: white; font-size: 11pt; }
    body::before { display: none; }
    .sidebar, .menu-toggle, .page-head__actions, .form__footer { display: none !important; }
    .main { margin: 0; max-width: 100%; padding: 0; }
    .page-head { border-bottom: 2px solid var(--ink); page-break-inside: avoid; }
    .page-head__title-th { font-size: 20pt; }
    .page-head__title-en { font-size: 12pt; }
    .section { page-break-inside: avoid; margin-bottom: 18pt; }
    .section__title { font-size: 14pt; }
    .check__mark { border-color: var(--ink) !important; }
    .field__input,
    .field__textarea,
    .check-input__field {
        background: white;
    }
    .note { background: #f5f5f5; }
    a { color: inherit; text-decoration: none; }
}

/* Honor the user's reduced-motion preference */
@media (prefers-reduced-motion: reduce) {
    * { transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; }
}
