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

:root {
    --primary: #3b82f6;
    --primary-hover: #2563eb;
    --secondary: #6b7280;
    --secondary-hover: #4b5563;
    --success: #10b981;
    --danger: #ef4444;
    --danger-hover: #dc2626;
    --warning: #f59e0b;
    --info: #0ea5e9;
    --bg-light: #f9fafb;
    --bg-white: #ffffff;
    --border: #e5e7eb;
    --text-dark: #1f2937;
    --text-light: #6b7280;
    --shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    background-color: var(--bg-light);
    color: var(--text-dark);
    line-height: 1.5;
}

/* Dashboard Container */
.dashboard-container {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.dashboard-header {
    background: var(--bg-white);
    padding: 1.5rem;
    box-shadow: var(--shadow);
    border-bottom: 1px solid var(--border);
}

.dashboard-header h1 {
    font-size: 1.875rem;
    margin-bottom: 1rem;
    color: var(--text-dark);
}

.header-actions {
    display: flex;
    gap: 0.75rem;
    flex-wrap: wrap;
}

.dashboard-main {
    flex: 1;
    padding: 1.5rem;
}

/* Search & Sort */
.search-container {
    display: flex;
    gap: 1rem;
    margin-bottom: 1.5rem;
    flex-wrap: wrap;
}

.search-input,
.sort-select {
    padding: 0.75rem 1rem;
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    font-size: 0.95rem;
    background: var(--bg-white);
    color: var(--text-dark);
    flex: 1;
    min-width: 200px;
}

.search-input:focus,
.sort-select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

/* Buttons */
.btn {
    padding: 0.75rem 1.25rem;
    border: none;
    border-radius: 0.5rem;
    font-size: 0.95rem;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    white-space: nowrap;
}

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

.btn-primary:hover {
    background: var(--primary-hover);
    box-shadow: var(--shadow-lg);
}

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

.btn-secondary:hover {
    background: var(--secondary-hover);
}

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

.btn-info:hover {
    background: #0284c7;
}

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

.btn-danger:hover {
    background: var(--danger-hover);
}

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

.btn-link {
    background: transparent;
    color: var(--primary);
    text-decoration: underline;
    padding: 0;
}

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

.btn-sm {
    padding: 0.5rem 0.75rem;
    font-size: 0.85rem;
}

/* Table */
.table-wrapper {
    background: var(--bg-white);
    border-radius: 0.5rem;
    box-shadow: var(--shadow);
    overflow-x: auto;
}

.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.95rem;
}

.data-table thead {
    background: var(--bg-light);
    border-bottom: 2px solid var(--border);
}

.data-table th {
    padding: 1rem;
    text-align: left;
    font-weight: 600;
    color: var(--text-dark);
    position: relative;
    user-select: none;
}

.data-table th.action-col {
    min-width: 120px;
    text-align: center;
}

.data-table td {
    padding: 1rem;
    border-bottom: 1px solid var(--border);
    color: var(--text-dark);
}

.data-table tbody tr {
    transition: background-color 0.2s ease;
}

.data-table tbody tr:hover {
    background: var(--bg-light);
}

.data-table td.editable {
    cursor: pointer;
    position: relative;
    max-width: 300px;
    overflow: hidden;
    text-overflow: ellipsis;
}

.data-table td.editable:hover::after {
    content: '✎';
    position: absolute;
    right: 0.5rem;
    color: var(--primary);
    font-size: 0.85rem;
}

.edit-input {
    width: 100%;
    padding: 0.5rem;
    border: 1px solid var(--primary);
    border-radius: 0.25rem;
    font-size: 0.95rem;
    font-family: inherit;
}

.edit-input:focus {
    outline: none;
    box-shadow: 0 0 0 2px rgba(59, 130, 246, 0.1);
}

.action-buttons {
    display: flex;
    gap: 0.5rem;
    justify-content: center;
    flex-wrap: wrap;
}

.col-header-actions {
    display: flex;
    gap: 0.25rem;
    align-items: center;
}

.col-header-actions button {
    padding: 0.25rem;
    background: transparent;
    border: none;
    color: var(--text-light);
    cursor: pointer;
    font-size: 0.8rem;
    opacity: 0;
    transition: opacity 0.2s;
}

.data-table th:hover .col-header-actions button {
    opacity: 1;
}

.col-header-actions button:hover {
    color: var(--danger);
}

/* Empty State */
.empty-state {
    text-align: center;
    padding: 3rem 1.5rem;
    color: var(--text-light);
}

.empty-state p {
    font-size: 1.1rem;
    margin-bottom: 1rem;
}

/* Modal */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: var(--bg-white);
    border-radius: 0.75rem;
    box-shadow: var(--shadow-lg);
    width: 100%;
    max-width: 600px;
    max-height: 90vh;
    overflow-y: auto;
    animation: slideUp 0.3s ease;
}

.modal-content.modal-sm {
    max-width: 400px;
}

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

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1.5rem;
    border-bottom: 1px solid var(--border);
}

.modal-header h2 {
    font-size: 1.25rem;
    margin: 0;
}

.close-btn {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--text-light);
    transition: color 0.2s;
}

.close-btn:hover {
    color: var(--text-dark);
}

.modal-body {
    padding: 1.5rem;
}

.modal-footer {
    display: flex;
    gap: 1rem;
    justify-content: flex-end;
    padding: 1.5rem;
    border-top: 1px solid var(--border);
}

/* Form */
.form-group {
    margin-bottom: 1.5rem;
}

.form-group label {
    display: block;
    margin-bottom: 0.5rem;
    font-weight: 500;
    color: var(--text-dark);
}

.form-input {
    width: 100%;
    padding: 0.75rem;
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    font-size: 0.95rem;
    font-family: inherit;
    background: var(--bg-white);
    color: var(--text-dark);
}

.form-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

/* Toast */
.toast {
    position: fixed;
    bottom: 1.5rem;
    right: 1.5rem;
    padding: 1rem 1.5rem;
    background: var(--success);
    color: white;
    border-radius: 0.5rem;
    box-shadow: var(--shadow-lg);
    opacity: 0;
    transform: translateY(20px);
    transition: all 0.3s ease;
    z-index: 2000;
    max-width: 400px;
}

.toast.show {
    opacity: 1;
    transform: translateY(0);
}

.toast.error {
    background: var(--danger);
}

.toast.warning {
    background: var(--warning);
}

.toast.info {
    background: var(--info);
}

/* Responsive Design */
@media (max-width: 768px) {
    .dashboard-header {
        padding: 1rem;
    }

    .dashboard-header h1 {
        font-size: 1.5rem;
        margin-bottom: 0.75rem;
    }

    .header-actions {
        gap: 0.5rem;
    }

    .btn {
        padding: 0.625rem 1rem;
        font-size: 0.875rem;
    }

    .search-container {
        flex-direction: column;
        gap: 0.75rem;
    }

    .search-input,
    .sort-select {
        min-width: auto;
    }

    .data-table {
        font-size: 0.85rem;
    }

    .data-table th,
    .data-table td {
        padding: 0.75rem 0.5rem;
    }

    .action-buttons {
        gap: 0.25rem;
    }

    .btn-sm {
        padding: 0.375rem 0.5rem;
        font-size: 0.75rem;
    }

    .modal-content {
        max-width: 95%;
    }

    .modal-body {
        padding: 1rem;
    }

    .modal-footer {
        flex-direction: column;
        gap: 0.75rem;
    }

    .modal-footer .btn {
        width: 100%;
    }

    .toast {
        right: 1rem;
        left: 1rem;
    }
}

@media (max-width: 480px) {
    .dashboard-main {
        padding: 1rem;
    }

    .dashboard-header h1 {
        font-size: 1.25rem;
    }

    .header-actions {
        flex-direction: column;
    }

    .header-actions .btn {
        width: 100%;
        justify-content: center;
    }

    .data-table th.action-col {
        min-width: 100px;
    }

    .btn-sm {
        padding: 0.25rem 0.5rem;
        font-size: 0.65rem;
    }

    .action-buttons {
        gap: 0.25rem;
    }
}

/* Print Styles */
@media print {
    .dashboard-header,
    .search-container,
    .action-buttons,
    .modal {
        display: none;
    }

    .data-table {
        box-shadow: none;
    }

    .data-table tr {
        page-break-inside: avoid;
    }
}

/* Enhanced table controls */
.dashboard-footer { display:flex; justify-content:space-between; align-items:center; gap:1rem; padding:1rem 1.5rem; background:var(--bg-white); border-top:1px solid var(--border); }
.footer-hint { color:var(--text-light); margin-left:.5rem; font-size:.9rem; }
.select-col { width:46px; min-width:46px; text-align:center !important; }
.select-col input { width:18px; height:18px; cursor:pointer; }
.column-heading { display:flex; align-items:center; gap:.5rem; min-width:150px; }
.column-title { flex:1; }
.drag-handle { cursor:grab; color:var(--text-light); letter-spacing:-2px; font-weight:700; }
.draggable-col.dragging { opacity:.45; }
.draggable-col.drag-over { box-shadow: inset 3px 0 0 var(--primary); }
.col-header-actions { margin-left:auto; }
.icon-btn { border:0; background:transparent; cursor:pointer; padding:.25rem .35rem; color:var(--text-light); font-size:.9rem; }
.icon-btn:hover { color:var(--primary); }
.delete-column:hover { color:var(--danger); }
.form-help { color:var(--text-light); font-size:.85rem; display:block; margin-top:.4rem; }
.multi-options { display:grid; gap:.5rem; padding:.75rem; border:1px solid var(--border); border-radius:.5rem; background:var(--bg-light); }
.check-row { display:flex !important; gap:.55rem; align-items:center; margin:0 !important; font-weight:400 !important; }
.check-row input { width:18px; height:18px; }
.data-table tfoot { background:var(--bg-light); border-top:2px solid var(--border); }
.data-table tfoot td { font-weight:600; white-space:nowrap; }
.footer-label { color:var(--text-light); text-align:center; }
.calculation-cell { color:var(--primary); }
.calculation-setting { display:grid; grid-template-columns:minmax(0,1fr) minmax(180px,240px); gap:1rem; align-items:center; padding:1rem 0; border-bottom:1px solid var(--border); }
.calculation-setting strong, .calculation-setting small { display:block; }
.calculation-setting small { color:var(--text-light); text-transform:capitalize; margin-top:.15rem; }
.settings-intro { margin-top:0; margin-bottom:.5rem; }
.btn:disabled { opacity:.5; cursor:not-allowed; box-shadow:none; }

@media (max-width:768px){
    .dashboard-footer { flex-direction:column; align-items:stretch; }
    .dashboard-footer .btn { justify-content:center; }
    .footer-hint { display:block; margin:.25rem 0 0; }
    .calculation-setting { grid-template-columns:1fr; gap:.5rem; }
}

/* Auto Sum controls */
.auto-sum-toggle {
    display: inline-flex;
    align-items: center;
    gap: 0.45rem;
    cursor: pointer;
    font-weight: 600;
    white-space: nowrap;
}

.auto-sum-toggle input,
.calculation-check-row input {
    width: 1rem;
    height: 1rem;
    cursor: pointer;
}

.auto-sum-value {
    margin-top: 0.35rem;
    font-weight: 700;
    color: var(--text-dark);
}

.auto-sum-na {
    color: var(--text-light);
}

.calculation-check-row {
    cursor: pointer;
}

.auto-sum-setting-control {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 600;
    white-space: nowrap;
}

/* Analysis modal */
.analysis-modal-content { width:min(96vw, 1500px); max-width:1500px; }
.analysis-body { display:grid; grid-template-columns:minmax(320px, 400px) minmax(0, 1fr); gap:1.25rem; align-items:start; }
.analysis-controls { display:grid; gap:1rem; }
.analysis-axis-grid { display:grid; grid-template-columns:1fr 1fr; gap:.75rem; }
.analysis-axis-grid .form-group:last-child { grid-column:1 / -1; }
.analysis-filter-section { border-top:1px solid var(--border); padding-top:1rem; }
.analysis-section-heading { display:flex; align-items:center; justify-content:space-between; gap:.75rem; margin-bottom:.75rem; }
.analysis-section-heading small { display:block; color:var(--text-light); margin-top:.15rem; }
.analysis-filters { display:grid; gap:.65rem; }
.analysis-filter-row { display:grid; grid-template-columns:minmax(100px,1fr) minmax(105px,.8fr) minmax(100px,1fr) auto; gap:.5rem; align-items:end; }
.analysis-filter-row .form-input { min-width:0; }
.analysis-filter-remove { align-self:stretch; padding:.55rem .7rem; }
.analysis-actions { display:flex; justify-content:flex-end; gap:.75rem; border-top:1px solid var(--border); padding-top:1rem; }
.analysis-result { min-width:0; }
.analysis-summary { color:var(--text-light); margin-bottom:.75rem; font-size:.9rem; }
.chart-shell { position:relative; width:100%; min-height:520px; border:1px solid var(--border); border-radius:.65rem; background:var(--bg-white); overflow:hidden; }
#analysisCanvas { display:block; width:100%; height:520px; }
.chart-empty { position:absolute; inset:0; display:flex; align-items:center; justify-content:center; padding:1rem; color:var(--text-light); text-align:center; pointer-events:none; }
.chart-legend { display:flex; flex-wrap:wrap; gap:.5rem .85rem; margin-top:.75rem; font-size:.85rem; }
.chart-legend-item { display:inline-flex; align-items:center; gap:.35rem; }
.chart-legend-swatch { width:.75rem; height:.75rem; border-radius:.2rem; border:1px solid var(--border); }
.analysis-help { color:var(--text-light); font-size:.85rem; margin-top:.4rem; }
@media (max-width: 980px) {
    .analysis-modal-content { width:min(96vw, 900px); }
    .analysis-body { grid-template-columns:1fr; }
    .chart-shell { min-height:340px; }
    #analysisCanvas { height:340px; }
}
@media (max-width: 560px) {
    .analysis-axis-grid { grid-template-columns:1fr; }
    .analysis-axis-grid .form-group:last-child { grid-column:auto; }
    .analysis-filter-row { grid-template-columns:1fr 1fr; }
    .analysis-filter-row .filter-value { grid-column:1 / -1; }
    .analysis-filter-remove { grid-column:1 / -1; justify-content:center; }
    .analysis-actions { flex-direction:column-reverse; }
    .analysis-actions .btn { width:100%; justify-content:center; }
}

/* Print settings modal */
.print-modal-content {
    width: min(97vw, 1600px);
    max-width: 1600px;
}


.print-settings-body {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(440px, 1fr);
    gap: 1.25rem;
    align-items: start;
}

.print-settings-body > .print-settings-section {
    grid-column: 1;
}

.print-settings-body > .print-preview-section {
    grid-column: 2;
    grid-row: 1 / span 4;
    position: sticky;
    top: 0;
}

.print-settings-section,
.print-preview-section {
    border: 1px solid var(--border-color, #e5e7eb);
    border-radius: .75rem;
    padding: 1rem;
    background: var(--card-bg, #fff);
}

.print-section-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: .9rem;
}

.print-section-heading > div {
    display: grid;
    gap: .2rem;
}

.print-section-heading small {
    color: var(--text-secondary, #64748b);
}


.print-wysiwyg {
    border: 1px solid var(--border-color, #d1d5db);
    border-radius: .65rem;
    overflow: hidden;
    background: var(--card-bg, #fff);
}

.print-wysiwyg-toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: .4rem;
    padding: .55rem;
    border-bottom: 1px solid var(--border-color, #e5e7eb);
    background: var(--bg-secondary, #f8fafc);
}

.print-header-editor {
    min-height: 120px;
    padding: .8rem;
    outline: none;
    line-height: 1.5;
}

.print-header-editor:empty::before {
    content: attr(data-placeholder);
    color: var(--text-muted, #94a3b8);
    pointer-events: none;
}

.print-header-editor ul,
.print-header-editor ol {
    padding-left: 1.5rem;
}

.print-margin-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: .8rem;
}

.print-preview-header {
    padding: .85rem;
    border-bottom: 1px solid #e5e7eb;
    background: #fff;
    color: #111827;
}

.print-preview-header:empty { display:none; }

.print-color-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 1rem;
}

.color-input-wrap {
    display: grid;
    grid-template-columns: 52px minmax(0, 1fr);
    gap: .6rem;
    align-items: center;
    margin-top: .4rem;
}

.color-input-wrap input[type="color"] {
    width: 52px;
    height: 42px;
    padding: 2px;
    border: 1px solid var(--border-color, #d1d5db);
    border-radius: .5rem;
    background: transparent;
    cursor: pointer;
}

.print-column-highlights {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: .65rem;
}

.print-highlight-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(145px, .55fr);
    gap: .75rem;
    align-items: center;
    border: 1px solid var(--border-color, #e5e7eb);
    border-radius: .65rem;
    padding: .65rem .75rem;
}

.print-highlight-toggle {
    display: flex;
    gap: .55rem;
    align-items: center;
    min-width: 0;
}

.print-highlight-toggle span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.print-highlight-row select:disabled {
    opacity: .55;
    cursor: not-allowed;
}


.print-column-controls {
    display: flex;
    align-items: center;
    gap: .75rem;
    min-width: 0;
}

.print-display-toggle {
    display: inline-flex;
    align-items: center;
    gap: .35rem;
    white-space: nowrap;
    font-size: .82rem;
    color: var(--text-muted);
}

.print-empty-columns {
    padding: 1rem;
    text-align: center;
    color: var(--text-muted);
    border: 1px dashed var(--border);
    border-radius: .5rem;
}
.print-preview-wrap {
    overflow: auto;
    border: 1px solid var(--border-color, #e5e7eb);
    border-radius: .6rem;
    background: #eef2f7;
    padding: .8rem;
    min-height: 520px;
    max-height: 72vh;
}

.print-preview-scale {
    width: 200%;
    transform: scale(.5);
    transform-origin: top left;
}


.print-preview-page {
    width: 210mm;
    min-width: 210mm;
    background: #fff;
    color: #111827;
    box-shadow: 0 1px 4px rgba(15, 23, 42, .12);
    transition: padding .15s ease;
}

.print-preview-empty {
    padding: .8rem;
    margin: 0;
}

.print-preview-table {
    width: 100%;
    min-width: 0;
    border-collapse: collapse;
    background: #fff;
    color: #111827;
}

.print-preview-table th,
.print-preview-table td {
    border: 1px solid #d1d5db;
    padding: .55rem .65rem;
    text-align: left;
    vertical-align: top;
}

.print-preview-table tfoot td {
    font-weight: 600;
    background: #f8fafc;
}

@media (max-width: 1050px) {
    .print-settings-body { grid-template-columns: 1fr; }
    .print-settings-body > .print-settings-section,
    .print-settings-body > .print-preview-section { grid-column: 1; grid-row: auto; }
    .print-settings-body > .print-preview-section { position: static; }
    .print-preview-wrap { min-height: 420px; max-height: 60vh; }
}

@media (max-width: 760px) {
    .print-modal-content { width: 96vw; }
    .print-color-grid,
    .print-column-highlights { grid-template-columns: 1fr; }
    .print-margin-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .print-section-heading { align-items: flex-start; flex-direction: column; }
    .print-highlight-row { grid-template-columns: minmax(0, 1fr) 140px; }
}

/* Print header image + font controls */
.print-toolbar-field {
    display: inline-flex;
    align-items: center;
    gap: .35rem;
    font-size: .78rem;
    color: var(--text-secondary, #64748b);
}

.print-font-size-select {
    width: auto;
    min-width: 78px;
    padding: .3rem .45rem;
    font-size: .8rem;
}

.print-image-upload-btn {
    position: relative;
    cursor: pointer;
}

.print-image-size-control {
    min-width: 230px;
}

.print-image-size-control input[type="range"] {
    width: 120px;
}

#printHeaderImageWidthValue {
    min-width: 44px;
    color: var(--text-dark, #111827);
    font-size: .78rem;
}

.print-header-editor img,
.print-preview-header img,
.print-custom-header img {
    max-width: 100%;
    height: auto;
    vertical-align: middle;
}

.print-header-editor img {
    cursor: pointer;
    outline: 2px solid transparent;
    outline-offset: 2px;
}

.print-header-editor img.print-header-image-selected {
    outline-color: var(--primary, #2563eb);
}

@media (max-width: 700px) {
    .print-image-size-control {
        width: 100%;
        min-width: 0;
    }
    .print-image-size-control input[type="range"] {
        flex: 1;
    }
}
