/* =========================================
   File: style.css
   Version: 3.0.0
   Description: Persona Weaver Core Styles
========================================= */

/* --- Main Wrapper --- */
.pw-wrapper {
    /* Core Variables */
    --pw-text-main: var(--smart-theme-body-color); 
    --pw-border: var(--SmartThemeBorderColor);
    --pw-paper-bg: var(--smart-theme-input-bg, rgba(0, 0, 0, 0.05)); 
    --pw-danger: #ff6b6b;
    
    /* Toggle Switch Colors */
    --pw-toggle-bg: rgba(0, 0, 0, 0.1);
    --pw-toggle-active: var(--pw-paper-bg);

    display: flex; flex-direction: column; height: 100%; max-height: 95vh;
    font-size: 14px; 
    color: var(--pw-text-main);
    background-color: transparent;
    position: relative;
    box-sizing: border-box; overflow: hidden;
    font-family: inherit; 
}

/* Force ST popup to use full screen */
.dialogue_popup_large .dialogue_popup_text:has(.pw-wrapper) {
    max-height: none !important;
    height: 100% !important;
    overflow: visible !important;
    padding: 4px !important;
}
.dialogue_popup_large:has(.pw-wrapper) {
    width: 95vw !important;
    max-width: 95vw !important;
    height: 95vh !important;
    max-height: 95vh !important;
    margin: auto !important;
    overflow: visible !important;
}
.dialogue_popup_large:has(.pw-wrapper) .dialogue_popup_controls {
    padding: 4px 8px !important;
}

/* --- Scroll Area --- */
.pw-scroll-area {
    flex-grow: 1; overflow-y: auto; padding-right: 5px;
    display: flex; flex-direction: column; gap: 8px;
    padding-bottom: 10px; min-height: 0;
}
.pw-scroll-area::-webkit-scrollbar { width: 6px; }
.pw-scroll-area::-webkit-scrollbar-track { background: rgba(0,0,0,0.05); }
.pw-scroll-area::-webkit-scrollbar-thumb { background: var(--pw-text-main); opacity: 0.3; border-radius: 3px; }

/* =========================================
   Header & Navigation
========================================= */
.pw-header { flex-shrink: 0; margin-bottom: 8px; border-bottom: 1px solid var(--pw-border); padding-bottom: 5px; }
.pw-top-bar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.pw-title { font-size: 1.2em; font-weight: bold; display: flex; align-items: center; gap: 8px; color: var(--pw-text-main); }
.pw-header-subtitle { font-size: 0.65em; opacity: 0.6; font-weight: normal; margin-left: 10px; color: var(--pw-text-main); }

.pw-tabs { display: flex; flex-direction: row; gap: 5px; overflow-x: auto; }
.pw-tab {
    padding: 6px 12px; cursor: pointer; border-radius: 6px 6px 0 0;
    opacity: 0.6; transition: all 0.2s; white-space: nowrap; 
    border: 1px solid transparent;
    display: flex; align-items: center; gap: 5px; font-size: 0.9em;
    color: var(--pw-text-main);
    background: rgba(128,128,128,0.1);
}
.pw-tab:hover { opacity: 0.9; background: rgba(128,128,128,0.2); }
.pw-tab.active { opacity: 1; background: var(--pw-paper-bg); border-bottom: 2px solid var(--pw-text-main); font-weight: bold; }

.pw-view { display: none; flex-grow: 1; overflow: hidden; flex-direction: column; height: 100%; width: 100%; }
.pw-view.active { display: flex; }

/* =========================================
   General Components
========================================= */
.pw-card-section { 
    background: rgba(128, 128, 128, 0.1); 
    border: 1px solid var(--pw-border); 
    border-radius: 8px; padding: 10px; 
    display: flex; flex-direction: column; gap: 8px; 
}
.pw-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; width: 100%; flex-wrap: wrap; }
.pw-section-label { font-weight: bold; font-size: 1em; padding: 2px 4px; color: var(--pw-text-main); opacity: 0.9; }

/* Info Display & Mode Switcher */
.pw-info-display {
    display: flex; 
    align-items: center; 
    gap: 8px; 
    background: rgba(128,128,128,0.1); 
    padding: 4px; 
    border-radius: 6px; 
    border: 1px solid var(--pw-border); 
    margin-bottom: 5px;
    justify-content: space-between;
    flex-wrap: nowrap;
}

/* Toggle Group Wrapper */
.pw-mode-toggle-group {
    display: flex;
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    align-items: center;
    background: var(--pw-toggle-bg); 
    border-radius: 20px;
    padding: 3px;
    gap: 0;
    flex: 1;
    max-width: 70%;
    box-shadow: inset 0 1px 3px rgba(0,0,0,0.1);
    overflow: hidden;
}

/* Individual Mode Items */
.pw-mode-item {
    flex-grow: 1;
    flex-shrink: 1;
    display: flex; 
    align-items: center; 
    justify-content: center; 
    gap: 6px;
    padding: 6px 12px; 
    border-radius: 18px;
    cursor: pointer;
    font-size: 0.9em; 
    color: var(--pw-text-main); 
    opacity: 0.6; 
    transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    font-weight: bold;
    background: transparent;
    border: 1px solid transparent;
    white-space: nowrap;
    overflow: hidden;
}

.pw-mode-item:hover { opacity: 0.9; }

.pw-mode-item.active {
    flex-grow: 2;
    background: var(--pw-toggle-active);
    opacity: 1;
    box-shadow: 0 1px 3px rgba(0,0,0,0.2);
    border-color: var(--pw-border);
    color: var(--pw-text-main);
    z-index: 1;
}

.pw-info-item { display: flex; align-items: center; gap: 6px; font-weight: bold; color: var(--pw-text-main); font-size: 1.05em; }
#pw-display-name { 
    opacity: 0.9; 
    white-space: nowrap; 
    overflow: hidden; 
    text-overflow: ellipsis; 
    max-width: 100px;
}

.pw-load-btn {
    font-size: 0.85em; 
    background: transparent; 
    border: 1px solid currentColor;
    padding: 5px 12px; 
    border-radius: 4px; 
    cursor: pointer; 
    color: var(--pw-text-main);
    opacity: 0.7; 
    font-weight: normal; 
    margin-left: auto;
    transition: all 0.2s;
    white-space: nowrap; 
    flex-shrink: 0;
}
.pw-load-btn:hover { opacity: 1; background: rgba(128,128,128,0.2); }

/* Inputs */
.pw-input, .pw-textarea, .pw-select {
    background-color: var(--pw-paper-bg); border: 1px solid var(--pw-border);
    color: var(--pw-text-main); border-radius: 6px; padding: 8px;
    font-family: inherit; font-size: inherit; outline: none; box-sizing: border-box;
}
.pw-input:focus, .pw-textarea:focus, .pw-select:focus { border-color: var(--pw-text-main); box-shadow: 0 0 5px rgba(128,128,128,0.2); }
.pw-textarea { resize: vertical; line-height: 1.5; }
#pw-request { min-height: 200px; transition: min-height 0.3s ease, height 0.3s ease; }
#pw-request.minimized { min-height: 80px; height: 80px !important; }
.pw-result-textarea.minimized { min-height: 80px !important; height: 80px !important; }

#pw-greetings-select { flex: 1; width: 100%; max-width: 100%; }

/* Buttons */
.pw-btn {
    display: flex; align-items: center; justify-content: center; gap: 6px;
    padding: 6px 8px;
    border-radius: 6px; border: 1px solid var(--pw-border);
    background: rgba(128,128,128,0.1); color: var(--pw-text-main);
    cursor: pointer; font-weight: bold; width: auto; transition: all 0.2s; font-size: 0.95em;
    white-space: nowrap;
}
.pw-btn:hover { background: rgba(128,128,128,0.25); filter: brightness(1.1); }
.pw-btn.gen {
    background: var(--SmartThemeQuoteColor, #83c168);
    color: #fff; border: none; width: 100%; margin-top: 8px;
    padding: 12px 16px; font-size: 1.05em; font-weight: bold; letter-spacing: 0.5px;
    border-radius: 8px; box-shadow: 0 2px 6px rgba(0,0,0,0.15);
    transition: all 0.2s ease;
}
.pw-btn.gen:hover { filter: brightness(1.1); box-shadow: 0 3px 10px rgba(0,0,0,0.25); transform: translateY(-1px); }
.pw-btn.gen:active { transform: translateY(0); box-shadow: 0 1px 4px rgba(0,0,0,0.15); }
.pw-btn.save { border-color: var(--pw-text-main); color: var(--pw-text-main); }
.pw-btn.danger { border-color: var(--pw-danger); color: var(--pw-danger); background: transparent; }
.pw-btn.danger:hover { background: rgba(255, 107, 107, 0.1); }
.pw-btn.wi { border-color: var(--pw-text-main); color: var(--pw-text-main); opacity: 0.8; }
.pw-mini-btn {
    font-size: 0.85em; opacity: 0.8; cursor: pointer; display: flex; align-items: center; gap: 4px;
    padding: 6px 10px; border-radius: 4px; border: 1px solid var(--pw-border);
    background: rgba(128,128,128,0.1); color: var(--pw-text-main);
}
.pw-mini-btn:hover { opacity: 1; border-color: var(--pw-text-main); }

/* Toggle Switch Button (Debug) */
.pw-toggle-switch {
    border-radius: 50px !important;
    padding: 4px 14px !important;
    transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    border: 1px solid var(--pw-border);
    background: rgba(128,128,128,0.1);
    color: var(--pw-text-main);
    opacity: 0.7;
    cursor: pointer;
    display: inline-flex; align-items: center; gap: 6px;
    font-size: 0.85em; font-weight: bold;
}
.pw-toggle-switch:hover { opacity: 1; }

.pw-toggle-switch.active {
    background: var(--pw-text-main);
    color: var(--smart-theme-bg, #000);
    border-color: transparent;
    opacity: 1;
    box-shadow: 0 2px 5px rgba(0,0,0,0.2);
}

/* =========================================
   Template Editor
========================================= */
.pw-tags-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
.pw-tags-label { 
    font-weight: bold; opacity: 0.8; font-size: 0.85em; color: var(--pw-text-main); 
    cursor: pointer; user-select: none;
}
.pw-tags-label:hover { opacity: 1; }

.pw-tags-edit-toggle { cursor: pointer; font-size: 0.8em; opacity: 0.7; text-decoration: underline; }
.pw-tags-edit-toggle.editing { color: var(--pw-danger) !important; }
.pw-tags-container { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }

/* Chips */
.pw-tag-chip {
    display: inline-flex; align-items: center; background: rgba(128, 128, 128, 0.15);
    border: 1px solid var(--pw-border); 
    border-left: 5px solid var(--pw-text-main); 
    padding: 4px 10px 4px 8px; 
    border-radius: 4px;
    font-size: 0.9em; cursor: pointer; transition: all 0.2s; 
    color: var(--pw-text-main);
    opacity: 0.9;
}
.pw-tag-chip:hover { 
    background: rgba(128, 128, 128, 0.3); 
    transform: translateY(-1px); 
    opacity: 1;
    border-left-color: var(--pw-text-main);
}

.pw-template-editor-area { display: none; flex-direction: column; gap: 0; background: var(--pw-paper-bg); border-radius: 6px; border: 1px solid var(--pw-border); overflow: hidden; }
.pw-template-textarea { width: 100%; min-height: 200px; font-family: monospace; font-size: 0.85em; background: transparent; color: var(--pw-text-main); border: none; resize: vertical; padding: 8px; white-space: pre; outline: none; box-sizing: border-box; transition: min-height 0.3s ease, max-height 0.3s ease; }
.pw-template-textarea.expanded { min-height: 400px; }
.pw-template-textarea.minimized { min-height: 80px; max-height: 80px; overflow-y: auto; }
.pw-template-toolbar { display: flex; justify-content: flex-start; align-items: center; padding: 5px 10px; background: rgba(128,128,128,0.2); border-bottom: 1px solid var(--pw-border); border-radius: 6px 6px 0 0; }
.pw-template-footer { display: flex; justify-content: space-between; align-items: center; background: rgba(128,128,128,0.1); padding: 5px 10px; border-top: 1px solid var(--pw-border); }

/* Shortcuts & Vars */
.pw-shortcut-bar { display: flex; gap: 5px; align-items: center; }
.pw-shortcut-btn, .pw-var-btn { 
    display: inline-flex; flex-direction: column; align-items: center; justify-content: center;
    padding: 5px 2px; min-width: 50px; 
    border: 1px solid var(--pw-border); border-radius: 4px; background: rgba(128,128,128,0.08); 
    color: var(--pw-text-main); cursor: pointer; transition: all 0.2s; user-select: none; line-height: 1.1; opacity: 0.9;
}
.pw-shortcut-btn:hover, .pw-var-btn:hover { opacity: 1; border-color: var(--pw-text-main); background: rgba(128,128,128,0.15); transform: translateY(-1px); }
.pw-shortcut-btn span:first-child, .pw-var-btn span:first-child { font-size: 0.75em; font-weight: bold; opacity: 0.9; margin-bottom: 1px; }
.pw-shortcut-btn .code, .pw-var-btn .code { margin-left: 0; background: transparent; border: none; padding: 0; font-family: Consolas, "Courier New", monospace; font-size: 0.7em; opacity: 0.6; }
.pw-var-btns { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }

/* Result & Refine */
.pw-relative-container { position: relative; }
.pw-result-textarea {
    width: 100%; min-height: 450px; background: var(--pw-paper-bg);
    border: 1px solid var(--pw-border); border-bottom: none; border-radius: 6px 6px 0 0;
    color: var(--pw-text-main); padding: 12px; font-family: inherit; font-size: 1.0em; line-height: 1.6;
    resize: vertical; outline: none; white-space: pre-wrap; margin-bottom: 0;
}
.pw-refine-toolbar { display: flex; flex-direction: row; gap: 0; align-items: stretch; background: rgba(128,128,128,0.1); border: 1px solid var(--pw-border); border-top: none; border-radius: 0 0 6px 6px; margin-bottom: 5px; overflow: hidden; }
.pw-refine-input { flex: 1; border: none; background: transparent; padding: 10px; font-size: 0.95em; color: var(--pw-text-main); resize: none; overflow-y: auto; min-height: 56px; max-height: 56px; line-height: 1.5; }
.pw-refine-btn-vertical { width: 44px; cursor: pointer; background: var(--SmartThemeQuoteColor, #83c168); border-left: none; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 5px; color: #fff; font-weight: bold; transition: all 0.2s; flex-shrink: 0; }
.pw-refine-btn-vertical:hover { filter: brightness(1.1); }
.pw-refine-btn-text { writing-mode: vertical-rl; text-orientation: upright; letter-spacing: 2px; }
.pw-float-quote-btn { position: fixed; top: 40%; right: 0; background: var(--pw-paper-bg); color: var(--pw-text-main); padding: 8px 12px; border-radius: 20px 0 0 20px; font-weight: bold; font-size: 0.85em; box-shadow: -2px 2px 8px rgba(0,0,0,0.4); cursor: pointer; z-index: 9999; display: none; align-items: center; gap: 4px; border: 1px solid var(--pw-border); border-right: none; backdrop-filter: blur(5px); }

/* Load Overlay (Theme-Adaptive) */
.pw-load-overlay-backdrop {
    display: none;
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    z-index: 2001;
    background: var(--SmartThemeBlurTintColor, rgba(0,0,0,0.75));
    backdrop-filter: blur(3px);
    flex-direction: column;
    justify-content: center;
    align-items: center;
}
.pw-load-overlay-card {
    width: 92%;
    max-width: 480px;
    max-height: 75vh;
    background: var(--SmartThemeBlurTintColor, var(--smart-theme-bg, rgba(30,30,30,0.97)));
    border: 1px solid var(--pw-border);
    border-radius: 10px;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    box-shadow: 0 8px 32px rgba(0,0,0,0.5);
    color: var(--pw-text-main);
}
.pw-load-overlay-header {
    padding: 12px 15px;
    border-bottom: 1px solid var(--pw-border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
    background: rgba(128,128,128,0.08);
    font-weight: bold;
    font-size: 1em;
}
.pw-load-overlay-body {
    flex: 1;
    overflow-y: auto;
    padding: 12px 15px;
    min-height: 0;
}
.pw-load-overlay-body .pw-load-choice {
    background: rgba(128,128,128,0.15) !important;
    border: 1px solid var(--pw-border) !important;
    color: var(--pw-text-main) !important;
}
.pw-load-overlay-body .pw-load-choice:hover:not(:disabled) {
    background: rgba(128,128,128,0.3) !important;
}
.pw-load-overlay-body .pw-input {
    background: var(--pw-paper-bg);
    color: var(--pw-text-main);
    border-color: var(--pw-border);
}

/* World Info */
.pw-wi-book { border: 1px solid var(--pw-border); border-radius: 6px; margin-bottom: 6px; overflow: hidden; }
.pw-wi-header { padding: 8px; background: rgba(128,128,128,0.1); cursor: pointer; display: flex; justify-content: space-between; align-items: center; color: var(--pw-text-main); }
.pw-wi-header-checkbox {
    margin-right: 8px;
    transform: scale(1.2);
    cursor: pointer;
    position: relative;
    -webkit-appearance: none;
    appearance: none;
    width: 14px;
    height: 14px;
    border: 2px solid rgba(180,180,180,0.7);
    border-radius: 3px;
    background: transparent;
    vertical-align: middle;
    flex-shrink: 0;
}
.pw-wi-header-checkbox:checked {
    background: cornflowerblue;
    border-color: cornflowerblue;
}
.pw-wi-header-checkbox:checked::after {
    content: '';
    position: absolute;
    left: 2px;
    top: -1px;
    width: 5px;
    height: 9px;
    border: solid #fff;
    border-width: 0 2px 2px 0;
    transform: rotate(45deg);
}
.pw-wi-header-checkbox.pw-indeterminate {
    background: rgba(128,128,128,0.35);
    border-color: rgba(180,180,180,0.7);
}
.pw-wi-header-checkbox.pw-indeterminate::after {
    content: '';
    position: absolute;
    left: 2px;
    top: 4px;
    width: 6px;
    height: 2px;
    background: #fff;
    border-radius: 1px;
}
.pw-wi-book-title { flex: 1; display: block; overflow: visible; text-overflow: clip; white-space: normal; word-break: break-word; margin-right: 10px; font-weight: bold; min-width: 0; line-height: 1.2; text-align: left; }
.pw-wi-title-text { flex: 1; display: flex; align-items: center; font-weight: bold; font-size: 0.9em; text-align: left; white-space: normal; line-height: 1.3; }
.pw-wi-header-actions { display: flex; align-items: center; gap: 12px; flex-shrink: 0; }
.pw-wi-controls { display: flex; gap: 6px; width: 100%; align-items: center; flex-wrap: nowrap !important; }
.pw-wi-select { flex-grow: 1; width: 100%; min-width: 0; }
.pw-wi-add-btn { flex-shrink: 0; padding: 6px 12px; width: auto; white-space: nowrap; }

/* Filter Tools */
.pw-wi-depth-tools { display: none; flex-direction: column; gap: 8px; padding: 10px; background: rgba(0,0,0,0.1); border-bottom: 1px solid var(--pw-border); }
.pw-wi-filter-row { display: flex; align-items: center; gap: 8px; flex-wrap: nowrap; width: 100%; }
.pw-keyword-input { width: 100%; padding: 6px; font-size: 0.9em; }
.pw-pos-select { flex: 1; padding: 6px; min-width: 0; }
.pw-depth-input { width: 50px; text-align: center; padding: 6px; }
.pw-depth-btn { padding: 6px 12px; background: var(--pw-paper-bg); border: 1px solid var(--pw-border); color: var(--pw-text-main); border-radius: 4px; cursor: pointer; flex: 1; text-align: center; font-size: 0.9em; }
.pw-depth-btn.active { border-color: var(--pw-danger) !important; color: var(--pw-danger) !important; background: rgba(255, 107, 107, 0.1) !important; }
.pw-wi-list { display: none; padding: 8px; border-top: 1px solid var(--pw-border); }
.pw-wi-item { background: rgba(128,128,128,0.05); padding: 6px 10px; border-radius: 4px; margin-bottom: 4px; }
.pw-wi-item-row { display: flex; align-items: center; gap: 8px; }
.pw-wi-check { transform: scale(1.1); cursor: pointer; margin-right: 8px; } 
.pw-wi-toggle-icon { color: var(--pw-text-main); opacity: 0.5; cursor: pointer; margin-left: 8px; flex-shrink: 0;}
.pw-wi-toggle-icon.active { opacity: 1; }
.pw-remove-book-icon { color: var(--pw-danger); cursor: pointer; transition: transform 0.2s; }
.pw-remove-book-icon:hover { transform: scale(1.1); }
.pw-bound-status { color: var(--pw-text-main) !important; opacity: 0.6; font-size: 0.8em; margin-left: 5px; font-weight: bold; }
.pw-wi-desc, #pw-greetings-preview { display: none; background: rgba(128,128,128,0.1) !important; color: var(--pw-text-main) !important; border: 1px solid var(--pw-border); border-radius: 4px; padding: 8px; font-size: 0.9em; width: 100%; box-sizing: border-box; resize: vertical; line-height: 1.5; margin-top: 5px; text-align: left; white-space: pre-wrap; }
.pw-wi-info-badge { font-size: 0.65em; background: rgba(128,128,128,0.1); padding: 1px 4px; border-radius: 3px; color: var(--pw-text-main); opacity: 0.4; margin-right: 5px; white-space: nowrap; vertical-align: middle; }

/* History */
.pw-search-box { position: relative; display: flex; align-items: center; margin-bottom: 10px; background: rgba(128,128,128,0.1); border-radius: 6px; border: 1px solid var(--pw-border); width: 100%; }
.pw-search-icon { position: absolute; left: 10px; opacity: 0.5; pointer-events: none; color: var(--pw-text-main); }
.pw-search-input { width: 100%; padding-left: 32px !important; padding-right: 30px; border: none; background: transparent; color: var(--pw-text-main); box-sizing: border-box; }
.pw-search-clear { position: absolute; right: 10px; opacity: 0.5; cursor: pointer; z-index: 2; color: var(--pw-text-main); }
.pw-history-item { background: rgba(128,128,128,0.1); border: 1px solid var(--pw-border); border-radius: 6px; padding: 8px; margin-bottom: 6px; display: flex; gap: 8px; cursor: pointer; transition: background 0.2s; }
.pw-history-item:hover { background: rgba(128,128,128,0.2); }
.pw-hist-main { flex: 1; display: flex; flex-direction: column; gap: 3px; overflow: hidden; }
.pw-hist-header { display: flex; align-items: center; justify-content: space-between; gap: 5px; width: 100%; }
.pw-hist-title-display { font-weight: bold; color: var(--pw-text-main); font-size: 1.0em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; }
.pw-hist-title-input { background: rgba(0,0,0,0.1); border: 1px solid var(--pw-text-main); color: inherit; font-size: 1.0em; padding: 2px 4px; border-radius: 3px; width: 100%; box-sizing: border-box; }
.pw-hist-meta { font-size: 0.75em; opacity: 0.6; color: var(--pw-text-main); }
.pw-hist-desc { font-size: 0.85em; opacity: 0.8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--pw-text-main); }
.pw-hist-action-btn { opacity: 0.5; cursor: pointer; padding: 2px 5px; font-size: 0.85em; color: var(--pw-text-main); }
.pw-hist-action-btn:hover { opacity: 1; }
.pw-hist-action-btn.del:hover { color: var(--pw-danger); }
.pw-pagination { display: flex; justify-content: center; align-items: center; gap: 10px; margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--pw-border); }
.pw-page-btn { padding: 4px 10px; background: rgba(128,128,128,0.1); border: 1px solid var(--pw-border); border-radius: 4px; cursor: pointer; color: var(--pw-text-main); font-size: 0.9em; }
.pw-page-btn:disabled { opacity: 0.3; cursor: default; }
.pw-page-info { font-size: 0.9em; opacity: 0.8; }
.pw-badge { display: inline-block; padding: 2px 5px; border-radius: 3px; font-size: 0.7em; margin-right: 5px; font-weight: bold; text-transform: uppercase; vertical-align: middle; }
.pw-badge.persona { background-color: rgba(65, 150, 255, 0.2); color: #88c0ff; border: 1px solid rgba(65, 150, 255, 0.4); }
.pw-badge.template { background-color: rgba(255, 165, 0, 0.2); color: #ffbc42; border: 1px solid rgba(255, 165, 0, 0.4); }

/* =========================================
   Diff / Refine View (Theme-Adaptive)
========================================= */
.pw-diff-container {
    background-color: var(--SmartThemeBlurTintColor, var(--smart-theme-bg, rgba(20,20,20,0.95)));
    color: var(--pw-text-main);
    padding: 0;
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    z-index: 2000;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
}

.pw-diff-toolbar {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    gap: 6px;
    padding: 6px 10px;
    background: rgba(0,0,0,0.12);
    border-bottom: 1px solid var(--pw-border);
    flex-shrink: 0;
}

.pw-diff-hint-inline {
    font-size: 0.8em;
    color: #d68b1c;
    opacity: 0.9;
    white-space: nowrap;
    display: flex;
    align-items: center;
    gap: 4px;
}

.pw-diff-mode-btn {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 4px 10px;
    border-radius: 3px;
    border: 1px solid var(--pw-border);
    background: rgba(0,0,0,0.05);
    color: var(--pw-text-main);
    cursor: pointer;
    font-size: 0.8em;
    font-weight: bold;
    white-space: nowrap;
    transition: 0.2s;
    opacity: 0.6;
}
.pw-diff-mode-btn:hover { opacity: 0.9; background: rgba(128,128,128,0.15); }
.pw-diff-mode-btn.active {
    background: var(--SmartThemeQuoteColor, #5b8db8) !important;
    color: #fff !important;
    border-color: transparent !important;
    opacity: 1;
}

.pw-diff-content-area {
    flex: 1;
    overflow: hidden;
    padding: 15px;
    background-color: var(--pw-paper-bg);
    text-align: left;
}

/* (old card-based diff styles removed — replaced by inline LCS diff) */

.pw-diff-actions {
    background: rgba(0,0,0,0.1);
    padding: 10px 15px;
    border-top: 1px solid var(--pw-border);
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}
.pw-diff-actions .pw-btn {
    font-size: 0.8em;
    padding: 6px 12px;
}

#pw-diff-confirm.pw-btn.gen { margin-top: 0; }
#pw-diff-cancel { background: transparent; border: 1px solid var(--pw-danger); color: var(--pw-danger); }
#pw-diff-cancel:hover { background: rgba(255, 107, 107, 0.1); }

/* =========================================
   Inline LCS Diff View + Sub-view Modes
========================================= */
.pw-diff-merge-view { overflow-y: auto; height: 100%; padding-right: 5px; }
#pw-diff-merge-list {
    line-height: 1.8;
    word-break: break-word;
    overflow-wrap: break-word;
    white-space: pre-wrap;
    font-size: 0.95em;
    color: var(--pw-text-main);
    text-align: left;
}

.pw-diff-group { display: inline; }

.pw-idiff-old, .pw-idiff-new {
    cursor: pointer;
    border-radius: 2px;
    padding: 0 1px;
    transition: background 0.15s, opacity 0.15s;
    display: inline;
    -webkit-box-decoration-break: clone;
    box-decoration-break: clone;
}

.pw-idiff-old { color: #d9534f; background: rgba(217,83,79,0.08); }
.pw-idiff-new { color: #5cb85c; background: rgba(92,184,92,0.08); }

.pw-idiff-old.inactive, .pw-idiff-new.inactive {
    text-decoration: line-through;
    opacity: 0.3;
    background: transparent;
    cursor: pointer;
}
.pw-idiff-old.inactive:hover, .pw-idiff-new.inactive:hover { opacity: 0.55; }

.pw-idiff-old.active {
    background: rgba(217,83,79,0.15);
    color: #d9534f;
    border-bottom: 1px dashed rgba(217,83,79,0.5);
}
.pw-idiff-new.active {
    background: rgba(92,184,92,0.15);
    color: #5cb85c;
    border-bottom: 1px dashed rgba(92,184,92,0.5);
}

#pw-diff-merge-list[contenteditable="true"] { cursor: text; outline: none; }
.pw-idiff-old.active,
.pw-idiff-new.active,
.pw-idiff-equal {
    cursor: text;
}
.pw-idiff-equal:focus {
    outline: none;
}

.pw-idiff-old.active:focus,
.pw-idiff-new.active:focus {
    outline: none;
    background: rgba(128,128,128,0.12);
    border-bottom-style: solid;
}

/* Sub-view: Old Only — hide new, show old as plain */
.pw-diff-mode-old .pw-idiff-new { display: none; }
.pw-diff-mode-old .pw-idiff-old {
    background: none; color: inherit; padding: 0; border: none;
    text-decoration: none; opacity: 1; pointer-events: none; cursor: default;
}

/* Sub-view: New Only — hide old, show new as plain */
.pw-diff-mode-new .pw-idiff-old { display: none; }
.pw-diff-mode-new .pw-idiff-new {
    background: none; color: inherit; padding: 0; border: none;
    text-decoration: none; opacity: 1; pointer-events: none; cursor: default;
}

/* Sub-view: Final Preview — hide inactive, show active as plain */
.pw-diff-mode-final .pw-idiff-old.inactive,
.pw-diff-mode-final .pw-idiff-new.inactive { display: none; }
.pw-diff-mode-final .pw-idiff-old.active,
.pw-diff-mode-final .pw-idiff-new.active {
    background: none; color: inherit; padding: 0; border: none;
    text-decoration: none; opacity: 1; pointer-events: none; cursor: default;
}

/* =========================================
   System & Footer
========================================= */
.pw-footer { margin-top: auto; padding: 8px 4px; border-top: 1px solid var(--pw-border); display: flex; justify-content: space-between; align-items: center; gap: 4px; flex-shrink: 0; background: rgba(128,128,128,0.1); }
.pw-footer-group { display: flex; gap: 4px; align-items: center; }
.pw-compact-btn { width: 32px; height: 32px; border-radius: 4px; border: 1px solid var(--pw-border); background: transparent; color: var(--pw-text-main); display: flex; align-items: center; justify-content: center; cursor: pointer; font-size: 1em; opacity: 0.7; transition: all 0.2s; }
.pw-compact-btn:hover { opacity: 1; background: rgba(128,128,128,0.2); }
#pw_persona_tool_btn { color: var(--smart-theme-body-color); cursor: pointer; display: flex; align-items: center; justify-content: center; width: 30px; height: 30px; margin-right: 5px; font-size: 1.1em; opacity: 0.7; transition: opacity 0.2s; }
#pw_persona_tool_btn:hover { opacity: 1; color: var(--pw-text-main); }
.pw-context-header { padding: 8px 12px; background: var(--SmartThemeBtnBg); color: var(--SmartThemeBtnText); border: 1px solid var(--SmartThemeBorderColor); border-radius: 6px; cursor: pointer; display: flex; justify-content: space-between; align-items: center; font-weight: bold; margin-bottom: 5px; text-align: left; font-size: 0.85em; }
.pw-context-header:hover { background: var(--SmartThemeBtnHoverBg); }
.pw-context-header i { margin-right: 6px; }
.swal2-actions { flex-wrap: nowrap !important; }
.swal2-styled { white-space: nowrap !important; width: auto !important; max-width: none !important; flex-shrink: 0 !important; }

/* =========================================
   Responsive Layout
========================================= */
@media screen and (max-width: 600px) {
    /* Full screen popup on mobile */
    .dialogue_popup_large:has(.pw-wrapper) {
        width: 100vw !important; max-width: 100vw !important;
        height: 100vh !important; max-height: 100vh !important;
        border-radius: 0 !important; margin: 0 !important;
        top: 0 !important; left: 0 !important;
    }
    .pw-wrapper { max-height: 100vh; border-radius: 0; }

    .pw-row { flex-wrap: wrap; }
    .pw-row label { width: 100%; margin-bottom: 4px; text-align: left; display: block; }
    .pw-input, .pw-select, #pw-api-url, #pw-api-key { min-width: 0 !important; width: 100% !important; flex: 1 1 auto; }
    .pw-wi-depth-tools { gap: 5px; }
    .pw-wi-depth-tools .pw-keyword-input, .pw-wi-depth-tools .pw-pos-select, .pw-wi-depth-tools .pw-depth-input, .pw-wi-depth-tools .pw-depth-btn { height: 30px !important; box-sizing: border-box !important; vertical-align: middle; }
    .pw-keyword-input { width: 100%; font-size: 0.8em; padding: 4px 6px; }
    #d-filter-toggle { font-size: 0.8em; padding: 4px 8px; }
    .pw-wi-filter-row:nth-child(2) { flex-wrap: nowrap !important; gap: 4px; align-items: center; }
    .pw-pos-select { flex: 0 0 85px !important; max-width: 85px !important; padding: 4px 2px; font-size: 0.8em; margin-bottom: 0; }
    .pw-depth-input { flex: 1; min-width: 0; padding: 4px 2px; text-align: center; font-size: 0.8em; }
    .pw-wi-filter-row span { font-size: 0.8em; align-self: center; }
    #d-reset { margin-left: 2px; padding: 4px 6px; font-size: 0.8em; white-space: nowrap; }
    .pw-footer { flex-wrap: wrap; }

    /* Avatar grid: fixed 3 per row on mobile */
    .pw-avatar-mgmt-grid { grid-template-columns: repeat(3, 1fr); gap: 8px; }
    .pw-avatar-card { max-width: none; }
    .pw-avatar-card-img { width: 100%; height: auto; aspect-ratio: 1; }
    .pw-avatar-card-top { width: 100%; height: auto; aspect-ratio: 1; }
    .pw-avatar-card-name { max-width: 100%; }

    /* Chat history settings: wrap message range */
    #pw-chat-history-body .pw-row { flex-wrap: wrap !important; }
    #pw-chat-history-body .pw-row > label { width: auto; flex-shrink: 0; }
    #pw-chat-preset { width: 100% !important; flex: 1 1 auto !important; }
    #pw-chat-custom-range { width: 100%; justify-content: center; }
    #pw-chat-custom-range input { width: 60px !important; }

    /* Diff toolbar: compact on mobile */
    .pw-diff-toolbar { flex-wrap: wrap; gap: 4px; padding: 6px 8px; }
    .pw-diff-mode-btn { padding: 3px 8px; font-size: 0.75em; }
    .pw-diff-hint-inline { font-size: 0.7em; }
    .pw-diff-actions { flex-wrap: wrap; gap: 6px; padding: 8px 10px; }
    .pw-diff-actions .pw-btn { flex: 1; min-width: 70px; font-size: 0.72em; padding: 5px 8px; }

    /* System page: theme buttons row */
    #pw-view-system .pw-row > div[style*="display:flex"] { flex-wrap: wrap; }
}

/* =============================================== */
/* Chat History Reference Section                  */
/* =============================================== */
#pw-chat-history-section {
    transition: border-color 0.2s;
}

#pw-chat-history-body {
    display: none;
    flex-direction: column !important;
    gap: 8px;
    animation: pw-fade-in 0.2s ease;
}

@keyframes pw-fade-in {
    from { opacity: 0; transform: translateY(-4px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Token Badge */
.pw-chat-token-badge {
    font-size: 0.72em;
    padding: 2px 8px;
    border-radius: 10px;
    font-family: monospace;
    letter-spacing: 0.3px;
    white-space: nowrap;
    margin-left: auto;
    transition: all 0.2s;
}

/* Custom Range Inputs */
#pw-chat-custom-range {
    display: flex;
    align-items: center;
    gap: 4px;
}
#pw-chat-custom-range input[type="number"] {
    -moz-appearance: textfield;
    appearance: textfield;
}
#pw-chat-custom-range input[type="number"]::-webkit-inner-spin-button,
#pw-chat-custom-range input[type="number"]::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

/* Filter Section */
.pw-chat-filter-section {
    border: 1px solid var(--SmartThemeBorderColor);
    border-radius: 6px;
    overflow: hidden;
}
.pw-chat-filter-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 10px;
    cursor: pointer;
    user-select: none;
    transition: background 0.15s;
}
.pw-chat-filter-header:hover {
    background: rgba(100,149,237,0.06);
}
#pw-chat-filter-body {
    padding: 6px 10px;
    border-top: 1px solid var(--SmartThemeBorderColor);
    flex-direction: column;
    gap: 6px;
}

/* Tag Chips */
.pw-chat-tag-chip {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 0.78em;
    border: 1px solid var(--SmartThemeBorderColor);
    background: rgba(128,128,128,0.08);
    transition: all 0.15s;
    cursor: pointer;
    user-select: none;
}
.pw-chat-tag-chip:hover {
    border-color: rgba(100,149,237,0.5);
    background: rgba(100,149,237,0.08);
}
.pw-chat-tag-text {
    cursor: pointer;
}
.pw-chat-tag-del {
    cursor: pointer;
    opacity: 0.5;
    font-size: 0.85em;
    transition: opacity 0.15s;
}
.pw-chat-tag-del:hover {
    opacity: 1;
    color: #ff6b6b;
}
.pw-chat-tag-include {
    border-color: rgba(92,184,92,0.5);
    background: rgba(92,184,92,0.1);
    color: #5cb85c;
}
.pw-chat-tag-include .pw-chat-tag-text i { color: #5cb85c; }
.pw-chat-tag-exclude {
    border-color: rgba(255,107,107,0.4);
    background: rgba(255,107,107,0.08);
    color: #ff6b6b;
}
.pw-chat-tag-exclude .pw-chat-tag-text i { color: #ff6b6b; }

/* Chat Preview Area */
#pw-chat-preview-area {
    font-family: var(--mainFontFamily, sans-serif);
    word-break: break-word;
    scrollbar-width: thin;
    scrollbar-color: rgba(128,128,128,0.3) transparent;
}
#pw-chat-preview-area::-webkit-scrollbar { width: 5px; }
#pw-chat-preview-area::-webkit-scrollbar-thumb { background: rgba(128,128,128,0.3); border-radius: 3px; }

/* Context Row (unified for avatar ref + chat infer on main view) */
.pw-context-row {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 10px;
    margin: 4px 0;
    border-radius: 8px;
    font-size: 0.82em;
    background: var(--pw-paper-bg);
    border: 1px solid var(--pw-border);
    transition: all 0.2s;
    opacity: 0.55;
    cursor: pointer;
}
.pw-context-row.active {
    opacity: 1;
    border-color: var(--SmartThemeQuoteColor, var(--pw-border));
    box-shadow: 0 0 0 1px var(--SmartThemeQuoteColor, var(--pw-border)), 0 2px 8px rgba(128,128,128,0.15);
}
.pw-context-row-label {
    user-select: none;
    white-space: nowrap;
    color: var(--pw-text-main);
    flex-shrink: 0;
    font-weight: bold;
    font-size: 0.92em;
}
.pw-context-row-right {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-left: auto;
    flex-shrink: 0;
    cursor: pointer;
}
.pw-context-row-hint {
    font-size: 0.88em;
    opacity: 0.6;
    white-space: nowrap;
}
.pw-avatar-strip {
    display: flex;
    align-items: center;
    gap: 10px;
    overflow-x: auto;
    overflow-y: hidden;
    flex: 1;
    min-width: 0;
    padding: 4px 0 4px 8px;
    scrollbar-width: thin;
    scrollbar-color: rgba(128,128,128,0.3) transparent;
    touch-action: pan-x;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
}
.pw-avatar-strip::-webkit-scrollbar { height: 4px; }
.pw-avatar-strip::-webkit-scrollbar-thumb { background: rgba(128,128,128,0.3); border-radius: 2px; }
.pw-avatar-strip-img {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    object-fit: cover;
    border: 2px dashed rgba(128,128,128,0.3);
    cursor: pointer;
    flex-shrink: 0;
    transition: border-color 0.15s, box-shadow 0.15s, transform 0.1s, opacity 0.15s;
    opacity: 0.5;
    filter: grayscale(30%);
}
.pw-avatar-strip-img:hover {
    opacity: 0.85;
    border-style: solid;
    border-color: rgba(128,128,128,0.5);
    transform: scale(1.08);
    filter: none;
}
.pw-avatar-strip-img.selected {
    border: 3px solid var(--SmartThemeQuoteColor, #83c168);
    border-style: solid;
    box-shadow: 0 0 0 2px var(--SmartThemeQuoteColor, #83c168), 0 2px 8px rgba(128,128,128,0.3);
    opacity: 1;
    filter: none;
    transform: scale(1.05);
}
.pw-avatar-add-btn {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: 2px dashed var(--pw-border);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    opacity: 0.4;
    flex-shrink: 0;
    font-size: 0.8em;
    color: var(--pw-text-main);
    transition: opacity 0.15s, border-color 0.15s;
}
.pw-avatar-add-btn:hover {
    opacity: 0.8;
    border-color: var(--SmartThemeQuoteColor, #83c168);
}
.pw-context-badge {
    display: none;
    min-width: 16px;
    height: 16px;
    padding: 0 4px;
    border-radius: 8px;
    background: var(--SmartThemeQuoteColor, #83c168);
    color: #fff;
    font-size: 0.65em;
    font-weight: bold;
    line-height: 16px;
    text-align: center;
    flex-shrink: 0;
    margin-left: 4px;
}
.pw-context-badge.visible { display: inline-block; }

/* Avatar Management (Context Tab) */
.pw-avatar-mgmt-body {
    max-height: 240px;
    overflow-y: auto;
    overflow-x: hidden;
    scrollbar-width: thin;
    scrollbar-color: rgba(128,128,128,0.3) transparent;
    touch-action: pan-y;
    overscroll-behavior-x: none;
}
.pw-avatar-mgmt-body::-webkit-scrollbar { width: 4px; }
.pw-avatar-mgmt-body::-webkit-scrollbar-thumb { background: rgba(128,128,128,0.3); border-radius: 2px; }
.pw-avatar-mgmt-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(72px, 1fr));
    gap: 10px;
    padding: 4px 0;
    justify-items: center;
    overflow-x: hidden;
}
.pw-avatar-card {
    width: 100%;
    max-width: 90px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 3px;
    position: relative;
}
.pw-avatar-card-top {
    position: relative;
    width: 100%;
    aspect-ratio: 1;
}
.pw-avatar-card-img {
    width: 100%;
    height: 100%;
    border-radius: 8px;
    object-fit: cover;
}
.pw-avatar-card-del {
    position: absolute;
    top: -4px;
    right: -4px;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--pw-danger, #e74c3c);
    color: #fff;
    font-size: 0.6em;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.15s;
}
.pw-avatar-card:hover .pw-avatar-card-del { opacity: 1; }
.pw-avatar-card-name {
    font-size: 0.7em;
    color: var(--pw-text-main);
    opacity: 0.7;
    text-align: center;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 72px;
    cursor: pointer;
}
.pw-avatar-card-name:hover { opacity: 1; text-decoration: underline; }
.pw-avatar-card-tags {
    display: flex;
    gap: 3px;
}
.pw-avatar-tag {
    font-size: 0.72em;
    padding: 1px 8px;
    border-radius: 10px;
    border: 1px solid rgba(128,128,128,0.3);
    cursor: pointer;
    user-select: none;
    color: var(--pw-text-main);
    opacity: 0.4;
    transition: all 0.15s;
}
.pw-avatar-tag:hover { opacity: 0.7; }
.pw-avatar-tag.active {
    opacity: 1;
    border-color: var(--SmartThemeQuoteColor, #83c168);
    background: rgba(128,128,128,0.1);
    color: var(--SmartThemeQuoteColor, #83c168);
}
/* Scan Results Container */
#pw-chat-scan-results {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    max-height: 100px;
    overflow-y: auto;
}

/* Responsive adjustments */
@media (max-width: 600px) {
    #pw-chat-history-section { padding: 8px; }
    .pw-chat-token-badge { font-size: 0.65em; }
    #pw-chat-custom-range input { width: 55px !important; }
    .pw-avatar-strip-img { width: 32px; height: 32px; }
    .pw-avatar-add-btn { width: 28px; height: 28px; }
    .pw-context-row { padding: 6px 8px; font-size: 0.9em; }
}
