/* CRM V94 mobile dashboard refinement. Desktop and laptop remain unchanged. */
@media (max-width: 767px) {
  :root {
    --crm-v94-accent: #ff6a42;
    --crm-v94-line: #d9e3ee;
    --crm-v94-surface: #ffffff;
    --crm-v94-text: #172b45;
    --crm-v94-muted: #6f829a;
  }

  /* Top bar: menu, full page title, refresh, add, account. */
  #topbar {
    grid-template-columns: 42px minmax(0, 1fr) 42px 42px 42px !important;
    gap: 6px !important;
    padding: 7px 9px !important;
  }

  #topbar .page-title,
  #page-title-text {
    grid-column: 2 !important;
    width: 100% !important;
    min-width: 0 !important;
    max-width: none !important;
    overflow: visible !important;
    text-overflow: clip !important;
    white-space: nowrap !important;
    font-size: 15px !important;
    line-height: 1.1 !important;
    letter-spacing: -.01em !important;
  }

  #topbar #crm-refresh-btn {
    display: inline-flex !important;
    grid-column: 3 !important;
    grid-row: 1 !important;
    width: 42px !important;
    min-width: 42px !important;
    height: 42px !important;
    min-height: 42px !important;
    padding: 0 !important;
    margin: 0 !important;
    align-items: center !important;
    justify-content: center !important;
    border: 1px solid var(--crm-mobile-simple-line, var(--crm-v94-line)) !important;
    border-radius: 12px !important;
    background: #ffffff !important;
    color: #465b75 !important;
    box-shadow: none !important;
    font-size: 0 !important;
  }

  #topbar #crm-refresh-btn svg {
    width: 19px !important;
    height: 19px !important;
    fill: none !important;
    stroke: currentColor !important;
    stroke-width: 1.8 !important;
    stroke-linecap: round !important;
    stroke-linejoin: round !important;
  }

  #topbar .hub-quick-add {
    grid-column: 4 !important;
  }

  #topbar .hub-account-chip {
    grid-column: 5 !important;
  }

  /* The dashboard refresh now lives in the top bar. */
  #page-dashboard > .section-header > button {
    display: none !important;
  }

  /* Compact greeting block. */
  #page-dashboard > .section-header {
    display: block !important;
    min-height: 0 !important;
    padding: 12px 14px !important;
    margin-bottom: 8px !important;
    border-radius: 15px !important;
  }

  #page-dashboard > .section-header #dashboard-greeting {
    margin: 0 !important;
    font-size: 18px !important;
    line-height: 1.2 !important;
    font-weight: 900 !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
  }

  #page-dashboard > .section-header #dash-date {
    margin-top: 3px !important;
    font-size: 9.5px !important;
    line-height: 1.2 !important;
  }

  /* Four compact statistics in one row. */
  #page-dashboard #metrics-row.grid4 {
    display: grid !important;
    grid-template-columns: repeat(4, minmax(0, 1fr)) !important;
    gap: 6px !important;
    width: 100% !important;
    margin: 0 0 8px !important;
  }

  #page-dashboard #metrics-row .metric-card {
    position: relative !important;
    min-width: 0 !important;
    min-height: 84px !important;
    padding: 9px 7px 8px 10px !important;
    display: grid !important;
    grid-template-columns: 22px minmax(0, 1fr) !important;
    grid-template-rows: auto 1fr !important;
    column-gap: 5px !important;
    row-gap: 4px !important;
    align-items: center !important;
    border-radius: 13px !important;
    overflow: hidden !important;
  }

  #page-dashboard #metrics-row .metric-card::before {
    content: '' !important;
    position: absolute !important;
    left: 0 !important;
    top: 10px !important;
    bottom: 10px !important;
    width: 3px !important;
    border-radius: 0 4px 4px 0 !important;
    background: #3b82f6 !important;
  }

  #page-dashboard #metrics-row .metric-card:nth-child(2)::before { background: #22a06b !important; }
  #page-dashboard #metrics-row .metric-card:nth-child(3)::before { background: #ef476f !important; }
  #page-dashboard #metrics-row .metric-card:nth-child(4)::before { background: #d99113 !important; }

  #page-dashboard #metrics-row .metric-icon {
    grid-column: 1 !important;
    grid-row: 1 !important;
    width: 22px !important;
    min-width: 22px !important;
    height: 22px !important;
    min-height: 22px !important;
    margin: 0 !important;
    border-radius: 7px !important;
    font-size: 0 !important;
    box-shadow: none !important;
  }

  #page-dashboard #metrics-row .metric-icon svg {
    width: 14px !important;
    height: 14px !important;
    fill: none !important;
    stroke: currentColor !important;
    stroke-width: 1.9 !important;
    stroke-linecap: round !important;
    stroke-linejoin: round !important;
  }

  #page-dashboard #metrics-row .metric-card:nth-child(1) .metric-icon { background: rgba(59,130,246,.13) !important; color: #2563eb !important; }
  #page-dashboard #metrics-row .metric-card:nth-child(2) .metric-icon { background: rgba(34,160,107,.13) !important; color: #16865a !important; }
  #page-dashboard #metrics-row .metric-card:nth-child(3) .metric-icon { background: rgba(239,71,111,.13) !important; color: #d92d5c !important; }
  #page-dashboard #metrics-row .metric-card:nth-child(4) .metric-icon { background: rgba(217,145,19,.14) !important; color: #b97808 !important; }

  #page-dashboard #metrics-row .metric-val {
    grid-column: 2 !important;
    grid-row: 1 !important;
    min-width: 0 !important;
    margin: 0 !important;
    font-size: 19px !important;
    line-height: 1 !important;
    font-weight: 900 !important;
    white-space: nowrap !important;
  }

  #page-dashboard #metrics-row .metric-label {
    grid-column: 1 / -1 !important;
    grid-row: 2 !important;
    align-self: start !important;
    margin: 0 !important;
    font-size: 8.5px !important;
    line-height: 1.25 !important;
    font-weight: 800 !important;
    text-align: left !important;
    overflow-wrap: anywhere !important;
  }

  /* Pull the useful schedule higher and make rows readable. */
  #page-dashboard > .grid2.mt16 {
    margin-top: 8px !important;
  }

  #page-dashboard > .grid2.mt16 > .card:first-child {
    border-radius: 15px !important;
  }

  #page-dashboard > .grid2.mt16 > .card:first-child .card-header {
    min-height: 50px !important;
    padding: 11px 12px !important;
    align-items: center !important;
  }

  #page-dashboard > .grid2.mt16 > .card:first-child .card-title {
    font-size: 16px !important;
  }

  #page-dashboard > .grid2.mt16 > .card:first-child .card-subtitle {
    margin-top: 2px !important;
    font-size: 9.5px !important;
  }

  #page-dashboard > .grid2.mt16 > .card:first-child .card-header .btn {
    min-height: 34px !important;
    height: 34px !important;
    padding: 0 11px !important;
    border-radius: 10px !important;
    font-size: 10.5px !important;
  }

  #page-dashboard #todays-schedule {
    padding: 0 48px 0 12px !important;
  }

  #page-dashboard #todays-schedule .crm-v94-schedule-row {
    display: grid !important;
    grid-template-columns: 38px 30px minmax(0, 1fr) auto !important;
    gap: 8px !important;
    align-items: center !important;
    min-height: 62px !important;
    padding: 9px 0 !important;
  }

  #page-dashboard #todays-schedule .crm-v94-schedule-time {
    width: auto !important;
    min-width: 0 !important;
    font-size: 10px !important;
    line-height: 1.2 !important;
    white-space: nowrap !important;
  }

  #page-dashboard #todays-schedule .crm-v94-schedule-avatar {
    width: 30px !important;
    min-width: 30px !important;
    height: 30px !important;
    min-height: 30px !important;
    font-size: 9px !important;
  }

  #page-dashboard #todays-schedule .crm-v94-schedule-info {
    min-width: 0 !important;
    width: 100% !important;
  }

  #page-dashboard #todays-schedule .crm-v94-schedule-patient {
    font-size: 12.5px !important;
    line-height: 1.3 !important;
    font-weight: 850 !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
  }

  #page-dashboard #todays-schedule .crm-v94-schedule-meta {
    margin-top: 3px !important;
    max-width: 100% !important;
    font-size: 10px !important;
    line-height: 1.35 !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
  }

  #page-dashboard #todays-schedule .crm-v94-schedule-status {
    max-width: 64px !important;
    padding: 5px 7px !important;
    font-size: 8.5px !important;
    line-height: 1.15 !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
  }

  /* AI launcher has a dedicated gutter and no longer covers appointment text. */
  #page-dashboard ~ .crm-ai-fab,
  .crm-ai-fab {
    right: 10px !important;
    bottom: calc(72px + env(safe-area-inset-bottom)) !important;
    width: 40px !important;
    min-width: 40px !important;
    height: 40px !important;
    min-height: 40px !important;
    border-radius: 12px !important;
  }

  .crm-ai-fab span {
    font-size: 16px !important;
  }

  /* Bottom navigation state is clear without oversized blocks. */
  #mobile-bottom-nav .mbn-item.active {
    background: rgba(255,106,66,.10) !important;
    color: var(--crm-v94-accent) !important;
    box-shadow: inset 0 3px 0 var(--crm-v94-accent) !important;
  }

  #mobile-bottom-nav .mbn-item:not(.active) {
    box-shadow: none !important;
  }

  body.crm-night-mode #topbar #crm-refresh-btn {
    background: #202832 !important;
    border-color: #445160 !important;
    color: #dbe5ef !important;
  }

  body.crm-night-mode #page-dashboard #metrics-row .metric-card,
  body.crm-night-mode #page-dashboard > .section-header,
  body.crm-night-mode #page-dashboard > .grid2.mt16 > .card:first-child {
    background: #111820 !important;
    border-color: #3c4856 !important;
  }

  body.crm-night-mode #page-dashboard #metrics-row .metric-val,
  body.crm-night-mode #page-dashboard #metrics-row .metric-label,
  body.crm-night-mode #page-dashboard #todays-schedule .crm-v94-schedule-patient {
    color: #edf3f9 !important;
  }
}
