* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; background: #f4f6f8; color: #1a202c; }

.topbar {
    display: flex; align-items: center; gap: 24px;
    background: #1f2937; color: #f9fafb;
    padding: 0 20px; height: 52px;
    box-shadow: 0 1px 4px rgba(0,0,0,0.1);
}
.topbar .brand { font-weight: 600; font-size: 16px; letter-spacing: 0.3px; }
.topbar nav { display: flex; gap: 4px; flex: 1; }
.topbar nav a {
    color: #d1d5db; text-decoration: none; padding: 8px 14px;
    border-radius: 6px; font-size: 14px;
}
.topbar nav a:hover { background: rgba(255,255,255,0.06); }
.topbar nav a.active { background: rgba(59,130,246,0.25); color: #fff; }
.topbar .meta { font-size: 12px; color: #9ca3af; }
.badge {
    padding: 4px 10px; border-radius: 999px;
    background: #374151; color: #d1d5db; font-size: 12px;
}
.badge.ok { background: #10b981; color: #fff; }
.badge.warn { background: #f59e0b; color: #fff; }
.badge.err { background: #ef4444; color: #fff; }

main { padding: 20px; max-width: 1600px; margin: 0 auto; }

.kpi-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 12px;
    margin-bottom: 20px;
}
.kpi {
    background: #fff;
    border-left: 4px solid #6b7280;
    padding: 14px 16px;
    border-radius: 6px;
    box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}
.kpi.info { border-color: #3b82f6; }
.kpi.ok { border-color: #10b981; }
.kpi.warn { border-color: #f59e0b; }
.kpi.err { border-color: #ef4444; }
.kpi-clickable { cursor: pointer; }
.kpi-clickable:hover { background: #f8fafc; transform: translateY(-1px); }
.kpi-title { font-size: 12px; color: #6b7280; text-transform: uppercase; letter-spacing: 0.3px; }
.kpi-value { font-size: 26px; font-weight: 600; margin-top: 4px; color: #111827; }
.kpi-sub { font-size: 11px; color: #6b7280; margin-top: 2px; }

.card {
    background: #fff; border-radius: 8px;
    box-shadow: 0 1px 2px rgba(0,0,0,0.04);
    padding: 16px; margin-bottom: 16px;
}
.card-h { font-size: 14px; font-weight: 600; color: #374151; margin-bottom: 12px; padding-bottom: 8px; border-bottom: 1px solid #e5e7eb; }

.row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 1024px) { .row { grid-template-columns: 1fr; } }
.half { margin-bottom: 0; }

.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th, .data-table td {
    text-align: left; padding: 8px 10px; border-bottom: 1px solid #f1f5f9;
}
.data-table th { background: #f8fafc; color: #6b7280; font-weight: 500; font-size: 12px; text-transform: uppercase; letter-spacing: 0.2px; }
.data-table tbody tr:hover { background: #f9fafb; }
.data-table td.num { text-align: right; font-variant-numeric: tabular-nums; }
.data-table td.nowrap { white-space: nowrap; }
.data-table td.text-cell { max-width: 400px; overflow: hidden; text-overflow: ellipsis; }
.data-table td.cell-warn { color: #b91c1c; font-weight: 600; }
.data-table td .sub { font-size: 10px; color: #9ca3af; font-weight: 400; margin-top: 2px; letter-spacing: 0.1px; }
.data-table tr.row-unsorted { background: #fffbeb; }
.data-table tr.row-unsorted td { border-bottom: 2px solid #fbbf24; }
.customer-cell { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.customer-badge {
    display: inline-flex; align-items: center; min-height: 18px;
    padding: 1px 6px; border-radius: 5px; font-size: 11px; font-weight: 600;
}
.customer-new { background: #dbeafe; color: #1e40af; }
.customer-regular { background: #fef3c7; color: #92400e; }
.customer-repeat { background: #e5e7eb; color: #374151; }
.customer-unknown { background: #f3f4f6; color: #9ca3af; }

/* Ticket card */
.ticket-header { background: linear-gradient(180deg, #ffffff 0%, #f9fafb 100%); }
.ticket-h-row { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; }
.back-link { font-size: 13px; color: #6b7280; text-decoration: none; }
.back-link:hover { color: #1f2937; }
.ticket-no { font-size: 22px; margin: 4px 0 8px 0; color: #111827; font-weight: 600; }
.ticket-no #t-no { color: #1d4ed8; }
.ticket-summary { font-size: 14px; color: #4b5563; }
.ticket-status { display: flex; gap: 6px; flex-wrap: wrap; }
.status-badge {
    padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 500;
    white-space: nowrap;
}
.tone-info    { background: #dbeafe; color: #1e40af; }
.tone-ok      { background: #d1fae5; color: #065f46; }
.tone-warn    { background: #fef3c7; color: #92400e; }
.tone-err     { background: #fee2e2; color: #991b1b; }
.tone-neutral { background: #e5e7eb; color: #374151; }

.kv-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.kv-table th {
    text-align: left; padding: 6px 10px; color: #6b7280; font-weight: 500;
    width: 200px; vertical-align: top;
    border-bottom: 1px solid #f1f5f9;
}
.kv-table td { padding: 6px 10px; color: #1f2937; border-bottom: 1px solid #f1f5f9; }

.ticket-text { white-space: pre-wrap; font-size: 14px; line-height: 1.5; color: #1f2937; }
.muted { color: #9ca3af; font-size: 13px; }

.usage-footer {
    background: transparent;
    border: 1px solid #e5e7eb;
    box-shadow: none;
    padding: 12px 14px;
}
.usage-footer .card-h {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 8px;
    padding-bottom: 6px;
    border-bottom-color: #f1f5f9;
    color: #6b7280;
    font-size: 13px;
}
.usage-footer .card-h::after {
    content: "служебная метрика";
    color: #9ca3af;
    font-size: 11px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}
.usage-summary {
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.usage-metrics {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 8px;
}
.usage-metric {
    min-height: 44px;
    padding: 8px 10px;
    border-radius: 6px;
    background: #f8fafc;
    border: 1px solid #edf2f7;
}
.usage-metric-value {
    color: #374151;
    font-size: 17px;
    font-weight: 600;
    line-height: 1;
    font-variant-numeric: tabular-nums;
}
.usage-metric-label {
    margin-top: 4px;
    color: #9ca3af;
    font-size: 11px;
}
.usage-pages {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-wrap: wrap;
    color: #9ca3af;
    font-size: 12px;
}
.usage-page-chip {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    max-width: 220px;
    padding: 3px 8px;
    border-radius: 999px;
    background: #f3f4f6;
    color: #4b5563;
    font-size: 12px;
}
.usage-page-chip code {
    max-width: 150px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    background: transparent;
    padding: 0;
}

.timeline { display: flex; flex-direction: column; gap: 14px; }
.tl-item { display: flex; gap: 12px; }
.tl-marker {
    width: 28px; height: 28px; border-radius: 50%;
    background: #3b82f6; color: #fff;
    display: flex; align-items: center; justify-content: center;
    font-weight: 600; font-size: 13px; flex-shrink: 0;
}
.tl-body { flex: 1; padding-bottom: 8px; border-bottom: 1px solid #f3f4f6; }
.tl-h { font-size: 14px; margin-bottom: 4px; }
.tl-cost { margin-left: 8px; color: #059669; font-weight: 500; }
.tl-meta { font-size: 13px; color: #374151; line-height: 1.7; }
.tl-meta b { color: #6b7280; font-weight: 500; }

code { background: #f3f4f6; padding: 1px 6px; border-radius: 3px; font-size: 12px; }

.btn-sm {
    padding: 4px 12px; font-size: 12px; cursor: pointer;
    background: #3b82f6; color: #fff; border: none; border-radius: 4px;
    float: right;
}
.btn-sm:hover { background: #2563eb; }
.btn-sm:disabled { background: #9ca3af; cursor: wait; }
.ai-feedback-btn { float: none; margin-top: 6px; }

.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 1000;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background: rgba(17, 24, 39, 0.45);
}
.modal-card {
    width: min(720px, 100%);
    max-height: calc(100vh - 40px);
    overflow-y: auto;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 20px 45px rgba(15, 23, 42, 0.25);
    padding: 16px;
}
.modal-head {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 14px;
    padding-bottom: 10px;
    border-bottom: 1px solid #e5e7eb;
}
.modal-title { font-size: 16px; font-weight: 600; color: #111827; }
.modal-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 12px;
}
.fb-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 10px;
    margin-bottom: 10px;
}
.fb-grid label,
.fb-comment-label {
    display: flex;
    flex-direction: column;
    gap: 4px;
    color: #4b5563;
    font-size: 13px;
}
.fb-grid input,
.fb-grid select,
.fb-comment-label textarea {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid #d1d5db;
    border-radius: 6px;
    font-size: 13px;
    font-family: inherit;
}
.fb-fieldset {
    display: flex;
    flex-wrap: wrap;
    gap: 8px 14px;
    margin: 10px 0;
    padding: 10px;
    border: 1px solid #e5e7eb;
    border-radius: 6px;
    background: #f8fafc;
    font-size: 13px;
}
.fb-fieldset label {
    display: inline-flex;
    align-items: center;
    gap: 5px;
}
.fb-label {
    width: 100%;
    color: #6b7280;
    font-weight: 600;
    font-size: 12px;
}

.manager-groups { display: flex; flex-direction: column; gap: 14px; }
.manager-block {
    border: 1px solid #e5e7eb;
    border-radius: 6px;
    overflow: hidden;
    background: #fff;
}
.manager-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 10px 12px;
    background: #f8fafc;
    border-bottom: 1px solid #e5e7eb;
}
.manager-head .btn-sm { float: none; }
.manager-title-row {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}
.recheck-progress {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 12px;
    color: #475569;
    white-space: nowrap;
}
.progress-ring {
    --progress: 0%;
    width: 18px;
    height: 18px;
    border-radius: 999px;
    background: conic-gradient(#3b82f6 var(--progress), #dbeafe 0);
    position: relative;
    flex: 0 0 auto;
}
.progress-ring::after {
    content: "";
    position: absolute;
    inset: 4px;
    border-radius: inherit;
    background: #f8fafc;
}
.progress-ring.running {
    animation: progress-pulse 1.2s ease-in-out infinite;
}
@keyframes progress-pulse {
    0%, 100% { opacity: .72; }
    50% { opacity: 1; }
}
.recheck-mark {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 22px;
    height: 22px;
    border-radius: 999px;
    font-weight: 700;
    font-size: 14px;
}
.recheck-mark.fixed { background: #d1fae5; color: #047857; }
.recheck-mark.bad { background: #fee2e2; color: #b91c1c; }
.recheck-mark.error { background: #fef3c7; color: #92400e; }
.recheck-mark.pending { background: #f3f4f6; color: #9ca3af; }
.recheck-mark.ignored { background: #e5e7eb; color: #4b5563; }
.order-actions {
    display: flex;
    gap: 4px;
    flex-wrap: wrap;
    margin-top: 6px;
}
.order-override-input {
    width: 110px;
    padding: 4px 6px;
    border: 1px solid #d1d5db;
    border-radius: 4px;
    font-size: 12px;
}
.mini-btn {
    padding: 4px 8px;
    border: none;
    border-radius: 4px;
    background: #3b82f6;
    color: #fff;
    cursor: pointer;
    font-size: 12px;
}
.mini-btn:hover { background: #2563eb; }
.mini-btn:disabled { background: #9ca3af; cursor: wait; }
.mini-btn.muted-btn { background: #6b7280; }
.mini-btn.muted-btn:hover { background: #4b5563; }

tr.row-clickable { cursor: pointer; }
tr.row-clickable:hover { background: #eef2ff !important; }

.chart-wrap { position: relative; height: 280px; }
.row .chart-wrap { height: 320px; }
.data-table a { color: #1d4ed8; text-decoration: none; }
.data-table a:hover { text-decoration: underline; }

.row-overdue { background: #fef2f2 !important; }
.row-overdue td { color: #991b1b; }

.age-grid { display: grid; gap: 6px; }
.age-row {
    display: grid;
    grid-template-columns: 110px 1fr 80px 56px;
    gap: 12px; align-items: center;
    padding: 6px 10px;
    border-radius: 4px;
    font-size: 13px;
}
.age-label { font-weight: 500; color: #374151; white-space: nowrap; }
.age-bar { background: #f1f5f9; height: 16px; border-radius: 8px; overflow: hidden; }
.age-bar-fill { height: 100%; transition: width 0.4s; border-radius: 8px; }
.age-count { text-align: right; font-variant-numeric: tabular-nums; color: #1f2937; font-weight: 500; }
.age-share { text-align: right; color: #6b7280; font-size: 12px; font-variant-numeric: tabular-nums; }
.age-ok    { background: rgba(16,185,129,0.06); } .age-ok    .age-bar-fill { background: #10b981; }
.age-warn  { background: rgba(245,158,11,0.06); } .age-warn  .age-bar-fill { background: #f59e0b; }
.age-orange{ background: rgba(249,115,22,0.07); } .age-orange .age-bar-fill { background: #f97316; }
.age-err   { background: rgba(239,68,68,0.07); }  .age-err   .age-bar-fill { background: #ef4444; }
.age-clickable { cursor: pointer; }
.age-clickable:hover { background: #eef2ff; }

.hint { font-size: 12px; color: #6b7280; margin: -6px 0 14px; line-height: 1.5; }
.hint b { color: #b91c1c; font-weight: 600; }
.legend-table { width: 100%; border-collapse: collapse; font-size: 12px; margin: 4px 0 8px; }
.legend-table th, .legend-table td { padding: 4px 8px; border-bottom: 1px solid #e5e7eb; text-align: left; vertical-align: top; }
.legend-table th { font-weight: 600; color: #374151; background: #f9fafb; width: 22%; white-space: nowrap; }
.legend-table td { color: #4b5563; }
.legend-table code { background: #f3f4f6; padding: 1px 4px; border-radius: 3px; font-size: 11px; }
th[title] { cursor: help; border-bottom: 1px dotted #9ca3af; }
.data-table th.sortable { cursor: pointer; user-select: none; white-space: nowrap; }
.data-table th.sortable:hover { color: #1f2937; }
.data-table th.sorted { color: #1d4ed8; }
.data-table th .sort-ind { font-size: 10px; margin-left: 4px; opacity: 0.5; }
.data-table th.sorted .sort-ind { opacity: 1; }
.sla-overdue { color: #b91c1c; font-weight: 600; }
.sla-days { color: #b91c1c; font-size: 11px; }

.filters {
    display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 16px;
}
.filters input, .filters select, .filters button {
    padding: 8px 12px; font-size: 13px; border: 1px solid #d1d5db;
    border-radius: 6px; background: #fff;
}
.filters input { flex: 2 1 240px; }
.filters select { flex: 1 1 160px; }
.filters .filter-date-label {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: #6b7280;
    font-size: 13px;
}
.filters .filter-date-label input {
    flex: 0 0 150px;
}
.filters button { cursor: pointer; }
.filters button.active { background: #ef4444; color: #fff; border-color: #ef4444; }
.filters button:hover { background: #f3f4f6; }
.filters button.active:hover { background: #dc2626; }

.recheck-delta {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 8px;
    margin: 0 0 14px;
}
.delta-title {
    grid-column: 1 / -1;
    color: #4b5563;
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}
.delta-chip {
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-height: 46px;
    padding: 8px 10px;
    border-radius: 6px;
    border-left: 4px solid #9ca3af;
    background: #f8fafc;
    font-size: 12px;
}
.delta-chip.ok { border-left-color: #10b981; }
.delta-chip.bad { border-left-color: #ef4444; }
.delta-chip.warn { border-left-color: #f59e0b; }
.delta-chip a { color: #1d4ed8; text-decoration: none; }
.delta-chip a:hover { text-decoration: underline; }

.ai-check-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    gap: 16px;
    margin-bottom: 16px;
}
.ai-check-head h1 {
    margin: 0 0 4px;
    font-size: 22px;
    line-height: 1.2;
    color: #111827;
}
.ai-check-table td {
    vertical-align: top;
}
.ai-check-table .ai-explanation {
    max-width: 520px;
    line-height: 1.45;
}
.ai-check-table .ai-sources {
    min-width: 150px;
}
.ai-check-table .ai-sources .status-badge {
    display: inline-block;
    margin: 0 4px 4px 0;
}

@media (max-width: 900px) {
    .ai-check-head {
        align-items: flex-start;
        flex-direction: column;
    }
}

.theme-toggle {
    padding: 4px 9px;
    border: 1px solid rgba(255,255,255,0.18);
    border-radius: 5px;
    background: rgba(255,255,255,0.06);
    color: #e5e7eb;
    font-size: 12px;
    cursor: pointer;
}
.theme-toggle:hover { background: rgba(255,255,255,0.12); }

body.theme-dark {
    background: #111318;
    color: #e5e7eb;
}
body.theme-dark .topbar {
    background: #0b0d12;
    border-bottom: 1px solid #262a33;
    box-shadow: none;
}
body.theme-dark .card,
body.theme-dark .kpi,
body.theme-dark .manager-block {
    background: #1a1d24;
    border-color: #2b303a;
    box-shadow: none;
}
body.theme-dark .ticket-header {
    background: #1a1d24;
}
body.theme-dark .card-h,
body.theme-dark .ticket-no,
body.theme-dark .kpi-value,
body.theme-dark .kv-table td,
body.theme-dark .ticket-text,
body.theme-dark .age-count,
body.theme-dark .tl-meta {
    color: #f3f4f6;
}
body.theme-dark .card-h,
body.theme-dark .kv-table th,
body.theme-dark .kv-table td,
body.theme-dark .tl-body,
body.theme-dark .legend-table th,
body.theme-dark .legend-table td,
body.theme-dark .data-table th,
body.theme-dark .data-table td {
    border-color: #2b303a;
}
body.theme-dark .kpi-title,
body.theme-dark .kpi-sub,
body.theme-dark .muted,
body.theme-dark .hint,
body.theme-dark .ticket-summary,
body.theme-dark .kv-table th,
body.theme-dark .tl-meta b,
body.theme-dark .age-share,
body.theme-dark .delta-title {
    color: #a8b0bd;
}
body.theme-dark .data-table th,
body.theme-dark .manager-head,
body.theme-dark .legend-table th,
body.theme-dark .usage-metric,
body.theme-dark .delta-chip {
    background: #222733;
    color: #cbd5e1;
}
body.theme-dark .data-table tbody tr:hover,
body.theme-dark tr.row-clickable:hover,
body.theme-dark .kpi-clickable:hover {
    background: #252b36 !important;
}
body.theme-dark .data-table tr.row-unsorted {
    background: rgba(245,158,11,0.12);
}
body.theme-dark .row-overdue {
    background: rgba(239,68,68,0.12) !important;
}
body.theme-dark .row-overdue td,
body.theme-dark .data-table td.cell-warn,
body.theme-dark .sla-overdue,
body.theme-dark .sla-days {
    color: #fca5a5;
}
body.theme-dark .filters input,
body.theme-dark .filters select,
body.theme-dark .filters button,
body.theme-dark .order-override-input {
    background: #151922;
    border-color: #343b49;
    color: #e5e7eb;
}
body.theme-dark .filters button:hover {
    background: #252b36;
}
body.theme-dark code,
body.theme-dark .usage-page-chip,
body.theme-dark .customer-unknown,
body.theme-dark .tone-neutral {
    background: #2a303b;
    color: #cbd5e1;
}
body.theme-dark .data-table a,
body.theme-dark .delta-chip a,
body.theme-dark .back-link,
body.theme-dark a {
    color: #93c5fd;
}
body.theme-dark .age-bar,
body.theme-dark .sla-progress-bar {
    background: #2a303b;
}
body.theme-dark .ltv-item {
    background: #171b23;
    border-color: #2b303a;
}
body.theme-dark .ltv-row,
body.theme-dark .ltv-row span:last-child,
body.theme-dark .usage-metric-value {
    color: #e5e7eb;
}
