/*
 * Mobile-Responsive Styles for Konexion
 * Applied only on smaller viewports (phones/tablets)
 */

/* ============================================
   MOBILE BACKDROP
   ============================================ */

.mobile-backdrop {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 1055;
  opacity: 0;
  transition: opacity 0.3s ease-in-out;
}

@media (max-width: 768px) {
  .mobile-backdrop.visible {
    display: block;
    opacity: 1;
  }
}

/* ============================================
   MOBILE BREAKPOINT: 768px and below
   ============================================ */

@media (max-width: 768px) {

  /* Main App Layout */
  #app {
    padding: 0.75rem !important;
  }

  main#app {
    padding: 1rem !important;
  }

  /* Mobile Sidebar Fixes */
  .app-sidebar {
    padding: 1rem !important;
    padding-top: 5rem !important; /* Extra space at top to avoid hamburger overlap */
    display: block !important; /* Block layout, not flex */
    overflow-y: auto !important;
    overflow-x: hidden !important;
    -webkit-overflow-scrolling: touch !important;
  }

  .app-sidebar h4 {
    display: block !important;
    margin-top: 0 !important;
    margin-bottom: 1rem !important;
    padding-top: 0 !important;
  }

  /* Ensure all nav items visible */
  .app-sidebar .nav {
    display: block !important; /* Use block instead of flex on mobile */
    margin-bottom: 1rem !important;
  }

  .app-sidebar .nav-item {
    display: block !important;
    margin-bottom: 0.25rem !important;
  }

  /* Remove mt-auto on mobile - it causes layout issues */
  .app-sidebar .nav-item.mt-auto {
    margin-top: 1rem !important; /* Fixed margin instead of auto */
    margin-bottom: 0 !important;
  }

  .app-sidebar .nav-link {
    display: flex !important;
    align-items: center !important;
    padding: 0.75rem 1rem !important;
  }

  /* HR separator */
  .app-sidebar hr {
    display: block !important;
    margin: 1rem 0 !important;
  }

  /* Groups section */
  #groupList {
    display: block !important;
    margin-top: 1rem !important;
  }

  #groupContainer {
    display: block !important;
  }

  /* Logout button */
  #logoutBtn {
    display: block !important;
    width: 100% !important;
    margin-bottom: 1rem !important;
  }

  /* Contact List - Convert table to cards */
  .contacts-table {
    display: block;
    border: none;
  }

  .contacts-table thead {
    display: none;
  }

  .contacts-table tbody {
    display: block;
  }

  .contacts-table tr {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto;
    gap: 0.125rem 0.5rem;
    margin-bottom: 0.375rem;
    border: 1px solid #e0e0e0;
    border-radius: 0.375rem;
    padding: 0.5rem 0.625rem;
    background: white;
    cursor: pointer;
    transition: background-color 0.15s;
  }

  .contacts-table tr:active {
    background-color: #f8f9fa;
  }

  .contacts-table td {
    border: none;
    padding: 0;
    text-align: left !important;
    line-height: 1.3;
  }

  /* Hide star toggle column */
  .contacts-table td:nth-child(2) {
    display: none;
  }

  /* Name - top left, bold */
  .contacts-table td:nth-child(1) {
    grid-column: 1;
    grid-row: 1;
    font-size: 0.9375rem;
    font-weight: 600;
    color: #212529;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* Email - hide */
  .contacts-table td:nth-child(3) {
    display: none;
  }

  /* Company & Job - combine on one line with bullet separator */
  .contacts-table td:nth-child(4) {
    grid-column: 1;
    grid-row: 2;
    font-size: 0.75rem;
    color: #6c757d;
    display: inline !important;
    max-width: none !important;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .contacts-table td:nth-child(4):empty {
    display: none !important;
  }

  .contacts-table td:nth-child(4):not(:empty)::before {
    content: "\F1AD  "; /* bi-building icon + space */
    font-family: 'bootstrap-icons';
    font-size: 0.75rem;
    color: #6c757d;
  }

  /* Job title - inline after company with bullet */
  .contacts-table td:nth-child(5) {
    grid-column: 1;
    grid-row: 2;
    font-size: 0.75rem;
    color: #6c757d;
    display: inline !important;
    max-width: none !important;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .contacts-table td:nth-child(5):empty {
    display: none !important;
  }

  /* Add bullet separator before job if company exists */
  .contacts-table td:nth-child(4):not(:empty) ~ td:nth-child(5):not(:empty)::before {
    content: " • ";
    color: #adb5bd;
    font-family: inherit;
  }

  /* Last interaction - top right, compact badge */
  .contacts-table td:nth-child(6) {
    grid-column: 2;
    grid-row: 1 / 3;
    font-size: 0.6875rem;
    align-self: start;
    text-align: right;
  }

  .contacts-table td:nth-child(6) .badge {
    font-size: 0.6875rem;
    padding: 0.2em 0.4em;
    font-weight: 500;
  }

  /* Groups - hide */
  .contacts-table td:nth-child(7) {
    display: none;
  }

  /* Contact Detail View */
  .card {
    border-radius: 0.375rem;
    margin-bottom: 0.75rem;
  }

  .card-body {
    padding: 0.875rem;
  }

  .card-header {
    padding: 0.875rem;
  }

  /* Fix interaction cards overflow */
  .interaction-card,
  .interaction-row,
  .notification-card {
    margin-left: 0 !important;
    margin-right: 0 !important;
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
  }

  /* Ensure interaction cards don't break container */
  #interactionPanel .interaction-card,
  #interactionPanel .card,
  #previewPanel .interaction-card,
  #previewPanel .card {
    max-width: 100% !important;
    box-sizing: border-box !important;
  }

  #interactionPanel .card-body,
  #previewPanel .card-body {
    padding: 0.75rem !important;
    overflow-x: hidden !important;
    overflow-wrap: break-word !important;
    word-wrap: break-word !important;
  }

  /* Fix interaction history content overflow */
  #interactionPanel .card-body > *,
  #previewPanel .card-body > * {
    max-width: 100% !important;
    overflow-wrap: break-word !important;
    word-wrap: break-word !important;
  }

  /* DON'T clip at panel level - let cards constrain themselves */
  #interactionPanel,
  #previewPanel {
    overflow-y: auto !important;
  }

  /* NUCLEAR OPTION: Force entire chain to viewport width */
  #previewPanel > *,
  #previewPanel > .card,
  #previewPanel .card,
  #previewPanel .interaction-history-section,
  #previewPanel .interaction-grid,
  #previewPanel #interactionList {
    width: 100vw !important;
    max-width: 100vw !important;
    min-width: 0 !important;
    margin-left: calc(-1rem) !important;
    margin-right: calc(-1rem) !important;
    padding-left: 1rem !important;
    padding-right: 1rem !important;
    box-sizing: border-box !important;
  }

  /* Specific fix for interaction-row cards */
  .interaction-row,
  .card.interaction-row,
  #interactionList .interaction-row,
  #interactionList .card.interaction-row {
    margin: 0 0 0.75rem 0 !important;
    padding: 0.75rem !important;
    position: relative !important;
    width: 100% !important;
    min-width: 0 !important;
    max-width: none !important;
    box-sizing: border-box !important;
  }

  /* Hide button text on mobile, keep only icons */
  .interaction-row .btn-expandable span {
    display: none !important;
  }

  .interaction-row .position-absolute {
    right: 0.5rem !important;
    top: 0.5rem !important;
    z-index: 10 !important;
  }

  /* Stack buttons vertically to save horizontal space */
  .interaction-row .position-absolute .d-flex {
    flex-direction: column !important;
    gap: 0.25rem !important;
  }

  /* Make buttons icon-only and smaller */
  .interaction-row .btn-sm {
    padding: 0.25rem !important;
    min-width: unset !important;
    width: 28px !important;
    height: 28px !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
  }

  /* Add padding to content area to avoid button overlap */
  .interaction-row > div:not(.position-absolute) {
    padding-right: 3.5rem !important;
  }

  /* FORCE text wrapping on all elements */
  .interaction-row *,
  .interaction-row .mb-1,
  .interaction-row .mb-1 *,
  .interaction-row strong,
  .interaction-row a,
  .interaction-row .text-brand,
  .interaction-row .interaction-contact-link {
    white-space: normal !important;
    overflow-wrap: break-word !important;
    word-break: break-word !important;
    max-width: 100% !important;
  }

  /* Override desktop truncation on mobile */
  .interaction-row .interaction-notes.truncated {
    overflow: visible !important;
    overflow-x: visible !important;
    overflow-y: visible !important;
    max-height: none !important;
  }

  /* Ensure parent containers in contact detail don't overflow */
  .interaction-history-section,
  #notificationsSection,
  #interactionHistorySection {
    max-width: 100% !important;
  }

  /* Contact detail header */
  .contact-detail-header {
    flex-direction: column !important;
    align-items: flex-start !important;
  }

  .contact-detail-header h2 {
    font-size: 1.25rem;
    margin-bottom: 0.625rem;
  }

  .contact-detail-header .btn-group {
    width: 100%;
    margin-top: 0.5rem;
  }

  .contact-detail-header .btn {
    flex: 1;
    font-size: 0.8125rem;
    padding: 0.5rem 0.625rem;
  }

  /* Contact Photo */
  .contact-photo-large {
    width: 100px !important;
    height: 100px !important;
    margin: 0 auto 1rem auto;
    display: block;
  }

  /* Forms */
  .row.g-3 {
    row-gap: 1rem !important;
  }

  .form-control,
  .form-select {
    font-size: 16px !important; /* Prevents zoom on iOS */
    min-height: 48px;
  }

  textarea.form-control {
    min-height: 120px;
  }

  .btn {
    min-height: 44px;
    font-size: 1rem;
    padding: 0.5rem 1rem;
  }

  .btn-sm {
    min-height: 38px;
    font-size: 0.875rem;
  }

  /* Headers with actions */
  .d-flex.justify-content-between.align-items-center {
    flex-direction: column;
    align-items: stretch !important;
    gap: 0.75rem;
  }

  .d-flex.justify-content-between.align-items-center h2,
  .d-flex.justify-content-between.align-items-center h3 {
    margin-bottom: 0;
  }

  .d-flex.justify-content-between.align-items-center .btn,
  .d-flex.justify-content-between.align-items-center .btn-group {
    width: 100%;
  }

  /* Side Panels - Full screen on mobile */
  #reminderPanel,
  #interactionPanel,
  #previewPanel {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    right: 0 !important;
    bottom: 0 !important;
    width: 100% !important;
    max-width: 100% !important;
    padding: 1rem !important;
    z-index: 1060;
    border: none !important;
    transform: translateX(100%);
    transition: transform 0.3s ease-in-out;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    box-sizing: border-box !important;
  }

  #reminderPanel.visible,
  #interactionPanel.visible,
  #previewPanel.visible {
    transform: translateX(0);
    width: 100% !important;
    max-width: 100% !important;
  }

  /* Global Search Panel */
  #globalSearchPanel {
    width: 100% !important;
    left: 0 !important;
    right: 0 !important;
  }

  /* Preview Panel/Floating Modals */
  .floating-modal {
    position: fixed !important;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100% !important;
    max-width: 100% !important;
    height: 100% !important;
    border-radius: 0 !important;
    transform: translateY(100%);
  }

  .floating-modal.visible {
    transform: translateY(0);
  }

  /* Interactions List */
  .interaction-card {
    margin-bottom: 1rem;
    padding: 1rem;
  }

  .interaction-card .d-flex {
    flex-direction: column !important;
    align-items: flex-start !important;
  }

  .interaction-card .text-end {
    text-align: left !important;
    margin-top: 0.5rem;
  }

  /* Reminders List */
  .reminder-item {
    padding: 1rem;
    margin-bottom: 0.75rem;
  }

  /* Filter/Search Bars */
  .input-group {
    flex-wrap: nowrap;
  }

  .input-group .form-control {
    min-width: 0;
  }

  /* Cards */
  .card {
    margin-bottom: 1rem;
  }

  .card-body {
    padding: 1rem;
  }

  /* Tabs */
  .nav-tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .nav-tabs .nav-link {
    white-space: nowrap;
    font-size: 0.9rem;
  }

  /* Badge adjustments */
  .badge {
    padding: 0.35em 0.65em;
    font-size: 0.875rem;
  }

  /* Table responsive wrapper */
  .table-responsive {
    margin-bottom: 1rem;
  }

  /* Statistics/Metrics */
  .col-md-3,
  .col-md-4,
  .col-md-6 {
    margin-bottom: 1rem;
  }

  /* Map View */
  #mapContainer {
    height: 400px !important;
  }

  /* Settings Page */
  .row.mb-3 .col-sm-5,
  .row.mb-3 .col-sm-7 {
    margin-bottom: 0.5rem;
  }

  /* Billing Page */
  .billing-page .card {
    margin-bottom: 1.5rem;
  }

  /* Groups List */
  .group-item {
    padding: 0.75rem;
    margin-bottom: 0.5rem;
  }

  /* Modal Headers */
  .modal-header h5 {
    font-size: 1.25rem;
  }

  /* Sticky Headers */
  .sticky-top {
    position: -webkit-sticky;
    position: sticky;
    top: 0;
    background: white;
    z-index: 20;
    padding: 0.75rem;
    border-bottom: 1px solid #dee2e6;
    margin: -0.75rem -0.75rem 0.75rem -0.75rem;
  }

  /* Contact Sort Dropdown */
  #contactSortDropdown {
    width: 100%;
    margin-bottom: 1rem;
  }

  /* Quill Editor */
  .ql-container {
    font-size: 16px !important;
  }

  .ql-editor {
    min-height: 150px;
    font-size: 16px !important;
  }
}

/* ============================================
   SMALL MOBILE: 576px and below
   ============================================ */

@media (max-width: 576px) {

  /* Even more compact */
  #app {
    padding: 0.5rem !important;
  }

  main#app {
    padding: 0.75rem !important;
  }

  /* Typography */
  h1 {
    font-size: 1.75rem;
  }

  h2 {
    font-size: 1.5rem;
  }

  h3 {
    font-size: 1.25rem;
  }

  h4 {
    font-size: 1.1rem;
  }

  /* Buttons - stack vertically */
  .btn-group {
    flex-direction: column;
  }

  .btn-group .btn {
    width: 100%;
    border-radius: 0.375rem !important;
    margin-bottom: 0.5rem;
  }

  .btn-group .btn:last-child {
    margin-bottom: 0;
  }

  /* Contact card even more compact */
  .contacts-table tr {
    padding: 0.75rem;
  }

  .contacts-table td:nth-child(1) {
    font-size: 1rem;
  }

  /* Form labels */
  .form-label {
    margin-bottom: 0.35rem;
    font-size: 0.9rem;
  }

  /* Compact cards */
  .card-body {
    padding: 0.75rem;
  }

  /* Settings navigation on mobile */
  .settings-nav {
    margin-left: -1rem;
    margin-right: -1rem;
    padding-left: 0.5rem;
    padding-right: 0.5rem;
  }

  .settings-nav-link {
    font-size: 0.85rem;
    padding: 0.4rem 0.75rem;
  }

  /* Fix settings save bar on mobile */
  .settings-save-bar {
    padding: 0.625rem !important;
  }

  .settings-save-bar > div {
    display: flex !important;
    gap: 0.5rem !important;
    justify-content: center !important;
    width: 100%;
  }

  .settings-save-bar .btn {
    min-width: 0 !important;
    flex: 1 !important;
    max-width: 140px !important;
    padding: 0.5rem 1rem !important;
  }

  /* Add bottom padding to forms so content isn't hidden by fixed bar */
  #previewPanel form,
  .contact-edit-form {
    padding-bottom: 80px !important;
  }

  /* Delete button - center it below Update/Cancel */
  .settings-save-bar #deleteContactBtn {
    display: block !important;
    width: 100% !important;
    max-width: 280px !important;
    margin: 0.5rem auto 0 !important;
    padding: 0.5rem 1rem !important;
  }

  /* Smaller badges */
  .badge {
    font-size: 0.75rem;
    padding: 0.25em 0.5em;
  }

  /* Two-column layout for small items */
  .row.g-3 > .col-6 {
    padding-left: 0.5rem;
    padding-right: 0.5rem;
  }

  /* Hide certain UI elements on very small screens */
  .btn .bi + span {
    /* Hide button text, keep icons */
    display: none;
  }

  .btn-expandable .bi + span {
    display: none;
  }

  /* But keep text for primary actions */
  .btn-primary span,
  .btn-success span,
  .btn-danger span {
    display: inline;
  }
}

/* ============================================
   LANDSCAPE PHONE: 568px - 768px
   ============================================ */

@media (min-width: 568px) and (max-width: 768px) and (orientation: landscape) {

  /* Optimize for landscape phones */
  .modal-dialog {
    max-width: 90%;
  }

  #reminderPanel,
  #interactionPanel,
  #previewPanel {
    width: 50% !important;
    right: auto !important;
  }

  .floating-modal {
    max-width: 80% !important;
    left: 10% !important;
    right: 10% !important;
  }
}

/* ============================================
   TOUCH IMPROVEMENTS (All mobile)
   ============================================ */

@media (hover: none) and (pointer: coarse) {

  /* Larger tap targets - but exclude form checkboxes/radios */
  a, button, select {
    min-height: 44px;
  }

  /* Form checkboxes and radios should stay normal size */
  input[type="checkbox"],
  input[type="radio"] {
    min-height: unset !important;
    min-width: unset !important;
    width: 1.125rem !important;
    height: 1.125rem !important;
  }

  /* Bootstrap form-check styling */
  .form-check-input {
    width: 1.125rem !important;
    height: 1.125rem !important;
    min-width: unset !important;
    min-height: unset !important;
  }

  /* Increase clickable area for icon buttons */
  .btn-sm {
    min-height: 40px;
  }

  /* Quill editor toolbar buttons - make them touch-friendly */
  .ql-toolbar button {
    min-width: 32px !important;
    min-height: 32px !important;
    width: 32px !important;
    height: 32px !important;
    padding: 4px !important;
  }

  .ql-toolbar button svg {
    width: 18px !important;
    height: 18px !important;
  }

  .ql-toolbar .ql-picker {
    min-height: 32px !important;
    height: 32px !important;
  }

  .ql-toolbar .ql-picker-label {
    padding: 4px 8px !important;
    min-height: 32px !important;
  }

  /* Better touch feedback */
  .btn:active {
    transform: scale(0.97);
  }

  .nav-link:active,
  .list-group-item:active {
    background-color: rgba(0,0,0,0.05);
  }

  /* Remove hover effects that don't work on touch */
  .btn:hover {
    transform: none;
  }

  /* Improve form controls */
  input[type="text"],
  input[type="email"],
  input[type="password"],
  input[type="tel"],
  input[type="number"],
  input[type="url"],
  textarea,
  select {
    font-size: 16px !important; /* Prevents iOS zoom */
  }
}

/* ============================================
   DARK MODE MOBILE ADJUSTMENTS
   ============================================ */

@media (max-width: 768px) and (prefers-color-scheme: dark) {

  .contacts-table tr {
    background: #1a1a1a;
    border-color: #333;
  }

  #reminderPanel,
  #interactionPanel,
  #previewPanel,
  .floating-modal {
    background: #1a1a1a;
    color: #fff;
  }

  .sticky-top {
    background: #1a1a1a;
    border-color: #333;
  }
}
