/* ==========================================================================
   Chrono AI — mobile polish layer
   Loaded after style.css. Everything here is scoped to small / touch
   screens, so the desktop layout is untouched.
   ========================================================================== */

/* ---------- Global touch behaviour (all touch devices) ---------- */
@media (hover: none), (pointer: coarse) {
  html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
  a, button, [role="button"], label, summary { -webkit-tap-highlight-color: transparent; touch-action: manipulation; }
  /* Native pickers / selects follow the dark theme instead of flashing white */
  input, select, textarea { color-scheme: dark; }
}

@media (max-width: 900px) {
  /* ---------- Form controls ----------
     iOS Safari zooms the whole page when a focused field is < 16px.
     16px stops that jump and makes typing on a phone comfortable. */
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]):not([type="color"]),
  select,
  textarea {
    font-size: 16px !important;
  }
  textarea { font-family: inherit !important; }
  select { color-scheme: dark; }
  select option { background: #16161a; color: #f4f4f5; }

  /* ---------- Top bar ---------- */
  .topbar {
    padding: 0 14px !important;
    padding-top: env(safe-area-inset-top) !important;
    gap: 10px !important;
    height: calc(64px + env(safe-area-inset-top));
  }
  .topbar-search { flex: 1 1 auto; min-width: 0; position: relative; }
  .topbar-search input { min-width: 0; width: 100%; text-overflow: ellipsis; }
  .topbar-search .kbd-hint { display: none !important; }   /* ⌘K means nothing on a phone */
  .topbar-actions { flex-shrink: 0; gap: 8px; }
  .topbar .icon-btn, .topbar .avatar-wrap { min-width: 40px; min-height: 40px; }

  /* ---------- Scroll area ---------- */
  main.main {
    /* room for the floating assistant button + iPhone home bar */
    padding-bottom: calc(96px + env(safe-area-inset-bottom)) !important;
    overscroll-behavior-y: contain;
    -webkit-overflow-scrolling: touch;
  }

  /* ---------- Readability: nothing below ~11px on a phone ---------- */
  .nav-group-label { font-size: 11px !important; }
  .modal-body label,
  .modal-card label,
  .cal-modal label,
  .cal-modal-label { font-size: 11.5px !important; }
  .empty-sub, .section-sub { font-size: 12px; }

  /* ---------- Floating assistant button ----------
     Hide it while a sheet / modal / drawer is open so it never covers
     “Save”, “Cancel” or form fields. */
  body:has(.modal-backdrop.open) #chatbotLauncher,
  body:has(.cal-modal-backdrop.open) #chatbotLauncher,
  body:has(.sidebar.mobile-open) #chatbotLauncher,
  body.m-sheet-open #chatbotLauncher {
    opacity: 0 !important;
    pointer-events: none !important;
    transform: scale(.6) !important;
  }
  #chatbotLauncher {
    bottom: calc(16px + env(safe-area-inset-bottom)) !important;
    transition: opacity .18s ease, transform .18s ease;
  }

  /* ---------- Global search results (added by mobile.js) ---------- */
  .m-search-results {
    position: absolute;
    top: calc(100% + 8px);
    left: 0;
    right: 0;
    min-width: 240px;
    max-height: min(60vh, 420px);
    overflow-y: auto;
    background: #17171b;
    border: 1px solid rgba(255,255,255,.08);
    border-radius: 14px;
    box-shadow: 0 18px 40px rgba(0,0,0,.55);
    padding: 6px;
    z-index: 400;
  }
}

/* Global search dropdown is also useful on desktop */
.m-search-results { display: none; }
.m-search-results.open { display: block; }
@media (min-width: 901px) {
  .m-search-results {
    position: absolute; top: calc(100% + 8px); left: 0; right: 0;
    max-height: 420px; overflow-y: auto;
    background: #17171b; border: 1px solid rgba(255,255,255,.08);
    border-radius: 14px; box-shadow: 0 18px 40px rgba(0,0,0,.55);
    padding: 6px; z-index: 400;
  }
  .topbar-search { position: relative; }
}
.m-search-item {
  display: flex; align-items: center; gap: 10px;
  width: 100%; padding: 11px 12px; min-height: 44px;
  border: 0; border-radius: 10px; background: transparent;
  color: #e4e4e7; font: inherit; font-size: 14px; text-align: left; cursor: pointer;
}
.m-search-item small { margin-left: auto; color: #71717a; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; }
.m-search-item:hover, .m-search-item.active { background: rgba(255,255,255,.06); }
.m-search-empty { padding: 14px 12px; color: #71717a; font-size: 13px; }

/* ==========================================================================
   Phones
   ========================================================================== */
@media (max-width: 640px) {

  /* ---------- Dashboard: “My Projects” rows ----------
     The row was a single flex line, so the name column collapsed to 0px
     and the status badge sat on top of the progress label. Stack it. */
  .my-proj-row {
    flex-wrap: wrap !important;
    align-items: center !important;
    gap: 10px 12px !important;
    padding-top: 14px !important;
    padding-bottom: 14px !important;
  }
  .my-proj-info { flex: 1 1 100% !important; min-width: 0 !important; width: 100% !important; }
  .my-proj-name { flex-wrap: wrap; gap: 6px 8px; min-width: 0; overflow-wrap: anywhere; }
  .my-proj-client { overflow-wrap: anywhere; }
  .my-proj-progress-wrap { flex: 1 1 100% !important; width: 100% !important; min-width: 0 !important; }
  .my-proj-deadline { flex: 1 1 auto !important; margin: 0 !important; text-align: left !important; }
  .my-proj-row > .btn-secondary { flex: 0 0 auto !important; min-height: 40px; }

  /* ---------- Dashboard: analytics chart ----------
     Month labels used a fixed gap and ran past the card (Nov/Dec were cut
     off). Give each label the same flexible column as its bar. */
  .analytics-controls { flex-wrap: wrap; gap: 8px !important; }
  .analytics-controls .seg-btn, .analytics-controls .dropdown-btn { min-height: 36px; }
  .chart-bars { gap: 4px !important; }
  .chart-xaxis { gap: 4px !important; }
  .chart-xaxis > * {
    flex: 1 1 0 !important;
    min-width: 0 !important;
    text-align: center;
    font-size: 9.5px !important;
    letter-spacing: -.02em;
    overflow: hidden;
    white-space: nowrap;
  }

  /* ---------- Tables → stacked cards (labels added by mobile.js) ---------- */
  table.m-cards { min-width: 0 !important; width: 100% !important; border-collapse: separate; border-spacing: 0; }
  table.m-cards thead { display: none !important; }
  table.m-cards, table.m-cards tbody { display: block !important; }
  table.m-cards tr {
    display: block !important;
    padding: 12px 14px;
    border-bottom: 1px solid rgba(255,255,255,.06);
  }
  table.m-cards tr:last-child { border-bottom: 0; }
  table.m-cards td {
    display: flex !important;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    width: auto !important;
    max-width: none !important;
    padding: 5px 0 !important;
    border: 0 !important;
    text-align: right !important;
    white-space: normal !important;
    overflow-wrap: anywhere;
    min-height: 28px;
  }
  table.m-cards td::before {
    content: attr(data-label);
    flex: 0 0 auto;
    max-width: 45%;
    color: #71717a;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: .06em;
    text-transform: uppercase;
    text-align: left;
  }
  table.m-cards td[data-label=""]::before,
  table.m-cards td:not([data-label])::before { content: none; }
  table.m-cards td:first-child {
    justify-content: flex-start;
    text-align: left !important;
    font-weight: 600;
    font-size: 14px;
  }
  table.m-cards td:first-child::before { content: none; }
  /* empty-state row (“No tasks yet — add one”) spans the table */
  table.m-cards td[colspan] {
    display: block !important;
    text-align: center !important;
    font-weight: 400;
    padding: 22px 8px !important;
  }
  table.m-cards td[colspan]::before { content: none; }
  table.m-cards td button, table.m-cards td .icon-btn { min-width: 34px; min-height: 34px; }
  .table-wrap:has(table.m-cards) { overflow-x: visible !important; }

  /* ---------- Modals → bottom sheets ---------- */
  .modal-backdrop,
  .cal-modal-backdrop {
    align-items: flex-end !important;
    justify-content: center !important;
    padding: 0 !important;
  }
  .modal-backdrop > .modal-card,
  .modal-backdrop > .modal-box,
  .cal-modal-backdrop > .cal-modal {
    width: 100% !important;
    max-width: 100% !important;
    max-height: calc(100dvh - 24px - env(safe-area-inset-top)) !important;
    margin: 0 !important;
    border-radius: 20px 20px 0 0 !important;
    border-bottom: 0 !important;
    padding-bottom: env(safe-area-inset-bottom);
    display: flex;
    flex-direction: column;
  }
  .modal-backdrop.open > .modal-card,
  .modal-backdrop.open > .modal-box,
  .cal-modal-backdrop.open > .cal-modal {
    animation: m-sheet-up .26s cubic-bezier(.2,.8,.2,1) both;
  }
  .modal-backdrop > .modal-card::before,
  .modal-backdrop > .modal-box::before,
  .cal-modal-backdrop > .cal-modal::before {
    content: "";
    display: block;
    flex: 0 0 auto;
    width: 38px; height: 4px;
    margin: 8px auto 0;
    border-radius: 4px;
    background: rgba(255,255,255,.18);
  }
  .modal-body, .cal-modal-body {
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    flex: 1 1 auto;
    min-height: 0;
  }
  .modal-foot, .cal-modal-footer {
    flex-shrink: 0;
    gap: 10px !important;
    flex-wrap: wrap;
  }
  .modal-foot > .btn-primary, .modal-foot > .btn-secondary, .modal-foot > button,
  .cal-modal-footer > button {
    flex: 1 1 0;
    min-height: 46px;
    justify-content: center;
  }
  .modal-grid-2, .cal-modal-grid2 { grid-template-columns: 1fr !important; }
  .modal-close, .cal-modal-close { min-width: 40px; min-height: 40px; }

  /* ---------- Bigger tap targets ---------- */
  .btn-primary, .btn-secondary { min-height: 40px; }
  .seg-btn { min-height: 36px; }
  .icon-btn { min-width: 38px; min-height: 38px; }
  .cal-nav-btn,
  [aria-label="Previous month"], [aria-label="Next month"] { min-width: 36px !important; min-height: 36px !important; }
  .dropdown-menu li, .dropdown-menu button, .dropdown-menu a { min-height: 42px; display: flex; align-items: center; }

  /* Date picker: comfortable day cells */
  .cz-datepicker { max-width: calc(100vw - 24px); }
  .cz-datepicker button { min-height: 34px; }

  /* ---------- Settings ---------- */
  .settings-wrap { overflow-x: hidden !important; }
  .s-info-row { min-width: 0; }
  .s-info-row > * { min-width: 0; }
  .s-info-value { overflow-wrap: anywhere; word-break: break-word; min-width: 0; text-align: right; }

  /* Long names / emails never push cards wider than the screen */
  .section-card, .stat-card, .kpi-card, .doc-card, .proj-card { min-width: 0; }
  .section-title, .section-sub { overflow-wrap: anywhere; }
}

@keyframes m-sheet-up {
  from { transform: translateY(40px); opacity: 0; }
  to   { transform: translateY(0);    opacity: 1; }
}

/* ==========================================================================
   Calendar (≤ 900px): side panels become proper slide-over sheets
   ========================================================================== */
/* mobile-only controls stay hidden on desktop */
.m-cal-actions, .m-cal-backdrop { display: none; }
@media (max-width: 900px) {
  /* the 16px edge “handles” were the only way in — replaced by the
     Create / Filters / Schedule buttons that mobile.js adds */
  .cal-page .cal-handle { display: none !important; }
  .cal-page { position: relative; }
  .m-cal-actions {
    display: flex;
    gap: 8px;
    width: 100%;
    order: 99;
  }
  .m-cal-actions button {
    flex: 1 1 0;
    min-height: 38px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    border-radius: 10px;
    font: inherit;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    border: 1px solid rgba(255,255,255,.1);
    background: rgba(255,255,255,.04);
    color: #e4e4e7;
  }
  .m-cal-actions button.m-primary {
    background: var(--accent, #E55027);
    border-color: transparent;
    color: #fff;
  }
  .m-cal-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.55);
    z-index: 60;
    opacity: 0;
    pointer-events: none;
    transition: opacity .2s ease;
  }
  .m-cal-backdrop.open { opacity: 1; pointer-events: auto; }
  .cal-page.m-left-open .cal-left,
  .cal-page.m-right-open .cal-right {
    position: fixed !important;
    top: 0 !important;
    bottom: 0 !important;
    z-index: 70 !important;
    width: min(86vw, 340px) !important;
    max-width: 340px;
    height: 100dvh !important;
    overflow-y: auto !important;
    background: #141417;
    box-shadow: 0 0 40px rgba(0,0,0,.6);
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .cal-page.m-left-open .cal-left  { left: 0 !important; border-radius: 0 18px 18px 0; }
  .cal-page.m-right-open .cal-right { right: 0 !important; left: auto !important; border-radius: 18px 0 0 18px; }
  .cal-page.m-left-open .cal-left .cal-panel-inner,
  .cal-page.m-right-open .cal-right .cal-panel-inner { width: 100% !important; }
  .cal-toolbar .cal-search-wrap { flex: 1 1 0 !important; min-width: 0 !important; width: auto !important; }
  /* sheets start below the top bar so the header stays usable */
  .cal-page.m-left-open .cal-left,
  .cal-page.m-right-open .cal-right { top: calc(64px + env(safe-area-inset-top)) !important; height: auto !important; }
  .m-cal-backdrop { top: calc(64px + env(safe-area-inset-top)) !important; }
  /* keyboard-shortcut legend is meaningless on touch screens */
  .cal-page .m-touch-hide { display: none !important; }
  .cal-quickbar { overflow-x: auto; scrollbar-width: none; }
  .cal-quickbar::-webkit-scrollbar { display: none; }
}
