/**
 * RichTextEditor Enhanced CSS - 功能強化版樣式
 * @version 1.2.0
 * @author Claude
 */

/* CSS 變數定義 */
:root {
    --rte-primary-color: #007bff;
    --rte-secondary-color: #6c757d;
    --rte-success-color: #28a745;
    --rte-danger-color: #dc3545;
    --rte-warning-color: #ffc107;
    --rte-info-color: #17a2b8;
    --rte-light-color: #f8f9fa;
    --rte-dark-color: #343a40;
    --rte-border-color: #dee2e6;
    --rte-border-radius: 4px;
    --rte-font-family: '微軟正黑體', 'Microsoft JhengHei', Arial, sans-serif;
    --rte-font-size: 14px;
    --rte-line-height: 1.5;
    --rte-shadow: 0 2px 4px rgba(0,0,0,0.1);
    --rte-shadow-lg: 0 4px 8px rgba(0,0,0,0.15);
}

/* 基礎容器樣式 */
.rte-container {
    font-family: var(--rte-font-family);
    font-size: var(--rte-font-size);
    line-height: var(--rte-line-height);
    border: 1px solid var(--rte-border-color);
    border-radius: var(--rte-border-radius);
    background: #fff;
    box-shadow: var(--rte-shadow);
    overflow: hidden;
}

/* 工具列樣式 */
.rte-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    padding: 8px;
    background: var(--rte-light-color);
    border-bottom: 1px solid var(--rte-border-color);
    gap: 4px;
}

.rte-toolbar-group {
    display: flex;
    align-items: center;
    gap: 2px;
    margin-right: 8px;
    padding-right: 8px;
    border-right: 1px solid var(--rte-border-color);
}

.rte-toolbar-group:last-child {
    border-right: none;
    margin-right: 0;
}

/* 按鈕樣式 */
.rte-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 6px 8px;
    border: 1px solid transparent;
    border-radius: var(--rte-border-radius);
    background: transparent;
    color: var(--rte-dark-color);
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s ease;
    min-width: 32px;
    height: 32px;
}

.rte-btn:hover {
    background: rgba(0,0,0,0.05);
    border-color: var(--rte-border-color);
}

.rte-btn:active,
.rte-btn.active {
    background: var(--rte-primary-color);
    color: white;
    border-color: var(--rte-primary-color);
}

.rte-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.rte-btn-primary {
    background: var(--rte-primary-color);
    color: white;
    border-color: var(--rte-primary-color);
    padding: 8px 16px;
    font-weight: 500;
}

.rte-btn-primary:hover:not(:disabled) {
    background: #0056b3;
    border-color: #0056b3;
}

.rte-btn-secondary {
    background: var(--rte-secondary-color);
    color: white;
    border-color: var(--rte-secondary-color);
    padding: 8px 16px;
}

.rte-btn-secondary:hover:not(:disabled) {
    background: #545b62;
    border-color: #545b62;
}

/* 選擇器樣式 */
.rte-select {
    padding: 4px 8px;
    border: 1px solid var(--rte-border-color);
    border-radius: var(--rte-border-radius);
    background: white;
    font-size: 13px;
    cursor: pointer;
    min-width: 80px;
    height: 32px;
    position: relative;
    z-index: 100;
}

.rte-select:focus {
    outline: none;
    border-color: var(--rte-primary-color);
    box-shadow: 0 0 0 2px rgba(0,123,255,0.25);
    z-index: 101;
}

.rte-select option {
    padding: 4px 8px;
    background: white;
    color: var(--rte-dark-color);
}

.rte-font-select {
    min-width: 120px;
    max-width: 150px;
}

.rte-size-select {
    min-width: 60px;
    max-width: 80px;
}

/* 顏色選擇器 */
.rte-color-dropdown {
    position: relative;
    display: inline-block;
    margin: 0 2px;
}

.rte-color-btn {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 4px 6px;
    border: 1px solid var(--rte-border-color);
    border-radius: var(--rte-border-radius);
    background: white;
    cursor: pointer;
    transition: all 0.2s ease;
}

.rte-color-btn:hover {
    border-color: var(--rte-primary-color);
    background: #f8f9fa;
}

.rte-current-color {
    width: 16px;
    height: 16px;
    border: 1px solid #ddd;
    border-radius: 2px;
    display: inline-block;
}

.rte-bg-color {
    background-image: 
        linear-gradient(45deg, #ddd 25%, transparent 25%), 
        linear-gradient(-45deg, #ddd 25%, transparent 25%), 
        linear-gradient(45deg, transparent 75%, #ddd 75%), 
        linear-gradient(-45deg, transparent 75%, #ddd 75%);
    background-size: 8px 8px;
    background-position: 0 0, 0 4px, 4px -4px, -4px 0;
}

.rte-color-palette {
    position: absolute;
    top: 100%;
    left: 0;
    background: white;
    border: 1px solid var(--rte-border-color);
    border-radius: var(--rte-border-radius);
    box-shadow: var(--rte-shadow);
    display: none;
    grid-template-columns: repeat(6, 1fr);
    gap: 2px;
    padding: 8px;
    z-index: 1000;
    min-width: 150px;
}

.rte-color-dropdown.active .rte-color-palette {
    display: grid;
}

.rte-color-option {
    width: 20px;
    height: 20px;
    border: 1px solid #ddd;
    border-radius: 3px;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 10px;
    color: #666;
}

.rte-color-option:hover {
    border-color: var(--rte-primary-color);
    transform: scale(1.1);
}

.rte-transparent {
    background-image: 
        linear-gradient(45deg, #ddd 25%, transparent 25%), 
        linear-gradient(-45deg, #ddd 25%, transparent 25%), 
        linear-gradient(45deg, transparent 75%, #ddd 75%), 
        linear-gradient(-45deg, transparent 75%, #ddd 75%);
    background-size: 6px 6px;
    background-position: 0 0, 0 3px, 3px -3px, -3px 0;
}

/* 編輯器內容區域 */
.rte-content {
    position: relative;
}

.rte-editor {
    min-height: 200px;
    max-height: 600px;
    padding: 16px;
    border: none;
    outline: none;
    overflow-y: auto;
    line-height: 1.6;
    font-size: 14px;
    background: white;
}

.rte-editor:empty:before {
    content: attr(data-placeholder);
    color: #aaa;
    font-style: italic;
    pointer-events: none;
}

.rte-editor:focus {
    background: #fafafa;
}

/* 編輯器內容格式化 */
.rte-editor h1, .rte-editor h2, .rte-editor h3,
.rte-editor h4, .rte-editor h5, .rte-editor h6 {
    margin: 16px 0 8px 0;
    font-weight: bold;
}

.rte-editor h1 { font-size: 2em; }
.rte-editor h2 { font-size: 1.5em; }
.rte-editor h3 { font-size: 1.3em; }
.rte-editor h4 { font-size: 1.1em; }
.rte-editor h5 { font-size: 1em; }
.rte-editor h6 { font-size: 0.9em; }

.rte-editor p {
    margin: 8px 0;
}

.rte-editor blockquote {
    margin: 16px 0;
    padding: 12px 16px;
    border-left: 4px solid var(--rte-primary-color);
    background: #f8f9fa;
    font-style: italic;
}

.rte-editor code {
    padding: 2px 4px;
    background: #f1f3f4;
    border-radius: 3px;
    font-family: 'Courier New', monospace;
    font-size: 0.9em;
}

.rte-editor pre {
    margin: 16px 0;
    padding: 12px;
    background: #f8f9fa;
    border: 1px solid var(--rte-border-color);
    border-radius: var(--rte-border-radius);
    overflow-x: auto;
}

.rte-editor pre code {
    background: none;
    padding: 0;
}

.rte-editor table {
    width: 100%;
    border-collapse: collapse;
    margin: 16px 0;
}

.rte-editor table th,
.rte-editor table td {
    padding: 8px 12px;
    border: 1px solid var(--rte-border-color);
    text-align: left;
}

.rte-editor table th {
    background: var(--rte-light-color);
    font-weight: bold;
}

.rte-editor img {
    max-width: 100%;
    height: auto;
    border-radius: var(--rte-border-radius);
}

.rte-editor hr {
    margin: 24px 0;
    border: none;
    border-top: 2px solid var(--rte-border-color);
}

.rte-editor ul, .rte-editor ol {
    margin: 8px 0;
    padding-left: 24px;
}

.rte-editor li {
    margin: 4px 0;
}

/* 頁尾樣式 */
.rte-footer {
    padding: 8px 16px;
    background: var(--rte-light-color);
    border-top: 1px solid var(--rte-border-color);
    font-size: 12px;
    color: var(--rte-secondary-color);
}

.rte-word-count .count {
    font-weight: bold;
    color: var(--rte-dark-color);
}

/* 模態框樣式 */
.rte-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.5);
    display: none;
    z-index: 1000;
}

.rte-modal.show {
    display: flex;
    justify-content: center;
    align-items: center;
    animation: rte-modal-fade-in 0.2s ease-out;
}

.rte-modal-content {
    background: white;
    border-radius: var(--rte-border-radius);
    box-shadow: var(--rte-shadow-lg);
    width: 90%;
    max-width: 600px;
    max-height: 80vh;
    overflow-y: auto;
    position: relative;
    animation: rte-modal-scale-in 0.2s ease-out;
    margin: 20px;
}

.rte-modal-close {
    position: absolute;
    top: 12px;
    right: 16px;
    font-size: 24px;
    font-weight: bold;
    cursor: pointer;
    color: var(--rte-secondary-color);
    z-index: 1;
}

.rte-modal-close:hover {
    color: var(--rte-danger-color);
}

.rte-modal-content h3 {
    margin: 0 0 20px 0;
    padding: 20px 20px 0 20px;
    font-size: 18px;
    font-weight: 600;
    color: var(--rte-dark-color);
}

.rte-modal-actions {
    padding: 20px;
    border-top: 1px solid var(--rte-border-color);
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

/* 輸入框樣式 */
.rte-input {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--rte-border-color);
    border-radius: var(--rte-border-radius);
    font-size: 14px;
    margin-bottom: 12px;
    box-sizing: border-box;
}

.rte-input:focus {
    outline: none;
    border-color: var(--rte-primary-color);
    box-shadow: 0 0 0 2px rgba(0,123,255,0.25);
}

/* 頁籤樣式 */
.rte-tabs {
    display: flex;
    border-bottom: 1px solid var(--rte-border-color);
    margin: 0 20px;
}

.rte-tab-btn {
    padding: 12px 20px;
    border: none;
    background: transparent;
    cursor: pointer;
    font-size: 14px;
    color: var(--rte-secondary-color);
    border-bottom: 2px solid transparent;
    transition: all 0.2s ease;
}

.rte-tab-btn:hover {
    color: var(--rte-primary-color);
}

.rte-tab-btn.active {
    color: var(--rte-primary-color);
    border-bottom-color: var(--rte-primary-color);
}

.rte-tab-content {
    display: none;
    padding: 20px;
}

.rte-tab-content.active {
    display: block;
}

/* 圖片上傳樣式 */
.rte-upload-area {
    border: 2px dashed var(--rte-border-color);
    border-radius: var(--rte-border-radius);
    padding: 40px 20px;
    text-align: center;
    cursor: pointer;
    transition: all 0.2s ease;
    margin-bottom: 16px;
}

.rte-upload-area:hover {
    border-color: var(--rte-primary-color);
    background: rgba(0,123,255,0.05);
}

.rte-upload-area i {
    font-size: 48px;
    color: var(--rte-secondary-color);
    margin-bottom: 12px;
    display: block;
}

.rte-upload-area p {
    margin: 0;
    color: var(--rte-secondary-color);
    font-size: 14px;
}

/* 上傳進度條 */
.rte-upload-progress {
    margin-bottom: 16px;
}

.rte-progress-bar {
    width: 100%;
    height: 8px;
    background: var(--rte-light-color);
    border-radius: 4px;
    overflow: hidden;
    margin-bottom: 8px;
}

.rte-progress-fill {
    height: 100%;
    background: var(--rte-primary-color);
    transition: width 0.3s ease;
    width: 0%;
}

.rte-progress-text {
    font-size: 12px;
    color: var(--rte-secondary-color);
    text-align: center;
    display: block;
}

/* 圖片預覽 */
.rte-image-preview {
    border: 1px solid var(--rte-border-color);
    border-radius: var(--rte-border-radius);
    padding: 16px;
    margin-bottom: 16px;
}

.rte-preview-img {
    max-width: 100%;
    max-height: 200px;
    display: block;
    margin: 0 auto 12px auto;
    border-radius: var(--rte-border-radius);
}

.rte-image-info {
    display: grid;
    gap: 8px;
    grid-template-columns: 1fr 1fr;
}

.rte-image-info .rte-input:first-child {
    grid-column: 1 / -1;
}

/* 圖片瀏覽器樣式 */
.rte-image-browser {
    border: 1px solid var(--rte-border-color);
    border-radius: var(--rte-border-radius);
    margin-bottom: 16px;
    max-height: 400px;
    overflow-y: auto;
}

.rte-browser-toolbar {
    display: flex;
    align-items: center;
    padding: 8px 12px;
    background: var(--rte-light-color);
    border-bottom: 1px solid var(--rte-border-color);
    gap: 8px;
}

.rte-btn-refresh {
    min-width: auto;
    width: 32px;
    height: 32px;
    padding: 0;
}

.rte-browser-path {
    flex: 1;
    font-size: 12px;
    color: var(--rte-secondary-color);
}

.rte-browser-content {
    padding: 12px;
}

.rte-loading {
    text-align: center;
    color: var(--rte-secondary-color);
    padding: 40px 20px;
}

.rte-no-images {
    text-align: center;
    color: var(--rte-secondary-color);
    padding: 40px 20px;
    font-style: italic;
}

.rte-image-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 12px;
}

.rte-browser-image {
    border: 2px solid transparent;
    border-radius: var(--rte-border-radius);
    overflow: hidden;
    cursor: pointer;
    transition: all 0.2s ease;
    background: white;
}

.rte-browser-image:hover {
    border-color: var(--rte-primary-color);
    box-shadow: var(--rte-shadow);
}

.rte-browser-image.selected {
    border-color: var(--rte-primary-color);
    box-shadow: 0 0 0 2px rgba(0,123,255,0.25);
}

.rte-browser-image img {
    width: 100%;
    height: 80px;
    object-fit: cover;
    display: block;
}

.rte-image-details {
    padding: 8px;
}

.rte-image-name {
    font-size: 11px;
    font-weight: 500;
    color: var(--rte-dark-color);
    margin-bottom: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.rte-image-size {
    font-size: 10px;
    color: var(--rte-secondary-color);
}

/* URL 預覽 */
.rte-url-preview {
    border: 1px solid var(--rte-border-color);
    border-radius: var(--rte-border-radius);
    padding: 12px;
    text-align: center;
    margin-top: 12px;
}

.rte-url-preview img {
    max-width: 100%;
    max-height: 150px;
    border-radius: var(--rte-border-radius);
}

/* 表格配置樣式 */
.rte-table-config {
    margin-bottom: 20px;
    padding: 20px 20px 0 20px;
}

.rte-config-row {
    display: flex;
    align-items: center;
    margin-bottom: 12px;
    gap: 12px;
}

.rte-config-row label {
    min-width: 60px;
    font-weight: 500;
    color: var(--rte-dark-color);
}

.rte-config-row input[type="number"] {
    width: 80px;
    margin-bottom: 0;
}

.rte-config-row input[type="checkbox"] {
    margin: 0 8px 0 0;
}

/* 表格預覽 */
.rte-table-preview {
    border: 1px solid var(--rte-border-color);
    border-radius: var(--rte-border-radius);
    padding: 16px;
    background: var(--rte-light-color);
    margin-bottom: 20px;
}

.rte-table-preview-content table {
    width: 100%;
    font-size: 12px;
}

/* 原始碼編輯器 */
.rte-source-textarea {
    width: 100%;
    height: 300px;
    padding: 12px;
    border: 1px solid var(--rte-border-color);
    border-radius: var(--rte-border-radius);
    font-family: 'Courier New', monospace;
    font-size: 13px;
    line-height: 1.4;
    resize: vertical;
    margin-bottom: 0;
}

.rte-source-textarea:focus {
    outline: none;
    border-color: var(--rte-primary-color);
    box-shadow: 0 0 0 2px rgba(0,123,255,0.25);
}

/* 響應式設計 */
@media (max-width: 768px) {
    .rte-toolbar {
        padding: 6px;
    }
    
    .rte-toolbar-group {
        margin-right: 6px;
        padding-right: 6px;
    }
    
    .rte-btn {
        padding: 4px 6px;
        min-width: 28px;
        height: 28px;
        font-size: 12px;
    }
    
    .rte-select {
        min-width: 60px;
        height: 28px;
        font-size: 12px;
    }
    
    .rte-color-picker {
        width: 28px;
        height: 28px;
    }
    
    .rte-modal-content {
        width: 95%;
        margin: 10px;
        max-height: 90vh;
    }
    
    .rte-modal.show {
        align-items: flex-start;
        padding-top: 20px;
    }
    
    .rte-image-grid {
        grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
        gap: 8px;
    }
    
    .rte-image-info {
        grid-template-columns: 1fr;
    }
    
    .rte-config-row {
        flex-direction: column;
        align-items: flex-start;
        gap: 4px;
    }
    
    .rte-config-row label {
        min-width: auto;
    }
}

@media (max-width: 480px) {
    .rte-toolbar {
        flex-direction: column;
        align-items: flex-start;
        gap: 6px;
    }
    
    .rte-toolbar-group {
        border-right: none;
        border-bottom: 1px solid var(--rte-border-color);
        padding-bottom: 6px;
        margin-bottom: 6px;
        margin-right: 0;
    }
    
    .rte-editor {
        padding: 12px;
        font-size: 16px; /* 防止 iOS Safari 自動縮放 */
    }
    
    .rte-modal-actions {
        flex-direction: column;
        gap: 8px;
    }
    
    .rte-btn-primary,
    .rte-btn-secondary {
        width: 100%;
        justify-content: center;
    }
}

/* 載入動畫 */
@keyframes rte-spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

/* 模態框動畫 */
@keyframes rte-modal-fade-in {
    from { 
        opacity: 0; 
    }
    to { 
        opacity: 1; 
    }
}

@keyframes rte-modal-scale-in {
    from { 
        opacity: 0;
        transform: scale(0.9) translateY(-10px); 
    }
    to { 
        opacity: 1;
        transform: scale(1) translateY(0); 
    }
}

.rte-loading:before {
    content: "";
    display: inline-block;
    width: 16px;
    height: 16px;
    border: 2px solid var(--rte-border-color);
    border-radius: 50%;
    border-top-color: var(--rte-primary-color);
    animation: rte-spin 1s ease-in-out infinite;
    margin-right: 8px;
}

/* 錯誤訊息 */
.rte-error {
    color: var(--rte-danger-color);
    text-align: center;
    padding: 20px;
    font-style: italic;
}

/* 工具提示 */
.rte-btn[title]:hover:after,
.rte-select[title]:hover:after {
    content: attr(title);
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    background: var(--rte-dark-color);
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 12px;
    white-space: nowrap;
    z-index: 1000;
    margin-bottom: 5px;
}

/* 主題變化 */
.rte-container.dark-theme {
    --rte-primary-color: #4dabf7;
    --rte-light-color: #2d3748;
    --rte-dark-color: #e2e8f0;
    --rte-border-color: #4a5568;
    background: #1a202c;
}

.rte-container.dark-theme .rte-editor {
    background: #2d3748;
    color: #e2e8f0;
}

.rte-container.dark-theme .rte-editor:focus {
    background: #4a5568;
}

/* 打印樣式 */
@media print {
    .rte-toolbar,
    .rte-footer,
    .rte-modal {
        display: none !important;
    }
    
    .rte-container {
        border: none;
        box-shadow: none;
    }
    
    .rte-editor {
        padding: 0;
    }
}