/* ==========================================================================
   SMI-V design system — โรงพยาบาลสีชมพู
   Shared tokens + components for every page. Page-specific layout lives in
   each page's own <style>.
   ========================================================================== */

/* ---------- Tokens ---------- */
:root {
    color-scheme: light;

    --font-display: 'Anuphan', 'IBM Plex Sans Thai Looped', system-ui, sans-serif;
    --font-body: 'IBM Plex Sans Thai Looped', 'Anuphan', system-ui, sans-serif;

    --paper: #F2F4F8;
    --surface: #FFFFFF;
    --surface-2: #F7F8FB;
    --surface-3: #ECEFF4;
    --ink: #1A2233;
    --ink-2: #4E586C;
    --ink-3: #848DA0;
    --line: #E1E5EC;
    --line-strong: #CAD1DC;

    /* สีชมพู — orchid brand */
    --brand: #8B2F7A;
    --brand-strong: #6F2461;
    --brand-ink: #FFFFFF;
    --brand-soft: #F5E6F1;
    --brand-line: #E3C4DC;

    /* Risk scale — the only saturated colors in the UI */
    --green: #2E9A57;
    --green-soft: #E2F3E8;
    --green-ink: #1D6B3B;
    --yellow: #E3AE0B;
    --yellow-soft: #FCF1CF;
    --yellow-ink: #7A5A00;
    --orange: #E8741C;
    --orange-soft: #FDE8D7;
    --orange-ink: #9A440A;
    --red: #D9342B;
    --red-soft: #FCE4E2;
    --red-ink: #A3201A;
    --none: #B5BDCA;

    --login-panel: #1D1427;
    --login-panel-ink: #F3E9F1;

    --shadow-1: 0 1px 2px rgba(22, 30, 48, .06), 0 1px 1px rgba(22, 30, 48, .04);
    --shadow-2: 0 10px 28px -10px rgba(22, 30, 48, .22), 0 2px 6px rgba(22, 30, 48, .05);
    --shadow-3: 0 28px 70px -18px rgba(22, 30, 48, .42), 0 6px 16px rgba(22, 30, 48, .08);
    --scrim: rgba(14, 18, 28, .46);

    --r-xs: 6px;
    --r-sm: 10px;
    --r-md: 14px;
    --r-lg: 20px;

    --t-xs: 12px;
    --t-sm: 13.5px;
    --t-md: 15px;
    --t-lg: 18px;
    --t-xl: 22px;
    --t-2xl: 28px;
    --t-3xl: 40px;
    --t-4xl: 56px;

    --ease-out: cubic-bezier(.22, 1, .36, 1);
    --ease-spring: cubic-bezier(.34, 1.56, .64, 1);
    --ease-in: cubic-bezier(.55, 0, 1, .45);
    --dur-1: .15s;
    --dur-2: .28s;
    --dur-3: .5s;

    --topbar-h: 60px;
}

:root[data-theme="dark"] {
    color-scheme: dark;
    --paper: #0D121C;
    --surface: #141B28;
    --surface-2: #19212F;
    --surface-3: #212B3C;
    --ink: #E8ECF4;
    --ink-2: #A8B2C5;
    --ink-3: #6E7990;
    --line: #243045;
    --line-strong: #334058;
    --brand: #D98BC8;
    --brand-strong: #EBA9DC;
    --brand-ink: #241020;
    --brand-soft: rgba(217, 139, 200, .14);
    --brand-line: rgba(217, 139, 200, .35);
    --green: #45BE76;
    --green-soft: rgba(69, 190, 118, .15);
    --green-ink: #7FD9A3;
    --yellow: #F2C53D;
    --yellow-soft: rgba(242, 197, 61, .15);
    --yellow-ink: #F5D57A;
    --orange: #F68F3C;
    --orange-soft: rgba(246, 143, 60, .16);
    --orange-ink: #FAB27A;
    --red: #F2564C;
    --red-soft: rgba(242, 86, 76, .16);
    --red-ink: #FF8E86;
    --none: #4A566C;
    --login-panel: #160F1E;
    --shadow-1: 0 1px 2px rgba(0, 0, 0, .4);
    --shadow-2: 0 12px 30px -10px rgba(0, 0, 0, .6);
    --shadow-3: 0 30px 80px -16px rgba(0, 0, 0, .75);
    --scrim: rgba(3, 6, 12, .62);
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        color-scheme: dark;
        --paper: #0D121C;
        --surface: #141B28;
        --surface-2: #19212F;
        --surface-3: #212B3C;
        --ink: #E8ECF4;
        --ink-2: #A8B2C5;
        --ink-3: #6E7990;
        --line: #243045;
        --line-strong: #334058;
        --brand: #D98BC8;
        --brand-strong: #EBA9DC;
        --brand-ink: #241020;
        --brand-soft: rgba(217, 139, 200, .14);
        --brand-line: rgba(217, 139, 200, .35);
        --green: #45BE76;
        --green-soft: rgba(69, 190, 118, .15);
        --green-ink: #7FD9A3;
        --yellow: #F2C53D;
        --yellow-soft: rgba(242, 197, 61, .15);
        --yellow-ink: #F5D57A;
        --orange: #F68F3C;
        --orange-soft: rgba(246, 143, 60, .16);
        --orange-ink: #FAB27A;
        --red: #F2564C;
        --red-soft: rgba(242, 86, 76, .16);
        --red-ink: #FF8E86;
        --none: #4A566C;
        --login-panel: #160F1E;
        --shadow-1: 0 1px 2px rgba(0, 0, 0, .4);
        --shadow-2: 0 12px 30px -10px rgba(0, 0, 0, .6);
        --shadow-3: 0 30px 80px -16px rgba(0, 0, 0, .75);
        --scrim: rgba(3, 6, 12, .62);
    }
}

/* ---------- Base ---------- */
*,
*::before,
*::after {
    box-sizing: border-box;
}

* {
    margin: 0;
    padding: 0;
}

html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    font-family: var(--font-body);
    font-size: var(--t-md);
    line-height: 1.6;
    color: var(--ink);
    background: var(--paper);
    -webkit-font-smoothing: antialiased;
    -webkit-tap-highlight-color: transparent;
}

h1,
h2,
h3,
h4 {
    font-family: var(--font-display);
    font-weight: 600;
    line-height: 1.3;
    color: var(--ink);
}

button,
input,
select,
textarea {
    font: inherit;
    color: inherit;
}

button {
    cursor: pointer;
    background: none;
    border: 0;
}

a {
    color: var(--brand);
}

img {
    max-width: 100%;
    display: block;
}

[hidden] {
    display: none !important;
}

.num {
    font-family: var(--font-display);
    font-variant-numeric: tabular-nums;
}

:focus-visible {
    outline: 2.5px solid var(--brand);
    outline-offset: 2px;
    border-radius: 4px;
}

::selection {
    background: var(--brand-soft);
}

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

.icon {
    width: 1.15em;
    height: 1.15em;
    flex-shrink: 0;
    stroke: currentColor;
    fill: none;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

/* Scrollbars */
* {
    scrollbar-width: thin;
    scrollbar-color: var(--line-strong) transparent;
}

::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-thumb {
    background: var(--line-strong);
    border-radius: 8px;
    border: 2px solid transparent;
    background-clip: padding-box;
}

/* ---------- Buttons ---------- */
.btn {
    --btn-h: 42px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    height: var(--btn-h);
    padding: 0 16px;
    border-radius: var(--r-sm);
    font-family: var(--font-display);
    font-weight: 600;
    font-size: 14.5px;
    line-height: 1;
    white-space: nowrap;
    text-decoration: none;
    border: 1.5px solid transparent;
    transition: background var(--dur-1), border-color var(--dur-1), color var(--dur-1), transform var(--dur-1) var(--ease-out), box-shadow var(--dur-2);
    user-select: none;
    position: relative;
}

.btn:active:not(:disabled) {
    transform: scale(.97);
}

.btn:disabled {
    opacity: .5;
    cursor: not-allowed;
}

.btn-primary {
    background: var(--brand);
    color: var(--brand-ink);
}

.btn-primary:hover:not(:disabled) {
    background: var(--brand-strong);
}

.btn-secondary {
    background: var(--surface);
    border-color: var(--line-strong);
    color: var(--ink);
}

.btn-secondary:hover:not(:disabled) {
    border-color: var(--ink-3);
    background: var(--surface-2);
}

.btn-ghost {
    color: var(--ink-2);
}

.btn-ghost:hover:not(:disabled) {
    background: var(--surface-3);
    color: var(--ink);
}

.btn-danger {
    background: var(--red);
    color: #fff;
}

.btn-danger:hover:not(:disabled) {
    filter: brightness(.92);
}

.btn-danger-soft {
    background: var(--red-soft);
    color: var(--red-ink);
}

.btn-danger-soft:hover:not(:disabled) {
    border-color: var(--red);
}

.btn-sm {
    --btn-h: 34px;
    padding: 0 12px;
    font-size: 13px;
    border-radius: 8px;
}

.btn-lg {
    --btn-h: 52px;
    padding: 0 22px;
    font-size: 16px;
    border-radius: 12px;
}

.btn-block {
    width: 100%;
}

.btn-icon {
    width: var(--btn-h);
    padding: 0;
}

.btn .icon {
    width: 18px;
    height: 18px;
}

.btn.is-loading {
    color: transparent !important;
    pointer-events: none;
}

.btn.is-loading::after {
    content: '';
    position: absolute;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    border: 2.5px solid currentColor;
    border-color: var(--spinner, #fff) var(--spinner, #fff) transparent transparent;
    animation: spin .7s linear infinite;
}

.btn-secondary.is-loading,
.btn-ghost.is-loading {
    --spinner: var(--ink-2);
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* ---------- Form controls ---------- */
.field {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.field+.field,
.field-row+.field,
.field+.field-row {
    margin-top: 16px;
}

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

.field-row .field+.field {
    margin-top: 0;
}

.label {
    font-family: var(--font-display);
    font-size: var(--t-sm);
    font-weight: 600;
    color: var(--ink-2);
}

.label .req {
    color: var(--red);
    margin-left: 2px;
}

.hint {
    font-size: var(--t-xs);
    color: var(--ink-3);
}

.input,
.select,
.textarea {
    width: 100%;
    min-height: 44px;
    padding: 9px 14px;
    border-radius: var(--r-sm);
    border: 1.5px solid var(--line);
    background: var(--surface);
    color: var(--ink);
    font-size: var(--t-md);
    transition: border-color var(--dur-1), box-shadow var(--dur-2), background var(--dur-1);
}

.input::placeholder,
.textarea::placeholder {
    color: var(--ink-3);
}

.input:hover,
.select:hover,
.textarea:hover {
    border-color: var(--line-strong);
}

.input:focus,
.select:focus,
.textarea:focus {
    outline: none;
    border-color: var(--brand);
    box-shadow: 0 0 0 4px var(--brand-soft);
}

.input[aria-invalid="true"] {
    border-color: var(--red);
    box-shadow: 0 0 0 4px var(--red-soft);
}

.select {
    appearance: none;
    -webkit-appearance: none;
    padding-right: 40px;
    cursor: pointer;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='18' height='18' viewBox='0 0 24 24' fill='none' stroke='%238790A3' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 12px center;
}

.select:disabled {
    opacity: .55;
    cursor: not-allowed;
}

.textarea {
    resize: vertical;
    min-height: 88px;
    line-height: 1.55;
}

.input-wrap {
    position: relative;
}

.input-wrap>.icon {
    position: absolute;
    left: 13px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--ink-3);
    pointer-events: none;
}

.input-wrap>.input {
    padding-left: 40px;
}

.input-wrap>.input-action {
    position: absolute;
    right: 4px;
    top: 50%;
    transform: translateY(-50%);
    width: 36px;
    height: 36px;
    border-radius: 8px;
    display: grid;
    place-items: center;
    color: var(--ink-3);
}

.input-wrap>.input-action:hover {
    color: var(--ink);
    background: var(--surface-3);
}

/* Risk chooser (radio chips) */
.risk-choice {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
}

.risk-choice input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}

.risk-choice label {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 2px;
    padding: 10px 12px;
    border-radius: var(--r-sm);
    border: 1.5px solid var(--line);
    background: var(--surface);
    cursor: pointer;
    transition: border-color var(--dur-1), background var(--dur-2), transform var(--dur-1) var(--ease-out);
    position: relative;
}

.risk-choice label:active {
    transform: scale(.97);
}

.risk-choice label::before {
    content: '';
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: var(--c);
    box-shadow: 0 0 0 3px var(--c-soft);
    margin-bottom: 6px;
    transition: transform var(--dur-2) var(--ease-spring);
}

.risk-choice b {
    font-family: var(--font-display);
    font-weight: 600;
    font-size: 14px;
}

.risk-choice small {
    font-size: 11.5px;
    color: var(--ink-3);
    line-height: 1.35;
}

.risk-choice input:checked+label {
    border-color: var(--c);
    background: var(--c-soft);
}

.risk-choice input:checked+label b {
    color: var(--c-ink);
}

.risk-choice input:checked+label::before {
    transform: scale(1.25);
}

.risk-choice input:focus-visible+label {
    outline: 2.5px solid var(--brand);
    outline-offset: 2px;
}

[data-risk="green"] {
    --c: var(--green);
    --c-soft: var(--green-soft);
    --c-ink: var(--green-ink);
}

[data-risk="yellow"] {
    --c: var(--yellow);
    --c-soft: var(--yellow-soft);
    --c-ink: var(--yellow-ink);
}

[data-risk="orange"] {
    --c: var(--orange);
    --c-soft: var(--orange-soft);
    --c-ink: var(--orange-ink);
}

[data-risk="red"] {
    --c: var(--red);
    --c-soft: var(--red-soft);
    --c-ink: var(--red-ink);
}

/* Photo drop zone */
.dropzone {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 12px;
    border-radius: var(--r-sm);
    border: 1.5px dashed var(--line-strong);
    background: var(--surface-2);
    cursor: pointer;
    transition: border-color var(--dur-1), background var(--dur-1);
}

.dropzone:hover,
.dropzone.is-over {
    border-color: var(--brand);
    background: var(--brand-soft);
}

.dropzone input {
    position: absolute;
    opacity: 0;
    width: 1px;
    height: 1px;
}

.dropzone .dz-thumb {
    width: 56px;
    height: 56px;
    border-radius: 12px;
    background: var(--surface-3);
    display: grid;
    place-items: center;
    color: var(--ink-3);
    overflow: hidden;
    flex-shrink: 0;
}

.dropzone .dz-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    animation: pop var(--dur-3) var(--ease-spring);
}

.dropzone .dz-text {
    font-size: var(--t-sm);
    color: var(--ink-2);
    line-height: 1.45;
}

.dropzone .dz-text b {
    display: block;
    color: var(--ink);
    font-family: var(--font-display);
    font-weight: 600;
}

/* ---------- Pills, dots, badges ---------- */
.risk-pill {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 24px;
    padding: 0 10px 0 8px;
    border-radius: 999px;
    background: var(--c-soft, var(--surface-3));
    color: var(--c-ink, var(--ink-2));
    font-family: var(--font-display);
    font-size: 12.5px;
    font-weight: 600;
    white-space: nowrap;
}

.risk-pill::before {
    content: '';
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--c, var(--ink-3));
}

.risk-pill[data-risk="red"]::before {
    animation: beacon 1.6s var(--ease-out) infinite;
}

.dot {
    width: 9px;
    height: 9px;
    border-radius: 50%;
    background: var(--c, var(--ink-3));
    flex-shrink: 0;
}

@keyframes beacon {
    0% {
        box-shadow: 0 0 0 0 color-mix(in srgb, var(--red) 60%, transparent);
    }

    80%,
    100% {
        box-shadow: 0 0 0 7px transparent;
    }
}

.count-badge {
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: 999px;
    background: var(--red);
    color: #fff;
    font-family: var(--font-display);
    font-size: 11.5px;
    font-weight: 700;
    display: inline-grid;
    place-items: center;
    line-height: 1;
}

.count-badge.is-bump {
    animation: bump .5s var(--ease-spring);
}

@keyframes bump {
    0% {
        transform: scale(1);
    }

    40% {
        transform: scale(1.35);
    }

    100% {
        transform: scale(1);
    }
}

.tag {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    height: 22px;
    padding: 0 8px;
    border-radius: var(--r-xs);
    font-size: 12px;
    font-weight: 500;
    background: var(--surface-3);
    color: var(--ink-2);
    white-space: nowrap;
}

.tag .icon {
    width: 13px;
    height: 13px;
}

.tag-ok {
    background: var(--green-soft);
    color: var(--green-ink);
}

.tag-warn {
    background: var(--yellow-soft);
    color: var(--yellow-ink);
}

.tag-danger {
    background: var(--red-soft);
    color: var(--red-ink);
}

/* Avatar */
.avatar {
    --size: 40px;
    width: var(--size);
    height: var(--size);
    border-radius: 50%;
    flex-shrink: 0;
    display: grid;
    place-items: center;
    font-family: var(--font-display);
    font-weight: 600;
    font-size: calc(var(--size) * .4);
    background: var(--c-soft, var(--surface-3));
    color: var(--c-ink, var(--ink-2));
    box-shadow: inset 0 0 0 2px var(--c, var(--line));
    overflow: hidden;
}

.avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* ---------- Topbar ---------- */
.topbar {
    height: var(--topbar-h);
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 16px 0 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--line);
    position: relative;
    z-index: 600;
}

.brand {
    display: flex;
    align-items: center;
    gap: 11px;
    text-decoration: none;
    color: inherit;
    min-width: 0;
}

.brand img {
    width: 34px;
    height: 34px;
    border-radius: 9px;
    object-fit: contain;
    background: #fff;
    padding: 2px;
}

.drawer:focus {
    outline: none;
}

.brand-text {
    display: flex;
    flex-direction: column;
    line-height: 1.15;
    min-width: 0;
}

.brand-text b {
    font-family: var(--font-display);
    font-weight: 700;
    font-size: 16.5px;
    letter-spacing: -.01em;
}

.brand-text span {
    font-size: 12px;
    color: var(--ink-3);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.topnav {
    display: flex;
    gap: 2px;
    margin-left: 18px;
}

.topnav a {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    height: 36px;
    padding: 0 12px;
    border-radius: 9px;
    font-family: var(--font-display);
    font-weight: 500;
    font-size: 14px;
    color: var(--ink-2);
    text-decoration: none;
    transition: background var(--dur-1), color var(--dur-1);
}

.topnav a:hover {
    background: var(--surface-3);
    color: var(--ink);
}

.topnav a[aria-current="page"] {
    color: var(--brand);
    background: var(--brand-soft);
}

.topbar-spacer {
    flex: 1;
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 4px;
}

.user-chip {
    display: flex;
    align-items: center;
    gap: 9px;
    padding: 4px 6px 4px 4px;
    margin-left: 6px;
    border-radius: 999px;
    border: 1px solid var(--line);
}

.user-chip .avatar {
    --size: 30px;
    --c: var(--brand);
    --c-soft: var(--brand-soft);
    --c-ink: var(--brand);
}

.user-chip-text {
    display: flex;
    flex-direction: column;
    line-height: 1.15;
    padding-right: 4px;
}

.user-chip-text b {
    font-family: var(--font-display);
    font-size: 13px;
    font-weight: 600;
    max-width: 140px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-chip-text span {
    font-size: 11px;
    color: var(--ink-3);
}

.toggle-on {
    color: var(--brand) !important;
    background: var(--brand-soft) !important;
}

@media (max-width: 760px) {
    .topbar {
        padding: 0 8px 0 12px;
        gap: 6px;
    }

    .topnav,
    .user-chip-text,
    .hide-sm {
        display: none !important;
    }

    .user-chip {
        padding: 3px;
        margin-left: 2px;
    }
}

/* ---------- Tabs & segmented control ---------- */
.tabs {
    position: relative;
    display: flex;
    gap: 2px;
    border-bottom: 1px solid var(--line);
    padding: 0 12px;
    overflow-x: auto;
    scrollbar-width: none;
}

.tabs::-webkit-scrollbar {
    display: none;
}

.tab {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    height: 48px;
    padding: 0 12px;
    font-family: var(--font-display);
    font-weight: 500;
    font-size: 14.5px;
    color: var(--ink-3);
    white-space: nowrap;
    transition: color var(--dur-1);
}

.tab:hover {
    color: var(--ink);
}

.tab[aria-selected="true"] {
    color: var(--ink);
    font-weight: 600;
}

.tab .icon {
    width: 17px;
    height: 17px;
}

.tab-indicator {
    position: absolute;
    bottom: -1px;
    left: 0;
    height: 2.5px;
    border-radius: 2px 2px 0 0;
    background: var(--brand);
    transition: transform var(--dur-3) var(--ease-out), width var(--dur-3) var(--ease-out);
    pointer-events: none;
}

.tab-panel {
    animation: panelIn var(--dur-3) var(--ease-out);
}

@keyframes panelIn {
    from {
        opacity: 0;
        transform: translateY(6px);
    }
}

.seg {
    position: relative;
    display: inline-flex;
    padding: 3px;
    border-radius: 12px;
    background: var(--surface-3);
    isolation: isolate;
}

.seg button {
    position: relative;
    z-index: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    height: 34px;
    padding: 0 12px;
    border-radius: 9px;
    font-family: var(--font-display);
    font-weight: 500;
    font-size: 13.5px;
    color: var(--ink-2);
    white-space: nowrap;
    transition: color var(--dur-2);
}

.seg button[aria-pressed="true"] {
    color: var(--ink);
    font-weight: 600;
}

.seg .tab-indicator {
    top: 3px;
    bottom: 3px;
    height: auto;
    border-radius: 9px;
    background: var(--surface);
    box-shadow: var(--shadow-1);
    z-index: 0;
}

/* ---------- Drawer / bottom sheet ---------- */
.scrim {
    position: fixed;
    inset: 0;
    background: var(--scrim);
    z-index: 1200;
    opacity: 0;
    transition: opacity var(--dur-3) var(--ease-out);
    backdrop-filter: blur(2px);
}

.scrim.is-open {
    opacity: 1;
}

.drawer {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(540px, 100vw);
    z-index: 1210;
    background: var(--surface);
    box-shadow: var(--shadow-3);
    display: flex;
    flex-direction: column;
    transform: translateX(104%);
    transition: transform .46s var(--ease-out), visibility 0s linear .46s;
    visibility: hidden;
}

.drawer.is-open {
    transform: none;
    visibility: visible;
    transition: transform .46s var(--ease-out), visibility 0s;
}

.drawer-head {
    display: flex;
    align-items: flex-start;
    gap: 14px;
    padding: 20px 20px 16px 24px;
    border-bottom: 1px solid var(--line);
}

.drawer-head h2 {
    font-size: var(--t-xl);
    line-height: 1.25;
}

.drawer-body {
    flex: 1;
    overflow-y: auto;
    padding: 20px 24px 28px;
    overscroll-behavior: contain;
}

.drawer-foot {
    padding: 14px 24px;
    border-top: 1px solid var(--line);
    display: flex;
    gap: 10px;
    background: var(--surface);
}

.drawer-grabber {
    display: none;
}

@media (max-width: 720px) {
    .drawer {
        top: auto;
        left: 0;
        width: 100%;
        max-height: 92vh;
        max-height: 92dvh;
        border-radius: var(--r-lg) var(--r-lg) 0 0;
        transform: translateY(104%);
    }

    .drawer-grabber {
        display: block;
        width: 40px;
        height: 4.5px;
        border-radius: 4px;
        background: var(--line-strong);
        margin: 9px auto -6px;
        flex-shrink: 0;
    }

    .drawer-head {
        padding: 16px 16px 14px 20px;
    }

    .drawer-body {
        padding: 18px 20px 24px;
    }

    .drawer-foot {
        padding: 12px 20px calc(12px + env(safe-area-inset-bottom));
    }
}

/* ---------- Dialog ---------- */
.dialog-layer {
    position: fixed;
    inset: 0;
    z-index: 1400;
    display: grid;
    place-items: center;
    padding: 16px;
}

.dialog-layer .scrim {
    z-index: 0;
}

.dialog {
    position: relative;
    width: min(440px, 100%);
    max-height: calc(100vh - 32px);
    overflow-y: auto;
    background: var(--surface);
    border-radius: var(--r-lg);
    box-shadow: var(--shadow-3);
    padding: 24px;
    opacity: 0;
    transform: translateY(14px) scale(.96);
    transition: opacity var(--dur-2) var(--ease-out), transform .42s var(--ease-spring);
}

.dialog-layer.is-open .dialog {
    opacity: 1;
    transform: none;
}

.dialog-icon {
    width: 46px;
    height: 46px;
    border-radius: 14px;
    display: grid;
    place-items: center;
    margin-bottom: 14px;
    background: var(--brand-soft);
    color: var(--brand);
}

.dialog-icon .icon {
    width: 24px;
    height: 24px;
}

.dialog-icon.is-danger {
    background: var(--red-soft);
    color: var(--red);
}

.dialog h3 {
    font-size: var(--t-lg);
    margin-bottom: 6px;
}

.dialog p {
    color: var(--ink-2);
    font-size: var(--t-sm);
}

.dialog-actions {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
    margin-top: 22px;
}

.dialog-actions .btn {
    flex: 1;
}

.choice-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin: 16px -4px 0;
    padding: 2px 4px;
    max-height: min(52vh, 440px);
    overflow-y: auto;
    overscroll-behavior: contain;
}

.choice {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    border-radius: 12px;
    border: 1.5px solid var(--line);
    text-align: left;
    transition: border-color var(--dur-1), background var(--dur-1);
}

.choice:hover {
    border-color: var(--line-strong);
    background: var(--surface-2);
}

.choice[aria-checked="true"] {
    border-color: var(--brand);
    background: var(--brand-soft);
}

.choice-icon {
    width: 38px;
    height: 38px;
    border-radius: 10px;
    background: var(--surface-3);
    display: grid;
    place-items: center;
    color: var(--ink-2);
    flex-shrink: 0;
}

.choice[aria-checked="true"] .choice-icon {
    background: var(--brand);
    color: var(--brand-ink);
}

.choice b {
    display: block;
    font-family: var(--font-display);
    font-weight: 600;
    font-size: 14.5px;
}

.choice span {
    font-size: 12.5px;
    color: var(--ink-3);
}

.choice-radio {
    margin-left: auto;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    border: 2px solid var(--line-strong);
    flex-shrink: 0;
    transition: border var(--dur-2) var(--ease-spring);
}

.choice[aria-checked="true"] .choice-radio {
    border: 6px solid var(--brand);
}

/* ---------- Toasts ---------- */
.toast-stack {
    position: fixed;
    right: 20px;
    bottom: 20px;
    z-index: 1600;
    display: flex;
    flex-direction: column;
    gap: 10px;
    align-items: flex-end;
    pointer-events: none;
}

.toast {
    pointer-events: auto;
    position: relative;
    display: flex;
    align-items: flex-start;
    gap: 12px;
    width: min(380px, calc(100vw - 32px));
    padding: 14px 12px 14px 14px;
    border-radius: var(--r-md);
    background: var(--ink);
    color: var(--paper);
    box-shadow: var(--shadow-3);
    overflow: hidden;
    opacity: 0;
    transform: translateY(16px) scale(.96);
    transition: opacity var(--dur-2) var(--ease-out), transform .45s var(--ease-spring);
}

:root[data-theme="dark"] .toast {
    background: var(--surface-3);
    color: var(--ink);
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) .toast {
        background: var(--surface-3);
        color: var(--ink);
    }
}

.toast.is-in {
    opacity: 1;
    transform: none;
}

.toast.is-out {
    opacity: 0;
    transform: translateX(30px);
    transition-duration: .25s;
}

.toast-icon {
    width: 28px;
    height: 28px;
    border-radius: 8px;
    display: grid;
    place-items: center;
    flex-shrink: 0;
    background: rgba(255, 255, 255, .12);
}

.toast-success .toast-icon {
    background: var(--green);
    color: #fff;
}

.toast-error .toast-icon {
    background: var(--red);
    color: #fff;
}

.toast-alert .toast-icon {
    background: var(--red);
    color: #fff;
    animation: beacon 1.6s infinite;
}

.toast-body {
    flex: 1;
    min-width: 0;
    font-size: var(--t-sm);
    line-height: 1.5;
    padding-top: 3px;
}

.toast-body strong {
    display: block;
    font-family: var(--font-display);
    font-weight: 600;
    font-size: 14.5px;
}

.toast-close {
    width: 28px;
    height: 28px;
    border-radius: 8px;
    display: grid;
    place-items: center;
    opacity: .6;
    flex-shrink: 0;
}

.toast-close:hover {
    opacity: 1;
    background: rgba(255, 255, 255, .1);
}

.toast-timer {
    position: absolute;
    left: 0;
    bottom: 0;
    height: 3px;
    width: 100%;
    background: currentColor;
    opacity: .25;
    transform-origin: left;
    animation: toastTimer linear forwards;
}

@keyframes toastTimer {
    to {
        transform: scaleX(0);
    }
}

@media (max-width: 720px) {
    .toast-stack {
        right: 16px;
        left: 16px;
        bottom: 16px;
        align-items: stretch;
    }

    .toast {
        width: 100%;
    }
}

/* ---------- Empty, skeleton ---------- */
.empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    padding: 48px 24px;
    color: var(--ink-2);
}

.empty-icon {
    width: 64px;
    height: 64px;
    border-radius: 20px;
    display: grid;
    place-items: center;
    background: var(--surface-3);
    color: var(--ink-3);
    margin-bottom: 14px;
}

.empty-icon .icon {
    width: 30px;
    height: 30px;
}

.empty-icon.is-ok {
    background: var(--green-soft);
    color: var(--green);
}

.empty h4 {
    font-size: var(--t-md);
    margin-bottom: 4px;
}

.empty p {
    font-size: var(--t-sm);
    color: var(--ink-3);
    max-width: 32ch;
}

.empty .btn {
    margin-top: 16px;
}

.skeleton {
    border-radius: 8px;
    background: linear-gradient(90deg, var(--surface-3) 0%, var(--surface-2) 50%, var(--surface-3) 100%);
    background-size: 200% 100%;
    animation: shimmer 1.3s linear infinite;
}

@keyframes shimmer {
    from {
        background-position: 200% 0;
    }

    to {
        background-position: -200% 0;
    }
}

/* ---------- Shared motion ---------- */
@keyframes pop {
    0% {
        transform: scale(.6);
        opacity: 0;
    }

    100% {
        transform: scale(1);
        opacity: 1;
    }
}

@keyframes rise {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
}

@keyframes shake {

    10%,
    90% {
        transform: translateX(-1px);
    }

    20%,
    80% {
        transform: translateX(3px);
    }

    30%,
    50%,
    70% {
        transform: translateX(-6px);
    }

    40%,
    60% {
        transform: translateX(6px);
    }
}

@keyframes flash {
    0% {
        background: var(--red-soft);
    }

    100% {
        background: transparent;
    }
}

.stagger>* {
    animation: rise .5s var(--ease-out) both;
    animation-delay: calc(min(var(--i, 0), 12) * 35ms);
}

/* ---------- Login (shared by dashboard + incident center) ---------- */
.login {
    position: fixed;
    inset: 0;
    z-index: 2000;
    display: grid;
    grid-template-columns: minmax(0, 1.15fr) minmax(380px, 1fr);
    background: var(--surface);
    transition: opacity .6s var(--ease-out), transform .6s var(--ease-out);
}

.login.is-leaving {
    opacity: 0;
    transform: scale(1.02);
    pointer-events: none;
}

.login-art {
    position: relative;
    background: var(--login-panel);
    color: var(--login-panel-ink);
    display: flex;
    flex-direction: column;
    padding: 40px 48px;
    overflow: hidden;
}

.login-art::before {
    content: '';
    position: absolute;
    inset: 0;
    background:
        radial-gradient(circle at 70% 45%, rgba(217, 139, 200, .16), transparent 55%),
        radial-gradient(circle at 15% 90%, rgba(232, 116, 28, .08), transparent 40%);
    pointer-events: none;
}

.login-art .brand {
    position: relative;
}

.login-art .brand img {
    background: #fff;
    padding: 3px;
}

.login-art .brand-text span {
    color: rgba(243, 233, 241, .55);
}

.login-art svg.district {
    position: relative;
    flex: 1;
    width: 100%;
    min-height: 0;
    margin: 12px 0;
}

.login-art-caption {
    position: relative;
    max-width: 46ch;
}

.login-art-caption h2 {
    color: var(--login-panel-ink);
    font-size: var(--t-2xl);
    font-weight: 600;
    letter-spacing: -.01em;
    line-height: 1.25;
}

.login-art-caption p {
    margin-top: 8px;
    color: rgba(243, 233, 241, .62);
    font-size: var(--t-sm);
}

.login-legend {
    position: relative;
    display: flex;
    flex-wrap: wrap;
    gap: 14px;
    margin-top: 18px;
    font-size: 12.5px;
    color: rgba(243, 233, 241, .7);
}

.login-legend span {
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.login-legend i {
    width: 10px;
    height: 10px;
    border-radius: 3px;
}

.login-form-wrap {
    display: grid;
    place-items: center;
    padding: 40px 32px;
    overflow-y: auto;
}

.login-form {
    width: min(380px, 100%);
}

.login-form h1 {
    font-size: var(--t-2xl);
    letter-spacing: -.015em;
}

.login-form .lead {
    color: var(--ink-3);
    margin: 6px 0 28px;
    font-size: var(--t-sm);
}

.login-form .btn-primary {
    margin-top: 24px;
}

.login-error {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 16px;
    padding: 10px 12px;
    border-radius: var(--r-sm);
    background: var(--red-soft);
    color: var(--red-ink);
    font-size: var(--t-sm);
}

.login-error.is-shaking {
    animation: shake .5s;
}

.login-foot {
    margin-top: 32px;
    padding-top: 18px;
    border-top: 1px solid var(--line);
    display: flex;
    flex-wrap: wrap;
    gap: 6px 18px;
    font-size: 13px;
}

.login-foot a {
    color: var(--ink-2);
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

.login-foot a:hover {
    color: var(--brand);
}

.login-foot a.is-urgent {
    color: var(--red);
    font-weight: 600;
}

/* District drawing */
.district-shape {
    fill: transparent;
    stroke: rgba(243, 233, 241, .75);
    stroke-width: 1.4;
    stroke-linejoin: round;
    stroke-dasharray: 1;
    stroke-dashoffset: 1;
    animation:
        drawLine 1.6s var(--ease-out) forwards,
        fillIn .9s var(--ease-out) forwards;
    animation-delay: calc(var(--i) * 110ms), calc(1.1s + var(--i) * 90ms);
}

.district-label {
    fill: rgba(243, 233, 241, .9);
    font-family: var(--font-display);
    font-weight: 500;
    text-anchor: middle;
    paint-order: stroke;
    stroke: rgba(22, 15, 30, .55);
    stroke-width: 3px;
    stroke-linejoin: round;
    opacity: 0;
    animation: labelIn .6s var(--ease-out) forwards;
    animation-delay: calc(1.6s + var(--i) * 70ms);
    pointer-events: none;
}

@keyframes labelIn {
    from {
        opacity: 0;
        transform: translateY(4px);
    }

    to {
        opacity: 1;
        transform: none;
    }
}

.district-label .count {
    font-weight: 700;
    fill: #fff;
}

@keyframes drawLine {
    to {
        stroke-dashoffset: 0;
    }
}

@keyframes fillIn {
    to {
        fill: var(--fill);
    }
}

@media (max-width: 880px) {
    .login {
        grid-template-columns: 1fr;
        grid-template-rows: auto 1fr;
        overflow-y: auto;
    }

    .login-art {
        padding: 20px 20px 18px;
        min-height: 0;
    }

    .login-art svg.district {
        height: 190px;
        flex: none;
        margin: 8px 0 4px;
    }

    .login-art-caption h2 {
        font-size: var(--t-lg);
    }

    .login-art-caption p,
    .login-legend {
        display: none;
    }

    .login-form-wrap {
        place-items: start center;
        padding: 28px 20px 40px;
    }
}

/* ---------- Leaflet skin ---------- */
.leaflet-container {
    font-family: var(--font-body);
    background: var(--surface-3);
}

.leaflet-bar {
    border: 0 !important;
    box-shadow: var(--shadow-2) !important;
    border-radius: 12px !important;
    overflow: hidden;
}

.leaflet-bar a {
    width: 38px !important;
    height: 38px !important;
    line-height: 38px !important;
    background: var(--surface) !important;
    color: var(--ink) !important;
    border-bottom: 1px solid var(--line) !important;
    font-size: 18px !important;
}

.leaflet-bar a:hover {
    background: var(--surface-2) !important;
}

.leaflet-popup-content-wrapper {
    background: var(--surface);
    color: var(--ink);
    border-radius: var(--r-md);
    box-shadow: var(--shadow-3);
}

.leaflet-popup-tip {
    background: var(--surface);
}

.leaflet-popup-content {
    margin: 14px 16px;
    font-size: var(--t-sm);
    line-height: 1.5;
}

.leaflet-container a.leaflet-popup-close-button {
    color: var(--ink-3);
}

.leaflet-control-attribution {
    background: color-mix(in srgb, var(--surface) 80%, transparent) !important;
    color: var(--ink-3) !important;
    font-size: 10px !important;
}

.leaflet-control-attribution a {
    color: var(--ink-2) !important;
}

.is-dark-tiles .leaflet-tile-pane {
    filter: invert(1) hue-rotate(180deg) brightness(.92) contrast(.88) saturate(.55);
}

.leaflet-tooltip.tambon-label {
    background: transparent;
    border: 0;
    box-shadow: none;
    padding: 0;
    color: var(--ink);
    text-align: center;
    font-family: var(--font-display);
    pointer-events: auto;
    cursor: pointer;
}

.leaflet-tooltip.tambon-label::before {
    display: none;
}

.tl {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    transition: transform var(--dur-2) var(--ease-spring);
}

.tl:hover {
    transform: scale(1.08);
}

.tl-name {
    font-size: 12.5px;
    font-weight: 600;
    line-height: 1.1;
    color: #1A2233;
    text-shadow: 0 0 3px #fff, 0 0 3px #fff, 0 0 6px #fff;
    white-space: nowrap;
}

.tl-count {
    min-width: 26px;
    height: 26px;
    padding: 0 7px;
    border-radius: 999px;
    background: #fff;
    color: #1A2233;
    font-size: 13px;
    font-weight: 700;
    display: grid;
    place-items: center;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .25), inset 0 0 0 2px var(--c, #CAD1DC);
}

.tl-count.is-alert {
    background: var(--red);
    color: #fff;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .3), 0 0 0 2.5px #fff;
    animation: mapPulse 1.8s var(--ease-out) infinite;
}

.tl-count.is-warn {
    background: var(--orange);
    color: #fff;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .3), 0 0 0 2.5px #fff;
}

.is-dark-map .tl-name {
    color: #F2F4F8;
    text-shadow: 0 0 3px #000, 0 0 4px #000, 0 0 8px rgba(0, 0, 0, .8);
}

/* --- โหมด 3D (MapLibre canvas อยู่ใต้ Leaflet overlay) ---
   ห้ามซ่อน .leaflet-tile-pane: canvas ของ MapLibre อยู่ใน pane นี้ (แผนที่แบนถูกถอดออกใน enter3d แล้ว) */
.is-3d {
    background: #dfe6ee;
}

/* --- Route from the hospital (SMIV.routeFromHospital) --- */
.hosp-icon {
    background: none;
    border: 0;
}

.hosp-pin {
    width: 36px;
    height: 36px;
    border-radius: 50% 50% 50% 4px;
    transform: rotate(-45deg);
    background: #ffffff;
    border: 3px solid var(--red);
    box-shadow: 0 6px 14px rgb(0 0 0 / .3);
    display: grid;
    place-items: center;
}

.hosp-pin span {
    transform: rotate(45deg);
    font: 800 16px/1 var(--font-display);
    color: var(--red);
}

.route-line {
    stroke-dasharray: 14 10;
    animation: route-march 1.1s linear infinite;
}

.route-line.is-straight {
    stroke-dasharray: 8 9;
}

@keyframes route-march {
    to {
        stroke-dashoffset: -24;
    }
}

.route-runner {
    background: none;
    border: 0;
}

.route-runner span {
    display: block;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: #ffffff;
    border: 4px solid var(--route-c, var(--red));
    box-shadow: 0 0 0 6px rgb(217 52 43 / .25), 0 0 14px rgb(217 52 43 / .65);
    box-shadow: 0 0 0 6px color-mix(in srgb, var(--route-c, var(--red)) 25%, transparent), 0 0 14px color-mix(in srgb, var(--route-c, var(--red)) 65%, transparent);
}

.route-card {
    position: absolute;
    left: 12px;
    bottom: 12px;
    z-index: 800;
    width: min(320px, calc(100% - 80px));
    padding: 12px;
    border-radius: var(--r-md);
    background: var(--surface);
    color: var(--ink);
    border: 1px solid var(--line);
    box-shadow: var(--shadow-3);
    display: flex;
    flex-direction: column;
    gap: 10px;
    animation: rise .35s var(--ease-out) both;
}

.route-top {
    display: flex;
    gap: 10px;
    align-items: flex-start;
}

.route-ico {
    flex: 0 0 34px;
    height: 34px;
    border-radius: 10px;
    display: grid;
    place-items: center;
    background: var(--red-soft);
    color: var(--red);
}

.route-txt {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.route-txt b {
    font: 600 14px/1.35 var(--font-display);
}

.route-meta {
    font-size: 13px;
    color: var(--ink-2);
}

.route-card:not(.is-ready) .route-meta::after {
    content: '';
    display: inline-block;
    width: 10px;
    height: 10px;
    margin-left: 6px;
    border-radius: 50%;
    border: 2px solid currentColor;
    border-right-color: transparent;
    animation: spin .8s linear infinite;
    vertical-align: -1px;
}

.route-card.is-small {
    left: 8px;
    bottom: 8px;
    padding: 8px 10px;
    gap: 6px;
    width: calc(100% - 64px);
}

.route-card.is-small .route-ico {
    display: none;
}

.route-card.is-small .route-txt b {
    font-size: 13px;
}

.route-card.is-small .route-go {
    min-height: 30px;
    padding: 3px 10px;
    font-size: 12.5px;
}

.route-card .route-go,
.route-card .route-go:hover {
    justify-content: center;
    color: var(--brand-ink); /* Leaflet's ".leaflet-container a" colour would otherwise win */
    text-decoration: none;
}

@media (prefers-reduced-motion: reduce) {
    .route-line {
        animation: none;
    }
}

.map-3d-hint {
    display: none;
    position: absolute;
    left: 50%;
    bottom: 14px;
    transform: translateX(-50%);
    z-index: 650;
    max-width: calc(100% - 140px);
    padding: 6px 12px;
    border-radius: 999px;
    background: rgb(15 18 24 / .78);
    color: #fff;
    font: 500 12.5px/1.4 var(--font-body);
    text-align: center;
    pointer-events: none;
}

.is-3d .map-3d-hint {
    display: block;
}

:root[data-theme="dark"] .is-3d {
    background: #10151d;
}

.is-3d.is-loading-3d::after {
    content: 'กำลังโหลดโหมด 3D…';
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font: 600 13px/1 var(--font-body, sans-serif);
    color: var(--ink-2, #556);
    background: linear-gradient(0deg, rgba(255, 255, 255, .5), rgba(255, 255, 255, .5));
    z-index: 500;
    pointer-events: none;
}

:root[data-theme="dark"] .is-3d.is-loading-3d::after {
    color: #aab;
    background: linear-gradient(0deg, rgba(16, 21, 29, .6), rgba(16, 21, 29, .6));
}

@keyframes mapPulse {
    0% {
        box-shadow: 0 0 0 2.5px #fff, 0 0 0 2.5px rgba(217, 52, 43, .7);
    }

    75%,
    100% {
        box-shadow: 0 0 0 2.5px #fff, 0 0 0 18px rgba(217, 52, 43, 0);
    }
}

.map-pin {
    background: none !important;
    border: 0 !important;
}

.map-pin i {
    display: block;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--c);
    border: 3px solid #fff;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .35);
    transition: transform var(--dur-2) var(--ease-spring);
}

.map-pin.is-new i {
    animation: mapPulse 1.6s var(--ease-out) infinite;
}

.map-pin.is-active i {
    transform: scale(1.5);
}

.map-tools {
    position: absolute;
    z-index: 500;
    display: flex;
    gap: 8px;
}

.map-tools .seg {
    background: var(--surface);
    box-shadow: var(--shadow-2);
}

.map-tools .seg .tab-indicator {
    background: var(--surface-3);
    box-shadow: none;
}

.map-tools .btn-secondary {
    border: 0;
    box-shadow: var(--shadow-2);
}

/* ---------- Reduced motion ---------- */
@media (prefers-reduced-motion: reduce) {

    *,
    *::before,
    *::after {
        animation-duration: .001ms !important;
        animation-iteration-count: 1 !important;
        animation-delay: 0s !important;
        transition-duration: .001ms !important;
    }
}
/* ==========================================================================
   Mascot — ปอนด์ ผู้ช่วย SMI-V (ตัวการ์ตูนขยับได้ตามบริบท)
   ========================================================================== */
.mascot {
    position: fixed;
    left: calc(var(--mc-x, 86vw) - 46px);
    top: calc(var(--mc-y, 78vh) - 46px);
    width: 92px;
    height: 132px;
    z-index: 950;
    pointer-events: none;
    transition: left .7s var(--ease-out), top .7s var(--ease-out);
}

.mascot.is-hidden {
    display: none;
}

/* Phones / narrow windows: no free corner, so peek in small at the top right only while talking */
/* Standing beside a button (desktop): the bubble opens away from that button */
.mascot.is-beside[data-side='left'] .mascot-bubble {
    left: 0;
    right: auto;
    transform: none;
    animation: none;
}

.mascot.is-beside[data-side='left'] .mascot-bubble-tail {
    left: 26px;
    right: auto;
}

.mascot.is-beside[data-side='right'] .mascot-bubble {
    left: auto;
    right: 0;
    transform: none;
    animation: none;
}

.mascot.is-compact {
    left: auto;
    top: calc(env(safe-area-inset-top, 0px) + 64px);
    right: max(8px, env(safe-area-inset-right, 0px));
    width: 56px;
    height: 80px;
    opacity: 0;
    visibility: hidden;
    transform: translateY(-6px) scale(.92);
    transition: opacity .25s var(--ease-out), transform .25s var(--ease-out), visibility 0s linear .25s;
}

.mascot.is-compact.is-talking {
    opacity: 1;
    visibility: visible;
    transform: none;
    transition: opacity .25s var(--ease-out), transform .25s var(--ease-out), visibility 0s;
}

.mascot.is-compact .mascot-close {
    display: none;
}

.mascot.is-compact[data-side] .mascot-bubble {
    bottom: auto;
    top: 50%;
    right: calc(100% + 10px);
    left: auto;
    transform: translateY(-50%);
    max-width: min(240px, calc(100vw - 96px));
    animation: none;
}

.mascot.is-compact[data-side] .mascot-bubble-tail {
    left: auto;
    right: -7px;
    top: 50%;
    bottom: auto;
    transform: translateY(-50%) rotate(-45deg);
}

.mascot-inner {
    position: relative;
    width: 100%;
    height: 100%;
}

.mascot-body {
    position: absolute;
    inset: 0;
    pointer-events: auto;
    cursor: pointer;
    filter: drop-shadow(0 6px 10px rgb(0 0 0 / .18));
}

.mascot-svg {
    width: 100%;
    height: 100%;
    overflow: visible;
}

.mascot-close {
    position: absolute;
    top: -4px;
    right: -2px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: 1px solid var(--line);
    background: var(--surface);
    color: var(--ink-3);
    font-size: 11px;
    line-height: 1;
    cursor: pointer;
    opacity: 0;
    transition: opacity .2s;
    pointer-events: auto;
    box-shadow: 0 2px 6px rgb(0 0 0 / .15);
}

.mascot:hover .mascot-close {
    opacity: 1;
}

.mascot-bubble {
    position: absolute;
    bottom: calc(100% + 10px);
    left: 50%;
    transform: translateX(-50%);
    background: var(--surface);
    color: var(--ink);
    border: 1.5px solid var(--brand);
    border-radius: 14px;
    padding: 9px 13px;
    font: 600 12.5px/1.45 var(--font-body);
    max-width: 220px;
    width: max-content;
    text-align: center;
    box-shadow: 0 8px 20px rgb(0 0 0 / .16);
    animation: bubbleIn .25s var(--ease-out);
    white-space: normal;
    pointer-events: none;
    z-index: 2;
}

.mascot-bubble-tail {
    position: absolute;
    left: 50%;
    bottom: -7px;
    width: 12px;
    height: 12px;
    background: var(--surface);
    border-right: 1.5px solid var(--brand);
    border-bottom: 1.5px solid var(--brand);
    transform: translateX(-50%) rotate(45deg);
}

@keyframes bubbleIn {
    from {
        opacity: 0;
        transform: translateX(-50%) translateY(6px) scale(.9);
    }
}

/* ---- ชิ้นส่วนใน SVG ---- */
.mc-shadow {
    fill: rgb(0 0 0 / .18);
}

.mc-body {
    fill: var(--brand);
    stroke: color-mix(in srgb, var(--brand) 60%, #000);
    stroke-width: 2;
}

.mc-ear {
    fill: var(--brand);
    stroke: color-mix(in srgb, var(--brand) 60%, #000);
    stroke-width: 2;
}

.mc-ear-in {
    fill: color-mix(in srgb, var(--brand) 55%, #fff);
}

.mc-cheek {
    fill: color-mix(in srgb, var(--brand) 30%, transparent);
}

.mc-eye {
    fill: #2A1A33;
}

.mc-spark {
    fill: #fff;
}

.mc-line {
    fill: none;
    stroke: #2A1A33;
    stroke-width: 2.4;
    stroke-linecap: round;
}

.mc-mouth {
    fill: none;
    stroke: #2A1A33;
    stroke-width: 2.6;
    stroke-linecap: round;
}

.mc-arm {
    fill: var(--brand);
    stroke: color-mix(in srgb, var(--brand) 60%, #000);
    stroke-width: 2;
    transform-box: fill-box;
    transform-origin: center;
}

.mc-leg {
    fill: none;
    stroke: color-mix(in srgb, var(--brand) 70%, #000);
    stroke-width: 3.5;
    stroke-linecap: round;
    transform-box: fill-box;
    transform-origin: 50% 0;
}

.mc-badge-bg {
    fill: #fff;
    stroke: #D9342B;
    stroke-width: 1.6;
}

.mc-badge-siren,
.mc-badge .mc-badge-siren {
    fill: none;
    stroke: #D9342B;
    stroke-width: 4;
    stroke-linecap: round;
    stroke-linejoin: round;
}

/* ---- สถานะ (state machine) ---- */
.mascot.state-idle .mc-bounce {
    animation: mcIdle 2.6s var(--ease-out) infinite;
}

.mascot.state-idle .mc-eyes {
    animation: mcBlink 4.8s ease-in-out infinite;
}

.mascot.state-walk .mc-bounce {
    animation: mcBounce .42s ease-in-out infinite;
}

.mascot.state-walk .leg-l {
    animation: mcLeg .42s ease-in-out infinite;
}

.mascot.state-walk .leg-r {
    animation: mcLeg .42s ease-in-out infinite .21s;
}

.mascot.state-walk .arm-l {
    animation: mcArm .42s ease-in-out infinite;
}

.mascot.state-walk .arm-r {
    animation: mcArm .42s ease-in-out infinite .21s;
}

.mascot.state-walk .mc-shadow {
    animation: mcShadow .42s ease-in-out infinite;
}

.mascot.state-alert .mc-bounce {
    animation: mcAlarm .38s ease-in-out 6;
}

.mascot.state-alert .mc-eye {
    animation: mcBlinkEyes .38s ease-in-out 6;
}

.mascot.state-happy .mc-bounce {
    animation: mcJump .5s var(--ease-out) 2;
}

.mascot.state-happy .mc-mouth {
    d: path('M46 68 q14 16 28 0');
}

.mascot.state-point .mc-bounce {
    animation: mcLean 2.2s var(--ease-out) infinite;
}

.mascot.state-point .arm-r {
    transform: rotate(-55deg) translate(3px, -10px);
}

.mascot.state-busy .mc-bounce {
    animation: mcIdle 1.4s var(--ease-out) infinite;
}

.mascot.state-busy .mc-eyes {
    animation: mcThink 1.4s ease-in-out infinite;
}

/* ข้อความพูดวนจุดไปด์ */
.mascot.is-talking .mascot-bubble-text::after {
    content: '…';
    animation: mcDots 1.2s steps(4, end) infinite;
}

@keyframes mcIdle {

    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(-2.5px);
    }
}

@keyframes mcBounce {

    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(-6px);
    }
}

@keyframes mcJump {

    0%,
    100% {
        transform: translateY(0);
    }

    40% {
        transform: translateY(-14px);
    }
}

@keyframes mcAlarm {

    0%,
    100% {
        transform: translateX(0);
    }

    25% {
        transform: translateX(-3.5px);
    }

    75% {
        transform: translateX(3.5px);
    }
}

@keyframes mcBlinkEyes {

    0%,
    100% {
        transform: scaleY(1);
    }

    50% {
        transform: scaleY(.1);
    }
}

@keyframes mcBlink {

    0%,
    92%,
    100% {
        transform: scaleY(1);
    }

    96% {
        transform: scaleY(.08);
    }
}

@keyframes mcThink {

    0%,
    100% {
        transform: translateX(-1.2px);
    }

    50% {
        transform: translateX(1.2px);
    }
}

@keyframes mcLeg {

    0%,
    100% {
        transform: rotate(24deg);
    }

    50% {
        transform: rotate(-24deg);
    }
}

@keyframes mcArm {

    0%,
    100% {
        transform: rotate(0deg);
    }

    50% {
        transform: rotate(30deg);
    }
}

@keyframes mcShadow {

    0%,
    100% {
        transform: scaleX(1);
        opacity: .18;
    }

    50% {
        transform: scaleX(.85);
        opacity: .1;
    }
}

@keyframes mcLean {

    0%,
    100% {
        transform: rotate(0deg);
    }

    50% {
        transform: rotate(5deg);
    }
}

@keyframes mcDots {
    0% {
        content: '';
    }

    25% {
        content: '•';
    }

    50% {
        content: '••';
    }

    75% {
        content: '•••';
    }
}

/* หันขวา: กลับหัวไปทางขวา + ย้ายบับเบิล */
.mascot[data-side='right'] .mascot-svg {
    transform: scaleX(-1);
}

.mascot[data-side='right'] .mascot-bubble {
    left: auto;
    right: -10px;
    transform: none;
}

.mascot[data-side='right'] .mascot-bubble-tail {
    left: auto;
    right: 18px;
    transform: rotate(45deg);
}

/* ผู้ใช้ขอลดโมชัน */
@media (prefers-reduced-motion: reduce) {
    .mascot {
        transition: none;
    }

    .mascot .mc-bounce,
    .mascot .leg-l,
    .mascot .leg-r,
    .mascot .arm-l,
    .mascot .arm-r,
    .mascot .mc-eyes,
    .mascot .mc-shadow {
        animation: none !important;
    }
}

/* ---------- Incident-type characters ----------
   Sheets in assets/images/incident-characters: 3 poses side by side, played 1 > 2 > 3 > 2.
   Size: the parent sets --fit-h (max height) and --fit-w (max width); the sheet keeps its ratio. */
.inc-char {
    --ch: min(var(--fit-h, 64px), calc(var(--fit-w, 1000px) / var(--ratio)));
    display: inline-block;
    flex-shrink: 0;
    width: calc(var(--ch) * var(--ratio));
    height: var(--ch);
    background: var(--sheet) no-repeat 0 0 / 300% 100%;
    transform-origin: 50% 100%;
    animation: incCharPose var(--speed) steps(1, end) infinite, var(--life) var(--life-speed) ease-in-out infinite alternate;
    animation-delay: var(--delay, 0s);
    pointer-events: none;
}

.inc-char[data-type='aggressive'] {
    --sheet: url(../images/incident-characters/aggressive-v1.png);
    --ratio: .723;
    --speed: 1.5s;
    --life: incCharBob;
    --life-speed: .375s;
}

.inc-char[data-type='rampage'] {
    --sheet: url(../images/incident-characters/rampage-v1.png);
    --ratio: 1.208;
    --speed: 1.1s;
    --life: incCharShake;
    --life-speed: .14s;
}

.inc-char[data-type='delusion'] {
    --sheet: url(../images/incident-characters/delusion-v1.png);
    --ratio: .431;
    --speed: 2.8s;
    --life: incCharSway;
    --life-speed: 1.4s;
}

.inc-char[data-type='paranoid'] {
    --sheet: url(../images/incident-characters/paranoid-v1.png);
    --ratio: .65;
    --speed: 2.2s;
    --life: incCharGlance;
    --life-speed: .55s;
}

.inc-char[data-type='hallucination'] {
    --sheet: url(../images/incident-characters/hallucination-v1.png);
    --ratio: .927;
    --speed: 2.4s;
    --life: incCharSway;
    --life-speed: 1.2s;
}

.inc-char[data-type='suicide'] {
    --sheet: url(../images/incident-characters/suicide-v1.png);
    --ratio: .569;
    --speed: 3.2s;
    --life: incCharBreath;
    --life-speed: 1.6s;
}

/* hold the middle pose (the one each sheet is named after) */
.inc-char.is-still,
.characters-paused .inc-char {
    animation: none;
    background-position: 50% 0;
}

@keyframes incCharPose {
    0% {
        background-position: 0 0;
    }

    35% {
        background-position: 50% 0;
    }

    50% {
        background-position: 100% 0;
    }

    85% {
        background-position: 50% 0;
    }

    100% {
        background-position: 0 0;
    }
}

@keyframes incCharBob {
    to {
        transform: translateY(-4%);
    }
}

@keyframes incCharShake {
    from {
        transform: rotate(-2.5deg);
    }

    to {
        transform: rotate(2.5deg);
    }
}

@keyframes incCharSway {
    from {
        transform: rotate(-2deg);
    }

    to {
        transform: rotate(2deg);
    }
}

@keyframes incCharGlance {
    0%,
    60% {
        transform: translateX(0);
    }

    80% {
        transform: translateX(-3%);
    }

    100% {
        transform: translateX(3%);
    }
}

@keyframes incCharBreath {
    to {
        transform: scale(1.02, .975);
    }
}

/* Incident pin with its character standing on the dot */
.map-pin.has-char {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    --fit-h: 58px;
    --fit-w: 64px;
}

.map-pin.has-char .inc-char {
    margin-bottom: -7px;
    filter: drop-shadow(0 2px 3px rgba(0, 0, 0, .35));
    transition: transform var(--dur-2) var(--ease-spring);
}

.map-pin.has-char i {
    flex-shrink: 0;
    width: 14px;
    height: 14px;
    border-width: 2.5px;
}

.map-pin.has-char.is-active .inc-char {
    --fit-h: 76px;
    --fit-w: 84px;
}

.map-pin.has-char.is-active i {
    transform: none;
}

@media (prefers-reduced-motion: reduce) {
    .inc-char {
        animation: none;
        background-position: 50% 0;
    }
}
