/* Tablet and phone adjustments. Loaded last so it overrides the other files on
   small screens: 1000px = tablet, 760px = phone, 360px = very small phone. */

@media (max-width: 1000px) {
  .grid4 {
    grid-template-columns: 1fr 1fr;
  }

  .two {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 760px) {
  .app {
    display: block;
  }

  /* Phone header: logo | month | burger, everything else in the menu */
  .sidebar {
    position: sticky;
    top: 0;
    z-index: 15;
    display: block;
    height: auto;
    overflow: visible;
    padding: 10px 12px 10px 16px;
  }

  .sidebar-top {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 0;
  }

  .logo {
    gap: 8px;
    font-size: 18px;
  }

  .logo img,
  .logo-mark {
    width: 24px;
    height: 24px;
  }

  .mobile-month {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin-left: auto;
    min-height: 38px;
    padding: 6px 12px;
    border: 1px solid var(--nav-line);
    border-radius: 99px;
    background: var(--nav-hover);
    color: var(--on-dark);
    font-size: 14px;
    font-weight: 600;
    white-space: nowrap;
  }

  .sidebar .burger {
    display: flex;
  }

  .sidebar .burger:hover:not(:disabled) {
    background: var(--nav-hover);
  }

  /* The menu opens below the header */
  .sidebar-menu {
    position: absolute;
    left: 0;
    right: 0;
    top: 100%;
    display: grid;
    grid-template-rows: 0fr;
    margin: 0;
    background: var(--nav);
    box-shadow: var(--shadow-menu);
    transition: grid-template-rows 0.3s ease;
  }

  .sidebar-menu.open {
    grid-template-rows: 1fr;
  }

  .sidebar-menu-inner {
    overflow: hidden;
    padding: 0 16px;
  }

  .sidebar-menu.open .sidebar-menu-inner {
    padding-bottom: 16px;
    border-top: 1px solid var(--nav-line);
  }

  .sidebar-menu .month-switch:first-child {
    display: none; /* the month is already shown in the header */
  }

  .account-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin: 14px 0 0;
    color: var(--on-dark-soft);
  }

  .sidebar-tools {
    margin: 14px 0 0;
  }

  .save-status:not(.error) {
    display: block;
  }

  .month-switch button {
    flex-shrink: 0;
    width: auto;
    margin: 0;
    min-height: 36px;
    padding: 6px 11px;
    font-size: 13px;
  }

  .nav button.active {
    box-shadow: inset 0 -3px 0 var(--brand-light);
  }

  .nav {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 10;
    display: grid;
    grid-template-columns: repeat(6, minmax(0, 1fr));
    background: var(--nav);
    border-top: 1px solid var(--nav-line);
    padding: 8px 4px calc(8px + env(safe-area-inset-bottom));
  }

  .nav button {
    min-height: 54px;
    margin: 0;
    padding: 7px 1px;
    border-radius: 10px;
    text-align: center;
    font-size: 11px;
    letter-spacing: -0.2px;
    line-height: 1.3;
    overflow-wrap: anywhere;
  }

  .nav button.active {
    background: var(--nav-active);
  }

  /* Short labels in the bottom bar so nothing wraps */
  .nav-long {
    display: none;
  }

  .nav-short {
    display: inline;
  }

  table.compact-list td.no-end {
    display: none;
  }

  .nav-icon {
    display: block;
    margin: 0 0 5px;
    font-size: 20px;
    line-height: 1;
  }

  .content {
    margin: 0;
    padding: 20px 16px calc(110px + env(safe-area-inset-bottom));
  }

  .head {
    flex-direction: column;
    align-items: stretch;
    gap: 14px;
    margin-bottom: 20px;
  }

  .head h1 {
    font-size: 26px;
  }

  .head p {
    line-height: 1.5;
  }

  .head-actions {
    flex-direction: column;
  }

  .head-actions select {
    max-width: none;
  }

  .grid4 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }

  .card {
    padding: 16px;
    border-radius: 18px;
  }

  .kpi strong {
    font-size: clamp(15px, 4.5vw, 22px);
    white-space: nowrap;
  }

  .kpi .label,
  .kpi .sub {
    font-size: 12px;
    letter-spacing: 0.3px;
  }

  .row .field {
    flex-basis: 100%;
    min-width: 0;
  }

  .row > button,
  .row > .button-link {
    width: 100%;
  }

  /* Date/amount/category etc.: two fields side by side instead of stacked */
  .row .field.half {
    flex: 1 1 calc(50% - 6px);
  }

  .card.fold {
    padding: 14px 16px;
  }

  .fold-body .chart-legend {
    margin-top: 6px;
  }

  .chart {
    gap: 5px;
  }

  /* Tables as cards */
  table.mobile-cards thead {
    display: none;
  }

  table.mobile-cards,
  table.mobile-cards tbody,
  table.mobile-cards tr {
    display: block;
    width: 100%;
  }

  table.mobile-cards tr {
    border: 1px solid var(--line);
    border-radius: 12px;
    padding: 10px;
    margin-top: 12px;
  }

  table.mobile-cards td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 16px;
    width: 100%;
    padding: 8px 3px;
    border: 0;
    overflow-wrap: anywhere;
  }

  table.mobile-cards td::before {
    content: attr(data-label);
    flex-shrink: 0;
    max-width: 46%;
    font-size: 13px;
    font-weight: 400;
    color: var(--muted);
  }

  table.mobile-cards td > div {
    text-align: right;
  }

  table.mobile-cards td:empty,
  table.mobile-cards td[data-label=""]::before,
  table.mobile-cards td.empty::before {
    display: none;
  }

  table.mobile-cards td.empty {
    justify-content: center;
  }

  table.mobile-cards td input {
    max-width: 52%;
    min-width: 0;
  }

  /* Salary per apprentice year: year on top, then the fields two per row */
  table.year-table tr {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px 8px;
    padding: 12px;
  }

  table.year-table td {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
    padding: 0;
  }

  table.year-table td:first-child {
    grid-column: 1 / -1;
  }

  table.year-table td:first-child::before {
    display: none;
  }

  table.year-table td::before {
    max-width: none;
    overflow-wrap: normal;
    hyphens: auto;
    font-size: 12px;
    line-height: 1.3;
  }

  table.year-table td input {
    max-width: none;
    margin-top: auto;
  }

  table.year-table td input[type="checkbox"] {
    margin-top: 0;
  }

  /* Allowances: one row per apprentice year (year | amount | of which saved), labels only on top */
  .allowance-years {
    grid-template-columns: 1fr;
    gap: 6px;
  }

  .allowance-year {
    grid-template-columns: 58px 1fr 1fr;
    align-items: end;
    padding: 6px 8px;
  }

  .allowance-year b {
    grid-column: auto;
    padding-bottom: 11px;
    font-size: 12px;
  }

  .allowance-year:not(:first-child) label {
    gap: 0;
    font-size: 0;
  }

  .allowance-year input {
    font-size: 16px;
  }

  /* Compact long lists: description left, amount right, date/category small below */
  table.compact-list tr {
    display: flex;
    flex-wrap: wrap;
    align-items: baseline;
    gap: 3px 10px;
    margin: 0;
    padding: 12px 0;
    border: 0;
    border-bottom: 1px solid var(--line);
    border-radius: 0;
  }

  table.compact-list td {
    order: 3;
    display: block;
    width: auto;
    padding: 0;
    color: var(--muted);
    font-size: 13px;
  }

  table.compact-list td::before {
    display: none;
  }

  /* Line break after description + amount */
  table.compact-list tr::after {
    content: "";
    order: 2;
    flex-basis: 100%;
    height: 0;
  }

  table.compact-list tr.more-row::after {
    display: none;
  }

  table.compact-list td > div {
    text-align: left;
  }

  table.compact-list td.ledger-description,
  table.compact-list td.item-name {
    order: 1;
    flex: 1 1 0;
    min-width: 0;
    color: var(--ink);
    font-size: 15px;
  }

  table.compact-list td.money {
    order: 2;
    color: var(--ink);
    font-size: 15px;
    font-weight: 700;
    white-space: nowrap;
  }

  table.compact-list td.good {
    color: var(--green);
  }

  table.compact-list td.bad {
    color: var(--red);
  }

  table.compact-list td.transfer {
    color: var(--orange);
  }

  table.compact-list td.row-status {
    order: 3;
  }

  table.compact-list td.row-actions {
    order: 4;
    margin-left: auto;
  }

  table.compact-list td.row-actions .actions {
    flex-wrap: nowrap;
    gap: 4px;
  }

  table.compact-list td.row-actions button {
    min-width: 0;
    min-height: 32px;
    padding: 4px 10px;
    font-size: 13px;
  }

  table.compact-list .ledger-detail {
    margin-top: 3px;
    font-size: 12px;
  }

  table.compact-list td.empty,
  table.compact-list .more-row td {
    flex-basis: 100%;
    text-align: center;
  }

  table.compact-list tr.more-row,
  table.compact-list tr:last-child {
    border-bottom: 0;
  }

  table.compact-list tr.more-row {
    padding-bottom: 0;
  }

  .toast {
    bottom: calc(94px + env(safe-area-inset-bottom));
  }
}

@media (max-width: 360px) {
  .content {
    padding-left: 12px;
    padding-right: 12px;
  }

  .grid4 {
    gap: 8px;
  }

  .card {
    padding: 12px;
  }
}

/* Phone: compact account balances, hide the logo text when space is short */
@media (max-width: 760px) {
  .account-card {
    padding: 12px 14px;
  }

  .account-card.spending {
    grid-column: 1 / -1;
  }

  .account-card b {
    font-size: 14px;
    overflow-wrap: anywhere;
  }

  .account-card strong {
    font-size: clamp(16px, 4.6vw, 20px);
    white-space: nowrap;
  }
}

@media (max-width: 420px) {
  .sidebar .logo > span {
    display: none;
  }
}


/* Phones (iPhone 11 and newer: 375–440px, Android: 360–412px) */

@media (max-width: 760px) {
  /* Notch and home bar (app.html uses viewport-fit=cover) */
  .sidebar {
    padding-top: env(safe-area-inset-top);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
  }

  .content {
    padding-left: max(16px, env(safe-area-inset-left));
    padding-right: max(16px, env(safe-area-inset-right));
  }

  /* Bottom bar: slightly larger labels where "Einstellungen" still fits on one line */
  .nav button {
    font-size: 12px;
    overflow-wrap: normal;
  }

  /* "Anordnen": small button next to the title instead of a full-width bar */
  .head {
    position: relative;
  }

  .head:has(.arrange-toggle) > div:first-child {
    padding-right: 112px;
  }

  .head .arrange-toggle {
    position: absolute;
    top: 0;
    right: 0;
    width: auto;
    min-height: 40px;
    padding: 8px 14px;
    font-size: 14px;
  }

  /* Month page: the month is chosen at the top (▾), not twice */
  #monthPicker {
    display: none;
  }

  /* Arranging: one column, so wider/narrower has no effect */
  [data-action="resize-block"] {
    display: none;
  }

  /* Settings tabs: all four visible as 2 × 2 */
  .settings-tabs {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    overflow: visible;
  }

  .settings-tabs button {
    white-space: normal;
    min-height: 44px;
  }

  /* Allowances: name and switch, then monthly/yearly and "Remove" */
  .allowance-head {
    flex-wrap: wrap;
    gap: 8px;
  }

  .allowance-head .switch {
    flex: 1 1 100%;
  }

  .allowance-per {
    flex: 1 1 100px;
    min-width: 0;
    font-size: 16px; /* smaller text makes iPhones zoom in */
  }

  /* Larger tap areas (about 44px) without changing the look */
  .fold > summary {
    min-height: 44px;
  }

  .chip-button {
    width: 30px;
    height: 36px;
  }

  .tour-skip,
  .onboarding-close {
    padding: 11px 6px;
    margin: -11px -6px;
  }
}

@media (max-width: 400px) {
  .nav button {
    font-size: 11px;
  }
}

/* Compact phone layout: tidy header, smaller headings and cards, more side by side */

@media (max-width: 760px) {
  /* Header: logo left, month in the middle, menu right – with even margins (also beside a notch) */
  .sidebar {
    padding: env(safe-area-inset-top) max(14px, env(safe-area-inset-right)) 0 max(14px, env(safe-area-inset-left));
    border-bottom: 1px solid var(--nav-line);
  }

  .sidebar-top {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
    align-items: center;
    gap: 8px;
    min-height: 56px;
  }

  .sidebar-top .logo {
    justify-self: start;
    min-width: 0;
    gap: 8px;
    font-size: 17px;
    white-space: nowrap;
  }

  .sidebar-top .logo-mark {
    flex-shrink: 0;
    width: 30px;
    height: 30px;
  }

  .mobile-month {
    justify-self: center;
    margin-left: 0;
    min-height: 36px;
    padding: 6px 14px;
    font-size: 13px;
  }

  .sidebar .burger {
    justify-self: end;
    margin-right: -6px;
  }

  /* Page head: smaller title, "Anordnen" as a small button beside it */
  .content {
    padding-top: 16px;
  }

  .head {
    gap: 12px;
    margin-bottom: 14px;
  }

  .head h1 {
    font-size: 23px;
    line-height: 1.2;
  }

  .head p {
    margin-top: 3px;
    font-size: 14px;
    line-height: 1.4;
  }

  .head:has(.arrange-toggle) > div:first-child {
    padding-right: 96px;
  }

  .head .arrange-toggle {
    min-height: 34px;
    padding: 6px 12px;
    font-size: 13px;
  }

  .blocks {
    gap: 10px;
    margin-top: 10px;
  }

  .card {
    padding: 14px;
    border-radius: 16px;
  }

  .card.fold {
    padding: 12px 14px;
  }

  /* Dashboard: "Noch verfügbar" more compact */
  .hero-period {
    margin-bottom: 8px;
    font-size: 13px;
  }

  .hero-label {
    font-size: 14px;
  }

  .hero-amount {
    margin: 0 0 10px;
    font-size: 34px;
  }

  .hero-progress {
    height: 8px;
  }

  .available-hero > .note {
    font-size: 13px;
  }

  .summary-row {
    padding: 9px 0;
    font-size: 15px;
  }

  /* Dashboard on the phone: own positions (js/app/layout.js); by default the accounts side by side */
  .blocks[data-sortable] {
    --gap: 10px;
  }


  .account-card {
    gap: 2px;
    height: 100%;
    padding: 10px 12px;
    border-radius: 14px;
  }

  /* Name and amount at the top in both cards (same height), the small note below */
  .account-card {
    align-content: start;
  }

  .account-card .account-type {
    order: 3;
    margin-top: 2px;
    overflow: hidden;
    font-size: 11px;
    line-height: 1.3;
    white-space: nowrap;
    text-overflow: ellipsis;
  }

  .account-card b {
    font-size: 13px;
  }

  .account-card strong {
    font-size: clamp(15px, 4.4vw, 18px);
  }

  .upcoming li {
    gap: 10px;
    padding: 8px 0;
  }

  .compare-values {
    font-size: 13px;
  }

  .compare-row {
    padding: 10px 0;
  }

  /* Month page: smaller key figures */
  .grid4 {
    gap: 8px;
  }

  .kpi {
    padding: 10px 12px;
  }

  .kpi strong {
    font-size: clamp(15px, 4.4vw, 19px);
  }

  .kpi .label,
  .kpi .sub {
    font-size: 11px;
  }

  table.compact-list tr {
    padding: 9px 0;
  }

  table.compact-list td.ledger-description,
  table.compact-list td.item-name,
  table.compact-list td.money {
    font-size: 14px;
  }

  /* Settings: wage per apprentice year as a small card each:
     year on top, net wage and automatic saving side by side (with labels),
     then the 13th-month salary with the switch "13. Lohn sparen" on its own line */
  table.year-table thead {
    display: none;
  }

  table.year-table tr {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px 10px;
    margin: 0 0 10px;
    padding: 12px;
    border: 1px solid var(--line);
    border-radius: 12px;
  }

  table.year-table td:first-child {
    grid-column: 1 / -1;
    font-size: 15px;
  }

  table.year-table td::before {
    font-size: 12px;
  }

  /* 13th-month salary: tinted box at the bottom of the card, purple when it is saved */
  table.year-table td.computed {
    grid-column: 1 / -1;
    margin-top: 4px;
    padding: 10px 12px;
    border-radius: 10px;
    background: var(--soft);
    text-align: left;
  }

  table.year-table td.computed:has(input:checked) {
    background: var(--brand-soft);
  }

  table.year-table tr td.computed::before {
    display: none;
  }

  table.year-table .thirteenth-cell {
    justify-content: space-between;
    width: 100%;
  }

  table.year-table .thirteenth-info {
    display: grid;
    gap: 1px;
  }

  table.year-table .thirteenth-title {
    display: block;
    color: var(--label);
    font-size: 12px;
    font-weight: 500;
  }

  table.year-table .thirteenth-amount {
    color: var(--ink);
    font-size: 15px;
    font-weight: 700;
  }

  table.year-table .thirteenth-save .long-label {
    display: none;
  }

  table.year-table .thirteenth-save .short-label {
    display: inline;
  }

  .allowance {
    padding: 10px 12px;
  }

  /* Forms and fields a little tighter (fields stay 16px, otherwise iPhones zoom in) */
  .row {
    gap: 10px;
  }

  .field {
    gap: 4px;
  }

  .field label {
    font-size: 13px;
  }

  input,
  select {
    min-height: 42px;
    padding: 8px 10px;
  }

  /* room for the ▾ arrow, so long choices do not run into it */
  select {
    padding-right: 34px;
    text-overflow: ellipsis;
  }

  .primary,
  .secondary {
    padding: 10px 14px;
  }
}

/* Phones: logo only, so the month fits exactly in the middle */
@media (max-width: 480px) {
  .sidebar-top .logo > span {
    display: none;
  }
}

/* Phone: entries and standing orders as tidy blocks. Flex rows (not a grid), so the
   amount column does not take the width of the buttons:
   Kaugummi                              − CHF 2.50
   06.10.2026 · Essen · Karte      Bearbeiten  Löschen
   Standing orders get their status (Offen / fällig am …) on the last line, next to the buttons. */
@media (max-width: 760px) {
  table.compact-list.entry-table tr,
  table.compact-list.recurring-table tr {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 2px 12px;
    padding: 10px 0;
  }

  table.compact-list.entry-table td,
  table.compact-list.recurring-table td {
    display: none;
  }

  /* line break after name + amount */
  table.compact-list.entry-table tr::after,
  table.compact-list.recurring-table tr::after {
    display: block;
    content: "";
    order: 3;
    flex-basis: 100%;
    height: 0;
  }

  table.compact-list.entry-table td.ledger-description,
  table.compact-list.recurring-table td.ledger-description {
    display: block;
    order: 1;
    flex: 1 1 0;
    min-width: 0;
    font-weight: 600;
  }

  table.compact-list.entry-table td.money,
  table.compact-list.recurring-table td.money {
    display: block;
    order: 2;
    text-align: right;
    white-space: nowrap;
  }

  table.compact-list.entry-table td.entry-meta {
    display: block;
    order: 4;
    flex: 1 1 0;
    min-width: 0;
    font-size: 13px;
  }

  table.compact-list.recurring-table td.recurring-meta {
    display: block;
    order: 4;
    flex: 1 1 100%;
    font-size: 13px;
  }

  table.compact-list.recurring-table td.row-status {
    display: flex;
    order: 5;
    flex: 1 1 0;
    flex-direction: column;
    align-items: flex-start;
    gap: 3px;
    min-width: 0;
    margin-top: 4px;
  }

  table.compact-list.recurring-table td.row-status .ledger-detail {
    margin: 0;
  }

  table.compact-list.entry-table td.row-actions,
  table.compact-list.recurring-table td.row-actions {
    display: block;
    order: 6;
    margin: 0 0 0 auto;
  }

  table.compact-list.entry-table td.empty,
  table.compact-list.recurring-table td.empty {
    display: block;
    flex-basis: 100%;
  }

  /* Small "Bearbeiten / Löschen" in lists, so a row stays tidy */
  table.compact-list td.row-actions button {
    min-height: 30px;
    padding: 4px 9px;
    font-size: 13px;
  }
}

/* iPhone/iPad (Safari): date fields have their own width and centred text there.
   This makes them as wide as the other fields, with the date on the left. */
@supports (-webkit-touch-callout: none) {
  input[type="date"] {
    -webkit-appearance: none;
    appearance: none;
    display: block;
    width: 100%;
    min-width: 0;
    max-width: 100%;
    min-height: 42px;
    line-height: 1.4;
    text-align: left;
  }

  input[type="date"]::-webkit-date-and-time-value {
    margin: 0;
    text-align: left;
  }
}

/* Fields inside rows may shrink instead of pushing the row wider than the card */
@media (max-width: 760px) {
  .row .field,
  .field input,
  .field select {
    min-width: 0;
    max-width: 100%;
  }
}

/* Narrow dashboard blocks (any screen): the content adapts to the width of its block,
   so nothing is cut off. The blocks are size containers (base.css). */
@container (max-width: 560px) {
  .grid4 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .block .two {
    grid-template-columns: minmax(0, 1fr);
  }
}

@container (max-width: 420px) {
  .hero-amount {
    font-size: clamp(22px, 11cqi, 34px);
  }
}

@container (max-width: 300px) {
  .grid4 {
    grid-template-columns: minmax(0, 1fr);
  }

  .summary-row {
    flex-wrap: wrap;
    gap: 2px 12px;
  }

  .summary-row b {
    margin-left: auto;
  }

  .block .grid2 {
    grid-template-columns: minmax(0, 1fr);
  }

  .compare-values {
    grid-template-columns: minmax(0, 1fr);
    gap: 2px;
  }
}

/* Very narrow blocks (about 160–240 px) */
@container (max-width: 240px) {
  .card {
    padding: 12px;
  }

  .hero-amount {
    font-size: 19px;
  }

  .account-card strong,
  .kpi strong {
    font-size: 17px;
  }

  /* Whole words wrap (long ones are hyphenated), never single letters */
  .card,
  .fold-title {
    overflow-wrap: break-word;
    hyphens: auto;
  }

  .fold > summary {
    font-size: 15px;
  }

  /* "Kommt noch": name on its own line, below it date and amount */
  .upcoming li {
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-areas: "name name" "date money";
    gap: 2px 8px;
  }

  .upcoming-name {
    grid-area: name;
    overflow-wrap: break-word;
  }

  .upcoming-date {
    grid-area: date;
  }

  .upcoming .money {
    grid-area: money;
  }

  .category-label {
    flex-wrap: wrap;
    gap: 2px 8px;
  }
}
