:root {
    --bg: #f5f7fa;
    --surface: #ffffff;
    --surface-2: #f0f4f8;
    --text: #1f2733;
    --muted: #6b7686;
    --border: #e3e8f0;
    --accent: #4f46e5;
    --accent-soft: #eef2ff;
    --shadow: 0 1px 3px rgba(16,24,40,0.06), 0 8px 24px rgba(16,24,40,0.06);
    --low: #16a34a;
    --medium: #d97706;
    --high: #ea580c;
    --critical: #dc2626;
    --pending: #6b7280;
    --progress: #2563eb;
    --completed: #16a34a;
    --archived: #9ca3af;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    font-family: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
}

/* ===== TOPBAR ===== */
.topbar {
    display: flex; align-items: center; justify-content: space-between;
    padding: 14px 24px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow);
    position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 12px; }
.logo {
    width: 40px; height: 40px; display: grid; place-items: center;
    background: var(--accent); color: #fff; border-radius: 10px;
    font-size: 20px; font-weight: 700;
}
.topbar h1 { font-size: 1.15rem; margin: 0; }
.subtitle { margin: 1px 0 0; color: var(--muted); font-size: 0.8rem; }
.topbar-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.last-updated { color: var(--muted); font-size: 0.75rem; }

/* ===== BUTTONS ===== */
.primary-btn {
    background: var(--accent); color: #fff; border: none;
    padding: 8px 14px; border-radius: 8px; cursor: pointer;
    font-weight: 600; font-size: 0.82rem;
}
.primary-btn:hover { filter: brightness(1.08); }
.ghost-btn {
    background: transparent; border: 1px solid var(--border); color: var(--muted);
    padding: 8px 14px; border-radius: 8px; cursor: pointer;
    font-weight: 600; font-size: 0.82rem; display: inline-flex; align-items: center;
}
.ghost-btn:hover { color: var(--accent); border-color: var(--accent); }
#todayBtn.active { color: #fff; background: var(--accent); border-color: var(--accent); }

/* ===== VIEW SWITCHER ===== */
.view-switcher {
    display: flex; gap: 6px; margin: 18px 0 14px;
}
.view-btn {
    background: var(--surface); border: 1px solid var(--border); color: var(--muted);
    padding: 8px 18px; border-radius: 8px; cursor: pointer;
    font-weight: 600; font-size: 0.82rem;
}
.view-btn:hover { color: var(--accent); border-color: var(--accent); }
.view-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }

/* ===== CONTAINER ===== */
.container { padding: 0 24px 30px; max-width: 1400px; margin: 0 auto; }

/* ===== KPI CARDS ===== */
.kpis {
    display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 14px; margin-bottom: 22px;
}
.kpi {
    background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
    padding: 16px 18px; box-shadow: var(--shadow); position: relative; overflow: hidden;
}
.kpi::before {
    content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px;
    background: var(--accent);
}
.kpi.accent::before { background: var(--accent); }
.kpi.success::before { background: var(--completed); }
.kpi.danger::before { background: var(--critical); }
.kpi.warn::before { background: var(--medium); }
.kpi .value { font-size: 1.8rem; font-weight: 700; line-height: 1; }
.kpi .label { color: var(--muted); font-size: 0.72rem; margin-top: 6px; text-transform: uppercase; letter-spacing: 0.04em; }

/* ===== CHARTS ===== */
.charts-row {
    display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; margin-bottom: 22px;
}
.chart-panel {
    background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
    padding: 16px 18px; box-shadow: var(--shadow);
}
.chart-panel h3 { margin: 0 0 12px; font-size: 0.85rem; color: var(--muted); }
.chart { display: flex; flex-direction: column; gap: 8px; }
.chart-bar-row { display: flex; align-items: center; gap: 8px; }
.chart-bar-label { width: 90px; font-size: 0.78rem; text-align: right; color: var(--muted); }
.chart-bar-track { flex: 1; height: 20px; background: var(--surface-2); border-radius: 6px; overflow: hidden; }
.chart-bar-fill { height: 100%; border-radius: 6px; transition: width 0.4s ease; }
.chart-bar-value { width: 30px; font-size: 0.78rem; font-weight: 600; }
.donut-chart { display: flex; align-items: center; gap: 14px; }
.donut-legend { display: flex; flex-direction: column; gap: 4px; }
.legend-item { display: flex; align-items: center; gap: 6px; font-size: 0.78rem; }
.legend-dot { width: 10px; height: 10px; border-radius: 50%; }

/* ===== PANEL ===== */
.panel {
    background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
    box-shadow: var(--shadow); height: 715px;
}
.panel-head {
    padding: 16px 18px; border-bottom: 1px solid var(--border);
    display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: space-between;
}
.panel-head h2 { margin: 0; font-size: 1rem; }
.controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.search-box { position: relative; display: flex; align-items: center; }
.search-icon { position: absolute; left: 10px; font-size: 0.82rem; opacity: 0.6; }
.controls input, .controls select {
    padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px;
    background: var(--surface-2); color: var(--text); font-size: 0.85rem;
}
.controls input { min-width: 180px; }
.tag-filter-input { min-width: 120px !important; max-width: 160px; }

/* ===== TABLE ===== */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 12px 14px; border-bottom: 1px solid var(--border); font-size: 0.85rem; }
th { background: var(--surface-2); color: var(--muted); text-transform: uppercase; font-size: 0.7rem; letter-spacing: 0.05em; }
tbody tr { transition: background 0.15s ease; }
tbody tr:hover { background: var(--accent-soft); }
.bulk-col { width: 36px; }

/* ===== BADGES ===== */
.badge { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 0.7rem; font-weight: 700; color: #fff; }
.priority-LOW { background: var(--low); }
.priority-MEDIUM { background: var(--medium); }
.priority-HIGH { background: var(--high); }
.priority-CRITICAL { background: var(--critical); }
.status-PENDING { background: var(--pending); }
.status-IN_PROGRESS { background: var(--progress); }
.status-COMPLETED { background: var(--completed); }
.status-ARCHIVED { background: var(--archived); }
.tag-badge {
    display: inline-block; padding: 2px 7px; border-radius: 6px; font-size: 0.68rem;
    background: var(--accent-soft); color: var(--accent); font-weight: 600; margin: 1px 2px;
}
.dep-badge {
    display: inline-block; padding: 2px 7px; border-radius: 6px; font-size: 0.68rem;
    background: #fef3c7; color: #92400e; font-weight: 600;
}

/* ===== ACTION BUTTONS ===== */
.action-btn {
    border: none; background: var(--surface-2); cursor: pointer;
    font-size: 0.88rem; padding: 4px 6px; border-radius: 6px; margin-right: 2px;
}
.action-btn:hover { background: var(--accent-soft); }
.del-btn:hover { background: #fde2e2; }
.detail-btn { background: var(--accent); color: #fff; border: none; padding: 5px 10px; border-radius: 6px; cursor: pointer; font-size: 0.78rem; }

/* ===== BULK BAR ===== */
.bulk-bar {
    padding: 10px 18px; background: var(--accent-soft); border-bottom: 1px solid var(--border);
    display: flex; align-items: center; gap: 10px; font-size: 0.82rem;
}
.bulk-bar span { font-weight: 600; }

/* ===== MODALS ===== */
.modal { position: fixed; inset: 0; background: rgba(15,20,32,0.55); display: grid; place-items: center; z-index: 50; }
.modal-card {
    background: var(--surface); color: var(--text); border-radius: 14px;
    padding: 24px; max-width: 440px; width: 92%; position: relative; box-shadow: var(--shadow);
}
.modal-card-wide { max-width: 620px; }
.modal-close { position: absolute; top: 10px; right: 12px; background: transparent; border: none; font-size: 1.5rem; color: var(--muted); cursor: pointer; }
.modal-card h2 { margin-top: 0; font-size: 1.05rem; }
.modal-card .row { display: flex; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--border); gap: 10px; }
.modal-card .row span:first-child { color: var(--muted); font-weight: 600; }
.modal-card .row span:last-child { text-align: right; }
.overdue-warn { color: var(--critical); font-weight: 600; margin-top: 8px; }
.detail-actions { margin-top: 14px; display: flex; gap: 8px; justify-content: flex-end; }

/* ===== FORM ===== */
.task-form { display: flex; flex-direction: column; gap: 12px; margin-top: 12px; }
.task-form label { display: flex; flex-direction: column; gap: 5px; font-size: 0.78rem; color: var(--muted); font-weight: 600; }
.task-form input, .task-form textarea, .task-form select {
    padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px;
    background: var(--surface-2); color: var(--text); font-size: 0.85rem; font-weight: 400; width: 100%;
}
.form-row { display: flex; gap: 12px; }
.form-row label { flex: 1; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

/* ===== COMMENTS ===== */
.comment-section { margin-top: 16px; border-top: 1px solid var(--border); padding-top: 14px; }
.comment-section h3 { margin: 0 0 10px; font-size: 0.9rem; }
.comment-list { max-height: 200px; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.comment-item { padding: 8px 10px; background: var(--surface-2); border-radius: 8px; font-size: 0.82rem; }
.comment-meta { color: var(--muted); font-size: 0.72rem; margin-bottom: 3px; }
.comment-form { display: flex; gap: 8px; }
.comment-author-input { width: 120px !important; flex-shrink: 0; }
.comment-text-input { flex: 1; }

/* ===== KANBAN ===== */
.kanban-board { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; padding: 16px; }
.kanban-col { background: var(--surface-2); border-radius: 10px; padding: 12px; min-height: 200px; }
.kanban-header { padding: 8px 12px; border-radius: 8px; font-weight: 700; font-size: 0.82rem; margin-bottom: 10px; text-align: center; color: #fff; }
.pending-header { background: var(--pending); }
.progress-header { background: var(--progress); }
.completed-header { background: var(--completed); }
.kanban-card {
    background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
    padding: 10px 12px; margin-bottom: 8px; box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}
.kanban-card-title { font-weight: 600; font-size: 0.82rem; margin-bottom: 4px; }
.kanban-card-meta { font-size: 0.72rem; color: var(--muted); display: flex; gap: 6px; align-items: center; }

/* ===== ACTIVITY FEED ===== */
.activity-feed { padding: 16px; display: flex; flex-direction: column; gap: 10px; max-height: 500px; overflow-y: auto; }
.activity-item { display: flex; gap: 12px; align-items: flex-start; padding: 10px 12px; border-radius: 8px; background: var(--surface-2); }
.activity-icon { width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center; font-size: 0.82rem; color: #fff; flex-shrink: 0; }
.activity-icon.CREATED { background: var(--completed); }
.activity-icon.UPDATED { background: var(--progress); }
.activity-icon.DELETED { background: var(--critical); }
.activity-icon.STATUS_CHANGED { background: var(--medium); }
.activity-icon.COMMENT_ADDED { background: #8b5cf6; }
.activity-icon.ARCHIVED { background: var(--archived); }
.activity-icon.RESTORED { background: var(--low); }
.activity-text { flex: 1; }
.activity-task { font-weight: 600; font-size: 0.82rem; }
.activity-detail { font-size: 0.78rem; color: var(--muted); margin-top: 2px; }
.activity-time { font-size: 0.7rem; color: var(--muted); white-space: nowrap; }

/* ===== TEMPLATES ===== */
.template-list { padding: 16px; display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 12px; }
.template-card {
    background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
    padding: 14px 16px; box-shadow: var(--shadow);
}
.template-card h4 { margin: 0 0 6px; font-size: 0.88rem; }
.template-card p { margin: 0; font-size: 0.78rem; color: var(--muted); }
.template-card .tpl-actions { margin-top: 10px; display: flex; gap: 6px; }

/* ===== EMPTY STATE ===== */
.empty-state { padding: 40px 20px; text-align: center; color: var(--muted); }
.empty-emoji { font-size: 2.2rem; display: block; margin-bottom: 8px; }

/* ===== NOTES ===== */
.source-note { margin-top: 16px; color: var(--muted); font-size: 0.75rem; text-align: center; }
.source-note code { background: var(--accent-soft); padding: 2px 5px; border-radius: 5px; }
.store-note { margin: 12px 16px 0; padding: 8px 10px; background: var(--surface-2); border: 1px dashed var(--border); border-radius: 8px; color: var(--muted); font-size: 0.75rem; }
.store-note code { background: var(--accent-soft); padding: 1px 4px; border-radius: 4px; }

.hidden { display: none !important; }

@keyframes fadeOut {
    0% { opacity: 1; }
    70% { opacity: 1; }
    100% { opacity: 0; }
}

#exportBadge { animation: fadeOut 2.5s forwards; }
.auto-export-active { color: #fff !important; background: #16a34a !important; border-color: #16a34a !important; }

/* ===== RESPONSIVE ===== */
@media (max-width: 900px) {
    .charts-row { grid-template-columns: 1fr; }
    .kanban-board { grid-template-columns: 1fr; }
    .form-row { flex-direction: column; }
}
@media (max-width: 640px) {
    .topbar { padding: 12px 14px; flex-wrap: wrap; }
    .topbar-actions { gap: 6px; }
    .controls input, .controls select { min-width: 100%; }
    .panel-head { flex-direction: column; align-items: stretch; }
    thead { display: none; }
    tbody tr { display: block; border-bottom: 6px solid var(--bg); }
    tbody td { display: flex; justify-content: space-between; border: none; padding: 8px; }
    tbody td::before { content: attr(data-label); color: var(--muted); font-weight: 600; }
}
