/* =============================================
   CSS Custom Properties (Design Tokens)
   ============================================= */
:root {
    --primary-color: #2563eb;
    --primary-hover: #1d4ed8;
    --primary-light: rgba(37, 99, 235, 0.1);
    --success-color: #10b981;
    --success-hover: #059669;
    --bg-color: #f0f4f8;
    --surface-color: #ffffff;
    --text-main: #1e293b;
    --text-muted: #64748b;
    --border-color: #e2e8f0;
    --error-color: #ef4444;
    --radius-sm: 8px;
    --radius-md: 14px;
    --radius-lg: 20px;
    --shadow-sm: 0 1px 3px 0 rgb(0 0 0 / 0.07), 0 1px 2px -1px rgb(0 0 0 / 0.07);
    --shadow-md: 0 4px 12px -1px rgb(0 0 0 / 0.12), 0 2px 6px -2px rgb(0 0 0 / 0.08);
    --shadow-lg: 0 10px 28px -5px rgb(0 0 0 / 0.15), 0 4px 10px -3px rgb(0 0 0 / 0.1);
    --transition: 0.22s cubic-bezier(0.4, 0, 0.2, 1);

    /* Touch target minimum (WCAG / HIG / Material) */
    --touch-target: 48px;
}

/* =============================================
   Reset & Base
   ============================================= */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    /* Убираем синюю подсветку при тапе на Android/iOS */
    -webkit-tap-highlight-color: transparent;
}

html {
    /* Предотвращает автозум при фокусе на input (поле < 16px) */
    font-size: 16px;
    scroll-behavior: smooth;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
    /* КРИТИЧНО: запрет горизонтального скролла */
    overflow-x: hidden;
    max-width: 100%;
}

body {
    font-family: 'Inter', system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-main);
    line-height: 1.5;
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    /* НЕ ставим overflow-x: hidden на body — сломает вертикальный скролл на iOS/Android.
       Горизонтальный overflow блокируется на html (выше) и через clip на контейнере. */
    width: 100%;
    /* Инерционный скролл на iOS */
    -webkit-overflow-scrolling: touch;
}

/* =============================================
   Layout
   ============================================= */
.app-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: min(640px, 100vw);
    margin: 0 auto;
    /* clip — в отличие от hidden, не создаёт scroll-контейнер, не ломает тач-скролл */
    overflow-x: clip;
}

/* =============================================
   Header
   ============================================= */
.app-header {
    background: var(--surface-color);
    /* Safe area для iPhone с челкой/Dynamic Island */
    padding: calc(env(safe-area-inset-top) + 20px) 20px 20px;
    text-align: center;
    border-bottom: 1px solid var(--border-color);
    box-shadow: var(--shadow-sm);
    position: sticky;
    top: 0;
    z-index: 10;
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    background: rgba(255, 255, 255, 0.95);
}

.app-title {
    font-size: 1.4rem;
    font-weight: 700;
    color: var(--primary-color);
    letter-spacing: -0.4px;
    line-height: 1.2;
}

.app-subtitle {
    font-size: 0.875rem;
    color: var(--text-muted);
    margin-top: 4px;
}

/* =============================================
   Main Content
   ============================================= */
.app-main {
    flex: 1;
    padding: 20px 16px;
    /* Safe area для iPhone с закруглениями */
    padding-bottom: calc(env(safe-area-inset-bottom) + 24px);
}

.search-section {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin-bottom: 28px;
}

/* =============================================
   Search Box
   ============================================= */
.search-box {
    display: flex;
    gap: 10px;
    align-items: stretch;
    /* Не выходить за границы */
    width: 100%;
    min-width: 0;
}

#searchInput {
    flex: 1;
    /* КРИТИЧНО: минимум 16px — иначе iOS Safari зумит при фокусе */
    font-size: 1rem;
    font-family: inherit;
    padding: 0 16px;
    height: var(--touch-target);
    border: 1.5px solid var(--border-color);
    border-radius: var(--radius-md);
    background: var(--surface-color);
    color: var(--text-main);
    transition: border-color var(--transition), box-shadow var(--transition);
    -webkit-appearance: none;
    appearance: none;
    /* Не вылезать за контейнер */
    min-width: 0;
    width: 100%;
}

#searchInput::placeholder {
    color: var(--text-muted);
    font-size: 0.9rem;
}

#searchInput:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px var(--primary-light);
}

/* =============================================
   Buttons
   ============================================= */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: var(--touch-target);
    padding: 0 24px;
    font-size: 0.9375rem;
    font-weight: 600;
    font-family: inherit;
    border: none;
    border-radius: var(--radius-md);
    cursor: pointer;
    text-decoration: none;
    transition: background-color var(--transition), transform var(--transition), box-shadow var(--transition);
    /* Предотвращаем выделение текста при тапе */
    user-select: none;
    -webkit-user-select: none;
    /* Улучшение кликабельности */
    -webkit-appearance: none;
    white-space: nowrap;
}

.btn:active {
    transform: scale(0.96);
}

/* iOS: убираем стандартное выделение при долгом тапе */
.btn:focus-visible {
    outline: 2px solid var(--primary-color);
    outline-offset: 2px;
}

.btn-primary {
    background-color: var(--primary-color);
    color: white;
    box-shadow: 0 2px 8px rgba(37, 99, 235, 0.35);
    flex-shrink: 0;
}

.btn-primary:hover {
    background-color: var(--primary-hover);
    box-shadow: 0 4px 12px rgba(37, 99, 235, 0.4);
}

.btn-secondary {
    background-color: var(--surface-color);
    color: var(--text-main);
    border: 1.5px solid var(--border-color);
    width: 100%;
    font-size: 0.9375rem;
}

.btn-secondary:hover {
    background-color: #f1f5f9;
    border-color: #cbd5e1;
}

.btn-success {
    background: linear-gradient(135deg, #10b981, #059669);
    color: white;
    width: 100%;
    margin-top: 14px;
    box-shadow: 0 2px 8px rgba(16, 185, 129, 0.3);
    font-size: 0.9375rem;
}

.btn-success:hover {
    box-shadow: 0 4px 12px rgba(16, 185, 129, 0.4);
}

.hidden-input {
    position: absolute;
    width: 1px;
    height: 1px;
    opacity: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    pointer-events: none;
}

/* =============================================
   Camera Button (специальный стиль)
   ============================================= */
.camera-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    width: 100%;
    min-height: var(--touch-target);
    padding: 12px 20px;
    background: var(--surface-color);
    border: 1.5px dashed #94a3b8;
    border-radius: var(--radius-md);
    color: var(--text-muted);
    font-size: 0.9375rem;
    font-weight: 500;
    cursor: pointer;
    transition: border-color var(--transition), background var(--transition), color var(--transition);
    font-family: inherit;
    user-select: none;
    -webkit-user-select: none;
}

.camera-btn:hover,
.camera-btn:active {
    border-color: var(--primary-color);
    background: var(--primary-light);
    color: var(--primary-color);
    border-style: solid;
}

.camera-btn .icon {
    font-size: 1.3rem;
    line-height: 1;
    flex-shrink: 0;
}

/* =============================================
   Divider
   ============================================= */
.search-divider {
    display: flex;
    align-items: center;
    gap: 10px;
    color: var(--text-muted);
    font-size: 0.8rem;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.search-divider::before,
.search-divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border-color);
}

/* =============================================
   Loader
   ============================================= */
.loader-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 48px 20px;
    gap: 14px;
    color: var(--text-muted);
    font-size: 0.9375rem;
    font-weight: 500;
}

.spinner {
    width: 44px;
    height: 44px;
    border: 3px solid rgba(37, 99, 235, 0.12);
    border-radius: 50%;
    border-top-color: var(--primary-color);
    animation: spin 0.85s linear infinite;
    flex-shrink: 0;
}

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

.hidden {
    display: none !important;
}

/* =============================================
   Error / Notice Message
   ============================================= */
.error-msg {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    padding: 16px;
    background: #fffbeb;
    color: #92400e;
    border-radius: var(--radius-md);
    border: 1px solid #fde68a;
    margin-bottom: 20px;
    font-size: 0.9rem;
    font-weight: 500;
    line-height: 1.5;
    box-shadow: 0 2px 8px rgba(251, 191, 36, 0.12);
}

.error-msg .error-icon {
    font-size: 1.2rem;
    flex-shrink: 0;
    line-height: 1.4;
}

/* =============================================
   Results
   ============================================= */
.results-container {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

/* =============================================
   Group Container
   ============================================= */
.group-container {
    background: var(--surface-color);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-lg);
    overflow: hidden;
    box-shadow: var(--shadow-sm);
    opacity: 0;
    transform: translateY(12px);
    animation: fadeUp 0.4s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

/* =============================================
   Cards
   ============================================= */
.card {
    padding: 16px;
    transition: background-color var(--transition);
}

.card + .card {
    border-top: 1px solid var(--border-color);
}

/* Карточка оригинала */
.card-original {
    background: linear-gradient(135deg, #eff6ff 0%, #f0f9ff 100%);
    border-bottom: 2px solid #bfdbfe;
    padding: 20px 16px 16px;
}

.badge-original {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    background-color: var(--primary-color);
    color: white;
    font-size: 0.7rem;
    font-weight: 700;
    padding: 3px 9px;
    border-radius: 100px; /* pill */
    margin-bottom: 10px;
    text-transform: uppercase;
    letter-spacing: 0.6px;
}

.card-header {
    margin-bottom: 10px;
}

.card-title {
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--text-main);
    line-height: 1.3;
    margin-bottom: 4px;
}

.card-manufacturer {
    font-size: 0.82rem;
    color: var(--text-muted);
    line-height: 1.4;
}

.card-details {
    display: flex;
    flex-wrap: wrap;
    gap: 6px 16px;
    margin-bottom: 4px;
}

.detail-row {
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 80px;
}

.detail-label {
    font-size: 0.75rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.3px;
    font-weight: 600;
}

.detail-value {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-main);
}

/* =============================================
   Analogs Section Title
   ============================================= */
.analogs-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px;
    background: #f8fafc;
    border-bottom: 1px solid var(--border-color);
    font-size: 0.8rem;
    font-weight: 700;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.analogs-count {
    background: var(--primary-color);
    color: white;
    font-size: 0.7rem;
    font-weight: 700;
    padding: 2px 7px;
    border-radius: 100px;
    margin-left: auto;
}

/* =============================================
   No Items Placeholder
   ============================================= */
.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    padding: 32px 20px;
    color: var(--text-muted);
    gap: 8px;
}

.empty-state .empty-icon {
    font-size: 2.5rem;
    line-height: 1;
}

.empty-state p {
    font-size: 0.9rem;
    line-height: 1.5;
}

/* =============================================
   Footer
   ============================================= */
.app-footer {
    background: #f1f5f9;
    border-top: 1px solid var(--border-color);
    padding: 16px 20px;
    padding-bottom: calc(env(safe-area-inset-bottom) + 16px);
    text-align: center;
}

.disclaimer {
    font-size: 0.78rem;
    color: var(--text-muted);
    line-height: 1.6;
    max-width: 580px;
    margin: 0 auto;
}

/* =============================================
   Animations
   ============================================= */
@keyframes fadeUp {
    from {
        opacity: 0;
        transform: translateY(12px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* =============================================
   Desktop adjustments
   ============================================= */
@media (min-width: 640px) {
    body {
        padding: 32px 20px;
        background: var(--bg-color);
        overflow-x: hidden;
    }

    .app-container {
        border-radius: var(--radius-lg);
        box-shadow: var(--shadow-lg);
        overflow: hidden;
        background: var(--surface-color);
        max-width: 640px;
    }

    .app-header {
        position: static;
        padding: 28px 24px 24px;
        background: rgba(255, 255, 255, 1);
        border-radius: 0;
    }

    .app-main {
        padding: 24px 24px;
    }

    .app-footer {
        border-radius: 0;
    }

    .app-title {
        font-size: 1.6rem;
    }
}

/* =============================================
   Reduce motion (доступность)
   ============================================= */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}
