:root {
    --primary: #c41e3a;
    --primary-dark: #9a1830;
    --secondary: #4a4a4a;
    --bg: #f5f5f5;
    --card-bg: #ffffff;
    --border: #ddd;
    --text: #222;
    --text-muted: #666;
    --success: #28a745;
    --error: #dc3545;
}

* {
    box-sizing: border-box;
}

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

.hidden {
    display: none !important;
}

/* ── Авторизация ───────────────────────────────────────────── */

.login-screen {
    min-height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, #1a1a1a 0%, #2d2d2d 100%);
}

.login-box {
    background: var(--card-bg);
    padding: 40px;
    border-radius: 12px;
    box-shadow: 0 20px 60px rgba(0,0,0,0.3);
    width: 100%;
    max-width: 400px;
    text-align: center;
}

.login-logo {
    width: 100px;
    height: 100px;
    object-fit: contain;
    margin-bottom: 16px;
}

.login-box h1 {
    margin: 0 0 8px;
    font-size: 24px;
    color: var(--primary);
}

.login-subtitle {
    color: var(--text-muted);
    margin-bottom: 24px;
}

.login-box input {
    width: 100%;
    padding: 12px 16px;
    margin-bottom: 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 16px;
}

.login-box input:focus {
    outline: none;
    border-color: var(--primary);
}

.error-text {
    color: var(--error);
    font-size: 14px;
    margin-top: 8px;
    min-height: 20px;
}

/* ── Общие элементы ────────────────────────────────────────── */

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 10px 20px;
    border: none;
    border-radius: 8px;
    font-size: 15px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.2s, transform 0.1s;
    text-decoration: none;
}

.btn:hover {
    opacity: 0.9;
}

.btn:active {
    transform: translateY(1px);
}

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

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

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

.btn-block {
    width: 100%;
}

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

.container {
    max-width: 1100px;
    margin: 0 auto;
    padding: 20px;
}

/* ── Шапка ─────────────────────────────────────────────────── */

.header {
    background: var(--card-bg);
    border-bottom: 1px solid var(--border);
    padding: 16px 24px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: sticky;
    top: 0;
    z-index: 100;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 16px;
}

.header-logo {
    width: 56px;
    height: 56px;
    object-fit: contain;
}

.header h1 {
    margin: 0;
    font-size: 22px;
    color: var(--primary);
}

.header-contacts {
    margin: 4px 0 0;
    font-size: 13px;
    color: var(--text-muted);
}

.header-right {
    display: flex;
    gap: 10px;
}

/* ── Настройки ─────────────────────────────────────────────── */

.settings-panel {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 20px;
}

.settings-panel h2 {
    margin-top: 0;
    font-size: 18px;
}

.settings-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 16px;
    margin-bottom: 16px;
}

.settings-grid label {
    display: flex;
    flex-direction: column;
    font-size: 14px;
    color: var(--text-muted);
}

.settings-grid input {
    margin-top: 6px;
    padding: 10px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 15px;
}

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

/* ── Форма ввода ───────────────────────────────────────────── */

.input-section {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 20px;
    margin-bottom: 20px;
}

.input-section h2 {
    margin-top: 0;
    font-size: 18px;
}

.estimate-meta {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 12px;
    margin-bottom: 16px;
}

.estimate-meta input,
.input-section input,
.input-section textarea {
    width: 100%;
    padding: 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 15px;
    font-family: inherit;
}

.input-section textarea {
    resize: vertical;
    min-height: 100px;
}

.input-section label {
    display: block;
    margin-bottom: 8px;
    font-weight: 500;
}

/* ── Блок автомобиля/VIN ───────────────────────────────────── */

.car-block {
    background: #fafafa;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 16px;
    margin-bottom: 16px;
}

.vin-row {
    display: flex;
    gap: 10px;
    margin-bottom: 8px;
    flex-wrap: wrap;
}

.frame-row {
    margin-top: 10px;
}

input.missing,
textarea.missing {
    border-color: var(--error) !important;
    background-color: #fff5f5 !important;
}

.vin-row input {
    flex: 1;
    min-width: 200px;
    text-transform: uppercase;
    font-family: monospace;
    font-size: 16px;
    letter-spacing: 1px;
}

.car-details {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
    gap: 10px;
    margin-top: 12px;
}

.car-details input[readonly] {
    background: #f0f0f0;
    color: var(--text-muted);
}

.input-actions {
    display: flex;
    gap: 12px;
    margin-top: 12px;
    flex-wrap: wrap;
}

.status-text {
    margin-top: 12px;
    font-size: 14px;
    color: var(--text-muted);
    min-height: 20px;
}

.status-text.loading {
    color: var(--primary);
}

.status-text.error {
    color: var(--error);
}

.status-text.success {
    color: var(--success);
}

/* ── Смета ─────────────────────────────────────────────────── */

.estimate-section {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 20px;
}

.estimate-title {
    margin-top: 0;
    font-size: 22px;
    text-align: center;
    margin-bottom: 8px;
}

.estimate-info {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 8px;
    margin-bottom: 20px;
    font-size: 14px;
    color: var(--text-muted);
}

.estimate-table-wrap {
    width: 100%;
    overflow-x: auto;
    margin-bottom: 20px;
    -webkit-overflow-scrolling: touch;
}

.estimate-table {
    width: 100%;
    min-width: 800px;
    border-collapse: collapse;
}

.estimate-table th,
.estimate-table td {
    border: 1px solid var(--border);
    padding: 10px;
    text-align: left;
    vertical-align: top;
}

.estimate-table th {
    background: #f0f0f0;
    font-weight: 600;
    font-size: 14px;
}

.estimate-table td input,
.estimate-table td textarea {
    width: 100%;
    border: 1px solid var(--border);
    background: #fff;
    padding: 6px;
    font-size: 14px;
    font-family: inherit;
    resize: none;
    overflow: hidden;
    line-height: 1.4;
    border-radius: 4px;
}

.estimate-table td textarea {
    min-height: 36px;
    field-sizing: content;
}

.estimate-table td input:focus,
.estimate-table td textarea:focus {
    outline: none;
    border-color: var(--primary);
    background: #fff;
    border-radius: 4px;
}

.col-num { width: 40px; text-align: center; }
.col-work { min-width: 220px; width: 30%; }
.col-hours { width: 90px; min-width: 80px; text-align: center; }
.col-hours input { text-align: center; font-weight: 500; width: 100%; padding: 6px 4px; }
.col-price { width: 120px; text-align: right; }
.col-price input { text-align: right; font-weight: 500; }
.col-hint { min-width: 200px; width: 25%; }
.col-hint textarea,
.col-note textarea {
    min-height: 44px;
    max-height: none;
    overflow: hidden;
    resize: none;
    field-sizing: content;
    line-height: 1.45;
}
.col-explanation { min-width: 200px; width: 25%; }
.col-note { min-width: 200px; width: 25%; }
.col-actions { width: 60px; text-align: center; }

.estimate-table td textarea {
    min-height: 44px;
    max-height: none;
    field-sizing: content;
    overflow: hidden;
}

.col-explanation.print-only {
    display: none;
}

.btn-sm {
    padding: 4px 10px;
    font-size: 13px;
}

input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

input[type="number"] {
    -moz-appearance: textfield;
}

.estimate-table tfoot td {
    background: #fafafa;
    font-size: 16px;
}

.empty-cell {
    text-align: center;
    color: var(--text-muted);
    padding: 40px 20px;
}

.text-right {
    text-align: right;
}

.estimate-actions {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
}

/* ── Модальное окно ────────────────────────────────────────── */

.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
    padding: 20px;
}

.modal-content {
    background: var(--card-bg);
    border-radius: 12px;
    width: 100%;
    max-width: 800px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
}

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

.modal-header h2 {
    margin: 0;
    font-size: 18px;
}

.modal-body {
    padding: 20px;
    overflow-y: auto;
}

.saved-table {
    width: 100%;
    border-collapse: collapse;
}

.saved-table th,
.saved-table td {
    border: 1px solid var(--border);
    padding: 10px;
    text-align: left;
}

.saved-table th {
    background: #f0f0f0;
}

.empty-text {
    text-align: center;
    color: var(--text-muted);
    padding: 30px;
}

/* ── Печать ────────────────────────────────────────────────── */

.print-only {
    display: none;
}

@media print {
    @page {
        size: A4 portrait;
        margin: 12mm;
    }

    .no-print {
        display: none !important;
    }

    body {
        background: white;
        font-size: 12pt;
        color: #000;
    }

    .app {
        display: block !important;
    }

    .container {
        max-width: none;
        padding: 0;
    }

    .estimate-section {
        border: none;
        box-shadow: none;
        padding: 0;
    }

    .estimate-header {
        display: flex;
        align-items: center;
        gap: 16px;
        margin-bottom: 16px;
        border-bottom: 2px solid #000;
        padding-bottom: 16px;
    }

    .print-logo {
        width: 64px;
        height: 64px;
        object-fit: contain;
    }

    .print-header-info h2 {
        margin: 0;
        font-size: 18pt;
    }

    .print-header-info p {
        margin: 4px 0 0;
        font-size: 10pt;
    }

    .estimate-title {
        font-size: 16pt;
        margin-bottom: 12px;
    }

    .estimate-info {
        font-size: 10pt;
        margin-bottom: 14px;
    }

    .estimate-table-wrap {
        overflow: visible;
        margin-bottom: 12px;
    }

    .estimate-table {
        min-width: 0;
        width: 100%;
        font-size: 9pt;
        table-layout: fixed;
    }

    .estimate-table th,
    .estimate-table td {
        border: 1px solid #000;
        padding: 5px 6px;
        vertical-align: top;
        word-break: break-word;
        overflow-wrap: break-word;
    }

    .estimate-table th {
        background: #eee;
    }

    .estimate-table td input,
    .estimate-table td textarea {
        border: none;
        resize: none;
        background: transparent;
        padding: 0;
        font-size: 9pt;
        max-height: none;
        overflow: visible;
    }

    .estimate-table tbody tr {
        page-break-inside: auto;
    }

    .estimate-table tfoot td {
        background: #f5f5f5;
        font-size: 10pt;
    }

    .col-num { width: 5%; }
    .col-work { width: 24%; min-width: 0; }
    .col-hours { width: 10%; text-align: center; }
    .col-price { width: 12%; text-align: right; }
    .col-explanation.print-only,
    th.col-explanation.print-only {
        display: table-cell !important;
        width: 28%;
        min-width: 0;
        font-size: 9pt;
        white-space: normal;
        word-wrap: break-word;
        overflow-wrap: break-word;
        line-height: 1.35;
        vertical-align: top;
    }
    .col-note { width: 21%; min-width: 0; }
    .col-hint.no-print { display: none !important; }

    .print-signatures {
        margin-top: 40px;
        page-break-inside: avoid;
    }

    .signature-line {
        display: flex;
        align-items: center;
        margin-bottom: 24px;
    }

    .signature-line span:first-child {
        width: 100px;
        font-weight: 500;
    }

    .signature-space {
        flex: 1;
        border-bottom: 1px solid #000;
        height: 24px;
    }

    .print-only {
        display: block !important;
    }
}

/* ── Подвал ────────────────────────────────────────────────── */

.footer {
    text-align: center;
    padding: 20px;
    color: var(--text-muted);
    font-size: 13px;
}

/* ── Анимации и визуальные эффекты ─────────────────────────── */

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

@keyframes pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.05); }
}

@keyframes float {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-6px); }
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

@keyframes confetti-fall {
    0% { transform: translateY(-10vh) rotate(0deg); opacity: 1; }
    100% { transform: translateY(110vh) rotate(720deg); opacity: 0; }
}

.confetti-piece {
    position: fixed;
    top: -10px;
    width: 10px;
    height: 10px;
    z-index: 400;
    pointer-events: none;
    animation: confetti-fall 2.5s ease-out forwards;
}

.status-flash {
    animation: statusPulse 0.4s ease-out;
}

@keyframes statusPulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.02); }
    100% { transform: scale(1); }
}

.app .input-section,
.app .estimate-section,
.app .settings-panel {
    animation: fadeInUp 0.4s ease-out;
}

.calculating .input-section,
.calculating .estimate-section {
    opacity: 0.7;
    transition: opacity 0.2s;
}

.calculating::after {
    content: "";
    position: fixed;
    top: 50%;
    left: 50%;
    width: 48px;
    height: 48px;
    margin: -24px 0 0 -24px;
    border: 4px solid rgba(196, 30, 58, 0.2);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    z-index: 300;
}

/* Помощник */
.helper-bot {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 64px;
    height: 64px;
    background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 8px 24px rgba(196, 30, 58, 0.4);
    cursor: pointer;
    z-index: 150;
    animation: float 3s ease-in-out infinite;
    transition: transform 0.2s, box-shadow 0.2s;
}

.helper-bot:hover {
    transform: scale(1.08);
    box-shadow: 0 12px 32px rgba(196, 30, 58, 0.5);
}

.helper-bot.thinking {
    animation: pulse-ring 1.5s ease-in-out infinite;
}

.helper-face {
    position: relative;
    width: 38px;
    height: 30px;
}

.helper-eyes {
    display: flex;
    justify-content: space-between;
    width: 100%;
    padding: 0 2px;
}

.helper-eye {
    width: 12px;
    height: 14px;
    background: white;
    border-radius: 50%;
    position: relative;
    overflow: hidden;
    animation: blink 4s infinite;
}

.helper-pupil {
    position: absolute;
    width: 6px;
    height: 6px;
    background: #1a1a1a;
    border-radius: 50%;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    transition: transform 0.15s;
}

.helper-mouth {
    width: 18px;
    height: 8px;
    background: white;
    border-radius: 0 0 12px 12px;
    margin: 5px auto 0;
    transition: height 0.2s, border-radius 0.2s;
}

.helper-bot:hover .helper-mouth {
    height: 10px;
    border-radius: 0 0 14px 14px;
}

.helper-aura {
    position: absolute;
    inset: -8px;
    border-radius: 50%;
    border: 2px solid rgba(196, 30, 58, 0.25);
    opacity: 0;
    transform: scale(0.9);
    transition: opacity 0.3s, transform 0.3s;
}

.helper-bot:hover .helper-aura {
    opacity: 1;
    transform: scale(1);
}

.helper-thinking {
    position: absolute;
    top: -10px;
    right: -6px;
    background: white;
    color: var(--primary);
    font-size: 18px;
    font-weight: 700;
    width: 26px;
    height: 26px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
    animation: pulse 1.2s ease-in-out infinite;
}

@keyframes pulse-ring {
    0% { box-shadow: 0 0 0 0 rgba(196, 30, 58, 0.45); }
    70% { box-shadow: 0 0 0 14px rgba(196, 30, 58, 0); }
    100% { box-shadow: 0 0 0 0 rgba(196, 30, 58, 0); }
}

@keyframes blink {
    0%, 48%, 52%, 100% { transform: scaleY(1); }
    50% { transform: scaleY(0.1); }
}

.helper-tooltip {
    position: fixed;
    bottom: 92px;
    right: 24px;
    width: 280px;
    max-width: calc(100vw - 48px);
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 16px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.15);
    z-index: 150;
    font-size: 14px;
    color: var(--text);
    opacity: 0;
    pointer-events: none;
    transform: translateY(10px);
    transition: opacity 0.3s, transform 0.3s;
}

.helper-tooltip.visible {
    opacity: 1;
    pointer-events: auto;
    transform: translateY(0);
}

.helper-tooltip h4 {
    margin: 0 0 8px;
    color: var(--primary);
    font-size: 15px;
}

.helper-tooltip ul {
    margin: 0;
    padding-left: 18px;
    color: var(--text-muted);
}

.helper-tooltip li {
    margin-bottom: 6px;
}

.helper-close {
    position: absolute;
    top: 8px;
    right: 10px;
    background: none;
    border: none;
    font-size: 18px;
    cursor: pointer;
    color: var(--text-muted);
}

/* ── Адаптив ───────────────────────────────────────────────── */

@media (max-width: 768px) {
    .header {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
        padding: 12px 16px;
    }

    .header-logo {
        width: 44px;
        height: 44px;
    }

    .header h1 {
        font-size: 18px;
    }

    .header-right {
        width: 100%;
        justify-content: space-between;
    }

    .container {
        padding: 12px;
    }

    .input-section,
    .estimate-section,
    .settings-panel {
        padding: 14px;
    }

    .estimate-meta,
    .settings-grid,
    .car-details {
        grid-template-columns: 1fr;
    }

    .vin-row {
        flex-direction: column;
    }

    .vin-row input,
    .vin-row button {
        width: 100%;
    }

    .input-actions,
    .estimate-actions,
    .settings-actions {
        flex-direction: column;
    }

    .btn {
        width: 100%;
    }

    .estimate-table th,
    .estimate-table td {
        padding: 6px;
        font-size: 13px;
    }

    .estimate-table td textarea {
        min-height: 50px;
    }

    .modal-content {
        max-height: 95vh;
        margin: 10px;
    }

    .saved-table {
        font-size: 13px;
    }
}
