:root {
    color-scheme: light;
    --primary-color: #2563eb;
    --primary-hover: #1d4ed8;
    --body-bg-1: #f0f4f8;
    --body-bg-2: #e2e8f0;
    --card-bg: #ffffff;
    --text-main: #1f2937;
    --text-muted: #6b7280;
    --border-color: #e5e7eb;
    --input-bg: #ffffff;
    --success-color: #10b981;
    --danger-color: #dc2626;
    --danger-bg: #fef2f2;
    --ai-section-bg: #f8fafc;
    --ai-section-border: #cbd5e1;
    --ai-section-hover: #eff6ff;
    --upload-card-bg: #ffffff;
    --badge-pending-bg: #fef3c7;
    --badge-pending-text: #d97706;
    --badge-paid-bg: #d1fae5;
    --badge-paid-text: #059669;
}

[data-theme="dark"] {
    color-scheme: dark;
    --primary-color: #3b82f6;
    --primary-hover: #60a5fa;
    --body-bg-1: #0f172a;
    --body-bg-2: #1e293b;
    --card-bg: #1e293b;
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --border-color: #334155;
    --input-bg: #0f172a;
    --success-color: #34d399;
    --danger-color: #fca5a5;
    --danger-bg: rgba(220, 38, 38, 0.12);
    --ai-section-bg: #0f172a;
    --ai-section-border: #475569;
    --ai-section-hover: #1e293b;
    --upload-card-bg: #1e293b;
    --badge-pending-bg: rgba(217, 119, 6, 0.2);
    --badge-pending-text: #fbbf24;
    --badge-paid-bg: rgba(5, 150, 105, 0.2);
    --badge-paid-text: #34d399;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

html { min-height: 100%; }

body {
    min-height: 100vh;
    min-height: 100dvh;
    background: linear-gradient(135deg, var(--body-bg-1), var(--body-bg-2));
    color: var(--text-main);
    font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
    transition: background 0.3s ease, color 0.3s ease;
    -webkit-text-size-adjust: 100%;
}

button, input { font: inherit; }
button { touch-action: manipulation; }

.svg-sprite {
    position: absolute;
    width: 0;
    height: 0;
    overflow: hidden;
}

.screen {
    display: flex;
    justify-content: center;
    min-height: 100vh;
    min-height: 100dvh;
    padding: max(20px, env(safe-area-inset-top)) 20px max(20px, env(safe-area-inset-bottom));
}

.app-container {
    align-self: center;
    position: relative;
    width: 100%;
    max-width: 420px;
    padding: 32px 24px;
    border-radius: 20px;
    background: var(--card-bg);
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1);
    transition: background-color 0.3s ease, box-shadow 0.3s ease;
}

.icon, .logo-icon, .ai-icon, .upload-icon, .title-icon, .btn-icon {
    fill: none;
    stroke: currentColor;
    stroke-width: 1.9;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.logo-icon {
    width: 34px;
    height: 34px;
    margin-bottom: 12px;
    color: var(--primary-color);
    fill: currentColor;
    stroke: none;
}

.theme-toggle, .settings-toggle {
    position: absolute;
    z-index: 2;
    top: 18px;
    right: 18px;
    display: grid;
    place-items: center;
    width: 40px;
    height: 40px;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: var(--text-muted);
    cursor: pointer;
}

.theme-toggle:active, .settings-toggle:active { transform: scale(0.92); }
.theme-toggle .icon, .settings-toggle .icon { width: 21px; height: 21px; }

.settings-toggle {
    right: auto;
    left: 18px;
}

.connection-dot {
    position: absolute;
    top: 6px;
    right: 5px;
    width: 8px;
    height: 8px;
    border: 2px solid var(--card-bg);
    border-radius: 50%;
    background: var(--danger-color);
}

.connection-dot.configured { background: var(--success-color); }

.header {
    margin-bottom: 30px;
    text-align: center;
}

.header h1 {
    margin-bottom: 6px;
    color: var(--text-main);
    font-size: 24px;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.header p, .ai-header p {
    color: var(--text-muted);
    font-size: 14px;
    line-height: 1.45;
}

.form-group { margin-bottom: 18px; }

.form-group label {
    display: block;
    margin-bottom: 6px;
    color: var(--text-main);
    font-size: 13px;
    font-weight: 600;
}

.form-control {
    width: 100%;
    min-height: 46px;
    padding: 12px 14px;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    outline: none;
    background: var(--input-bg);
    color: var(--text-main);
    font-size: 16px;
    transition: border-color 0.2s ease, box-shadow 0.2s ease, background-color 0.3s ease;
}

.form-control::placeholder { color: var(--text-muted); opacity: 0.85; }
.form-control:focus { border-color: var(--primary-color); box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12); }
.form-control.invalid { border-color: var(--danger-color); }

.field-error {
    display: none;
    margin-top: 5px;
    color: var(--danger-color);
    font-size: 11px;
}

.field-error.show { display: block; }

.ai-fill-status {
    display: none;
    align-items: center;
    gap: 5px;
    margin-top: 7px;
    color: var(--success-color);
    font-size: 11px;
    font-weight: 600;
}

.ai-fill-status.show { display: flex; }
.ai-fill-status::before { content: "✓"; }
.ai-fill-status.error { color: var(--danger-color); }
.ai-fill-status.error::before { content: "!"; }

.form-control.ai-filled {
    border-color: var(--success-color);
    animation: ai-field-filled 0.8s ease;
    box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.14);
}

@keyframes ai-field-filled {
    0% { transform: scale(0.985); }
    55% { transform: scale(1.012); }
    100% { transform: none; }
}

.btn-submit {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    min-height: 48px;
    margin-top: 10px;
    padding: 14px;
    border: 0;
    border-radius: 10px;
    background: var(--primary-color);
    color: #fff;
    font-size: 16px;
    font-weight: 650;
    cursor: pointer;
    box-shadow: 0 4px 10px rgba(37, 99, 235, 0.18);
    transition: background-color 0.2s ease, transform 0.15s ease, opacity 0.2s ease;
}

.btn-submit:active { transform: scale(0.985); }
.btn-submit:disabled { cursor: default; opacity: 0.7; }
.btn-icon { width: 18px; height: 18px; }
.spin { animation: spin 1.6s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.consult-progress {
    min-height: 17px;
    margin-top: 8px;
    color: var(--text-muted);
    font-size: 11px;
    line-height: 1.4;
    text-align: center;
}

.game-wait-button {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    min-height: 44px;
    margin-top: 9px;
    padding: 11px 14px;
    border: 1px solid rgba(37, 99, 235, 0.35);
    border-radius: 10px;
    background: var(--ai-section-hover);
    color: var(--primary-color);
    font-size: 13px;
    font-weight: 700;
    cursor: pointer;
    animation: game-button-in 0.28s ease both;
}

.game-wait-button[hidden] { display: none; }
.game-wait-button:active { transform: scale(0.985); }
.game-wait-button .btn-icon { width: 19px; height: 19px; }

@keyframes game-button-in {
    from { opacity: 0; transform: translateY(5px); }
    to { opacity: 1; transform: none; }
}

.form-message {
    display: none;
    margin: -2px 0 12px;
    padding: 10px 12px;
    border-radius: 8px;
    background: var(--danger-bg);
    color: var(--danger-color);
    font-size: 12px;
    line-height: 1.4;
}

.form-message.show { display: block; }

.divider {
    display: flex;
    align-items: center;
    margin: 25px 0;
    color: var(--text-muted);
    font-size: 12px;
    font-weight: 600;
    letter-spacing: 0.02em;
}

.divider::before, .divider::after {
    flex: 1;
    border-bottom: 1px solid var(--border-color);
    content: "";
}

.divider span { padding: 0 10px; }

.ai-section {
    padding: 20px;
    border: 2px dashed var(--ai-section-border);
    border-radius: 16px;
    background: var(--ai-section-bg);
    text-align: center;
    transition: border-color 0.25s ease, background-color 0.3s ease;
}

.ai-header { margin-bottom: 15px; }
.ai-icon { width: 23px; height: 23px; margin-bottom: 7px; color: var(--primary-color); }
.ai-header h2 { margin-bottom: 4px; font-size: 15px; font-weight: 650; }
.ai-header p { font-size: 12px; }

.upload-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.upload-card {
    display: flex;
    min-width: 0;
    min-height: 78px;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 5px;
    padding: 11px 8px;
    overflow: hidden;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    background: var(--upload-card-bg);
    color: var(--text-main);
    cursor: pointer;
    transition: border-color 0.2s ease, transform 0.15s ease, background-color 0.3s ease;
}

.upload-card:active { transform: scale(0.98); }
.upload-card.selected { border-color: var(--success-color); }
.upload-card.reading { pointer-events: none; opacity: 0.72; }
.upload-card .upload-icon { width: 20px; height: 20px; color: var(--text-muted); }
.upload-card.selected .upload-icon, .upload-card.selected span { color: var(--success-color); }
.upload-card span { font-size: 12px; font-weight: 600; }
.upload-card small { display: none; width: 100%; overflow: hidden; color: var(--text-muted); font-size: 9px; text-overflow: ellipsis; white-space: nowrap; }
.upload-card.selected small { display: block; }
.upload-card input { display: none; }

.results-section {
    display: none;
    margin-top: 30px;
    padding-top: 25px;
    border-top: 1px dashed var(--border-color);
}

.results-section.show { display: block; animation: slide-up 0.45s ease both; }
@keyframes slide-up { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }

.results-title {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 15px;
    font-size: 16px;
    font-weight: 650;
}

.result-notice {
    display: none;
    margin-bottom: 12px;
    padding: 10px 12px;
    border-radius: 9px;
    background: var(--ai-section-bg);
    color: var(--text-muted);
    font-size: 11px;
    line-height: 1.5;
}

.result-notice.show { display: block; }

.title-icon { width: 19px; height: 19px; color: var(--primary-color); }
.invoice-list { display: flex; flex-direction: column; gap: 12px; }

.invoice-card {
    padding: 16px;
    border: 1px solid var(--border-color);
    border-radius: 12px;
    background: var(--input-bg);
    transition: border-color 0.2s ease, background-color 0.3s ease;
}

.invoice-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.invoice-info { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.invoice-month { font-size: 15px; font-weight: 650; }
.invoice-date { color: var(--text-muted); font-size: 12px; }
.invoice-status { display: flex; flex-shrink: 0; flex-direction: column; align-items: flex-end; gap: 6px; }
.invoice-value { font-size: 16px; font-weight: 750; }

.badge {
    padding: 4px 10px;
    border-radius: 20px;
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.05em;
    text-transform: uppercase;
}

.badge-pending { background: var(--badge-pending-bg); color: var(--badge-pending-text); }
.badge-paid { background: var(--badge-paid-bg); color: var(--badge-paid-text); }
.invoice-actions { display: flex; gap: 10px; }

.btn-action {
    display: flex;
    flex: 1;
    align-items: center;
    justify-content: center;
    gap: 6px;
    min-height: 40px;
    padding: 9px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: transparent;
    color: var(--text-main);
    font-size: 12px;
    font-weight: 600;
    cursor: pointer;
}

.btn-action:active { transform: scale(0.98); }
.btn-action.btn-primary { border-color: var(--primary-color); background: var(--primary-color); color: #fff; }
.btn-action .btn-icon { width: 15px; height: 15px; }

.empty-state { padding: 18px; color: var(--text-muted); font-size: 13px; text-align: center; }

.toast {
    position: fixed;
    z-index: 20;
    right: 20px;
    bottom: max(24px, env(safe-area-inset-bottom));
    left: 20px;
    max-width: 380px;
    margin: auto;
    padding: 12px 16px;
    border-radius: 10px;
    background: #111827;
    color: #fff;
    font-size: 13px;
    text-align: center;
    opacity: 0;
    pointer-events: none;
    transform: translateY(10px);
    transition: opacity 0.2s ease, transform 0.2s ease;
}

.toast.show { opacity: 1; transform: none; }

body.game-open { overflow: hidden; }

.game-modal {
    position: fixed;
    z-index: 80;
    inset: 0;
    display: flex;
    visibility: hidden;
    flex-direction: column;
    background: #050510;
    opacity: 0;
    pointer-events: none;
    transform: translateY(18px);
    transition: opacity 0.2s ease, transform 0.2s ease, visibility 0.2s;
}

.game-modal.show {
    visibility: visible;
    opacity: 1;
    pointer-events: auto;
    transform: none;
}

.game-toolbar {
    position: absolute;
    z-index: 3;
    top: max(8px, env(safe-area-inset-top));
    right: max(8px, env(safe-area-inset-right));
    display: flex;
    flex: 0 0 auto;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    min-height: 0;
    padding: 0;
    border: 0;
    background: transparent;
    color: #fff;
    pointer-events: none;
}

.game-toolbar-title { display: none; align-items: center; gap: 10px; min-width: 0; }
.game-toolbar-title > .icon { flex: 0 0 auto; width: 24px; height: 24px; color: #00ffcc; }
.game-toolbar-title > div { display: flex; min-width: 0; flex-direction: column; gap: 2px; }
.game-toolbar-title strong { overflow: hidden; font-size: 14px; text-overflow: ellipsis; white-space: nowrap; }
.game-toolbar-title small { color: #94a3b8; font-size: 10px; }

.game-close {
    display: flex;
    flex: 0 0 auto;
    align-items: center;
    justify-content: center;
    width: 40px;
    gap: 0;
    min-height: 38px;
    padding: 8px;
    border: 1px solid rgba(255, 255, 255, 0.18);
    border-radius: 9px;
    background: rgba(3, 8, 22, 0.82);
    color: #fff;
    font-size: 12px;
    font-weight: 650;
    box-shadow: 0 5px 18px rgba(0, 0, 0, 0.42);
    pointer-events: auto;
    backdrop-filter: blur(5px);
}

.game-close span { display: none; }

.game-close:active { transform: scale(0.96); }
.game-close .icon { width: 17px; height: 17px; }
.game-frame { display: block; width: 100%; height: 100%; min-height: 0; flex: 1 1 auto; border: 0; background: #050510; }

.modal {
    position: fixed;
    z-index: 50;
    inset: 0;
    display: none;
    align-items: flex-end;
    justify-content: center;
}

.modal.show { display: flex; }

.modal-backdrop {
    position: absolute;
    inset: 0;
    border: 0;
    background: rgba(15, 23, 42, 0.58);
    backdrop-filter: blur(2px);
}

.settings-panel {
    position: relative;
    z-index: 1;
    width: 100%;
    max-width: 460px;
    max-height: calc(100vh - 24px);
    max-height: calc(100dvh - 24px);
    padding: 24px 22px max(24px, env(safe-area-inset-bottom));
    overflow-y: auto;
    border-radius: 20px 20px 0 0;
    background: var(--card-bg);
    box-shadow: 0 -15px 40px rgba(0, 0, 0, 0.2);
    animation: panel-up 0.25s ease both;
}

@keyframes panel-up { from { transform: translateY(30px); opacity: 0; } to { transform: none; opacity: 1; } }

.settings-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    margin-bottom: 12px;
}

.settings-header > div { display: flex; align-items: center; gap: 9px; }
.settings-header h2 { font-size: 18px; }
.settings-title-icon { width: 22px; height: 22px; color: var(--primary-color); fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }

.modal-close {
    display: grid;
    place-items: center;
    width: 38px;
    height: 38px;
    border: 0;
    border-radius: 50%;
    background: var(--ai-section-bg);
    color: var(--text-muted);
}

.modal-close .icon { width: 19px; height: 19px; }
.settings-help { margin-bottom: 20px; color: var(--text-muted); font-size: 12px; line-height: 1.5; }
.secure-hint { display: block; margin-top: 5px; color: var(--text-muted); font-size: 10px; }

.secret-field { position: relative; }
.secret-field .form-control { padding-right: 78px; }
.secret-toggle {
    position: absolute;
    top: 50%;
    right: 8px;
    min-height: 32px;
    padding: 5px 8px;
    transform: translateY(-50%);
    border: 0;
    border-radius: 7px;
    background: var(--ai-section-bg);
    color: var(--primary-color);
    font-size: 11px;
    font-weight: 650;
}

.connection-message {
    display: none;
    margin: 4px 0 14px;
    padding: 10px 12px;
    border-radius: 8px;
    background: var(--ai-section-bg);
    color: var(--text-muted);
    font-size: 12px;
    line-height: 1.4;
}

.connection-message.show { display: block; }
.connection-message.success { background: var(--badge-paid-bg); color: var(--badge-paid-text); }
.connection-message.error { background: var(--danger-bg); color: var(--danger-color); }
.settings-actions { display: flex; gap: 10px; }

.btn-secondary, .btn-save {
    flex: 1;
    min-height: 44px;
    padding: 11px 14px;
    border-radius: 9px;
    font-size: 13px;
    font-weight: 650;
}

.btn-secondary { border: 1px solid var(--border-color); background: transparent; color: var(--text-main); }
.btn-save { border: 1px solid var(--primary-color); background: var(--primary-color); color: #fff; }
.btn-secondary:disabled, .btn-save:disabled { opacity: 0.6; }

.btn-clear-connection {
    width: 100%;
    min-height: 42px;
    margin-top: 12px;
    border: 0;
    border-radius: 9px;
    background: transparent;
    color: var(--danger-color);
    font-size: 12px;
    font-weight: 650;
}

.settings-credits {
    margin-top: 10px;
    padding: 15px 14px;
    border-top: 1px solid var(--border-color);
    color: var(--text-muted);
    text-align: center;
}

.settings-credits span,
.settings-credits strong { display: block; }
.settings-credits span {
    margin-bottom: 5px;
    color: var(--primary-color);
    font-size: 10px;
    font-weight: 800;
    letter-spacing: 0.12em;
    text-transform: uppercase;
}
.settings-credits strong {
    color: var(--text-main);
    font-size: 12px;
    line-height: 1.45;
}

.pwa-install-card {
    display: grid;
    grid-template-columns: 54px 1fr auto;
    align-items: center;
    gap: 12px;
    margin-top: 22px;
    padding: 13px;
    border: 1px solid rgba(37, 99, 235, 0.28);
    border-radius: 15px;
    background: linear-gradient(135deg, rgba(37, 99, 235, 0.1), rgba(14, 165, 233, 0.05));
}

.pwa-install-card[hidden] { display: none; }
.pwa-install-card img { border-radius: 13px; box-shadow: 0 6px 16px rgba(37, 99, 235, 0.2); }
.pwa-install-card strong, .pwa-install-card span { display: block; }
.pwa-install-card strong { margin-bottom: 3px; color: var(--text-main); font-size: 13px; }
.pwa-install-card span { color: var(--text-muted); font-size: 10px; line-height: 1.35; }
.pwa-install-card button {
    min-height: 38px;
    padding: 8px 11px;
    border: 0;
    border-radius: 9px;
    background: var(--primary-color);
    color: #fff;
    font-size: 11px;
    font-weight: 750;
}

.pwa-guide { z-index: 80; }
.pwa-guide-panel {
    position: relative;
    z-index: 1;
    width: min(calc(100% - 24px), 390px);
    margin: auto;
    padding: 24px 20px max(20px, env(safe-area-inset-bottom));
    border-radius: 24px;
    background: var(--card-bg);
    box-shadow: 0 24px 70px rgba(0, 0, 0, 0.32);
    text-align: center;
    animation: panel-up 0.25s ease both;
}

.pwa-guide-icon { border-radius: 19px; box-shadow: 0 12px 28px rgba(37, 99, 235, 0.24); }
.pwa-guide-panel h2 { margin: 13px 0 5px; font-size: 21px; }
.pwa-guide-panel > p { color: var(--text-muted); font-size: 13px; line-height: 1.5; }
.pwa-guide-panel ol { display: grid; gap: 10px; margin: 18px 0; list-style: none; text-align: left; }
.pwa-guide-panel li { display: grid; grid-template-columns: 30px 1fr; align-items: center; gap: 10px; color: var(--text-main); font-size: 12px; line-height: 1.45; }
.pwa-guide-panel li > span { display: grid; width: 30px; height: 30px; place-items: center; border-radius: 50%; background: rgba(37, 99, 235, 0.12); color: var(--primary-color); font-weight: 800; }
.pwa-guide-close { width: 100%; }

.network-status {
    position: fixed;
    z-index: 90;
    top: max(8px, env(safe-area-inset-top));
    left: 50%;
    width: min(calc(100% - 24px), 420px);
    padding: 9px 12px;
    transform: translateX(-50%);
    border: 1px solid rgba(245, 158, 11, 0.55);
    border-radius: 999px;
    background: rgba(30, 25, 12, 0.94);
    color: #fde68a;
    font-size: 10px;
    font-weight: 700;
    text-align: center;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}

.network-status[hidden] { display: none; }

@media (display-mode: standalone) {
    .screen { padding-top: max(10px, env(safe-area-inset-top)); }
}

@media (max-width: 460px) {
    .screen { padding: 0; }
    .app-container { min-height: 100vh; min-height: 100dvh; border-radius: 0; box-shadow: none; padding: max(30px, env(safe-area-inset-top)) 22px max(28px, env(safe-area-inset-bottom)); }
    .theme-toggle, .settings-toggle { top: max(12px, env(safe-area-inset-top)); }
    .pwa-install-card { grid-template-columns: 48px 1fr auto; margin-bottom: 4px; }
    .pwa-install-card img { width: 48px; height: 48px; }
}

@media (max-width: 330px) {
    .app-container { padding-right: 16px; padding-left: 16px; }
    .upload-grid { grid-template-columns: 1fr; }
    .invoice-header { flex-direction: column; }
    .invoice-status { align-items: flex-start; }
    .pwa-install-card { grid-template-columns: 44px 1fr; }
    .pwa-install-card img { width: 44px; height: 44px; }
    .pwa-install-card button { grid-column: 1 / -1; width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after { scroll-behavior: auto !important; animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
