/*
 * mobile.css — Mobile (≤ 768px) overrides.
 * Load LAST (after all other CSS).
 *
 * Strategy: remove desktop padding/width constraints, reflow dense
 * horizontal layouts into stacked rows, adapt navigation for touch.
 */

/* ---- Base: full-width layout ------------------------------------------- */
@media (max-width: 768px) {
  .content {
    padding: 0;
    max-width: none;
  }

  .calendar__body {
    border-radius: 0;
    box-shadow: none;
  }

  /* ---- Scroll: let the page controls scroll away first -------------------
   * The timeline renders at full height and the whole calendar scrolls
   * within the panel, so the header (date + nav + view toggle) scrolls up
   * and out of view before the timeline continues.
   *
   * .calendar / .calendar__body sit in column flex parents, so flex: 0 0 auto
   * makes them natural-height (width still stretches). The scroll + view
   * containers sit in ROW parents, so we must keep their flex: 1 (fill width)
   * and only switch overflow to visible to stop the internal scroll. */
  .calendar {
    flex: 0 0 auto;
  }

  .calendar__body {
    flex: 0 0 auto;
    min-height: 0;
  }

  .calendar__scroll,
  #calendar-today-view,
  .calendar__week,
  .calendar__month,
  .calendar__year {
    overflow: visible;
  }

  /* Today filter bar: chips on one line, search box full-width below. */
  .today__filter {
    flex-wrap: wrap;
  }
  .today__search {
    flex-basis: 100%;
    min-width: 0;
  }

  /* Non-calendar panels: breathing room (calendar stays full-bleed) */
  .panel:not(#panel-schedule) {
    padding: 0 16px;
  }

  /* Header: single row — date on top, nav + view toggle on one line below.
     flex-wrap: nowrap guarantees the controls never wrap to a second row. */
  .calendar__header {
    flex-wrap: nowrap;
    gap: 8px;
    padding: 8px 12px;
    padding-bottom: 8px;
    margin-bottom: 8px;
  }

  .calendar__date {
    flex: 0 0 100%;
    order: -1;
    font-size: 1rem;
    text-align: center;
    margin: 0;
  }

  .calendar__nav {
    flex: 0 0 auto;
    order: 0;
  }

  .calendar__view-toggle {
    flex: 0 1 auto;
    order: 2;
    margin-left: auto;
    /* If the segmented control is too wide for a very narrow screen, let it
       scroll horizontally instead of wrapping onto a second row. */
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .calendar__fullscreen-btn {
    display: none;
  }

  /* Time axis: narrower, hide right axis */
  .calendar__time-axis {
    flex: 0 0 36px;
    width: 36px;
    min-width: 36px;
    font-size: 0.625rem;
  }

  .calendar__time-axis--right {
    display: none !important;
  }

  .calendar__week-headers__spacer {
    width: 36px;
  }

  /* Hide app bar quote + brand */
  .app-bar__quote {
    display: none;
  }

  .brand {
    display: none;
  }

  /* Keyboard help is a desktop feature — hide the `?` button on mobile */
  .app-bar__help-btn {
    display: none;
  }

  /* Settings: wrap rows, cap input width */
  .settings__row {
    flex-wrap: wrap;
    gap: 4px;
  }

  .settings__input {
    width: 100%;
    max-width: 160px;
  }

  /* ---- Hover-revealed controls: always visible on mobile (no hover) ----
   * These are hidden by default on desktop and only appear on :hover.
   * Touch devices have no hover, so they must be persistently visible. */

  /* Habit card: pause/play toggle */
  .habit-card__toggle {
    opacity: 1;
  }

  /* Notes: row actions (rename / delete) */
  .notes__row-actions {
    display: inline-flex;
  }

  /* Entry cards: remove + upgrade-to-habit buttons */
  .entry__remove {
    opacity: 1;
  }

  .entry__upgrade {
    display: inline-flex;
  }

  /* Missed entries: show the done checkbox (keep the warn emoji too —
   *  they're separate flex siblings, so both render side by side). */
  .entry--missed .entry__done {
    display: flex;
  }

  /* Week entries: remove button + reserve title space so text doesn't
   *  run under the (now always-visible) remove button. */
  .week-entry__remove {
    opacity: 1;
  }

  .week-entry__title {
    padding-right: 20px;
  }

  .week-entry--missed .week-entry__done {
    display: flex;
  }

  /* Resize handles (bottom edge) */
  /* Bigger resize target on mobile: the 8px handle is easy to miss with a
     finger, and a missed touch lands on the timeline and scrolls the page.
     Extend the hit area up into the entry body (the ::after bar is unchanged). */
  .entry__resize,
  .week-entry__resize {
    height: 16px;
  }

  .entry__resize::after {
    opacity: 0.5;
  }

  .week-entry__resize::after {
    opacity: 0.6;
  }

  /* Safe area (notch phones) */
  .app-bar {
    padding-bottom: env(safe-area-inset-bottom, 0);
  }

  /* Prevent double-tap zoom */
  button, .tab, .entry__done {
    touch-action: manipulation;
  }

  /* Smooth scroll, no pull-to-refresh interference */
  .calendar__scroll {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  /* Timeline & week columns: the quick-create module drives touch gestures
     (swipe to scroll, long-press to create), so disable native touch scrolling
     here — otherwise a touch-drag on empty space scrolls the page and cancels
     the create-drag (pointercancel). Scrolling is handled in JS via the panel. */
  #calendar-timeline,
  .calendar__day-col {
    touch-action: none;
  }

  /* Compact controls so nav + view toggle fit on a single line.
     height: auto is essential — the base stylesheet sets a fixed
     height (32px / 30px) which would otherwise override min-height. */
  .calendar__nav-btn {
    height: auto;
    min-height: 28px;
    min-width: 0;
    padding: 3px 7px;
    font-size: 0.7rem;
  }

  .calendar__view-btn {
    height: auto;
    min-height: 28px;
    min-width: 0;
    padding: 3px 7px;
    font-size: 0.7rem;
  }
}

/* ---- Year view: 2-col on narrow phones --------------------------------- */
@media (max-width: 480px) {
  .calendar__year-grid {
    grid-template-columns: repeat(2, 1fr);
    grid-template-rows: repeat(6, 1fr);
  }
}

/* ---- Hide "Week" in portrait ------------------------------------------- */
@media (max-width: 768px) and (orientation: portrait) {
  .calendar__view-btn[data-view="week"] {
    display: none;
  }
}

/* ---- Mobile nav: icon bar mode ------------------------------------------ */
.app-bar__menu-btn {
  display: none;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: none;
  background: transparent;
  font-size: 1.5rem;
  color: var(--text);
  cursor: pointer;
  border-radius: var(--radius-md);
  flex-shrink: 0;
}

.app-bar__menu-btn:hover {
  background: var(--bg);
}

@media (max-width: 768px) {
  html[data-mobile-nav="icons"] .app-bar {
    justify-content: center;
    gap: 0;
    padding: 0 8px;
    height: 52px;
  }

  html[data-mobile-nav="icons"] .tabs {
    gap: 0;
    width: 100%;
    justify-content: space-around;
  }

  html[data-mobile-nav="icons"] .tab {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 6px 4px;
    font-size: 0.625rem;
    gap: 2px;
    border-radius: 0;
  }

  html[data-mobile-nav="icons"] .tab__icon {
    font-size: 1.25rem;
    line-height: 1;
  }

  html[data-mobile-nav="icons"] .tab__label {
    font-size: 0.5625rem;
    font-weight: 500;
  }

  html[data-mobile-nav="icons"] .tab[aria-selected="true"] {
    background: transparent;
    color: var(--accent);
  }
}

/* ---- Mobile nav: menu button mode --------------------------------------- */
@media (max-width: 768px) {
  html[data-mobile-nav="menu"] .app-bar {
    justify-content: center;
    padding: 0;
  }

  html[data-mobile-nav="menu"] .tabs {
    display: none;
  }

  html[data-mobile-nav="menu"] .app-bar__menu-btn {
    display: flex;
  }

  html[data-mobile-nav="menu"] .app-bar--menu-open .tabs {
    display: flex;
    flex-direction: column;
    position: fixed;
    bottom: 52px;
    left: 0;
    right: 0;
    background: var(--surface);
    border-top: 1px solid var(--border);
    box-shadow: var(--shadow-lg);
    padding: 8px;
    gap: 4px;
    z-index: 99;
    animation: slide-up 150ms ease;
  }

  html[data-mobile-nav="menu"] .app-bar--menu-open .tab {
    width: 100%;
    justify-content: flex-start;
    padding: 12px 16px;
    border-radius: var(--radius-md);
    font-size: 0.9375rem;
  }

  html[data-mobile-nav="menu"] .app-bar--menu-open .tab__icon {
    margin-right: 12px;
    font-size: 1.125rem;
  }

  @keyframes slide-up {
    from { transform: translateY(100%); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
  }
}

/* ---- Mobile nav: split mode (primary icons + ☰ sheet) --------------------
 * Schedule + Notes stay pinned as icon buttons in the bar; the ☰ button
 * opens a slide-up sheet holding the remaining (secondary) tabs. The best
 * of the other two modes. brand + quote are already hidden on mobile. */
@media (max-width: 768px) {
  html[data-mobile-nav="split"] .app-bar {
    justify-content: space-between;
    gap: 8px;
    padding: 0 8px;
    height: 52px;
  }

  /* Primary tabs render as compact icon buttons in the bar. */
  html[data-mobile-nav="split"] .tabs {
    display: flex;
    gap: 4px;
  }

  html[data-mobile-nav="split"] .tab {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    padding: 6px 12px;
    border-radius: var(--radius-md);
    font-size: 0.8125rem;
    font-weight: 600;
  }

  html[data-mobile-nav="split"] .tab__icon {
    font-size: 1.125rem;
    line-height: 1;
  }

  /* Hide the secondary group inside the bar by default. */
  html[data-mobile-nav="split"] .tabs__sheet {
    display: none;
  }

  html[data-mobile-nav="split"] .app-bar__menu-btn {
    display: flex;
  }

  /* When open: the sheet becomes a slide-up panel of the secondary tabs. */
  html[data-mobile-nav="split"] .app-bar--menu-open .tabs__sheet {
    display: flex;
    flex-direction: column;
    position: fixed;
    bottom: 52px;
    left: 0;
    right: 0;
    background: var(--surface);
    border-top: 1px solid var(--border);
    box-shadow: var(--shadow-lg);
    padding: 8px;
    gap: 4px;
    z-index: 99;
    animation: slide-up 150ms ease;
  }

  html[data-mobile-nav="split"] .app-bar--menu-open .tabs__sheet .tab {
    width: 100%;
    justify-content: flex-start;
    padding: 12px 16px;
    border-radius: var(--radius-md);
    font-size: 0.9375rem;
    font-weight: 500;
  }

  html[data-mobile-nav="split"] .app-bar--menu-open .tabs__sheet .tab__icon {
    margin-right: 12px;
    font-size: 1.125rem;
  }
}

/* Patterns view: tighten the label column + legend so the graph fits a phone
 * (the grid scrolls horizontally; only the leading label needs to shrink). */
@media (max-width: 768px) {
  .patterns__label {
    flex-basis: 88px;
    font-size: 0.7rem;
  }
  .patterns__legend {
    padding-left: 96px;
    gap: 8px;
  }
  .patterns__cell {
    flex-basis: 10px;
    width: 10px;
    height: 10px;
  }
  .patterns__head-cell {
    flex-basis: 10px;
    width: 10px;
  }
}

