/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
    --primary: #0d6eaa;
    --primary-dark: #084a73;
    --primary-light: #e8f4fc;
    --accent: #10a37f;
    --danger: #d9534f;
    --gray-50: #f8f9fa;
    --gray-100: #e9ecef;
    --gray-200: #dee2e6;
    --gray-600: #6c757d;
    --gray-800: #343a40;
    --radius: 8px;
    --shadow: 0 2px 8px rgba(0,0,0,0.08);
}

body {
    font-family: 'Segoe UI', Arial, sans-serif;
    color: var(--gray-800);
    background: var(--gray-50);
    line-height: 1.6;
}

/* ===== Header ===== */
header {
    background: var(--primary);
    color: #fff;
    padding: 0.6rem 1.5rem;
    box-shadow: var(--shadow);
    position: sticky;
    top: 0;
    z-index: 900;
}
.header-inner {
    max-width: 1100px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.logo { height: 48px; }
nav { display: flex; gap: 0.5rem; }
nav a {
    color: #fff;
    text-decoration: none;
    margin-left: 1rem;
    font-weight: 500;
    opacity: 0.9;
    transition: opacity .2s;
    padding: 0.5rem 0.8rem;
    border-radius: 4px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
}
nav a:hover { opacity: 1; background: rgba(255,255,255,0.15); }

/* ===== Container ===== */
.container {
    max-width: 1100px;
    margin: 2rem auto;
    padding: 0 1.5rem;
}

h1 {
    font-size: 1.6rem;
    margin-bottom: 1.5rem;
    color: var(--primary-dark);
}

/* ===== Card ===== */
.card {
    background: #fff;
    border-radius: var(--radius);
    padding: 1.5rem 2rem;
    margin-bottom: 1.5rem;
    box-shadow: var(--shadow);
}
.card h2 {
    font-size: 1.15rem;
    color: var(--primary);
    margin-bottom: 1rem;
    border-bottom: 2px solid var(--primary-light);
    padding-bottom: 0.5rem;
}

/* ===== Form ===== */
.form-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem 2rem;
}
.form-group { display: flex; flex-direction: column; }
.form-group label { font-weight: 600; font-size: 0.85rem; margin-bottom: 0.3rem; color: var(--gray-600); }
.form-group input,
.form-group select {
    padding: 0.5rem 0.7rem;
    border: 1px solid var(--gray-200);
    border-radius: 4px;
    font-size: 0.95rem;
    transition: border-color .2s;
}
.form-group input:focus,
.form-group select:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(13,110,170,0.12);
}
.radio-group {
    display: flex;
    gap: 1.2rem;
    padding-top: 0.3rem;
}
.radio-group label { font-weight: 400; font-size: 0.95rem; color: var(--gray-800); cursor: pointer; }

/* ===== Add Row ===== */
.add-row {
    display: flex;
    gap: 0.8rem;
    align-items: center;
    margin-bottom: 1rem;
    flex-wrap: wrap;
}
.add-row select,
.add-row input {
    padding: 0.5rem 0.7rem;
    border: 1px solid var(--gray-200);
    border-radius: 4px;
    font-size: 0.95rem;
    flex: 1;
    min-width: 200px;
}

/* ===== Autocomplete ===== */
.autocomplete-wrap {
    position: relative;
    flex: 1;
    min-width: 200px;
}
.autocomplete-wrap input {
    width: 100%;
    padding: 0.5rem 0.7rem;
    border: 1px solid var(--gray-200);
    border-radius: 4px;
    font-size: 0.95rem;
}
.autocomplete-list {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    max-height: 260px;
    overflow-y: auto;
    background: #fff;
    border: 1px solid var(--gray-200);
    border-top: none;
    border-radius: 0 0 4px 4px;
    z-index: 100;
    display: none;
    box-shadow: 0 4px 12px rgba(0,0,0,0.12);
}
.autocomplete-list .ac-item {
    padding: 0.55rem 0.7rem;
    cursor: pointer;
    font-size: 0.93rem;
    min-height: 44px;
    display: flex;
    align-items: center;
}
.autocomplete-list .ac-item:hover,
.autocomplete-list .ac-item.ac-active {
    background: var(--primary-light);
    color: var(--primary-dark);
}
.autocomplete-list .ac-item mark {
    background: #ffe08a;
    padding: 0;
    border-radius: 2px;
}

/* ===== Table ===== */
table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.92rem;
}
thead th {
    background: var(--primary);
    color: #fff;
    padding: 0.65rem 0.8rem;
    text-align: left;
    font-weight: 600;
}
thead th:first-child { border-radius: 6px 0 0 0; }
thead th:last-child { border-radius: 0 6px 0 0; }
tbody td {
    padding: 0.55rem 0.8rem;
    border-bottom: 1px solid var(--gray-100);
}
tbody tr:hover { background: var(--primary-light); }
tbody input {
    width: 110px;
    padding: 0.35rem 0.5rem;
    border: 1px solid var(--gray-200);
    border-radius: 4px;
    font-size: 0.9rem;
    text-align: right;
}
tfoot td {
    padding: 0.7rem 0.8rem;
    font-size: 0.95rem;
}
.totals-row { background: var(--gray-100); font-weight: 600; }
.vm-row {
    background: var(--primary-light);
    font-size: 1.05rem;
}
.vm-row td { color: var(--primary-dark); }

/* ===== Infraestrutura Summary ===== */
.infra-summary {
    margin-top: 1.2rem;
    padding-top: 1rem;
    border-top: 2px solid var(--gray-100);
    display: flex;
    flex-wrap: wrap;
    gap: 1.2rem 2.5rem;
}
.infra-item {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
}
.infra-label { font-size: 0.85rem; color: var(--gray-600); font-weight: 600; }
.infra-value { font-size: 1.1rem; color: var(--primary-dark); font-weight: 700; }
.infra-item small { font-size: 0.78rem; color: var(--gray-600); }
.infra-total .infra-value { color: var(--primary); font-size: 1.2rem; }

/* ===== Grand Total Card ===== */
.grand-total-card { border-left: 4px solid var(--primary); }
.summary-grid { display: flex; flex-direction: column; gap: 0.5rem; }
.summary-line {
    display: flex;
    justify-content: space-between;
    padding: 0.4rem 0;
    font-size: 0.95rem;
    border-bottom: 1px solid var(--gray-100);
}
.summary-line span:last-child { font-weight: 600; }
.summary-total {
    border-bottom: none;
    border-top: 2px solid var(--primary);
    margin-top: 0.4rem;
    padding-top: 0.7rem;
    font-size: 1.15rem;
}
.summary-total span { color: var(--primary-dark); font-weight: 700; }

/* ===== Buttons ===== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.55rem 1.4rem;
    border: none;
    border-radius: 4px;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    text-decoration: none;
    transition: background .2s, transform .1s;
    min-height: 44px;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-secondary { background: var(--gray-200); color: var(--gray-800); }
.btn-secondary:hover { background: var(--gray-100); }
.btn-danger {
    background: transparent;
    color: var(--danger);
    font-size: 1.3rem;
    padding: 0;
    min-width: 44px;
    min-height: 44px;
    border-radius: 50%;
}
.btn-danger:hover { background: #fce4e4; }

.actions {
    display: flex;
    gap: 1rem;
    align-items: center;
    flex-wrap: wrap;
}
.status-msg {
    font-size: 0.9rem;
    margin-left: 1rem;
}
.status-msg.error { color: var(--danger); }
.status-msg.success { color: var(--accent); }

/* ===== Toast ===== */
.toast-container {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 9999;
    display: flex;
    flex-direction: column;
    align-items: center;
    pointer-events: none;
    padding-top: 0.5rem;
}
.toast {
    pointer-events: auto;
    background: var(--gray-800);
    color: #fff;
    padding: 0.8rem 1.5rem;
    border-radius: var(--radius);
    font-size: 0.95rem;
    box-shadow: 0 4px 16px rgba(0,0,0,0.2);
    margin-bottom: 0.5rem;
    animation: toastIn 0.3s ease;
    max-width: 90vw;
    text-align: center;
}
.toast.toast-error { background: var(--danger); }
.toast.toast-success { background: var(--accent); }
.toast.toast-out { animation: toastOut 0.3s ease forwards; }
@keyframes toastIn { from { opacity: 0; transform: translateY(-20px); } to { opacity: 1; transform: translateY(0); } }
@keyframes toastOut { from { opacity: 1; transform: translateY(0); } to { opacity: 0; transform: translateY(-20px); } }

/* ===== Footer ===== */
footer {
    text-align: center;
    padding: 1.5rem;
    font-size: 0.8rem;
    color: var(--gray-600);
}

/* ===== Generate options (format choice) ===== */
.generate-options {
    display: flex;
    gap: 1rem;
    align-items: center;
    flex-wrap: wrap;
}
.format-radio-group {
    display: flex;
    gap: 0.5rem;
    align-items: center;
}
.format-radio-group label {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.6rem 1.2rem;
    border: 2px solid var(--gray-200);
    border-radius: 6px;
    cursor: pointer;
    font-weight: 500;
    font-size: 0.95rem;
    transition: all .2s;
    user-select: none;
    min-height: 48px;
}
.format-radio-group input[type="radio"] {
    accent-color: var(--primary);
    width: 18px;
    height: 18px;
}
.format-radio-group label:has(input:checked) {
    border-color: var(--primary);
    background: var(--primary-light);
    color: var(--primary-dark);
}

/* Table scroll wrapper base */
.table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

/* ===== Page Header ===== */
.page-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
    flex-wrap: wrap;
    gap: 0.5rem;
}
.page-header h1 { margin-bottom: 0; }

.btn-outline {
    background: transparent;
    color: var(--primary);
    border: 2px solid var(--primary);
    padding: 0.5rem 1.2rem;
    font-size: 0.9rem;
    font-weight: 600;
    border-radius: 4px;
    cursor: pointer;
    transition: all .2s;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
}
.btn-outline:hover {
    background: var(--primary);
    color: #fff;
}

.btn-sm { padding: 0.4rem 0.9rem; font-size: 0.88rem; min-height: 40px; }

/* ===== Modal ===== */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
}
.modal-content {
    background: #fff;
    border-radius: var(--radius);
    width: 90%;
    max-width: 900px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 8px 32px rgba(0,0,0,0.2);
}
.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.5rem;
    border-bottom: 2px solid var(--primary-light);
}
.modal-header h2 {
    font-size: 1.15rem;
    color: var(--primary-dark);
    margin: 0;
}
.modal-header .btn-danger {
    font-size: 1.5rem;
    min-width: 44px;
    min-height: 44px;
}
.modal-body {
    padding: 1rem 1.5rem;
    overflow-y: auto;
    flex: 1;
}
.modal-loading, .modal-empty {
    text-align: center;
    padding: 2rem;
    color: var(--gray-600);
    font-size: 0.95rem;
}

/* ===== Cost Items Table ===== */
#cost_table tbody td {
    font-size: 0.88rem;
    vertical-align: middle;
}
.item-hint {
    color: var(--gray-600);
    font-size: 0.78rem;
    font-style: italic;
    font-weight: normal;
}
#cost_table tbody input {
    width: 100px;
    padding: 0.35rem 0.5rem;
    border: 1px solid var(--gray-200);
    border-radius: 4px;
    font-size: 0.88rem;
    text-align: right;
}
#cost_table tbody input[type="number"] {
    width: 70px;
}
#cost_table tfoot td {
    padding: 0.7rem 0.8rem;
    font-size: 0.95rem;
}
.sim-col {
    background: #fffbe6 !important;
    border-left: 2px solid #e6c200;
}
th.sim-col {
    background: #e6c200 !important;
    color: var(--gray-800);
    font-size: 0.82rem;
}
.sim-col input {
    width: 80px;
    padding: 0.35rem 0.5rem;
    border: 1px solid #e6c200;
    border-radius: 4px;
    font-size: 0.88rem;
    text-align: right;
    background: #fff;
}
tr.sim-row td {
    background: #fffbe6 !important;
    border-top: 1px solid #e6c200;
    font-size: 0.88rem;
}

/* =========================================================
   RESPONSIVE — Mobile-first card layout for tables
   ========================================================= */
@media (max-width: 700px) {
    /* Layout */
    .form-grid { grid-template-columns: 1fr; gap: 0.8rem; }
    .header-inner { flex-direction: column; gap: 0.3rem; }
    .logo { height: 36px; }
    nav { gap: 0.3rem; flex-wrap: wrap; justify-content: center; }
    nav a { margin-left: 0; padding: 0.5rem 1rem; font-size: 0.9rem; min-height: 44px; }
    .page-header { flex-direction: column; align-items: stretch; gap: 0.5rem; }
    .page-header h1 { font-size: 1.2rem; text-align: center; }
    .container { padding: 0 0.5rem; margin-top: 0.8rem; }

    /* Card */
    .card { padding: 1rem 0.8rem; }
    .card h2 { font-size: 1.05rem; }

    /* Inputs — bigger touch targets */
    .form-group input,
    .form-group select,
    .autocomplete-wrap input {
        font-size: 16px;
        padding: 0.7rem;
        min-height: 48px;
    }
    .radio-group {
        flex-direction: column;
        gap: 0.5rem;
    }
    .radio-group label {
        font-size: 1rem;
        padding: 0.5rem 0;
        min-height: 44px;
        display: flex;
        align-items: center;
        gap: 0.5rem;
    }
    .radio-group input[type="radio"] {
        width: 20px;
        height: 20px;
    }

    /* Buttons full width */
    .actions {
        flex-direction: column;
        gap: 0.7rem;
    }
    .actions .btn,
    .actions button,
    .actions a.btn {
        width: 100%;
        text-align: center;
        min-height: 48px;
    }
    .btn-outline {
        width: 100%;
        text-align: center;
        min-height: 48px;
        justify-content: center;
    }

    /* Add specialty row */
    .add-row {
        flex-direction: column;
        align-items: stretch;
    }
    .add-row .autocomplete-wrap { min-width: 0; }
    .add-row .btn {
        min-height: 48px;
        justify-content: center;
    }

    /* Autocomplete touch */
    .autocomplete-list {
        max-height: 220px;
        position: fixed;
        left: 0.5rem;
        right: 0.5rem;
        top: auto;
        border: 1px solid var(--gray-200);
        border-radius: var(--radius);
        z-index: 500;
    }
    .autocomplete-list .ac-item {
        padding: 0.75rem 1rem;
        font-size: 1rem;
        min-height: 48px;
        border-bottom: 1px solid var(--gray-100);
    }
    .autocomplete-list .ac-item:last-child { border-bottom: none; }

    /* Generate format */
    .generate-options {
        flex-direction: column;
        align-items: stretch;
    }
    .format-radio-group {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 0.5rem;
    }
    .format-radio-group label {
        min-width: 0;
        justify-content: center;
        min-height: 52px;
        font-size: 1rem;
    }

    /* Summary grid */
    .summary-line {
        flex-direction: column;
        gap: 0.15rem;
        padding: 0.5rem 0;
    }
    .summary-line span:first-child { font-size: 0.85rem; color: var(--gray-600); }
    .summary-line span:last-child { font-size: 1.05rem; }
    .summary-total { font-size: 1rem; }
    .summary-total span:last-child { font-size: 1.2rem; }

    /* Infra summary */
    .infra-summary { flex-direction: column; gap: 0.8rem; }

    /* ---- CARD LAYOUT: Cost Table ---- */
    #cost_table { min-width: 0 !important; }
    #cost_table thead { display: none; }
    #cost_table tbody tr {
        display: block;
        background: #fff;
        border: 1px solid var(--gray-200);
        border-radius: var(--radius);
        padding: 0.8rem;
        margin-bottom: 0.8rem;
        box-shadow: var(--shadow);
    }
    #cost_table tbody tr:hover { background: #fff; }
    #cost_table tbody td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0.4rem 0;
        border-bottom: 1px solid var(--gray-50);
        font-size: 0.9rem;
    }
    #cost_table tbody td:first-child {
        display: block;
        font-weight: 600;
        font-size: 0.95rem;
        color: var(--primary-dark);
        border-bottom: 1px solid var(--gray-100);
        padding-bottom: 0.5rem;
        margin-bottom: 0.3rem;
    }
    #cost_table tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        font-size: 0.8rem;
        color: var(--gray-600);
        flex-shrink: 0;
        margin-right: 0.5rem;
    }
    #cost_table tbody td:first-child::before { content: ""; }
    #cost_table tbody td:last-child { border-bottom: none; }
    #cost_table tbody input {
        width: 100%;
        max-width: 140px;
        min-height: 44px;
        font-size: 16px;
        text-align: right;
    }
    #cost_table tbody input[type="number"] {
        width: 80px;
    }
    #cost_table tfoot tr {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
        align-items: center;
        background: var(--gray-100);
        border-radius: var(--radius);
        padding: 0.8rem;
    }
    #cost_table tfoot td {
        border: none;
        padding: 0.2rem 0;
    }
    #cost_table tfoot td[colspan] {
        width: 100%;
        margin-bottom: 0.3rem;
    }

    /* ---- CARD LAYOUT: Specialties Table ---- */
    #spec_table { min-width: 0 !important; }
    #spec_table thead { display: none; }
    #spec_table tbody tr {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 0.3rem 0.8rem;
        background: #fff;
        border: 1px solid var(--gray-200);
        border-radius: var(--radius);
        padding: 0.8rem;
        margin-bottom: 0.8rem;
        box-shadow: var(--shadow);
        position: relative;
    }
    #spec_table tbody tr:hover { background: #fff; }
    #spec_table tbody td {
        display: flex;
        flex-direction: column;
        padding: 0.2rem 0;
        border-bottom: none;
    }
    #spec_table tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        font-size: 0.75rem;
        color: var(--gray-600);
        margin-bottom: 0.15rem;
        text-transform: uppercase;
        letter-spacing: 0.3px;
    }
    /* Specialty name — full width */
    #spec_table tbody td:first-child {
        grid-column: 1 / -1;
        font-weight: 600;
        font-size: 0.95rem;
        color: var(--primary-dark);
        border-bottom: 1px solid var(--gray-100);
        padding-bottom: 0.5rem;
    }
    #spec_table tbody td:first-child::before { content: ""; }
    /* Remove button — absolute top right */
    #spec_table tbody td:last-child {
        position: absolute;
        top: 0.3rem;
        right: 0.3rem;
        padding: 0;
    }
    #spec_table tbody td:last-child::before { content: ""; }
    /* Sim col inside card */
    #spec_table tbody td.sim-col {
        background: #fffbe6;
        border-radius: 4px;
        padding: 0.3rem;
        border-left: 2px solid #e6c200;
    }
    #spec_table tbody input {
        width: 100%;
        min-height: 44px;
        font-size: 16px;
        text-align: left;
    }
    #spec_table tbody .sim-col input {
        width: 100%;
        min-height: 44px;
        font-size: 16px;
    }

    /* Specialties tfoot — card style */
    #spec_table tfoot tr {
        display: block;
        background: var(--gray-100);
        border-radius: var(--radius);
        padding: 0.8rem;
        margin-bottom: 0.5rem;
    }
    #spec_table tfoot tr.vm-row {
        background: var(--primary-light);
    }
    #spec_table tfoot tr.sim-row {
        background: #fffbe6;
    }
    #spec_table tfoot td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0.25rem 0;
        border: none;
        font-size: 0.88rem;
    }
    #spec_table tfoot td:first-child {
        display: block;
        width: 100%;
        margin-bottom: 0.3rem;
        font-size: 0.85rem;
    }
    #spec_table tfoot td::before {
        content: attr(data-label);
        font-weight: 600;
        font-size: 0.78rem;
        color: var(--gray-600);
        margin-right: 0.5rem;
    }
    #spec_table tfoot td:first-child::before { content: ""; }
    #spec_table tfoot td.sim-col {
        background: #fffbe6;
        border-radius: 4px;
        padding: 0.2rem 0.4rem;
    }
    /* Hide empty tfoot cells */
    #spec_table tfoot td:empty { display: none; }

    /* Table scroll — don't need horizontal scroll anymore */
    .table-scroll {
        overflow-x: visible;
        margin: 0;
        padding: 0;
    }

    /* ---- CARD LAYOUT: Modal Proposals ---- */
    .modal-content { width: 98%; max-height: 95vh; border-radius: var(--radius) var(--radius) 0 0; }
    .modal-header { padding: 0.8rem 1rem; }
    .modal-header h2 { font-size: 1.05rem; }
    .modal-body { padding: 0.5rem; }
    #proposals_table { min-width: 0 !important; }
    #proposals_table thead { display: none; }
    #proposals_table tbody tr {
        display: block;
        background: #fff;
        border: 1px solid var(--gray-200);
        border-radius: var(--radius);
        padding: 0.8rem;
        margin-bottom: 0.8rem;
        box-shadow: var(--shadow);
    }
    #proposals_table tbody td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0.3rem 0;
        border-bottom: 1px solid var(--gray-50);
        font-size: 0.9rem;
    }
    #proposals_table tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        font-size: 0.8rem;
        color: var(--gray-600);
        margin-right: 0.5rem;
    }
    #proposals_table tbody td:last-child {
        border-bottom: none;
        justify-content: center;
        padding-top: 0.5rem;
    }
    #proposals_table tbody td:last-child::before { content: ""; }
    #proposals_table .btn-load-item {
        width: 100%;
        min-height: 44px;
    }
}

/* ===== Very small screens ===== */
@media (max-width: 400px) {
    h1 { font-size: 1.05rem; }
    .card { padding: 0.8rem 0.6rem; }
    .card h2 { font-size: 0.95rem; }
    nav a { padding: 0.4rem 0.6rem; font-size: 0.85rem; }
    .format-radio-group { grid-template-columns: 1fr; }
}
