/* 모바일 전용 (max-width: 768px). 데스크톱 style.css는 수정하지 않음 */

@media (max-width: 768px) {
  :root {
    --mobile-tab-bar-h: 58px;
    --mobile-header-h: 152px;
  }

  html,
  body {
    height: auto;
    overflow: auto;
    -webkit-text-size-adjust: 100%;
  }

  html.display-timeline,
  html.display-timeline body {
    height: auto;
    overflow: auto;
  }

  html.display-timeline .page {
    height: auto;
    min-height: 100dvh;
    overflow: visible;
  }

  html.display-timeline .layout {
    flex: none;
    min-height: auto;
    overflow: visible;
  }

  html.display-timeline .calendar-column {
    overflow: visible;
    max-height: none;
  }

  .page {
    height: auto;
    min-height: 100dvh;
    overflow: visible;
    padding:
      max(8px, env(safe-area-inset-top))
      max(10px, env(safe-area-inset-right))
      calc(var(--mobile-tab-bar-h) + env(safe-area-inset-bottom, 0px) + 10px)
      max(10px, env(safe-area-inset-left));
  }

  .layout {
    display: flex !important;
    flex-direction: column;
    grid-template-columns: unset !important;
    align-items: stretch !important;
    gap: 10px;
    min-height: auto;
    width: 100%;
    max-width: 100%;
  }

  html.display-timeline .layout {
    align-items: stretch !important;
  }

  .sidebar {
    display: flex;
    flex-direction: column;
    order: 1;
    width: 100%;
    max-width: 100%;
    min-width: 0;
    height: 0;
    min-height: 0;
    gap: 0;
    max-height: none;
    overflow: visible;
    pointer-events: none;
  }

  .header--sidebar {
    position: fixed;
    top: max(8px, env(safe-area-inset-top));
    left: max(10px, env(safe-area-inset-left));
    right: max(10px, env(safe-area-inset-right));
    width: auto;
    z-index: 50;
    margin-bottom: 0;
    padding: 10px 12px;
    pointer-events: auto;
    box-shadow: 0 4px 18px rgba(26, 39, 63, 0.12);
  }

  body.is-mobile .layout {
    padding-top: calc(var(--mobile-header-h, 152px) + max(8px, env(safe-area-inset-top)) + 6px);
  }

  html.is-mobile-view .layout {
    padding-top: calc(var(--mobile-header-h, 152px) + max(8px, env(safe-area-inset-top)) + 6px);
  }

  html.is-mobile-view .header--sidebar {
    position: fixed;
    top: max(8px, env(safe-area-inset-top));
    left: max(10px, env(safe-area-inset-left));
    right: max(10px, env(safe-area-inset-right));
    width: auto;
    z-index: 50;
  }

  html.is-mobile-view .sidebar {
    height: 0;
    min-height: 0;
    overflow: visible;
    pointer-events: none;
  }

  body.is-mobile .highlights-panel,
  body.is-mobile .legend-panel {
    pointer-events: auto;
  }

  .header--sidebar .header-top-row {
    gap: 8px;
  }

  .header--sidebar .header-top-row .view-toggle {
    margin-left: auto;
  }

  .header--sidebar .page-title {
    font-size: 1.28rem;
    line-height: 1.15;
  }

  .header--sidebar .header-auth .auth-login-btn {
    height: 24px;
    min-height: 24px;
    font-size: 0.7rem;
    padding: 0 9px;
  }

  .header--sidebar .header-auth .auth-user-avatar {
    width: 26px;
    height: 26px;
  }

  .header--sidebar .header-month .month-title {
    font-size: 1.5rem;
  }

  .header--sidebar .header-month .nav-btn,
  .nav-btn {
    width: 44px;
    height: 44px;
    font-size: 0.85rem;
  }

  .view-toggle-btn {
    min-height: 40px;
    padding: 8px 14px;
    font-size: 0.82rem;
  }

  .header--sidebar .view-toggle {
    width: auto;
    flex-shrink: 0;
  }

  .calendar-column {
    order: 2;
    min-height: 0;
    height: auto;
    width: 100%;
    max-width: 100%;
    min-width: 0;
    align-self: stretch;
    display: flex;
    flex-direction: column;
    gap: 10px;
  }

  html.display-timeline .calendar-column {
    width: 100% !important;
    align-self: stretch;
  }

  .legend-panel {
    margin-top: 0;
    width: 100%;
  }

  .highlights-panel {
    width: 100%;
  }

  /* 데스크톱 그리드 대신 모바일 아젠다만 표시 */
  .calendar-wrap {
    display: none !important;
  }

  #mobile-agenda {
    display: flex !important;
    width: 100%;
  }

  #mobile-agenda.hidden {
    display: flex !important;
  }

  .mobile-agenda-empty {
    margin: 0;
    padding: 24px 16px;
    text-align: center;
    color: var(--muted);
    font-size: 0.9rem;
    font-weight: 600;
  }

  .mobile-agenda-empty--error {
    color: #c0392b;
  }

  /* 달력 탭: 일정 + 카테고리(아래) */
  body.is-mobile.mobile-tab-calendar .highlights-panel {
    display: none;
  }

  body.is-mobile.mobile-tab-calendar .legend-panel {
    display: block;
  }

  body.is-mobile.mobile-tab-highlights .calendar-column,
  body.is-mobile.mobile-tab-highlights .legend-panel {
    display: none;
  }

  body.is-mobile.mobile-tab-calendar .calendar-column {
    display: flex;
    flex: none;
    min-height: auto;
  }

  body.is-mobile.mobile-tab-highlights .highlights-panel {
    display: block;
    min-height: calc(100dvh - 200px - var(--mobile-tab-bar-h) - env(safe-area-inset-bottom, 0px));
  }

  body.is-mobile.mobile-tab-highlights .hl-text {
    font-size: 0.88rem;
    line-height: 1.5;
  }

  body.is-mobile.mobile-tab-highlights .hl-card {
    min-height: 64px;
  }

  body.is-mobile.mobile-tab-calendar .legend-panel {
    padding: 12px 14px;
  }

  body.is-mobile .font-credit {
    display: block;
    pointer-events: auto;
    margin: 10px 14px 0;
    padding-bottom: 4px;
  }

  body.is-mobile.mobile-tab-highlights .font-credit {
    margin-top: 14px;
  }

  body.is-mobile.mobile-tab-calendar .legend-panel .panel-head h3 {
    font-size: 0.9rem;
  }

  body.is-mobile.mobile-tab-calendar .legend {
    gap: 8px;
  }

  body.is-mobile.mobile-tab-calendar .legend-item {
    font-size: 0.78rem;
    padding: 6px 10px 6px 6px;
  }

  /* ── 모바일 일정 목록 (아젠다) ── */
  .mobile-agenda {
    overflow: visible;
    display: flex;
    flex-direction: column;
    gap: 10px;
    padding: 2px 0 4px;
  }

  .mobile-agenda--week {
    gap: 12px;
  }

  .mobile-agenda--week .mobile-agenda-day {
    border-radius: 16px;
  }

  .mobile-agenda--week .mobile-agenda-head {
    padding: 12px 16px;
  }

  .mobile-agenda--week .mobile-agenda-date-num {
    font-size: 1.08rem;
  }

  .mobile-agenda--week .mobile-agenda-dow {
    font-size: 0.88rem;
    font-weight: 800;
    color: var(--text);
  }

  .mobile-agenda--week .mobile-agenda-day.sun .mobile-agenda-dow {
    color: var(--sun);
  }

  .mobile-agenda--week .mobile-agenda-day.sat .mobile-agenda-dow {
    color: var(--sat);
  }

  .mobile-agenda-day {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    box-shadow: var(--shadow-sm);
    overflow: hidden;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-agenda-day.sun .mobile-agenda-date-num {
    color: var(--sun);
  }

  .mobile-agenda-day.sat .mobile-agenda-date-num {
    color: var(--sat);
  }

  .mobile-agenda-day--today {
    border: 2px solid var(--today-border);
    box-shadow:
      0 0 0 3px rgba(139, 125, 200, 0.18),
      0 6px 20px rgba(139, 125, 200, 0.22);
    background: var(--today-bg);
  }

  .mobile-agenda-day--today .mobile-agenda-head {
    background: linear-gradient(
      180deg,
      rgba(139, 125, 200, 0.22) 0%,
      rgba(139, 125, 200, 0.08) 100%
    );
    border-bottom: 1px solid rgba(139, 125, 200, 0.25);
  }

  .mobile-agenda-day--today .mobile-agenda-date-num {
    font-size: 1.14rem;
    color: var(--today-border);
  }

  .mobile-agenda-day--today .mobile-agenda-dow {
    color: var(--today-border);
    font-weight: 800;
  }

  .mobile-agenda-day--today .today-badge {
    margin-left: auto;
    font-size: 0.72rem;
    padding: 4px 10px;
    animation: today-badge-blink 1.5s ease-in-out infinite;
  }

  .mobile-agenda-day--empty {
    opacity: 0.72;
  }

  .mobile-agenda-head {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    background: linear-gradient(180deg, var(--surface-2) 0%, var(--surface) 100%);
    border-bottom: 1px solid rgba(0, 0, 0, 0.05);
  }

  .mobile-agenda-day--empty .mobile-agenda-head {
    border-bottom: none;
    padding: 12px 14px;
  }

  .mobile-agenda-date-num {
    font-size: 1rem;
    font-weight: 800;
    letter-spacing: -0.02em;
    line-height: 1;
  }

  .mobile-agenda-dow {
    font-size: 0.82rem;
    font-weight: 700;
    color: var(--muted);
  }

  .mobile-agenda-bangon {
    display: inline-flex;
    align-items: center;
    margin-left: auto;
    max-width: 52%;
    padding: 4px 9px;
    border-radius: 999px;
    background: linear-gradient(180deg, #f7faff 0%, #edf2fa 100%);
    border: 1px solid color-mix(in srgb, var(--accent) 24%, #d5dde8);
    box-shadow:
      0 1px 0 rgba(255, 255, 255, 0.92) inset,
      0 1px 2px rgba(26, 39, 63, 0.05);
    font-size: 0.74rem;
    font-weight: 800;
    letter-spacing: -0.03em;
    color: color-mix(in srgb, var(--accent) 82%, #1a2233);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .mobile-agenda-all-day {
    padding: 8px 10px;
    text-align: center;
    font-size: 0.95rem;
    font-weight: 800;
    color: var(--chip-text, var(--text));
    background: var(--chip-bg, var(--surface-2));
  }

  .mobile-agenda-slots {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
  }

  .mobile-agenda-slot {
    display: flex;
    align-items: stretch;
    background: var(--chip-bg, #fff);
    color: var(--chip-text, #333);
  }

  .mobile-agenda-duration {
    flex: 0 0 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.72rem;
    font-weight: 800;
    color: color-mix(in srgb, var(--chip-text, #333) 88%, #000);
    background: color-mix(in srgb, #fff 55%, var(--chip-bg, #fff));
    border-right: 1px dashed color-mix(in srgb, var(--chip-text, #333) 16%, transparent);
  }

  .mobile-agenda-duration--all-day {
    background-image: none;
  }

  .mobile-agenda-slot + .mobile-agenda-slot {
    border-top: 1px solid color-mix(in srgb, var(--chip-text, #333) 14%, transparent);
  }

  .mobile-agenda-slot-body {
    min-width: 0;
    display: flex;
    align-items: flex-start;
    gap: 8px;
    padding: 5px 10px;
  }

  .mobile-agenda-cat {
    flex: 0 0 auto;
    margin-top: 0.1em;
    font-size: 0.65rem;
    font-weight: 700;
    line-height: 1.25;
    padding: 3px 8px;
    border-radius: 999px;
    white-space: nowrap;
    background: color-mix(in srgb, var(--chip-text, #333) 12%, var(--chip-bg, #fff));
    border: 1px solid color-mix(in srgb, var(--chip-text, #333) 18%, transparent);
  }

  .mobile-agenda-title {
    flex: 1;
    min-width: 0;
    margin: 0;
    padding-top: 0.05em;
    font-family: var(--font-schedule);
    font-size: 0.94rem;
    font-weight: normal;
    line-height: 1.45;
    white-space: pre-line;
    word-break: keep-all;
    overflow-wrap: break-word;
  }

  .mobile-agenda-members {
    margin: 2px 0 0;
    font-size: 0.72rem;
    font-weight: 700;
    line-height: 1.3;
    color: color-mix(in srgb, var(--chip-text, #333) 75%, #000);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .mobile-agenda--week .mobile-agenda-title {
    font-size: 1rem;
  }

  @media (hover: none) {
    .mobile-agenda-day:active {
      opacity: 0.92;
    }

    .highlight-item:hover .hl-card {
      transform: none;
      box-shadow: inherit;
    }

    .highlight-item--today:hover .hl-card {
      border-color: var(--today-border);
      box-shadow: 0 2px 10px rgba(139, 125, 200, 0.16);
    }
  }

  /* 하단 탭 */
  .mobile-tab-bar {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 60;
    display: flex;
    align-items: stretch;
    min-height: var(--mobile-tab-bar-h);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    background: rgba(255, 255, 255, 0.96);
    border-top: 1px solid var(--border);
    box-shadow: 0 -6px 24px rgba(26, 39, 63, 0.08);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
  }

  .mobile-tab-bar.hidden {
    display: none;
  }

  body.modal-open .mobile-tab-bar {
    display: none;
  }

  .mobile-tab {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: var(--mobile-tab-bar-h);
    padding: 8px 6px 6px;
    border: none;
    background: transparent;
    color: var(--muted);
    font-family: inherit;
    font-size: 0.8rem;
    font-weight: 700;
    line-height: 1.2;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
  }

  .mobile-tab.active {
    color: var(--accent);
    box-shadow: inset 0 2px 0 var(--highlight);
  }

  /* 모달 · 바텀시트 */
  .day-modal:not(.hidden),
  .slot-edit-modal:not(.hidden) {
    display: flex;
    align-items: flex-end;
    justify-content: center;
    padding: 0;
  }

  .day-modal-card,
  .slot-edit-card {
    width: 100%;
    max-width: none;
    max-height: min(92dvh, 720px);
    border-radius: 18px 18px 0 0;
    margin: 0;
  }

  .day-modal-header {
    padding: 18px 18px 14px;
  }

  .day-modal-body {
    padding: 10px 14px 14px;
  }

  .day-modal-bangon-wrap {
    margin-bottom: 10px;
    padding-bottom: 10px;
  }

  .bangon-picker {
    padding: 9px 10px;
    gap: 7px;
  }

  .bangon-quick-actions {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .bangon-quick-btn {
    min-height: 42px;
    font-size: 0.76rem;
  }

  .day-modal-actions {
    flex-direction: column;
    padding: 12px 16px calc(16px + env(safe-area-inset-bottom, 0px));
  }

  .day-modal-actions .btn {
    width: 100%;
    min-height: 44px;
  }

  .slot-edit-card {
    padding: 20px 18px calc(18px + env(safe-area-inset-bottom, 0px));
  }

  .slot-edit-actions {
    flex-wrap: wrap;
    gap: 8px;
  }

  .slot-edit-actions .btn {
    min-height: 44px;
    flex: 1 1 calc(50% - 4px);
  }

  .slot-edit-actions #slot-edit-save {
    flex-basis: 100%;
  }

  .ctx-menu {
    min-width: 180px;
  }

  .ctx-menu button {
    min-height: 44px;
    padding: 10px 14px;
    font-size: 0.88rem;
  }

  html.display-timeline .page {
    padding-bottom: calc(var(--mobile-tab-bar-h) + env(safe-area-inset-bottom, 0px) + 10px);
  }
}

@media (max-width: 400px) {
  .header--sidebar .header-month .month-title {
    font-size: 1.3rem;
  }

  .mobile-agenda-title {
    font-size: 0.88rem;
  }
}
