/**
 * Redmine Mobile Enhancements
 * Comprehensive mobile-first responsive improvements
 *
 * CONTENTS:
 *   1. Safe Area & Base
 *   2. Camera Upload & Attachment Previews
 *   3. Issue List Card Layout
 *   4. Modals (Full-screen/Bottom-sheet)
 *   5. Toasts & Timer Bar
 *   6. Flyout Menu
 *   7. Touch Targets
 *   8. Enhanced Gantt
 *   9. Kanban Board
 *  10. Dashboard & KPI
 *  11. Emoji Picker
 *  12. Form Improvements
 */

/* ==========================================================================
   1. SAFE AREA & BASE
   ========================================================================== */

/* Safe area padding for notched devices */
@supports (padding: env(safe-area-inset-bottom)) {
  #footer {
    padding-bottom: env(safe-area-inset-bottom);
  }
}

/* ==========================================================================
   2. CAMERA UPLOAD & ATTACHMENT PREVIEWS
   ========================================================================== */

/* Camera button - hidden on desktop, shown on mobile via media query below */
.mobile-camera-btn {
  display: none;
}

/* Attachment preview thumbnails */
.attachment-previews {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}

.attachment-preview-thumb {
  width: 80px;
  text-align: center;
  position: relative;
}

.attachment-preview-thumb img {
  width: 80px;
  height: 60px;
  object-fit: cover;
  border-radius: 4px;
  border: 1px solid var(--oc-gray-3, #dee2e6);
}

.attachment-preview-name {
  display: block;
  font-size: 0.7em;
  color: var(--oc-gray-6, #868e96);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 80px;
}

.attachment-preview-size {
  display: block;
  font-size: 0.65em;
  color: var(--oc-gray-5, #adb5bd);
}

/* ==========================================================================
   TABLET BREAKPOINT (768px)
   ========================================================================== */

@media screen and (max-width: 768px) {
  /* Dashboard: KPI cards 2-column */
  .kpi-cards {
    flex-wrap: wrap !important;
  }
  .kpi-card {
    flex: 1 1 calc(50% - 6px) !important;
    min-width: 0 !important;
  }

  /* Dashboard chart heights */
  .chart-container-bar,
  .chart-container-donut {
    height: 200px !important;
  }

  /* Priority bar labels */
  .priority-bar-label {
    flex: 0 0 80px !important;
  }
}

/* ==========================================================================
   MOBILE BREAKPOINT (899px) - augments existing responsive.css
   ========================================================================== */

@media screen and (max-width: 899px) {

  /* ---- Safe area for fixed header ---- */
  @supports (padding: env(safe-area-inset-top)) {
    #header {
      padding-top: env(safe-area-inset-top);
      height: calc(64px + env(safe-area-inset-top));
    }
    #main {
      padding-top: calc(64px + env(safe-area-inset-top));
    }
  }

  /* ---- Camera button visible ---- */
  .mobile-camera-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 36px;
    border: 1px solid var(--oc-gray-4, #ced4da);
    border-radius: 4px;
    background: var(--oc-gray-0, #f8f9fa);
    color: var(--oc-gray-7, #495057);
    cursor: pointer;
    vertical-align: middle;
    margin-left: 4px;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-camera-btn:active {
    background: var(--oc-gray-2, #e9ecef);
  }

  .add-attachment-label {
    display: block;
    margin-top: 4px;
    font-size: 0.8em;
    color: var(--oc-gray-6, #868e96);
  }

  /* ---- Attachment preview larger on mobile ---- */
  .attachment-preview-thumb {
    width: 72px;
  }
  .attachment-preview-thumb img {
    width: 72px;
    height: 54px;
  }

  /* ========================================================================
     3. ISSUE LIST CARD LAYOUT
     ======================================================================== */

  /* Transform issue table into cards on mobile */
  .autoscroll {
    overflow-x: visible !important;
  }

  table.list.issues {
    border-collapse: separate;
    border-spacing: 0 8px;
  }

  table.list.issues thead {
    display: none;
  }

  table.list.issues tbody tr.issue {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    padding: 10px 12px;
    margin-bottom: 0;
    background: var(--oc-white, #fff);
    border: 1px solid var(--oc-gray-2, #e9ecef);
    border-radius: 6px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
    position: relative;
    gap: 4px 8px;
  }

  table.list.issues tbody tr.issue td {
    display: inline;
    border: none;
    padding: 0;
    background: transparent !important;
    vertical-align: middle;
    line-height: 1.5;
  }

  /* Checkbox column */
  table.list.issues tbody tr.issue td.checkbox {
    position: absolute;
    top: 10px;
    right: 10px;
    width: auto;
  }

  table.list.issues tbody tr.issue td.checkbox input[type="checkbox"] {
    width: 20px;
    height: 20px;
  }

  /* ID column */
  table.list.issues tbody tr.issue td.id {
    order: 1;
    font-weight: 700;
    color: var(--oc-blue-7, #1971c2);
    font-size: 0.85em;
  }

  table.list.issues tbody tr.issue td.id a {
    color: inherit;
  }

  /* Tracker */
  table.list.issues tbody tr.issue td.tracker {
    order: 2;
    font-size: 0.75em;
    text-transform: uppercase;
    color: var(--oc-gray-6, #868e96);
    font-weight: 600;
  }

  /* Status */
  table.list.issues tbody tr.issue td.status {
    order: 3;
    font-size: 0.75em;
    background: var(--oc-gray-1, #f1f3f5) !important;
    padding: 2px 8px !important;
    border-radius: 10px;
    white-space: nowrap;
  }

  /* Priority */
  table.list.issues tbody tr.issue td.priority {
    order: 4;
    font-size: 0.75em;
    font-weight: 600;
  }

  /* Subject - full width */
  table.list.issues tbody tr.issue td.subject {
    order: 10;
    width: 100%;
    flex: 0 0 100%;
    font-size: 0.95em;
    font-weight: 500;
    padding-right: 30px !important;
    line-height: 1.4;
  }

  /* Assignee */
  table.list.issues tbody tr.issue td.assigned_to {
    order: 20;
    font-size: 0.8em;
    color: var(--oc-gray-6, #868e96);
  }

  /* Updated/Created/Due date */
  table.list.issues tbody tr.issue td.updated_on,
  table.list.issues tbody tr.issue td.created_on,
  table.list.issues tbody tr.issue td.due_date {
    order: 21;
    font-size: 0.75em;
    color: var(--oc-gray-5, #adb5bd);
  }

  /* Done ratio */
  table.list.issues tbody tr.issue td.done_ratio {
    order: 22;
  }

  table.list.issues tbody tr.issue td.done_ratio table.progress {
    width: 60px;
    display: inline-block;
    vertical-align: middle;
  }

  /* Hide less important columns on mobile */
  table.list.issues tbody tr.issue td.cf_,
  table.list.issues tbody tr.issue td.estimated_hours,
  table.list.issues tbody tr.issue td.spent_hours,
  table.list.issues tbody tr.issue td.total_estimated_hours,
  table.list.issues tbody tr.issue td.category,
  table.list.issues tbody tr.issue td.fixed_version {
    display: none;
  }

  /* Card group headers */
  table.list.issues tbody tr.group td {
    display: block;
    width: 100%;
    padding: 8px 12px;
    font-weight: 700;
    border: none;
    background: transparent;
  }

  /* ========================================================================
     4. MODALS (FULL-SCREEN / BOTTOM-SHEET)
     ======================================================================== */

  /* Pivot modal → full screen on mobile */
  .pivot-modal {
    align-items: flex-end !important;
  }

  .pivot-modal-content {
    width: 100% !important;
    max-width: 100% !important;
    max-height: 90vh !important;
    border-radius: 12px 12px 0 0 !important;
    margin: 0 !important;
  }

  .pivot-modal-header {
    position: sticky;
    top: 0;
    z-index: 1;
    border-radius: 12px 12px 0 0;
  }

  /* iCal modal → bottom sheet */
  .ical-modal {
    align-items: flex-end !important;
  }

  .ical-modal-content {
    width: 100% !important;
    max-width: 100% !important;
    max-height: 85vh !important;
    border-radius: 12px 12px 0 0 !important;
  }

  /* Generic jQuery UI modals */
  .ui-dialog {
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    top: auto !important;
    width: 100% !important;
    max-width: 100% !important;
    max-height: 90vh;
    border-radius: 12px 12px 0 0;
    margin: 0;
  }

  .ui-dialog .ui-dialog-content {
    max-height: calc(90vh - 60px);
    overflow-y: auto;
  }

  /* ========================================================================
     5. TOASTS & TIMER BAR
     ======================================================================== */

  /* Toast notifications → bottom center on mobile */
  .realtime-toast-container {
    bottom: auto !important;
    top: 70px;
    right: 8px !important;
    left: 8px;
    max-width: none !important;
  }

  .realtime-toast {
    max-width: 100% !important;
    font-size: 12px;
  }

  /* Kanban flash → repositioned */
  .kanban-flash {
    top: 70px !important;
    right: 8px !important;
    left: 8px;
    max-width: none !important;
  }

  /* Timer bar → compact on mobile */
  #timer-bar {
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }

  .timer-bar-inner {
    padding: 6px 12px;
    gap: 8px;
    font-size: 0.75rem;
  }

  /* Ensure content doesn't hide behind timer bar */
  body:has(#timer-bar) #footer {
    padding-bottom: 52px;
  }

  /* ========================================================================
     6. FLYOUT MENU - Responsive width
     ======================================================================== */

  .flyout-menu {
    inline-size: min(250px, 80vw) !important;
    inset-inline-end: calc(-1 * min(250px, 80vw)) !important;
  }

  .flyout-is-active #wrapper,
  .flyout-is-active #header {
    inset-inline-end: min(250px, 80vw) !important;
  }

  /* ========================================================================
     7. TOUCH TARGETS (44px minimum)
     ======================================================================== */

  /* Pagination buttons */
  .pagination ul.pages li a,
  .pagination ul.pages li span.current {
    min-height: 44px;
    min-width: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 8px 12px;
  }

  /* Context menu links */
  #content > .contextual > a,
  #content > .contextual .drdn,
  p.buttons a {
    min-height: 44px;
    padding: 10px 12px;
  }

  /* Issue action buttons */
  a.icon, a.icon-only {
    min-height: 44px;
    min-width: 44px;
    display: inline-flex;
    align-items: center;
  }

  /* Form checkboxes - larger touch area */
  input[type="checkbox"],
  input[type="radio"] {
    width: 20px;
    height: 20px;
  }

  /* Tab navigation items */
  #main-menu ul li a,
  .tabs ul li a {
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    padding: 8px 12px;
  }

  /* Admin menu items in flyout */
  .flyout-menu ul li a {
    min-height: 44px;
    line-height: 44px;
    block-size: 44px;
  }

  .flyout-menu ul li:first-child a {
    line-height: 43px;
    block-size: 43px;
  }

  /* Filter toggle */
  .add-filter select,
  #filters-table select {
    min-height: 36px;
  }

  /* Remove-upload button */
  .attachments_fields span a.remove-upload {
    min-width: 44px;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  /* ========================================================================
     8. ENHANCED GANTT - Collapse task panel
     ======================================================================== */

  #enhanced-gantt-container {
    font-size: 12px;
  }

  #eg-main {
    flex-direction: column !important;
    height: auto !important;
    min-height: 0 !important;
    max-height: none !important;
  }

  #eg-task-list {
    width: 100% !important;
    min-width: 0 !important;
    max-height: 40vh;
    border-right: none !important;
    border-bottom: 1px solid #ccc;
  }

  #eg-timeline {
    width: 100% !important;
    min-height: 200px;
    max-height: 50vh;
  }

  #eg-toolbar {
    flex-wrap: wrap;
    gap: 6px;
    padding: 6px 8px;
  }

  .eg-toolbar-group {
    flex-wrap: wrap;
  }

  #eg-toolbar select {
    max-width: 120px;
    font-size: 11px;
  }

  .eg-zoom-btn {
    min-width: 36px;
    min-height: 36px;
    padding: 4px 8px;
  }

  /* Gantt task header */
  #eg-task-header .eg-th {
    font-size: 10px;
    padding: 3px 4px;
  }

  /* Resize handle */
  #eg-resize-handle {
    display: none !important;
  }

  /* ========================================================================
     9. KANBAN BOARD - Stacked on small phones
     ======================================================================== */

  .kanban-board {
    gap: 8px;
    padding-bottom: 12px;
  }

  .kanban-column {
    flex: 0 0 220px !important;
    min-width: 220px !important;
    max-width: 220px !important;
    max-height: calc(100vh - 160px);
  }

  .kanban-card {
    padding: 8px 10px;
  }

  .kanban-card-subject {
    font-size: 0.825em;
  }

  /* ========================================================================
     10. DASHBOARD & KPI
     ======================================================================== */

  .dashboard-row {
    flex-direction: column !important;
  }

  .dashboard-widget-half,
  .dashboard-widget-third {
    flex: 1 1 100% !important;
  }

  .time-summary-grid {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  .widget-scrollable .widget-body {
    max-height: 300px;
  }

  /* ========================================================================
     11. EMOJI PICKER
     ======================================================================== */

  .emoji-picker-popup {
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    top: auto !important;
    min-width: 100% !important;
    max-width: 100% !important;
    border-radius: 12px 12px 0 0 !important;
    transform: none !important;
    max-height: 50vh;
    overflow-y: auto;
  }

  .emoji-picker-popup.emoji-picker-visible {
    transform: none !important;
  }

  .emoji-grid {
    grid-template-columns: repeat(8, 1fr) !important;
  }

  /* ========================================================================
     12. FORM IMPROVEMENTS
     ======================================================================== */

  /* Issue attributes: stack labels on mobile */
  .attribute {
    padding-inline-start: 0 !important;
  }

  .attribute .label {
    inline-size: auto !important;
    margin-inline-start: 0 !important;
    float: none !important;
    display: block;
    font-weight: 700;
    margin-bottom: 2px;
  }

  .attribute .value {
    display: block;
    margin-bottom: 8px;
  }

  /* Wider file input on mobile */
  .file_selector {
    max-width: calc(100% - 60px);
  }

  /* Add attachment area - better layout */
  .add_attachment {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 4px;
    padding: 8px 0;
  }

  /* Scroll hint for tables still using autoscroll */
  .autoscroll:not(:has(table.list.issues))::after {
    content: 'Scroll \2192';
    display: block;
    text-align: center;
    font-size: 0.75em;
    color: var(--oc-gray-5, #adb5bd);
    padding: 4px;
  }
}

/* ==========================================================================
   SMALL PHONE BREAKPOINT (480px)
   ========================================================================== */

@media screen and (max-width: 480px) {

  /* KPI cards single column */
  .kpi-card {
    flex: 1 1 100% !important;
  }

  /* Dashboard charts reduce height further */
  .chart-container-bar,
  .chart-container-donut {
    height: 180px !important;
  }

  /* Kanban single column swipe */
  .kanban-column {
    flex: 0 0 calc(100vw - 32px) !important;
    min-width: calc(100vw - 32px) !important;
    max-width: calc(100vw - 32px) !important;
    scroll-snap-align: start;
  }

  .kanban-board {
    scroll-snap-type: x mandatory;
    -webkit-overflow-scrolling: touch;
  }

  /* Issue card layout refinement */
  table.list.issues tbody tr.issue {
    padding: 8px 10px;
  }

  table.list.issues tbody tr.issue td.subject {
    font-size: 0.9em;
  }

  /* Sticky issue header: reduce height */
  div#sticky-issue-header {
    font-size: 0.85em;
  }

  /* Time summary: single column */
  .time-summary-grid {
    grid-template-columns: 1fr !important;
  }

  /* Announcement banner: compact */
  .announcement-banner {
    padding: 6px 10px !important;
    font-size: 0.8em !important;
  }

  /* Flyout menu: full width on small phones */
  .flyout-menu {
    inline-size: 80vw !important;
    inset-inline-end: -80vw !important;
  }

  .flyout-is-active #wrapper,
  .flyout-is-active #header {
    inset-inline-end: 80vw !important;
  }

  /* Calendar: show day abbreviations */
  .cal .calhead {
    display: block !important;
    font-size: 0.7em;
    text-align: center;
  }

  /* Attachment previews smaller */
  .attachment-preview-thumb {
    width: 60px;
  }
  .attachment-preview-thumb img {
    width: 60px;
    height: 45px;
  }

  /* Form buttons: full width */
  input[type="submit"],
  button.btn {
    width: 100%;
    min-height: 44px;
    margin-bottom: 8px;
  }
}

/* ==========================================================================
   LANDSCAPE MODE ADJUSTMENTS
   ========================================================================== */

@media screen and (max-width: 899px) and (orientation: landscape) {
  /* Reduce header height in landscape */
  #header {
    block-size: 48px !important;
  }

  .mobile-toggle-button {
    block-size: 48px !important;
    line-height: 48px !important;
  }

  #project-jump.drdn {
    block-size: 48px !important;
  }

  #project-jump .drdn-trigger {
    block-size: 38px !important;
    line-height: 28px !important;
    font-size: 1.2em;
  }

  #main {
    padding-block-start: 48px !important;
  }

  div#sticky-issue-header {
    inset-block-start: 48px !important;
  }

  /* Enhanced Gantt: side-by-side in landscape */
  #eg-main {
    flex-direction: row !important;
  }

  #eg-task-list {
    width: 40% !important;
    max-height: none;
    border-bottom: none;
    border-right: 1px solid #ccc !important;
  }

  #eg-timeline {
    max-height: none;
  }
}

/* ==========================================================================
   DARK MODE MOBILE FIXES
   ========================================================================== */

@media screen and (max-width: 899px) {
  html[data-theme="dark"] table.list.issues tbody tr.issue {
    background: var(--oc-gray-9, #212529);
    border-color: var(--oc-gray-7, #495057);
  }

  html[data-theme="dark"] table.list.issues tbody tr.issue td.status {
    background: var(--oc-gray-8, #343a40) !important;
    color: var(--oc-gray-3, #dee2e6);
  }

  html[data-theme="dark"] .mobile-camera-btn {
    background: var(--oc-gray-8, #343a40);
    border-color: var(--oc-gray-6, #868e96);
    color: var(--oc-gray-2, #e9ecef);
  }

  html[data-theme="dark"] .attachment-preview-thumb img {
    border-color: var(--oc-gray-7, #495057);
  }

  html[data-theme="dark"] .pivot-modal-content,
  html[data-theme="dark"] .ical-modal-content {
    background: var(--oc-gray-9, #212529);
  }

  html[data-theme="dark"] .kanban-column {
    background-color: var(--oc-gray-8, #343a40);
  }
}
