/* XRPG Engine Theme System
 *
 * DUAL-PERSONALITY THEMES:
 * - Dark mode and Light mode use neutral greyscale by default (white label)
 * - Per-instance accent colors are injected via metagame config in base.html
 *
 * HOW IT WORKS:
 * All colors use CSS custom properties. The accent colors (--accent-1, --accent-2,
 * --accent-warm, --accent-copper, etc.) are set INSIDE each theme block, not in :root.
 * This means switching themes automatically swaps the entire color personality.
 * Instance-specific colors override these defaults via <style> block in base.html.
 *
 * ADDING NEW STYLES:
 * - Use var(--accent-1) for primary accent
 * - Use var(--accent-copper) for secondary accent
 * - Use var(--text-accent) for accent-colored text
 * - Use var(--btn-primary-text) for text on primary buttons
 * - Never hardcode hex values for accents — always use variables
 * - Semantic colors: use var(--text-success), var(--text-danger), var(--text-info)
 */

:root {
    /* Font families */
    --font-display: 'Cinzel', serif;
    --font-body: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    --font-mono: 'JetBrains Mono', monospace;

    /* Transitions */
    --transition-fast: 150ms ease;
    --transition-normal: 300ms ease;
    --transition-slow: 500ms ease;

    /* Spacing */
    --radius-sm: 4px;
    --radius-md: 8px;
    --radius-lg: 12px;
    --radius-xl: 20px;
}

/* ===== Dark Theme (default) — Neutral Greyscale (white label) ===== */
[data-theme="dark"] {
    /* Accent palette */
    --accent-1: #a0a0a0;
    --accent-2: #666666;
    --accent-1-dim: #a0a0a018;
    --accent-2-dim: #66666618;
    --accent-warm: #b0b0b0;
    --accent-copper: #888888;

    /* Backgrounds */
    --bg-primary: #111111;
    --bg-secondary: #1a1a1a;
    --bg-tertiary: #222222;
    --bg-card: #1c1c1c;
    --bg-hover: #2a2a2a;
    --bg-inset: #0d0d0d;

    /* Text */
    --text-primary: #d4d4d4;
    --text-secondary: #999999;
    --text-muted: #666666;
    --text-accent: #b0b0b0;

    /* Borders */
    --border: #2e2e2e;
    --border-bright: #3d3d3d;

    /* Shadows */
    --shadow-card: 0 2px 12px rgba(0, 0, 0, 0.4);
    --shadow-hover: 0 4px 20px rgba(0, 0, 0, 0.5);

    /* Scrollbar */
    --scrollbar-track: #1a1a1a;
    --scrollbar-thumb: #3d3d3d;
    --scrollbar-thumb-hover: #a0a0a0;

    /* Button */
    --btn-primary-bg: #a0a0a0;
    --btn-primary-text: #0e0e0e;
    --btn-primary-hover-bg: #888888;
    --btn-primary-hover-text: #ffffff;

    /* Semantic status */
    --text-success: #6bbd7b;
    --text-danger: #d47272;
    --text-info: #6ba3d4;
    --bg-success: #16513118;
    --bg-danger: #51161618;
    --bg-info: #1e3a5f18;
}

/* ===== Light Theme — Neutral Greyscale (white label) ===== */
[data-theme="light"] {
    /* Accent palette */
    --accent-1: #555555;
    --accent-2: #333333;
    --accent-1-dim: #55555518;
    --accent-2-dim: #33333318;
    --accent-warm: #666666;
    --accent-copper: #4a4a4a;

    /* Backgrounds */
    --bg-primary: #f5f5f5;
    --bg-secondary: #ebebeb;
    --bg-tertiary: #e0e0e0;
    --bg-card: #fafafa;
    --bg-hover: #e8e8e8;
    --bg-inset: #e2e2e2;

    /* Text */
    --text-primary: #1a1a1a;
    --text-secondary: #444444;
    --text-muted: #777777;
    --text-accent: #444444;

    /* Borders */
    --border: #cccccc;
    --border-bright: #aaaaaa;

    /* Shadows */
    --shadow-card: 0 2px 12px rgba(0, 0, 0, 0.06);
    --shadow-hover: 0 4px 20px rgba(0, 0, 0, 0.1);

    /* Scrollbar */
    --scrollbar-track: #ebebeb;
    --scrollbar-thumb: #aaaaaa;
    --scrollbar-thumb-hover: #555555;

    /* Button */
    --btn-primary-bg: #555555;
    --btn-primary-text: #ffffff;
    --btn-primary-hover-bg: #333333;
    --btn-primary-hover-text: #ffffff;

    /* Semantic status */
    --text-success: #1a7a34;
    --text-danger: #b33c3c;
    --text-info: #2a6b8a;
    --bg-success: #d4edda;
    --bg-danger: #f8d7da;
    --bg-info: #d1ecf1;
}

/* ===== Scrollbar Styles ===== */
* {
    scrollbar-width: thin;
    scrollbar-color: var(--scrollbar-thumb) var(--scrollbar-track);
}

::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: var(--scrollbar-track);
}

::-webkit-scrollbar-thumb {
    background: var(--scrollbar-thumb);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--scrollbar-thumb-hover);
}

::-webkit-scrollbar-corner {
    background: var(--scrollbar-track);
}

/* Reset & Base */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    font-size: 16px;
    scroll-behavior: smooth;
}

body {
    font-family: var(--font-body);
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
    min-height: 100vh;
    overflow-x: hidden;
}

/* Typography */
h1, h2, h3, h4 {
    font-family: var(--font-display);
    font-weight: 600;
    line-height: 1.2;
    color: var(--text-primary);
}

h1 { font-size: 3rem; }
h2 { font-size: 2rem; }
h3 { font-size: 1.5rem; }

/* Links */
a {
    color: var(--text-accent);
    transition: color var(--transition-fast);
}

a:hover {
    color: var(--accent-warm);
}

/* Utility classes */
.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 1.5rem;
}

.text-accent {
    color: var(--text-accent);
}

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

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

/* Status feedback */
.status-ok { background: var(--bg-success); color: var(--text-success); border: 1px solid var(--text-success); }
.status-error { background: var(--bg-danger); color: var(--text-danger); border: 1px solid var(--text-danger); }

/* ===== Theme-aware image switching ===== */
[data-theme="dark"] .theme-img-light { display: none !important; }
[data-theme="dark"] .theme-img-dark { display: block; }
[data-theme="light"] .theme-img-dark { display: none !important; }
[data-theme="light"] .theme-img-light { display: block; }

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1.5rem;
    border-radius: var(--radius-md);
    font-family: var(--font-body);
    font-size: 0.95rem;
    font-weight: 500;
    cursor: pointer;
    transition: all var(--transition-fast);
    border: none;
    text-decoration: none;
}

.btn-primary {
    background: var(--btn-primary-bg);
    color: var(--btn-primary-text);
    font-weight: 600;
}

.btn-primary:hover {
    background: var(--btn-primary-hover-bg);
    color: var(--btn-primary-hover-text);
    transform: translateY(-1px);
    box-shadow: var(--shadow-hover);
}

.btn-outline {
    background: transparent;
    color: var(--text-primary);
    border: 1px solid var(--border-bright);
}

.btn-outline:hover {
    background: var(--bg-hover);
    border-color: var(--accent-1);
    color: var(--text-accent);
}

.btn-sm {
    padding: 0.5rem 1rem;
    font-size: 0.85rem;
}

/* Hero button with background image */
.btn-hero {
    position: relative;
    overflow: hidden;
}

.btn-hero::before {
    content: '';
    position: absolute;
    inset: 0;
    background-image: var(--btn-hero-img);
    background-size: cover;
    background-position: center;
    opacity: 0.15;
    transition: opacity var(--transition-normal);
}

.btn-hero:hover::before {
    opacity: 0.25;
}

.btn-hero > * {
    position: relative;
    z-index: 1;
}

/* Cards */
.card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 1.5rem;
    transition: all var(--transition-normal);
    box-shadow: var(--shadow-card);
}

.card:hover {
    border-color: var(--border-bright);
    box-shadow: var(--shadow-hover);
    transform: translateY(-2px);
}

/* Badge */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    padding: 0.25rem 0.75rem;
    border-radius: 9999px;
    font-size: 0.75rem;
    font-weight: 500;
}

.badge-accent {
    background: var(--accent-1-dim);
    color: var(--text-accent);
    border: 1px solid var(--accent-copper);
}

/* Divider */
.divider {
    width: 60px;
    height: 2px;
    background: var(--accent-1);
    margin: 0 auto;
    opacity: 0.6;
}

/* ===== Site Navigation ===== */
.site-nav {
    position: sticky;
    top: 0;
    z-index: 100;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    backdrop-filter: blur(8px);
}

.nav-inner {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 1.5rem;
    height: 56px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.nav-brand {
    display: flex;
    align-items: center;
    text-decoration: none;
}

.nav-brand-logo {
    height: 36px;
    width: auto;
    object-fit: contain;
}

.nav-brand-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-accent);
}

.nav-brand:hover {
    opacity: 0.85;
}

.nav-links {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.nav-link {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    padding: 0.4rem 0.7rem;
    font-size: 0.85rem;
    color: var(--text-secondary);
    text-decoration: none;
    border-radius: var(--radius-sm);
    transition: all var(--transition-fast);
}

.nav-link:hover {
    color: var(--text-accent);
    background: var(--bg-hover);
}

.nav-link-btn {
    background: none;
    border: none;
    cursor: pointer;
    font-family: var(--font-body);
}

.theme-toggle {
    background: none;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 0.35rem;
    cursor: pointer;
    color: var(--text-muted);
    transition: all var(--transition-fast);
    display: flex;
    align-items: center;
    justify-content: center;
    margin-left: 0.25rem;
}

.theme-toggle:hover {
    color: var(--text-accent);
    border-color: var(--accent-1);
}

@media (max-width: 600px) {
    .nav-link { padding: 0.35rem 0.45rem; font-size: 0.8rem; }
    .nav-inner { padding: 0 1rem; }
}

/* Animations */
@keyframes fade-in {
    from { opacity: 0; transform: translateY(16px); }
    to { opacity: 1; transform: translateY(0); }
}

@keyframes blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
}

.animate-fade-in {
    animation: fade-in 0.5s ease-out forwards;
}

/* ===== Site Footer ===== */
.site-footer {
    border-top: 1px solid var(--border);
    padding: 2rem 0;
    background: var(--bg-secondary);
}

.site-footer-inner {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
}

.footer-brand {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.footer-logo {
    height: 32px;
    width: auto;
    object-fit: contain;
}

.footer-logo-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-accent);
}

.footer-version {
    font-family: var(--font-mono);
    font-size: 0.78rem;
    color: var(--text-muted);
}

.footer-nav {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.5rem;
}

@media (min-width: 600px) {
    .footer-nav {
        flex-direction: row;
        gap: 0;
    }
}

.footer-links {
    display: flex;
    align-items: center;
    gap: 1.25rem;
}

.footer-link {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    font-size: 0.85rem;
    color: var(--text-secondary);
    text-decoration: none;
    transition: color var(--transition-fast);
}

.footer-link:hover {
    color: var(--text-accent);
}

.footer-legal {
    padding-top: 0.5rem;
    border-top: 1px solid var(--border);
}

@media (min-width: 600px) {
    .footer-legal {
        padding-top: 0;
        border-top: none;
        padding-left: 1.25rem;
        border-left: 1px solid var(--border);
    }
}

.footer-copy {
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* ===== Card Background Icon System ===== */
.card-icon-bg {
    position: relative;
    overflow: hidden;
}

.card-bg-icon {
    position: absolute;
    top: -10px;
    right: -10px;
    width: 120px;
    height: 120px;
    opacity: 0.05;
    pointer-events: none;
    display: flex;
    align-items: center;
    justify-content: center;
}

.card-bg-icon svg {
    width: 100%;
    height: 100%;
}

/* Size variants */
.card-icon-sm .card-bg-icon { width: 80px; height: 80px; }
.card-icon-lg .card-bg-icon { width: 160px; height: 160px; }

/* Position variants */
.card-icon-tl .card-bg-icon { top: -10px; right: auto; left: -10px; }
.card-icon-br .card-bg-icon { top: auto; bottom: -10px; right: -10px; }
.card-icon-bl .card-bg-icon { top: auto; bottom: -10px; right: auto; left: -10px; }
.card-icon-center .card-bg-icon { top: 50%; left: 50%; transform: translate(-50%, -50%); right: auto; }

/* ===== Form Inputs (service keys, etc.) ===== */
.svc-key-input {
    width: 200px;
    padding: 0.4rem 0.6rem;
    font-size: 0.82rem;
    background: var(--bg-inset);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font-family: var(--font-body);
}

.svc-key-input:focus {
    outline: none;
    border-color: var(--accent-1);
}

.svc-key-input::placeholder {
    color: var(--text-muted);
}

/* ── Tier Picker — interactive badge-group selector ────────
   Usage:  <div class="tier-picker" data-field="text_model_tier" data-uid="...">
             <button class="tp-badge tp-free tp-active" ...>Free</button>
             <button class="tp-badge tp-mid" ...>Mid</button>
             <button class="tp-badge tp-pro" ...>Pro</button>
           </div>
   Label:  Add <span class="tp-label">Text</span> before badges for context.
   Reusable on admin, account, or any page. */
.tier-picker {
    display: inline-flex;
    align-items: center;
    gap: 0.2rem;
    background: var(--bg-inset);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.15rem;
}
.tp-label {
    font-size: 0.62rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-muted);
    padding: 0 0.35rem;
    white-space: nowrap;
}
.tp-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.2rem;
    padding: 0.2rem 0.5rem;
    border: 1.5px solid transparent;
    border-radius: 6px;
    font-size: 0.65rem;
    font-weight: 700;
    letter-spacing: 0.03em;
    cursor: pointer;
    transition: all 0.2s ease;
    background: transparent;
    opacity: 0.4;
    filter: saturate(0.3);
    font-family: var(--font-body);
    white-space: nowrap;
}
.tp-badge:hover:not(.tp-active) {
    opacity: 0.65;
    filter: saturate(0.6);
}
.tp-badge .tp-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    flex-shrink: 0;
}
/* Free */
.tp-free { color: #9ca3af; }
.tp-free .tp-dot { background: #9ca3af; }
.tp-free.tp-active {
    opacity: 1; filter: saturate(1);
    background: color-mix(in srgb, #9ca3af 12%, transparent);
    border-color: color-mix(in srgb, #9ca3af 40%, transparent);
    box-shadow: 0 1px 4px color-mix(in srgb, #9ca3af 20%, transparent);
}
/* Mid */
.tp-mid { color: #60a5fa; }
.tp-mid .tp-dot { background: #3b82f6; }
.tp-mid.tp-active {
    opacity: 1; filter: saturate(1);
    background: color-mix(in srgb, #3b82f6 12%, transparent);
    border-color: color-mix(in srgb, #3b82f6 45%, transparent);
    box-shadow: 0 1px 6px color-mix(in srgb, #3b82f6 30%, transparent);
}
/* Pro */
.tp-pro { color: #fbbf24; }
.tp-pro .tp-dot { background: #f59e0b; }
.tp-pro.tp-active {
    opacity: 1; filter: saturate(1);
    background: color-mix(in srgb, #f59e0b 14%, transparent);
    border-color: color-mix(in srgb, #f59e0b 50%, transparent);
    box-shadow: 0 2px 8px color-mix(in srgb, #f59e0b 25%, transparent),
                0 0 12px color-mix(in srgb, #f59e0b 10%, transparent);
}
.tp-pro.tp-active .tp-dot { box-shadow: 0 0 5px #f59e0b80; }

/* ── Action Icon Button — reusable icon-only action buttons ──
   Usage:  <button class="action-icon-btn"> <i data-lucide="image" ...></i> </button>
           <button class="action-icon-btn action-icon-danger"> <i data-lucide="trash-2" ...></i> </button>
   Variants: default (accent), .action-icon-danger (red), .action-icon-success (green)
   Works anywhere: factory cards, admin rows, inventory, etc. */
.action-icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm, 6px);
    background: var(--bg-card);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all 0.2s ease;
    padding: 0;
    flex-shrink: 0;
}
.action-icon-btn:hover {
    border-color: var(--accent-1);
    color: var(--text-accent);
    background: color-mix(in srgb, var(--accent-1) 8%, var(--bg-card));
    box-shadow: 0 2px 8px color-mix(in srgb, var(--accent-1) 15%, transparent);
    transform: translateY(-1px);
}
.action-icon-btn:active {
    transform: translateY(0);
    box-shadow: none;
}
.action-icon-btn.action-icon-danger:hover {
    border-color: #ef4444;
    color: #ef4444;
    background: color-mix(in srgb, #ef4444 8%, var(--bg-card));
    box-shadow: 0 2px 8px color-mix(in srgb, #ef4444 15%, transparent);
}
.action-icon-btn.action-icon-success:hover {
    border-color: #22c55e;
    color: #22c55e;
    background: color-mix(in srgb, #22c55e 8%, var(--bg-card));
    box-shadow: 0 2px 8px color-mix(in srgb, #22c55e 15%, transparent);
}

/* Responsive */
@media (max-width: 768px) {
    h1 { font-size: 2rem; }
    h2 { font-size: 1.5rem; }
    h3 { font-size: 1.25rem; }
}

/* ===== Mobile Navigation (Hamburger) ===== */
.nav-hamburger {
    display: none;
    background: none;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 0.3rem;
    cursor: pointer;
    color: var(--text-secondary);
    transition: all var(--transition-fast);
}

.nav-hamburger:hover { color: var(--text-accent); border-color: var(--accent-1); }

@media (max-width: 540px) {
    .nav-hamburger { display: flex; align-items: center; justify-content: center; }

    .nav-links {
        display: none;
        position: absolute;
        top: 100%;
        right: 0;
        left: 0;
        background: var(--bg-secondary);
        border-bottom: 1px solid var(--border);
        padding: 0.5rem 1rem;
        flex-direction: column;
        align-items: stretch;
        gap: 0.15rem;
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
        z-index: 99;
    }

    .nav-links.nav-open { display: flex; }

    .nav-link {
        padding: 0.6rem 0.75rem;
        font-size: 0.85rem;
        border-radius: var(--radius-sm);
        gap: 0.5rem;
        min-height: 44px;
    }

    .nav-link::after {
        content: attr(title);
        font-size: 0.82rem;
    }

    .nav-link-btn {
        min-height: 44px;
        justify-content: flex-start;
        padding: 0.6rem 0.75rem;
        gap: 0.5rem;
    }

    .nav-link-btn::after {
        content: 'Sign out';
        font-size: 0.82rem;
    }

    .theme-toggle {
        margin-left: 0;
        min-height: 44px;
        justify-content: flex-start;
        padding: 0.6rem 0.75rem;
        border: none;
        gap: 0.5rem;
    }

    .theme-toggle::after {
        content: 'Toggle theme';
        font-size: 0.82rem;
        color: var(--text-secondary);
    }

    .nav-inner { position: relative; }
}

/* ===== Global Mobile Optimization ===== */
@media (max-width: 600px) {
    /* Container padding reduction */
    .container { padding-left: 0.75rem; padding-right: 0.75rem; }

    /* Touch-friendly buttons */
    .btn, button { min-height: 44px; }
    .btn-sm { min-height: 36px; }

    /* Use dynamic viewport height where applicable */
    .full-vh { height: 100dvh; }

    /* Cards and modals: reduce padding */
    .card { padding: 0.75rem; }

    /* Footer compactness */
    .site-footer { padding: 1.5rem 0; }
    .footer-links { gap: 0.75rem; flex-wrap: wrap; justify-content: center; }
    .footer-link { font-size: 0.78rem; }
}

@media (max-width: 400px) {
    .container { padding-left: 0.5rem; padding-right: 0.5rem; }
    h1 { font-size: 1.5rem; }
    h2 { font-size: 1.25rem; }
}

/* Safe area insets for notched phones */
@supports (padding: max(0px)) {
    .site-nav { padding-left: max(0px, env(safe-area-inset-left)); padding-right: max(0px, env(safe-area-inset-right)); }
    .site-footer { padding-bottom: max(1.5rem, env(safe-area-inset-bottom)); }
}
