/* assets/css/codex.css */
/* =====================================================
   CODEX AI ASSISTANT — Styles
   The floating grimoire, chat panel, action cards, and
   all Codex-related UI components.
   ===================================================== */

/* ----- CSS Variables ----- */
:root {
    --codex-primary: #8B6914;        /* Aged gold — grimoire leather */
    --codex-primary-light: #C4A234;  /* Bright gold — active/glow state */
    --codex-accent: #4a69bd;         /* Match site primary */
    --codex-bg: #1a1207;             /* Dark parchment */
    --codex-bg-light: #2d2010;       /* Lighter parchment */
    --codex-text: #e8dcc8;           /* Warm off-white */
    --codex-text-muted: #c0b49c;     /* Muted parchment text (4.5:1 on --codex-bg) */
    --codex-border: #5a4a2a;         /* Gold-brown border */
    --codex-panel-width: 380px;
    --codex-icon-size: 60px;
    --codex-z-icon: 1010;   /* Must sit above cookie banner (z-index: 1001) */
    --codex-z-panel: 1020;
    --codex-transition: 0.3s ease;
    --codex-glow: 0 0 15px rgba(196, 162, 52, 0.4);
    --codex-header-offset: 9rem; /* Clear fixed site header + rules toggle bar */
}

/* Dark mode adjustments — site uses html.dark */
html.dark {
    --codex-bg: #0d0a04;
    --codex-bg-light: #1a1207;
    --codex-border: #3d3018;
}

/* ----- Floating Grimoire Icon ----- */
.codex-grimoire {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: var(--codex-icon-size);
    height: var(--codex-icon-size);
    z-index: var(--codex-z-icon);
    cursor: pointer;
    transition: transform var(--codex-transition), box-shadow var(--codex-transition);
    /* No outline/border — the icon itself is the visual */
    background: none;
    border: none;
    padding: 0;
}

.codex-grimoire:focus-visible {
    outline: 2px solid var(--codex-primary-light);
    outline-offset: 4px;
    border-radius: 8px;
}

/* Grimoire visual — book shape using CSS */
.codex-grimoire-body {
    width: 100%;
    height: 100%;
    background: linear-gradient(135deg, var(--codex-bg-light) 0%, var(--codex-bg) 100%);
    border: 2px solid var(--codex-border);
    border-radius: 4px 12px 12px 4px; /* Book spine left, rounded right */
    position: relative;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    transition: box-shadow var(--codex-transition), transform var(--codex-transition);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

/* Spine detail */
.codex-grimoire-body::before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 6px;
    background: linear-gradient(to bottom, var(--codex-primary), var(--codex-primary-light), var(--codex-primary));
    border-radius: 4px 0 0 4px;
}

/* Eye / face of the grimoire */
.codex-eye {
    width: 14px;
    height: 14px;
    background: radial-gradient(circle, var(--codex-primary-light) 30%, var(--codex-primary) 70%);
    border-radius: 50%;
    position: relative;
    transition: transform 0.2s ease;
    box-shadow: 0 0 6px rgba(196, 162, 52, 0.5);
}

.codex-eye::after {
    content: '';
    position: absolute;
    width: 5px;
    height: 5px;
    background: var(--codex-bg);
    border-radius: 50%;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    transition: transform 0.15s ease;
}

/* Page ruffle lines */
.codex-pages {
    position: absolute;
    right: 8px;
    top: 20%;
    bottom: 20%;
    width: 2px;
    display: flex;
    flex-direction: column;
    justify-content: space-around;
}

.codex-pages span {
    display: block;
    width: 100%;
    height: 1px;
    background: var(--codex-border);
    opacity: 0.5;
}

/* ----- Animation States ----- */

/* Idle — gentle float */
@keyframes codex-idle-float {
    0%, 100% { transform: translateY(0px); }
    50% { transform: translateY(-6px); }
}

.codex-grimoire[data-state="idle"] {
    animation: codex-idle-float 4s ease-in-out infinite;
}

/* Attentive — perks up, slight lean toward content */
@keyframes codex-attentive-perk {
    0% { transform: translateY(0) rotate(0deg); }
    100% { transform: translateY(-4px) rotate(-3deg); }
}

.codex-grimoire[data-state="attentive"] {
    animation: codex-attentive-perk 0.3s ease forwards;
}

.codex-grimoire[data-state="attentive"] .codex-grimoire-body {
    box-shadow: var(--codex-glow);
}

/* Shy — looks away when cursor approaches */
.codex-grimoire[data-state="shy"] .codex-eye::after {
    transform: translate(-150%, -50%); /* Pupil looks away */
}

.codex-grimoire[data-state="shy"] {
    animation: none;
    transform: translateY(-2px) rotate(2deg); /* Slight turn away */
}

/* Active — glowing, open */
@keyframes codex-active-glow {
    0%, 100% { box-shadow: 0 0 15px rgba(196, 162, 52, 0.4); }
    50% { box-shadow: 0 0 25px rgba(196, 162, 52, 0.7); }
}

.codex-grimoire[data-state="active"] {
    animation: none;
    transform: translateY(-4px) scale(1.05);
}

.codex-grimoire[data-state="active"] .codex-grimoire-body {
    animation: codex-active-glow 2s ease-in-out infinite;
    border-color: var(--codex-primary-light);
}

/* Hidden when chat is open (on mobile) */
.codex-grimoire.hidden {
    display: none;
}

/* ----- Locked State (no API key) ----- */
.codex-grimoire[data-locked="true"] {
    opacity: 0.5;
    cursor: default;
}

.codex-grimoire[data-locked="true"] .codex-eye {
    background: radial-gradient(circle, #666 30%, #444 70%);
    box-shadow: none;
}

.codex-locked-tooltip {
    display: none;
    position: absolute;
    bottom: calc(100% + 10px);
    right: 0;
    background: var(--codex-bg);
    color: var(--codex-text);
    padding: 8px 12px;
    border-radius: 6px;
    border: 1px solid var(--codex-border);
    font-size: 0.8rem;
    white-space: nowrap;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    pointer-events: none;
}

.codex-grimoire[data-locked="true"]:hover .codex-locked-tooltip,
.codex-grimoire[data-locked="true"]:focus-visible .codex-locked-tooltip {
    display: block;
}

/* ----- Chat Panel ----- */
.codex-panel {
    position: fixed;
    top: var(--codex-header-offset);
    right: 0;
    width: var(--codex-panel-width);
    height: calc(100vh - var(--codex-header-offset));
    z-index: var(--codex-z-panel);
    background: var(--codex-bg);
    color: var(--codex-text);
    border-left: 2px solid var(--codex-border);
    display: flex;
    flex-direction: column;
    transform: translateX(100%);
    transition: transform var(--codex-transition);
    box-shadow: -4px 0 20px rgba(0, 0, 0, 0.3);
}

.codex-panel.open {
    transform: translateX(0);
}

/* Panel header */
.codex-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    border-bottom: 1px solid var(--codex-border);
    background: var(--codex-bg-light);
    min-height: 52px;
}

.codex-panel-title {
    font-family: 'MedievalSharp', cursive;
    color: var(--codex-primary-light);
    font-size: 1.2rem;
    margin: 0;
}

/* Close button hover: subtle red tint to indicate destructive action */
.codex-panel-close:hover {
    color: #e74c3c !important;
    border-color: rgba(231, 76, 60, 0.3) !important;
    background: rgba(231, 76, 60, 0.08) !important;
}

/* Message area */
.codex-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    scroll-behavior: smooth;
}

/* Scrollbar styling */
.codex-messages::-webkit-scrollbar {
    width: 6px;
}

.codex-messages::-webkit-scrollbar-track {
    background: transparent;
}

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

/* Message bubbles */
.codex-message {
    max-width: 90%;
    padding: 10px 14px;
    border-radius: 12px;
    font-size: 0.9rem;
    line-height: 1.5;
    animation: codex-msg-in 0.2s ease;
    overflow-wrap: break-word;
    word-break: break-word;
}

@keyframes codex-msg-in {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.codex-message.codex-reply {
    align-self: flex-start;
    background: var(--codex-bg-light);
    color: var(--codex-text);
    border: 1px solid var(--codex-border);
    border-bottom-left-radius: 4px;
}

.codex-message.user-message {
    align-self: flex-end;
    background: var(--codex-accent);
    color: #fff;
    border-bottom-right-radius: 4px;
}

/* Typing indicator */
.codex-typing {
    align-self: flex-start;
    padding: 10px 14px;
    display: flex;
    gap: 4px;
}

.codex-typing span {
    width: 6px;
    height: 6px;
    background: var(--codex-text-muted);
    border-radius: 50%;
    animation: codex-typing-dot 1.2s ease-in-out infinite;
}

.codex-typing span:nth-child(2) { animation-delay: 0.2s; }
.codex-typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes codex-typing-dot {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-6px); opacity: 1; }
}

/* --- Waiting Quips --- */
.codex-waiting-quip {
    color: var(--codex-text-muted, #a89880);
    font-size: 0.8rem;
    padding: 4px 12px;
    opacity: 0.8;
    animation: codex-quip-fade 0.3s ease-in;
}

@keyframes codex-quip-fade {
    from { opacity: 0; transform: translateY(4px); }
    to { opacity: 0.8; transform: translateY(0); }
}

/* ----- Action Cards ----- */
.codex-action-card {
    background: var(--codex-bg-light);
    border: 1px solid var(--codex-border);
    border-radius: 8px;
    padding: 12px 14px;
    margin-top: 8px;
    max-width: 95%;
}

.codex-action-card-title {
    font-family: 'MedievalSharp', cursive;
    color: var(--codex-primary-light);
    font-size: 0.85rem;
    margin: 0 0 8px 0;
}

.codex-action-option {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 8px;
    border-radius: 4px;
    cursor: pointer;
    transition: background var(--codex-transition);
    color: var(--codex-text);
    font-size: 0.85rem;
}

.codex-action-option:hover {
    background: rgba(255, 255, 255, 0.05);
}

.codex-action-option input[type="radio"] {
    accent-color: var(--codex-primary-light);
}

.codex-action-option.selected {
    background: rgba(196, 162, 52, 0.1);
    border: 1px solid var(--codex-primary);
}

.codex-action-buttons {
    display: flex;
    gap: 8px;
    margin-top: 10px;
}

.codex-btn {
    padding: 6px 16px;
    border-radius: 6px;
    border: 1px solid var(--codex-border);
    font-size: 0.8rem;
    cursor: pointer;
    transition: all var(--codex-transition);
    font-family: 'Poppins', sans-serif;
}

.codex-btn-primary {
    background: var(--codex-primary);
    color: #fff;
    border-color: var(--codex-primary);
}

.codex-btn-primary:hover {
    background: var(--codex-primary-light);
}

.codex-btn-secondary {
    background: transparent;
    color: var(--codex-text-muted);
}

.codex-btn-secondary:hover {
    color: var(--codex-text);
    background: rgba(255, 255, 255, 0.05);
}

/* ----- Quick Action Buttons ----- */
.codex-quick-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 8px 16px;
    border-top: 1px solid var(--codex-border);
    background: var(--codex-bg);
}

button.codex-quick-btn {
    background: transparent !important;
    color: var(--codex-primary-light) !important;
    border: 1px solid var(--codex-primary) !important;
    border-radius: 16px;
    padding: 6px 14px !important;
    font-size: 0.75rem !important;
    cursor: pointer;
    transition: all var(--codex-transition);
    font-family: 'Poppins', sans-serif;
    white-space: nowrap;
    min-height: 36px !important;
    height: auto !important;
}

button.codex-quick-btn:hover {
    background: var(--codex-primary) !important;
    color: #fff !important;
}

/* ----- Copy Button (hover-reveal on Codex replies) ----- */
.codex-copy-btn {
    position: absolute;
    top: 4px;
    right: 4px;
    background: var(--codex-bg);
    color: var(--codex-text-muted);
    border: 1px solid var(--codex-border);
    border-radius: 4px;
    width: 32px;
    height: 32px;
    font-size: 0.75rem;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 6px;
    line-height: 1;
}

.codex-message:hover .codex-copy-btn,
.codex-message:focus-within .codex-copy-btn,
.codex-copy-btn:focus-visible {
    opacity: 1;
}

.codex-copy-btn:hover {
    color: var(--codex-text);
    border-color: var(--codex-primary-light);
}

/* ----- Header Action Buttons ----- */
.codex-header-actions {
    display: flex;
    align-items: center;
    gap: 2px;
}

.codex-header-actions button,
.codex-header-actions .codex-panel-close,
.codex-header-actions .codex-new-chat-btn {
    background: none !important;
    border: 1px solid transparent !important;
    color: var(--codex-text-muted);
    cursor: pointer;
    width: 44px !important;
    height: 44px !important;
    padding: 0 !important;
    margin: 0 !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    border-radius: 6px;
    transition: all var(--codex-transition);
}

.codex-header-actions button:hover {
    color: var(--codex-primary-light);
    border-color: var(--codex-border);
    background: rgba(139, 105, 20, 0.1);
}

/* ----- Retry Button ----- */
.codex-retry-btn {
    align-self: flex-start;
    background: transparent;
    color: var(--codex-text-muted);
    border: 1px solid var(--codex-border);
    border-radius: 12px;
    padding: 6px 14px;
    font-size: 0.75rem;
    cursor: pointer;
    transition: all var(--codex-transition);
    font-family: 'Poppins', sans-serif;
    margin-top: -2px;
    min-height: 32px;
}

.codex-retry-btn:hover {
    color: var(--codex-text);
    border-color: var(--codex-primary-light);
}

/* ----- Session Tabs ----- */
.codex-session-tabs {
    display: none;
    flex-wrap: nowrap;
    overflow-x: auto;
    gap: 2px;
    padding: 4px 16px;
    border-bottom: 1px solid var(--codex-border);
    background: var(--codex-bg);
}

.codex-session-tab {
    background: transparent !important;
    color: var(--codex-text-muted) !important;
    border: none !important;
    border-bottom: 2px solid transparent !important;
    padding: 4px 12px !important;
    font-size: 0.75rem !important;
    cursor: pointer;
    white-space: nowrap;
    transition: all var(--codex-transition);
    font-family: 'Poppins', sans-serif;
    min-width: unset !important;
    min-height: unset !important;
    height: auto !important;
}

.codex-session-tab.active {
    color: var(--codex-primary-light) !important;
    border-bottom-color: var(--codex-primary-light) !important;
}

.codex-session-tab:hover:not(.active) {
    color: var(--codex-text) !important;
}

/* ----- Drag-to-Resize Handle ----- */
.codex-resize-handle {
    position: absolute;
    left: -3px;
    top: 0;
    bottom: 0;
    width: 6px;
    cursor: col-resize;
    z-index: 1;
}

.codex-resize-handle:hover {
    background: var(--codex-primary-light);
    opacity: 0.3;
}

@media (max-width: 768px) {
    .codex-resize-handle {
        display: none;
    }
}

/* ----- Field Highlight (Codex applied a value) ----- */
@keyframes codex-field-pulse {
    0% { box-shadow: 0 0 0 0 rgba(196, 162, 52, 0.6); }
    50% { box-shadow: 0 0 0 4px rgba(196, 162, 52, 0.3); }
    100% { box-shadow: 0 0 0 0 rgba(196, 162, 52, 0); }
}

.codex-field-highlight {
    animation: codex-field-pulse 1.5s ease !important;
    border-color: var(--codex-primary-light, #C4A234) !important;
}

/* ----- Input Area ----- */
.codex-input-area {
    display: flex;
    gap: 8px;
    padding: 12px 16px;
    border-top: 1px solid var(--codex-border);
    background: var(--codex-bg-light);
}

.codex-input {
    flex: 1;
    background: var(--codex-bg);
    color: var(--codex-text);
    border: 1px solid var(--codex-border);
    border-radius: 8px;
    padding: 8px 12px;
    font-size: 0.9rem;
    font-family: 'Poppins', sans-serif;
    resize: none;
    min-height: 38px;
    max-height: 100px;
    overflow-y: auto;
    transition: border-color var(--codex-transition);
}

.codex-input:focus {
    outline: none;
    border-color: var(--codex-primary-light);
}

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

.codex-send-btn {
    background: var(--codex-primary);
    color: #fff;
    border: none;
    border-radius: 8px;
    width: 44px;
    height: 44px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background var(--codex-transition);
    align-self: flex-end;
}

.codex-send-btn:hover {
    background: var(--codex-primary-light);
}

.codex-send-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

/* ----- Fixed "Ask Codex" Link (bottom-left) ----- */
.codex-ask-link {
    position: fixed;
    bottom: 1.5rem;
    left: 1.5rem;
    z-index: var(--codex-z-icon);
    background: var(--codex-bg, #1a1207);
    color: var(--codex-primary-light, #C4A234);
    border: 1px solid var(--codex-primary-light, #C4A234);
    padding: 8px 16px;
    border-radius: 20px;
    font-size: 0.85rem;
    text-decoration: none;
    cursor: pointer;
    transition: background 0.2s, transform 0.2s;
    font-family: inherit;
}

.codex-ask-link:hover {
    background: var(--codex-primary-light, #C4A234);
    color: var(--codex-bg, #1a1207);
    transform: scale(1.05);
}

.codex-ask-link[data-locked="true"] {
    opacity: 0.4;
    cursor: not-allowed;
}

/* Hide on mobile — grimoire serves as the entry point */
@media (max-width: 768px) {
    .codex-ask-link {
        display: none;
    }
}

/* ----- Image Preview in Chat ----- */
.codex-image-preview {
    margin: 8px 0;
    text-align: center;
}

.codex-image-preview img {
    max-width: 100%;
    max-height: 300px;
    border-radius: 8px;
    border: 1px solid var(--codex-border, #5a4a2a);
}

/* ----- Monster Generate Preview ----- */
.codex-gen-preview {
    font-size: 0.8rem;
    color: var(--codex-text-muted);
    margin: 4px 0;
}

/* ----- Large Screen — wider panel for breathing room ----- */
@media (min-width: 1280px) {
    .codex-panel {
        --codex-panel-width: 420px;
        width: var(--codex-panel-width);
    }

    .codex-messages {
        padding: 20px;
        gap: 14px;
    }
}

/* ----- Tablet — narrower panel so content isn't squeezed ----- */
@media (min-width: 769px) and (max-width: 1024px) {
    .codex-panel {
        --codex-panel-width: 300px;
        width: var(--codex-panel-width);
    }

    .codex-message {
        max-width: 95%;
    }

    .codex-quick-btn {
        font-size: 0.7rem;
        padding: 3px 10px;
    }
}

/* ----- Push Mode (desktop) — shrink page content when panel is open ----- */
@media (min-width: 769px) {
    body.codex-push-active {
        --codex-push-margin: var(--codex-panel-width, 380px);
        width: calc(100vw - var(--codex-push-margin));
        transition: width var(--codex-transition);
    }

    /* Form containers must not extend under the panel */
    body.codex-push-active .step,
    body.codex-push-active .container {
        max-width: 100% !important;
    }

    /* Fixed bars: full-width bg edge-to-edge, pad content clear of panel */
    body.codex-push-active .sticky-rules-toggle,
    body.codex-push-active #progress-bar {
        width: 100vw !important;
        max-width: 100vw !important;
        padding-right: var(--codex-push-margin) !important;
        box-sizing: border-box;
    }

    /* Tighten rules toggle layout so content doesn't wrap with reduced space */
    body.codex-push-active .rules-toggle-wrapper {
        gap: 8px !important;
        flex-wrap: nowrap !important;
    }

    body.codex-push-active .rules-toggle-controls {
        gap: 6px !important;
        flex-wrap: nowrap !important;
    }

    body.codex-push-active .monster-creator-title {
        font-size: 1.2rem !important;
        white-space: nowrap;
    }

    /* Align step nav and monster controls to match form width */
    body.codex-push-active #progress-bar {
        padding-left: 16px !important;
    }

    body.codex-push-active .monster-controls {
        max-width: calc(100% - 32px);
        margin-left: auto;
        margin-right: auto;
    }

    /* Cap panel width on medium screens */
    .codex-panel {
        max-width: min(380px, 30vw);
    }
}

@media (min-width: 769px) and (max-width: 1024px) {
    body.codex-push-active {
        --codex-push-margin: 300px;
        width: calc(100vw - 300px);
    }
}

@media (min-width: 1280px) {
    .codex-panel {
        max-width: 420px;
    }

    body.codex-push-active {
        --codex-push-margin: 420px;
        width: calc(100vw - 420px);
    }
}

/* ----- Mobile — full-screen overlay ----- */
@media (max-width: 768px) {
    .codex-panel.open {
        width: 100vw;
        height: 100vh;
        right: 0;
        top: 0;
        border-radius: 0;
        z-index: 10000;
    }

    .codex-messages {
        padding: 16px;
        gap: 10px;
    }

    .codex-message {
        max-width: 95%;
        font-size: 0.88rem;
    }

    .codex-input-area {
        padding-bottom: env(safe-area-inset-bottom, 8px);
    }

    .codex-grimoire {
        width: 50px;
        height: 50px;
        bottom: 1rem;
        right: 1rem;
    }
}

@media (max-width: 480px) {
    .codex-panel-header {
        padding: 10px 12px;
    }

    .codex-messages {
        padding: 12px;
        gap: 8px;
    }

    .codex-message {
        padding: 8px 12px;
    }

    .codex-input-area {
        padding: 10px 12px;
    }

    .codex-action-card {
        padding: 10px;
    }

    .codex-quick-actions {
        padding: 6px 12px;
        gap: 4px;
    }

    .codex-quick-btn {
        font-size: 0.7rem;
        padding: 3px 10px;
    }
}

/* ----- Reduced Motion ----- */
@media (prefers-reduced-motion: reduce) {
    .codex-grimoire { animation: none !important; }
    .codex-typing-dot { animation: none !important; opacity: 0.6; }
    .codex-message { animation: none !important; }
    .codex-panel { transition: none !important; }
    body.codex-push-active { transition: none !important; }
    .codex-field-highlight { animation: none !important; }
}
