/* Generic mobile list component. Used for any narrow-viewport view that
   wants a stack of card-style rows: title block (+ optional subtitle),
   right-aligned tabular-num stats, optional chevron. Item types:
   `.item.aggregate` (flat header), `.item[data-depth="0"]` (dark root
   card), `.item.drillable` (tappable card pill), `.item.sub` (indented
   inline-revealed row). Depth-based colour gradient via [data-depth].
   Optional `.view-toggle` for two-section client-side switching (e.g.
   Monat / Geschäftsjahr in BWA). The expand/collapse animation hooks
   live in components/tree_table.css and the tree-table Stimulus
   controller — opt in by setting data-tree-* attributes. */

.mobile-list {
  --m-pad: 0.75rem;
  --m-gap: 0.5rem;
  --m-radius: var(--app-radius-card);
  --m-fg-muted: var(--app-fg-muted);
  --m-divider: var(--app-separator);
  --tree-guide-width: 0.8125rem;
  --tree-guide-color: var(--app-accent-yellow);
  --tree-guide-radius: 5px;
  --guide-thickness: 1px;
  /* ---------- Toggle Monat / Geschäftsjahr ---------- */

  .view-toggle {
    margin: var(--m-gap) 0 var(--m-pad);
  }

  /* ---------- Common item layout ---------- */

  .item {
    --item-bg: transparent;
    display: flex;
    align-items: stretch;
    gap: 0.1rem;
    padding: 0.55rem var(--m-pad);
    padding-left: 4px;
    text-decoration: none;
    color: var(--app-fg);
    background-color: var(--item-bg);

    .title-block {
      flex: 1 1 auto;
      min-width: 0;
      align-self: flex-start;

      .title {
        margin-left: 2px;
        font-weight: 600;
        display: block;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }

    .stats {
      align-self: flex-start;
      flex: 0 0 auto;
      display: flex;
      flex-direction: column;
      align-items: flex-end;
      gap: 0.05rem;
    }

    .primary,
    .delta {
      display: flex;
      align-items: baseline;
      gap: 0.4rem;
    }

    .delta,
    .secondary {
      font-size: var(--app-text-xs);
      color: var(--m-fg-muted);
    }

    .secondary {
      display: flex;
      align-items: baseline;
      gap: 0.4rem;
    }

    .amount {
      font-variant-numeric: tabular-nums;
      font-weight: 600;
    }

    .delta-amount {
      font-variant-numeric: tabular-nums;
    }

    .subtitle {
      display: block;
      font-size: var(--app-text-xs);
      color: var(--m-fg-muted);
      font-weight: 400;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }

    .stats .period-label {
      display: block;
      font-size: var(--app-text-xs);
      color: var(--m-fg-muted);
      font-weight: 400;
      line-height: 1;
    }

    .chev {
      flex: 0 0 auto;
      align-self: center;
      color: var(--app-accent-yellow);
      font-size: 0.95rem;
    }
  }

  /* ---------- Aggregates: flat headers, no card chrome ---------- */

  .item.aggregate {
    cursor: default;
  }

  .item.aggregate[data-action] {
    cursor: pointer;
  }

  .item[data-depth="0"] .title {
    margin-left: 8px;
  }

  .item.aggregate[data-depth="1"] .title {
    font-size: 1rem;
  }

  .item.aggregate[data-depth="2"] .title {
    font-size: 0.95rem;
  }

  /* Adjacent aggregates: rely on each row's own margin for spacing. */

  /* Root row of each tree (Vorläufiges Ergebnis, Liquidität). */
  .item[data-depth="0"] {
    --item-bg: var(--hc-ink-600);
    background-color: var(--item-bg);
    color: var(--app-fg-on-inverse);
    border-radius: var(--m-radius);
    margin: 0.75rem 0 0.5rem;
    border: none;

    .delta {
      color: color-mix(in srgb, white 80%, transparent);
    }

    .subtitle,
    .stats .period-label {
      color: color-mix(in srgb, white 75%, transparent);
    }
  }

  .item[data-depth="0"]:first-child {
    margin-top: 0;
  }

  /* ---------- Top-level drillable: card pill ---------- */

  .item.drillable {
    --item-bg: var(--app-bg-muted);
    background-color: var(--item-bg);
    border-radius: var(--m-radius);
    margin: 0.4rem 0;
    cursor: pointer;
  }

  @media (hover: hover) and (pointer: fine) {
    .item.drillable:hover {
      background-color: color-mix(in srgb, var(--app-fg) 5%, var(--item-bg));
      /* The whole card is the click target; the global a:hover underline
         would wrongly mark the inner text as a link. */
      text-decoration: none;
    }
  }

  /* When expanded, the chevron below a drillable card swings down so the
     opened branch reads as connected. */
  .item.drillable[data-tree-children-are-visible="true"] {
    margin-bottom: 0;
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 0;
  }

  /* ---------- Sub-row: revealed by inline expansion ---------- */

  .item.sub {
    --item-bg: var(--app-bg-muted);
    background-color: var(--item-bg);

    .title {
      font-weight: 500;
      font-size: 0.95rem;
    }

  }

  a.item.sub,
  .item.sub[data-action] {
    cursor: pointer;
  }

  /* Last sub-row of a branch closes the bottom corners. */
  .item.sub:not(:has(+ .item.sub)) {
    border-bottom-left-radius: var(--m-radius);
    border-bottom-right-radius: var(--m-radius);
    margin-bottom: 0.4rem;
  }

  /* When followed by a non-sub row (next drillable card or section), close it. */
  .item.sub+.item:not(.sub) {
    margin-top: 0;
  }

  /* ---------- Depth-based colour gradient (mirrors desktop bwa_tree.css) ----------
     Placed after the type-specific (.aggregate / .drillable / .sub) rules so the
     depth-keyed selectors win on source order at matching specificity
     (e.g. .item:not(.aggregate)[data-depth="1"] vs a.item.drillable). */

  /* depth 1 — ink-500 (dark, white text) */
  .item.aggregate[data-depth="1"],
  .item:not(.aggregate)[data-depth="1"] {
    --item-bg: var(--hc-ink-500);
    background-color: var(--item-bg);
    color: var(--app-fg-on-inverse);
  }

  .item.aggregate[data-depth="1"] {
    border-radius: var(--m-radius);
    margin: 0.35rem 0;
    padding-top: 0.7rem;
    padding-bottom: 0.7rem;
  }

  .item[data-depth="1"] .delta {
    color: color-mix(in srgb, white 80%, transparent);
  }

  /* depth 2 — ink-300 (light, dark text) */
  .item.aggregate[data-depth="2"],
  .item:not(.aggregate)[data-depth="2"] {
    --item-bg: var(--hc-ink-300);
    background-color: var(--item-bg);
  }

  .item.aggregate[data-depth="2"] {
    border-radius: var(--m-radius);
    margin: 0.3rem 0;
  }

  /* depth 3 — sage-400 */
  .item.aggregate[data-depth="3"],
  .item:not(.aggregate)[data-depth="3"] {
    --item-bg: var(--hc-sage-400);
    background-color: var(--item-bg);
  }

  .item.aggregate[data-depth="3"] {
    border-radius: var(--m-radius);
    margin: 0.3rem 0;
  }

  /* depth 4 — sage-300 */
  .item.aggregate[data-depth="4"],
  .item:not(.aggregate)[data-depth="4"] {
    --item-bg: var(--hc-sage-300);
    background-color: var(--item-bg);
  }

  .item.aggregate[data-depth="1"],
  .item:not(.aggregate)[data-depth="1"],
  .item.aggregate[data-depth="2"],
  .item:not(.aggregate)[data-depth="2"],
  .item.aggregate[data-depth="3"],
  .item:not(.aggregate)[data-depth="3"],
  .item.aggregate[data-depth="4"],
  .item:not(.aggregate)[data-depth="4"] {
    border-color: transparent;
  }

  .delta-pill {
    padding: 0.05rem 0.45rem;
  }

  .item[data-depth="0"] .delta-pill,
  .item[data-depth="1"] .delta-pill {
    color: var(--app-fg-on-inverse);
    background-color: color-mix(in srgb, white 18%, transparent);
    border-color: transparent;
  }

  /* ---------- BWA two-column row layout (income statement) ----------
     Each period block (.stats.bwa-row-period) replaces the old
     title-block + stats pair: name + comparison on the left, current
     value + delta row on the right. */

  /* These flat report rows carry no tree guides, so drop the guide-reserved
     4px left inset for symmetric horizontal padding and give them a bit more
     vertical room than the guide-aligned tree rows. */
  .item:has(> .stats.bwa-row-period) {
    padding: 0.75rem var(--m-pad);
  }

  .item .stats.bwa-row-period {
    flex: 1;
    /* min-width:0 lets the row shrink below its content so .bwa-name can
       ellipsis instead of forcing the whole page to scroll horizontally. */
    min-width: 0;
    align-self: stretch;
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 12px;
  }

  .bwa-left {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 3px;
  }

  .bwa-name {
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .bwa-prev {
    font-size: var(--app-text-xs);
    color: var(--m-fg-muted);
    font-variant-numeric: tabular-nums;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .bwa-right {
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 4px;
  }

  .bwa-val {
    font-weight: 700;
    font-size: 1.0625rem;
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.01em;
    white-space: nowrap;
  }

  .bwa-delta {
    display: flex;
    align-items: center;
    gap: 5px;
  }

  .bwa-delta-abs {
    font-size: var(--app-text-xs);
    font-variant-numeric: tabular-nums;
    color: var(--m-fg-muted);
    font-weight: 500;
    white-space: nowrap;
  }

  /* Sum rows: stronger typography */
  .item.aggregate .bwa-name {
    font-weight: 700;
  }

  .item.aggregate .bwa-val {
    font-size: 1.125rem;
    font-weight: 800;
    letter-spacing: -0.015em;
  }

  /* Dark-background rows (depth 0–1): adapt muted text to stay legible */
  .item[data-depth="0"] .bwa-prev,
  .item[data-depth="0"] .bwa-delta-abs,
  .item[data-depth="1"] .bwa-prev,
  .item[data-depth="1"] .bwa-delta-abs {
    color: color-mix(in srgb, white 65%, transparent);
  }

  /* Whole-row press feedback on touch (links + expand rows). The darken is
     instant on press and while the next page loads (.is-navigating), and only
     eases back out on release — so a quick tap still reads as registered. */
  .item:is(a, [data-action]) {
    -webkit-tap-highlight-color: transparent;
    transition: background-color 0.25s ease-out;
  }

  .item:is(a, [data-action]):active,
  .item:is(a, [data-action]).is-navigating {
    transition: none;
    background-color: color-mix(in srgb, var(--app-fg) 10%, var(--item-bg));
  }

  .item[data-depth="0"]:is(a, [data-action]):active,
  .item[data-depth="0"]:is(a, [data-action]).is-navigating,
  .item[data-depth="1"]:is(a, [data-action]):active,
  .item[data-depth="1"]:is(a, [data-action]).is-navigating {
    background-color: color-mix(in srgb, white 14%, var(--item-bg));
  }

  /* ---------- Tree guide lines (mirrors bwa_tree.css) ----------
     .guides is a direct flex child of .item so it stretches to the
     full row height, keeping vertical lines aligned across depths. */

  .item>.guides {
    display: flex;
    align-self: stretch;
    flex: 0 0 auto;
  }

  .item>.guides .guide {
    display: inline-block;
    position: relative;
    width: var(--tree-guide-width);
    flex: 0 0 var(--tree-guide-width);
    margin-top: -20px;
    margin-bottom: -20px;
  }

  .item>.guides .guide-through::before,
  .item>.guides .guide-elbow::before {
    content: "";
    position: absolute;
    background: var(--tree-guide-color);
    width: var(--guide-thickness);
    top: 0;
    bottom: 0;
    left: 50%;
  }

  .item>.guides .guide-elbow::after {
    content: "";
    position: absolute;
    background: var(--tree-guide-color);
    height: var(--guide-thickness);
    top: 50%;
    left: 50%;
    right: 0;
  }

  .item>.guides .guide-elbow-last::before {
    content: "";
    position: absolute;
    border-left: var(--guide-thickness) solid var(--tree-guide-color);
    border-bottom: var(--guide-thickness) solid var(--tree-guide-color);
    border-bottom-left-radius: var(--tree-guide-radius);
    background: transparent;
    top: 0;
    bottom: 50%;
    left: 50%;
    right: 0;
  }
}

/* ---------- Sticky controls band for BWA mobile pages ---------- */
/* Stays under the fixed app header; groups period selector + view-toggle so
   navigation controls stay reachable while scrolling long lists. */

.bwa-mobile-sticky-controls {
  position: sticky;
  top: calc(var(--app-header-height) + var(--app-admin-bar-height, 0px) + env(safe-area-inset-top, 0px));
  z-index: 2;
  background: var(--app-bg);
  padding: 0 0 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;

  .controls-row {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    min-width: 0;

    .period-wrap {
      flex: 1 1 auto;
      min-width: 0;
    }
  }

  .mobile-back-bar {
    flex: 0 0 auto;

    .mobile-back-link {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      gap: 0.4rem;
      padding: 0.4rem 0.85rem 0.4rem 0.7rem;
      background: var(--app-bg-sheet);
      border: 1px solid var(--app-chrome);
      border-radius: 999px;
      color: var(--app-link);
      font-size: var(--app-text-sm);
      font-weight: 500;
      line-height: 1;
      cursor: pointer;
      transition:
        background-color 0.15s ease,
        border-color 0.15s ease,
        color 0.15s ease;

      .bi {
        font-size: 1rem;
      }

      &:focus-visible {
        background-color: var(--app-bg-wash);
        border-color: var(--app-fg-muted);
      }

      @media (hover: hover) and (pointer: fine) {
        &:hover {
          background-color: var(--app-bg-wash);
          border-color: var(--app-fg-muted);
        }
      }
    }

    &.compact .mobile-back-link {
      flex-direction: column;
      gap: 0.1rem;
      min-width: 2.75rem;
      padding: 0.35rem 0.4rem 0.3rem;
      border-radius: 14px;

      .bi {
        font-size: 1.1rem;
        line-height: 1;
      }

      .micro-label {
        font-size: 0.6rem;
        font-weight: 600;
        line-height: 1;
        letter-spacing: 0.02em;
        text-transform: none;
        max-width: 3.25rem;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }
  }

  /* Compact mobile period selector: pill on the left, prev/next arrows on the right. */
  .period-selector {
    flex-wrap: nowrap;
    align-items: center;
    margin: 0;

    .period {
      max-width: none;
      flex: 1 1 auto;
      min-width: 0;

      .form-select {
        border-radius: 999px;
        padding: 0.4rem 2rem 0.4rem 0.9rem;
        font-weight: 500;
      }
    }

    > .col-auto.period-trailing-action {
      margin-left: 0.25rem;
    }

    .nav .btn {
      padding: 0.3rem 0.55rem;
    }
  }

  .view-toggle {
    margin: 0;
  }
}

.bwa-mobile-booking-date-hint {
  font-size: var(--app-text-xs);
  color: var(--app-fg-muted);
  text-align: right;
  margin: 0.25rem 0.25rem 0.5rem;
}

/* ---------- Operativer Cash-flow card in mobile BWA income statement ---------- */

.mobile-list .operative-cashflow {
  margin-top: 0.5rem;
}

/* ---------- Liquidität separator in mobile BWA overview ---------- */

.mobile-list .liquidity-separator {
  margin-top: 1.5rem;
  margin-bottom: 0.25rem;
}

.liquidity-statement-mobile .liquidity-mobile-stats {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin-top: 0.25rem;
}

.liquidity-statement-mobile .liquidity-stat {
  display: flex;
  justify-content: space-between;
  gap: 0.5rem;
  font-size: var(--app-text-xs);

  .stat-label {
    color: var(--app-fg-muted);
    flex: 1;
    min-width: 0;
  }

  .amount {
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
  }
}

/* ---------- Booking date hint on mobile BWA index ---------- */

.bwa-booking-date-hint {
  font-size: var(--app-text-xs);
  margin-bottom: 0.25rem;
  padding: 0 0.25rem;
}