:root {
    color-scheme: light dark;

    /* Color Palette - Modern Scandinavian Warm Minimal */
    --bg: #f8fafc;
    --bg-subtle: #f1f5f9;
    --bg-glow: #ffffff;
    --card-bg: #ffffff;
    --card-border: #e2e8f0;
    --fg: #0f172a;
    --muted: #5f6f84;
    --muted-light: #94a3b8;
    --input-border: #848fa1;

    /* Interactive Elements */
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --primary-active: #1e40af;
    --primary-fg: #ffffff;
    --primary-subtle: #eff6ff;
    --primary-glow: 0 4px 12px rgba(37, 99, 235, 0.25);
    --primary-glow-active: 0 2px 6px rgba(37, 99, 235, 0.2);
    --focus-ring: 0 0 0 3px rgba(37, 99, 235, 0.15);

    --input-focus-bg: #ffffff;
    --input-shadow-inset: inset 0 2px 4px rgba(15, 23, 42, 0.03);
    --editor-advance-bg: #fdfefe;

    --toggle-off-bg: #7c8799;
    --toggle-on-bg: #dc2626;
    --toggle-knob: #ffffff;
    --toggle-knob-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
    --toggle-focus-ring: 0 0 0 3px rgba(225, 29, 72, 0.3);

    --key-bg: #ffffff;
    --key-border: #cbd5e1;
    --key-active: #f1f5f9;
    --key-shadow: 0 2px 0 #94a3b8, 0 1px 3px rgba(15, 23, 42, 0.08);
    --key-shadow-active: 0 0 0 transparent, 0 1px 1px rgba(15, 23, 42, 0.08);

    /* Alerts & States */
    --calm-bg: #ecfdf5;
    --calm-border: #a7f3d0;
    --calm-fg: #065f46;

    --important-bg: #b91c1c;
    --important-border: #991b1b;
    --important-fg: #ffffff;
    --important-shadow: 0 10px 25px -4px rgba(185, 28, 28, 0.35);
    --important-flag-bg: #ffffff;
    --important-flag-fg: #b91c1c;
    --important-flag-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
    --important-quote-border: #fecaca;
    --important-quote-bg: rgba(0, 0, 0, 0.2);

    /* Soft red for the Important toggle and the clear button's hover */
    --important-soft-bg: #fef2f2;
    --important-soft-border: #fca5a5;
    --important-soft-fg: #991b1b;

    --error-bg: #fef2f2;
    --error-border: #fecaca;
    --error-fg: #991b1b;

    /* Elevation & Radius */
    --radius-sm: 0.5rem;
    --radius-md: 0.75rem;
    --radius-lg: 1rem;
    --radius-xl: 1.25rem;
    --radius-full: 9999px;

    --shadow-sm: 0 1px 2px 0 rgba(15, 23, 42, 0.05);
    --shadow-md: 0 4px 6px -1px rgba(15, 23, 42, 0.07), 0 2px 4px -2px rgba(15, 23, 42, 0.05);
    --shadow-lg: 0 10px 25px -3px rgba(15, 23, 42, 0.08), 0 4px 6px -4px rgba(15, 23, 42, 0.04);
    --shadow-overlay: 0 4px 12px rgba(0, 0, 0, 0.12);

    /* Health Check Page */
    --health-bg: #0f172a;
    --health-fg: #f8fafc;
    --health-item-bg: rgba(255, 255, 255, 0.05);
    --health-accent: #4ade80;
}

/* Dark theme: the same slate and blue families, inverted. Only the tokens
   change, so every rule follows the device's setting. */
@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f172a;
        --bg-subtle: #273449;
        --bg-glow: #1e293b;
        --card-bg: #1e293b;
        --card-border: #334155;
        --fg: #e2e8f0;
        --muted: #94a3b8;
        --muted-light: #64748b;
        --input-border: #64748b;

        --primary: #60a5fa;
        --primary-hover: #93c5fd;
        --primary-active: #3b82f6;
        --primary-fg: #0f172a;
        --primary-subtle: #172554;
        --primary-glow: 0 4px 12px rgba(96, 165, 250, 0.2);
        --primary-glow-active: 0 2px 6px rgba(96, 165, 250, 0.15);
        --focus-ring: 0 0 0 3px rgba(96, 165, 250, 0.35);

        --input-focus-bg: #0f172a;
        --input-shadow-inset: inset 0 2px 4px rgba(0, 0, 0, 0.25);
        --editor-advance-bg: #1b2638;

        --toggle-off-bg: #64748b;
        --toggle-on-bg: #ef4444;
        --toggle-knob: #f8fafc;
        --toggle-knob-shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
        --toggle-focus-ring: 0 0 0 3px rgba(248, 113, 113, 0.4);

        --key-bg: #1e293b;
        --key-border: #64748b;
        --key-active: #334155;
        --key-shadow: 0 2px 0 #020617, 0 1px 3px rgba(0, 0, 0, 0.3);
        --key-shadow-active: 0 0 0 transparent, 0 1px 1px rgba(0, 0, 0, 0.3);

        --calm-bg: #022c22;
        --calm-border: #065f46;
        --calm-fg: #6ee7b7;

        /* A deeper red than in light mode, so the banner stays the loudest
           thing on the screen without glaring. */
        --important-bg: #991b1b;
        --important-border: #b91c1c;
        --important-fg: #fef2f2;
        --important-shadow: 0 10px 25px -4px rgba(0, 0, 0, 0.5);
        --important-flag-bg: #fef2f2;
        --important-flag-fg: #991b1b;
        --important-flag-shadow: 0 2px 4px rgba(0, 0, 0, 0.4);
        --important-quote-border: #fca5a5;
        --important-quote-bg: rgba(0, 0, 0, 0.2);

        --important-soft-bg: #2b1418;
        --important-soft-border: #7f1d1d;
        --important-soft-fg: #fca5a5;

        --error-bg: #2b1418;
        --error-border: #7f1d1d;
        --error-fg: #fca5a5;

        --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
        --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.35), 0 2px 4px -2px rgba(0, 0, 0, 0.3);
        --shadow-lg: 0 10px 25px -3px rgba(0, 0, 0, 0.4), 0 4px 6px -4px rgba(0, 0, 0, 0.3);
        --shadow-overlay: 0 4px 12px rgba(0, 0, 0, 0.5);
    }
}

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

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

body {
    margin: 0;
    min-height: 100vh;
    min-height: 100dvh;
    background-color: var(--bg);
    background-image: radial-gradient(at 50% 0%, var(--bg-glow) 0%, var(--bg) 80%);
    color: var(--fg);
    font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 18px;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* App Shell */

.app {
    max-width: 32rem;
    margin: 0 auto;
    padding: 2rem 1.25rem 3rem;
}

.app h1 {
    font-size: 1.85rem;
    font-weight: 700;
    letter-spacing: -0.025em;
    margin: 0 0 0.5rem;
    color: var(--fg);
}

.app h2 {
    font-size: 1.35rem;
    font-weight: 700;
    letter-spacing: -0.02em;
    margin: 0 0 0.5rem;
    color: var(--fg);
}

/* Logout */

.logout-form {
    display: flex;
    justify-content: flex-end;
    margin: -0.75rem 0 0.75rem;
}

.logout-button {
    min-height: 2.5rem;
    padding: 0.4rem 1rem;
    border: 1px solid var(--card-border);
    border-radius: var(--radius-full);
    background: var(--card-bg);
    color: var(--muted);
    font: inherit;
    font-size: 0.875rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.15s, color 0.15s, border-color 0.15s;
    touch-action: manipulation;
}

.logout-button:hover,
.logout-button:focus-visible {
    border-color: var(--key-border);
    color: var(--fg);
}

.logout-button:active {
    background: var(--key-active);
}

/* Header & Date */

.note-date {
    display: inline-block;
    margin: 0 0 1.25rem;
    padding: 0.35rem 0.85rem;
    background: var(--bg-subtle);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-full);
    color: var(--muted);
    font-size: 0.875rem;
    font-weight: 600;
    letter-spacing: -0.01em;
    text-transform: capitalize;
}

/* ==========================================================================
   PIN Entry Screen
   ========================================================================== */

.entry {
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    padding: 2rem 1.25rem;
}

.entry h1 {
    font-size: 1.75rem;
    margin-top: 0;
    margin-bottom: 1.5rem;
}

.pin-form {
    width: 100%;
    max-width: 22rem;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
}

.pin-display {
    width: 100%;
    padding: 1rem;
    font-size: 2.25rem;
    letter-spacing: 0.4em;
    text-align: center;
    border: 2px solid var(--input-border);
    border-radius: var(--radius-lg);
    background: var(--card-bg);
    color: var(--fg);
    box-shadow: var(--shadow-sm), var(--input-shadow-inset);
    transition: border-color 0.15s, box-shadow 0.15s;
    outline: none;
}

.pin-display:focus {
    border-color: var(--primary);
    box-shadow: var(--focus-ring);
}

.pin-error {
    margin: 0;
    padding: 0.85rem 1rem;
    border-radius: var(--radius-md);
    background: var(--error-bg);
    border: 1px solid var(--error-border);
    color: var(--error-fg);
    font-size: 0.925rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    box-shadow: var(--shadow-sm);
}

.pin-submit {
    min-height: 3.75rem;
    margin-top: 0.5rem;
    border: none;
    border-radius: var(--radius-lg);
    background: var(--primary);
    color: var(--primary-fg);
    font: inherit;
    font-size: 1.25rem;
    font-weight: 700;
    cursor: pointer;
    box-shadow: var(--primary-glow);
    transition: background-color 0.15s, transform 0.05s, box-shadow 0.15s;
    touch-action: manipulation;
}

.pin-submit:hover {
    background: var(--primary-hover);
}

.pin-submit:active {
    transform: translateY(1px);
    background: var(--primary-active);
    box-shadow: var(--primary-glow-active);
}


/* ==========================================================================
   Daily Note Card
   ========================================================================== */

.note {
    position: relative;
    padding: 1.5rem;
    border: 1px solid var(--card-border);
    border-radius: var(--radius-xl);
    background: var(--card-bg);
    box-shadow: var(--shadow-md);
    transition: box-shadow 0.2s;
}

.note-text {
    margin: 0;
    font-size: 1.15rem;
    line-height: 1.65;
    color: var(--fg);
    white-space: pre-wrap;
    overflow-wrap: anywhere;
}

/* Markdown rendered note */
.note-markdown {
    white-space: normal;
}

.note-markdown > :first-child {
    margin-top: 0;
}

.note-markdown > :last-child {
    margin-bottom: 0;
}

.note-markdown p,
.note-markdown ul,
.note-markdown ol,
.note-markdown blockquote {
    margin: 0 0 0.85em;
}

.note-markdown h1,
.note-markdown h2,
.note-markdown h3 {
    margin: 1.2em 0 0.5em;
    line-height: 1.3;
    font-weight: 700;
    letter-spacing: -0.02em;
    color: inherit;
}

.note-markdown h1 {
    font-size: 1.45em;
}

.note-markdown h2 {
    font-size: 1.25em;
}

.note-markdown h3 {
    font-size: 1.1em;
}

.note-markdown ul,
.note-markdown ol {
    padding-left: 1.5em;
}

.note-markdown li + li {
    margin-top: 0.35em;
}

.note-markdown a {
    color: var(--primary);
    text-decoration: underline;
    text-underline-offset: 3px;
    font-weight: 500;
}

.note-markdown blockquote {
    padding: 0.5rem 0.85rem;
    border-left: 4px solid var(--card-border);
    background: var(--bg-subtle);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
    font-style: italic;
    color: var(--muted);
}

/* Important Note Card */
.note-important {
    border: 3px solid var(--important-border);
    background: var(--important-bg);
    color: var(--important-fg);
    box-shadow: var(--important-shadow), var(--shadow-sm);
}

.note-flag {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    margin: 0 0 0.85rem;
    padding: 0.3rem 0.85rem;
    background: var(--important-flag-bg);
    color: var(--important-flag-fg);
    border-radius: var(--radius-full);
    font-size: 0.85rem;
    font-weight: 800;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    box-shadow: var(--important-flag-shadow);
}

.note-important .note-text {
    color: var(--important-fg);
    font-weight: 600;
}

.note-important .note-markdown a {
    color: var(--important-fg);
    text-decoration: underline;
    font-weight: 700;
}

.note-important .note-markdown blockquote {
    border-left-color: var(--important-quote-border);
    background: var(--important-quote-bg);
    color: var(--important-fg);
}

/* Empty State Note */
.note-empty {
    border: 1px solid var(--calm-border);
    background: var(--calm-bg);
    color: var(--calm-fg);
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.note-empty .note-text {
    color: var(--calm-fg);
    font-weight: 500;
    font-size: 1.1rem;
}

/* ==========================================================================
   Caregiver Acknowledgement Flow
   ========================================================================== */

.ack-confirmation {
    margin: 1.25rem 0 0;
    padding: 1rem 1.25rem;
    border-radius: var(--radius-lg);
    background: var(--calm-bg);
    border: 1px solid var(--calm-border);
    color: var(--calm-fg);
    font-size: 1.05rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    box-shadow: var(--shadow-sm);
    overflow-wrap: anywhere;
    animation: fadeIn 0.3s ease-out;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(-4px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.ack-form {
    display: flex;
    flex-direction: column;
    gap: 0.65rem;
    margin-top: 1.75rem;
    background: var(--card-bg);
    padding: 1.5rem;
    border: 1px solid var(--card-border);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow-sm);
}

.ack-label {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--muted);
}

.ack-name {
    width: 100%;
    padding: 0.85rem 1rem;
    border: 1.5px solid var(--input-border);
    border-radius: var(--radius-md);
    background: var(--bg);
    color: var(--fg);
    font: inherit;
    font-size: 1.15rem;
    outline: none;
    transition: border-color 0.15s, box-shadow 0.15s, background-color 0.15s;
}

.ack-name:focus {
    background: var(--input-focus-bg);
    border-color: var(--primary);
    box-shadow: var(--focus-ring);
}

.ack-submit {
    min-height: 3.75rem;
    margin-top: 0.5rem;
    border: none;
    border-radius: var(--radius-lg);
    background: var(--primary);
    color: var(--primary-fg);
    font: inherit;
    font-size: 1.25rem;
    font-weight: 700;
    cursor: pointer;
    box-shadow: var(--primary-glow);
    transition: background-color 0.15s, transform 0.05s, box-shadow 0.15s;
    touch-action: manipulation;
}

.ack-submit:hover {
    background: var(--primary-hover);
}

.ack-submit:active {
    transform: translateY(1px);
    background: var(--primary-active);
    box-shadow: var(--primary-glow-active);
}

/* ==========================================================================
   Client Dashboard & Editors
   ========================================================================== */

.editor-section {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-xl);
    padding: 1.75rem;
    box-shadow: var(--shadow-sm);
    margin-bottom: 2rem;
}

.editor-advance {
    background: var(--editor-advance-bg);
    border-left: 4px solid var(--primary);
}

.editor-hint {
    margin: 0 0 1.25rem;
    font-size: 0.875rem;
    color: var(--muted);
    line-height: 1.5;
}

.editor-form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.editor-label {
    font-size: 0.95rem;
    font-weight: 600;
    color: var(--fg);
}

.editor-text {
    width: 100%;
    min-height: 12rem;
    padding: 1rem;
    border: 1.5px solid var(--input-border);
    border-radius: var(--radius-md);
    background: var(--bg);
    color: var(--fg);
    font: inherit;
    font-size: 1.1rem;
    line-height: 1.6;
    resize: vertical;
    outline: none;
    transition: border-color 0.15s, box-shadow 0.15s, background-color 0.15s;
}

.editor-text:focus {
    background: var(--input-focus-bg);
    border-color: var(--primary);
    box-shadow: var(--focus-ring);
}

/* Toggle Switch for Viktigt */
.editor-important {
    display: inline-flex;
    align-items: center;
    gap: 0.85rem;
    padding: 0.75rem 1rem;
    border: 1.5px solid var(--important-soft-border);
    border-radius: var(--radius-lg);
    background: var(--important-soft-bg);
    color: var(--important-soft-fg);
    font-weight: 700;
    font-size: 0.95rem;
    cursor: pointer;
    user-select: none;
    transition: background-color 0.15s, border-color 0.15s;
}

.editor-toggle {
    appearance: none;
    position: relative;
    flex: none;
    width: 3.25rem;
    height: 1.85rem;
    margin: 0;
    border-radius: var(--radius-full);
    background: var(--toggle-off-bg);
    cursor: pointer;
    transition: background-color 0.2s ease;
    outline: none;
}

.editor-toggle::before {
    content: "";
    position: absolute;
    top: 0.2rem;
    left: 0.2rem;
    width: 1.45rem;
    height: 1.45rem;
    border-radius: 50%;
    background: var(--toggle-knob);
    box-shadow: var(--toggle-knob-shadow);
    transition: transform 0.2s cubic-bezier(0.16, 1, 0.3, 1);
}

.editor-toggle:checked {
    background: var(--toggle-on-bg);
}

.editor-toggle:checked::before {
    transform: translateX(1.4rem);
}

.editor-toggle:focus-visible {
    box-shadow: var(--toggle-focus-ring);
}

.editor-save {
    min-height: 3.5rem;
    border: none;
    border-radius: var(--radius-lg);
    background: var(--primary);
    color: var(--primary-fg);
    font: inherit;
    font-size: 1.25rem;
    font-weight: 700;
    cursor: pointer;
    box-shadow: var(--primary-glow);
    transition: background-color 0.15s, transform 0.05s, box-shadow 0.15s;
    touch-action: manipulation;
}

.editor-save:hover {
    background: var(--primary-hover);
}

.editor-save:active {
    transform: translateY(1px);
    background: var(--primary-active);
}

.editor-save:disabled,
.editor-clear:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    box-shadow: none;
    pointer-events: none;
}

.editor-saved {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    margin: 0.85rem 0 0;
    padding: 0.35rem 0.75rem;
    background: var(--calm-bg);
    border: 1px solid var(--calm-border);
    border-radius: var(--radius-full);
    color: var(--calm-fg);
    font-size: 0.85rem;
    font-weight: 600;
}

.editor-clear-form {
    margin-top: 1rem;
}

.editor-clear {
    width: 100%;
    min-height: 3rem;
    border: 1.5px solid var(--card-border);
    border-radius: var(--radius-md);
    background: var(--bg);
    color: var(--muted);
    font: inherit;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.15s, color 0.15s, border-color 0.15s;
    touch-action: manipulation;
}

.editor-clear:hover {
    background: var(--bg-subtle);
    color: var(--error-fg);
    border-color: var(--important-soft-border);
}

/* ==========================================================================
   Acknowledgement Feed
   ========================================================================== */

.ack-feed-section {
    background: var(--card-bg);
    border: 1px solid var(--card-border);
    border-radius: var(--radius-xl);
    padding: 1.75rem;
    box-shadow: var(--shadow-sm);
    margin-bottom: 2rem;
}

.ack-feed {
    margin: 0;
    padding: 0;
    list-style: none;
    display: flex;
    flex-direction: column;
}

.ack-feed-item {
    display: flex;
    align-items: center;
    padding: 0.85rem 0.5rem;
    border-bottom: 1px solid var(--bg-subtle);
    font-size: 1.05rem;
    font-weight: 500;
    color: var(--fg);
    overflow-wrap: anywhere;
}

.ack-feed-item:last-child {
    border-bottom: none;
}

.ack-feed-empty {
    margin: 0;
    padding: 1rem;
    text-align: center;
    background: var(--bg);
    border: 1px dashed var(--card-border);
    border-radius: var(--radius-lg);
    color: var(--muted);
    font-size: 0.95rem;
}

/* Shown when the feed poll finds the session has expired; stays in view
   while the Client copies unsaved text. */
.session-expired {
    position: sticky;
    top: 0;
    z-index: 10;
    margin: 0 0 1.25rem;
    padding: 1rem 1.25rem;
    border-radius: var(--radius-lg);
    background: var(--error-bg);
    border: 1px solid var(--error-border);
    color: var(--error-fg);
    font-weight: 700;
    box-shadow: var(--shadow-overlay);
}

/* The PIN Alert sits at the top of the dashboard until the service restarts
   with a new Caregiver PIN. */
.pin-alert {
    margin: 0 0 1.25rem;
    padding: 1rem 1.25rem;
    border-radius: var(--radius-lg);
    background: var(--error-bg);
    border: 1px solid var(--error-border);
    color: var(--error-fg);
    box-shadow: var(--shadow-sm);
}

.pin-alert p {
    margin: 0;
}

.pin-alert .pin-alert-title {
    margin-bottom: 0.35rem;
    font-weight: 700;
}

.ack-feed-expired {
    margin: 0;
    padding: 1rem 1.25rem;
    border-radius: var(--radius-lg);
    background: var(--error-bg);
    border: 1px solid var(--error-border);
    color: var(--error-fg);
    font-weight: 600;
    font-size: 0.95rem;
}

/* ==========================================================================
   Health Check Page
   ========================================================================== */

.health-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    min-height: 100dvh;
    text-align: center;
    background-color: var(--health-bg);
    color: var(--health-fg);
    font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
    font-size: 1.25rem;
    padding: 2rem;
}

.logo {
    max-width: 160px;
    margin-bottom: 2rem;
}

.status-item {
    margin: 0.5rem 0;
    padding: 0.5rem 1rem;
    background: var(--health-item-bg);
    border-radius: var(--radius-md);
}

.accent {
    color: var(--health-accent);
    font-weight: 700;
}
