:root {
    --app-font-size: 16px;
    --doc-font-family: 'Inter', sans-serif;
    --radius: 12px;
    --transition: 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

[data-theme="light"] {
    --bg-primary: #f8fafc;
    --bg-secondary: #f1f5f9;
    --bg-card: #ffffff;
    --text-primary: #0f172a;
    --text-secondary: #475569;
    --border: #e2e8f0;
    --accent: #6366f1;
    --accent-glow: rgba(99, 102, 241, 0.1);
}

[data-theme="dark"] {
    --bg-primary: #0a0e1a;
    --bg-secondary: #111827;
    --bg-card: #1e293b;
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --border: #334155;
    --accent: #818cf8;
    --accent-glow: rgba(129, 140, 248, 0.15);
}

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

html {
    font-size: var(--app-font-size);
}

body {
    font-family: var(--doc-font-family);
    background: var(--bg-primary);
    color: var(--text-primary);
    transition: background 0.3s;
    height: 100vh;
    overflow: hidden;
}

/* Layout */
.app-header {
    height: 60px;
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    position: relative;
    z-index: 1000;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.app-layout {
    display: flex;
    height: calc(100vh - 60px);
}

.sidebar {
    width: 280px;
    border-right: 1px solid var(--border);
    background: var(--bg-card);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

.main-content {
    flex: 1;
    overflow-y: auto;
    background: var(--bg-secondary);
    position: relative;
}

/* CKEditor Pro Workspace */
.editor-wrapper {
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
    background: var(--bg-secondary);
    position: absolute;
    top: 0;
    left: 0;
    z-index: 100;
}

#toolbar-container {
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 150;
}

.doc-scroller {
    flex: 1;
    overflow-y: auto;
    padding: 40px 20px;
    display: flex;
    justify-content: center;
    background: #e2e8f0;
}

/* Concrete bg for contrast */

.ck-content,
.ck.ck-editor__editable,
.ck.ck-editor__editable.ck-focused,
.ck-editor__editable_inline {
    width: 210mm;
    min-height: 297mm;
    padding: 2.5cm !important;
    background: white !important;
    color: black !important;
    box-shadow: 0 10px 50px rgba(0, 0, 0, 0.1) !important;
    border: 0 !important;
    outline: none !important;
    font-family: var(--doc-font-family) !important;
    margin-bottom: 50px;
}

[data-theme="dark"] .doc-scroller {
    background: #0f172a;
}

[data-theme="dark"] .ck-content {
    background: white !important;
    color: black !important;
}

/* Dashboard & Screens */
.welcome-screen {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    text-align: center;
    padding: 40px;
}

.stats-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 20px;
    margin-bottom: 24px;
}

.glass-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.stat-val {
    font-size: 1.8rem;
    font-weight: 800;
    color: var(--accent);
}

/* Matrix Tables */
.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

.data-table th {
    text-align: left;
    padding: 12px;
    border-bottom: 2px solid var(--border);
    color: var(--text-secondary);
    background: var(--bg-primary);
}

.data-table td {
    padding: 12px;
    border-bottom: 1px solid var(--border);
    font-family: inherit;
}

.row-mandatory {
    border-left: 4px solid #ef4444;
}

/* Utilities */
.flex {
    display: flex;
}

.items-center {
    align-items: center;
}

.justify-between {
    justify-content: space-between;
}

.justify-start {
    justify-content: start;
}

.gap-2 {
    gap: 8px;
}

.gap-3 {
    gap: 12px;
}

.gap-4 {
    gap: 16px;
}

.gap-6 {
    gap: 24px;
}

.w-full {
    width: 100%;
}

.flex-1 {
    flex: 1;
}

.p-2 {
    padding: 8px;
}

.p-3 {
    padding: 12px;
}

.p-4 {
    padding: 16px;
}

.p-10 {
    padding: 40px;
}

.max-w-xl {
    max-width: 36rem;
}

.mx-auto {
    margin-left: auto;
    margin-right: auto;
}

.py-3 {
    padding-top: 12px;
    padding-bottom: 12px;
}

.py-10 {
    padding-top: 2.5rem;
    padding-bottom: 2.5rem;
}

.border-t {
    border-top: 1px solid var(--border);
}

.border-b {
    border-bottom: 1px solid var(--border);
}

.border-l {
    border-left: 1px solid var(--border);
}

.pl-4 {
    padding-left: 16px;
}

.mb-2 {
    margin-bottom: 8px;
}

.mb-4 {
    margin-bottom: 16px;
}

.mb-6 {
    margin-bottom: 24px;
}

.mb-8 {
    margin-bottom: 32px;
}

.mt-4 {
    margin-top: 16px;
}

.font-bold {
    font-weight: 700;
}

.text-sm {
    font-size: 0.875rem;
}

.text-xs {
    font-size: 0.75rem;
}

.text-xl {
    font-size: 1.25rem;
}

.text-2xl {
    font-size: 1.5rem;
}

.text-accent {
    color: var(--accent);
}

.text-truncate {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.opacity-60 {
    opacity: 0.6;
}

.opacity-20 {
    opacity: 0.2;
}

.cursor-pointer {
    cursor: pointer;
}

.bg-accent-glow {
    background: var(--accent-glow);
}

.rounded-lg {
    border-radius: 8px;
}

.space-y-1>*+* {
    margin-top: 4px;
}

.space-y-6>*+* {
    margin-top: 24px;
}

.block {
    display: block;
}

/* Buttons */
.btn {
    padding: 8px 16px;
    border-radius: 8px;
    border: 1px solid var(--border);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    font-weight: 600;
    font-family: inherit;
    transition: var(--transition);
    background: var(--bg-card);
    color: var(--text-primary);
}

.btn-primary {
    background: var(--accent);
    color: white;
    border: none;
}

.btn-primary:hover {
    opacity: 0.9;
    transform: translateY(-1px);
    box-shadow: 0 4px 12px var(--accent-glow);
}

.btn-icon {
    width: 36px;
    height: 36px;
    padding: 0;
    border: none;
    background: transparent;
    cursor: pointer;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
}

.btn-icon:hover {
    background: var(--bg-secondary);
    color: var(--text-primary);
}

.btn-sm {
    padding: 4px 8px;
    font-size: 0.75rem;
}

/* Status Badges */
.status-badge {
    padding: 2px 8px;
    border-radius: 12px;
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
}

.status-m {
    background: #fee2e2;
    color: #ef4444;
}

/* Inputs */
.input {
    width: 100%;
    padding: 10px 14px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg-primary);
    color: var(--text-primary);
    outline: none;
    transition: var(--transition);
}

.input:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-glow);
}

/* Utilities for Matrix */
.level-badge {
    display: inline-block;
    width: 22px;
    height: 22px;
    line-height: 22px;
    text-align: center;
    border-radius: 4px;
    font-weight: 700;
    font-size: 11px;
    margin-right: 4px;
    border: 1px solid rgba(0, 0, 0, 0.1);
}

.lv-M {
    background: #fee2e2;
    color: #ef4444;
}

.lv-S {
    background: #fef3c7;
    color: #d97706;
}

.lv-O {
    background: #dcfce7;
    color: #10b981;
}

/* Class đặc biệt để xoá sạch viền/shadow khi capture */
.printing-mode {
    border: none !important;
    box-shadow: none !important;
    outline: none !important;
}

.printing-mode .ck.ck-content {
    border: none !important;
    box-shadow: none !important;
}

/* Quan trọng: Xoá viền của chính editor khi nó mất focus */
.printing-mode .ck-editor__editable_themed {
    border: none !important;
    box-shadow: none !important;
}

/* Printing Optimization */
@media print {
    body {
        background: white !important;
        overflow: visible !important;
        height: auto !important;
    }

    .app-header,
    .sidebar,
    #toolbar-container,
    .btn,
    .header-center,
    .welcome-screen {
        display: none !important;
    }

    .app-layout {
        display: block !important;
        height: auto !important;
    }

    .main-content {
        overflow: visible !important;
        background: white !important;
        padding: 0 !important;
    }

    .doc-scroller {
        padding: 0 !important;
        background: white !important;
        display: block !important;
        overflow: visible !important;
    }

    .editor-wrapper {
        position: static !important;
    }

    .ck-content {
        width: 100% !important;
        box-shadow: none !important;
        border: none !important;
        margin: 0 !important;
        padding: 0 !important;
    }
}

/* Page Pagination Simulation */
.ck-content {
    background-image: linear-gradient(to bottom, transparent 296.5mm, var(--border) 296.5mm, var(--border) 297mm, transparent 297mm) !important;
    background-size: 100% 297mm !important;
    position: relative;
}

.page-break {
    border: none;
    border-top: 2px dashed var(--accent);
    margin: 40px -2.5cm !important;
    height: 1px;
    position: relative;
    cursor: default;
    clear: both;
}

.page-break::after {
    content: "✂️ ISO-210 NGẮT TRANG (PAGE BREAK)";
    position: absolute;
    top: -10px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--bg-secondary);
    padding: 2px 12px;
    font-size: 10px;
    font-weight: bold;
    color: var(--accent);
    border-radius: 20px;
    border: 1px solid var(--border);
}

/* Mermaid Diagram Modal */
#mermaid-modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.8);
    z-index: 10000;
    display: none;
    align-items: center;
    justify-content: center;
    backdrop-filter: blur(5px);
}

.m-modal-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 16px;
    width: 900px;
    max-width: 95vw;
    max-height: 90vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.m-header {
    padding: 15px;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
}

.m-body {
    padding: 0;
    display: grid;
    grid-template-columns: 350px 1fr;
    height: 500px;
}

.m-editor {
    background: #1a1a1a;
    padding: 20px;
    display: flex;
    flex-direction: column;
}

.m-textarea {
    flex: 1;
    background: transparent;
    border: 1px solid #444;
    color: #10b981;
    font-family: monospace;
    font-size: 13px;
    padding: 10px;
    outline: none;
    resize: none;
    border-radius: 8px;
}

.m-preview {
    background: white;
    padding: 20px;
    overflow: auto;
    display: flex;
    align-items: center;
    justify-content: center;
}

.m-footer {
    padding: 15px;
    border-top: 1px solid var(--border);
    display: flex;
    gap: 10px;
    justify-content: flex-end;
}

/* Version History Modal */
#history-modal {
    position: fixed; inset: 0; background: rgba(0,0,0,0.6); 
    z-index: 10000; display: flex; align-items: center; justify-content: center; 
    backdrop-filter: blur(5px);
}
.h-card {
    background: var(--bg-card); width: 500px; max-width: 95vw; 
    border-radius: 16px; overflow: hidden; border: 1px solid var(--border); box-shadow: 0 25px 50px -12px rgba(0,0,0,0.5);
}
.h-header { padding: 20px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; }
.h-item { padding: 15px 20px; border-bottom: 1px solid var(--border); cursor: pointer; display: flex; justify-content: space-between; align-items: center; transition: background 0.2s; }
.h-item:hover { background: var(--bg-secondary); }
.h-item:last-child { border-bottom: none; }
.v-tag { font-weight: 800; color: var(--accent); }
.v-date { font-size: 11px; opacity: 0.5; margin-top: 4px; }

/* Scrollbar */
::-webkit-scrollbar {
    width: 8px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

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

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