/* Keep desktop and print styles intact. The touch query also covers phones
   reporting a wider layout viewport and unfolded mobile screens. */
@media screen and (max-width: 760px),
       screen and (max-device-width: 760px),
       screen and (max-width: 1100px) and (hover: none) and (pointer: coarse) {
  :root { font-size: 125%; }
  body { font-size: 1rem; }

  .app-header { padding: .8rem; gap: .4rem; }
  .app-header > div:first-child { flex: 1; min-width: 0; }
  .app-header h1 span { display: inline-block; }
  .header-actions { flex-shrink: 0; }
  .header-actions .quiet-btn { min-width: 2.9rem; padding-inline: .45rem; }
  .app-shell { padding: .8rem .8rem calc(6rem + env(safe-area-inset-bottom)); }
  .view-heading { flex-wrap: wrap; }
  .view-heading h2 { font-size: 1.4rem; }

  .primary-btn, .secondary-btn, .quiet-btn, .danger-btn, .file-btn,
  .scope-dialog button { min-height: 2.9rem; }
  .period-control { min-width: 0; max-width: 100%; }
  .period-control button { flex: 0 0 2.75rem; width: 2.75rem; height: 2.75rem; }
  .period-control h2 { min-width: 0; }
  .week-heading .period-control { flex: 1 1 100%; justify-content: space-between; }
  .week-heading .secondary-btn { min-height: 2.9rem; }
  #weekTitle { text-align: center; line-height: 1.4; }

  /* Padding belongs to the actual button so the entire card is tappable. */
  .event-card { padding: 0; }
  .event-card button {
    box-sizing: border-box;
    padding: 1rem;
    min-height: 4rem;
    border-radius: inherit;
  }
  .event-card button > span:last-child { min-width: 0; overflow-wrap: anywhere; }
  .empty-card { padding: 1.5rem 1rem; }
  .week-list { grid-template-columns: 1fr; }
  .week-day { min-height: auto; display: grid; grid-template-columns: 6.6rem minmax(0, 1fr); gap: .7rem; }
  .week-date { min-height: 2.9rem; border: 0; }
  .week-events { min-width: 0; margin: 0; }
  .week-event, .week-more { min-height: 2.9rem; overflow-wrap: anywhere; }

  #monthView .view-heading { align-items: flex-start; gap: .65rem; }
  #monthView .period-control { gap: .3rem; }
  #monthView .period-control h2 { font-size: 1.25rem; white-space: nowrap; }
  .month-actions .primary-btn { display: none; }
  #monthView .weekday-row { display: grid; }
  #monthView .month-grid { grid-template-columns: repeat(7, minmax(0, 1fr)); }
  #monthView .day-cell { display: block; min-height: 4.65rem; padding: 0; }
  #monthView .day-cell.outside { display: block; opacity: .42; background: #f4f6f8; }
  #monthView .day-summary {
    min-height: 4.65rem;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    padding: .46rem .1rem;
  }
  #monthView .day-number { font-size: .92rem; margin: 0; }
  #monthView .slot-grid, #monthView .more-count { display: none; }
  #monthView .month-indicator { margin-top: .35rem; gap: .16rem; }
  .year-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .mini-grid { font-size: .75rem; }

  .bottom-nav { width: calc(100% - 1rem); bottom: max(.6rem, env(safe-area-inset-bottom)); }
  .bottom-nav button { min-width: 0; min-height: 3rem; padding: .55rem .15rem; line-height: 1.35; }
  .bottom-nav span { font-size: .82rem; }

  dialog { max-height: 90dvh; }
  .dialog-head h2 { min-width: 0; overflow-wrap: anywhere; }
  .icon-btn {
    flex: 0 0 2.9rem;
    min-width: 2.9rem;
    min-height: 2.9rem;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    line-height: 1;
  }
  .form-grid, .lunar-fields, .mapping-grid { min-width: 0; }
  label { min-width: 0; }
  label input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]),
  label select, label textarea, .mapping-grid select, .import-preview select {
    min-width: 0;
    max-width: 100%;
    min-height: 2.9rem;
    font-size: 1rem;
  }
  .segmented label, .check-grid label, .all-day-toggle {
    display: flex;
    align-items: center;
    gap: .4rem;
    min-height: 2.9rem;
  }
  .check-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .check-grid label { padding: .3rem .15rem; }
  .segmented input, .check-grid input, .all-day-toggle input {
    flex: 0 0 1.2rem;
    width: 1.2rem;
    height: 1.2rem;
    margin: 0;
    accent-color: var(--blue);
  }
  .dialog-actions { flex-wrap: wrap; }
  .dialog-actions button { min-width: 4rem; }
  .day-dialog, .day-dialog-panel { max-height: 84dvh; }
  .day-dialog-event { min-height: 3.75rem; grid-template-columns: 4.2rem minmax(0, 1fr) auto; }
  .day-dialog-actions { grid-template-columns: auto minmax(0, 1fr); padding-bottom: max(.75rem, env(safe-area-inset-bottom)); }
  .calendar-conversion div { grid-template-columns: 5rem minmax(0, 1fr); }
  .calendar-conversion strong, .detail-row { overflow-wrap: anywhere; }
  .mapping-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .file-name { overflow-wrap: anywhere; }

  button:focus-visible, .file-btn:focus-within {
    outline: 3px solid var(--blue);
    outline-offset: 2px;
  }
}

@media screen and (max-width: 420px) {
  .year-grid, .form-grid, .lunar-fields, .mapping-grid { grid-template-columns: minmax(0, 1fr); }
  .week-day { grid-template-columns: 5.5rem minmax(0, 1fr); }
  .event-dialog form, .detail-dialog, .scope-dialog form { padding: .8rem; }
  .day-dialog-event { grid-template-columns: 3.6rem minmax(0, 1fr) auto; gap: .4rem; }
}
