:root {
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --success: #10b981;
    --success-hover: #059669;
    --bg-color: #f1f5f9;
    --sidebar-bg: #ffffff;
    --text-main: #1e293b;
    --text-muted: #64748b;
    --border-color: #e2e8f0;
    --danger: #ef4444;
}

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

body {
    font-family: 'Inter', sans-serif;
    background: var(--bg-color);
    color: var(--text-main);
    height: 100vh;
    overflow: hidden;
}

.app-container {
    display: flex;
    height: 100vh;
}

/* Sidebar Styling */
.sidebar {
    width: 400px;
    background: var(--sidebar-bg);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    box-shadow: 2px 0 10px rgba(0,0,0,0.05);
    z-index: 10;
}

.sidebar-header {
    padding: 20px;
    border-bottom: 1px solid var(--border-color);
    background: #f8fafc;
}

.sidebar-header h2 {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--primary);
    margin-bottom: 4px;
}

.sidebar-header p {
    font-size: 0.875rem;
    color: var(--text-muted);
}

.tabs {
    display: flex;
    border-bottom: 1px solid var(--border-color);
    background: #f8fafc;
    overflow-x: auto;
}

.tab-btn {
    padding: 12px 15px;
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    cursor: pointer;
    font-weight: 500;
    color: var(--text-muted);
    transition: all 0.2s;
    white-space: nowrap;
}

.tab-btn:hover {
    color: var(--primary);
}

.tab-btn.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
    background: #fff;
}

.form-content {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
}

.tab-pane {
    display: none;
}

.tab-pane.active {
    display: block;
    animation: fadeIn 0.3s;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(5px); }
    to { opacity: 1; transform: translateY(0); }
}

.form-group {
    margin-bottom: 15px;
}

.form-row {
    display: flex;
    gap: 15px;
}

.form-row .form-group {
    flex: 1;
}

label {
    display: block;
    font-size: 0.875rem;
    font-weight: 500;
    margin-bottom: 6px;
    color: var(--text-main);
}

.form-control {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-family: inherit;
    font-size: 0.875rem;
    transition: border-color 0.2s;
}

.form-control:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(37,99,235,0.1);
}

.form-control::placeholder {
    color: #9ca3af;
    font-style: italic;
    opacity: 1; /* Firefox */
}

.form-control:focus::placeholder {
    opacity: 0.5;
}

.checkbox-group {
    display: flex;
    align-items: center;
    gap: 8px;
}

.checkbox-group label {
    margin-bottom: 0;
    cursor: pointer;
}

.sidebar-footer {
    padding: 15px 20px;
    border-top: 1px solid var(--border-color);
    background: #f8fafc;
}

.action-buttons {
    display: flex;
    gap: 10px;
    justify-content: space-between;
}

.btn {
    padding: 8px 16px;
    border-radius: 6px;
    border: none;
    font-weight: 500;
    cursor: pointer;
    font-size: 0.875rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    transition: all 0.2s;
    font-family: inherit;
}

.btn-primary { background: var(--primary); color: white; }
.btn-primary:hover { background: var(--primary-hover); }

.btn-success { background: var(--success); color: white; }
.btn-success:hover { background: var(--success-hover); }

.btn-outline { background: white; border: 1px solid var(--border-color); color: var(--text-main); }
.btn-outline:hover { background: #f1f5f9; }

.btn-danger { background: white; border: 1px solid var(--danger); color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: white; }

.btn-secondary { background: #9ca3af; color: white; }
.btn-secondary:hover { background: #6b7280; }
.btn:disabled { opacity: 0.6; cursor: not-allowed; }

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

/* Item styles */
.items-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
}

.item-row {
    background: #f8fafc;
    border: 1px solid var(--border-color);
    padding: 15px;
    border-radius: 8px;
    margin-bottom: 15px;
    position: relative;
}

.item-row .btn-remove {
    position: absolute;
    top: 10px;
    right: 10px;
    background: none;
    border: none;
    color: var(--danger);
    cursor: pointer;
}

/* Main Preview Area */
.preview-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: #e2e8f0;
}

.preview-toolbar {
    padding: 15px 30px;
    background: white;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
}

.doc-type-selector {
    display: flex;
    gap: 10px;
}

.btn-doc {
    padding: 8px 20px;
    border-radius: 20px;
    border: 1px solid var(--primary);
    background: white;
    color: var(--primary);
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-doc.active {
    background: var(--primary);
    color: white;
}

.preview-actions {
    display: flex;
    gap: 10px;
}

.preview-scroll {
    flex: 1;
    overflow-y: auto;
    padding: 40px;
    display: flex;
    justify-content: center;
    align-items: flex-start;
}

/* A4 Paper styles */
.paper-a4 {
    background: white;
    width: 210mm;
    min-height: 297mm;
    padding: 10mm 12mm;
    box-sizing: border-box;
    box-shadow: 0 10px 25px rgba(0,0,0,0.1);
    font-family: 'Times New Roman', Times, serif;
    font-size: 14px;
    color: black;
    position: relative;
    line-height: 1.5;
}

/* Watermark */
.watermark {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 70%;
    height: 70%;
    display: flex;
    justify-content: center;
    align-items: center;
    opacity: 0.1;
    z-index: 0;
    pointer-events: none;
}
.watermark img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

.watermark.text-watermark {
    font-size: 50px;
    font-weight: 700;
    color: #00a9c8;
    opacity: 0.12;
    white-space: nowrap;
    text-transform: uppercase;
    transform: translate(-50%, -50%) rotate(-30deg);
}

/* Print specific */
@media print {
    body {
        margin: 0;
        background: white;
    }
    body * { visibility: hidden; }
    .app-container { display: block; height: auto; }
    .sidebar, .preview-toolbar { display: none !important; }
    .preview-area { background: none; overflow: visible !important; }
    .preview-scroll { padding: 0; overflow: visible !important; display: block; }
    #preview-content, #preview-content * {
        visibility: visible;
    }
    #preview-content {
        position: absolute;
        left: 0;
        top: 0;
        margin: 0;
        padding: 0;
        box-shadow: none;
        width: 100%;
        min-height: auto;
    }
    @page {
        size: A4;
        margin: 10mm 12mm;
    }
    
    .watermark {
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        z-index: -1;
    }

    .paper-a4 {
        height: auto !important;
        min-height: 297mm;
        overflow: visible !important;
        page-break-after: always;
    }

    .paper-a4:last-child {
        page-break-after: auto;
    }

    .paper-a4.order-layout {
        height: 297mm !important;
        page-break-after: avoid;
        overflow: hidden !important;
    }
}

/* Content specific styles inside paper */
.paper-a4.order-layout {
    overflow: hidden;
}

.paper-a4 .header-table {
    width: 100%;
    margin-bottom: 20px;
    border: none;
}
.paper-a4 .header-table td {
    vertical-align: top;
    padding: 2px 5px;
}

.paper-a4 h1.title {
    text-align: center;
    font-size: 20px;
    font-weight: bold;
    margin: 20px 0;
    text-transform: uppercase;
}

.paper-a4 .info-group {
    margin-bottom: 15px;
}

.paper-a4 .data-table {
    width: 100%;
    border-collapse: collapse;
    margin: 20px 0;
    font-size: 14px;
}

.paper-a4 .data-table th,
.paper-a4 .data-table td {
    border: 1px solid #000;
    padding: 6px 8px;
}

.paper-a4 .data-table th {
    font-weight: bold;
    text-align: center;
    background-color: #f2f2f2;
    font-size: 14px;
}

.paper-a4 .data-table td.num {
    text-align: right;
}

.paper-a4 .data-table td.center {
    text-align: center;
}

.paper-a4 .signature-section {
    display: flex;
    justify-content: space-around;
    margin-top: 40px;
    text-align: center;
    font-size: 14px;
}

.paper-a4 .signature-box {
    width: 40%;
}

.paper-a4 .signature-title {
    font-weight: bold;
}

.paper-a4 .signature-note {
    font-style: italic;
    font-size: 14px;
}

.paper-a4 .signature-name {
    margin-top: 40px;
    font-weight: bold;
}

table { 
    width: 100%;
    border-collapse: collapse;
    break-inside: avoid; 
    page-break-inside: avoid; 
}
tr, td, th { 
    break-inside: avoid; 
    page-break-inside: avoid; 
}

p, li {
    orphans: 3;
    widows: 3;
}

.paper-a4 p {
    margin: 4px 0;
    line-height: 1.5;
}

.paper-a4 li {
    margin: 3px 0;
}

.total-section {
    font-size: 14px;
    font-weight: bold;
}

.article-title {
    font-size: 15px;
    font-weight: bold;
}

.contract-content {
    box-sizing: border-box;
}

.contract-section,
.contract-article,
.signature-section,
.total-section,
.payment-section {
    break-inside: avoid;
    page-break-inside: avoid;
}

.contract-title,
.article-title,
.section-title {
    break-after: avoid;
    page-break-after: avoid;
}

.paper-a4.order-layout .header-table { margin-bottom: 10px; }
.paper-a4.order-layout h1.title { margin: 12px 0; font-size: 20px; }
.paper-a4.order-layout .signature-section {
    margin-top: 15px;
    page-break-inside: avoid;
    break-inside: avoid;
    display: flex;
    justify-content: space-around;
    text-align: center;
}
.paper-a4.order-layout .signature-name { margin-top: 40px; }

/* For contract layout */
.paper-a4 .contract-header {
    text-align: center;
    margin-bottom: 20px;
}
.paper-a4 .contract-header p {
    margin: 0;
    font-weight: bold;
}
.paper-a4 .contract-article {
    font-weight: bold;
    text-decoration: underline;
    margin-top: 15px;
    margin-bottom: 5px;
}
.paper-a4 .contract-content p {
    margin: 5px 0;
    text-align: justify;
}

.text-bold { font-weight: bold; }
.text-italic { font-style: italic; }
.text-center { text-align: center; }
.text-right { text-align: right; }

/* Mobile Responsive */
@media (max-width: 768px) {
    body {
        overflow-y: auto;
    }
    .app-container {
        flex-direction: column;
        height: auto;
        min-height: 100vh;
    }
    .sidebar {
        width: 100%;
        height: auto;
        flex: 1;
        border-right: none;
        border-bottom: 2px solid var(--primary);
    }
    .preview-area {
        height: auto;
    }
    .preview-toolbar {
        flex-direction: column;
        gap: 10px;
        align-items: stretch;
    }
    .doc-type-selector {
        flex-wrap: wrap;
        justify-content: center;
        margin-bottom: 10px;
    }
    .preview-actions {
        display: none; /* Hidden by default on mobile */
        justify-content: center;
        gap: 5px;
        flex-wrap: wrap;
    }
    .preview-scroll {
        display: none; /* Hidden by default on mobile */
        overflow-x: hidden;
        padding: 0;
        align-items: center;
        justify-content: center;
    }
    .paper-a4 {
        transform: scale(0.46);
        transform-origin: top left;
        margin-bottom: calc(-297mm * 0.54); /* Thu hồi lại khoảng trống bị dư thừa do scale */
    }
    
    /* Flow Mode: Preview Active */
    body.mobile-preview-mode .sidebar {
        display: none;
    }
    body.mobile-preview-mode .preview-area {
        height: 100vh;
        display: flex;
        flex-direction: column;
    }
    body.mobile-preview-mode .preview-scroll,
    body.mobile-preview-mode .preview-actions {
        display: flex;
    }
    body.mobile-preview-mode .preview-scroll {
        display: block;
    }
    
    #btn-back-input {
        display: block !important;
        width: 100%;
        margin-bottom: 10px;
        font-weight: bold;
    }
    .preview-actions button {
        flex: 1;
        font-size: 13px;
        padding: 8px 5px;
    }
}
