/* ==========================================================================
   GestSure Interpreter
   1. Tokens
   2. Base
   3. Layout
   4. Components
   5. JS colour mapping
   6. Responsive
   7. Motion preferences
   ========================================================================== */


/* ==========================================================================
   1. Tokens
   ========================================================================== */

:root {
    /* Primary blue */
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --primary-press: #1e40af;
    --primary-50: #eff6ff;
    --primary-100: #dbeafe;
    --primary-200: #bfdbfe;

    /* Aliases used by the dialog styles */
    --brand-50: var(--primary-50);
    --brand-100: var(--primary-100);
    --brand-600: var(--primary);
    --brand-700: var(--primary-hover);
    --brand-800: var(--primary-press);

    /* Slate scale */
    --gray-0: #ffffff;
    --gray-50: #f8fafc;
    --gray-100: #f1f5f9;
    --gray-200: #e2e8f0;
    --gray-300: #cbd5e1;
    --gray-400: #94a3b8;
    --gray-500: #64748b;
    --gray-600: #475569;
    --gray-700: #334155;
    --gray-800: #1e293b;
    --gray-900: #0f172a;

    /* Semantic */
    --success: #16a34a;
    --success-50: #f0fdf4;
    --success-200: #bbf7d0;
    --success-700: #15803d;
    --warning: #d97706;
    --danger: #dc2626;
    --danger-hover: #b91c1c;
    --info: var(--primary);

    /* Surfaces and text */
    --bg-page: #f6f8ff;
    --bg-page-art:
        radial-gradient(1100px 520px at 88% -8%, #e4ebff 0%, rgba(228, 235, 255, 0) 60%),
        radial-gradient(900px 520px at -8% 6%, #efeaff 0%, rgba(239, 234, 255, 0) 58%);
    --bg-surface: #ffffff;
    --bg-tint: #f5f8ff;
    --bg-subtle: var(--gray-100);
    --border: var(--gray-200);
    --border-strong: var(--gray-300);
    --text: var(--gray-900);
    --text-secondary: var(--gray-600);
    --text-muted: var(--gray-500);

    /* Spacing */
    --space-1: 4px;
    --space-2: 8px;
    --space-3: 12px;
    --space-4: 16px;
    --space-5: 24px;
    --space-6: 32px;
    --space-7: 48px;
    --gutter: clamp(16px, 3.5vw, 64px);
    --card-pad: clamp(16px, 1.6vw, 24px);

    /* Radii */
    --radius-xs: 4px;
    --radius-sm: 10px;    /* controls */
    --radius-md: 14px;    /* inner panels, dialogs */
    --radius-lg: 16px;    /* video */
    --radius-xl: 22px;    /* cards */
    --radius-full: 999px;

    /* Type */
    --font: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
    --text-xs: 0.75rem;
    --text-sm: 0.875rem;
    --text-base: 1rem;
    --text-lg: 1.125rem;
    --text-xl: 1.375rem;
    --text-hero: clamp(1.5rem, 0.9rem + 1.5vw, 2.125rem);
    --text-result: clamp(2.5rem, 1.4rem + 3.6vw, 5rem);

    /* Shadows */
    --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.05), 0 1px 3px rgba(15, 23, 42, 0.05);
    --shadow:
        0 1px 2px rgba(15, 23, 42, 0.04),
        0 8px 24px -8px rgba(15, 23, 42, 0.08),
        0 24px 48px -16px rgba(37, 99, 235, 0.10);
    --shadow-glow: 0 8px 20px -6px rgba(37, 99, 235, 0.55);

    /* Motion */
    --ease: cubic-bezier(0.2, 0, 0, 1);
    --dur: 160ms;

    /* Sizing */
    --control-h: 48px;
    --focus-ring: 0 0 0 2px var(--bg-surface), 0 0 0 4px var(--primary);
}


/* ==========================================================================
   2. Base
   ========================================================================== */

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

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

body {
    font-family: var(--font);
    font-size: var(--text-base);
    line-height: 1.5;
    color: var(--text);
    background: var(--bg-page-art), var(--bg-page);
    background-attachment: fixed;
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow-x: hidden;
    -webkit-font-smoothing: antialiased;
}

img,
svg,
video,
canvas {
    display: block;
    max-width: 100%;
}

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

:focus-visible {
    outline: none;
    box-shadow: var(--focus-ring);
}

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

.icon {
    width: 18px;
    height: 18px;
    flex: none;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.icon--lg {
    width: 36px;
    height: 36px;
    stroke-width: 1.5;
}


/* ==========================================================================
   3. Layout
   ========================================================================== */

/* Header */
.header {
    position: relative;
    z-index: 5;
    background: var(--bg-surface);
    box-shadow: 0 1px 0 rgba(15, 23, 42, 0.05), 0 6px 20px -12px rgba(15, 23, 42, 0.18);
}

.topbar {
    display: flex;
    align-items: center;
    gap: var(--space-4);
    max-width: 1520px;
    margin: 0 auto;
    padding: var(--space-3) var(--gutter);
    min-height: 72px;
}

.brand {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    text-decoration: none;
    color: var(--primary-press);
    border-radius: var(--radius-sm);
}

.nav-logo {
    width: 36px;
    height: 36px;
}

.nav-brand {
    font-size: var(--text-xl);
    font-weight: 800;
    letter-spacing: -0.02em;
}

.topbar-divider {
    width: 1px;
    height: 24px;
    background: var(--border);
}

.page-title {
    font-size: var(--text-base);
    font-weight: 500;
    color: var(--text-muted);
}

.topbar-btn {
    margin-left: auto;
    min-height: 44px;
}

/* Page */
.main-content {
    flex: 1;
    width: 100%;
    max-width: 1520px;
    margin: 0 auto;
    padding: clamp(16px, 2vw, 28px) var(--gutter) clamp(24px, 3vw, 48px);
}

/* Hero */
.hero {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-4);
    margin-bottom: clamp(14px, 1.6vw, 22px);
}

.hero-title {
    font-size: var(--text-hero);
    font-weight: 800;
    line-height: 1.12;
    letter-spacing: -0.03em;
    color: var(--text);
}

.hero-title .accent {
    color: var(--primary);
}

.hero-sub {
    margin-top: var(--space-1);
    font-size: var(--text-sm);
    color: var(--text-muted);
}

.promo-card {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    max-width: 340px;
    padding: var(--space-3) var(--space-4);
    background: rgba(255, 255, 255, 0.85);
    border: 1px solid rgba(255, 255, 255, 0.9);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow);
    font-size: var(--text-xs);
    color: var(--text-secondary);
}

.promo-art {
    display: grid;
    place-items: center;
    width: 40px;
    height: 40px;
    flex: none;
    border-radius: var(--radius-sm);
    background: var(--primary-50);
    color: var(--primary);
}

.promo-art svg {
    width: 22px;
    height: 22px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.75;
    stroke-linecap: round;
    stroke-linejoin: round;
}

/* Two-column workspace */
.workspace {
    display: grid;
    /* Message card slightly wider than the camera card */
    grid-template-columns: minmax(0, 1fr) minmax(0, 1.12fr);
    gap: clamp(16px, 2vw, 28px);
    /* Both cards share the row height (set by the camera card) */
    align-items: stretch;
}

.card {
    min-width: 0;
    padding: var(--card-pad);
    background: var(--bg-surface);
    border: 1px solid rgba(226, 232, 240, 0.7);
    border-radius: var(--radius-xl);
    box-shadow: var(--shadow);
}

.camera-section {
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
}

.message-section {
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
}


/* ==========================================================================
   4. Components
   ========================================================================== */

/* --- Status row ----------------------------------------------------------- */
.status-indicator {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-3);
}

.status-dot {
    width: 14px;
    height: 14px;
    flex: none;
    border-radius: 50%;
    background: var(--gray-400);
    box-shadow: 0 0 0 4px rgba(148, 163, 184, 0.18);
    transition: background-color var(--dur) var(--ease);
}

.status-lines {
    display: flex;
    flex-direction: column;
    flex: 1 1 180px;
    min-width: 0;
}

.status-text {
    font-size: var(--text-base);
    font-weight: 700;
    color: var(--text);
    overflow-wrap: anywhere;
}

.status-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 0 6px;
    font-size: var(--text-xs);
    font-weight: 500;
    color: var(--text-muted);
    letter-spacing: 0.01em;
}

.model-status,
.fps-counter {
    font-variant-numeric: tabular-nums;
}

/* Camera selector */
.camera-select {
    position: relative;
    display: flex;
    align-items: center;
    flex: 0 1 240px;
    min-width: 0;
}

.camera-select .icon {
    position: absolute;
    left: var(--space-3);
    width: 16px;
    height: 16px;
    color: var(--text-secondary);
    pointer-events: none;
}

.camera-select select {
    width: 100%;
    min-height: 44px;
    padding: 0 36px 0 38px;
    font-size: var(--text-sm);
    font-weight: 500;
    color: var(--text);
    background: var(--bg-surface) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23475569' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E") right 12px center / 16px no-repeat;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-sm);
    appearance: none;
    text-overflow: ellipsis;
    cursor: pointer;
    transition: border-color var(--dur) var(--ease);
}

.camera-select select:hover:not(:disabled) {
    border-color: var(--border-strong);
}

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

/* Phase progress track (created by interpreter.js with inline styles, no class) */
.status-indicator > div:not([class]) {
    flex: 1 0 100%;
    height: 4px !important;
    margin-top: 0 !important;
    background: var(--bg-subtle) !important;
    border-radius: var(--radius-full) !important;
}

/* --- Mode selector (segmented control) ------------------------------------ */
.mode-buttons {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: var(--space-1);
    padding: var(--space-1);
    background: var(--bg-subtle);
    border-radius: var(--radius-md);
}

.mode-btn {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-width: 0;
    min-height: 56px;
    padding: var(--space-2) var(--space-4);
    background: transparent;
    border: 0;
    border-radius: var(--radius-sm);
    color: var(--text);
    text-align: left;
    cursor: pointer;
    transition: background-color var(--dur) var(--ease),
                color var(--dur) var(--ease),
                box-shadow var(--dur) var(--ease);
}

.mode-btn:hover:not(.active) {
    background: rgba(255, 255, 255, 0.7);
}

.mode-btn:active:not(.active) {
    background: var(--bg-surface);
}

.mode-glyph {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    flex: none;
    font-size: var(--text-base);
    font-weight: 600;
    line-height: 1;
    color: var(--text-secondary);
    letter-spacing: -0.02em;
}

.mode-glyph sub {
    font-size: 0.7em;
    vertical-align: -0.35em;
}

.mode-text {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.mode-name {
    font-size: var(--text-sm);
    font-weight: 700;
    line-height: 1.3;
}

.mode-sub {
    font-size: var(--text-xs);
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.mode-btn.active {
    background: var(--primary);
    color: var(--gray-0);
    box-shadow: var(--shadow-glow);
}

.mode-btn.active .mode-glyph,
.mode-btn.active .mode-sub {
    color: rgba(255, 255, 255, 0.85);
}

.mode-btn:focus-visible {
    box-shadow: var(--focus-ring);
}

/* --- Camera --------------------------------------------------------------- */
.camera-container {
    position: relative;
    width: 100%;
    aspect-ratio: 16 / 9;
    background: #0b1220;
    border-radius: var(--radius-lg);
    overflow: hidden;
    isolation: isolate;
}

.camera-placeholder {
    position: absolute;
    inset: 0;
    z-index: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    color: var(--gray-400);
    font-size: var(--text-sm);
    font-weight: 500;
}

/* Plain mirrored video, filling the 16:9 frame */
#videoElement {
    position: absolute;
    inset: 0;
    z-index: 1;
    width: 100%;
    height: 100%;
    object-fit: cover;
    background: #0b1220;
}

/* Always empty — kept only because interpreter.js references it */
#canvasElement {
    position: absolute;
    inset: 0;
    z-index: 2;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

.tracking-overlay {
    transform: scaleX(-1);
}

/* Overlays inside the video */
.camera-pill {
    position: absolute;
    top: var(--space-3);
    left: var(--space-3);
    z-index: 3;
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    background: rgba(15, 23, 42, 0.62);
    color: #ecfdf5;
    border-radius: var(--radius-full);
    font-size: var(--text-xs);
    font-weight: 600;
}

.camera-pill[hidden] {
    display: none;
}

.camera-pill-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #4ade80;
}

/* Phase is also spelled out in the pill text */
.camera-pill[data-phase="ready"] .camera-pill-dot { background: #fbbf24; }
.camera-pill[data-phase="recording"] .camera-pill-dot { background: #f87171; }
.camera-pill[data-phase="loading"] .camera-pill-dot { background: #93c5fd; }

.camera-tools {
    position: absolute;
    top: var(--space-3);
    right: var(--space-3);
    z-index: 3;
    display: flex;
    gap: var(--space-2);
}

.camera-tool {
    display: grid;
    place-items: center;
    width: 40px;
    height: 40px;
    background: rgba(15, 23, 42, 0.55);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 50%;
    color: var(--gray-0);
    cursor: pointer;
    transition: background-color var(--dur) var(--ease), opacity var(--dur) var(--ease);
}

.camera-tool:hover:not(:disabled) {
    background: rgba(15, 23, 42, 0.8);
}

.camera-tool:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.camera-tip {
    position: absolute;
    left: var(--space-3);
    bottom: var(--space-3);
    z-index: 3;
    display: inline-flex;
    align-items: center;
    gap: var(--space-2);
    max-width: calc(100% - 2 * var(--space-3));
    padding: 6px 12px;
    background: rgba(15, 23, 42, 0.62);
    color: var(--gray-0);
    border-radius: var(--radius-full);
    font-size: var(--text-xs);
    font-weight: 500;
}

.camera-tip .icon {
    width: 14px;
    height: 14px;
    color: #fde68a;
}

/* Fullscreen camera */
.camera-container:fullscreen {
    border-radius: 0;
    aspect-ratio: auto;
}

.camera-container:fullscreen #videoElement {
    object-fit: contain;
}

/* One-hand warning (created by interpreter.js inside .camera-container) */
#warning-toast {
    position: absolute;
    top: var(--space-3);
    left: 50%;
    transform: translateX(-50%);
    z-index: 4;
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    background: var(--danger);
    color: var(--gray-0);
    border-radius: var(--radius-full);
    font-size: var(--text-sm);
    font-weight: 600;
    white-space: nowrap;
    pointer-events: none;
    animation: fadeIn var(--dur) var(--ease);
}

/* Replace the emoji the script injects with a line icon */
#warning-toast span {
    display: none;
}

#warning-toast::before {
    content: "";
    width: 16px;
    height: 16px;
    flex: none;
    background: currentColor;
    -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3'/%3E%3Cpath d='M12 9v4'/%3E%3Cpath d='M12 17h.01'/%3E%3C/svg%3E") center / contain no-repeat;
            mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m21.73 18-8-14a2 2 0 0 0-3.48 0l-8 14A2 2 0 0 0 4 21h16a2 2 0 0 0 1.73-3'/%3E%3Cpath d='M12 9v4'/%3E%3Cpath d='M12 17h.01'/%3E%3C/svg%3E") center / contain no-repeat;
}

/* --- Buttons -------------------------------------------------------------- */
.control-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    min-height: var(--control-h);
    padding: 0 var(--space-5);
    border: 1px solid transparent;
    border-radius: var(--radius-sm);
    font-size: var(--text-sm);
    font-weight: 600;
    line-height: 1.2;
    white-space: nowrap;
    cursor: pointer;
    transition: background-color var(--dur) var(--ease),
                border-color var(--dur) var(--ease),
                color var(--dur) var(--ease),
                box-shadow var(--dur) var(--ease),
                transform var(--dur) var(--ease),
                opacity var(--dur) var(--ease);
}

.control-btn:active:not(:disabled) {
    transform: scale(0.98);
}

.control-btn:disabled {
    cursor: not-allowed;
    opacity: 0.45;
    box-shadow: none;
}

/* Primary */
.control-btn--primary {
    background: var(--primary);
    color: var(--gray-0);
    box-shadow: 0 6px 16px -6px rgba(37, 99, 235, 0.5);
}

.control-btn--primary:hover:not(:disabled) {
    background: var(--primary-hover);
    box-shadow: var(--shadow-glow);
}

.control-btn--primary:active:not(:disabled) {
    background: var(--primary-press);
}

/* Destructive */
.control-btn--danger,
.control-btn.stop {
    background: var(--danger);
    color: var(--gray-0);
    box-shadow: 0 6px 16px -6px rgba(220, 38, 38, 0.45);
}

.control-btn--danger:hover:not(:disabled),
.control-btn.stop:hover:not(:disabled) {
    background: var(--danger-hover);
}

/* Secondary (outlined) */
.control-btn--secondary {
    background: var(--bg-surface);
    border-color: var(--border);
    color: var(--text);
    box-shadow: var(--shadow-sm);
}

.control-btn--secondary:hover:not(:disabled) {
    background: var(--gray-50);
    border-color: var(--border-strong);
}

.control-btn--secondary:active:not(:disabled) {
    background: var(--bg-subtle);
}

.control-btn:focus-visible,
.control-btn--primary:focus-visible,
.control-btn--secondary:focus-visible {
    box-shadow: var(--focus-ring);
}

/* --- Camera controls ------------------------------------------------------ */
.camera-controls {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
}

/* Compact enough that all four fit on one row down to ~1280px wide windows */
.camera-controls .control-btn {
    flex: 1 1 88px;
    padding: 0 14px;
}

.camera-controls #startBtn,
.camera-controls #stopBtn {
    flex: 1.6 1 140px;
}

/* --- Message card --------------------------------------------------------- */
.message-header {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-3);
}

.message-icon {
    display: grid;
    place-items: center;
    width: 44px;
    height: 44px;
    flex: none;
    background: var(--primary);
    color: var(--gray-0);
    border-radius: var(--radius-sm);
    box-shadow: 0 6px 14px -6px rgba(37, 99, 235, 0.6);
}

.message-heading {
    flex: 1 1 160px;
    min-width: 0;
}

.message-title {
    font-size: var(--text-lg);
    font-weight: 700;
    line-height: 1.3;
    color: var(--text);
}

.message-mode {
    font-size: var(--text-xs);
    font-weight: 500;
    color: var(--text-muted);
    letter-spacing: 0.01em;
}

#speakBtn {
    flex: none;
    min-height: 44px;
    padding: 0 var(--space-4);
}

/* Detected text display */
.message-display {
    position: relative;
    display: flex;
    flex-direction: column;
    /* Takes whatever height is left in the card; its content never grows the card */
    flex: 1 1 0;
    min-height: 140px;
    padding: var(--space-5) var(--space-5) var(--space-4);
    background: var(--bg-tint);
    border: 1px solid #e8eeff;
    border-radius: var(--radius-lg);
    overflow-y: auto;
    overscroll-behavior: contain;
}

.message-content {
    /* auto margins centre vertically without clipping when content overflows */
    margin-block: auto;
    width: 100%;
    min-width: 0;
    text-align: center;
}

.detected-text {
    display: none; /* shown by interpreter.js when the camera starts */
    /* --result-size is set by fitDetectedText() in interpreter.js as the text grows */
    font-size: var(--result-size, var(--text-result));
    font-weight: 800;
    line-height: 1.1;
    letter-spacing: -0.02em;
    color: var(--text);
    /* Words don't break, so a long word shrinks the font instead */
    overflow-wrap: normal;
    white-space: pre-wrap;
    animation: fadeIn 0.4s var(--ease);
}

/* Already at the smallest size: allow breaking and let the display scroll */
.message-display.is-min-size .detected-text {
    overflow-wrap: anywhere;
}

.confidence-score {
    display: none; /* shown by interpreter.js when the camera starts */
    margin-top: var(--space-2);
    font-size: var(--text-sm);
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}

.placeholder-text {
    max-width: 38ch;
    margin: 0 auto;
    font-size: var(--text-base);
    line-height: 1.6;
    color: var(--text-muted);
}

.message-actions {
    display: flex;
    justify-content: center;
    margin-top: var(--space-4);
}

.round-btn {
    display: grid;
    place-items: center;
    width: 52px;
    height: 52px;
    background: var(--primary);
    color: var(--gray-0);
    border: 0;
    border-radius: 50%;
    box-shadow: var(--shadow-glow);
    cursor: pointer;
    transition: background-color var(--dur) var(--ease), opacity var(--dur) var(--ease), transform var(--dur) var(--ease);
}

.round-btn .icon {
    width: 22px;
    height: 22px;
}

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

.round-btn:active:not(:disabled) {
    transform: scale(0.95);
}

.round-btn:disabled {
    opacity: 0.4;
    box-shadow: none;
    cursor: not-allowed;
}

/* Copy buttons */
.copy-btn {
    position: relative;
    display: grid;
    place-items: center;
    width: 40px;
    height: 40px;
    flex: none;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    box-shadow: var(--shadow-sm);
    cursor: pointer;
    transition: background-color var(--dur) var(--ease), color var(--dur) var(--ease), opacity var(--dur) var(--ease);
}

.copy-btn:hover:not(:disabled) {
    background: var(--gray-50);
    color: var(--text);
}

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

/* Clear + copy, bottom-right of the display (level with the speaker button,
   so they never sit on top of the text) */
.display-tools {
    position: absolute;
    right: var(--space-4);
    bottom: var(--space-4);
    display: flex;
    gap: var(--space-2);
}

.clear-message-btn:hover:not(:disabled) {
    background: #fef2f2;
    border-color: #fecaca;
    color: var(--danger);
}

.copy-feedback {
    position: absolute;
    bottom: calc(100% + 6px);
    left: 50%;
    transform: translateX(-50%);
    padding: 3px 8px;
    background: var(--gray-900);
    color: var(--gray-0);
    border-radius: var(--radius-xs);
    font-size: var(--text-xs);
    font-weight: 600;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--dur) var(--ease);
}

.copy-btn.is-copied .copy-feedback {
    opacity: 1;
}

/* Translation section below the display */
.block-title {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    font-size: var(--text-base);
    font-weight: 700;
    color: var(--text);
}

.message-block > .block-title {
    margin-bottom: var(--space-3);
}

.block-title .icon {
    width: 20px;
    height: 20px;
    color: var(--primary);
}

/* Translation */
.translation-card {
    display: flex;
    align-items: flex-start;
    gap: var(--space-3);
    padding: var(--space-4) var(--space-5);
    background: var(--success-50);
    border: 1px solid var(--success-200);
    border-radius: var(--radius-md);
}

.translation-text {
    flex: 1;
    min-width: 0;
    font-size: var(--text-lg);
    font-weight: 700;
    line-height: 1.4;
    color: var(--success-700);
    overflow-wrap: anywhere;
    /* Long translations scroll inside the card (about 3 lines) */
    max-height: calc(1.4em * 3);
    overflow-y: auto;
    overscroll-behavior: contain;
}

.translation-text.is-empty {
    font-size: var(--text-sm);
    font-weight: 500;
    color: var(--text-muted);
}

@keyframes fadeIn {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* --- Sign dictionary ------------------------------------------------------ */
.dict-dialog {
    width: min(640px, calc(100% - 2 * var(--space-4)));
    max-width: none;
    max-height: calc(100dvh - 2 * var(--space-4));
    margin: auto;
    padding: 0;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--bg-surface);
    color: var(--text);
    box-shadow: var(--shadow);
    overflow: hidden;
}

.dict-dialog::backdrop {
    background: rgba(17, 24, 39, 0.45);
}

.dict-panel {
    display: flex;
    flex-direction: column;
    max-height: calc(100dvh - 2 * var(--space-4));
}

.dict-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: var(--space-3);
    padding: var(--space-5) var(--space-5) var(--space-3);
}

.dict-title {
    font-size: var(--text-lg);
    font-weight: 600;
    line-height: 1.3;
}

.dict-count {
    font-size: var(--text-xs);
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}

.dict-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    margin: calc(-1 * var(--space-2)) calc(-1 * var(--space-2)) 0 0;
    flex: none;
    background: transparent;
    border: 0;
    border-radius: var(--radius-sm);
    color: var(--text-secondary);
    cursor: pointer;
    transition: background-color var(--dur) var(--ease), color var(--dur) var(--ease);
}

.dict-close:hover {
    background: var(--bg-subtle);
    color: var(--text);
}

.dict-search {
    position: relative;
    margin: 0 var(--space-5) var(--space-3);
}

.dict-search .icon {
    position: absolute;
    left: var(--space-3);
    top: 50%;
    transform: translateY(-50%);
    width: 16px;
    height: 16px;
    color: var(--text-muted);
    pointer-events: none;
}

.dict-search input {
    width: 100%;
    min-height: var(--control-h);
    padding: 0 var(--space-3) 0 36px;
    font: inherit;
    font-size: var(--text-sm);
    color: var(--text);
    background: var(--bg-surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    transition: border-color var(--dur) var(--ease);
}

.dict-search input::placeholder {
    color: var(--text-muted);
}

.dict-search input:hover {
    border-color: var(--gray-400);
}

.dict-search input:focus-visible {
    border-color: var(--brand-600);
}

.dict-body {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 0 var(--space-5) var(--space-5);
    border-top: 1px solid var(--border);
}

.dict-group {
    padding-top: var(--space-5);
}

.dict-group + .dict-group {
    margin-top: var(--space-5);
    border-top: 1px solid var(--border);
}

.dict-group-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: var(--space-3);
    margin-bottom: var(--space-3);
}

.dict-group-title {
    font-size: var(--text-base);
    font-weight: 600;
}

.dict-group-note {
    font-size: var(--text-xs);
    color: var(--text-muted);
}

.dict-use-btn {
    flex: none;
    min-height: 32px;
    padding: 0 var(--space-3);
    font: inherit;
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--brand-700);
    background: transparent;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background-color var(--dur) var(--ease), border-color var(--dur) var(--ease);
}

.dict-use-btn:hover:not(:disabled) {
    background: var(--brand-50);
    border-color: var(--brand-600);
}

.dict-use-btn:disabled {
    color: var(--text-muted);
    border-color: var(--border);
    cursor: default;
}

.dict-section + .dict-section {
    margin-top: var(--space-4);
}

.dict-section-title {
    margin-bottom: var(--space-1);
    font-size: var(--text-xs);
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-muted);
}

/* Letters and numbers: compact tiles */
.dict-tiles {
    list-style: none;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(40px, 1fr));
    gap: var(--space-2);
}

.dict-tile {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 40px;
    font-size: var(--text-base);
    font-weight: 600;
    color: var(--text);
    background: var(--gray-50);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
}

/* Words and phrases: two-column rows */
.dict-list {
    list-style: none;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(min(100%, 260px), 1fr));
    column-gap: var(--space-5);
}

.dict-item {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: var(--space-3);
    padding: var(--space-2) 0;
    border-bottom: 1px solid var(--border);
    font-size: var(--text-sm);
}

.dict-sign {
    font-weight: 500;
    color: var(--text);
}

.dict-filipino {
    color: var(--text-secondary);
    text-align: right;
}

.dict-tile[hidden],
.dict-item[hidden],
.dict-section[hidden],
.dict-group[hidden],
.dict-empty[hidden] {
    display: none;
}

.dict-empty {
    padding: var(--space-6) 0;
    font-size: var(--text-sm);
    color: var(--text-muted);
    text-align: center;
}

/* --- Loading dialog ------------------------------------------------------- */
.loader-overlay {
    position: fixed;
    inset: 0;
    z-index: 100;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-4);
    background: rgba(17, 24, 39, 0.45);
    overflow-y: auto;
}

.loader-overlay[hidden],
.loader-error[hidden] {
    display: none;
}

.loader-panel {
    width: 100%;
    max-width: 460px;
    max-height: calc(100dvh - 2 * var(--space-4));
    overflow-y: auto;
    padding: var(--space-5);
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow);
}

.loader-head {
    display: flex;
    align-items: center;
    gap: var(--space-3);
}

.loader-logo {
    width: 32px;
    height: 32px;
    flex: none;
}

.loader-heading {
    flex: 1;
    min-width: 0;
}

.loader-title {
    font-size: var(--text-base);
    font-weight: 600;
    line-height: 1.3;
    color: var(--text);
}

.loader-summary {
    min-height: 1.5em;
    font-size: var(--text-sm);
    color: var(--text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.loader-percent {
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--text);
    font-variant-numeric: tabular-nums;
}

.loader-track {
    height: 4px;
    margin: var(--space-4) 0 var(--space-5);
    background: var(--bg-subtle);
    border-radius: var(--radius-xs);
    overflow: hidden;
}

.loader-bar {
    width: 0;
    height: 100%;
    background: var(--brand-600);
    transition: width 180ms var(--ease);
}

.loader-list {
    display: flex;
    flex-direction: column;
    gap: var(--space-4);
}

.loader-group-title {
    margin-bottom: var(--space-1);
    font-size: var(--text-xs);
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--text-muted);
}

.loader-rows {
    list-style: none;
}

.loader-row {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-height: 36px;
    border-bottom: 1px solid var(--border);
}

.loader-rows .loader-row:last-child {
    border-bottom: 0;
}

.loader-label {
    flex: 1;
    min-width: 0;
    font-size: var(--text-sm);
    color: var(--text-muted);
}

.loader-detail {
    flex: none;
    font-size: var(--text-xs);
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

.loader-row[data-state="active"] .loader-label,
.loader-row[data-state="done"] .loader-label {
    color: var(--text);
}

.loader-row[data-state="active"] .loader-label {
    font-weight: 500;
}

.loader-row[data-state="error"] .loader-label,
.loader-row[data-state="error"] .loader-detail {
    color: var(--danger);
}

/* State icons: pending ring, spinner, check, cross */
.loader-icon {
    width: 16px;
    height: 16px;
    flex: none;
    border-radius: 50%;
    border: 1.5px solid var(--border-strong);
}

.loader-row[data-state="active"] .loader-icon {
    border-color: var(--bg-subtle);
    border-top-color: var(--brand-600);
    animation: loaderSpin 0.8s linear infinite;
}

.loader-row[data-state="done"] .loader-icon,
.loader-row[data-state="error"] .loader-icon {
    border: 0;
    border-radius: 0;
    -webkit-mask: var(--icon) center / contain no-repeat;
            mask: var(--icon) center / contain no-repeat;
}

.loader-row[data-state="done"] .loader-icon {
    --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M20 6 9 17l-5-5'/%3E%3C/svg%3E");
    background: var(--success);
}

.loader-row[data-state="error"] .loader-icon {
    --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M18 6 6 18'/%3E%3Cpath d='m6 6 12 12'/%3E%3C/svg%3E");
    background: var(--danger);
}

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

.loader-error {
    margin-top: var(--space-5);
    padding-top: var(--space-4);
    border-top: 1px solid var(--border);
    font-size: var(--text-sm);
    color: var(--text-secondary);
}

.loader-actions {
    display: flex;
    justify-content: flex-end;
    gap: var(--space-2);
    margin-top: var(--space-4);
}

.loader-foot {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2) var(--space-4);
    margin-top: var(--space-5);
}

.loader-note {
    flex: 1 1 220px;
    font-size: var(--text-xs);
    color: var(--text-muted);
}

.loader-link {
    min-height: 32px;
    padding: 0;
    font: inherit;
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--brand-600);
    background: none;
    border: 0;
    border-radius: var(--radius-xs);
    text-decoration: underline;
    text-underline-offset: 3px;
    cursor: pointer;
}

.loader-link:hover {
    color: var(--brand-800);
}




/* ==========================================================================
   5. JS colour mapping
   interpreter.js writes saturated inline colours (e.g. #FFD700, #FF0000,
   #00FF00) to the status dot and progress bar. These rules map them onto the
   palette without touching the script. Each state is also named in the
   status text, so colour is never the only signal.
   ========================================================================== */

.status-dot[style*="(255, 215, 0)"],
.status-indicator > div:not([class]) > div[style*="(255, 215, 0)"] {
    background: var(--warning) !important;
}

.status-dot[style*="(255, 0, 0)"],
.status-dot[style*="(239, 68, 68)"],
.status-indicator > div:not([class]) > div[style*="(255, 0, 0)"],
.status-indicator > div:not([class]) > div[style*="(239, 68, 68)"] {
    background: var(--danger) !important;
}

.status-dot[style*="(0, 255, 0)"],
.status-dot[style*="(22, 163, 74)"],
.status-indicator > div:not([class]) > div[style*="(0, 255, 0)"] {
    background: var(--success) !important;
}

.status-dot[style*="(59, 130, 246)"],
.status-indicator > div:not([class]) > div[style*="(59, 130, 246)"] {
    background: var(--info) !important;
}

.status-dot[style*="(100, 116, 139)"],
.status-indicator > div:not([class]) > div[style*="(51, 51, 51)"] {
    background: var(--gray-400) !important;
}

/* Soft halo matching the dot colour */
.status-dot[style*="(0, 255, 0)"],
.status-dot[style*="(22, 163, 74)"] {
    box-shadow: 0 0 0 4px rgba(22, 163, 74, 0.18);
}

.status-dot[style*="(255, 215, 0)"] {
    box-shadow: 0 0 0 4px rgba(217, 119, 6, 0.18);
}

.status-dot[style*="(255, 0, 0)"],
.status-dot[style*="(239, 68, 68)"] {
    box-shadow: 0 0 0 4px rgba(220, 38, 38, 0.18);
}


/* ==========================================================================
   6. Responsive
   ========================================================================== */

/* Narrow desktop / tablet: stack the cards, camera first */
@media (max-width: 1024px) {
    .workspace {
        grid-template-columns: minmax(0, 1fr);
    }

    /* Stacked: no shared row height, so give the display a fixed height to fit text into */
    .message-display {
        flex: none;
        height: clamp(200px, 30vh, 320px);
    }

    .promo-card {
        max-width: none;
        flex: 1 1 100%;
    }
}

@media (max-width: 640px) {
    .camera-select {
        flex-basis: 100%;
    }

    .mode-btn {
        flex-direction: column;
        justify-content: center;
        gap: 2px;
        padding: var(--space-2);
        text-align: center;
    }

    .mode-sub {
        display: none;
    }

    .camera-tip {
        display: none;
    }

    .message-display {
        padding: var(--space-4);
    }
}

/* Small screens */
@media (max-width: 480px) {
    .topbar {
        gap: var(--space-3);
        min-height: 60px;
    }

    .nav-brand {
        font-size: var(--text-lg);
    }

    .camera-controls #startBtn,
    .camera-controls #stopBtn {
        flex-basis: 100%;
    }

    .camera-controls .control-btn {
        padding: 0 var(--space-3);
    }

    /* Icon-only dictionary button (keeps its aria-label) */
    .topbar-btn {
        width: 44px;
        padding: 0;
    }

    .topbar-btn span {
        display: none;
    }

    #speakBtn {
        flex: 1 1 100%;
    }

    .dict-head,
    .dict-body {
        padding-left: var(--space-4);
        padding-right: var(--space-4);
    }

    .dict-search {
        margin-left: var(--space-4);
        margin-right: var(--space-4);
    }
}


/* ==========================================================================
   7. Motion preferences
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        transition-duration: 0.01ms !important;
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
    }

    /* interpreter.js re-applies an inline fade on every new word */
    .detected-text {
        animation: none !important;
    }

    .control-btn:active:not(:disabled),
    .round-btn:active:not(:disabled) {
        transform: none;
    }

    /* Static partial ring instead of a spinning one */
    .loader-row[data-state="active"] .loader-icon {
        animation: none !important;
        border-right-color: var(--brand-600);
    }
}
