/* ==========================================================================
1. GLOBAL SYSTEM ARCHITECTURE & MASTER VARIABLES
========================================================================== */

:root {
    --font-stack: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    --transition-speed: 220ms;
    --transition-curve: cubic-bezier(0.25, 1, 0.5, 1);

    /* Fallback / Default System Matching (Light Mode Default) */
    --bg-color: #f2f2f7;
    --box-bg: #ffffff;
    --text-primary: #3a3a3c;
    --text-bright: #000000;
    --text-muted: #8e8e93;
    --border-color: rgba(0, 0, 0, 0.08);
    --button-default: #e5e5ea;
    --button-text: #1c1c1e;
}

/* Testing switch: Uncomment the following line to hide the testing banner */
/* .testing-banner { display: none; } */

/* System Match: Dark Mode Preference */
@media (prefers-color-scheme: dark) {
    :root {
        --bg-color: #121212;
        --box-bg: #1c1c1e;
        --text-primary: #a0a0a2;
        --text-bright: #ffffff;
        --text-muted: #636366;
        --border-color: rgba(255, 255, 255, 0.08);
        --button-default: #2c2c2e;
        --button-text: #ffffff;
    }
}

/* Explicit Override: Muted Dark / Low Contrast */
body[data-theme="dark"][data-contrast="low"] {
    --bg-color: #121212;
    --box-bg: #1c1c1e;
    --text-primary: #a0a0a2;
    --text-bright: #ffffff;
    --text-muted: #636366;
    --border-color: rgba(255, 255, 255, 0.05);
    --button-default: #2c2c2e;
    --button-text: #ffffff;
}

/* Explicit Override: High Contrast Dark Mode */
body[data-theme="dark"][data-contrast="high"] {
    --bg-color: #000000;
    --box-bg: #121212;
    --text-primary: #ffffff;
    --text-bright: #ffffff;
    --text-muted: #eaeaea;
    --border-color: #ffffff;
    --button-default: #2c2c2e;
    --button-text: #ffffff;
}

/* Explicit Override: Clean Light Mode */
body[data-theme="light"][data-contrast="low"] {
    --bg-color: #f2f2f7;
    --box-bg: #ffffff;
    --text-primary: #3a3a3c;
    --text-bright: #000000;
    --text-muted: #8e8e93;
    --border-color: rgba(0, 0, 0, 0.08);
    --button-default: #e5e5ea;
    --button-text: #1c1c1e;
}

/* Explicit Override: High Contrast Light Mode */
body[data-theme="light"][data-contrast="high"] {
    --bg-color: #ffffff;
    --box-bg: #ffffff;
    --text-primary: #000000;
    --text-bright: #000000;
    --text-muted: #000000;
    --border-color: #000000;
    --button-default: #e5e5ea;
    --button-text: #000000;
}

/* Explicit Override: System Theme Matching (Low & High Contrast) */
body[data-theme="system"][data-contrast="low"] {
    --bg-color: #f2f2f7;
    --box-bg: #ffffff;
    --text-primary: #3a3a3c;
    --text-bright: #000000;
    --text-muted: #8e8e93;
    --border-color: rgba(0, 0, 0, 0.08);
    --button-default: #e5e5ea;
    --button-text: #1c1c1e;
}

@media (prefers-color-scheme: dark) {
    body[data-theme="system"][data-contrast="low"] {
        --bg-color: #121212;
        --box-bg: #1c1c1e;
        --text-primary: #a0a0a2;
        --text-bright: #ffffff;
        --text-muted: #636366;
        --border-color: rgba(255, 255, 255, 0.05);
        --button-default: #2c2c2e;
        --button-text: #ffffff;
    }
}

body[data-theme="system"][data-contrast="high"] {
    --bg-color: #ffffff;
    --box-bg: #ffffff;
    --text-primary: #000000;
    --text-bright: #000000;
    --text-muted: #000000;
    --border-color: #000000;
    --button-default: #e5e5ea;
    --button-text: #000000;
}

@media (prefers-color-scheme: dark) {
    body[data-theme="system"][data-contrast="high"] {
        --bg-color: #000000;
        --box-bg: #121212;
        --text-primary: #ffffff;
        --text-bright: #ffffff;
        --text-muted: #eaeaea;
        --border-color: #ffffff;
        --button-default: #2c2c2e;
        --button-text: #ffffff;
    }
}

html, body {
    margin: 0;
    padding: 0;
    width: 100vw;
    height: 100vh; /* Older viewport height spec*/
    overflow: hidden;
    background-color: var(--bg-color);
    font-family: var(--font-stack), sans-serif;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

@supports (height: 100dvh) {
    html, body {
        height: 100dvh; /* Dynamic viewport height for mobile devices */
    }
}

body {
    display: flex;
    flex-direction: column;
}

/* Testing banner styles */
.testing-banner {
    position: relative;
    flex: 0 0 auto;
    width: 100%;
    box-sizing: border-box;
    background-color: #ffeb3b; /* Bright yellow */
    color: #000000; /* Bold black text */
    text-align: center;
    padding: 3px 12px;
    z-index: 1000;
    font-weight: 600;
    font-size: 0.78rem;
    line-height: 1.25;
}

.testing-banner p {
    margin: 0;
}

/* ==========================================================================
2. VIEWPORT FRAMING & ORTHOGONAL TRANSITIONS
========================================================================== */
.viewport-wrapper {
    position: relative;
    width: 100vw;
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
}

.app-container {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: var(--bg-color);
}

.menu-toggle {
    position: absolute;
    top: 16px;
    right: 16px;
    z-index: 120;
    width: 44px;
    height: 44px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 0;
    border: 1px solid var(--border-color);
    border-radius: 12px;
    background-color: var(--box-bg);
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
    transition: transform 150ms var(--transition-curve), background-color 150ms var(--transition-curve), border-color 150ms var(--transition-curve);
}

body[data-menu-side="left"] .menu-toggle {
    left: 16px;
    right: auto;
}

body[data-menu-side="right"] .menu-toggle {
    right: 16px;
    left: auto;
}

.menu-toggle span {
    display: block;
    width: 20px;
    height: 2px;
    border-radius: 2px;
    background-color: var(--text-primary);
    transition: transform 200ms ease, opacity 200ms ease;
}

body.drawer-open .menu-toggle span:nth-child(1) {
    transform: translateY(6px) rotate(45deg);
}

body.drawer-open .menu-toggle span:nth-child(2) {
    opacity: 0;
}

body.drawer-open .menu-toggle span:nth-child(3) {
    transform: translateY(-6px) rotate(-45deg);
}

/* Slide-over Drawer Overlay & Panel */
.drawer-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.55);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: 100;
    opacity: 0;
    pointer-events: none;
    transition: opacity 250ms ease-out;
}

body.drawer-open .drawer-overlay {
    opacity: 1;
    pointer-events: auto;
}

.side-drawer {
    position: absolute;
    top: 0;
    bottom: 0;
    z-index: 110;
    width: calc(100% - 56px); /* Mobile sliver of underlying view visible on opposite side */
    max-width: 320px;
    background-color: var(--box-bg);
    border: 1px solid var(--border-color);
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    padding: 16px 20px 20px;
    gap: 20px;
    overflow-y: auto;
    transition: transform 260ms cubic-bezier(0.16, 1, 0.3, 1);
}

/* Right side default positioning */
body[data-menu-side="right"] .side-drawer,
body:not([data-menu-side]) .side-drawer {
    right: 0;
    left: auto;
    border-right: none;
    border-radius: 20px 0 0 20px;
    transform: translateX(100%);
    box-shadow: -8px 0 32px rgba(0, 0, 0, 0.35);
}

/* Left side positioning */
body[data-menu-side="left"] .side-drawer {
    left: 0;
    right: auto;
    border-left: none;
    border-radius: 0 20px 20px 0;
    transform: translateX(-100%);
    box-shadow: 8px 0 32px rgba(0, 0, 0, 0.35);
}

body.drawer-open .side-drawer {
    transform: translateX(0);
}

.drawer-header {
    display: flex;
    align-items: center;
    min-height: 44px;
    padding-right: 52px;
    padding-left: 0;
    padding-bottom: 12px;
    border-bottom: 1px solid var(--border-color);
}

.drawer-title {
    font-size: 1.1rem;
    font-weight: 700;
    margin: 0;
    color: var(--text-bright);
}

.drawer-nav {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.drawer-nav-button {
    display: flex;
    align-items: center;
    width: 100%;
    padding: 12px 16px;
    border: 1px solid var(--border-color);
    border-radius: 12px;
    background-color: rgba(255, 255, 255, 0.03);
    color: var(--text-primary);
    font-size: 0.95rem;
    font-weight: 600;
    text-align: left;
    cursor: pointer;
    transition: background-color 150ms ease, border-color 150ms ease, color 150ms ease;
}

.drawer-nav-button:hover {
    background-color: rgba(255, 255, 255, 0.08);
    border-color: var(--text-muted);
    color: var(--text-bright);
}

.drawer-nav-button:active {
    transform: scale(0.98);
}

.drawer-nav-button.active {
    background-color: var(--button-default);
    color: var(--text-bright);
    border-color: var(--text-muted);
    font-weight: 700;
}

.drawer-quick-prefs {
    margin-top: auto;
    padding-top: 16px;
    border-top: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.drawer-subtitle {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    margin: 0 0 4px 0;
}

.app-canvas {
    position: absolute;
    top: 0;
    left: 0;
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    padding: 16px;
    gap: 16px;
    transition: transform 260ms cubic-bezier(0.16, 1, 0.3, 1), opacity 220ms ease-out, filter 260ms ease-out, visibility 260ms linear;
    will-change: transform, opacity;
}

/* Suppress all transitions and animations during cold load, theme initialization, and page refreshes */
body.suppress-transitions,
body.suppress-transitions *,
body.suppress-transitions *::before,
body.suppress-transitions *::after {
    transition: none !important;
    animation: none !important;
}

.app-canvas.no-transition {
    transition: none !important;
}

.view-active {
    transform: translate(0, 0);
    opacity: 1;
    z-index: 10;
    visibility: visible;
    pointer-events: auto;
}

body.drawer-open .app-canvas.view-active {
    transform: scale(0.97);
    filter: brightness(0.75);
    pointer-events: none;
    overflow: hidden !important;
}

.view-hidden-bottom { transform: translateY(100%); opacity: 0; z-index: 5; visibility: hidden; pointer-events: none; }
.view-hidden-top { transform: translateY(-100%); opacity: 0; z-index: 5; visibility: hidden; pointer-events: none; }
.view-hidden-left { transform: translateX(-100%); opacity: 0; z-index: 5; visibility: hidden; pointer-events: none; }
.view-hidden-right { transform: translateX(100%); opacity: 0; z-index: 5; visibility: hidden; pointer-events: none; }

/* Question Advance Micro-Transitions within Tracker */
.question-transition-out {
    opacity: 0 !important;
    transform: translateX(-24px);
    transition: opacity 120ms ease-in, transform 120ms ease-in !important;
}

.question-transition-enter {
    opacity: 0 !important;
    transform: translateX(24px) !important;
    transition: none !important;
}

.question-transition-in {
    opacity: 1;
    transform: translateX(0);
    transition: opacity 160ms cubic-bezier(0.16, 1, 0.3, 1), transform 160ms cubic-bezier(0.16, 1, 0.3, 1);
}

/* ==========================================================================
3. BOX GRID COMPONENTS (THE FLEX ARCHITECTURE)
========================================================================== */
.canvas-box {
    background-color: var(--box-bg);
    border-radius: 20px;
    border: 1px solid var(--border-color);
    padding: 20px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.header-box {
    flex: 0 0 auto;
    text-align: center;
    padding: 16px 20px;
    min-height: 60px;
}

.view-header-box {
    flex: 0 0 auto;
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 14px;
    padding: 12px 18px;
    min-height: 56px;
    text-align: left;
}

.view-header-box .canvas-back-button {
    margin-bottom: 0;
    align-self: center;
    flex-shrink: 0;
}

.input-box, .menu-box {
    flex: 1;
    justify-content: space-around;
}

.view-content-box {
    flex: 1;
    overflow-y: auto;
    justify-content: flex-start;
    padding: 20px;
    gap: 16px;
}

.footer-box {
    flex: 0 0 auto;
    flex-direction: row;
    gap: 12px;
}

.progress-text {
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    font-weight: 600;
}

.question-text {
    font-size: 1.35rem;
    font-weight: 600;
    margin: 8px 0 0 0;
    color: var(--text-bright);
    line-height: 1.3;
}

.view-title {
    font-size: 1.18rem;
    font-weight: 700;
    margin: 0;
    color: var(--text-bright);
    line-height: 1.25;
    letter-spacing: -0.01em;
}

body[data-contrast="high"] .view-title {
    font-weight: 800;
}

h3 {
    color: var(--text-bright);
    margin-top: 0;
}

/* ==========================================================================
4. INTERACTIVE BUTTON INTERFACES (INPUT DECK)
========================================================================== */
.button-stack {
    display: flex;
    flex-direction: column;
    height: 100%;
    gap: 10px;
    justify-content: space-between;
}

.completion-view {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    width: 100%;
    max-width: 320px;
    margin: 0 auto;
    text-align: center;
    height: 100%;
}

.completion-view[hidden] {
    display: none !important;
}

.completion-message {
    margin: 0;
    color: var(--text-muted);
    font-size: 0.95rem;
    line-height: 1.4;
}

.button-new-checkin {
    width: 100%;
    min-height: 52px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 1rem;
    border: 1px solid var(--border-color);
    border-radius: 12px;
    background-color: var(--button-default);
    color: var(--text-bright);
    cursor: pointer;
    transition: transform 150ms var(--transition-curve), filter 150ms var(--transition-curve), background-color 150ms var(--transition-curve);
}

.button-new-checkin:active {
    transform: scale(0.98);
}

@media (hover: hover) {
    .button-new-checkin:hover {
        background-color: var(--border-color);
        filter: brightness(1.06);
    }
}

.score-button {
    flex: 1;
    display: flex;
    align-items: center;
    border: none;
    border-radius: 14px;
    padding: 0 20px;
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--button-text);
    cursor: pointer;
    text-align: left;
    background-color: var(--button-color, var(--button-default));
    transition: transform 150ms var(--transition-curve), filter 150ms var(--transition-curve), background-color 150ms var(--transition-curve);
}

.score-button .num {
    font-size: 1.75rem;
    font-weight: 700;
    margin-right: 16px;
    opacity: 0.5;
}

.score-button .label-desc {
    font-size: 0.95rem;
    font-weight: 500;
    opacity: 0.85;
}

.score-button:active,
.score-button.desktop-click:active {
    transform: scale(0.99);
}

/* Hover effects for interactive buttons on devices supporting hover */
@media (hover: hover) {
    .score-button:hover {
        filter: brightness(1.08);
    }
    .control-button:hover, .utility-button:hover, .menu-toggle:hover {
        background-color: var(--border-color);
    }
}

/* ==========================================================================
5. THE CHOREOGRAPHED MULTI-CURVE COLOR ENGINE (THEMED)
========================================================================== */

/* --- 5a. LIGHT MODE DEFAULT PALETTE --- */
.input-box[data-curve="less-is-better"] button[data-score="1"] { --button-color: #e2f0d9; }
.input-box[data-curve="less-is-better"] button[data-score="2"] { --button-color: #eef3db; }
.input-box[data-curve="less-is-better"] button[data-score="3"] { --button-color: #fff3cd; }
.input-box[data-curve="less-is-better"] button[data-score="4"] { --button-color: #fbe4db; }
.input-box[data-curve="less-is-better"] button[data-score="5"] { --button-color: #f8d7da; }

.input-box[data-curve="more-is-better"] button[data-score="1"] { --button-color: #f8d7da; }
.input-box[data-curve="more-is-better"] button[data-score="2"] { --button-color: #fbe4db; }
.input-box[data-curve="more-is-better"] button[data-score="3"] { --button-color: #fff3cd; }
.input-box[data-curve="more-is-better"] button[data-score="4"] { --button-color: #eef3db; }
.input-box[data-curve="more-is-better"] button[data-score="5"] { --button-color: #e2f0d9; }

.input-box[data-curve="middle-is-best"] button[data-score="1"] { --button-color: #d6e0f5; }
.input-box[data-curve="middle-is-best"] button[data-score="2"] { --button-color: #d8ece6; }
.input-box[data-curve="middle-is-best"] button[data-score="3"] { --button-color: #e2f0d9; }
.input-box[data-curve="middle-is-best"] button[data-score="4"] { --button-color: #fdeccf; }
.input-box[data-curve="middle-is-best"] button[data-score="5"] { --button-color: #ffd9c2; }

/* --- 5b. SYSTEM DARK MODE PALETTE (Applies when System is Dark) --- */
@media (prefers-color-scheme: dark) {
    .input-box[data-curve="less-is-better"] button[data-score="1"] { --button-color: #2e4a3f; }
    .input-box[data-curve="less-is-better"] button[data-score="2"] { --button-color: #3b4d36; }
    .input-box[data-curve="less-is-better"] button[data-score="3"] { --button-color: #524e31; }
    .input-box[data-curve="less-is-better"] button[data-score="4"] { --button-color: #5c3d2e; }
    .input-box[data-curve="less-is-better"] button[data-score="5"] { --button-color: #612f2f; }

    .input-box[data-curve="more-is-better"] button[data-score="1"] { --button-color: #612f2f; }
    .input-box[data-curve="more-is-better"] button[data-score="2"] { --button-color: #5c3d2e; }
    .input-box[data-curve="more-is-better"] button[data-score="3"] { --button-color: #524e31; }
    .input-box[data-curve="more-is-better"] button[data-score="4"] { --button-color: #3b4d36; }
    .input-box[data-curve="more-is-better"] button[data-score="5"] { --button-color: #2e4a3f; }

    .input-box[data-curve="middle-is-best"] button[data-score="1"] { --button-color: #2f3a61; }
    .input-box[data-curve="middle-is-best"] button[data-score="2"] { --button-color: #34485a; }
    .input-box[data-curve="middle-is-best"] button[data-score="3"] { --button-color: #2e4a3f; }
    .input-box[data-curve="middle-is-best"] button[data-score="4"] { --button-color: #5c4d2e; }
    .input-box[data-curve="middle-is-best"] button[data-score="5"] { --button-color: #6e3a24; }
}

/* --- 5c. EXPLICIT BODY THEME OVERRIDES --- */
body[data-theme="dark"] .input-box[data-curve="less-is-better"] button[data-score="1"] { --button-color: #2e4a3f; }
body[data-theme="dark"] .input-box[data-curve="less-is-better"] button[data-score="2"] { --button-color: #3b4d36; }
body[data-theme="dark"] .input-box[data-curve="less-is-better"] button[data-score="3"] { --button-color: #524e31; }
body[data-theme="dark"] .input-box[data-curve="less-is-better"] button[data-score="4"] { --button-color: #5c3d2e; }
body[data-theme="dark"] .input-box[data-curve="less-is-better"] button[data-score="5"] { --button-color: #612f2f; }

body[data-theme="dark"] .input-box[data-curve="more-is-better"] button[data-score="1"] { --button-color: #612f2f; }
body[data-theme="dark"] .input-box[data-curve="more-is-better"] button[data-score="2"] { --button-color: #5c3d2e; }
body[data-theme="dark"] .input-box[data-curve="more-is-better"] button[data-score="3"] { --button-color: #524e31; }
body[data-theme="dark"] .input-box[data-curve="more-is-better"] button[data-score="4"] { --button-color: #3b4d36; }
body[data-theme="dark"] .input-box[data-curve="more-is-better"] button[data-score="5"] { --button-color: #2e4a3f; }

body[data-theme="dark"] .input-box[data-curve="middle-is-best"] button[data-score="1"] { --button-color: #2f3a61; }
body[data-theme="dark"] .input-box[data-curve="middle-is-best"] button[data-score="2"] { --button-color: #34485a; }
body[data-theme="dark"] .input-box[data-curve="middle-is-best"] button[data-score="3"] { --button-color: #2e4a3f; }
body[data-theme="dark"] .input-box[data-curve="middle-is-best"] button[data-score="4"] { --button-color: #5c4d2e; }
body[data-theme="dark"] .input-box[data-curve="middle-is-best"] button[data-score="5"] { --button-color: #6e3a24; }

body[data-theme="light"] .input-box[data-curve="less-is-better"] button[data-score="1"] { --button-color: #e2f0d9; }
body[data-theme="light"] .input-box[data-curve="less-is-better"] button[data-score="2"] { --button-color: #eef3db; }
body[data-theme="light"] .input-box[data-curve="less-is-better"] button[data-score="3"] { --button-color: #fff3cd; }
body[data-theme="light"] .input-box[data-curve="less-is-better"] button[data-score="4"] { --button-color: #fbe4db; }
body[data-theme="light"] .input-box[data-curve="less-is-better"] button[data-score="5"] { --button-color: #f8d7da; }

body[data-theme="light"] .input-box[data-curve="more-is-better"] button[data-score="1"] { --button-color: #f8d7da; }
body[data-theme="light"] .input-box[data-curve="more-is-better"] button[data-score="2"] { --button-color: #fbe4db; }
body[data-theme="light"] .input-box[data-curve="more-is-better"] button[data-score="3"] { --button-color: #fff3cd; }
body[data-theme="light"] .input-box[data-curve="more-is-better"] button[data-score="4"] { --button-color: #eef3db; }
body[data-theme="light"] .input-box[data-curve="more-is-better"] button[data-score="5"] { --button-color: #e2f0d9; }

body[data-theme="light"] .input-box[data-curve="middle-is-best"] button[data-score="1"] { --button-color: #d6e0f5; }
body[data-theme="light"] .input-box[data-curve="middle-is-best"] button[data-score="2"] { --button-color: #d8ece6; }
body[data-theme="light"] .input-box[data-curve="middle-is-best"] button[data-score="3"] { --button-color: #e2f0d9; }
body[data-theme="light"] .input-box[data-curve="middle-is-best"] button[data-score="4"] { --button-color: #fdeccf; }
body[data-theme="light"] .input-box[data-curve="middle-is-best"] button[data-score="5"] { --button-color: #ffd9c2; }

body[data-theme="system"] .input-box[data-curve="less-is-better"] button[data-score="1"] { --button-color: #e2f0d9; }
body[data-theme="system"] .input-box[data-curve="less-is-better"] button[data-score="2"] { --button-color: #eef3db; }
body[data-theme="system"] .input-box[data-curve="less-is-better"] button[data-score="3"] { --button-color: #fff3cd; }
body[data-theme="system"] .input-box[data-curve="less-is-better"] button[data-score="4"] { --button-color: #fbe4db; }
body[data-theme="system"] .input-box[data-curve="less-is-better"] button[data-score="5"] { --button-color: #f8d7da; }

body[data-theme="system"] .input-box[data-curve="more-is-better"] button[data-score="1"] { --button-color: #f8d7da; }
body[data-theme="system"] .input-box[data-curve="more-is-better"] button[data-score="2"] { --button-color: #fbe4db; }
body[data-theme="system"] .input-box[data-curve="more-is-better"] button[data-score="3"] { --button-color: #fff3cd; }
body[data-theme="system"] .input-box[data-curve="more-is-better"] button[data-score="4"] { --button-color: #eef3db; }
body[data-theme="system"] .input-box[data-curve="more-is-better"] button[data-score="5"] { --button-color: #e2f0d9; }

body[data-theme="system"] .input-box[data-curve="middle-is-best"] button[data-score="1"] { --button-color: #d6e0f5; }
body[data-theme="system"] .input-box[data-curve="middle-is-best"] button[data-score="2"] { --button-color: #d8ece6; }
body[data-theme="system"] .input-box[data-curve="middle-is-best"] button[data-score="3"] { --button-color: #e2f0d9; }
body[data-theme="system"] .input-box[data-curve="middle-is-best"] button[data-score="4"] { --button-color: #fdeccf; }
body[data-theme="system"] .input-box[data-curve="middle-is-best"] button[data-score="5"] { --button-color: #ffd9c2; }

@media (prefers-color-scheme: dark) {
    body[data-theme="system"] .input-box[data-curve="less-is-better"] button[data-score="1"] { --button-color: #2e4a3f; }
    body[data-theme="system"] .input-box[data-curve="less-is-better"] button[data-score="2"] { --button-color: #3b4d36; }
    body[data-theme="system"] .input-box[data-curve="less-is-better"] button[data-score="3"] { --button-color: #524e31; }
    body[data-theme="system"] .input-box[data-curve="less-is-better"] button[data-score="4"] { --button-color: #5c3d2e; }
    body[data-theme="system"] .input-box[data-curve="less-is-better"] button[data-score="5"] { --button-color: #612f2f; }

    body[data-theme="system"] .input-box[data-curve="more-is-better"] button[data-score="1"] { --button-color: #612f2f; }
    body[data-theme="system"] .input-box[data-curve="more-is-better"] button[data-score="2"] { --button-color: #5c3d2e; }
    body[data-theme="system"] .input-box[data-curve="more-is-better"] button[data-score="3"] { --button-color: #524e31; }
    body[data-theme="system"] .input-box[data-curve="more-is-better"] button[data-score="4"] { --button-color: #3b4d36; }
    body[data-theme="system"] .input-box[data-curve="more-is-better"] button[data-score="5"] { --button-color: #2e4a3f; }

    body[data-theme="system"] .input-box[data-curve="middle-is-best"] button[data-score="1"] { --button-color: #2f3a61; }
    body[data-theme="system"] .input-box[data-curve="middle-is-best"] button[data-score="2"] { --button-color: #34485a; }
    body[data-theme="system"] .input-box[data-curve="middle-is-best"] button[data-score="3"] { --button-color: #2e4a3f; }
    body[data-theme="system"] .input-box[data-curve="middle-is-best"] button[data-score="4"] { --button-color: #5c4d2e; }
    body[data-theme="system"] .input-box[data-curve="middle-is-best"] button[data-score="5"] { --button-color: #6e3a24; }
}

/* ==========================================================================
6. HOLD-TO-ACTUATE ENGINE & UTILITIES
========================================================================== */
.control-button {
    position: relative;
    flex: 1;
    height: 56px;
    border: none;
    border-radius: 14px;
    background-color: var(--button-default);
    color: var(--text-primary);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
}

.hold-indicator {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 0;
    background-color: rgba(0, 0, 0, 0.18);
    pointer-events: none;
    transition: width 240ms cubic-bezier(0.2, 0.8, 0.4, 1);
}

/* Light Mode (Explicit or System Default) */
body[data-theme="light"] .hold-indicator,
body:not([data-theme="dark"]) .hold-indicator {
    background-color: rgba(0, 0, 0, 0.18);
}

/* Dark Mode (Explicit or System Preference) */
body[data-theme="dark"] .hold-indicator {
    background-color: rgba(255, 255, 255, 0.25);
}

@media (prefers-color-scheme: dark) {
    body[data-theme="system"] .hold-indicator,
    body:not([data-theme]) .hold-indicator {
        background-color: rgba(255, 255, 255, 0.25);
    }
}

/* High Contrast Adjustments */
body[data-contrast="high"] .hold-indicator {
    background-color: rgba(0, 0, 0, 0.35);
}

body[data-theme="dark"][data-contrast="high"] .hold-indicator {
    background-color: rgba(255, 255, 255, 0.45);
}

@media (prefers-color-scheme: dark) {
    body[data-theme="system"][data-contrast="high"] .hold-indicator {
        background-color: rgba(255, 255, 255, 0.45);
    }
}

.control-button.is-holding .hold-indicator {
    width: 100%;
    transition: width 1500ms cubic-bezier(0.08, 0.82, 0.17, 1);
}

body[data-hold-delay="disabled"] .hold-indicator {
    display: none !important;
}

.button-label {
    position: relative;
    z-index: 2;
}

.setting-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 14px;
    color: var(--text-primary);
}

select, .utility-button, .form-input {
    padding: 10px 14px;
    border-radius: 10px;
    border: 1px solid var(--border-color);
    background-color: var(--button-default);
    color: var(--text-bright);
    font-size: 0.95rem;
}

.button-grid {
    display: flex;
    gap: 10px;
    margin-bottom: 12px;
}

.utility-button {
    flex: 1;
    cursor: pointer;
    text-align: center;
}

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

.canvas-back-button {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    align-self: flex-start;
    padding: 8px 14px 8px 10px;
    margin-bottom: 8px;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    background-color: var(--button-default);
    color: var(--button-text);
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 150ms var(--transition-curve);
}

.canvas-back-button .back-arrow {
    font-size: 1.1rem;
    line-height: 1;
}

@media (hover: hover) {
    .canvas-back-button:hover {
        background-color: var(--border-color);
    }
}

/* ==========================================================================
7. CUSTOM-QUESTION AUTHORING FORM & VIEW PANELS
========================================================================== */

.view-canvas {
    overflow: hidden;
}

.question-form {
    display: flex;
    flex-direction: column;
    gap: 14px;
}

.form-field {
    display: flex;
    flex-direction: column;
    gap: 6px;
    color: var(--text-primary);
}

.form-field label,
.preview-caption {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-primary);
}

.form-input {
    width: 100%;
    box-sizing: border-box;
    font-family: inherit;
    resize: vertical;
}

.required-marker {
    color: #d9534f;
    font-weight: 700;
}

.optional-hint {
    color: var(--text-muted);
    font-weight: 400;
    font-size: 0.8rem;
}

.form-hint {
    margin: 0;
    font-size: 0.82rem;
    color: var(--text-muted);
    line-height: 1.4;
}

.question-preview {
    height: 300px;
    padding: 12px;
    border-radius: 14px;
    border: 1px solid var(--border-color);
    background-color: var(--bg-color);
    display: flex;
    flex-direction: column;
}

.checkbox-row {
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--text-primary);
    font-size: 0.95rem;
    cursor: pointer;
}

.checkbox-row input {
    width: 18px;
    height: 18px;
}

/* ==========================================================================
7b. GRAPH & TIMELINE LEGEND & CONTROLS (TASK 3.6 - 3.8)
========================================================================== */
.legend-quick-actions {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-top: 10px;
    margin-bottom: 4px;
    width: 100%;
    box-sizing: border-box;
    flex-wrap: wrap;
}

.legend-quick-title {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-muted);
}

.legend-quick-buttons {
    display: flex;
    align-items: center;
    gap: 6px;
}

.legend-quick-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 4px 10px;
    min-height: 28px;
    border-radius: 6px;
    border: 1px solid var(--border-color);
    background-color: var(--button-default);
    color: var(--text-bright);
    font-size: 0.78rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 150ms var(--transition-curve), border-color 150ms var(--transition-curve);
}

@media (hover: hover) {
    .legend-quick-button:hover {
        background-color: var(--border-color);
    }
}

.graph-legend-checklist {
    display: grid;
    grid-template-columns: 1fr;
    gap: 6px;
    margin-top: 4px;
    width: 100%;
    box-sizing: border-box;
}

@media (min-width: 480px) {
    .graph-legend-checklist {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}

@media (min-width: 768px) {
    .graph-legend-checklist {
        grid-template-columns: repeat(3, minmax(0, 1fr));
    }
}

@media (min-width: 1040px) {
    .graph-legend-checklist {
        grid-template-columns: repeat(4, minmax(0, 1fr));
    }
}

@media (orientation: landscape) and (max-height: 600px) {
    .graph-legend-checklist {
        grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
        gap: 6px;
    }
}

.legend-checklist-row {
    display: flex;
    align-items: stretch;
    gap: 4px;
    width: 100%;
    box-sizing: border-box;
}

.legend-checklist-row .legend-checklist-item {
    flex: 1;
    min-width: 0;
}

.legend-checklist-item {
    display: flex;
    align-items: center;
    gap: 7px;
    padding: 6px 10px;
    min-height: 36px;
    border-radius: 8px;
    border: 1px solid var(--border-color);
    background-color: var(--button-default);
    color: var(--text-bright);
    font-size: 0.84rem;
    font-weight: 500;
    cursor: pointer;
    text-align: left;
    user-select: none;
    transition: opacity 160ms var(--transition-curve), background-color 160ms var(--transition-curve), border-color 160ms var(--transition-curve), transform 120ms var(--transition-curve);
    box-sizing: border-box;
    overflow: hidden;
}

.legend-label {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
}

.legend-checklist-item:active {
    transform: scale(0.98);
}

@media (hover: hover) {
    .legend-checklist-item:hover {
        background-color: var(--border-color);
        filter: brightness(1.05);
    }
}

.legend-checklist-item[aria-checked="false"] {
    opacity: 0.42;
    background-color: transparent;
    border-style: dashed;
}

.legend-checkbox-box {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    border-radius: 3px;
    border: 1px solid var(--text-muted);
    flex-shrink: 0;
    font-size: 10px;
    font-weight: 800;
    line-height: 1;
    color: var(--text-bright);
    background-color: var(--box-bg);
}

.legend-checklist-item[aria-checked="true"] .legend-checkbox-box {
    border-color: var(--text-bright);
    background-color: var(--button-default);
    color: var(--text-bright);
}

.legend-isolate-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    min-height: 36px;
    padding: 0;
    border-radius: 8px;
    border: 1px solid var(--border-color);
    background-color: var(--button-default);
    color: var(--text-muted);
    cursor: pointer;
    flex-shrink: 0;
    transition: background-color 160ms var(--transition-curve), color 160ms var(--transition-curve), border-color 160ms var(--transition-curve), transform 120ms var(--transition-curve);
    user-select: none;
}

.legend-isolate-button:active {
    transform: scale(0.95);
}

@media (hover: hover) {
    .legend-isolate-button:hover {
        background-color: var(--border-color);
        color: var(--text-bright);
    }
}

.legend-isolate-button.is-isolated {
    background-color: var(--text-bright);
    color: var(--box-bg);
    border-color: var(--text-bright);
}

.isolate-icon {
    display: block;
    width: 14px;
    height: 14px;
    pointer-events: none;
}

body[data-contrast="high"] .legend-quick-button {
    border: 2px solid var(--text-bright);
}

body[data-contrast="high"] .legend-checklist-item {
    border: 2px solid var(--text-bright);
}

body[data-contrast="high"] .legend-checklist-item[aria-checked="false"] {
    border: 2px dashed var(--text-muted);
    opacity: 0.5;
}

body[data-contrast="high"] .legend-checkbox-box {
    border: 2px solid var(--text-bright);
}

body[data-contrast="high"] .legend-isolate-button {
    border: 2px solid var(--text-bright);
}

body[data-contrast="high"] .legend-isolate-button.is-isolated {
    border: 2px solid var(--text-bright);
    background-color: var(--text-bright);
    color: var(--box-bg);
}

.legend-swatch {
    flex-shrink: 0;
}

.legend-label {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* ==========================================================================
8. DESKTOP & TABLET LAYOUT ORIENTATION (MEDIA QUERY)
========================================================================== */
@media (min-width: 768px) {
    .app-container {
        width: 100%;
        max-width: 1080px;
        height: 100%;
        max-height: 640px;
        margin: auto;
        box-shadow: 0 20px 40px rgba(0,0,0,0.2);
        border-radius: 24px;
        border: 1px solid var(--border-color);
        overflow: hidden;
    }

    /* 8a. Tracker Canvas: 3-column deck layout (Question on left, input in center, action buttons on right) */
    #tracker-canvas {
        flex-direction: row;
        gap: 16px;
    }

    #tracker-canvas .header-box {
        flex: 1;
        height: 100%;
        text-align: center;
    }

    #tracker-canvas .input-box {
        flex: 2;
        height: 100%;
        overflow-y: auto;
    }

    #tracker-canvas .footer-box {
        flex: 0 0 180px;
        flex-direction: column;
        justify-content: center;
        height: 100%;
        gap: 16px;
    }

    #tracker-canvas .footer-box .control-button {
        flex: 0 0 64px;
        width: 100%;
    }

    /* 8b. Non-Tracker Views: Vertical hierarchy (Compact header bar on top above content, not taking full height side space) */
    .view-canvas,
    .app-canvas:not(#tracker-canvas) {
        flex-direction: column;
        gap: 14px;
    }

    .view-header-box {
        flex: 0 0 auto;
        width: 100%;
        height: auto;
        padding: 14px 20px;
        display: flex;
        flex-direction: row;
        align-items: center;
        gap: 16px;
    }

    .view-title {
        font-size: 1.25rem;
    }

    .view-content-box,
    .app-canvas:not(#tracker-canvas) .menu-box {
        flex: 1;
        width: 100%;
        height: auto;
        overflow-y: auto;
        justify-content: flex-start;
    }

    .button-stack {
        gap: 12px;
    }
}

/* ==========================================================================
9. LAYOUT RIG DEBUG OUTLINES (SANITY CHECK BOUNDARIES)
========================================================================== */
body[data-debug-bounds="true"] .viewport-wrapper { outline: 2px dashed #3b82f6 !important; outline-offset: -2px; }
body[data-debug-bounds="true"] .app-container { outline: 2px dashed #10b981 !important; outline-offset: -2px; }
body[data-debug-bounds="true"] .app-canvas { outline: 1px solid #f59e0b !important; }
body[data-debug-bounds="true"] .canvas-box { outline: 1px solid #ec4899 !important; }
body[data-debug-bounds="true"] .side-drawer { outline: 2px solid #8b5cf6 !important; }
body[data-debug-bounds="true"] .menu-toggle { outline: 2px solid #ef4444 !important; }

/* ==========================================================================
10. ACCESSIBILITY & KEYBOARD FOCUS STYLES
========================================================================== */
:focus-visible,
button:focus-visible,
select:focus-visible,
input:focus-visible,
textarea:focus-visible,
label[tabindex="0"]:focus-visible {
    outline: 3px solid #3b82f6;
    outline-offset: 3px;
    box-shadow: 0 0 0 4px rgba(59, 130, 246, 0.4);
}

body[data-contrast="high"] :focus-visible,
body[data-contrast="high"] button:focus-visible,
body[data-contrast="high"] select:focus-visible,
body[data-contrast="high"] input:focus-visible,
body[data-contrast="high"] textarea:focus-visible,
body[data-contrast="high"] label[tabindex="0"]:focus-visible {
    outline: 4px solid var(--text-bright);
    outline-offset: 3px;
}

/* ==========================================================================
11. MODAL POP-IN DIALOG (USER NOTES & OVERLAYS)
========================================================================== */
.modal-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.65);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    z-index: 150;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px;
    box-sizing: border-box;
    opacity: 0;
    pointer-events: none;
    transition: opacity 220ms var(--transition-curve);
}

body[data-theme="light"] .modal-overlay,
body:not([data-theme="dark"]) .modal-overlay {
    background-color: rgba(0, 0, 0, 0.45);
}

.modal-overlay.is-open {
    opacity: 1;
    pointer-events: auto;
}

.modal-dialog {
    width: 100%;
    max-width: 440px;
    background-color: var(--box-bg);
    border: 1px solid var(--border-color);
    border-radius: 20px;
    padding: 20px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    gap: 16px;
    box-shadow: 0 16px 40px rgba(0, 0, 0, 0.35);
    transform: scale(0.92) translateY(12px);
    opacity: 0;
    transition: transform 240ms cubic-bezier(0.16, 1, 0.3, 1), opacity 240ms ease;
}

.modal-overlay.is-open .modal-dialog {
    transform: scale(1) translateY(0);
    opacity: 1;
}

.modal-header {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.modal-title {
    margin: 0;
    font-size: 1.15rem;
    font-weight: 700;
    color: var(--text-bright);
    line-height: 1.25;
}

.modal-subtitle {
    margin: 0;
    font-size: 0.88rem;
    color: var(--text-muted);
    line-height: 1.35;
}

.modal-body {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.modal-textarea {
    min-height: 110px;
    max-height: 220px;
    padding: 12px 14px;
    line-height: 1.45;
    font-size: 0.95rem;
    border-radius: 12px;
    resize: vertical;
    border: 1px solid var(--border-color);
    background-color: var(--button-default);
    color: var(--text-bright);
    box-sizing: border-box;
    width: 100%;
}

.modal-footer {
    display: flex;
    gap: 10px;
    margin-bottom: 0;
}

.modal-footer-stacked {
    flex-direction: column;
    gap: 10px;
}

.modal-button {
    height: 50px;
    border-radius: 12px;
    font-size: 0.95rem;
    border: 1px solid var(--border-color);
}

.modal-button-primary {
    font-weight: 700;
}

.modal-button-danger {
    color: var(--text-bright);
    border-color: rgba(220, 38, 38, 0.4);
}

.modal-button-danger:hover {
    border-color: rgba(220, 38, 38, 0.8);
    background-color: rgba(220, 38, 38, 0.12);
}

.modal-info-badge {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    border-radius: 10px;
    background-color: var(--button-default);
    border: 1px solid var(--border-color);
    font-size: 0.9rem;
    overflow: hidden;
}

.modal-info-badge .badge-label {
    color: var(--text-muted);
    font-weight: 600;
    flex-shrink: 0;
}

.modal-info-badge .badge-value {
    color: var(--text-bright);
    font-family: monospace;
    font-size: 0.88rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.modal-info-text {
    display: flex;
    flex-direction: column;
    gap: 8px;
    font-size: 0.88rem;
    color: var(--text-muted);
    line-height: 1.4;
}

.modal-info-text p {
    margin: 0;
}

.modal-info-text strong {
    color: var(--text-bright);
}

/* High Contrast Adjustments for Modal */
body[data-contrast="high"] .modal-dialog {
    border: 2px solid var(--text-bright);
    box-shadow: none;
}

body[data-contrast="high"] .modal-textarea {
    border: 1px solid var(--text-bright);
}

body[data-contrast="high"] .modal-button {
    border: 2px solid var(--text-bright);
}

body[data-contrast="high"] .modal-button-danger {
    border: 2px solid var(--text-bright);
}

body[data-contrast="high"] .modal-info-badge {
    border: 1px solid var(--text-bright);
}

/* Screen reader only utility */
.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;
}

