/* ===== Sanctum Design Tokens =====
 * Theme layer on top of base.css.
 * Load AFTER base.css so overrides win.
 *
 * Themes:   data-theme="grimoire" (default, dark) | "parchment" (light)
 * Density:  data-density="cozy" (default) | "compact" | "comfortable"
 *
 * Palettes inspired by an external "Parchment & Ink / Ancient Grimoire" study,
 * re-voiced to match Sanctum's existing accent + gold DNA.
 */

/* --- Ancient Grimoire (default dark) --- */
:root,
[data-theme='grimoire'] {
    /* Keep existing Sanctum dark DNA; add alias layer so components can opt-in */
    --surface-app: #0f0a0a;
    --surface-card: #1a1215;
    --surface-card-hover: #23181c;
    --surface-popover: #1a1215;
    --surface-muted: #2a1f1f;

    --ink-primary: #e8dcc8;
    --ink-secondary: #a89075;
    --ink-dim: rgba(232, 220, 200, 0.55);

    --brand-primary: var(--gold, #d4a574);
    --brand-secondary: #4a6fa5;
    --brand-accent: #8b2635;
    --brand-accent-soft: rgba(139, 38, 53, 0.18);

    --line: rgba(212, 165, 116, 0.2);
    --line-strong: rgba(212, 165, 116, 0.32);

    /* HP zones — shared semantic tokens */
    --hp-healthy: #4ecca3;
    --hp-wounded: #f0a500;
    --hp-critical: #e94560;
}

/* --- Parchment & Ink (light) --- */
[data-theme='parchment'] {
    --surface-app: #f5f1e8;
    --surface-card: #fdfcf9;
    --surface-card-hover: #efe8d6;
    --surface-popover: #fdfcf9;
    --surface-muted: #e8e3d6;

    --ink-primary: #2a1810;
    --ink-secondary: #6b5d52;
    --ink-dim: rgba(42, 24, 16, 0.55);

    --brand-primary: #8b2635;
    --brand-secondary: #3d5a80;
    --brand-accent: #d4a574;
    --brand-accent-soft: rgba(139, 38, 53, 0.12);

    --line: rgba(139, 38, 53, 0.15);
    --line-strong: rgba(139, 38, 53, 0.25);

    --hp-healthy: #2b8a6b;
    --hp-wounded: #b47400;
    --hp-critical: #b01d34;

    /* Rebind legacy vars so existing components follow the theme */
    --bg-base: var(--surface-app);
    --bg-surface: var(--surface-card);
    --bg-elevated: var(--surface-card-hover);
    --text-primary: var(--ink-primary);
    --text-secondary: var(--ink-secondary);
    --text-dim: var(--ink-dim);
    --text-bright: #1a0e07;
    --accent: var(--brand-primary);
    --accent-hover: #a63242;
    --accent-strong: #731d2a;
    --accent-on-dark: var(--brand-primary);
    --border: var(--line);
    --border-strong: var(--line-strong);
}

/* --- Game-mode token overlays ---
 * Subtle brand-accent shift per mode. Set via JS:
 *   document.documentElement.setAttribute('data-mode', 'combat' | ...)
 * Overlays are intentionally low-contrast — atmosphere, not repaint.
 */
[data-mode='combat'] {
    --brand-accent: #c0392b;
    --brand-accent-soft: rgba(192, 57, 43, 0.18);
    --mode-tint: rgba(192, 57, 43, 0.08);
    --mode-glow: 0 0 24px rgba(192, 57, 43, 0.15);
}
[data-mode='exploration'] {
    --brand-accent: #3d8a6f;
    --brand-accent-soft: rgba(61, 138, 111, 0.18);
    --mode-tint: rgba(61, 138, 111, 0.06);
    --mode-glow: 0 0 24px rgba(61, 138, 111, 0.12);
}
[data-mode='social'] {
    --brand-accent: #8e6fbf;
    --brand-accent-soft: rgba(142, 111, 191, 0.18);
    --mode-tint: rgba(142, 111, 191, 0.06);
    --mode-glow: 0 0 24px rgba(142, 111, 191, 0.12);
}
[data-mode='rest'] {
    --brand-accent: #d89060;
    --brand-accent-soft: rgba(216, 144, 96, 0.18);
    --mode-tint: rgba(216, 144, 96, 0.06);
    --mode-glow: 0 0 24px rgba(216, 144, 96, 0.12);
}
[data-mode='downtime'] {
    --brand-accent: #6b8ab8;
    --brand-accent-soft: rgba(107, 138, 184, 0.18);
    --mode-tint: rgba(107, 138, 184, 0.06);
    --mode-glow: 0 0 24px rgba(107, 138, 184, 0.12);
}

/* Dashboard header gets the mode glow for ambient feedback */
.dashboard-header {
    box-shadow: var(--mode-glow, none);
    transition: box-shadow var(--transition-base);
}
.dashboard-header-meta {
    display: flex;
    align-items: center;
    gap: var(--space-md);
    flex-wrap: wrap;
}

/* --- Density variants ---
 * Scales the core spacing + text-md + radius + header/nav heights.
 */
[data-density='compact'] {
    --space-xs: 0.2rem;
    --space-sm: 0.4rem;
    --space-md: 0.75rem;
    --space-lg: 1.1rem;
    --space-xl: 1.5rem;
    --space-2xl: 2.25rem;
    --header-height: 48px;
    --nav-height: 52px;
    --text-md: 0.92rem;
}

[data-density='comfortable'] {
    --space-xs: 0.3rem;
    --space-sm: 0.65rem;
    --space-md: 1.2rem;
    --space-lg: 1.8rem;
    --space-xl: 2.4rem;
    --space-2xl: 3.5rem;
    --header-height: 64px;
    --nav-height: 64px;
    --text-md: 1.06rem;
}

/* --- Typography additions --- */
body {
    font-family: var(--font-body, var(--font-base));
}

code, kbd, samp, pre, .font-mono {
    font-family: var(--font-mono);
}

/* --- Semantic HP bar (color-zoned) ---
 * Usage:
 *   <div class="hp-bar" style="--hp-pct:67"></div>
 * Zone is derived from the --hp-pct custom property.
 */
.hp-bar {
    position: relative;
    display: block;
    height: 6px;
    border-radius: var(--radius-full);
    background: var(--surface-muted);
    overflow: hidden;
}

.hp-bar::after {
    content: '';
    position: absolute;
    inset: 0;
    width: calc(var(--hp-pct, 100) * 1%);
    background: var(--hp-healthy);
    transition: width var(--transition-base), background var(--transition-base);
}

.hp-bar[data-zone='wounded']::after {
    background: var(--hp-wounded);
}

.hp-bar[data-zone='critical']::after {
    background: var(--hp-critical);
    animation: hp-pulse 1.6s ease-in-out infinite;
}

@keyframes hp-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.7; }
}

/* --- Session presence footer ---
 * Fixed just above the bottom nav on mobile; bottom strip on desktop.
 * Slim, mono, non-intrusive.
 */
#app-footer {
    position: fixed;
    left: 0;
    right: 0;
    bottom: var(--nav-height);
    border-top: 1px solid var(--border);
    background: color-mix(in srgb, var(--bg-surface) 92%, transparent);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    color: var(--text-dim);
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    padding: 4px var(--space-md);
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: var(--space-md);
    letter-spacing: 0.02em;
    z-index: var(--z-sticky);
    min-height: 22px;
}

/* Give #app-main room for the footer so content doesn't hide behind it */
#app-main {
    padding-bottom: calc(var(--nav-height) + 26px + var(--space-md)) !important;
}

@media (min-width: 1024px) {
    /* Desktop: if the nav is moved/hidden, the footer sticks to viewport bottom */
    #app-footer { bottom: 0; }
}

#app-footer .session-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--hp-healthy);
    box-shadow: 0 0 0 3px rgba(78, 204, 163, 0.18);
    margin-right: 6px;
    vertical-align: middle;
    animation: session-breathe 2.4s ease-in-out infinite;
}

#app-footer .session-dot[data-state='offline'] {
    background: var(--text-dim);
    box-shadow: none;
    animation: none;
}

@keyframes session-breathe {
    0%, 100% { box-shadow: 0 0 0 3px rgba(78, 204, 163, 0.12); }
    50% { box-shadow: 0 0 0 5px rgba(78, 204, 163, 0.22); }
}

/* --- Initiative active-turn highlight ---
 * Subtle scale + accent border for the current combatant.
 */
.combat-entry[data-active='true'] {
    transform: scale(1.015);
    border-color: var(--accent);
    background: var(--brand-accent-soft, var(--accent-dim));
    transition: transform var(--transition-base), border-color var(--transition-base), background var(--transition-base);
}

/* --- Popover panel (unified) --- */
.popover-panel {
    background: var(--bg-surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-md);
    box-shadow: 0 12px 32px rgba(0,0,0,0.4);
    padding: var(--space-md);
    color: var(--text-primary);
    font-family: var(--font-body, var(--font-base));
    animation: popover-in 120ms ease-out;
}

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

.popover-title {
    font-family: var(--font-display), serif;
    font-size: var(--text-base);
    font-weight: 600;
    color: var(--accent);
    margin-bottom: 0.25rem;
    letter-spacing: 0.02em;
}
.popover-body {
    font-size: var(--text-sm);
    line-height: 1.45;
    color: var(--text-secondary);
}

/* Weapon-mastery badge — clickable pill in inventory rows */
.mastery-badge {
    display: inline-flex;
    align-items: center;
    padding: 0.1rem 0.5rem;
    background: color-mix(in srgb, var(--accent) 15%, transparent);
    color: var(--accent);
    border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent);
    border-radius: 999px;
    font-size: 0.7rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    cursor: help;
    font-family: inherit;
    transition: background 0.15s, transform 0.15s;
}
.mastery-badge:hover,
.mastery-badge:focus-visible {
    background: color-mix(in srgb, var(--accent) 30%, transparent);
    transform: translateY(-1px);
}

/* --- Hover card --- */
.hover-card {
    background: var(--bg-surface);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-md);
    box-shadow: 0 12px 32px rgba(0,0,0,0.4);
    padding: var(--space-sm) var(--space-md);
    color: var(--text-primary);
    font-size: var(--text-sm);
    max-width: 320px;
    pointer-events: auto;
}
.hover-card-head { display: flex; align-items: center; gap: var(--space-sm); margin-bottom: 4px; }
.hover-card-head strong { font-family: var(--font-display); font-weight: 600; }
.hover-card-type {
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 2px 6px;
    border-radius: var(--radius-sm);
    background: var(--brand-accent-soft, var(--accent-dim));
    color: var(--accent);
}
.hover-card-meta { font-size: var(--text-xs); color: var(--text-dim); margin-bottom: 4px; }
.hover-card-body { font-size: var(--text-sm); line-height: 1.4; color: var(--text-secondary); }

/* --- Expandable card --- */
.x-card {
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    margin-bottom: var(--space-sm);
    overflow: hidden;
}
.x-card-head {
    display: grid;
    grid-template-columns: 1fr auto auto;
    gap: var(--space-sm);
    align-items: center;
    padding: var(--space-sm) var(--space-md);
    cursor: pointer;
    user-select: none;
}
.x-card-head h3 {
    margin: 0;
    font-family: var(--font-display);
    font-size: var(--text-md);
}
.x-card-preview {
    color: var(--text-dim);
    font-size: var(--text-sm);
    font-family: var(--font-mono);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 50vw;
}
.x-card-toggle {
    background: transparent;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform var(--transition-fast);
    color: var(--text-secondary);
}
.x-card[data-expanded='true'] .x-card-toggle .x-chevron { transform: rotate(180deg); }
.x-chevron { display: inline-block; transition: transform var(--transition-fast); }
.x-card-body { padding: 0 var(--space-md) var(--space-md); border-top: 1px solid var(--border); padding-top: var(--space-md); }
.x-card[data-expanded='false'] .x-card-body { border-top: 0; padding-top: 0; }

/* --- Background particles --- */
.bg-particles {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 0;
    opacity: 0.85;
}

/* --- Presence avatars --- */
.presence-avatars {
    display: flex;
    align-items: center;
    gap: 2px;
}
.presence-avatar {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: var(--bg-elevated);
    border: 2px solid var(--bg-surface);
    margin-left: -6px;
    overflow: hidden;
    font-family: var(--font-display);
    font-size: 12px;
    color: var(--text-primary);
}
.presence-avatar:first-child { margin-left: 0; }
.presence-avatar img { width: 100%; height: 100%; object-fit: cover; }
.presence-avatar-initial { line-height: 1; }
.presence-count {
    margin-left: 6px;
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    color: var(--text-dim);
}

/* --- Feedback animations (CSS-only, reliable across element types) --- */
@keyframes fx-damage {
    0%   { transform: scale(1); color: inherit; text-shadow: none; }
    30%  { transform: scale(1.18); color: var(--hp-critical); text-shadow: 0 0 12px var(--hp-critical); }
    100% { transform: scale(1); color: inherit; text-shadow: none; }
}
@keyframes fx-heal {
    0%   { transform: scale(1); color: inherit; text-shadow: none; }
    30%  { transform: scale(1.12); color: var(--hp-healthy); text-shadow: 0 0 10px var(--hp-healthy); }
    100% { transform: scale(1); color: inherit; text-shadow: none; }
}
@keyframes fx-slot-fade {
    0%   { transform: scale(1); opacity: 1; }
    40%  { transform: scale(0.6); opacity: 0.3; filter: blur(1px); }
    100% { transform: scale(1); opacity: 1; filter: blur(0); }
}
.fx-damage { animation: fx-damage 0.55s ease-out; display: inline-block; }
.fx-heal   { animation: fx-heal 0.5s ease-out; display: inline-block; }
.fx-slot-fade { animation: fx-slot-fade 0.35s ease-out; }

@media (prefers-reduced-motion: reduce) {
    .fx-damage, .fx-heal, .fx-slot-fade { animation: none; }
}

/* --- Palette group headers (v2) --- */
.palette-group {
    font-family: var(--font-mono);
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    color: var(--text-dim);
    padding: 8px var(--space-md) 4px;
    position: sticky;
    top: 0;
    background: color-mix(in srgb, var(--bg-surface) 95%, transparent);
    z-index: 1;
}

/* --- View Transitions (root-level cross-fade) --- */
@supports (view-transition-name: auto) {
    ::view-transition-old(root),
    ::view-transition-new(root) {
        animation-duration: 220ms;
    }
}

@media (prefers-reduced-motion: reduce) {
    .combat-entry[data-active='true'] { transform: none; }
    .hp-bar[data-zone='critical']::after { animation: none; }
    #app-footer .session-dot { animation: none; }
}
