/**
 * Emma Dashboard v4 - Knowledge Hub CSS
 * Morandi Design System
 * 
 * Colors:
 *   M1 橄欖綠: #8E9E82
 *   M2 莫蘭迪紅: #C24F4F
 *   M3 草本綠: #A2C388
 *   M4 琥珀金: #D4AF37
 *   M6 莫蘭迪藍: #6695CC
 *   M7 金芒黃: #F4D03F
 * 
 * Revision: 260320-0002
 * v4.2 Update: 新增排序表頭樣式
 */

/* ================================================================
   CSS VARIABLES
   ================================================================ */

:root {
    /* Morandi Background */
    --bg-primary: #F5F5F0;
    --bg-secondary: #ECECEA;
    --bg-card: #FAFAF7;
    --bg-sidebar: #F0F0EC;

    /* Text */
    --text-primary: #333333;
    --text-secondary: #666666;
    --text-muted: #999999;

    /* Status */
    --status-done: #A2C388;
    --status-wip: #D4AF37;
    --status-todo: #BBBBBB;
    --status-blocked: #C24F4F;

    /* Links */
    --link-color: #6695CC;
    --link-hover: #4A7BBF;

    /* Borders */
    --border-color: #DDDDDD;
    --border-card: 2px solid #222222;
    --border-radius: 8px;
    --border-radius-sm: 4px;

    /* Spacing */
    --sp-xs: 4px;
    --sp-sm: 8px;
    --sp-md: 16px;
    --sp-lg: 24px;
    --sp-xl: 32px;

    /* Typography */
    --font-main: 'Inter', 'Noto Sans TC', -apple-system, BlinkMacSystemFont, sans-serif;
    --font-mono: 'JetBrains Mono', 'Fira Code', monospace;

    /* Transitions */
    --transition: 250ms ease-in-out;
}

/* ================================================================
   RESET & BASE
   ================================================================ */

*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

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

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

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

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

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

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

/* ================================================================
   HEADER
   ================================================================ */

.kh-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--sp-md) var(--sp-xl);
    background: var(--bg-card);
    border-bottom: var(--border-card);
    position: sticky;
    top: 0;
    z-index: 100;
}

.kh-header-left {
    display: flex;
    align-items: baseline;
    gap: var(--sp-md);
}

.kh-title {
    font-size: 1.3rem;
    font-weight: 700;
    color: var(--text-primary);
    letter-spacing: -0.3px;
}

.kh-subtitle {
    font-size: 0.8rem;
    color: var(--text-muted);
    font-family: var(--font-mono);
}

.kh-header-right {
    display: flex;
    align-items: center;
    gap: var(--sp-lg);
}

/* Version Tag */
.version-tag {
    font-size: 12px;
    font-weight: 600;
    padding: 3px 10px;
    border-radius: 12px;
    background: #F2F4F0;
    border: 1px solid var(--link-color);
    color: var(--text-secondary);
    transition: background var(--transition), color var(--transition);
}

.version-tag:hover {
    background: var(--link-color);
    color: #fff;
}

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

/* ================================================================
   LAYOUT
   ================================================================ */

.kh-layout {
    display: flex;
    min-height: calc(100vh - 56px);
    max-width: 1600px;
    margin: 0 auto;
}

/* ================================================================
   SIDEBAR
   ================================================================ */

.kh-sidebar {
    width: 280px;
    min-width: 280px;
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border-color);
    padding: var(--sp-lg);
    display: flex;
    flex-direction: column;
    gap: var(--sp-lg);
    overflow-y: auto;
    height: calc(100vh - 56px);
    position: sticky;
    top: 56px;
}

.kh-sidebar-section {
    display: flex;
    flex-direction: column;
    gap: var(--sp-sm);
}

.kh-sidebar-label {
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-muted);
    font-weight: 600;
    margin-bottom: 2px;
}

/* Module Navigation */
.kh-module-nav {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.kh-nav-item {
    display: flex;
    align-items: center;
    gap: var(--sp-sm);
    padding: 6px 8px;
    border-radius: var(--border-radius-sm);
    text-decoration: none;
    color: var(--text-primary);
    font-size: 0.85rem;
    transition: background var(--transition);
    cursor: pointer;
}

.kh-nav-item:hover {
    background: var(--bg-secondary);
}

.kh-nav-dot {
    width: 8px;
    height: 8px;
    border-radius: 2px;
    flex-shrink: 0;
}

.kh-nav-id {
    font-family: var(--font-mono);
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-secondary);
    min-width: 32px;
}

.kh-nav-name {
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-size: 0.8rem;
}

.kh-nav-progress {
    font-size: 0.7rem;
    color: var(--text-muted);
    font-family: var(--font-mono);
}

/* Sidebar Stats */
.kh-sidebar-stats {
    display: flex;
    flex-direction: column;
    gap: var(--sp-xs);
}

.kh-sb-stat {
    display: flex;
    justify-content: space-between;
    font-size: 0.8rem;
    padding: 3px 0;
    border-bottom: 1px solid var(--border-color);
}

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

.kh-sb-val {
    font-weight: 600;
    font-family: var(--font-mono);
}

/* Filters */
.kh-filter-group {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.kh-filter-label {
    font-size: 0.7rem;
    color: var(--text-muted);
}

.kh-filter-select {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius-sm);
    padding: 6px 8px;
    font-size: 0.8rem;
    color: var(--text-primary);
    font-family: var(--font-main);
    cursor: pointer;
    transition: border-color var(--transition);
}

.kh-filter-select:hover,
.kh-filter-select:focus {
    border-color: var(--link-color);
    outline: none;
}

/* ================================================================
   CONTENT
   ================================================================ */

.kh-content {
    flex: 1;
    padding: var(--sp-lg);
    display: flex;
    flex-direction: column;
    gap: var(--sp-lg);
    overflow-x: hidden;
}

/* ================================================================
   STATS BAR
   ================================================================ */

.kh-stats-bar {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: var(--sp-md);
}

.kh-stat-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    padding: var(--sp-md);
    text-align: center;
    transition: box-shadow var(--transition);
}

.kh-stat-card:hover {
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}

.kh-stat-value {
    display: block;
    font-size: 1.6rem;
    font-weight: 700;
    font-family: var(--font-mono);
    color: var(--text-primary);
}

.kh-stat-label {
    display: block;
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    margin-top: 2px;
}

.kh-stat-done .kh-stat-value { color: var(--status-done); }
.kh-stat-wip .kh-stat-value { color: var(--status-wip); }
.kh-stat-todo .kh-stat-value { color: var(--status-todo); }
.kh-stat-overall .kh-stat-value { color: var(--link-color); }

/* ================================================================
   BLOCKERS PANEL
   ================================================================ */

.kh-blockers-panel {
    background: #FFF5F5;
    border: 2px solid var(--status-blocked);
    border-radius: var(--border-radius);
    padding: var(--sp-md);
}

.kh-blockers-header {
    display: flex;
    align-items: center;
    gap: var(--sp-sm);
    margin-bottom: var(--sp-sm);
}

.kh-blockers-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 20px;
    height: 20px;
    background: var(--status-blocked);
    color: #fff;
    border-radius: 50%;
    font-size: 12px;
    font-weight: 700;
}

.kh-blockers-header h3 {
    font-size: 0.9rem;
    color: var(--status-blocked);
}

.kh-blockers-list {
    display: flex;
    flex-direction: column;
    gap: var(--sp-xs);
}

.kh-blocker-item {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding: var(--sp-xs) var(--sp-sm);
    background: #fff;
    border-radius: var(--border-radius-sm);
    font-size: 0.8rem;
    gap: var(--sp-md);
}

.kh-blocker-left {
    display: flex;
    align-items: center;
    gap: var(--sp-sm);
}

.kh-blocker-task {
    font-weight: 600;
    font-family: var(--font-mono);
    color: var(--status-blocked);
}

.kh-blocker-mod {
    font-size: 0.7rem;
    padding: 1px 6px;
    background: var(--status-blocked);
    color: #fff;
    border-radius: 3px;
    font-weight: 600;
}

.kh-blocker-right {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 2px;
}

.kh-blocker-issue {
    color: var(--text-primary);
}

.kh-blocker-impact {
    font-size: 0.7rem;
    color: var(--text-muted);
}

/* ================================================================
   SPRINT PANEL
   ================================================================ */

.kh-sprint-panel {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    padding: var(--sp-md);
}

.kh-sprint-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: var(--sp-sm);
}

.kh-sprint-header h3 {
    font-size: 1rem;
    color: var(--text-primary);
}

.kh-sprint-status {
    font-size: 0.8rem;
    color: var(--status-wip);
    font-weight: 500;
}

.kh-sprint-goals-list {
    padding-left: var(--sp-lg);
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.kh-sprint-goals-list li {
    margin-bottom: 4px;
}

/* ================================================================
   ACCORDION MODULES
   ================================================================ */

.kh-modules-section {
    display: flex;
    flex-direction: column;
    gap: var(--sp-md);
}

.kh-accordion {
    background: var(--bg-card);
    border: var(--border-card);
    border-radius: var(--border-radius);
    overflow: hidden;
}

.kh-accordion-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--sp-md);
    cursor: pointer;
    user-select: none;
    transition: background var(--transition);
}

.kh-accordion-header:hover {
    background: var(--bg-secondary);
}

.kh-accordion-active {
    background: var(--bg-secondary);
}

.kh-accordion-left {
    display: flex;
    align-items: center;
    gap: var(--sp-md);
}

.kh-module-id {
    font-family: var(--font-mono);
    font-size: 0.75rem;
    font-weight: 700;
    padding: 2px 8px;
    border-radius: 3px;
    color: #fff;
    min-width: 40px;
    text-align: center;
}

.kh-module-name {
    font-weight: 600;
    font-size: 0.95rem;
    color: var(--text-primary);
}

.kh-module-phase {
    font-size: 0.75rem;
    color: var(--text-muted);
    padding: 1px 6px;
    background: var(--bg-secondary);
    border-radius: 3px;
}

.kh-accordion-right {
    display: flex;
    align-items: center;
    gap: var(--sp-md);
    position: relative;
}

.kh-module-stats-inline {
    font-family: var(--font-mono);
    font-size: 0.8rem;
}

.kh-progress-mini {
    width: 80px;
    height: 4px;
    background: var(--bg-secondary);
    border-radius: 2px;
    overflow: hidden;
}

.kh-progress-mini-bar {
    height: 100%;
    border-radius: 2px;
    transition: width 0.5s ease;
}

.kh-progress-pct {
    font-family: var(--font-mono);
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-secondary);
    min-width: 32px;
    text-align: right;
    cursor: help;
    position: relative;
}

/* v4.1: Progress Tooltip */
.kh-progress-tooltip {
    display: none;
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    padding: var(--sp-md);
    min-width: 240px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    z-index: 100;
}

.kh-tooltip-visible {
    display: block;
}

.kh-tooltip-row {
    display: flex;
    align-items: center;
    gap: var(--sp-sm);
    margin-bottom: var(--sp-xs);
}

.kh-tooltip-label {
    font-size: 0.72rem;
    color: var(--text-secondary);
    min-width: 56px;
}

.kh-tooltip-bar {
    flex: 1;
    height: 6px;
    background: var(--bg-secondary);
    border-radius: 3px;
    overflow: hidden;
}

.kh-tooltip-bar-fill {
    height: 100%;
    border-radius: 3px;
    transition: width 0.3s ease;
}

.kh-tooltip-pct {
    font-family: var(--font-mono);
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--text-primary);
    min-width: 32px;
    text-align: right;
}

.kh-tooltip-divider {
    height: 1px;
    background: var(--border-color);
    margin: var(--sp-sm) 0;
}

.kh-tooltip-td {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.kh-tooltip-td-label {
    font-size: 0.68rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    font-weight: 600;
}

.kh-tooltip-td-count {
    font-size: 0.82rem;
    color: var(--text-primary);
    font-weight: 500;
}

.kh-tooltip-td-breakdown {
    display: flex;
    gap: var(--sp-sm);
    font-size: 0.72rem;
}

.kh-td-p2 {
    color: var(--status-wip);
}

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

/* Task Type Badges (v4.1) */
.kh-task-type {
    font-family: var(--font-mono);
    font-size: 0.65rem;
    font-weight: 600;
    padding: 1px 6px;
    border-radius: 3px;
    text-transform: uppercase;
}

.kh-type-feature {
    background: #E8F5E9;
    color: #2E7D32;
}

.kh-type-techdebt {
    background: #FFF3E0;
    color: #E65100;
}

.kh-type-bugfix {
    background: #FFEBEE;
    color: #C62828;
}

.kh-accordion-arrow {
    font-size: 0.7rem;
    color: var(--text-muted);
    transition: transform var(--transition);
    margin-left: var(--sp-xs);
}

.kh-accordion-active .kh-accordion-arrow {
    transform: rotate(180deg);
}

/* Accordion Body */
.kh-accordion-body {
    max-height: 0;
    overflow: hidden;
    transition: max-height var(--transition);
}

.kh-accordion-open {
    max-height: 2000px;
}

/* ================================================================
   TASK TABLE
   ================================================================ */

.kh-task-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.82rem;
}

.kh-task-table thead tr {
    background: var(--bg-secondary);
}

.kh-task-table th {
    padding: var(--sp-sm) var(--sp-md);
    text-align: left;
    font-size: 0.65rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    font-weight: 600;
    border-bottom: 1px solid var(--border-color);
}

/* v4.2: Sortable Table Headers */
.kh-sortable {
    cursor: pointer;
    user-select: none;
    transition: color var(--transition), background var(--transition);
    white-space: nowrap;
}

.kh-sortable:hover {
    color: var(--link-color);
    background: rgba(102, 149, 204, 0.1);
}

.kh-sort-indicator {
    font-size: 0.8rem;
    margin-left: 2px;
    color: var(--text-muted);
    opacity: 0.5;
}

.kh-sort-asc,
.kh-sort-desc {
    opacity: 1;
    color: var(--link-color);
    font-weight: 700;
}

.kh-task-table td {
    padding: var(--sp-sm) var(--sp-md);
    border-bottom: 1px solid var(--border-color);
    vertical-align: middle;
}

.kh-task-row {
    cursor: pointer;
    transition: background var(--transition);
}

.kh-task-row:hover {
    background: #F0F0EC;
}

.kh-task-id {
    font-family: var(--font-mono);
    font-size: 0.78rem;
    color: var(--link-color);
    font-weight: 500;
}

.kh-task-title {
    color: var(--text-primary);
    font-weight: 500;
}

.kh-task-blocker-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 14px;
    height: 14px;
    background: var(--status-blocked);
    color: #fff;
    border-radius: 50%;
    font-size: 9px;
    font-weight: 700;
    margin-left: 4px;
    cursor: help;
    vertical-align: middle;
}

.kh-task-owner,
.kh-task-phase {
    color: var(--text-secondary);
    font-size: 0.78rem;
}

.kh-task-links {
    display: flex;
    gap: 4px;
    flex-wrap: wrap;
}

/* ================================================================
   BADGES & CHIPS
   ================================================================ */

.kh-badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 0.68rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}

.kh-priority {
    font-family: var(--font-mono);
    font-size: 0.75rem;
    font-weight: 700;
}

/* GitHub Links */
a.kh-gh-link,
.kh-task-links a {
    font-size: 0.68rem;
    padding: 1px 6px;
    border-radius: 3px;
    background: var(--bg-secondary);
    color: var(--link-color) !important;
    text-decoration: none;
    font-family: var(--font-mono);
    border: 1px solid var(--border-color);
    transition: all var(--transition);
}

a.kh-gh-link:hover,
.kh-task-links a:hover {
    background: var(--link-color);
    color: #fff !important;
    border-color: var(--link-color);
}

/* Features chips */
.kh-features-label {
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    padding: var(--sp-sm) var(--sp-md) var(--sp-xs);
    font-weight: 600;
}

.kh-features-list {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    padding: 0 var(--sp-md) var(--sp-md);
}

.kh-feature-chip {
    font-size: 0.72rem;
    padding: 2px 8px;
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    color: var(--text-secondary);
    cursor: default;
}

/* ================================================================
   EMPTY STATE
   ================================================================ */

.kh-empty {
    padding: var(--sp-lg);
    text-align: center;
    color: var(--text-muted);
    font-size: 0.85rem;
}

/* ================================================================
   MODAL
   ================================================================ */

.kh-modal {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    padding: var(--sp-lg);
}

.kh-modal-open {
    display: flex;
}

.kh-modal-content {
    background: var(--bg-card);
    border: var(--border-card);
    border-radius: var(--border-radius);
    max-width: 700px;
    width: 100%;
    max-height: 80vh;
    overflow-y: auto;
    position: relative;
}

.kh-modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--sp-md) var(--sp-lg);
    border-bottom: 1px solid var(--border-color);
    position: sticky;
    top: 0;
    background: var(--bg-card);
    z-index: 1;
}

.kh-modal-title {
    font-size: 1rem;
    font-weight: 700;
}

.kh-modal-close {
    background: none;
    border: none;
    font-size: 1.2rem;
    color: var(--text-muted);
    cursor: pointer;
    padding: 4px 8px;
    border-radius: var(--border-radius-sm);
    transition: color var(--transition), background var(--transition);
    line-height: 1;
}

.kh-modal-close:hover {
    color: var(--status-blocked);
    background: var(--bg-secondary);
}

.kh-modal-body {
    padding: var(--sp-lg);
}

.kh-modal-meta {
    display: flex;
    flex-wrap: wrap;
    gap: var(--sp-md);
    margin-bottom: var(--sp-lg);
    padding-bottom: var(--sp-md);
    border-bottom: 1px solid var(--border-color);
}

.kh-modal-meta-item {
    font-size: 0.82rem;
    color: var(--text-secondary);
}

.kh-modal-meta-item strong {
    color: var(--text-primary);
}

.kh-modal-desc {
    font-size: 0.85rem;
    color: var(--text-secondary);
    line-height: 1.7;
    margin-bottom: var(--sp-md);
    white-space: pre-wrap;
}

.kh-modal-section {
    margin-bottom: var(--sp-md);
}

.kh-modal-section h4 {
    font-size: 0.72rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    margin-bottom: var(--sp-xs);
    font-weight: 600;
}

.kh-modal-section ul {
    padding-left: var(--sp-lg);
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.kh-modal-section ul li {
    margin-bottom: 3px;
}

.kh-blocker-list li {
    color: var(--status-blocked) !important;
}

a.kh-gh-link-modal {
    font-size: 0.8rem;
    padding: 3px 10px;
    margin-right: 4px;
    margin-bottom: 4px;
}

/* ================================================================
   LOADING
   ================================================================ */

.kh-loading {
    position: fixed;
    inset: 0;
    background: var(--bg-primary);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
}

.kh-loading-text {
    font-size: 1rem;
    color: var(--text-muted);
}

/* ================================================================
   RESPONSIVE
   ================================================================ */

@media (max-width: 1100px) {
    .kh-stats-bar {
        grid-template-columns: repeat(4, 1fr);
    }
}

@media (max-width: 900px) {
    .kh-layout {
        flex-direction: column;
    }

    .kh-sidebar {
        width: 100%;
        min-width: 0;
        height: auto;
        position: static;
        flex-direction: row;
        flex-wrap: wrap;
        border-right: none;
        border-bottom: 1px solid var(--border-color);
        padding: var(--sp-md);
    }

    .kh-sidebar-section {
        flex: 1;
        min-width: 200px;
    }

    .kh-module-nav {
        flex-direction: row;
        flex-wrap: wrap;
    }

    .kh-nav-name {
        display: none;
    }

    .kh-layout {
        min-height: auto;
    }
}

@media (max-width: 600px) {
    .kh-header {
        flex-direction: column;
        align-items: flex-start;
        gap: var(--sp-sm);
        padding: var(--sp-sm) var(--sp-md);
    }

    .kh-stats-bar {
        grid-template-columns: repeat(2, 1fr);
    }

    .kh-task-table th:nth-child(5),
    .kh-task-table td:nth-child(5),
    .kh-task-table th:nth-child(6),
    .kh-task-table td:nth-child(6) {
        display: none;
    }

    .kh-accordion-left {
        gap: var(--sp-sm);
    }

    .kh-module-phase {
        display: none;
    }
}

/* ================================================================
   UTILITIES
   ================================================================ */

.hidden { display: none !important; }
