/* ============================================================================
   NeuroCrush — Shared Design System
   Neutral layered surfaces + a scarce accent. Data drives the UI.
   Loaded on every page via bars.html.
   ========================================================================== */

:root {
    /* --- Legacy aliases (kept for every existing game page) --- */
    --bg-body: var(--global-bg-body);
    --bg-surface: var(--global-bg-surface);
    --bg-surface-2: var(--global-bg-surface-2);
    --border: var(--global-border);
    --text-primary: var(--global-text-primary);
    --text-secondary: var(--global-text-secondary);
    --accent: var(--global-accent);
    --accent-glow: var(--global-accent-glow);
    --radius-lg: var(--global-radius-lg);
    --radius-sm: var(--global-radius-sm);
    --success: var(--global-success);
    --error: var(--global-error);

    /* --- Spacing scale (8pt grid) --- */
    --s-1: 4px;
    --s-2: 8px;
    --s-3: 12px;
    --s-4: 16px;
    --s-5: 20px;
    --s-6: 24px;
    --s-8: 32px;
    --s-12: 48px;

    /* --- Radii --- */
    --r-xs: 8px;
    --r-sm: 10px;
    --r-md: 14px;
    --r-lg: 20px;
    --r-xl: 28px;
    --r-pill: 999px;

    /* --- Type scale (dense by default) --- */
    --fs-2xs: 0.68rem;
    --fs-xs: 0.75rem;
    --fs-sm: 0.82rem;
    --fs-md: 0.9rem;
    --fs-lg: 1.05rem;
    --fs-xl: 1.3rem;
    --fs-2xl: 1.65rem;

    /* --- Derived surfaces --- */
    --surface-raised: var(--global-bg-surface-2);
    --stroke: var(--global-border);
    --accent-soft: color-mix(in oklab, var(--accent) 14%, transparent);
    --accent-contrast: #fff;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.18);
    --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.18);
    --ease: cubic-bezier(0.2, 0.8, 0.2, 1);

    --focus-ring: 0 0 0 2px var(--global-bg-body), 0 0 0 4px var(--accent);
}

/* ---------------------------------------------------------------------------
   Accessibility helpers
   ------------------------------------------------------------------------- */
:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

.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;
}

.ic {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

/* ---------------------------------------------------------------------------
   Buttons
   ------------------------------------------------------------------------- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--s-2);
    padding: 12px 20px;
    border-radius: var(--r-pill);
    font-family: var(--global-font-main);
    font-size: var(--fs-md);
    font-weight: 700;
    line-height: 1;
    cursor: pointer;
    border: 1px solid transparent;
    outline: none;
    user-select: none;
    white-space: nowrap;
    transition: transform 0.15s var(--ease), background-color 0.2s ease, border-color 0.2s ease,
        box-shadow 0.2s ease, opacity 0.2s ease;
    text-decoration: none;
}

.btn:active {
    transform: scale(0.97);
}

.btn:disabled,
.btn[aria-disabled="true"] {
    opacity: 0.45;
    cursor: not-allowed;
    transform: none;
}

.btn-primary {
    background: var(--accent);
    color: var(--accent-contrast);
    box-shadow: 0 6px 18px var(--accent-glow);
}

.btn-primary:hover:not(:disabled) {
    transform: translateY(-1px);
    box-shadow: 0 10px 26px var(--accent-glow);
}

.btn-ghost {
    background: transparent;
    color: var(--text-primary);
    border-color: var(--border);
}

.btn-ghost:hover:not(:disabled) {
    background: var(--bg-surface-2);
    border-color: var(--accent);
}

.btn-subtle {
    background: var(--bg-surface-2);
    color: var(--text-primary);
    border-color: transparent;
}

.btn-subtle:hover:not(:disabled) {
    border-color: var(--accent);
}

.btn-danger {
    background: transparent;
    color: var(--error);
    border-color: var(--error);
}

.btn-danger:hover:not(:disabled) {
    background: var(--error);
    color: #fff;
}

.btn-sm {
    padding: 8px 14px;
    font-size: var(--fs-sm);
}

.btn-block {
    width: 100%;
}

.btn-icon {
    width: 44px;
    height: 44px;
    padding: 0;
    border-radius: 50%;
    background: var(--bg-surface-glass, var(--bg-surface));
    backdrop-filter: blur(var(--glass-blur, 14px));
    border: 1px solid var(--glass-border, var(--border));
    color: var(--text-primary);
    font-size: 1.2rem;
}

.btn-icon:hover:not(:disabled) {
    background: var(--bg-surface-2);
    transform: translateY(-1px);
}

/* ---------------------------------------------------------------------------
   Surfaces
   ------------------------------------------------------------------------- */
.card {
    background: var(--bg-surface-glass, var(--bg-surface));
    backdrop-filter: blur(var(--glass-blur, 14px));
    border: 1px solid var(--glass-border, var(--border));
    border-radius: var(--radius-lg);
    padding: var(--s-5);
    box-shadow: var(--glass-shadow, 0 8px 32px rgba(0, 0, 0, 0.15));
    transition: background-color 0.4s ease, border-color 0.3s ease;
}

/* Tighter panel used inside dashboards/hubs */
.panel {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--r-lg);
    padding: var(--s-5);
}

.panel-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--s-3);
    margin-bottom: var(--s-4);
}

/* ---------------------------------------------------------------------------
   Typography helpers
   ------------------------------------------------------------------------- */
.eyebrow {
    font-size: var(--fs-2xs);
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.14em;
    color: var(--accent);
}

.muted {
    color: var(--text-secondary);
}

.num {
    font-variant-numeric: tabular-nums;
    text-align: right;
}

/* ---------------------------------------------------------------------------
   Chips / badges / status
   ------------------------------------------------------------------------- */
.chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 10px;
    border-radius: var(--r-pill);
    background: var(--bg-surface-2);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    font-size: var(--fs-xs);
    font-weight: 600;
    line-height: 1.4;
    white-space: nowrap;
}

.chip-accent {
    color: var(--accent);
    border-color: color-mix(in oklab, var(--accent) 40%, transparent);
    background: var(--accent-soft);
}

.badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 3px 9px;
    border-radius: var(--r-pill);
    font-size: var(--fs-2xs);
    font-weight: 700;
    letter-spacing: 0.02em;
}

.badge-success {
    background: color-mix(in oklab, var(--success) 16%, transparent);
    color: var(--success);
}

.badge-error {
    background: color-mix(in oklab, var(--error) 16%, transparent);
    color: var(--error);
}

/* ---------------------------------------------------------------------------
   Segmented control / tabs (progressive disclosure)
   ------------------------------------------------------------------------- */
.segmented {
    display: inline-flex;
    gap: 2px;
    padding: 3px;
    border-radius: var(--r-pill);
    background: var(--bg-surface-2);
    border: 1px solid var(--border);
}

.segmented button {
    appearance: none;
    border: 0;
    background: transparent;
    color: var(--text-secondary);
    font-family: inherit;
    font-size: var(--fs-sm);
    font-weight: 700;
    padding: 8px 16px;
    border-radius: var(--r-pill);
    cursor: pointer;
    transition: color 0.2s ease, background-color 0.2s ease;
}

.segmented button:hover {
    color: var(--text-primary);
}

.segmented button[aria-selected="true"] {
    background: var(--bg-surface);
    color: var(--text-primary);
    box-shadow: var(--shadow-sm);
}

.tabs {
    display: flex;
    gap: var(--s-4);
    border-bottom: 1px solid var(--border);
}

.tab {
    appearance: none;
    background: none;
    border: 0;
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
    padding: 10px 2px;
    color: var(--text-secondary);
    font-family: inherit;
    font-size: var(--fs-md);
    font-weight: 700;
    cursor: pointer;
    transition: color 0.2s ease, border-color 0.2s ease;
}

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

.tab[aria-selected="true"] {
    color: var(--text-primary);
    border-bottom-color: var(--accent);
}

/* ---------------------------------------------------------------------------
   Progress
   ------------------------------------------------------------------------- */
.progress {
    height: 6px;
    border-radius: var(--r-pill);
    background: var(--bg-surface-2);
    overflow: hidden;
}

.progress > span {
    display: block;
    height: 100%;
    border-radius: inherit;
    background: var(--accent);
    transition: width 0.6s var(--ease);
}

/* ---------------------------------------------------------------------------
   Inputs
   ------------------------------------------------------------------------- */
.game-input {
    width: 100%;
    padding: 12px 16px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    background: var(--bg-surface-2);
    color: var(--text-primary);
    font-family: var(--global-font-main);
    font-size: var(--fs-md);
    outline: none;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.game-input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

.game-label {
    display: block;
    font-size: var(--fs-xs);
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-secondary);
    margin-bottom: 6px;
}

/* ---------------------------------------------------------------------------
   Stats
   ------------------------------------------------------------------------- */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: var(--s-3);
    padding: var(--s-4) 0;
}

.stat-box {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: var(--s-4);
    text-align: center;
}

.stat-val {
    font-size: 1.7rem;
    font-weight: 800;
    color: var(--text-primary);
    line-height: 1.2;
    font-variant-numeric: tabular-nums;
}

.stat-lbl {
    font-size: var(--fs-2xs);
    font-weight: 600;
    color: var(--text-secondary);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin-top: 4px;
}

/* ---------------------------------------------------------------------------
   Tooltip (invisible until needed)
   ------------------------------------------------------------------------- */
.tip {
    position: relative;
}

.tip::after {
    content: attr(data-tip);
    position: absolute;
    left: 50%;
    bottom: calc(100% + 8px);
    transform: translateX(-50%) translateY(4px);
    background: var(--bg-surface-2);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: var(--r-xs);
    padding: 5px 9px;
    font-size: var(--fs-2xs);
    font-weight: 600;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.15s ease, transform 0.15s ease;
    box-shadow: var(--shadow-md);
    z-index: 60;
}

.tip:hover::after,
.tip:focus-visible::after {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* ---------------------------------------------------------------------------
   Empty / loading states
   ------------------------------------------------------------------------- */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    gap: var(--s-2);
    padding: var(--s-8) var(--s-4);
    color: var(--text-secondary);
    border: 1px dashed var(--border);
    border-radius: var(--r-lg);
}

.empty-state strong {
    color: var(--text-primary);
    font-size: var(--fs-lg);
}

.empty-state .empty-icon {
    font-size: 1.8rem;
    opacity: 0.8;
}

.skeleton {
    position: relative;
    overflow: hidden;
    background: var(--bg-surface-2);
    border-radius: var(--r-sm);
}

.skeleton::after {
    content: "";
    position: absolute;
    inset: 0;
    transform: translateX(-100%);
    background: linear-gradient(90deg, transparent, color-mix(in oklab, var(--text-primary) 8%, transparent), transparent);
    animation: skeleton-sweep 1.4s ease-in-out infinite;
}

@keyframes skeleton-sweep {
    100% {
        transform: translateX(100%);
    }
}

/* ---------------------------------------------------------------------------
   Layout primitives
   ------------------------------------------------------------------------- */
.screen {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    opacity: 0;
    pointer-events: none;
    z-index: 1;
    transition: opacity 0.25s ease, transform 0.25s ease;
    transform: scale(0.99);
}

.screen.active {
    opacity: 1;
    pointer-events: auto;
    z-index: 10;
    transform: scale(1);
}

.content-scroll {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: var(--s-4);
    padding-bottom: 140px;
    -webkit-overflow-scrolling: touch;
}

.bottom-dock {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    padding: var(--s-3) var(--s-4);
    background: var(--bg-body);
    border-top: 1px solid var(--border);
    backdrop-filter: blur(12px);
    box-shadow: 0 -8px 26px rgba(0, 0, 0, 0.12);
    z-index: 50;
    display: flex;
    gap: var(--s-2);
    justify-content: center;
    align-items: center;
}

@media (min-width: 769px) {
    .bottom-dock {
        border-radius: var(--r-lg) var(--r-lg) 0 0;
        width: 90%;
        left: 50%;
        transform: translateX(-50%);
    }
}

/* ---------------------------------------------------------------------------
   Modal
   ------------------------------------------------------------------------- */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
}

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

.modal-card {
    background: var(--bg-surface-glass, var(--bg-surface));
    backdrop-filter: blur(var(--glass-blur, 20px));
    border: 1px solid var(--glass-border, var(--border));
    border-radius: var(--radius-lg);
    padding: var(--s-8) var(--s-6);
    max-width: 400px;
    width: 90%;
    box-shadow: var(--glass-shadow, 0 20px 60px rgba(0, 0, 0, 0.3));
    text-align: center;
    transform: translateY(16px);
    transition: transform 0.25s var(--ease);
}

.modal-overlay.open .modal-card {
    transform: translateY(0);
}

.modal-title {
    font-size: var(--fs-xl);
    font-weight: 800;
    color: var(--text-primary);
    margin-bottom: var(--s-3);
}

.modal-txt {
    font-size: var(--fs-md);
    color: var(--text-secondary);
    margin-bottom: var(--s-5);
    line-height: 1.6;
}

.hidden {
    display: none !important;
}

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

    *,
    *::before,
    *::after {
        animation-duration: 0.001ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.001ms !important;
        scroll-behavior: auto !important;
    }
}

/* ---------------------------------------------------------------------------
   Help icon tooltip ("?" in settings labels)
   Canonical component, extracted from nback.html — the copy-paste drift
   siblings in math.html / kakuro.html (.k-help-icon) / sudoku.html
   (.s-help-icon) are retired separately. Tooltip text comes from
   [data-title]; tap toggles .tooltip-active; pages may nudge the bubble
   horizontally by setting --tx on the icon (edge-collision JS).
   ------------------------------------------------------------------------- */
.help-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--bg-surface-2);
    color: var(--text-secondary);
    font-size: 0.8rem;
    margin-left: var(--s-2);
    cursor: pointer;
    border: 1px solid var(--border);
    position: relative;
}

.help-icon:hover,
.help-icon:active,
.help-icon.tooltip-active {
    color: var(--text-primary);
    border-color: var(--text-primary);
}

.help-icon::after {
    content: attr(data-title);
    position: absolute;
    bottom: 150%;
    left: 50%;
    transform: translateX(calc(-50% + var(--tx, 0px)));
    background: var(--bg-surface);
    color: var(--text-primary);
    padding: var(--s-3) var(--s-4);
    border-radius: var(--r-xs);
    border: 1px solid var(--border);
    box-shadow: 0 8px 24px oklch(0.00% 0.000 0.0 / 0.4);
    font-size: 0.8rem;
    line-height: 1.5;
    white-space: normal;
    width: 220px;
    text-align: left;
    font-weight: normal;
    pointer-events: none;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s, visibility 0.2s, bottom 0.2s;
    z-index: 9999;
}

.help-icon::before {
    content: '';
    position: absolute;
    bottom: calc(150% - 7px);
    left: 50%;
    transform: translateX(-50%) rotate(45deg);
    width: 14px;
    height: 14px;
    background: var(--bg-surface);
    border-right: 1px solid var(--border);
    border-bottom: 1px solid var(--border);
    pointer-events: none;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s, visibility 0.2s, bottom 0.2s;
    z-index: 10000;
}

.help-icon:hover::after,
.help-icon.tooltip-active::after {
    opacity: 1;
    visibility: visible;
    bottom: 120%;
}

.help-icon:hover::before,
.help-icon.tooltip-active::before {
    opacity: 1;
    visibility: visible;
    bottom: calc(120% - 7px);
}
