/* Shared responsive layout — ask, voice-rag, alolama */

html {
  -webkit-text-size-adjust: 100%;
}

body {
  overflow-x: hidden;
}

/* Mobile: stop page shift / rubber-band when touching and dragging */
@media (max-width: 980px) {
  html {
    overscroll-behavior: none;
    width: 100%;
    max-width: 100vw;
  }

  body {
    overflow-x: hidden;
    overscroll-behavior: none;
    width: 100%;
    max-width: 100vw;
  }

  /* Ask / alolama: scroll inside the page, not the viewport bounce */
  body:not(.alnasir-voice-body) {
    position: relative;
    min-height: 100dvh;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  body:not(.alnasir-voice-body) .container {
    overflow-x: hidden;
    max-width: 100%;
  }

  /* Voice: fixed header + fixed mic bar + scrollable chat (iOS-safe) */
  html.alnasir-voice-root {
    height: 100%;
    height: 100dvh;
    overflow: hidden;
  }

  body.alnasir-voice-body {
    position: fixed;
    inset: 0;
    width: 100%;
    height: var(--voice-vh, 100dvh);
    max-height: var(--voice-vh, 100dvh);
    overflow: hidden;
    overscroll-behavior: none;
    display: block;
    touch-action: pan-y;
  }

  body.alnasir-voice-body .top-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 40;
    margin: 0;
  }

  body.alnasir-voice-body .tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    padding: 4px;
    gap: 4px;
  }

  body.alnasir-voice-body .tabs .tab-btn {
    padding: 7px 10px;
    font-size: 11px;
    flex: 0 0 auto;
  }

  body.alnasir-voice-body .alnasir-voice-main {
    position: fixed;
    top: var(--voice-header-h, 100px);
    bottom: var(--voice-footer-h, 120px);
    left: 0;
    right: 0;
    width: 100%;
    max-width: 720px;
    margin: 0 auto;
    min-height: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    padding: 6px 10px 4px;
    box-sizing: border-box;
  }

  body.alnasir-voice-body .alnasir-chat-shell {
    flex: 1 1 0%;
    min-height: 0;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    margin-top: 4px;
    touch-action: pan-y;
  }

  body.alnasir-voice-body .alnasir-chat-thread {
    flex: 1 1 0%;
    min-height: 0;
    height: 100%;
    overflow-x: hidden;
    overflow-y: scroll;
    overscroll-behavior-y: contain;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
  }

  body.alnasir-voice-body .alnasir-voice-footer {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 50;
    margin-top: 0;
    touch-action: manipulation;
    background: #faf8f6;
    border-top: 1px solid rgba(11, 61, 46, 0.16);
    box-shadow: 0 -8px 24px rgba(15, 23, 42, 0.08);
    padding: 6px 10px max(10px, env(safe-area-inset-bottom, 0px));
    box-sizing: border-box;
  }

  /* Save space: mic label + recording hint are enough on phones */
  body.alnasir-voice-body .alnasir-voice-steps,
  body.alnasir-voice-body .alnasir-footer-hint {
    display: none !important;
  }

  body.alnasir-voice-body .alnasir-recording-send-hint {
    margin-bottom: 6px;
    padding: 8px 10px;
    font-size: 13px;
  }

  body.alnasir-voice-body .alnasir-mic-icon {
    width: 64px;
    height: 64px;
  }

  body.alnasir-voice-body .alnasir-mic-label {
    font-size: 13px;
    line-height: 1.35;
    max-width: 20rem;
    margin-inline: auto;
  }

  .tabs {
    touch-action: pan-x;
    overscroll-behavior-x: contain;
  }

  button,
  .tab-btn,
  .alnasir-mic-btn,
  .alnasir-playback-toggle,
  .alnasir-playback-stop {
    touch-action: manipulation;
  }
}

/* —— Header & navigation (all pages) —— */
@media (max-width: 980px) {
  body:not(.alnasir-voice-body) .top-header {
    position: sticky;
    top: 0;
    z-index: 200;
    margin-bottom: 10px;
  }

  .top-header-inner {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    padding: max(10px, env(safe-area-inset-top, 0px)) 14px 10px;
  }

  .brand {
    width: 100%;
    min-width: 0;
    align-items: center;
    gap: 10px;
  }

  .brand--banner {
    max-width: 100%;
  }

  .brand-text {
    flex: 1;
    min-width: 0;
  }

  .brand-text h1 {
    font-size: clamp(16px, 4.2vw, 20px);
    line-height: 1.25;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
    word-break: break-word;
  }

  .brand-text p {
    font-size: 12px;
    line-height: 1.4;
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    overflow: hidden;
    max-width: none;
  }

  .logo {
    width: 64px;
    height: 64px;
    flex-shrink: 0;
    padding: 4px;
  }

  .tabs {
    width: 100%;
    max-width: 100%;
    flex-wrap: nowrap;
    justify-content: flex-start;
    align-items: stretch;
    gap: 6px;
    padding: 6px;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: x proximity;
    scrollbar-width: thin;
    scrollbar-color: rgba(11, 61, 46, 0.35) transparent;
  }

  .tabs::-webkit-scrollbar {
    height: 4px;
  }

  .tabs::-webkit-scrollbar-thumb {
    background: rgba(11, 61, 46, 0.35);
    border-radius: 999px;
  }

  .tabs .tab-btn {
    flex: 0 0 auto;
    min-width: 0;
    max-width: min(220px, 78vw);
    white-space: nowrap;
    text-align: center;
    font-size: 12px;
    line-height: 1.35;
    padding: 9px 12px;
    scroll-snap-align: start;
  }
}

@media (max-width: 720px) {
  .top-header {
    margin-bottom: 8px;
    box-shadow: 0 6px 18px rgba(15, 23, 42, 0.08);
  }

  .top-header-inner {
    padding-inline: 12px;
    gap: 8px;
  }

  .logo {
    width: 56px;
    height: 56px;
  }

  .brand-text h1 {
    font-size: 16px;
  }

  .brand-text p {
    font-size: 11px;
  }

  .tabs .tab-btn {
    font-size: 11px;
    padding: 8px 10px;
  }

  .container {
    padding: 10px 12px max(24px, env(safe-area-inset-bottom, 0px));
    box-sizing: border-box;
  }

  /* Ask page cards & forms */
  .chat-card,
  .ingest-card,
  .data-card,
  .answer-card,
  .sources-card,
  .resources-card,
  .eval-card {
    padding: 14px;
    margin-bottom: 12px;
  }

  .action-row,
  .eval-actions,
  .inline-actions {
    flex-direction: column;
    align-items: stretch;
    width: 100%;
  }

  .action-row button,
  .eval-actions button,
  .inline-actions button {
    width: 100%;
    box-sizing: border-box;
  }

  textarea,
  input[type="url"],
  input[type="password"],
  input[type="text"] {
    font-size: 16px; /* avoids iOS zoom on focus */
  }

  .fanar-voice-row {
    flex-direction: column;
    align-items: stretch;
  }

  .fanar-voice-actions {
    width: 100%;
    justify-content: flex-start;
  }

  .fanar-voice-actions button {
    flex: 1 1 auto;
  }

  .eval-result-q {
    min-width: 0;
    width: 100%;
  }

  .history-delete-btn {
    margin-inline-start: 0;
    align-self: flex-start;
  }

  .eval-result-card summary {
    flex-direction: column;
    align-items: flex-start;
  }

  .tts-settings-card {
    padding: 14px 16px;
  }

  .answer-audio-row {
    flex-direction: column;
    align-items: stretch;
  }

  .answer-audio-row audio {
    width: 100%;
    max-width: 100%;
  }

  /* Alolama scholars */
  .page-lead {
    font-size: 14px;
    margin-bottom: 16px;
  }

  .scholar-card-content {
    padding: 16px 14px 18px;
  }

  .scholar-hero {
    min-height: 140px;
    max-height: min(200px, 32vh);
    padding: 12px 14px 14px;
  }

  .record-item {
    padding: 12px;
  }

  .record-transcript {
    max-height: 240px;
  }
}

@media (max-width: 420px) {
  .top-header-inner {
    padding-inline: 10px;
  }

  .logo {
    width: 48px;
    height: 48px;
    border-radius: 12px;
  }

  .brand {
    gap: 8px;
  }

  .brand-text h1 {
    font-size: 15px;
    -webkit-line-clamp: 2;
  }

  .tabs {
    padding: 5px;
    gap: 5px;
  }

  .tabs .tab-btn {
    font-size: 10px;
    padding: 7px 9px;
    max-width: min(200px, 85vw);
  }
}

@media (max-height: 640px) and (orientation: landscape) {
  .top-header {
    position: sticky;
    top: 0;
  }

  .top-header-inner {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    padding: 6px 12px;
    gap: 8px;
  }

  .brand {
    flex: 1 1 auto;
    min-width: 0;
    max-width: calc(100% - 120px);
  }

  .tabs {
    flex: 1 1 100%;
    order: 3;
  }

  .brand-text p {
    display: none;
  }

  .brand-text h1 {
    -webkit-line-clamp: 1;
    font-size: 15px;
  }

  .logo {
    width: 44px;
    height: 44px;
  }
}

/* Voice page: compact header + full-height main */
@media (max-width: 720px) {
  .alnasir-voice-body .top-header {
    margin-bottom: 0;
  }

  .alnasir-voice-body .alnasir-voice-header .brand-text p {
    display: none;
  }

  .alnasir-voice-main {
    padding-top: 6px;
    padding-inline: max(10px, env(safe-area-inset-left, 0px)) max(10px, env(safe-area-inset-right, 0px));
  }

  body.alnasir-voice-body .alnasir-voice-footer {
    padding-inline: max(6px, env(safe-area-inset-left, 0px)) max(6px, env(safe-area-inset-right, 0px));
  }

  body.alnasir-voice-body .alnasir-voice-steps,
  body.alnasir-voice-body .alnasir-recording-send-hint,
  body.alnasir-voice-body .alnasir-playback-bar {
    max-width: 100%;
  }
}
