/* Compact application bar. Loaded last: it only changes layouts at <= 900px; the desktop header is untouched.
   Layout budget at 390x844: controls row 52 + day strip 52 = about 108px before the schedule (there is no title bar:
   the bottom navigation says where you are, the summary lives in the panel, and the bell / menu sit in the controls row). */

/* The wrapper is invisible on desktop, so the original header layout is unchanged. */
.app-header { display: contents; }
.app-logo, .page-subtitle, .top-bar-buttons, .today-compact { display: none; }

@media (max-width: 900px), (max-height: 480px) and (orientation: landscape) {
  /* The sidebar brand block moves into the bar; the bottom navigation (fixed) stays where it is. */
  .sidebar > div:first-child { display: none; }
  .sidebar { padding: 0; border-bottom: 0; }

  .main { padding-top: 0; padding-left: 12px; padding-right: 12px; }
  #view { margin-top: 6px; }
  .stats, #notices { display: none; }          /* summary line + bell replace them */

  .app-header {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 2px 6px;
    position: sticky;
    top: 0;
    z-index: var(--ui-layer-nav);
    margin: 0 -12px;
    padding: calc(env(safe-area-inset-top) + 2px) 12px 2px;
    background: var(--bg-bg);
    border-bottom: 1px solid var(--line-border);
    transition: transform .2s ease;
  }
  .app-header.is-hidden { transform: translateY(-100%); }
  .app-header.has-open-panel { max-height: calc(100dvh - 96px); overflow: auto; overscroll-behavior: contain; }
  @media (prefers-reduced-motion: reduce) { .app-header { transition: none; } }

  /* Row 1 is the controls row: the old title bar is flattened away. The title stays for screen readers. */
  .app-header .topline, .app-header .toolbar { display: contents; }
  .topline-title { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
  .app-header .week-field { order: 1; flex: 1 1 0; min-width: 0; margin: 0; width: auto; }
  .today-compact { order: 2; }
  .app-header .mobile-filter-toggle { order: 3; }
  .top-bar-buttons { order: 4; display: flex; flex: none; gap: 0; }
  .bar-btn { position: relative; display: inline-grid; place-items: center; width: 38px; height: var(--tap-min); padding: 0; border: 0; border-radius: 10px; background: transparent; color: var(--ink-text); }
  .bar-btn[hidden] { display: none; }
  .bar-btn:hover, .bar-btn[aria-expanded="true"] { background: var(--softblue-bg); }
  .badge { display: inline-grid; place-items: center; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--badge-bg); color: var(--badge-fg); font-size: 0.6875rem; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; }
  .badge[hidden] { display: none; }
  .badge--alert { background: var(--badge-alert-bg); color: var(--badge-alert-fg); }
  .bar-btn .badge { position: absolute; top: 3px; right: -1px; }

  /* The old action row becomes the "more" menu. */
  .app-header .top-actions { display: none; position: absolute; right: 0; top: calc(100% + 2px); z-index: 2; flex-direction: column; flex-wrap: nowrap; gap: 2px; width: min(260px, calc(100vw - 24px)); max-width: none; margin: 0; padding: 6px; background: var(--paper-bg); border: 1px solid var(--line-border); border-radius: 12px; box-shadow: 0 12px 32px rgba(0, 0, 0, 0.18); }
  .app-header .top-actions.is-open { display: flex; }
  .app-header .top-actions button { display: flex; align-items: center; justify-content: flex-start; gap: 10px; flex: none; width: 100%; min-height: var(--tap-min); padding: 8px 12px; border: 0; border-radius: 8px; background: transparent; color: var(--ink-text); font-size: 0.9375rem; text-align: left; }
  .app-header .top-actions button:hover { background: var(--softblue-bg); }
  .app-header .top-actions .theme-btn span { display: inline; }
  .app-header .top-actions .clock { display: none; }

  /* Week stepper, "today" (only when you are not already on today) and filters. */
  .app-header .week-field > span:first-child { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
  .app-header .week-control { gap: 4px; width: 100%; }
  .app-header .week-control button { height: var(--tap-min); min-width: 30px; padding: 0 4px; font-size: 1.25rem; }
  .app-header .week-control select { height: var(--tap-min); padding: 4px 4px 4px 8px; font-size: 0.875rem; }
  .today-compact[hidden] { display: none; }
  .today-compact { display: inline-flex; align-items: center; justify-content: center; flex: none; height: var(--tap-min); padding: 0 8px; border: 1px solid var(--c-d4e1f8-border); border-radius: 10px; background: var(--softblue-bg); color: var(--blue-text); font-size: 0.875rem; }
  .app-header .mobile-filter-toggle { display: inline-flex; align-items: center; justify-content: center; flex: none; width: auto; min-height: var(--tap-min); gap: 4px; padding: 0 8px; border: 1px solid var(--line-border); border-radius: 10px; background: var(--paper-bg); color: var(--ink-text); }
  .app-header .filter-fields { order: 6; flex: 1 0 100%; }

  /* Row 2: day strip. */
  .app-header .weekstrip { order: 5; flex: 1 0 100%; margin: 0; padding: 0; gap: 2px; }
  .app-header .day-btn { min-height: 46px; padding-top: 3px; padding-bottom: 3px; }
  .app-header .day-btn strong { font-size: 0.875rem; white-space: nowrap; }
  /* "· 今天" would wrap in the narrow buttons: show a dot under the date instead (aria-current=date keeps it accessible). */
  .today-tag { display: none; }
  .app-header .day-btn.today::after { content: ''; display: block; width: 5px; height: 5px; margin-top: 1px; border-radius: 50%; background: currentColor; }

  /* Anchors must clear the sticky bar. */
  #view, #dayDutyBoard { scroll-margin-top: calc(var(--app-header-h, 160px) + 8px); }
}

/* Narrow phones (320px): squeeze the controls row instead of wrapping it, which would cost another 48px. */
@media (max-width: 340px) {
  .app-header { column-gap: 3px; }
  .app-header .week-control { gap: 2px; }
  .app-header .week-control button { min-width: 26px; padding: 0 2px; }
  .bar-btn { width: 34px; }
  .app-header .mobile-filter-toggle > span:last-child:not(.badge) { display: none; } /* chevron */
  .app-header .week-control select { font-size: 0.8125rem; padding-left: 4px; }
  .today-compact, .app-header .mobile-filter-toggle { padding: 0 6px; }
}

/* Short landscape screens (phones turned sideways, height < 480px): height is the scarce axis.
   Navigation moves from the bottom to a slim rail on the left, and the three header rows collapse into one,
   so the schedule starts about 55px from the top instead of 160px. */
@media (max-height: 480px) and (orientation: landscape) {
  :root { --rail-w: calc(56px + env(safe-area-inset-left)); --pad-l: calc(var(--rail-w) + 8px); --pad-r: calc(10px + env(safe-area-inset-right)); }

  .nav-list {
    inset: 0 auto 0 0; width: var(--rail-w); z-index: calc(var(--ui-layer-nav) + 2);
    display: flex; flex-direction: column; justify-content: center; gap: 2px; overflow-y: auto;
    padding: 4px 4px 4px calc(4px + env(safe-area-inset-left)); border-top: 0; border-right: 1px solid var(--c-dbe3ef-border); box-shadow: none;
  }
  .nav-btn { flex: none; min-height: 44px; padding: 3px 0; gap: 1px; font-size: 0.625rem; line-height: 1.15; }
  .nav-btn svg { width: 18px; height: 18px; }

  .main { padding: 0 var(--pad-r) calc(8px + env(safe-area-inset-bottom)) var(--pad-l); }
  .app-header { margin: 0 calc(-1 * var(--pad-r)) 0 calc(-1 * var(--pad-l)); padding: calc(env(safe-area-inset-top) + 2px) var(--pad-r) 2px var(--pad-l); }
  #view { margin-top: 4px; }

  /* One row: week stepper, today, filters, day strip, bell and menu. The title is still there for screen readers. */
  .app-header { display: flex; flex-wrap: wrap; align-items: center; gap: 2px 6px; }
  .app-header .topline, .app-header .toolbar { display: contents; }
  .topline-title { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }
  .app-header .week-field { order: 1; flex: 0 0 176px; }
  .app-header .week-control select { font-size: 0.8125rem; }
  .today-compact { order: 2; }
  .app-header .mobile-filter-toggle { order: 3; }
  .app-header .weekstrip { order: 4; flex: 1 1 280px; min-width: 0; padding: 0; }
  .app-header .day-btn { min-height: 40px; padding-top: 1px; padding-bottom: 1px; }
  .app-header .day-btn strong { font-size: 0.8125rem; }
  .top-bar-buttons { order: 5; margin-left: auto; }
  .app-header .filter-fields { order: 6; }
  .app-header .top-actions { top: 100%; }
  .app-header.has-open-panel { max-height: calc(100dvh - 16px); }
}
/* ~667px landscape: squeeze the controls so the day strip still fits on the same row (otherwise the bar wraps to two). */
@media (max-height: 480px) and (orientation: landscape) and (max-width: 760px) {
  .app-header .week-field { flex-basis: 128px; }
  .app-header .week-control { gap: 2px; }
  .app-header .week-control button { min-width: 30px; }
  .app-header .weekstrip { flex-basis: 236px; gap: 1px; }
  .app-header .day-btn { font-size: 0.6875rem; }
  .app-header .day-btn strong { font-size: 0.75rem; }
  .today-compact, .app-header .mobile-filter-toggle { padding: 0 6px; }
}

@media print {
  .app-header { position: static; display: block; transform: none; border: 0; }
  .top-bar-buttons, .today-compact, .app-logo, .page-subtitle { display: none !important; }
}
