/* data_engine: Tabellen als Karten auf kleinen Bildschirmen (< 640px).
   Opt-in: nur Tabellen mit der Klasse .engine-mobile-cards (gesetzt ueber mobile_cards=True in der Table-Config bzw. "mobile_cards": True in einer Table-Card).
   Je Spalte (Spalten-Dict) optional:  "mobile_hide": True  (auf dem Handy ausblenden)   "mobile_full": True  (ueber die ganze Kartenbreite)
   Die erste Spalte ist der Kartentitel. Desktop und alle anderen Tabellen bleiben unveraendert. */
@media (max-width: 639px) {
  .engine-mobile-cards, .engine-mobile-cards tbody, .engine-mobile-cards tr, .engine-mobile-cards td { display: block; width: 100%; box-sizing: border-box; }
  .engine-mobile-cards { border-collapse: separate; }
  .engine-mobile-cards thead { display: none; }
  .engine-mobile-cards tbody { padding: 8px; }
  .engine-mobile-cards tr {
    height: auto !important; margin: 0 0 10px; padding: 10px 12px;
    background: var(--brand-surface, #fff); border: 1px solid var(--brand-border, #e2e8f0); border-radius: 14px;
    box-shadow: 0 1px 2px rgba(15, 23, 42, .04);
    display: grid; grid-template-columns: 1fr 1fr; column-gap: 12px; row-gap: 8px; align-items: end;
  }
  .engine-mobile-cards td { padding: 0 !important; border: 0 !important; text-align: left !important; min-width: 0; white-space: normal !important; overflow-wrap: anywhere; }
  .engine-mobile-cards td[data-label]::before {
    content: attr(data-label); display: block; margin-bottom: 2px;
    font-size: .65rem; font-weight: 800; letter-spacing: .06em; text-transform: uppercase; color: var(--brand-text-muted, #64748b);
  }
  /* Titel (erste Spalte), "mobile_full" und Aktionen: ganze Breite */
  .engine-mobile-cards td:first-child, .engine-mobile-cards td.mc-full, .engine-mobile-cards td.mc-actions { grid-column: 1 / -1; }
  .engine-mobile-cards td:first-child { font-weight: 700; font-size: 1rem; }
  .engine-mobile-cards td:first-child::before { display: none; }
  .engine-mobile-cards td.mc-hide { display: none; }
  .engine-mobile-cards td.mc-actions { padding-top: 6px !important; border-top: 1px solid var(--brand-border, #e2e8f0) !important; width: 100%; }
  /* Eingabefelder: gross genug fuer den Daumen, 16px verhindert das Zoomen auf iOS */
  .engine-mobile-cards input[type=number], .engine-mobile-cards input[type=text], .engine-mobile-cards select {
    width: 100% !important; min-height: 44px; font-size: 16px; text-align: right;
  }
  .engine-mobile-cards td > div.flex { justify-content: flex-start !important; }
  .engine-mobile-cards td.text-right, .engine-mobile-cards td.text-center { text-align: left !important; }
  /* Summenzeile / Fusszeile */
  .engine-mobile-cards tbody.mc-footer tr { background: var(--brand-light, #f1f5f9); }
  /* Tabellenkopf-Rahmen darf mitwachsen */
  .engine-table-card .overflow-x-auto:has(.engine-mobile-cards) { overflow-x: visible; }
}

/* ---- Weitere Mobil-Regeln (global, alle Identities) ---- */
@media (max-width: 639px) {
  /* Tabellenrahmen der Gutachten-Tabellen darf bei Kartenansicht nicht scrollen/umrahmen */
  .ga-table-wrap:has(.engine-mobile-cards) { overflow-x: visible; border: 0; background: none; }
  .ga-table.engine-mobile-cards tbody tr:hover { background: var(--brand-surface, #fff); }
  /* Cookie-Hinweis kompakt: Tabelle ausblenden, Text gekuerzt scrollbar */
  .ck-box { max-height: 60vh; overflow-y: auto; }
  .ck-box table { display: none; }
}
@media (max-width: 480px) {
  /* Kopfzeile: Abstand der Symbole verringern und Slogan ausblenden, damit nichts ueber den Rand ragt */
  header .space-x-4 > :not([hidden]) ~ :not([hidden]), nav .space-x-4 > :not([hidden]) ~ :not([hidden]) { margin-left: .4rem !important; margin-right: 0 !important; }
  .cx-claim { display: none; }
}

/* ===== Mail-Hub mobil: zwei Ebenen (Liste -> Nachricht) =====
   Handy: erst die Liste in voller Breite; Tippen auf eine Mail zeigt die Nachricht im Vollbild mit "‹ Liste";
   die Ordner (Sidebar) kommen per Knopf "☰ Ordner" als Überlagerung. Steuerung: static/js/mail_mobile.js */
@media (max-width: 768px) {
  #mail-hub-main { height: calc(100vh - 150px); height: calc(100dvh - 150px); min-height: 420px; }
  #mail-hub-main .mail-workspace { position: relative; flex-direction: column !important; }
  #mail-hub-main #global-mail-toolbar { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  #mail-hub-main #mail-list { width: 100% !important; height: 100% !important; flex: 1 1 auto !important; border-right: 0 !important; }
  #mail-hub-main #email-preview { display: none !important; width: 100% !important; height: 100% !important; flex: 1 1 auto !important; }
  #mail-hub-main.mh-detail #mail-list { display: none !important; }
  #mail-hub-main.mh-detail #email-preview { display: flex !important; }
  #mail-hub-main #resizer-1, #mail-hub-main #resizer-2 { display: none !important; }
  #mail-hub-main #sidebar { display: none !important; }
  #mail-hub-main.mh-ordner #sidebar { display: flex !important; position: absolute; inset: 0; z-index: 40; width: 100% !important; max-width: none; }
  .mh-bar { display: flex; gap: 8px; align-items: center; padding: 8px 12px; border-bottom: 1px solid var(--brand-border); background:var(--brand-surface); position: sticky; top: 0; z-index: 25; }
  .mh-bar button { min-height: 44px; padding: 0 16px; border-radius: 12px; border: 1px solid var(--brand-border); background:color-mix(in srgb, var(--brand-text) 7%, var(--brand-surface)); font-weight: 700; font-size: .95rem; }
  .mh-zu { display: block; }
  /* Ribbon-Leiste: auf dem Handy ein Blatt von unten (über "Mehr"), keine scrollende Zeile mehr */
  #mail-hub-main #global-mail-toolbar { display: none !important; }
  #mail-hub-main #global-mail-toolbar.mh-sheet-offen { display: block !important; position: fixed; left: 0; right: 0; bottom: 64px; z-index: 70; max-height: 55vh; overflow-y: auto;
    border-radius: 18px 18px 0 0; box-shadow: 0 -10px 30px rgba(0,0,0,.25); background: var(--brand-surface); }
  #mail-hub-main #global-mail-toolbar.mh-sheet-offen #toolbar-action-row { flex-wrap: wrap; overflow: visible; }
  #mail-hub-main #global-mail-toolbar.mh-sheet-offen .mail-tab-panel { flex-wrap: wrap; row-gap: .4rem; }
  .mh-aktion { display: flex; position: fixed; left: 0; right: 0; bottom: 0; z-index: 80; gap: 6px; padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
    background: var(--brand-surface); border-top: 1px solid var(--brand-border); box-shadow: 0 -4px 16px rgba(0,0,0,.12); }
  .mh-aktion button { flex: 1 1 0; min-height: 48px; border-radius: 12px; border: 1px solid var(--brand-border); font-weight: 700; font-size: .8rem; color: var(--brand-text);
    background: color-mix(in srgb, var(--brand-text) 7%, var(--brand-surface)); }
  .mh-aktion button.mh-prim { background: var(--brand-600); color: #fff; border-color: transparent; flex: 1.4 1 0; }
  .mh-aktion button[data-a="antw"], .mh-aktion button[data-a="del"], .mh-aktion button[data-a="arch"] { display: none; }
  .mh-aktion.mh-mit-mail button[data-a="antw"], .mh-aktion.mh-mit-mail button[data-a="del"], .mh-aktion.mh-mit-mail button[data-a="arch"] { display: block; }
  #mail-hub-main { padding-bottom: 72px; }
  #email-list-container .mail-list__row { min-height: 64px; padding-top: 10px; padding-bottom: 10px; }
}
@media (min-width: 769px) { .mh-bar, .mh-aktion { display: none !important; } }
/* Web Mail mobil: "Neu verfassen"/Antworten - Formular scrollt, Felder in Daumengroesse, Schreibflaeche mit fester Mindesthoehe
   (sonst bleibt neben Ribbon, Von/An/Cc/Betreff und TinyMCE-Leiste kaum Platz zum Schreiben). 16px gegen iOS-Zoom. */
@media (max-width: 768px) {
  #email-preview .mh-compose { padding: 12px !important; overflow-y: auto !important; -webkit-overflow-scrolling: touch; height: auto !important; min-height: 100%; }
  .mh-compose .mh-compose-actions { flex-wrap: wrap; gap: 6px; }
  .mh-compose > div:first-child { padding-bottom: 8px; margin-bottom: 8px; gap: 8px; }
  .mh-compose #email-compose-form { flex: none !important; min-height: 0; }
  .mh-compose #email-compose-form input[type=text], .mh-compose #email-compose-form select { font-size: 16px !important; min-height: 44px !important; }
  .mh-compose #email-compose-form .w-14 { width: 4.2rem; }
  .mh-compose .mh-editor-wrap { height: 55dvh; min-height: 300px; flex: none !important; }
  .mh-compose .tox-tinymce { height: 100% !important; }
  .mh-compose .tox .tox-toolbar__group { padding: 0 4px; }
}


/* ===== Tabellen-Toolbar mobil (Opt-in: toolbar_mobile=True in der Table-Config) =====
   Kopf: Icon-Kachel + Titel + Treffer-Pille in einer Zeile. Darunter: Neu-Button volle Breite,
   Suche volle Breite, Filter nebeneinander. Alle Bedienelemente >= 44px (Daumen), 16px gegen iOS-Zoom. */
@media (max-width: 639px) {
  .engine-toolbar-m { padding: 14px 14px 16px !important; gap: 14px !important; align-items: stretch !important; }
  .engine-toolbar-m .engine-toolbar-title { font-size: 1.2rem; line-height: 1.2; gap: 10px; flex-wrap: nowrap; font-family: inherit; }
  .engine-toolbar-m .engine-toolbar-title > i, .engine-toolbar-m .engine-toolbar-title > svg {
    box-sizing: content-box; flex: none; padding: 9px; width: 20px; height: 20px; border-radius: 12px;
    background: color-mix(in srgb, var(--brand-primary, #b45309) 12%, transparent);
  }
  .engine-toolbar-m .engine-toolbar-title > span {
    margin-left: auto; flex: none; border-radius: 999px; padding: 5px 11px; font-size: .7rem; white-space: nowrap;
  }
  .engine-toolbar-m form { display: grid !important; grid-template-columns: 1fr 1fr; gap: 10px !important; width: 100%; }
  .engine-toolbar-m .engine-tb-create, .engine-toolbar-m .engine-tb-search { grid-column: 1 / -1; }
  .engine-toolbar-m .engine-tb-create a {
    display: flex; align-items: center; justify-content: center; width: 100%; min-height: 48px; border-radius: 14px; font-weight: 800;
  }
  .engine-toolbar-m .engine-tb-search input { width: 100% !important; min-height: 46px; font-size: 16px; border-radius: 14px; }
  .engine-toolbar-m .engine-tb-filters { display: contents; }
  .engine-toolbar-m .engine-tb-filters select {
    width: 100%; min-width: 0; min-height: 46px; font-size: 14px; border-radius: 14px; padding: 0 12px;
    text-overflow: ellipsis;
  }
  /* ein einzelner Filter (kein Kundenfilter) nimmt die ganze Breite */
  .engine-toolbar-m .engine-tb-filters select:only-of-type { grid-column: 1 / -1; }
  /* Paginierungsleiste kompakt und mittig */
  .engine-table-card > div:last-child { padding: 12px !important; justify-content: center; text-align: center; }
}

/* ===== ERP-Positionen mobil (alle Identities; greift, weil die Positionstabelle "mobile_cards": True hat) =====
   Karte je Position: Kopf (Pos-Nr. links, Loeschen rechts) / Bezeichnung+Artikel ganze Breite / Menge | Einheit / Einzelpreis | Rabatt / MwSt. | Netto.
   Das Ziehen zum Umsortieren (Griff) ist auf dem Handy ausgeblendet - Reihenfolge am Desktop/Tablet aendern. */
@media (max-width: 639px) {
  #item-table .engine-mobile-cards tbody { padding: 10px; }
  #item-table .engine-mobile-cards tr { padding: 12px 14px; row-gap: 12px; }
  #item-table .engine-mobile-cards td { order: 50; }
  #item-table .engine-mobile-cards td.mc-pos { order: 1; grid-column: 1; align-self: center; font-size: .8rem; font-weight: 800; color: var(--brand-text-muted, #64748b); }
  #item-table .engine-mobile-cards td.mc-pos::before { display: inline; margin: 0 4px 0 0; }
  #item-table .engine-mobile-cards td.mc-del { order: 2; grid-column: 2; justify-self: end; width: auto; border-top: 0 !important; padding-top: 0 !important; }
  #item-table .engine-mobile-cards td.mc-del button { min-width: 44px; min-height: 44px; }
  #item-table .engine-mobile-cards td.mc-full { order: 3; }
  #item-table .engine-mobile-cards td.mc-qty { order: 4; }
  #item-table .engine-mobile-cards td.mc-unit { order: 5; }
  #item-table .engine-mobile-cards td.mc-price { order: 6; }
  #item-table .engine-mobile-cards td.mc-disc { order: 7; }
  #item-table .engine-mobile-cards td.mc-tax { order: 8; }
  #item-table .engine-mobile-cards td.mc-net { order: 9; text-align: right !important; font-size: 1.15rem; font-weight: 800; align-self: end; }
  #item-table .engine-mobile-cards td.mc-net::before { text-align: right; font-weight: 800; }
  #item-table .engine-mobile-cards td.mc-full > div { min-width: 0 !important; }
  #item-table .engine-mobile-cards td.mc-full select { text-align: left; min-height: 44px; font-size: 16px; }
  #item-table .engine-mobile-cards td.mc-full button { min-width: 44px; min-height: 44px; }
  #item-table .engine-mobile-cards td.mc-full textarea { font-size: 16px; min-height: 84px; }
  #item-table .engine-mobile-cards td.mc-tax select, #item-table .engine-mobile-cards td.mc-unit select { max-width: none !important; border: 1px solid var(--brand-border, #cbd5e1); border-radius: 8px; background: var(--brand-surface, #fff); }
  #item-table .engine-mobile-cards td.mc-disc > div { flex-wrap: nowrap; gap: 6px; }
  #item-table .engine-mobile-cards td.mc-disc input { flex: 1 1 auto; }
  #items-table-wrapper button[hx-post*="add_item"] { min-height: 52px; }
}


/* Tabellen-Kopf und Dashboard-Ueberschriften auf dem Handy: zentriert, passend groß (die Mandanten-Textstile setzen sonst riesige h1/h2),
   "Treffer"-Marke bleibt sichtbar und bricht in die naechste Zeile statt abgeschnitten zu werden. */
@media (max-width: 768px) {
  .engine-table-card h2.engine-toolbar-title {
    font-size: 1.15rem !important; line-height: 1.3 !important; width: 100%;
    display: flex; flex-wrap: wrap; justify-content: center; align-items: center; text-align: center; gap: 6px 10px;
  }
  .engine-table-card h2.engine-toolbar-title > span { flex: 0 0 auto; white-space: nowrap; }
  .engine-table-card > div:first-child { align-items: center !important; padding: 16px !important; }
  .engine-table-card form[id$="-filter-form"] { flex-wrap: wrap; justify-content: center; width: 100%; }
  .engine-table-card .engine-tb-create, .engine-table-card .engine-tb-search { flex: 1 1 auto; }
  .engine-table-card .engine-tb-search input { width: 100% !important; }
  h1.dash-title { font-size: 1.35rem !important; line-height: 1.3 !important; justify-content: center; text-align: center; }
  h3.dash-sec-title { font-size: .72rem !important; line-height: 1.3 !important; width: 100%; text-align: center; letter-spacing: .08em; }
  .dash-sec-head { justify-content: center !important; padding: 10px 12px !important; }
}
@media (max-width: 768px) {
  .engine-table-card .engine-tb-create { text-align: center; }
  .engine-table-card .engine-tb-create a { display: inline-flex; align-items: center; justify-content: center; }
}
