:root {
    color-scheme: light;
    --navy-950: #071d30;
    --navy-900: #0d3150;
    --navy-800: #164668;
    --blue-600: #1672a8;
    --blue-100: #e9f5fb;
    --teal-600: #07887d;
    --teal-100: #e3f6f2;
    --green-600: #168554;
    --green-100: #e8f7ef;
    --amber-600: #b56b04;
    --amber-100: #fff4d8;
    --red-600: #c0363f;
    --red-100: #fdebed;
    --slate-900: #172534;
    --slate-700: #405265;
    --slate-500: #718095;
    --slate-300: #ccd5df;
    --slate-200: #dde5ec;
    --slate-100: #eef3f7;
    --surface: #ffffff;
    --background: #f4f7fa;
    --shadow: 0 18px 48px rgba(13, 49, 80, 0.09);
    --radius-lg: 22px;
    --radius-md: 14px;
    --radius-sm: 10px;
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
    box-sizing: border-box;
}

html {
    min-height: 100%;
    background: var(--background);
    -webkit-text-size-adjust: 100%;
}

body {
    min-height: 100vh;
    margin: 0;
    color: var(--slate-900);
    background: var(--background);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
    line-height: 1.45;
}

button,
input,
select {
    font: inherit;
}

button,
a,
input,
select {
    -webkit-tap-highlight-color: transparent;
}

button {
    cursor: pointer;
}

a {
    color: inherit;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.is-hidden {
    display: none !important;
}

.page-shell,
.topbar-inner {
    width: min(1160px, calc(100% - 32px));
    margin-inline: auto;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 30;
    color: #fff;
    background: linear-gradient(120deg, var(--navy-950), var(--navy-900));
    box-shadow: 0 6px 20px rgba(7, 29, 48, 0.18);
}

.topbar > .topbar-inner {
    min-height: 70px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
}

.brand {
    display: inline-flex;
    align-items: center;
    gap: 11px;
    color: #fff;
    text-decoration: none;
}

.brand strong,
.brand small {
    display: block;
}

.brand strong {
    font-size: 1.03rem;
    letter-spacing: 0.01em;
}

.brand small {
    margin-top: 1px;
    color: #bcd1df;
    font-size: 0.75rem;
}

.brand-mark {
    width: 64px;
    height: 64px;
    display: grid;
    place-items: center;
    border-radius: 20px;
    color: #fff;
    background: linear-gradient(145deg, #16a69b, #0a766f);
    box-shadow: 0 10px 25px rgba(7, 136, 125, 0.22);
    font-weight: 800;
    letter-spacing: -0.04em;
}

.brand-mark-small {
    width: 40px;
    height: 40px;
    border-radius: 12px;
    font-size: 0.86rem;
    box-shadow: none;
}

.main-nav {
    border-top: 1px solid rgba(255, 255, 255, 0.09);
    background: rgba(5, 23, 38, 0.28);
}

.nav-inner {
    display: flex;
    gap: 6px;
}

.nav-link {
    position: relative;
    padding: 11px 15px 12px;
    color: #c7d9e5;
    text-decoration: none;
    font-size: 0.92rem;
    font-weight: 700;
}

.nav-link::after {
    position: absolute;
    left: 14px;
    right: 14px;
    bottom: 0;
    height: 3px;
    border-radius: 3px 3px 0 0;
    background: #22b8ab;
    content: "";
    opacity: 0;
}

.nav-link.is-active {
    color: #fff;
}

.nav-link.is-active::after {
    opacity: 1;
}

.safety-banner {
    color: #5b4513;
    background: #fff5d9;
    border-bottom: 1px solid #efd99c;
    font-size: 0.8rem;
    font-weight: 650;
}

.safety-banner .page-shell {
    padding-block: 8px;
}

.page-main {
    padding-top: 34px;
    padding-bottom: calc(64px + var(--safe-bottom));
}

.page-heading {
    max-width: 740px;
    margin-bottom: 28px;
}

.page-heading h1,
.history-heading h2 {
    margin: 0;
    color: var(--navy-950);
    letter-spacing: -0.035em;
    line-height: 1.08;
}

.page-heading h1 {
    font-size: clamp(2rem, 4vw, 3.25rem);
}

.page-heading p:not(.eyebrow) {
    margin: 12px 0 0;
    color: var(--slate-700);
    font-size: 1.02rem;
}

.compact-heading {
    margin-bottom: 22px;
}

.eyebrow {
    margin: 0 0 7px;
    color: var(--teal-600);
    font-size: 0.74rem;
    font-weight: 850;
    letter-spacing: 0.13em;
    text-transform: uppercase;
}

.calculator-layout {
    display: grid;
    grid-template-columns: minmax(0, 1.2fr) minmax(360px, 0.8fr);
    align-items: start;
    gap: 22px;
}

.card {
    border: 1px solid var(--slate-200);
    border-radius: var(--radius-lg);
    background: var(--surface);
    box-shadow: var(--shadow);
}

.builder-card,
.summary-card {
    padding: 25px;
}

.summary-card {
    position: sticky;
    top: 140px;
    overflow: hidden;
    transition: border-color 180ms ease, box-shadow 180ms ease;
}

.summary-card[data-status="safe"] {
    border-color: #9fd8bf;
}

.summary-card[data-status="near"] {
    border-color: #e9c168;
    box-shadow: 0 18px 48px rgba(181, 107, 4, 0.12);
}

.summary-card[data-status="over"] {
    border-color: #e3999e;
    box-shadow: 0 18px 48px rgba(192, 54, 63, 0.12);
}

.section-title-row,
.history-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 14px;
}

.section-title-row > div:first-child {
    display: flex;
    align-items: center;
    gap: 10px;
}

.section-title-row h2,
.history-heading h2 {
    font-size: 1.25rem;
}

.step-badge {
    flex: 0 0 auto;
    width: 28px;
    height: 28px;
    display: grid;
    place-items: center;
    border-radius: 9px;
    color: var(--navy-900);
    background: var(--blue-100);
    font-size: 0.8rem;
    font-weight: 850;
}

.factor-pill,
.count-badge {
    display: inline-flex;
    align-items: center;
    min-height: 28px;
    padding: 4px 9px;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 800;
    white-space: nowrap;
}

.factor-pill {
    color: #076b64;
    background: var(--teal-100);
}

.factor-pill.is-missing {
    color: var(--red-600);
    background: var(--red-100);
}

.count-badge {
    color: var(--slate-700);
    background: var(--slate-100);
}

.field {
    position: relative;
    display: grid;
    gap: 7px;
    margin-top: 18px;
}

.field > span:first-child {
    color: var(--slate-700);
    font-size: 0.79rem;
    font-weight: 800;
}

input,
select {
    width: 100%;
    min-height: 50px;
    padding: 11px 13px;
    color: var(--slate-900);
    background: #fff;
    border: 1px solid var(--slate-300);
    border-radius: var(--radius-sm);
    outline: 0;
    transition: border-color 150ms ease, box-shadow 150ms ease;
}

input::placeholder {
    color: #9aa7b4;
}

input:focus,
select:focus {
    border-color: var(--blue-600);
    box-shadow: 0 0 0 4px rgba(22, 114, 168, 0.12);
}

select {
    appearance: none;
    background-image: linear-gradient(45deg, transparent 50%, var(--slate-500) 50%), linear-gradient(135deg, var(--slate-500) 50%, transparent 50%);
    background-position: calc(100% - 18px) 21px, calc(100% - 13px) 21px;
    background-size: 5px 5px, 5px 5px;
    background-repeat: no-repeat;
}

.section-divider {
    height: 1px;
    margin: 26px 0 23px;
    background: var(--slate-200);
}

.field-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 13px;
}

.input-unit {
    position: relative;
}

.input-unit input {
    padding-right: 45px;
}

.input-unit > span {
    position: absolute;
    right: 14px;
    top: 50%;
    color: var(--slate-500);
    font-size: 0.83rem;
    font-weight: 800;
    transform: translateY(-50%);
    pointer-events: none;
}

.suggestions {
    position: absolute;
    z-index: 20;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    max-height: 245px;
    overflow-y: auto;
    padding: 6px;
    border: 1px solid var(--slate-200);
    border-radius: 13px;
    background: #fff;
    box-shadow: 0 15px 40px rgba(7, 29, 48, 0.16);
}

.suggestion {
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 10px 11px;
    border: 0;
    border-radius: 9px;
    color: var(--slate-900);
    background: transparent;
    text-align: left;
}

.suggestion:hover,
.suggestion:focus {
    background: var(--blue-100);
    outline: 0;
}

.suggestion small {
    color: var(--slate-500);
    white-space: nowrap;
}

.live-preview {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 6px 12px;
    margin: 16px 0;
    padding: 12px 13px;
    border-radius: 11px;
    color: var(--slate-700);
    background: var(--slate-100);
    font-size: 0.84rem;
}

.live-preview strong {
    color: var(--navy-900);
}

.button {
    min-height: 42px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 9px 14px;
    border: 1px solid transparent;
    border-radius: 11px;
    font-weight: 800;
    text-decoration: none;
    transition: transform 100ms ease, filter 150ms ease, background 150ms ease;
}

.button:active:not(:disabled) {
    transform: translateY(1px);
}

.button:disabled {
    cursor: not-allowed;
    opacity: 0.46;
}

.button-large {
    min-height: 50px;
    padding: 11px 17px;
}

.button-full {
    width: 100%;
}

.button-primary {
    color: #fff;
    background: linear-gradient(135deg, #0b8b81, #08736b);
    box-shadow: 0 8px 18px rgba(7, 136, 125, 0.19);
}

.button-primary:hover:not(:disabled) {
    filter: brightness(1.06);
}

.button-secondary {
    color: var(--navy-900);
    background: var(--blue-100);
    border-color: #c4dfed;
}

.button-ghost {
    color: #d7e6ef;
    background: rgba(255, 255, 255, 0.07);
    border-color: rgba(255, 255, 255, 0.12);
}

.empty-state {
    display: grid;
    justify-items: center;
    gap: 8px;
    padding: 32px 16px 25px;
    color: var(--slate-500);
    text-align: center;
}

.empty-state p {
    margin: 0;
    font-size: 0.88rem;
}

.empty-icon {
    width: 42px;
    height: 42px;
    display: grid;
    place-items: center;
    border: 1px dashed #aab8c6;
    border-radius: 50%;
    font-size: 1.5rem;
}

.meal-items {
    display: grid;
    gap: 9px;
    margin-top: 18px;
}

.meal-item {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto auto;
    align-items: center;
    gap: 10px;
    padding: 11px 12px;
    border: 1px solid var(--slate-200);
    border-radius: 12px;
    background: #fbfcfd;
}

.meal-item strong,
.meal-item small {
    display: block;
}

.meal-item strong {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 0.9rem;
}

.meal-item small {
    margin-top: 2px;
    color: var(--slate-500);
    font-size: 0.72rem;
}

.item-ke {
    color: var(--navy-900);
    font-size: 0.82rem;
    font-weight: 850;
    white-space: nowrap;
}

.remove-item,
.delete-meal {
    border: 0;
    color: var(--red-600);
    background: transparent;
    font-weight: 850;
}

.remove-item {
    width: 31px;
    height: 31px;
    border-radius: 8px;
    font-size: 1.12rem;
}

.remove-item:hover {
    background: var(--red-100);
}

.limit-block {
    margin: 22px 0 16px;
}

.limit-label {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 7px;
    color: var(--slate-500);
    font-size: 0.74rem;
}

.limit-label strong {
    color: var(--slate-700);
}

.progress-track {
    width: 100%;
    height: 9px;
    display: block;
    overflow: hidden;
    appearance: none;
    border: 0;
    border-radius: 999px;
    background: var(--slate-100);
}

.progress-track::-webkit-progress-bar {
    border-radius: inherit;
    background: var(--slate-100);
}

.progress-track::-webkit-progress-value {
    border-radius: inherit;
    background: var(--green-600);
}

.progress-track::-moz-progress-bar {
    border-radius: inherit;
    background: var(--green-600);
}

.summary-card[data-status="near"] .progress-track::-webkit-progress-value {
    background: var(--amber-600);
}

.summary-card[data-status="near"] .progress-track::-moz-progress-bar {
    background: var(--amber-600);
}

.summary-card[data-status="over"] .progress-track::-webkit-progress-value {
    background: var(--red-600);
}

.summary-card[data-status="over"] .progress-track::-moz-progress-bar {
    background: var(--red-600);
}

.limit-warning {
    min-height: 18px;
    margin: 7px 0 0;
    color: var(--slate-500);
    font-size: 0.73rem;
    font-weight: 700;
}

.summary-card[data-status="near"] .limit-warning {
    color: var(--amber-600);
}

.summary-card[data-status="over"] .limit-warning {
    color: var(--red-600);
}

.totals-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    border: 1px solid var(--slate-200);
    border-radius: 12px;
}

.totals-grid > div {
    padding: 12px 13px;
}

.totals-grid > div + div {
    border-left: 1px solid var(--slate-200);
}

.totals-grid span,
.totals-grid strong {
    display: block;
}

.totals-grid span {
    color: var(--slate-500);
    font-size: 0.72rem;
}

.totals-grid strong {
    margin-top: 2px;
    color: var(--navy-900);
    font-size: 1.05rem;
}

.insulin-result {
    margin-top: 12px;
    padding: 15px;
    border-radius: 14px;
    color: #fff;
    background: linear-gradient(135deg, var(--navy-900), var(--navy-800));
}

.insulin-result > span,
.insulin-result > strong,
.insulin-result > small {
    display: block;
}

.insulin-result > span {
    color: #c2d9e7;
    font-size: 0.73rem;
    font-weight: 700;
}

.insulin-result > strong {
    margin-top: 2px;
    font-size: 2rem;
    line-height: 1.05;
}

.insulin-result > small {
    margin-top: 5px;
    color: #c2d9e7;
    font-size: 0.72rem;
}

.confirm-row {
    display: flex;
    align-items: flex-start;
    gap: 10px;
    margin: 22px 0;
    padding: 13px;
    border: 1px solid var(--slate-200);
    border-radius: 12px;
    color: var(--slate-700);
    background: #fbfcfd;
    font-size: 0.82rem;
    font-weight: 650;
}

.confirm-row input {
    flex: 0 0 auto;
    width: 19px;
    min-height: 19px;
    margin: 1px 0 0;
    accent-color: var(--teal-600);
}

.confirm-compact {
    margin: 13px 0;
    padding: 10px;
    font-size: 0.73rem;
}

.history-section {
    margin-top: 42px;
}

.history-heading {
    margin-bottom: 16px;
}

.date-navigator {
    display: flex;
    align-items: center;
    gap: 6px;
}

.date-navigator input {
    min-height: 42px;
    width: 155px;
    padding: 8px 9px;
    font-size: 0.83rem;
    font-weight: 750;
}

.icon-button {
    width: 42px;
    height: 42px;
    display: grid;
    place-items: center;
    border: 1px solid var(--slate-300);
    border-radius: 10px;
    color: var(--navy-900);
    background: #fff;
    font-size: 1.45rem;
}

.history-list {
    display: grid;
    gap: 13px;
}

.history-card {
    display: grid;
    grid-template-columns: 78px minmax(0, 1fr) auto;
    gap: 16px;
    padding: 18px;
    border: 1px solid var(--slate-200);
    border-radius: 16px;
    background: #fff;
    box-shadow: 0 8px 25px rgba(13, 49, 80, 0.05);
}

.history-time {
    color: var(--navy-900);
    font-size: 1.05rem;
    font-weight: 850;
}

.history-time small {
    display: block;
    margin-top: 2px;
    color: var(--slate-500);
    font-size: 0.68rem;
    font-weight: 700;
}

.history-content h3 {
    margin: 0;
    color: var(--slate-900);
    font-size: 1rem;
}

.history-products {
    margin: 5px 0 0;
    color: var(--slate-700);
    font-size: 0.78rem;
}

.history-metrics {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 10px;
}

.metric {
    padding: 4px 8px;
    border-radius: 999px;
    color: var(--slate-700);
    background: var(--slate-100);
    font-size: 0.7rem;
    font-weight: 800;
}

.metric.metric-insulin {
    color: #075e59;
    background: var(--teal-100);
}

.metric.metric-over {
    color: var(--red-600);
    background: var(--red-100);
}

.history-actions {
    display: flex;
    align-items: flex-start;
}

.delete-meal {
    padding: 6px;
    font-size: 0.72rem;
}

.history-empty,
.history-loading {
    padding: 28px;
    border: 1px dashed var(--slate-300);
    border-radius: 16px;
    color: var(--slate-500);
    background: rgba(255, 255, 255, 0.55);
    text-align: center;
}

.alert {
    margin: 0 0 18px;
    padding: 12px 14px;
    border: 1px solid transparent;
    border-radius: 12px;
    font-size: 0.84rem;
    font-weight: 700;
}

.alert-danger {
    color: #8f252c;
    background: var(--red-100);
    border-color: #f2c0c4;
}

.alert-warning {
    color: #7a4a08;
    background: var(--amber-100);
    border-color: #efd18a;
}

.alert-info,
.alert-success {
    color: #08655f;
    background: var(--teal-100);
    border-color: #aedfd9;
}

.factor-form {
    max-width: 940px;
}

.factor-grid {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 14px;
}

.factor-card {
    position: relative;
    display: grid;
    gap: 12px;
    padding: 18px;
    border: 1px solid var(--slate-200);
    border-radius: 16px;
    background: #fff;
    box-shadow: 0 8px 25px rgba(13, 49, 80, 0.045);
}

.factor-sequence {
    position: absolute;
    top: 14px;
    right: 14px;
    width: 24px;
    height: 24px;
    display: grid;
    place-items: center;
    border-radius: 8px;
    color: var(--slate-500);
    background: var(--slate-100);
    font-size: 0.7rem;
    font-weight: 850;
}

.factor-label {
    max-width: calc(100% - 28px);
    min-height: 42px;
    color: var(--navy-950);
    font-weight: 850;
}

.factor-input-wrap {
    position: relative;
}

.factor-input-wrap input {
    padding-right: 70px;
    font-size: 1.2rem;
    font-weight: 850;
}

.factor-input-wrap > span {
    position: absolute;
    right: 12px;
    top: 50%;
    color: var(--slate-500);
    font-size: 0.72rem;
    font-weight: 800;
    transform: translateY(-50%);
}

.form-actions {
    display: flex;
    align-items: center;
    gap: 14px;
}

.muted {
    color: var(--slate-500);
    font-size: 0.79rem;
}

.auth-body {
    display: grid;
    place-items: center;
    padding: 24px;
    background:
        radial-gradient(circle at 20% 10%, rgba(24, 163, 152, 0.2), transparent 32%),
        radial-gradient(circle at 90% 95%, rgba(22, 114, 168, 0.22), transparent 35%),
        linear-gradient(145deg, #071d30, #0d3150);
}

.auth-shell {
    width: min(100%, 450px);
}

.auth-card {
    padding: 34px;
    border: 1px solid rgba(255, 255, 255, 0.18);
    border-radius: 26px;
    background: rgba(255, 255, 255, 0.97);
    box-shadow: 0 28px 90px rgba(0, 0, 0, 0.24);
}

.auth-card .brand-mark {
    margin-bottom: 25px;
}

.auth-card h1 {
    margin: 0;
    color: var(--navy-950);
    font-size: 2rem;
    letter-spacing: -0.04em;
}

.auth-copy {
    margin: 11px 0 21px;
    color: var(--slate-700);
    font-size: 0.92rem;
}

.stack-form {
    display: grid;
    gap: 4px;
}

.privacy-note {
    margin: 19px 0 0;
    color: var(--slate-500);
    font-size: 0.72rem;
    text-align: center;
}

@media (max-width: 900px) {
    .calculator-layout {
        grid-template-columns: 1fr;
    }

    .summary-card {
        position: static;
    }

    .factor-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (max-width: 620px) {
    .page-shell,
    .topbar-inner {
        width: min(100% - 22px, 1160px);
    }

    .topbar > .topbar-inner {
        min-height: 58px;
    }

    .brand small {
        display: none;
    }

    .brand-mark-small {
        width: 36px;
        height: 36px;
    }

    .button-ghost {
        min-height: 38px;
        padding-inline: 10px;
        font-size: 0.78rem;
    }

    .nav-link {
        flex: 1;
        padding-inline: 8px;
        text-align: center;
        font-size: 0.83rem;
    }

    .safety-banner {
        font-size: 0.72rem;
    }

    .page-main {
        padding-top: 25px;
        padding-bottom: calc(34px + var(--safe-bottom));
    }

    .page-heading h1 {
        font-size: 2.05rem;
    }

    .page-heading p:not(.eyebrow) {
        font-size: 0.92rem;
    }

    .builder-card,
    .summary-card {
        padding: 18px;
        border-radius: 17px;
    }

    .section-title-row {
        align-items: flex-start;
    }

    .section-title-row h2 {
        font-size: 1.08rem;
    }

    .factor-pill {
        max-width: 135px;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .field-row {
        grid-template-columns: 1fr;
        gap: 0;
    }

    input,
    select {
        min-height: 52px;
        font-size: 16px;
    }

    .history-heading {
        align-items: flex-start;
        flex-direction: column;
    }

    .date-navigator {
        width: 100%;
    }

    .date-navigator label {
        flex: 1;
    }

    .date-navigator input {
        width: 100%;
    }

    .history-card {
        grid-template-columns: 58px minmax(0, 1fr);
        gap: 11px;
        padding: 14px;
    }

    .history-actions {
        grid-column: 2;
        justify-content: flex-end;
    }

    .factor-grid {
        grid-template-columns: 1fr;
    }

    .factor-card {
        padding: 16px;
    }

    .factor-label {
        min-height: auto;
    }

    .form-actions {
        align-items: stretch;
        flex-direction: column;
    }

    .auth-body {
        padding: 13px;
    }

    .auth-card {
        padding: 25px 20px;
        border-radius: 21px;
    }
}

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        scroll-behavior: auto !important;
        transition-duration: 0.01ms !important;
    }
}
