/* ─── Responsive ─── */

/* ── Narrow desktop / tablet (≤ 1024px) ── */
@media (max-width: 1024px) {

  /* Footer: hide on small screens to save space */
  #app-footer {
    display: none;
  }

  /* Topbar: wrap to two rows */
  #topbar {
    height: auto;
    flex-wrap: wrap;
    padding: 8px 12px;
    gap: 6px;
  }

  .top-left {
    flex: 0 0 auto;
    gap: 8px;
  }

  #topbar h1 {
    font-size: .95rem;
    flex-shrink: 0;
  }

  #search-box {
    flex: 1;
    width: auto;
    min-width: 0;
    font-size: .75rem;
    padding: 5px 24px 5px 10px;
    border-radius: 8px;
  }

  .search-input-wrap {
    flex: 1;
    width: auto;
    min-width: 0;
  }

  .top-right {
    flex-shrink: 0;
  }

  #domain-filter-area {
    order: 3;
    flex: 0 0 100%;
    margin: 0;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    gap: 6px;
  }

  #domain-filter-area::-webkit-scrollbar {
    display: none;
  }

  #topic-input-wrap {
    min-width: 160px;
    max-width: 240px;
    flex-shrink: 0;
  }

  #topic-input {
    font-size: .72rem;
    padding: 5px 24px 5px 10px;
  }

  #plan-ride-btn {
    font-size: .72rem;
    padding: 5px 12px;
  }

  #meetup-btn {
    font-size: .72rem;
    padding: 5px 12px;
  }

  #book-btn {
    font-size: .72rem;
    padding: 5px 10px;
  }

  /* Sidebar: compact at narrow desktop */
  #sidebar {
    left: 6px;
    width: 44px;
  }

  .floor-selector {
    padding: 4px;
    gap: 3px;
    border-radius: 10px;
  }

  .floor-btn {
    width: 34px;
    height: 24px;
    font-size: 7.5px;
    border-radius: 6px;
  }

  #zoom-hint {
    display: none;
  }

  /* Modals / overlays: bottom sheets */
  #ride-overlay {
    align-items: flex-end;
  }

  #ride-modal {
    width: 100vw;
    max-width: 100vw;
    max-height: 92vh;
    border-radius: 14px 14px 0 0;
  }

  #book-overlay {
    align-items: flex-end;
  }

  #book-panel {
    width: 100vw;
    max-width: 100vw;
    max-height: 92vh;
    border-radius: 14px 14px 0 0;
  }

  #meetup-overlay {
    align-items: flex-end;
  }

  #meetup-panel {
    width: 100vw;
    max-width: 100vw;
    max-height: 92vh;
    border-radius: 14px 14px 0 0;
  }

  #panel-overlay {
    width: min(400px, 100vw);
    top: 75px;
  }

  #concurrent-panel {
    width: min(400px, 100vw);
  }

  /* Search results: constrained width */
  #search-results {
    width: min(400px, calc(100vw - 16px));
  }
}

/* ── Mobile (≤ 768px) ── */
@media (max-width: 768px) {

  /* Footer: restore as a compact floating chip on phones */
  #app-footer {
    display: flex;
    left: auto;
    right: 8px;
    bottom: calc(8px + env(safe-area-inset-bottom));
    transform: none;
    width: max-content;
    max-width: calc(100vw - 24px);
    height: auto;
    min-height: 22px;
    justify-content: center;
    padding: 6px 10px;
    border: 1px solid rgba(44, 78, 138, 0.42);
    border-radius: 999px;
    background: rgba(19, 41, 75, 0.78);
    box-shadow: 0 10px 26px rgba(0, 0, 0, 0.24);
    font-size: .58rem;
    line-height: 1.2;
    white-space: nowrap;
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
  }

  body.sim-active #app-footer {
    right: 6px;
    bottom: calc(6px + env(safe-area-inset-bottom));
    padding: 5px 8px;
    font-size: .54rem;
    opacity: .76;
    background: rgba(19, 41, 75, 0.62);
  }

  /* Topbar tighter */
  #topbar {
    padding: 6px 10px;
    gap: 5px;
  }

  #topbar h1 {
    font-size: .88rem;
  }

  #search-box {
    font-size: .72rem;
    padding: 5px 24px 5px 8px;
  }

  /* Auth section: switch back to row on mobile */
  #auth-section {
    flex-direction: row;
    align-items: center;
    gap: 8px;
  }

  #auth-legal {
    display: none;
  }

  #user-chip {
    max-width: 124px;
    padding: 4px 8px;
    gap: 6px;
  }

  #user-chip span {
    display: block;
    max-width: 78px;
    font-size: .72rem;
  }

  /* Action buttons compact */
  #topic-input-wrap {
    min-width: 130px;
    max-width: 170px;
  }

  #plan-ride-btn {
    font-size: .68rem;
    padding: 5px 10px;
  }

  #meetup-btn {
    font-size: .68rem;
    padding: 5px 10px;
  }

  #book-btn {
    font-size: .68rem;
    padding: 4px 8px;
  }

  /* Calendar Book: compact floating card instead of full-width sheet */
  #book-overlay {
    align-items: flex-start;
    justify-content: center;
    padding: calc(96px + env(safe-area-inset-top)) 12px calc(86px + env(safe-area-inset-bottom));
    background: rgba(5, 15, 35, 0.2);
  }

  #book-panel {
    width: min(92vw, 430px);
    max-width: 430px;
    max-height: min(74vh, calc(100vh - 190px));
    border-radius: 18px;
    box-shadow: 0 18px 48px rgba(0, 0, 0, .3);
  }

  #book-panel-hdr {
    align-items: flex-start;
    flex-wrap: wrap;
    gap: 10px;
    padding: 12px 14px;
  }

  #book-panel-hdr h2 {
    font-size: .88rem;
  }

  #book-panel-meta {
    order: 3;
    width: 100%;
    font-size: .66rem;
    line-height: 1.35;
  }

  #book-download-btn {
    margin-left: auto;
    padding: 5px 10px;
    font-size: .7rem;
  }

  #book-day-tabs {
    padding: 8px 12px;
  }

  #book-day-tabs-hint {
    right: 8px;
    font-size: .58rem;
    padding: 3px 8px;
  }

  #book-schedule {
    padding: 10px 12px;
  }

  /* Ride Plan: match the compact Calendar Book card footprint */
  #ride-overlay {
    align-items: flex-start;
    justify-content: center;
    padding: calc(96px + env(safe-area-inset-top)) 12px calc(86px + env(safe-area-inset-bottom));
    background: rgba(5, 15, 35, 0.2);
  }

  #ride-modal {
    width: min(92vw, 430px);
    max-width: 430px;
    max-height: min(74vh, calc(100vh - 190px));
    border-radius: 18px;
    box-shadow: 0 18px 48px rgba(0, 0, 0, .3);
  }

  /* Domain dropdown: anchored close to the topic field */
  #domain-dropdown {
    right: auto;
    bottom: auto;
    max-height: min(320px, 48vh);
    border-radius: 16px;
    box-shadow: 0 14px 36px rgba(0, 0, 0, .38);
  }

  /* Sidebar compact */
  #sidebar {
    left: 6px;
    width: 44px;
  }

  .floor-selector {
    padding: 4px;
    gap: 3px;
    border-radius: 10px;
  }

  .floor-btn {
    width: 34px;
    height: 24px;
    font-size: 7.5px;
    border-radius: 6px;
  }

  /* Zoom controls */
  #zoom-controls {
    bottom: calc(14px + env(safe-area-inset-bottom));
    left: 6px;
  }

  /* Touch targets: min 44×44px for zoom buttons */
  .zoom-btn {
    width: 44px;
    height: 44px;
    font-size: 1rem;
  }

  .zoom-btn.tutorial-trigger {
    width: 44px;
    height: 44px;
  }

  /* Search results full width */
  #search-results {
    width: calc(100vw - 16px);
    left: 8px;
    right: 8px;
    max-height: 50vh;
  }

  /* Papers side panel: full screen, below topbar */
  #panel-overlay {
    width: 100vw;
    top: 80px;
    border-left: none;
    border-radius: 0;
  }

  #ride-modal-hdr {
    align-items: flex-start;
    flex-wrap: wrap;
    gap: 10px;
    padding: 12px 14px;
  }

  #ride-modal-hdr h2 {
    font-size: .85rem;
  }

  #ride-summary {
    order: 3;
    width: 100%;
    font-size: .66rem;
    line-height: 1.35;
  }

  #ride-save-wrap {
    margin-left: auto;
  }

  #ride-schedule {
    padding: 10px 12px;
  }

  .ride-slot-time {
    width: 52px;
    font-size: .55rem;
  }

  .ride-slot.archived-slot {
    margin-left: 68px;
  }

  #book-panel-hdr {
    flex-wrap: wrap;
    gap: 8px;
    padding: 12px;
  }

  #book-schedule {
    padding: 12px;
  }

  #meetup-hdr {
    flex-wrap: wrap;
    gap: 6px;
    padding: 10px 12px;
  }

  #meetup-hdr h2 {
    font-size: .8rem;
  }

  #meetup-topic-bar {
    width: 100%;
    order: 3;
  }

  #meetup-match-summary {
    font-size: .62rem;
  }

  #meetup-timeline {
    padding: 12px;
    gap: 14px;
  }

  #meetup-cluster-papers {
    padding: 12px;
  }

  #meetup-cluster-actions {
    padding: 10px 12px;
    gap: 8px;
  }

  #meetup-add-cluster-btn,
  #meetup-navigate-btn {
    font-size: .72rem;
    padding: 8px 12px;
  }

  /* Simulation */
  .sim-marker,
  .sim-marker-inner {
    width: 84px;
    height: 84px;
  }

  .sim-marker-img {
    width: 72px;
    height: 72px;
  }

  #sim-toggle {
    font-size: .72rem;
    padding: 7px 14px 5px;
    border-radius: 10px;
    bottom: calc(56px + env(safe-area-inset-bottom));
  }

  #sim-controls {
    padding: 5px 8px;
    gap: 6px;
    border-radius: 10px;
    bottom: 20px;
    width: auto;
    max-width: calc(100vw - 16px);
  }

  #sim-controls button {
    width: 26px;
    height: 26px;
    font-size: .72rem;
    border-radius: 7px;
  }

  #sim-progress {
    flex: 0 0 80px;
    width: 80px;
  }

  #sim-step-info {
    max-width: 78px;
    font-size: 8px;
  }

  #sim-info-card {
    max-width: calc(100vw - 20px);
    width: calc(100vw - 20px);
    bottom: 64px;
  }

  /* Toast */
  #add-toast {
    bottom: 14px;
    max-width: calc(100vw - 32px);
    font-size: .72rem;
  }

  /* Tutorial */
  #tut-overlay {
    padding: 0 10px calc(18px + env(safe-area-inset-bottom));
    justify-content: center;
  }

  #tut-card {
    width: min(100%, 292px);
    max-width: 292px;
    border-radius: 12px;
  }

  #tut-top {
    gap: 8px;
    padding: 8px 12px 0;
  }

  #tut-step-label {
    font-size: .58rem;
  }

  #tut-skip-btn {
    font-size: .64rem;
  }

  #tut-body {
    padding: 8px 12px 10px;
    gap: 2px;
  }

  #tut-emoji {
    font-size: 1.3rem;
  }

  .tut-media-image {
    width: 52px;
    height: 52px;
  }

  #tut-title {
    font-size: .78rem;
    margin-top: 2px;
  }

  #tut-desc {
    font-size: .66rem;
    line-height: 1.42;
  }

  #tut-dots-row {
    gap: 4px;
    padding: 0 12px 6px;
  }

  .tut-dot {
    width: 4px;
    height: 4px;
  }

  #tut-bottom {
    gap: 6px;
    padding: 0 12px 10px;
  }

  #tut-prev-btn {
    padding: 5px 9px;
    font-size: .7rem;
  }

  #tut-next-btn {
    padding: 6px 10px;
    font-size: .72rem;
  }

  /* Sim-active adjustments */
  body.sim-active #zoom-controls {
    bottom: calc(68px + env(safe-area-inset-bottom));
  }

  body.sim-active #sidebar {
    transform: translateY(calc(-50% - 36px));
  }
}

/* ── Compact bottom controls for narrow or short viewports ── */
@media (max-width: 1024px), (max-height: 560px) {
  #zoom-controls {
    left: 8px;
    bottom: calc(10px + env(safe-area-inset-bottom));
    gap: 4px;
  }

  #zoom-btns {
    gap: 3px;
    padding: 3px;
    border-radius: 8px;
  }

  .zoom-btn {
    width: 22px;
    height: 22px;
    border-radius: 5px;
    font-size: .76rem;
  }

  .zoom-btn.tutorial-trigger {
    width: 24px;
    height: 24px;
    border-radius: 7px;
  }

  .zoom-btn.tutorial-trigger svg {
    width: 12px;
    height: 12px;
  }

  #sim-toggle {
    bottom: calc(40px + env(safe-area-inset-bottom));
    font-size: .66rem;
    padding: 6px 12px 4px;
    border-radius: 10px;
    gap: 0;
  }

  .sim-toggle-sub {
    font-size: .48rem;
  }

  #sim-controls {
    bottom: calc(18px + env(safe-area-inset-bottom));
    padding: 4px 7px;
    gap: 5px;
  }

  #sim-controls button {
    width: 24px;
    height: 24px;
  }

  #sim-progress {
    flex: 0 0 72px;
    width: 72px;
  }

  #sim-step-info {
    max-width: 72px;
  }

  #sim-info-card {
    bottom: calc(56px + env(safe-area-inset-bottom));
  }

  body.sim-active #zoom-controls {
    bottom: calc(52px + env(safe-area-inset-bottom));
  }
}
