/* The #historicalQuickFilters bar uses the same .active-filter-bar and .neutral styles as the detail filter bar. */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background: linear-gradient(135deg, #1e3a5f 0%, #2c5282 100%);
    min-height: 100vh;
    height: 100vh;
    padding: 10px;
    color: #333;
    overflow: hidden;
}

.container {
    margin: 0 auto;
    width: 100%;
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

header {
    text-align: center;
    color: white;
    margin-bottom: 10px;
    padding: 10px;
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
    border-radius: 10px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
}

header h1 {
    font-size: 1.5em;
    margin-bottom: 5px;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
}

.subtitle {
    font-size: 0.9em;
    opacity: 0.9;
}

.dashboard {
    display: flex;
    flex-direction: column;
    gap: 10px;
    height: calc(100vh - 200px);
    overflow: auto;
}

.filters-bar {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    border-radius: 10px;
    padding: 10px 15px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
    display: flex;
    align-items: center;
    gap: 15px;
    flex-wrap: wrap;
    margin-bottom: 10px;
}

/* Persistent active-filter chips bar — gentle amber warning so users
   know data is filtered, regardless of Detail/Pivot/Charts view. */
.active-filter-bar {
    background: #fff8e1;
    border: 1px solid #ffd54f;
    border-left: 4px solid #ffa000;
    border-radius: 8px;
    padding: 8px 12px;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 8px;
    box-shadow: 0 2px 6px rgba(255, 160, 0, 0.12);
    font-size: 0.92em;
    color: #6d4c00;
}
/* Neutral state when bar only shows quick-filter dropdowns and nothing is active */
.active-filter-bar.neutral {
    background: rgba(255, 255, 255, 0.9);
    border: 1px solid #e2e8f0;
    border-left: 4px solid #a0aec0;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.05);
    color: #4a5568;
}
.active-filter-bar .persistent-filter {
    padding: 4px 8px;
    border: 1px solid #cbd5e0;
    border-radius: 6px;
    background: #fff;
    font-size: 0.9em;
    min-width: 120px;
    max-width: 200px;
}
.active-filter-bar .persistent-filter:focus {
    outline: none;
    border-color: #ffa000;
    box-shadow: 0 0 0 2px rgba(255, 160, 0, 0.2);
}
.active-filter-bar .detail-global-search {
    padding: 4px 10px;
    border: 1px solid #cbd5e0;
    border-radius: 6px;
    background: #fff;
    font-size: 0.9em;
    min-width: 220px;
    max-width: 320px;
}
.active-filter-bar .detail-global-search:focus {
    outline: none;
    border-color: #ffa000;
    box-shadow: 0 0 0 2px rgba(255, 160, 0, 0.2);
}
.filter-warning-icon {
    font-size: 1.05em;
    line-height: 1;
}
.filter-warning-label {
    font-weight: 600;
    color: #8a5a00;
    margin-right: 4px;
}
.filter-chip {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: #fff;
    border: 1px solid #ffcc80;
    color: #5d4037;
    padding: 3px 6px 3px 10px;
    border-radius: 14px;
    font-size: 0.88em;
    line-height: 1.2;
}
.filter-chip strong {
    color: #4e342e;
    font-weight: 600;
}
.filter-chip-remove {
    background: transparent;
    border: none;
    color: #8a5a00;
    cursor: pointer;
    font-size: 1em;
    line-height: 1;
    padding: 0 2px;
    border-radius: 50%;
}
.filter-chip-remove:hover {
    background: #ffe0b2;
    color: #bf360c;
}
.filter-clear-all {
    margin-left: auto;
    background: #fff;
    border: 1px solid #ffa000;
    color: #8a5a00;
    padding: 3px 10px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.85em;
    font-weight: 600;
}
.filter-clear-all:hover {
    background: #ffa000;
    color: #fff;
}

.filter-group {
    display: flex;
 

.card.collapsed .collapsible-content {
    display: none;
}

.collapsible {
    cursor: pointer;
    user-select: none;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.collapsible:hover {
    color: #764ba2;
}

.toggle-icon {
    transition: transform 0.3s ease;
}

.card:not(.collapsed) .toggle-icon {
    transform: rotate(180deg);
}

.card-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
    gap: 20px;
    margin-top: 20px;
}

.mini-card {
    background: #f8f9fa;
    border-radius: 10px;
    padding: 20px;
}   align-items: center;
    gap: 10px;
}

.filter-group label {
    font-weight: 600;
    color: #2c5282;
}

.filter-select {
    padding: 6px 10px;
    border: 2px solid #e9ecef;
    border-radius: 8px;
    font-size: 0.9em;
    background: white;
    cursor: pointer;
    transition: border-color 0.3s ease;
    min-width: 130px;
}

.filter-select:focus {
    outline: none;
    border-color: #667eea;
}

.chart-fullscreen {
    background: white;
    border-radius: 10px;
    padding: 15px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.15);
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 400px;
    overflow: auto;
}

.chart-fullscreen h2 {
    color: #2c5282;
    font-size: 1.2em;
    margin: 0;
}

.chart-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    padding-bottom: 8px;
    border-bottom: 2px solid #2c5282;
    flex-wrap: wrap;
    gap: 15px;
    flex-shrink: 0;
}

.chart-options {
    display: flex;
    gap: 15px;
    align-items: center;
}

.checkbox-label {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    font-size: 0.95em;
    color: #2c5282;
    font-weight: 500;
    user-select: none;
}

.checkbox-label input[type="checkbox"] {
    cursor: pointer;
    width: 18px;
    height: 18px;
    accent-color: #667eea;
}

.checkbox-label:hover {
    color: #1e3a5f;
}

.chart-fullscreen canvas {
    flex: 1;
    min-height: 350px;
}

/* Chart Tabs */
.chart-tabs {
    display: flex;
    gap: 5px;
    margin-bottom: 12px;
    flex-wrap: wrap;
}

.chart-tab {
    padding: 8px 16px;
    background: #f0f4ff;
    color: #2c5282;
    border: 2px solid transparent;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.9em;
    font-weight: 600;
    transition: all 0.3s ease;
    flex: 1;
    min-width: 100px;
}

.chart-tab:hover {
    background: #e0e7ff;
    transform: translateY(-2px);
}

.chart-tab.active {
    background: linear-gradient(135deg, #2c5282 0%, #1e3a5f 100%);
    color: white;
    border-color: #2c5282;
    box-shadow: 0 4px 8px rgba(44, 82, 130, 0.3);
}


.data-table-container {
    background: white;
    border-radius: 10px;
    padding: 15px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.15);
    display: flex;
    flex-direction: column;
    height: 45vh;
    min-height: 300px;
    overflow: hidden;
}

.data-table-container h2 {
    color: #2c5282;
    margin-bottom: 10px;
    font-size: 1.2em;
    border-bottom: 2px solid #2c5282;
    padding-bottom: 8px;
    flex-shrink: 0;
}

.table-wrapper {
    overflow: auto;
    flex: 1;
}

.monthly-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.75em;
    margin-top: 5px;
}

.monthly-table thead {
    background: linear-gradient(135deg, #2c5282 0%, #1e3a5f 100%);
    color: white;
}

.monthly-table th {
    padding: 8px 10px;
    text-align: center;
    font-weight: 600;
    position: sticky;
    top: 0;
    z-index: 10;
}

.monthly-table th.year-header {
    text-align: left;
    padding-left: 15px;
    min-width: 80px;
}

.monthly-table td {
    padding: 4px 10px;
    text-align: right;
    border-bottom: 1px solid #e9ecef;
}

.monthly-table td.year-cell {
    font-weight: 700;
    color: #2c5282;
    text-align: left;
    padding-left: 15px;
    background: #f8f9fa;
}

.monthly-table tbody tr:nth-child(even) {
    background-color: #fafbfc;
}

.monthly-table tbody tr:nth-child(odd) {
    background-color: white;
}

.monthly-table tr:hover {
    background: #e8f2f7 !important;
}

.monthly-table .company-row-tta {
    border-left: 4px solid #2c5282;
}

.monthly-table .company-row-ps {
    border-left: 4px solid #38a169;
}

.monthly-table .company-row-both {
    border-left: 4px solid #3182ce;
}

.monthly-table .total-cell {
    font-weight: 700;
    background: #f8f9fa;
    border-left: 2px solid #2c5282;
}

/* YoY column */
.monthly-table .yoy-cell {
    font-weight: 700;
    font-size: 0.95em;
    text-align: center;
    border-left: 2px solid #cbd5e0;
}
.monthly-table .yoy-up   { background: rgba(56, 161, 105, 0.15); color: #1f6f43; }
.monthly-table .yoy-down { background: rgba(229, 62, 62, 0.12);  color: #9b2c2c; }
.monthly-table .yoy-na   { color: #a0aec0; background: #f8f9fa; }

.card {
    background: white;
    border-radius: 15px;
    padding: 25px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
    transition: transform 0.3s ease, box-shadow 0.3s ease;
}

.card:hover {
    transform: translateY(-5px);
    box-shadow: 0 15px 40px rgba(0, 0, 0, 0.2);
}

.card.full-width {
    grid-column: 1 / -1;
}

.card h2 {
    color: #667eea;
    margin-bottom: 20px;
    font-size: 1.5em;
    border-bottom: 3px solid #667eea;
    padding-bottom: 10px;
}

.status-indicator {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 15px;
    background: #f0f4ff;
    border-radius: 8px;
}

.status-dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: #ffc107;
    animation: pulse 2s infinite;
}

.status-dot.connected {
    background: #4caf50;
}

.status-dot.error {
    background: #f44336;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.tables-container {
    display: flex;
    flex-direction: column;
    gap: 10px;
    max-height: 400px;
    overflow-y: auto;
}

.table-item {
    padding: 15px;
    background: #f8f9fa;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.3s ease;
    border-left: 4px solid transparent;
}

.table-item:hover {
    background: #e9ecef;
    border-left-color: #667eea;
    transform: translateX(5px);
}

.table-item.active {
    background: #667eea;
    color: white;
    border-left-color: #764ba2;
}

.table-item-name {
    font-weight: 600;
    font-size: 1.1em;
}

.table-item-count {
    font-size: 0.9em;
    opacity: 0.8;
    margin-top: 5px;
}

.table-controls {
    display: flex;
    gap: 10px;
    margin-bottom: 15px;
}

.btn {
    padding: 6px 12px;
    background: #667eea;
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.9em;
    transition: all 0.3s ease;
}

.btn:hover {
    background: #764ba2;
    transform: scale(1.05);
}

.btn-primary {
    background: #4caf50;
}

.btn-primary:hover {
    background: #45a049;
}

.search-input {
    flex: 1;
    padding: 10px 15px;
    border: 2px solid #e9ecef;
    border-radius: 8px;
    font-size: 1em;
    transition: border-color 0.3s ease;
}

.search-input:focus {
    outline: none;
    border-color: #667eea;
}

.table-scroll {
    overflow-x: auto;
    max-height: 500px;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9em;
}

table th {
    background: #667eea;
    color: white;
    padding: 12px;
    text-align: left;
    position: sticky;
    top: 0;
    z-index: 10;
}

table td {
    padding: 12px;
    border-bottom: 1px solid #e9ecef;
}

table tr:hover {
    background: #f8f9fa;
}

.query-input {
    width: 100%;
    min-height: 120px;
    padding: 15px;
    border: 2px solid #e9ecef;
    border-radius: 8px;
    font-family: 'Courier New', monospace;
    font-size: 0.95em;
    resize: vertical;
    margin-bottom: 15px;
}

.query-input:focus {
 

.mini-card canvas {
    max-height: 250px;
}   outline: none;
    border-color: #667eea;
}

.loading {
    text-align: center;
    padding: 20px;
    color: #999;
}

.info-text {
    text-align: center;
    padding: 40px;
    color: #999;
    font-style: italic;
}

.error-text {
    color: #f44336;
    padding: 15px;
    background: #ffebee;
    border-radius: 8px;
    margin-top: 10px;
}

footer {
    text-align: center;
    color: white;
    padding: 8px;
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
    border-radius: 8px;
    margin-top: 10px;
    font-size: 0.85em;
}

canvas {
    max-height: 300px;
}

#historicalChart {
    max-height: 400px;
}

.company-selector {
    display: flex;
    align-items: center;
    gap: 15px;
    margin-bottom: 20px;
    padding: 15px;
    background: #f8f9fa;
    border-radius: 8px;
    flex-wrap: wrap;
}

.company-label {
    display: flex;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    user-select: none;
}

.company-label input[type="checkbox"] {
    width: 18px;
    height: 18px;
    cursor: pointer;
}

.company-badge {
    padding: 8px 16px;
    border-radius: 6px;
    font-weight: 600;
    font-size: 0.95em;
    transition: all 0.3s ease;
}

.company-badge.tta {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.company-badge.ps {
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
    color: white;
}

.company-label input[type="checkbox"]:not(:checked) + .company-badge {
    opacity: 0.4;
    filter: grayscale(50%);
}

.table-select {
    padding: 10px 15px;
    border: 2px solid #e9ecef;
    border-radius: 8px;
    font-size: 1em;
    background: white;
    cursor: pointer;
    transition: border-color 0.3s ease;
    flex: 1;
    min-width: 200px;
}

.table-select:focus {
    outline: none;
    border-color: #667eea;
}

/* Responsive design */
@media (max-width: 1200px) {
    .data-table-container,
    .chart-fullscreen {
        height: 400px;
    }
}

@media (max-width: 768px) {
    body {
        padding: 5px;
        overflow-y: auto;
    }

    .container {
        height: auto;
        min-height: 100vh;
    }

    header {
        margin-bottom: 8px;
        padding: 8px;
    }

    header h1 {
        font-size: 1.3em;
    }
    
    .filters-bar {
        padding: 10px;
        gap: 10px;
        flex-direction: column;
        align-items: stretch;
    }

    .filter-group {
        flex-direction: column;
        align-items: stretch;
        gap: 5px;
    }

    .filter-select {
        width: 100%;
        min-width: auto;
        padding: 10px;
        font-size: 16px; /* Prevents zoom on iOS */
    }

    .dashboard {
        height: auto;
        gap: 12px;
        overflow-y: visible;
    }

    .data-table-container,
    .chart-fullscreen {
        height: auto;
        min-height: 250px;
        padding: 12px;
    }

    .data-table-container h2,
    .chart-fullscreen h2 {
        font-size: 1em;
        margin-bottom: 8px;
    }

    .table-wrapper {
        max-height: 400px;
        overflow: auto;
        -webkit-overflow-scrolling: touch;
    }

    .monthly-table {
        font-size: 0.65em;
    }

    .monthly-table th,
    .monthly-table td {
        padding: 6px 4px;
    }

    .monthly-table th.year-header,
    .monthly-table td.year-cell {
        padding-left: 8px;
    }

    .chart-fullscreen {
        height: 300px;
    }

    .chart-fullscreen canvas {
        max-height: 250px !important;
    }

    .chart-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
    }

    .chart-options {
        width: 100%;
    }

    .checkbox-label {
        font-size: 0.85em;
    }
    
    .card {
        padding: 15px;
    }

    .card-grid {
        grid-template-columns: 1fr;
        gap: 15px;
    }

    .company-selector {
        flex-direction: column;
        align-items: stretch;
        gap: 10px;
        padding: 10px;
    }

    .table-controls {
        flex-direction: column;
    }

    .search-input {
        width: 100%;
    }

    .btn {
        width: 100%;
        padding: 12px;
        font-size: 1em;
    }

    .table-select {
        font-size: 16px; /* Prevents zoom on iOS */
    }
}

/* Small phones - portrait */
@media (max-width: 480px) {
    header h1 {
        font-size: 1.1em;
    }

    .filters-bar {
        padding: 8px;
    }

    .data-table-container,
    .chart-fullscreen {
        padding: 10px;
    }

    .data-table-container h2,
    .chart-fullscreen h2 {
        font-size: 0.9em;
    }

    .monthly-table {
        font-size: 0.6em;
    }

    .monthly-table th,
    .monthly-table td {
        padding: 4px 2px;
    }

    .chart-fullscreen {
        height: 250px;
    }

    .chart-fullscreen canvas {
        max-height: 200px !important;
    }
}

/* Landscape phones */
@media (max-width: 900px) and (orientation: landscape) {
    body {
        overflow-y: auto;
    }

    .container {
        height: auto;
    }

    .dashboard {
        overflow-y: visible;
    }

    .data-table-container,
    .chart-fullscreen {
        height: 350px;
    }
}

/* Touch device optimizations */
@media (pointer: coarse) {
    .filter-select,
    .btn,
    .table-select,
    .search-input {
        min-height: 44px; /* Apple's recommended touch target */
    }

    .checkbox-label input[type="checkbox"] {
        width: 20px;
        height: 20px;
        min-width: 20px;
    }

    .company-label {
        padding: 5px;
        min-height: 44px;
    }

    .table-item {
        padding: 12px;
    }
}

/* ======== PAGE TABS ======== */
.page-tabs {
    display: flex;
    gap: 6px;
    margin-bottom: 10px;
}

.page-tab {
    padding: 10px 24px;
    background: rgba(255,255,255,0.15);
    color: rgba(255,255,255,0.85);
    border: 2px solid transparent;
    border-radius: 10px 10px 0 0;
    cursor: pointer;
    font-size: 1em;
    font-weight: 600;
    transition: all 0.25s ease;
}

.page-tab:hover {
    background: rgba(255,255,255,0.25);
    color: #fff;
}

.page-tab.active {
    background: rgba(255,255,255,0.95);
    color: #2c5282;
    border-color: rgba(255,255,255,0.95);
    border-bottom-color: transparent;
}

.tab-content {
    display: none;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
}

.tab-content.active {
    display: flex;
}

/* ======== DETAIL TAB ======== */
.detail-record-count {
    margin-left: auto;
    font-weight: 600;
    color: #2c5282;
    font-size: 0.95em;
}

.detail-table-container {
    background: white;
    border-radius: 10px;
    padding: 0;
    box-shadow: 0 5px 15px rgba(0,0,0,0.15);
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
    min-height: 0;
}

.detail-table-wrapper {
    overflow: auto;
    flex: 1;
    min-height: 0;
}

/* Detail table styles */
.detail-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.82em;
}

.detail-table thead {
    position: sticky;
    top: 0;
    z-index: 20;
}

.detail-table thead tr.detail-header-row {
    background: linear-gradient(135deg, #2c5282 0%, #1e3a5f 100%);
}

.detail-table thead th {
    padding: 10px 12px;
    color: white;
    font-weight: 600;
    text-align: left;
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
    position: relative;
    border-right: 1px solid rgba(255,255,255,0.15);
}

.detail-table thead th:hover {
    background: rgba(255,255,255,0.1);
}

.detail-table thead th .sort-icon {
    margin-left: 6px;
    font-size: 0.75em;
    opacity: 0.5;
}

.detail-table thead th.sort-asc .sort-icon,
.detail-table thead th.sort-desc .sort-icon {
    opacity: 1;
}

/* Filter row */
.detail-table thead tr.detail-filter-row {
    background: #e8eef6;
}

.detail-table thead tr.detail-filter-row th {
    padding: 4px 6px;
    cursor: default;
}

.detail-filter-input {
    width: 100%;
    padding: 5px 8px;
    border: 1px solid #c5cfdb;
    border-radius: 4px;
    font-size: 0.95em;
    background: #fff;
    color: #333;
    box-sizing: border-box;
}

.detail-filter-input:focus {
    outline: none;
    border-color: #667eea;
    box-shadow: 0 0 0 2px rgba(102,126,234,0.2);
}

/* Totals row */
.detail-table thead tr.detail-totals-row {
    background: #dfe7f3;
}
.detail-table thead tr.detail-totals-row th {
    padding: 6px 8px;
    font-weight: 700;
    color: #2d3748;
    border-top: 1px solid #c5cfdb;
    text-align: right;
    cursor: default;
}
.detail-table thead tr.detail-totals-row th.totals-cell:empty {
    background: #dfe7f3;
}

.btn-clear-filters {
    display: block;
    margin-top: 4px;
    width: 100%;
    padding: 4px 6px;
    font-size: 0.85em;
    background: #e53e3e;
    color: #fff;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-weight: 600;
}
.btn-clear-filters:hover {
    background: #c53030;
}
.active-filter-bar .btn-clear-filters {
    display: inline-flex;
    align-items: center;
    width: auto;
    margin: 0;
    padding: 3px 8px;
    font-size: 0.8em;
    line-height: 1.2;
    border-radius: 4px;
}

/* Filter preset buttons */
.btn-preset {
    padding: 4px 8px;
    font-size: 0.85em;
    background: #4a5568;
    color: #fff;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-weight: 600;
    margin-left: 2px;
}
.btn-preset:hover {
    background: #2d3748;
}
#presetsGroup {
    display: flex;
    align-items: center;
    gap: 6px;
}

/* Auto-corrected date cell */
.detail-table td.date-corrected {
    background: #fff3cd;
    color: #8a6d3b;
    font-weight: 600;
    cursor: help;
    position: relative;
}
.detail-table td.date-corrected::after {
    content: '⚠';
    margin-left: 6px;
    color: #d97706;
}

/* Auto-corrected text cell */
.detail-table td.text-corrected {
    background: #fef3c7;
    color: #78350f;
    font-weight: 600;
    cursor: help;
}

/* Detail table body */
.detail-table td {
    padding: 6px 12px;
    border-bottom: 1px solid #e9ecef;
    white-space: nowrap;
}

.detail-table tbody tr:nth-child(even) {
    background: #fafbfc;
}

.detail-table tbody tr:nth-child(odd) {
    background: #fff;
}

/* Stratasys table: horizontally scroll when wider than the viewport, with narrower columns */
.stratasys-table {
    width: max-content;     /* table expands to fit widest content across all columns */
    min-width: 100%;        /* but still fills the wrapper if content is narrow */
    table-layout: fixed;    /* honor per-cell max-width so columns can be truncated */
}
.stratasys-table thead th,
.stratasys-table td {
    max-width: 7em;             /* roughly half the previous content-sized width */
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    padding-left: 6px;
    padding-right: 6px;
    cursor: move;
}
.stratasys-table td {
    cursor: default;
}
.stratasys-table thead th {
    cursor: grab;
    position: relative;
}
.stratasys-table .col-resize-handle {
    position: absolute;
    top: 0;
    right: 0;
    width: 6px;
    height: 100%;
    cursor: col-resize;
    user-select: none;
    z-index: 2;
}
.stratasys-table .col-resize-handle:hover {
    background: rgba(246, 173, 85, 0.5);
}
.stratasys-table thead th.dragging {
    opacity: 0.4;
}
.stratasys-table thead th.drag-over {
    box-shadow: inset 3px 0 0 0 #f6ad55;
}

/* Stratasys sub-tabs */
.stratasys-subtabs {
    margin-bottom: 10px;
}
.stratasys-subcontent {
    display: none;
    flex-direction: column;
    flex: 1;
    min-height: 0;
}
.stratasys-subcontent.active {
    display: flex;
}
.stratasys-table td[title]:hover {
    /* tooltip shows full value when truncated */
}

/* Stratasys filter bar spacing */
#stratasysFilterBar {
    margin-bottom: 8px;
    display: flex;
    gap: 12px;
    align-items: center;
}

.detail-table tbody tr:hover {
    background: #e8f2f7 !important;
}

.detail-table .amount-cell {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

/* Inline editable cells (admin) */
.editable-cell {
    cursor: pointer;
    position: relative;
}
.editable-cell:hover {
    background: #fffbe6 !important;
    outline: 1px solid #e2c75a;
}
.cell-edit-input {
    width: 100%;
    padding: 2px 4px;
    font-size: inherit;
    font-family: inherit;
    border: 2px solid #2c5282;
    border-radius: 3px;
    outline: none;
    background: #fff;
    box-sizing: border-box;
}
.cell-saving {
    color: #888 !important;
    font-style: italic;
}
.cell-saved {
    animation: cellSavedFlash 0.5s ease;
    background: #c6f6d5 !important;
}
.cell-error {
    background: #fed7d7 !important;
}
@keyframes cellSavedFlash {
    0% { background: #38a169; color: #fff; }
    100% { background: #c6f6d5; color: #333; }
}

/* Delete row button */
.delete-col-header {
    width: 36px;
    min-width: 36px;
}
.delete-row-cell {
    text-align: center;
    padding: 2px !important;
    width: 36px;
    min-width: 36px;
}
.delete-row-btn {
    background: none;
    border: 1px solid transparent;
    border-radius: 4px;
    cursor: pointer;
    font-size: 1em;
    padding: 2px 5px;
    opacity: 0.4;
    transition: opacity 0.2s, background 0.2s, border-color 0.2s;
}
.delete-row-btn:hover {
    opacity: 1;
    background: #fed7d7;
    border-color: #e53e3e;
}

/* Responsive for detail tab */
@media (max-width: 768px) {
    .page-tabs {
        gap: 4px;
    }
    .page-tab {
        padding: 8px 14px;
        font-size: 0.9em;
    }
    .detail-table {
        font-size: 0.72em;
    }
    .detail-table thead th,
    .detail-table td {
        padding: 5px 6px;
    }
    .detail-filter-input {
        padding: 4px 5px;
        font-size: 0.85em;
    }
}

/* ======== ADMIN TAB ======== */

/* User area in header */
.header-row {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 16px;
    position: relative;
}

.user-area {
    position: absolute;
    right: 0;
    display: flex;
    align-items: center;
    gap: 10px;
}

.user-name {
    color: rgba(255,255,255,0.85);
    font-size: 0.9em;
    font-weight: 500;
}

.btn-login, .btn-logout {
    background: rgba(255,255,255,0.12);
    color: rgba(255,255,255,0.85);
    font-size: 0.8em;
    padding: 6px 14px;
    border-radius: 6px;
    cursor: pointer;
    border: 1px solid rgba(255,255,255,0.2);
    transition: all 0.2s;
}

.btn-login:hover, .btn-logout:hover {
    background: rgba(255,255,255,0.22);
    color: #fff;
}

.admin-tab {
    background: rgba(246, 173, 85, 0.15);
    color: rgba(255,255,255,0.9);
}

.admin-tab.active {
    background: rgba(255,255,255,0.95);
    color: #c05621;
    border-color: rgba(255,255,255,0.95);
}

.admin-panels {
    display: flex;
    flex-direction: column;
    gap: 12px;
    flex: 1;
    overflow: auto;
    padding-bottom: 10px;
}

/* Make the active admin sub-tab grow and own its own scroll region */
.admin-subtab-content {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
    overflow: hidden;
}
.admin-subtab-content > .admin-panels {
    min-height: 0;
}

/* Admin sub-tabs */
.admin-subtabs {
    display: flex;
    gap: 8px;
    margin: 10px 0 14px;
    border-bottom: 2px solid #cbd5e0;
}
.admin-subtab-btn {
    background: #edf2f7;
    color: #2d3748;
    border: 1px solid #cbd5e0;
    border-bottom: none;
    padding: 8px 16px;
    font-weight: 600;
    cursor: pointer;
    border-radius: 8px 8px 0 0;
    font-size: 0.95em;
}
.admin-subtab-btn.active {
    background: #fff;
    color: #667eea;
    border-color: #cbd5e0;
    border-bottom: 2px solid #fff;
    margin-bottom: -2px;
}

/* Fix table */
.fix-table-group { margin-bottom: 20px; }
.fix-table-group h3 { margin: 0 0 6px; color: #2d3748; }
.fix-table-group .fix-count { color: #718096; font-weight: normal; font-size: 0.9em; }
.fix-table {
    width: 100%;
    border-collapse: collapse;
    background: #fff;
    font-size: 0.92em;
}
.fix-table th, .fix-table td {
    padding: 6px 10px;
    border-bottom: 1px solid #e2e8f0;
    text-align: left;
    vertical-align: top;
}
.fix-table th { background: #edf2f7; color: #2d3748; font-weight: 700; }
.fix-table td.cell-current { color: #c53030; font-family: monospace; }
.fix-table td.cell-suggested { color: #2f855a; font-family: monospace; font-weight: 600; }
.fix-table tr.fix-done { opacity: 0.55; background: #f0fff4; }
.btn-fix-one { padding: 4px 10px; font-size: 0.9em; }

.admin-panel {
    background: white;
    border-radius: 10px;
    padding: 18px;
    box-shadow: 0 5px 15px rgba(0,0,0,0.15);
}

.admin-panel h2 {
    color: #2c5282;
    font-size: 1.15em;
    margin-bottom: 12px;
    border-bottom: 2px solid #2c5282;
    padding-bottom: 8px;
}

.admin-query-actions {
    margin-bottom: 12px;
}

.admin-query-results {
    overflow: auto;
    max-height: 400px;
}

.admin-query-results table {
    font-size: 0.82em;
}

.admin-tables-list {
    max-height: 300px;
    overflow: auto;
}

.admin-tables-list .table-item {
    padding: 8px 12px;
    border-left-width: 3px;
    font-size: 0.9em;
}

/* ---- Admin User Controls ---- */
.admin-user-controls {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 12px;
    flex-wrap: wrap;
}

.admin-user-controls .filter-input {
    flex: 1;
    min-width: 180px;
    padding: 7px 12px;
    border: 2px solid #e9ecef;
    border-radius: 8px;
    font-size: 0.92em;
    transition: border-color 0.3s;
}

.admin-user-controls .filter-input:focus {
    outline: none;
    border-color: #667eea;
}

/* ---- User List Table ---- */
.admin-user-list {
    max-height: 400px;
    overflow: auto;
}

.user-list-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85em;
}

.user-list-table th {
    background: #f0f4ff;
    color: #2c5282;
    padding: 8px 10px;
    text-align: left;
    position: sticky;
    top: 0;
    z-index: 1;
    border-bottom: 2px solid #d0d5e0;
    white-space: nowrap;
}

.user-list-table td {
    padding: 7px 10px;
    border-bottom: 1px solid #edf2f7;
    vertical-align: middle;
}

.user-list-table tbody tr:hover {
    background: #f7fafc;
}

/* Badges */
.badge {
    display: inline-block;
    padding: 2px 8px;
    border-radius: 10px;
    font-size: 0.82em;
    font-weight: 600;
}
.badge-green { background: #c6f6d5; color: #22543d; }
.badge-red   { background: #fed7d7; color: #9b2c2c; }
.badge-blue  { background: #bee3f8; color: #2a4365; }
.badge-gray  { background: #e2e8f0; color: #718096; }

.btn-sm {
    padding: 4px 10px;
    font-size: 0.8em;
    border-radius: 5px;
}

.btn-danger {
    background: #e53e3e;
    color: white;
    border-color: #c53030;
}
.btn-danger:hover {
    background: #c53030;
}

/* ---- User Detail Form ---- */
.user-detail-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 10px 16px;
}

.ud-field {
    display: flex;
    flex-direction: column;
    gap: 3px;
}

.ud-field label {
    font-size: 0.8em;
    font-weight: 600;
    color: #4a5568;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.ud-field input,
.ud-field select {
    padding: 7px 10px;
    border: 2px solid #e9ecef;
    border-radius: 6px;
    font-size: 0.92em;
    transition: border-color 0.3s;
}

.ud-field input:focus,
.ud-field select:focus {
    outline: none;
    border-color: #667eea;
}

/* ---- Login History Table ---- */
.admin-login-history {
    max-height: 350px;
    overflow: auto;
}

.login-history-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85em;
}

.login-history-table th {
    background: #f0f4ff;
    color: #2c5282;
    padding: 8px 10px;
    text-align: left;
    position: sticky;
    top: 0;
    z-index: 1;
    border-bottom: 2px solid #d0d5e0;
}

.login-history-table td {
    padding: 6px 10px;
    border-bottom: 1px solid #edf2f7;
}

.login-history-table tbody tr:hover {
    background: #f7fafc;
}

/* ---- Sortable Admin Table Headers ---- */
.sortable-admin th {
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
}

.sortable-admin th:hover {
    background: #dce4f0;
}

.sortable-admin th .sort-icon {
    margin-left: 5px;
    font-size: 0.72em;
    opacity: 0.4;
}

.sortable-admin th.sort-asc .sort-icon,
.sortable-admin th.sort-desc .sort-icon {
    opacity: 1;
}

/* ---- Pivot Toggle Button ---- */
.btn-pivot {
    background: rgba(102, 126, 234, 0.12);
    color: #667eea;
    border: 2px solid #667eea;
    font-weight: 600;
    padding: 6px 16px;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-pivot:hover {
    background: #667eea;
    color: white;
}

.btn-pivot.active {
    background: #667eea;
    color: white;
}

/* ---- Pivot Table ---- */
.pivot-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.92em;
}

.pivot-table thead th {
    background: linear-gradient(135deg, #2c5282 0%, #1e3a5f 100%);
    color: white;
    padding: 10px 14px;
    font-weight: 600;
    text-align: left;
    cursor: pointer;
    user-select: none;
    white-space: nowrap;
    border-right: 1px solid rgba(255,255,255,0.15);
}

.pivot-table thead th:hover {
    background: rgba(255,255,255,0.1);
}

.pivot-table thead th .sort-icon {
    margin-left: 6px;
    font-size: 0.75em;
    opacity: 0.5;
}

.pivot-table thead th.sort-asc .sort-icon,
.pivot-table thead th.sort-desc .sort-icon {
    opacity: 1;
}

.pivot-table td {
    padding: 8px 14px;
    border-bottom: 1px solid #e2e8f0;
}

.pivot-table tbody tr:hover {
    background: #f0f4ff;
}

.pivot-key {
    font-weight: 500;
}

.pivot-num {
    text-align: right;
    font-variant-numeric: tabular-nums;
}

.pivot-grand {
    background: #edf2f7;
    border-top: 2px solid #2c5282;
}

.pivot-grand td {
    border-bottom: none;
}

/* ---- Charts Grid ---- */
.charts-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(440px, 1fr));
    gap: 16px;
    padding: 4px 0;
}

.chart-card {
    background: white;
    border-radius: 10px;
    box-shadow: 0 4px 14px rgba(0,0,0,0.1);
    padding: 16px;
    min-height: 320px;
    display: flex;
    flex-direction: column;
}

.chart-card-header {
    font-weight: 600;
    font-size: 1.05em;
    color: #2c5282;
    margin-bottom: 10px;
    padding-bottom: 6px;
    border-bottom: 2px solid #e2e8f0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

.btn-chart-copy {
    background: #edf2f7;
    color: #2c5282;
    border: 1px solid #cbd5e0;
    border-radius: 6px;
    padding: 4px 10px;
    font-size: 0.85em;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}
.btn-chart-copy:hover {
    background: #2c5282;
    color: white;
    border-color: #2c5282;
}

.chart-card canvas {
    flex: 1;
    min-height: 260px;
}

@media (max-width: 768px) {
    .charts-grid {
        grid-template-columns: 1fr;
    }
}

/* ======== MODAL ======== */
.modal-overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.55);
    z-index: 1000;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-box {
    background: white;
    border-radius: 12px;
    padding: 28px 32px;
    width: 380px;
    max-width: 90vw;
    box-shadow: 0 15px 40px rgba(0,0,0,0.3);
}

.modal-box h2 {
    color: #2c5282;
    margin-bottom: 8px;
    font-size: 1.3em;
}

.modal-box p {
    color: #555;
    margin-bottom: 14px;
    font-size: 0.95em;
}

.modal-input {
    width: 100%;
    padding: 10px 14px;
    border: 2px solid #e9ecef;
    border-radius: 8px;
    font-size: 1em;
    margin-bottom: 10px;
    transition: border-color 0.3s;
}

.modal-input:focus {
    outline: none;
    border-color: #667eea;
}

.modal-actions {
    display: flex;
    gap: 10px;
    justify-content: flex-end;
    margin-top: 6px;
}

/* ======== BETA BADGE (used on tab buttons) ======== */
.beta-badge {
    display: inline-block;
    margin-left: 6px;
    padding: 1px 6px;
    font-size: 0.65em;
    font-weight: 700;
    letter-spacing: 0.05em;
    color: #fff;
    background: linear-gradient(135deg, #ed8936 0%, #dd6b20 100%);
    border-radius: 4px;
    vertical-align: middle;
    line-height: 1.4;
    text-transform: uppercase;
}

/* ======== STRATASYS CORRECTION BUTTON / MODAL ======== */
.stratasys-table th.stratasys-action-col,
.stratasys-table td.stratasys-action-col {
    width: 110px;
    text-align: center;
    cursor: default;
}
.stratasys-table th.stratasys-action-col {
    background: transparent;
}
.btn-correction {
    background: #edf2f7;
    color: #2c5282;
    border: 1px solid #cbd5e0;
    border-radius: 6px;
    padding: 4px 8px;
    font-size: 0.85em;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s, color 0.15s;
}
.btn-correction:hover {
    background: #2c5282;
    color: #fff;
    border-color: #2c5282;
}

.modal-box.pcm-box {
    width: 760px;
    max-width: 95vw;
    max-height: 90vh;
    display: flex;
    flex-direction: column;
}
.pcm-table-wrap {
    overflow-y: auto;
    max-height: 55vh;
    border: 1px solid #e2e8f0;
    border-radius: 8px;
}
.pcm-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.92em;
}
.pcm-table thead th {
    position: sticky;
    top: 0;
    background: #f7fafc;
    color: #2c5282;
    text-align: left;
    padding: 8px 10px;
    border-bottom: 2px solid #cbd5e0;
    z-index: 1;
}
.pcm-table tbody td {
    padding: 6px 10px;
    border-bottom: 1px solid #edf2f7;
    vertical-align: middle;
}
.pcm-table .pcm-label {
    font-weight: 600;
    color: #2d3748;
    width: 28%;
}
.pcm-table .pcm-old {
    color: #4a5568;
    width: 32%;
    word-break: break-word;
}
.pcm-table .pcm-new {
    width: 40%;
}
.pcm-table .pcm-input {
    margin-bottom: 0;
    padding: 6px 10px;
    font-size: 0.95em;
}

body.dark-mode .btn-correction {
    background: #2d3748;
    color: #cbd5e0;
    border-color: #4a5568;
}
body.dark-mode .btn-correction:hover {
    background: #4299e1;
    color: #fff;
    border-color: #4299e1;
}
body.dark-mode .pcm-table-wrap {
    border-color: #4a5568;
}
body.dark-mode .pcm-table thead th {
    background: #1a202c;
    color: #90cdf4;
    border-bottom-color: #4a5568;
}
body.dark-mode .pcm-table tbody td {
    border-bottom-color: #2d3748;
}
body.dark-mode .pcm-table .pcm-label {
    color: #e2e8f0;
}
body.dark-mode .pcm-table .pcm-old {
    color: #a0aec0;
}

/* ===================================================== */
/* DARK MODE                                             */
/* ===================================================== */
body.dark-mode {
    background: linear-gradient(135deg, #0f1419 0%, #1a1f2e 100%);
    color: #e2e8f0;
}
body.dark-mode header {
    background: rgba(0, 0, 0, 0.35);
}
body.dark-mode .filters-bar,
body.dark-mode .data-table-container,
body.dark-mode .chart-fullscreen,
body.dark-mode .detail-table-container,
body.dark-mode .card,
body.dark-mode .admin-panel,
body.dark-mode .admin-table,
body.dark-mode .users-management,
body.dark-mode .user-detail-modal-content,
body.dark-mode .modal-content {
    background: #1f2937 !important;
    color: #e2e8f0;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}
body.dark-mode h1,
body.dark-mode h2,
body.dark-mode h3,
body.dark-mode h4,
body.dark-mode label,
body.dark-mode .ud-field label {
    color: #e2e8f0 !important;
}
body.dark-mode .filter-select,
body.dark-mode .filter-input,
body.dark-mode input[type="text"],
body.dark-mode input[type="email"],
body.dark-mode input[type="password"],
body.dark-mode input[type="number"],
body.dark-mode select,
body.dark-mode textarea {
    background: #111827 !important;
    color: #e2e8f0 !important;
    border-color: #374151 !important;
}
body.dark-mode .monthly-table,
body.dark-mode .detail-table,
body.dark-mode .pivot-table,
body.dark-mode .fix-table,
body.dark-mode .admin-table table {
    background: #1f2937;
    color: #e2e8f0;
}
body.dark-mode .monthly-table th,
body.dark-mode .detail-table th,
body.dark-mode .pivot-table th,
body.dark-mode .fix-table th {
    background: #111827 !important;
    color: #e2e8f0 !important;
    border-color: #374151 !important;
}
body.dark-mode .monthly-table td,
body.dark-mode .detail-table td,
body.dark-mode .pivot-table td,
body.dark-mode .fix-table td {
    border-color: #2d3748 !important;
    color: #e2e8f0;
}
body.dark-mode .monthly-table tbody tr:nth-child(even),
body.dark-mode .detail-table tbody tr:nth-child(even) {
    background: #1a2230;
}
body.dark-mode .monthly-table tbody tr:nth-child(odd),
body.dark-mode .detail-table tbody tr:nth-child(odd) {
    background: #1f2937;
}
body.dark-mode .monthly-table tr:hover,
body.dark-mode .detail-table tr:hover {
    background: #2d3748 !important;
}
body.dark-mode .monthly-table .total-cell,
body.dark-mode .detail-table tfoot td {
    background: #0f1419 !important;
    color: #fbd38d;
}
body.dark-mode .page-tab {
    background: rgba(255, 255, 255, 0.08);
    color: #cbd5e0;
}
body.dark-mode .page-tab.active {
    background: rgba(255, 255, 255, 0.18);
    color: #fff;
}
body.dark-mode .info-text { color: #a0aec0; }
body.dark-mode .ms-badge { color: #fff; }
body.dark-mode .yoy-up { background: rgba(56, 161, 105, 0.25); color: #9ae6b4; }
body.dark-mode .yoy-down { background: rgba(229, 62, 62, 0.25); color: #feb2b2; }
body.dark-mode .date-corrected { background: #5a4a1a !important; color: #fef3c7 !important; }
body.dark-mode .text-corrected { background: #5a4a1a !important; color: #fef3c7 !important; }

.btn-theme {
    background: transparent;
    border: 1px solid rgba(255,255,255,0.4);
    color: #fff;
    padding: 4px 10px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 1em;
}
.btn-theme:hover { background: rgba(255,255,255,0.15); }

/* ===================================================== */
/* MOBILE LAYOUT IMPROVEMENTS                            */
/* ===================================================== */
@media (max-width: 768px) {
    body {
        height: auto;
        min-height: 100vh;
        overflow: auto;
        padding: 6px;
    }
    .container { height: auto; }
    header { padding: 8px; margin-bottom: 8px; }
    header h1 { font-size: 1.1em; }
    .header-row {
        flex-direction: column;
        gap: 6px;
        align-items: stretch;
    }
    .user-area {
        justify-content: center;
        flex-wrap: wrap;
        gap: 6px;
    }
    .page-tabs {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        white-space: nowrap;
        display: flex;
    }
    .page-tab {
        font-size: 0.85em;
        padding: 6px 10px;
        flex-shrink: 0;
    }
    .filters-bar {
        flex-direction: column;
        align-items: stretch;
        gap: 8px;
        padding: 8px;
    }
    .filter-group {
        flex-wrap: wrap;
        gap: 6px;
    }
    .filter-group label { min-width: 70px; }
    .filter-select, .filter-input {
        flex: 1;
        min-width: 0;
    }
    .dashboard {
        height: auto;
        gap: 8px;
    }
    .table-wrapper, .detail-table-wrapper {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }
    .monthly-table, .detail-table, .pivot-table {
        font-size: 0.78em;
    }
    .monthly-table th, .monthly-table td,
    .detail-table th, .detail-table td,
    .pivot-table th, .pivot-table td {
        padding: 4px 6px;
    }
    .chart-fullscreen {
        height: 60vh;
        min-height: 320px;
    }
    canvas#monthlyTrendChart,
    canvas#historicalChart {
        max-width: 100%;
    }
    .btn, .btn-pivot, .btn-preset { font-size: 0.85em; padding: 5px 8px; }
    .admin-subtabs { flex-wrap: wrap; }
    /* Make sticky filters less awkward on phones */
    #presetsGroup { flex-wrap: wrap; }
}
@media (max-width: 480px) {
    header h1 { font-size: 1em; }
    .filter-group label { font-size: 0.85em; }
    .btn { font-size: 0.8em; }
}
