* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: Arial, sans-serif;
    background: #f6f7f9;
    color: #1f2937;
}

body.edit-mode {
    background: #d7dce3;
}

.container {
    max-width: 900px;
    margin: 0 auto;
    padding: 1rem;
}

.topbar {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    margin-bottom: 1rem;
}

.topbar h1 {
    margin: 0;
    font-size: 1.4rem;
}

.tabs {
    display: flex;
    gap: 0.5rem;
}

.tab {
    text-decoration: none;
    border: 1px solid #d1d5db;
    background: #fff;
    color: #111827;
    border-radius: 8px;
    padding: 0.5rem 0.75rem;
    font-size: 0.95rem;
}

.tab-active {
    border-color: #2563eb;
    color: #2563eb;
}

.tab-disabled {
    border-color: #d1d5db;
    background: #f3f4f6;
    color: #6b7280;
    cursor: default;
    pointer-events: none;
}

.tab-graph {
    margin-left: auto;
}

.card {
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 10px;
    padding: 1rem;
    margin-bottom: 1rem;
}

.card h2 {
    margin-top: 0;
    font-size: 1.15rem;
}

.section-title-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
}

.btn-round-edit {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 2.3rem;
    height: 2.3rem;
    border-radius: 999px;
    border: 1px solid #9ca3af;
    background: #fff;
    color: #111827;
    text-decoration: none;
    font-size: 0.8rem;
    font-weight: 700;
    line-height: 1;
}

.btn-round-edit:hover {
    border-color: #6b7280;
    background: #f8fafc;
}

.form-grid {
    display: grid;
    gap: 0.75rem;
}

label {
    display: grid;
    gap: 0.25rem;
    font-size: 0.95rem;
}

input,
textarea {
    width: 100%;
    border: 1px solid #cbd5e1;
    border-radius: 8px;
    font-size: 1rem;
    padding: 0.55rem 0.65rem;
}

textarea {
    font-family: inherit;
    line-height: 1.25;
    overflow-y: auto;
    resize: vertical;
}

.entry-note-field {
    display: block;
}

button {
    cursor: pointer;
}

.btn-primary {
    border: none;
    background: #2563eb;
    color: #fff;
    border-radius: 8px;
    padding: 0.65rem 0.9rem;
    font-size: 1rem;
}

.btn-secondary {
    border: 1px solid #9ca3af;
    background: #fff;
    color: #111827;
    border-radius: 8px;
    padding: 0.35rem 0.55rem;
    font-size: 0.88rem;
}

.hidden {
    display: none;
}

.message {
    min-height: 1.2rem;
    margin: 0.6rem 0 0;
    color: #374151;
    font-size: 0.92rem;
}

.table-hint {
    margin: 0.3rem 0 0.15rem;
    font-size: 0.82rem;
    color: #4b5563;
}

.field-list {
    display: grid;
    gap: 0.55rem;
}

.field-row {
    display: grid;
    grid-template-columns: 1fr auto auto;
    gap: 0.45rem;
    align-items: center;
    padding: 0.4rem 0;
    border-bottom: 1px solid #f1f5f9;
}

.field-row:last-child {
    border-bottom: none;
}

.field-label {
    font-size: 0.9rem;
    color: #4b5563;
}

.field-value {
    font-weight: 600;
}

/* Bottom of Entry saved card / Review & Edit expanded BMI detail */
.stats-card-note {
    margin-top: 0.65rem;
    padding-top: 0.55rem;
    border-top: 1px solid #f1f5f9;
    font-size: 0.8rem;
    line-height: 1.4;
    color: #374151;
}

.stats-card-note__lead {
    display: inline;
}

.stats-card-note__value {
    margin-top: 0.35rem;
    font-weight: 500;
    white-space: pre-wrap;
    word-break: break-word;
}

/* Edit page: Note textarea inserted below date/weight/height inputs */
.edit-inline-extra-row td {
    padding: 0.55rem 0.75rem 0.75rem;
    vertical-align: top;
    background: #e8ecf3;
    border-bottom: 1px solid #cbd5e1;
}

.edit-inline-note-label {
    display: grid;
    gap: 0.25rem;
    font-size: 0.95rem;
}

.edit-inline-note-field {
    min-height: 4.25rem;
}

.edit-inline-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: 0.65rem;
}

body.edit-mode #review-table tbody tr.record-row--editing .delete-cell {
    opacity: 0.5;
}

body.edit-mode #review-table tbody tr.record-row--editing .delete-row-checkbox:disabled {
    cursor: not-allowed;
}

/* BMI expand column unavailable during inline edit (same cue as delete). */
body.edit-mode #review-table tbody tr.record-row--editing td[data-field='bmi'] {
    opacity: 0.5;
    cursor: not-allowed;
}

body.edit-mode #review-table tbody tr.record-row--editing td[data-field='bmi'] * {
    cursor: not-allowed;
}

.field-action-spacer {
    display: inline-block;
    width: 3.2rem;
    min-height: 1px;
}

.bmi-badge {
    display: inline-flex;
    align-items: center;
    border-radius: 999px;
    padding: 0.12rem 0.5rem;
    font-size: 0.75rem;
    font-weight: 700;
    line-height: 1.2;
    vertical-align: middle;
    white-space: nowrap;
}

.bmi-band-underweight {
    background: #ede9fe;
    color: #5b21b6;
}

.bmi-band-healthy {
    background: #dcfce7;
    color: #166534;
}

.bmi-band-overweight {
    background: #fef3c7;
    color: #92400e;
}

.bmi-band-obese {
    background: #fee2e2;
    color: #991b1b;
}

.bmi-visual {
    margin-top: 0.75rem;
    border-top: 1px solid #f1f5f9;
    padding-top: 0.65rem;
    display: grid;
    gap: 0.5rem;
}

.bmi-visual-compact {
    margin-top: 0;
    border-top: none;
    padding-top: 0;
}

.bmi-visual-title {
    font-size: 0.9rem;
    color: #374151;
}

.bmi-band-row {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    width: 100%;
    margin-bottom: 0.55rem;
}

.bmi-band-label {
    font-weight: 600;
}

.bmi-bar {
    position: relative;
    height: 12px;
    border-radius: 999px;
    overflow: hidden;
    display: grid;
    /* 10-18.5, 18.5-25, 25-30, 30-40 */
    grid-template-columns: 8.5fr 6.5fr 5fr 10fr;
}

.bmi-bar .bmi-band-underweight {
    background: #7c3aed;
}

.bmi-bar .bmi-band-healthy {
    background: #16a34a;
}

.bmi-bar .bmi-band-overweight {
    background: #d97706;
}

.bmi-bar .bmi-band-obese {
    background: #dc2626;
}

.bmi-segment {
    height: 100%;
}

.bmi-marker {
    position: absolute;
    top: -10px;
    transform: translateX(-50%);
    width: 2px;
    height: 25px;
    background: #000;
    z-index: 2;
}

.bmi-marker-value {
    display: block;
    position: absolute;
    bottom: calc(100% + 2px);
    left: 50%;
    transform: translateX(-50%);
    color: #000;
    font-size: 0.72rem;
    font-weight: 700;
    line-height: 1;
    white-space: nowrap;
}

.bmi-marker.bmi-marker-label-left .bmi-marker-value {
    left: calc(100% + 4px);
    transform: none;
}

.bmi-marker.bmi-marker-label-right .bmi-marker-value {
    left: auto;
    right: calc(100% + 4px);
    transform: none;
}

.bmi-scale {
    position: relative;
    display: grid;
    gap: 0;
    margin-top: 1rem;
}

.bmi-boundary-track {
    position: relative;
    height: 24px;
}

.bmi-boundary {
    position: absolute;
    top: 0;
    transform: translateX(-50%);
    display: grid;
    justify-items: center;
}

.bmi-boundary-line {
    width: 1px;
    height: 12px;
    background: #111827;
}

.bmi-boundary-label {
    margin-top: 1px;
    font-size: 0.68rem;
    color: #111827;
    font-weight: 600;
}

.bmi-detail-row td {
    background: #f8fafc;
    padding-top: 0.65rem;
    padding-bottom: 0.65rem;
}

body.edit-mode .bmi-detail-row td {
    background: #e8edf4;
}

.bmi-detail-empty {
    font-size: 0.82rem;
    color: #4b5563;
}

.bmi-empty-value {
    color: #6b7280;
}

.bmi-expand-indicator {
    display: inline-block;
    margin-left: 0.3rem;
    color: #6b7280;
    font-size: 0.72rem;
    line-height: 1;
    transition: transform 0.14s ease, color 0.14s ease;
}

#review-table tbody tr.record-row.expanded .bmi-expand-indicator {
    transform: rotate(180deg);
    color: #111827;
}

.bmi-extended-stats {
    margin-top: 0.85rem;
    padding-top: 0.65rem;
    border-top: 1px solid #f1f5f9;
    /* Desktop: label | primary | ave. change | projection — narrow gaps; col 4 weighted wider in @media */
    display: grid;
    grid-template-columns: max-content minmax(5rem, 1fr) minmax(5rem, 1fr) minmax(10rem, 1.25fr);
    column-gap: 1.5rem;
    row-gap: 0.45rem;
    align-items: start;
    justify-items: start;
    text-align: left;
    font-size: 0.8rem;
    line-height: 1.4;
    color: #374151;
}

/*
 * Mobile: row is a flex stack (see max-width media). Desktop: subgrid row so each logical row
 * is a real box for zebra bands (display:contents made every cell look like one flat grid).
 */
.bmi-extended-stats__row {
    display: contents;
}

.bmi-extended-stats__lead {
    max-width: 100%;
    white-space: nowrap;
}

/* Desktop: pull cols 2–4 left (tighter gaps / label pad); give col 4 more share so projection stays one line when possible */
@media (min-width: 700px) {
    /*
     * Subgrid rows do not always inherit the parent column-gap; repeating the same gap here keeps
     * gutters between cols 2–3 and 3–4 identical (otherwise col 4 can sit too close to col 3).
     */
    .bmi-extended-stats {
        --bmi-ext-stats-col-gap: 1rem;
        grid-template-columns: max-content minmax(4.25rem, 0.68fr) minmax(5.75rem, 0.72fr) minmax(15.75rem, 2.82fr);
        column-gap: var(--bmi-ext-stats-col-gap);
    }

    .bmi-extended-stats__lead {
        padding-right: var(--bmi-ext-stats-col-gap);
    }

    .bmi-extended-stats__cell--primary {
        margin-left: 0;
    }

    .bmi-extended-stats__row {
        display: grid;
        grid-template-columns: subgrid;
        grid-column: 1 / -1;
        column-gap: var(--bmi-ext-stats-col-gap);
        padding: 0.28rem 0.35rem;
        box-sizing: border-box;
        border-radius: 4px;
    }

    .bmi-extended-stats__row:nth-child(odd) {
        background-color: #eef2f7;
    }

    .bmi-extended-stats__row:nth-child(even) {
        background-color: #f9fafb;
    }
}

.bmi-extended-stats__label {
    font-weight: 700;
    color: #1f2937;
}

.bmi-extended-stats__colon {
    font-weight: 400;
    color: #1f2937;
}

.bmi-extended-stats__cell {
    font-weight: 500;
    min-width: 0;
}

.bmi-extended-stats__stack {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 0.15rem;
    width: 100%;
    min-width: 0;
}

.bmi-extended-stats__stack-label {
    font-weight: 700;
    color: #1f2937;
    line-height: 1.25;
}

.bmi-extended-stats__stack-value {
    font-weight: 500;
    color: #374151;
    line-height: 1.35;
}

@media (max-width: 699px) {
    .bmi-extended-stats {
        display: flex;
        flex-direction: column;
        gap: 0.5rem;
        row-gap: unset;
    }

    .bmi-extended-stats__row {
        display: flex;
        flex-direction: column;
        align-items: stretch;
        gap: 0.35rem;
        padding: 0.45rem 0.5rem;
        border-radius: 6px;
    }

    .bmi-extended-stats__row:nth-child(odd) {
        background-color: #eef2f7;
    }

    .bmi-extended-stats__row:nth-child(even) {
        background-color: #f9fafb;
    }

    .bmi-extended-stats__lead {
        white-space: normal;
    }

    .bmi-extended-stats__cell--empty {
        display: none;
    }
}

.bmi-legend {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.3rem 0.6rem;
    margin-top: 0.55rem;
}

.bmi-legend-item {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    font-size: 0.75rem;
    color: #4b5563;
}

.bmi-legend-swatch {
    width: 0.7rem;
    height: 0.7rem;
    border-radius: 2px;
    flex: 0 0 auto;
}

.bmi-target-distance {
    display: none;
}

.bmi-readout-row {
    display: none;
}

.bmi-readout-item {
    font-size: 0.8rem;
    line-height: 1.25;
    font-weight: 600;
    color: #1f2937;
    white-space: nowrap;
}

.bmi-swatch-underweight {
    background: #7c3aed;
}

.bmi-swatch-healthy {
    background: #16a34a;
}

.bmi-swatch-overweight {
    background: #d97706;
}

.bmi-swatch-obese {
    background: #dc2626;
}

.table-wrap {
    overflow-x: auto;
}

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

th,
td {
    padding: 0.55rem;
    border-bottom: 1px solid #e5e7eb;
    text-align: left;
    font-size: 0.92rem;
}

th {
    white-space: nowrap;
}

.sort-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    border: none;
    background: transparent;
    color: #111827;
    font-size: 0.92rem;
    font-weight: 600;
    padding: 0;
}

.sort-icons {
    display: inline-flex;
    flex-direction: column;
    gap: 0.15rem;
}

.label-mobile {
    display: none;
}

.tri {
    width: 0;
    height: 0;
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
}

.tri-up {
    border-bottom: 6px solid #9ca3af;
}

.tri-down {
    border-top: 6px solid #9ca3af;
}

.sort-btn:hover .tri-up {
    border-bottom-color: #4b5563;
}

.sort-btn:hover .tri-down {
    border-top-color: #4b5563;
}

.sort-btn[data-sort="asc"] .tri-up {
    border-bottom-color: #111827;
}

.sort-btn[data-sort="asc"] .tri-down {
    border-top-color: #9ca3af;
}

.sort-btn[data-sort="desc"] .tri-down {
    border-top-color: #111827;
}

.sort-btn[data-sort="desc"] .tri-up {
    border-bottom-color: #9ca3af;
}

tr:last-child td {
    border-bottom: none;
}

#review-table tbody tr.record-row {
    cursor: pointer;
}

#review-table tbody tr.record-row:hover td {
    background: #f8fafc;
}

#review-table tbody tr.record-row:active td {
    background: #f1f5f9;
}

#review-table tbody tr.record-row:focus-visible td {
    background: #eff6ff;
    box-shadow: inset 0 2px 0 #2563eb, inset 0 -2px 0 #2563eb;
}

#review-table tbody tr.record-row:focus-visible td:first-child {
    box-shadow: inset 2px 0 0 #2563eb, inset 0 2px 0 #2563eb, inset 0 -2px 0 #2563eb;
}

#review-table tbody tr.record-row:focus-visible td:last-child {
    box-shadow: inset -2px 0 0 #2563eb, inset 0 2px 0 #2563eb, inset 0 -2px 0 #2563eb;
}

#review-table tbody tr.record-row:focus-visible td + td {
    border-left-color: transparent;
}

body.edit-mode #review-table tbody tr.record-row:hover td {
    background: #e8edf4;
}

body.edit-mode #review-table tbody tr.record-row:active td {
    background: #dde4ee;
}

body.edit-mode #review-table tbody tr.record-row:focus-visible td {
    background: #dbe7f7;
}

.row-actions {
    display: flex;
    gap: 0.4rem;
}

.delete-column-header {
    width: 2.4rem;
    text-align: center;
}

.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2rem;
    height: 2rem;
    padding: 0;
}

.icon-btn svg {
    width: 1rem;
    height: 1rem;
    fill: currentColor;
}

.delete-header-btn {
    color: #b91c1c;
    border-color: #fca5a5;
}

.delete-header-btn:hover {
    background: #fef2f2;
}

.delete-header-btn.delete-header-btn-active {
    background: #b91c1c;
    border-color: #991b1b;
    color: #fff;
}

.delete-cell {
    text-align: center;
    width: 2.4rem;
}

.delete-row-checkbox {
    width: 1rem;
    height: 1rem;
    margin: 0;
}

#review-table th:nth-child(5),
#review-table td:nth-child(5) {
    padding-right: 0.2rem;
}

#review-table th:nth-child(6),
#review-table td:nth-child(6) {
    padding-left: 0.2rem;
}

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.45);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    z-index: 1000;
}

.modal-overlay.hidden {
    display: none;
}

.modal-card {
    width: min(380px, 100%);
    background: #fff;
    border: 1px solid #e5e7eb;
    border-radius: 10px;
    padding: 1rem;
}

.modal-card h2 {
    margin: 0 0 0.45rem;
    font-size: 1.1rem;
}

.modal-card p {
    margin: 0;
    color: #374151;
}

.modal-actions {
    margin-top: 0.9rem;
    display: flex;
    justify-content: flex-end;
    gap: 0.45rem;
}

.modal-delete-btn {
    border-color: #fca5a5;
    color: #b91c1c;
}

.modal-delete-btn:hover {
    background: #fef2f2;
}

.graph-hint {
    margin: 0 0 0.75rem;
    font-size: 0.9rem;
    color: #6b7280;
}

body.graph-page {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

body.graph-page .container {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    width: 100%;
    max-width: 900px;
}

body.graph-page .graph-card {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    margin-bottom: 0;
    width: 100%;
}

.graph-controls {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    margin-bottom: 0.75rem;
}

.graph-range-presets {
    display: flex;
    flex-wrap: nowrap;
    gap: 0.4rem;
    overflow-x: auto;
    padding-bottom: 0.15rem;
    -webkit-overflow-scrolling: touch;
}

.graph-chip {
    flex-shrink: 0;
    border: 1px solid #d1d5db;
    background: #fff;
    color: #374151;
    border-radius: 999px;
    padding: 0.4rem 0.75rem;
    font-size: 0.88rem;
    cursor: pointer;
}

.graph-chip-active {
    border-color: #2563eb;
    background: #eff6ff;
    color: #1d4ed8;
}

.graph-series-toggles {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.75rem 1.25rem;
    margin-top: 0.35rem;
    padding-top: 0.25rem;
}

.graph-toggle {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    font-size: 0.92rem;
    cursor: pointer;
}

.graph-toggle input {
    width: 1rem;
    height: 1rem;
    margin: 0;
}

.graph-canvas-wrap {
    position: relative;
    flex: 1;
    width: 100%;
    min-height: min(52vh, 480px);
    height: min(calc(100vh - 13rem), 720px);
}

.graph-canvas-wrap.hidden {
    display: none;
}

.graph-empty {
    padding: 1.5rem 0.5rem;
    text-align: center;
    color: #6b7280;
}

.graph-empty.hidden {
    display: none;
}

.graph-empty p {
    margin: 0.35rem 0;
}

@media (min-width: 700px) {
    .topbar {
        flex-direction: row;
        justify-content: space-between;
        align-items: center;
    }

    .form-grid {
        grid-template-columns: repeat(3, 1fr);
        align-items: end;
    }

    .form-grid .entry-note-label {
        grid-column: 1 / -1;
        align-self: start;
    }

    .form-grid .entry-note-field {
        /* Desktop: width spans weight→date row; height modest — mobile rule sets 3× single-line height. */
        min-height: 5rem;
    }

    .form-grid .btn-primary {
        grid-column: 1 / span 3;
        max-width: 220px;
    }

    .bmi-legend {
        grid-template-columns: repeat(4, minmax(0, 1fr));
        gap: 0.3rem;
    }

    .graph-controls {
        flex-direction: row;
        align-items: center;
        flex-wrap: wrap;
        width: 100%;
    }

    .graph-range-presets {
        flex-wrap: wrap;
        overflow-x: visible;
        flex: 1;
        min-width: min(100%, 36rem);
    }

    .graph-series-toggles {
        width: 100%;
        justify-content: center;
    }

    .graph-canvas-wrap {
        min-height: min(58vh, 520px);
        height: min(calc(100vh - 11rem), 820px);
    }
}

@media (max-width: 699px) {
    /* Three stacked fields share one visual “line height”; note uses triple that (scroll when taller). */
    .form-grid .entry-note-field {
        min-height: calc(3 * (0.55rem * 2 + 1.25rem));
    }

    .label-desktop {
        display: none;
    }

    .label-mobile {
        display: inline;
    }

    table {
        min-width: 540px;
    }

    th,
    td {
        padding: 0.35rem 0.3rem;
        font-size: 0.84rem;
    }

    /* Tighten the visual gap specifically between Date and Kg columns. */
    #review-table th:nth-child(1),
    #review-table td:nth-child(1) {
        padding-right: 0;
    }

    #review-table th:nth-child(2),
    #review-table td:nth-child(2) {
        padding-left: 0;
    }

    .sort-btn {
        gap: 0.2rem;
        font-size: 0.84rem;
    }

    .sort-icons {
        gap: 0.1rem;
    }

    .row-actions {
        gap: 0.25rem;
    }

    .btn-secondary {
        padding: 0.28rem 0.42rem;
        font-size: 0.8rem;
    }

    .bmi-legend {
        grid-template-columns: 1fr;
    }

    .btn-round-edit {
        min-width: 2.1rem;
        height: 2.1rem;
        font-size: 0.75rem;
    }

    .bmi-expand-indicator {
        margin-left: 0.38rem;
        font-size: 0.9rem;
    }
}

@media (max-width: 420px) {
    .bmi-marker {
        top: -8px;
        height: 22px;
    }

    .bmi-marker-value {
        font-size: 0.64rem;
    }

    .bmi-boundary-track {
        height: 22px;
    }

    .bmi-boundary-label {
        font-size: 0.62rem;
    }
}
