/**
 * LindonCRM – Responsive styles for mobile and tablet
 * Breakpoints: 576px (sm), 768px (md), 992px (lg)
 */

/* ── Main content (alle Seiten mit Sidebar): oberer Abstand für Hamburger ── */
@media (max-width: 767px) {
    main.main-content,
    .main-content {
        padding: 1rem !important;
        padding-top: 4.5rem !important; /* Platz für fixiertes Hamburger-Menü, damit Titel (z. B. „Kunden“) nicht verdeckt wird */
        width: 100%;
        max-width: 100%;
    }
}

/* ── Page headers (d-flex justify-content-between) ────────────── */
.page-header,
.main-content > .d-flex.justify-content-between.align-items-center.mb-4,
.main-content > .d-flex.justify-content-between.align-items-center {
    flex-wrap: wrap;
    gap: 0.75rem;
}

@media (max-width: 767px) {
    .page-header,
    .main-content > .d-flex.justify-content-between.align-items-center.mb-4,
    .main-content > .d-flex.justify-content-between.align-items-center {
        flex-direction: column;
        align-items: stretch;
    }

    .page-header .btn,
    .main-content > .d-flex.justify-content-between .btn {
        flex: 1;
        min-width: 0;
    }

    /* Button group in header: wrap or scroll */
    .main-content > .d-flex.justify-content-between.align-items-center.mb-4 > div:last-child,
    .main-content > .d-flex.justify-content-between.align-items-center > div:last-child {
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    /* Page title: prevent overflow on small screens */
    .main-content .page-title,
    .main-content h1 {
        font-size: 1.25rem;
        word-break: break-word;
    }
}

/* ── Table toolbar (search, filters, bulk actions) ───────────── */
.table-toolbar .status-filters,
.table-toolbar .toolbar-actions-row {
    flex-shrink: 0;
}

@media (max-width: 767px) {
    .table-toolbar {
        flex-direction: column;
        align-items: stretch;
        gap: 0.75rem;
    }
    .table-toolbar .search-wrapper {
        min-width: 0;
        width: 100%;
    }
    .table-toolbar .toolbar-actions-row {
        margin-left: 0;
        width: 100%;
    }
    .table-toolbar .status-filters {
        width: auto;
    }
    .table-toolbar .bulk-actions {
        margin-left: 0;
        margin-right: 0;
    }
}

/* ── Tables ─────────────────────────────────────────────────── */
@media (max-width: 767px) {
    .table-responsive {
        font-size: 0.875rem;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .table-responsive thead th,
    .table-responsive tbody td {
        padding: 0.5rem 0.375rem;
    }

    .table-responsive .table {
        margin-bottom: 0;
    }

    /* Card body padding on mobile */
    .main-content .card-body {
        padding: 1rem;
    }
}

/* ── List tables: card layout on mobile (invoices, quotes, letters, reminders, Auftragsbestätigungen) ── */
/* Auch .tab-pane .list-table für gleiches Design in Tabs (z. B. Auftragsbestätigungen) */
@media (max-width: 767px) {
    .table-responsive.list-table-wrapper,
    .tab-pane .table-responsive.list-table-wrapper,
    #orderConfirmationsTableWrapper {
        max-width: 100%;
        overflow-x: hidden;
        box-sizing: border-box;
        -webkit-overflow-scrolling: touch;
    }

    .table-responsive.list-table-wrapper > .table.list-table,
    .tab-pane .table-responsive.list-table-wrapper > .table.list-table,
    #orderConfirmationsTableWrapper > .table {
        width: 100%;
        max-width: 100%;
        min-width: 0;
        table-layout: fixed;
    }

    .table-responsive.list-table-wrapper .list-table tbody,
    .tab-pane .table-responsive.list-table-wrapper .list-table tbody {
        display: block;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
    }

    .list-table thead,
    .tab-pane .list-table thead {
        display: none;
    }

    .list-table tbody tr,
    .tab-pane .list-table tbody tr {
        display: block;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        margin-bottom: 0.75rem;
        padding: 0.75rem 1rem;
        background: #fff !important;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
    }

    .list-table tbody tr:last-child,
    .tab-pane .list-table tbody tr:last-child {
        margin-bottom: 0;
    }

    /* Status-Linie auf Karten: Innenbalken (inset), kein Layout-Shift durch border-left */
    .list-table tbody tr.quote-row-status-accepted,
    .tab-pane .list-table tbody tr.quote-row-status-accepted,
    #orderConfirmationsTable tbody tr.quote-row-status-accepted {
        box-shadow:
            0 1px 3px rgba(0, 0, 0, 0.08),
            inset 4px 0 0 #198754;
        border-left: 0 !important;
    }

    /* Empty state row (colspan): no card look */
    .list-table tbody tr:only-child,
    .list-table tbody tr:only-child td,
    .tab-pane .list-table tbody tr:only-child,
    .tab-pane .list-table tbody tr:only-child td {
        background: transparent !important;
        box-shadow: none;
        border: none !important;
    }

    .list-table tbody tr:only-child td,
    .list-table tbody td[colspan],
    .tab-pane .list-table tbody tr:only-child td,
    .tab-pane .list-table tbody td[colspan] {
        display: block !important;
        text-align: center;
        padding: 1rem;
        border: none;
    }

    .list-table tbody td[colspan]::before,
    .tab-pane .list-table tbody td[colspan]::before {
        display: none;
    }

    .list-table tbody td,
    .tab-pane .list-table tbody td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 0.5rem;
        min-width: 0;
        padding: 0.4rem 0;
        border: none;
        border-bottom: 1px solid #f1f3f5;
        text-align: right;
    }

    .list-table tbody td:last-child,
    .tab-pane .list-table tbody td:last-child {
        border-bottom: none;
        padding-top: 0.5rem;
    }

    /* Bezeichnung linksbündig */
    .list-table tbody td::before,
    .tab-pane .list-table tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        color: #6c757d;
        flex-shrink: 0;
        min-width: 6.5em;
        font-size: 0.8125rem;
        text-align: left;
    }

    /* Daten rechtsbündig */
    .list-table tbody td > *,
    .tab-pane .list-table tbody td > * {
        min-width: 0;
        max-width: 100%;
        margin-left: auto;
        text-align: right;
        overflow-wrap: anywhere;
        word-break: break-word;
    }

    /* Mehrzeilige Werte ohne horizontales Ausweichen der Karte */
    .list-table tbody td:not([data-label="Aktionen"]) > *,
    .tab-pane .list-table tbody td:not([data-label="Aktionen"]) > * {
        align-self: flex-end;
        text-align: right;
        flex-shrink: 1;
    }

    .list-table tbody td[data-label="Auswahl"]::before,
    .tab-pane .list-table tbody td[data-label="Auswahl"]::before {
        min-width: 4em;
    }

    /* Actions cell: Label oben links, Aktionen darunter rechts */
    .list-table tbody td[data-label="Aktionen"],
    .tab-pane .list-table tbody td[data-label="Aktionen"] {
        flex-wrap: wrap;
        padding-top: 0.5rem;
    }

    .list-table tbody td[data-label="Aktionen"]::before,
    .tab-pane .list-table tbody td[data-label="Aktionen"]::before {
        width: 100%;
        margin-bottom: 0.35rem;
    }

    .list-table tbody td[data-label="Aktionen"] > *,
    .tab-pane .list-table tbody td[data-label="Aktionen"] > * {
        margin-left: auto;
        text-align: right;
    }

    .list-table .row-actions,
    .tab-pane .list-table .row-actions {
        flex-wrap: wrap;
        gap: 0.35rem;
    }

    /* Auftragsbestätigungen: identisches Card-Design wie Angebote (höhere Spezifität) */
    #orderConfirmationsTable thead { display: none; }
    #orderConfirmationsTable tbody {
        display: block;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
    }
    #orderConfirmationsTable tbody tr {
        display: block;
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        margin-bottom: 0.75rem;
        padding: 0.75rem 1rem;
        background: #fff !important;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
    }
    #orderConfirmationsTable tbody tr:last-child { margin-bottom: 0; }
    #orderConfirmationsTable tbody tr:only-child,
    #orderConfirmationsTable tbody tr:only-child td {
        background: transparent !important;
        box-shadow: none;
        border: none !important;
    }
    #orderConfirmationsTable tbody tr:only-child td,
    #orderConfirmationsTable tbody td[colspan] {
        display: block !important;
        text-align: center;
        padding: 1rem;
        border: none;
    }
    #orderConfirmationsTable tbody td[colspan]::before { display: none; }
    #orderConfirmationsTable tbody td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        gap: 0.5rem;
        min-width: 0;
        padding: 0.4rem 0;
        border: none;
        border-bottom: 1px solid #f1f3f5;
        text-align: right;
    }
    #orderConfirmationsTable tbody td:last-child {
        border-bottom: none;
        padding-top: 0.5rem;
    }
    #orderConfirmationsTable tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        color: #6c757d;
        flex-shrink: 0;
        min-width: 6.5em;
        font-size: 0.8125rem;
        text-align: left;
    }
    #orderConfirmationsTable tbody td > * {
        min-width: 0;
        max-width: 100%;
        margin-left: auto;
        text-align: right;
        overflow-wrap: anywhere;
        word-break: break-word;
    }
    #orderConfirmationsTable tbody td[data-label="Aktionen"] {
        flex-wrap: wrap;
        padding-top: 0.5rem;
    }
    #orderConfirmationsTable tbody td[data-label="Aktionen"]::before {
        width: 100%;
        margin-bottom: 0.35rem;
    }
    #orderConfirmationsTable tbody td[data-label="Aktionen"] > * {
        margin-left: auto;
        text-align: right;
    }
    #orderConfirmationsTable .row-actions {
        flex-wrap: wrap;
        gap: 0.35rem;
    }
}

/* ── Position editor (Angebot/Rechnung create & edit) ───────────── */
.items-section {
    max-width: 100%;
    width: 100%;
    box-sizing: border-box;
}
.items-table-container {
    overflow-x: auto !important;
    overflow-y: visible;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
}
.items-table {
    min-width: 0;
}
@media (max-width: 767px) {
    .items-section {
        padding: 0.75rem 1rem !important;
        margin-left: 0;
        margin-right: 0;
    }
    .items-header {
        flex-direction: column;
        align-items: stretch;
        gap: 0.75rem;
        margin-bottom: 1rem !important;
    }
    .items-actions {
        flex-wrap: wrap;
        justify-content: flex-start;
    }
    .items-table-container {
        margin-left: -0.25rem;
        margin-right: -0.25rem;
        border-radius: 0.375rem;
        overflow-x: visible !important;
    }
    /* Card-Layout: Jede Position als Karte, alles passt rein */
    .items-table thead {
        display: none;
    }
    .items-table tbody tr.item-row {
        display: flex;
        flex-wrap: wrap;
        align-items: flex-start;
        gap: 0.5rem 0;
        padding: 0.75rem 1rem;
        margin-bottom: 0.75rem;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        background: #fff;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
    }
    .items-table tbody tr.item-row:last-of-type {
        margin-bottom: 0;
    }
    .items-table tbody td {
        padding: 0.35rem 0 !important;
        font-size: 0.8125rem;
        border: none !important;
        vertical-align: middle;
    }
    /* Grip + Löschen in einer Zeile oben */
    .items-table tbody td.item-cell-grip {
        order: 0;
        width: 50%;
        display: flex;
        align-items: center;
        padding-left: 0 !important;
    }
    .items-table tbody td.item-cell-actions {
        order: 0;
        width: 50%;
        display: flex;
        justify-content: flex-end;
        align-items: center;
        padding-right: 0 !important;
    }
    /* Beschreibung: volle Breite, Label darüber */
    .items-table tbody td.item-description-cell {
        order: 1;
        width: 100%;
        min-width: 0 !important;
        display: flex;
        flex-direction: column;
        align-items: stretch;
    }
    .items-table tbody td.item-description-cell::before {
        content: attr(data-label);
        font-weight: 600;
        color: #6c757d;
        font-size: 0.75rem;
        margin-bottom: 0.25rem;
    }
    .items-table tbody td.item-description-cell .form-control,
    .items-table tbody td.item-description-cell textarea {
        width: 100% !important;
        min-height: 56px;
        box-sizing: border-box;
    }
    /* Mengen/Preise: Label links, Eingabe rechts, 2 Spalten-Grid */
    .items-table tbody td[data-label="Menge"],
    .items-table tbody td[data-label="Einheit"],
    .items-table tbody td[data-label="Einzelpreis"],
    .items-table tbody td[data-label="Rabatt %"],
    .items-table tbody td[data-label="MwSt %"],
    .items-table tbody td[data-label="Gesamt"] {
        order: 2;
        width: 50%;
        min-width: 0;
        display: flex;
        flex-wrap: wrap;
        align-items: center;
        gap: 0.25rem;
    }
    .items-table tbody td[data-label="Menge"]::before,
    .items-table tbody td[data-label="Einheit"]::before,
    .items-table tbody td[data-label="Einzelpreis"]::before,
    .items-table tbody td[data-label="Rabatt %"]::before,
    .items-table tbody td[data-label="MwSt %"]::before,
    .items-table tbody td[data-label="Gesamt"]::before {
        content: attr(data-label);
        font-weight: 600;
        color: #6c757d;
        font-size: 0.75rem;
        width: 100%;
    }
    .items-table tbody td[data-label="Menge"] input,
    .items-table tbody td[data-label="Einheit"] select,
    .items-table tbody td[data-label="Einzelpreis"] input,
    .items-table tbody td[data-label="Rabatt %"] input,
    .items-table tbody td[data-label="MwSt %"] input,
    .items-table tbody td[data-label="Gesamt"] input {
        width: 100% !important;
        max-width: 100%;
        min-width: 0;
        box-sizing: border-box;
    }
    .items-table tbody td.item-cell-grip::before,
    .items-table tbody td.item-cell-actions::before {
        display: none;
    }
    .items-summary {
        margin-top: 0.75rem;
    }
    .invoice-actions {
        margin-left: 0 !important;
        margin-right: 0 !important;
        padding-left: 1rem !important;
        padding-right: 1rem !important;
    }
}

/* ── E-Mail: Nachricht & Anhänge – KI-Toolbar (eine Zeile) ───────── */
.card-message-attachments,
.card-message-attachments .card-body-message-attachments {
    overflow-x: visible !important;
}
.email-ai-toolbar {
    max-width: 100%;
    flex-wrap: nowrap;
    padding-right: 0.5rem;
    min-width: 0;
}
.email-ai-toolbar .email-ai-purpose-select {
    flex-shrink: 0;
    margin-right: 0.25rem;
}
@media (max-width: 767px) {
    .email-ai-toolbar {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 2px;
        padding-right: 1rem;
    }
}

/* ── Touch targets (min 44px for accessibility) ───────────────── */
@media (max-width: 767px) {
    .row-actions .btn-icon {
        width: 44px;
        min-width: 44px;
        height: 44px;
        min-height: 44px;
    }
}

/* ── Forms: spacing on small screens ─────────────────────────── */
@media (max-width: 767px) {
    .form-row .col-md-6,
    .row .col-md-6 {
        margin-bottom: 0.5rem;
    }
}

/* ── Login / Install / standalone pages ───────────────────────── */
@media (max-width: 767px) {
    .container .card-body,
    .install-container {
        padding-left: 1rem;
        padding-right: 1rem;
    }
}

/* ── List tables: konsistenter Zeilen-Hover ──────────────────────────────────
 * Checkbox-Spalte display:none bleibt im DOM; Bootstrap --bs-table-bg auf td + niedrigere
 * Spezifität der Klassen-Regel ⇒ erste sichtbare Zelle oft weiß. ID-hochverankerte Regeln
 * setzen Hover auf alle td. Serien-/Storno-/Gutschrift-Zeilen: invoice-only :not(...)
 * oder gleiche Hover-Farbe über spezielle #invoicesTable-Regeln in invoices.php. */
#invoicesTable.table.table-hover.list-table > tbody > tr:not(.list-month-header):not(.invoice-series-group-toggle):not(.invoice-cancellation-group-toggle):not(.invoice-credit-group-toggle):not(.invoice-series-child):not(.invoice-cancellation-child):not(.invoice-credit-child):hover > td,
#quotesTable.table.table-hover.list-table > tbody > tr:not(.list-month-header):hover > td,
.tab-pane .table.table-hover.list-table > tbody > tr:not(.list-month-header):hover > td,
#lettersTable.table.table-hover.list-table > tbody > tr:not(.list-month-header):hover > td,
#remindersTable.table.table-hover.list-table > tbody > tr:not(.list-month-header):hover > td,
#orderConfirmationsTable.table.table-hover.list-table > tbody > tr:not(.list-month-header):hover > td {
    background-color: var(--bs-table-hover-bg, #e9ecef) !important;
}

.table.table-hover.list-table > tbody > tr:not(.list-month-header):not(.invoice-series-group-toggle):not(.invoice-cancellation-group-toggle):not(.invoice-credit-group-toggle):not(.invoice-series-child):not(.invoice-cancellation-child):not(.invoice-credit-child):hover > td {
    background-color: var(--bs-table-hover-bg, #e9ecef) !important;
}
